scripts.step_2_simulate_derepression¶
Python port of the R 'REV (derepression)' step.
Pipeline: 1) NFC background from fcs_files/NFC → mBFP_neg, mmCherry_neg 2) Time-course medians from fcs_files/time-course_data with gating & background subtraction 3) Parse filenames → plasmid, exp, rep, time 4) REV (exp == "Rev"): compute fc.cherry (relative to SP411 at 150h) and norm.bfp (min-max via time 0 vs >10h) 5) Fit CasRx (SP411) mCherry half-life → alpha, write parameters/alphamcherry.csv 6) Load parameter CSVs (half_times_downregulation/upregulation, Hill_parameters, alphamcherry) 7) Simulate ODEs for each plasmid (SP411/430/430A/428/427), plot R (tagBFP proxy) & Y (mCherry) 8) Delay scan (0..25h, step 0.5) by MAE on mCherry vs mean per time; write MAE plots; run delayed sims 9) Save mCherry/tagBFP plots; write parameters/delays_derepression.csv
apply_boundary_gate ¶
apply_boundary_gate(df)
Apply coarse FSC/SSC rectangle gate; if empty, fall back to raw.
Parameters¶
df : pd.DataFrame Events with FSC/SSC channels.
Returns¶
pd.DataFrame Boundary-gated events (or original if gate yields 0).
Source code in scripts/step_2_simulate_derepression.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
apply_singlet_gate ¶
apply_singlet_gate(df)
Keep singlets using FSC-H/FSC-A ratio; if empty, fall back to input.
Parameters¶
df : pd.DataFrame Events with FSC-H and FSC-A.
Returns¶
pd.DataFrame Singlet-gated events (or input if gate yields 0).
Source code in scripts/step_2_simulate_derepression.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
compute_nfc_background ¶
compute_nfc_background(nfc_dir)
NFC background from first 3 medians (match R logic).
Parameters¶
nfc_dir : str Path to NFC control folder.
Returns¶
(float, float) (mBFP_neg, mmCherry_neg) background medians.
Source code in scripts/step_2_simulate_derepression.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
compute_rev_transforms ¶
compute_rev_transforms(df)
Compute Rev-only transforms
• fc.cherry = PE-A / mean(PE-A of SP411 at t=150) • norm.bfp = (BFP - mean.init) / (mean.final - mean.init)
Parameters¶
df : pd.DataFrame Full time-course with background-subtracted BFP/mCherry.
Returns¶
pd.DataFrame Rev-only table with columns fc.cherry and norm.bfp added.
Raises¶
RuntimeError If Rev data or SP411@150 reference is missing.
Source code in scripts/step_2_simulate_derepression.py
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 | |
delay_scan ¶
delay_scan(pl_df, pars, tmax=150.0, step=0.005)
Scan delays (0..25h) to minimize MAE between simulated Y and mean observed mCherry.
Parameters¶
pl_df : pd.DataFrame Rev data for one plasmid with 'time','fc.cherry','norm.bfp'. pars : dict Parameters dict with keys 't_down','K','n','alpha'. tmax : float, optional Simulation horizon, by default 150.0. step : float, optional Simulation time step, by default 0.005.
Returns¶
(pd.DataFrame, float|None, float|None, pd.DataFrame|None) (mae_table, best_delay, best_mae, shifted_sim_at_best)
Source code in scripts/step_2_simulate_derepression.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 | |
exp_recovery ¶
exp_recovery(t, t12, y0, yf)
Exponential recovery curve: y(t) = yf + (y0 - yf) * exp(-t * ln2 / t12)
Parameters¶
t : array-like Time values (hours). t12 : float Half-time. y0 : float Initial value at t=0. yf : float Final asymptote.
Returns¶
np.ndarray Modeled y(t).
Source code in scripts/step_2_simulate_derepression.py
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
fit_alpha_from_casrx ¶
fit_alpha_from_casrx(REV)
Fit mCherry half-life from SP411 Rev data and convert to alpha=ln2/t12.
Parameters¶
REV : pd.DataFrame Rev-only table with columns ['plasmid','time','fc.cherry'].
Returns¶
float Alpha (1/h). Also writes alphamcherry.csv (rounded to 3 decimals).
Raises¶
RuntimeError If SP411 records are missing.
Source code in scripts/step_2_simulate_derepression.py
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
load_flowset_medians ¶
load_flowset_medians(folder)
Load all FCS files in a folder and compute gated medians.
Parameters¶
folder : str Directory containing .fcs files.
Returns¶
pd.DataFrame One row per file with medians + '__filename'.
Raises¶
FileNotFoundError If folder has no .fcs inputs.
Source code in scripts/step_2_simulate_derepression.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
load_parameters ¶
load_parameters()
Load fitted parameter CSVs and merge into one table; broadcast alpha.
Returns¶
pd.DataFrame Columns: plasmid, t_down, t_up, K, n, alpha
Raises¶
KeyError If required columns are missing in any input CSV.
Source code in scripts/step_2_simulate_derepression.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | |
load_timecourse_with_bg ¶
load_timecourse_with_bg(mBFP_neg, mmCherry_neg)
Load time-course medians, subtract NFC backgrounds, and attach parsed metadata.
Parameters¶
mBFP_neg : float Background for BFP. mmCherry_neg : float Background for mCherry.
Returns¶
pd.DataFrame Columns: BV421-A, PE-A, plasmid, exp, rep, time
Source code in scripts/step_2_simulate_derepression.py
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
median_channels_for_file ¶
median_channels_for_file(fpath)
Read an .fcs file, apply gates, and compute per-channel medians.
Parameters¶
fpath : str Path to .fcs file.
Returns¶
pd.Series Numeric medians and '__filename' stem.
Raises¶
ValueError If any required channel is missing.
Source code in scripts/step_2_simulate_derepression.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
p_theme ¶
p_theme()
Return a clean, classic theme.
Source code in scripts/step_2_simulate_derepression.py
597 598 599 | |
parse_name ¶
parse_name(name)
Parse filename stem to (plasmid, exp, rep, time).
Parameters¶
name : str Basename without extension.
Returns¶
(str, str, str, float) (plasmid, exp, rep, time)
Source code in scripts/step_2_simulate_derepression.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 | |
rhs ¶
rhs(y, t, t_down, K, n, alpha)
Right-hand side of ODE system
dR/dt = -R * ln2 / t_down dY/dt = (K^n)/(K^n + R^n) - alpha * Y
Parameters¶
y : array-like State [R, Y/alpha] (internal scaling for numerical stability). t : float Time (hours). t_down : float Half-time for R decay (hours). K : float Hill constant. n : float Hill coefficient. alpha : float mCherry degradation rate (1/h).
Returns¶
list[float] Derivatives [dR, d(Y/alpha)].
Source code in scripts/step_2_simulate_derepression.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 | |
save_mae_plot ¶
save_mae_plot(mae_df, best_delay, fname, ylim=(0, 0.3))
Save MAE vs delay scatter with highlighted best point.
Parameters¶
mae_df : pd.DataFrame Columns ['t','MAE']. best_delay : float Best delay to highlight. fname : str Output filename under OUT_PATH. ylim : tuple Y-axis limits.
Source code in scripts/step_2_simulate_derepression.py
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | |
save_scatter_with_lines ¶
save_scatter_with_lines(df_pts, x, y, lines=None, fname='plot.pdf', xlim=None, ylim=None, y_label='', x_label='Time (hours)')
Scatter of observed points with optional model lines; save to PDF.
Parameters¶
df_pts : pd.DataFrame Points to plot. x, y : str Column names for x and y. lines : list[tuple] | None Optional [(df_line, aes_y, linetype), ...]. fname : str Output filename under OUT_PATH. xlim, ylim : tuple | None Axis limits, if any. y_label, x_label : str Axis labels.
Source code in scripts/step_2_simulate_derepression.py
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 | |
simulate_ode ¶
simulate_ode(R0, Y0, pars, t0=0.0, tmax=150.0, step=0.05, delay=0.0)
Integrate ODEs and return time series for R and Y.
Notes¶
Internally integrates Y/alpha for numerical stability; rescales on output.
Parameters¶
R0 : float Initial normalized repressor level at t=0. Y0 : float Initial mCherry fold-change at t=0. pars : pd.Series | dict Parameters with keys: t_down, K, n, alpha. t0 : float, optional Start time, by default 0.0. tmax : float, optional End time, by default 150.0. step : float, optional Time step for output grid, by default 0.05. delay : float, optional Display-only shift applied to the returned 'time' column.
Returns¶
pd.DataFrame Columns: time, R, Y
Source code in scripts/step_2_simulate_derepression.py
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 | |