-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_script
More file actions
1357 lines (1180 loc) · 59.2 KB
/
Copy pathpython_script
File metadata and controls
1357 lines (1180 loc) · 59.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
167
168
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
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
247
248
249
250
251
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
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
343
344
345
346
347
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
386
387
388
389
390
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
435
436
437
438
439
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
478
479
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
526
527
528
529
530
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
592
593
594
595
596
597
598
599
600
601
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
636
637
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
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
CellMorphR-Py -- Single-Cell Morphometry Analysis
==================================================
Version 1.0.0
Publication-ready analysis of time-resolved microscopy data from
bacteriophage infection experiments.
Data format (single Excel sheet or CSV):
Condition | Time_min | Replicate | Area | Perimeter | Circularity | ...
Uninfected| 30 | 1 | 2.89 | 6.12 | 0.82 |
+N4 | 120 | 3 | 6.44 | 11.23 | 0.64 |
Required columns:
- Condition : str (e.g., "Uninfected", "+N4")
- Time_min : int (e.g., 30, 60, 90, 120)
- Replicate : int (biological replicate: 1, 2, 3)
+ at least one numeric measurement column (Area, Perimeter, etc.)
Usage:
python cellmorphr.py data.xlsx --measure Area
python cellmorphr.py data.csv --measure Area --ref Uninfected
python cellmorphr.py data.xlsx --measure Area --all-plots --save-stats
Install dependencies:
pip install pandas numpy matplotlib scipy openpyxl xlrd
Citation:
If you use CellMorphR in a publication, please cite:
[Author(s)]. CellMorphR: Single-cell morphometry analysis with
anti-pseudoreplication statistics. Version 1.0.0.
URL: https://github.com/mbaffour/CellMorphR
"""
__version__ = "1.0.0"
import argparse
import sys
import warnings
from pathlib import Path
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats
from scipy.stats import gaussian_kde # used by sina plot
# Suppress scipy precision warning when replicate values are nearly identical
warnings.filterwarnings("ignore", message="Precision loss occurred in moment calculation")
# =============================================================================
# PUBLICATION STYLE
# =============================================================================
def set_pub_style(font_size=11):
"""Configure matplotlib for publication-quality figures."""
plt.rcParams.update({
# Font
"font.family": "sans-serif",
"font.sans-serif": ["Arial", "Helvetica", "DejaVu Sans"],
"font.size": font_size,
# Axes
"axes.linewidth": 0.8,
"axes.labelsize": font_size,
"axes.titlesize": font_size + 1,
"axes.titleweight": "bold",
"axes.spines.top": False,
"axes.spines.right": False,
# Ticks
"xtick.labelsize": font_size - 1,
"ytick.labelsize": font_size - 1,
"xtick.major.width": 0.8,
"ytick.major.width": 0.8,
"xtick.major.size": 4,
"ytick.major.size": 4,
# Legend
"legend.fontsize": font_size - 1,
"legend.frameon": False,
# Figure
"figure.dpi": 150,
"savefig.dpi": 300,
"savefig.bbox": "tight",
"savefig.pad_inches": 0.1,
})
# Default color palette: blue for control, red for treatment
COLORS = {"control": "#4A90D9", "treatment": "#D94A4A"}
REPLICATE_MARKERS = ["o", "s", "^", "D", "v", "P"] # per-replicate shapes
# =============================================================================
# DATA LOADING & VALIDATION
# =============================================================================
def load_data(filepath):
"""Load Excel or CSV data and validate required columns."""
fp = Path(filepath)
if not fp.exists():
sys.exit(f"Error: File not found: {filepath}")
if fp.suffix in (".xlsx", ".xls"):
df = pd.read_excel(fp)
elif fp.suffix == ".csv":
df = pd.read_csv(fp)
elif fp.suffix == ".tsv":
df = pd.read_csv(fp, sep="\t")
else:
sys.exit(f"Error: Unsupported file type: {fp.suffix}")
# Validate required columns
required = {"Condition", "Time_min", "Replicate"}
missing = required - set(df.columns)
if missing:
sys.exit(
f"Error: Missing required columns: {missing}\n"
f"Your file has: {list(df.columns)}\n"
f"Required: Condition, Time_min, Replicate, + numeric measure columns"
)
# Identify numeric measurement columns
meta_cols = {"Condition", "Time_min", "Replicate", "Image", "Cell_ID"}
measure_cols = [c for c in df.columns if c not in meta_cols and pd.api.types.is_numeric_dtype(df[c])]
if not measure_cols:
sys.exit("Error: No numeric measurement columns found.")
df["Time_min"] = pd.to_numeric(df["Time_min"])
df["Replicate"] = df["Replicate"].astype(str)
print(f"Loaded {len(df):,} cells")
print(f"Conditions: {df['Condition'].unique()}")
print(f"Time points: {sorted(df['Time_min'].unique())}")
print(f"Replicates: {sorted(df['Replicate'].unique())}")
print(f"Measurements: {measure_cols}")
return df, measure_cols
def compute_replicate_summaries(df, measure):
"""Collapse cell-level data to per-replicate medians and means."""
grouped = df.groupby(["Condition", "Time_min", "Replicate"])[measure]
summary = grouped.agg(
median_val="median",
mean_val="mean",
std_val="std",
n_cells="count",
q25=lambda x: x.quantile(0.25),
q75=lambda x: x.quantile(0.75),
).reset_index()
return summary
def compute_condition_summaries(rep_summary, stat="median_val"):
"""Average replicate summaries to get condition-level means ± SEM."""
grouped = rep_summary.groupby(["Condition", "Time_min"])[stat]
cond = grouped.agg(
grand_mean="mean",
grand_std="std",
n_reps="count",
).reset_index()
cond["sem"] = cond["grand_std"] / np.sqrt(cond["n_reps"])
return cond
# =============================================================================
# SIGNIFICANCE BRACKET HELPER
# =============================================================================
def _add_sig_brackets(ax, pairwise_df, times, offsets, style,
df, measure, rep_summary, font_size, sig_style="stars",
show_ns=True):
"""
Draw significance brackets with Holm-corrected p-values on a distribution plot.
METHODOLOGY (documented for transparency and reviewer scrutiny):
- Test: Welch's two-sample t-test (two-sided, unequal variance assumed)
- Tested on: Per-replicate medians — the biological replicate is the
statistical unit (NOT individual cells). See Lazic (2010).
- N per group: Number of biological replicates (typically 3).
- Correction: Holm–Bonferroni step-down across all time-point comparisons,
controlling family-wise error rate (Holm, 1979).
- Thresholds: * p<0.05, ** p<0.01, *** p<0.001 (APA convention).
- Two-sided: No directional hypothesis assumed.
Parameters
----------
pairwise_df : DataFrame from run_statistics() with columns:
Time_min, p_adj, p_raw (already Holm-corrected)
show_ns : bool, default True
If False, non-significant brackets are suppressed.
"""
if pairwise_df is None or len(pairwise_df) == 0:
return
n_cond = len(offsets)
if n_cond < 2:
return
half_dodge = abs(offsets[1] - offsets[0]) / 2 if n_cond >= 2 else 0.15
times_list = list(times) # avoid numpy float comparison issues
# Get y-range for positioning
if style == "dotplot":
y_vals = rep_summary["median_val"].values
else:
y_vals = df[measure].dropna().values
if len(y_vals) == 0:
return
y_range = float(y_vals.max() - y_vals.min())
if y_range == 0:
y_range = 1.0
brackets_drawn = 0
max_y_drawn = y_vals.max()
for _, row in pairwise_df.iterrows():
t = row["Time_min"]
# Safe float comparison: find the closest matching timepoint
matched_j = None
for j, tv in enumerate(times_list):
if abs(t - tv) < 1e-6:
matched_j = j
break
if matched_j is None:
continue
p_adj = row.get("p_adj", row.get("p_raw", 1.0))
if np.isnan(p_adj):
continue
# Star label (APA convention)
if p_adj < 0.001:
stars = "***"
elif p_adj < 0.01:
stars = "**"
elif p_adj < 0.05:
stars = "*"
else:
stars = "ns"
# Optionally skip non-significant
if not show_ns and stars == "ns":
continue
# Format label
if sig_style == "stars":
label = stars
elif sig_style == "stars_p":
pstr = "p<0.001" if p_adj < 0.001 else f"p={p_adj:.3f}"
label = f"{stars}\n({pstr})"
else: # "pval"
label = "p<0.001" if p_adj < 0.001 else f"p={p_adj:.3f}"
# y position: top of data at this time point
if style == "dotplot":
time_vals = rep_summary[rep_summary["Time_min"] == t]["median_val"].values
else:
time_vals = df[df["Time_min"] == t][measure].dropna().values
if len(time_vals) == 0:
continue
y_top = float(time_vals.max())
y_bar = y_top + y_range * 0.06
y_tick = y_bar - y_range * 0.015
x_left = matched_j - half_dodge
x_right = matched_j + half_dodge
x_center = matched_j
# Visual distinction: gray for ns, black + bold for significant
bracket_color = "#999999" if stars == "ns" else "black"
label_weight = "normal" if stars == "ns" else "bold"
label_color = "#888888" if stars == "ns" else "black"
# Draw bracket (3 segments: left tick, horizontal bar, right tick)
ax.plot([x_left, x_left], [y_tick, y_bar],
color=bracket_color, linewidth=0.7, clip_on=False)
ax.plot([x_left, x_right], [y_bar, y_bar],
color=bracket_color, linewidth=0.7, clip_on=False)
ax.plot([x_right, x_right], [y_tick, y_bar],
color=bracket_color, linewidth=0.7, clip_on=False)
# Label above bracket
ax.text(x_center, y_bar + y_range * 0.018, label,
ha="center", va="bottom", fontsize=max(7, font_size * 0.7),
fontweight=label_weight, color=label_color)
max_y_drawn = max(max_y_drawn, y_bar)
brackets_drawn += 1
if brackets_drawn > 0:
# Expand y-axis to fit all brackets + labels
label_extra = 0.22 if sig_style == "stars_p" else 0.14
new_top = max_y_drawn + y_range * label_extra
current_ylim = ax.get_ylim()
if new_top > current_ylim[1]:
ax.set_ylim(current_ylim[0], new_top)
# Add methodology caption as figure text
n_reps = rep_summary.groupby(["Condition", "Time_min"]).size()
n_min, n_max = int(n_reps.min()), int(n_reps.max())
n_str = str(n_min) if n_min == n_max else f"{n_min}–{n_max}"
n_tests = len(pairwise_df)
caption = (f"Brackets: Welch's t-test on per-replicate medians "
f"(N = {n_str} biological replicates/group), "
f"Holm-corrected ({n_tests} comparisons). "
f"* p<0.05, ** p<0.01, *** p<0.001.")
ax.annotate(caption, xy=(0.5, -0.12), xycoords="axes fraction",
ha="center", va="top", fontsize=max(6, font_size - 3),
color="#555555", style="italic")
# =============================================================================
# PLOT 1: DISTRIBUTION PLOTS
# =============================================================================
def plot_distributions(df, rep_summary, measure, conditions, colors,
figsize=(8, 5), font_size=11, style="violin",
sig_results=None, sig_style="stars", show_ns=True):
"""
Distribution plots of single-cell measurements at each condition x time,
with per-replicate medians overlaid.
Styles:
'violin' - Kernel density violins + replicate medians (default)
'strip' - Jittered raw data points + replicate medians
'sina' - Density-proportional jitter (like a violin made of points)
'dotplot' - Replicate-level summaries only (what the stats are based on)
'box' - Box plots + replicate medians
sig_results: dict from run_statistics() with 'pairwise' key, or None to skip.
sig_style: 'stars' (*, **, ***), 'stars_p' (stars + p-value), 'pval' (p only).
show_ns: If False, suppress non-significant brackets.
"""
set_pub_style(font_size)
fig, ax = plt.subplots(figsize=figsize)
times = sorted(df["Time_min"].unique())
n_cond = len(conditions)
width = 0.35
offsets = np.linspace(-width / 2, width / 2, n_cond)
style_labels = {
"violin": "Violins = cell distributions; markers = per-replicate medians (N=3)",
"strip": "Each point = one cell; large markers = per-replicate medians (N=3)",
"sina": "Point spread follows density; large markers = per-replicate medians (N=3)",
"dotplot": "Each point = one biological replicate median (N=3); bar = group mean",
"box": "Box = IQR; markers = per-replicate medians (N=3)",
}
style_titles = {
"violin": f"Single-Cell {measure} Distributions",
"strip": f"Single-Cell {measure} -- Jitter Plot",
"sina": f"Single-Cell {measure} -- Sina Plot",
"dotplot": f"{measure} -- Replicate Summary Dot Plot",
"box": f"Single-Cell {measure} -- Box Plot",
}
if style == "dotplot":
# Replicate-only dot plot: show ONLY the per-replicate medians
for i, cond in enumerate(conditions):
cond_rep = rep_summary[rep_summary["Condition"] == cond]
positions = np.arange(len(times)) + offsets[i]
for j, t in enumerate(times):
time_reps = cond_rep[cond_rep["Time_min"] == t]
x_pos = np.full(len(time_reps), positions[j])
x_pos += np.random.uniform(-0.02, 0.02, len(x_pos))
ax.scatter(x_pos, time_reps["median_val"],
c=colors[i], edgecolors="black", linewidths=0.8,
s=70, zorder=5,
marker=REPLICATE_MARKERS[i % len(REPLICATE_MARKERS)],
label=cond if j == 0 else None)
# Group mean crossbar
group_means = [cond_rep[cond_rep["Time_min"] == t]["median_val"].mean()
for t in times]
bar_hw = width * 0.3
for j, gm in enumerate(group_means):
ax.plot([positions[j] - bar_hw, positions[j] + bar_hw],
[gm, gm], color=colors[i], linewidth=1.5, zorder=4)
ax.set_ylabel(f"Median {measure} (per replicate)")
else:
for i, cond in enumerate(conditions):
cond_data = df[df["Condition"] == cond]
positions = np.arange(len(times)) + offsets[i]
if style == "violin":
violin_data = [cond_data[cond_data["Time_min"] == t][measure].dropna().values
for t in times]
parts = ax.violinplot(
violin_data, positions=positions, widths=width * 0.85,
showmeans=False, showmedians=False, showextrema=False)
for pc in parts["bodies"]:
pc.set_facecolor(colors[i])
pc.set_alpha(0.55)
pc.set_edgecolor("#4D4D4D")
pc.set_linewidth(0.5)
elif style == "strip":
for j, t in enumerate(times):
vals = cond_data[cond_data["Time_min"] == t][measure].dropna().values
jitter = np.random.uniform(-width * 0.35, width * 0.35, len(vals))
ax.scatter(positions[j] + jitter, vals,
c=colors[i], alpha=0.12, s=3, zorder=2, rasterized=True)
elif style == "sina":
# Sina: jitter width proportional to local density
for j, t in enumerate(times):
vals = cond_data[cond_data["Time_min"] == t][measure].dropna().values
if len(vals) < 3:
jitter = np.random.uniform(-0.05, 0.05, len(vals))
else:
try:
kde = gaussian_kde(vals)
densities = kde(vals)
max_d = densities.max() if densities.max() > 0 else 1
norm_d = densities / max_d # 0-1 scale
jitter = np.random.uniform(-1, 1, len(vals)) * norm_d * width * 0.4
except Exception:
jitter = np.random.uniform(-width * 0.3, width * 0.3, len(vals))
ax.scatter(positions[j] + jitter, vals,
c=colors[i], alpha=0.12, s=3, zorder=2, rasterized=True)
elif style == "box":
box_data = [cond_data[cond_data["Time_min"] == t][measure].dropna().values
for t in times]
bp = ax.boxplot(box_data, positions=positions, widths=width * 0.7,
patch_artist=True, showfliers=False, zorder=2)
for patch in bp["boxes"]:
patch.set_facecolor(colors[i])
patch.set_alpha(0.5)
for element in ["whiskers", "caps", "medians"]:
for item in bp[element]:
item.set_color("#4D4D4D")
# Overlay replicate medians (for all cell-level styles)
cond_rep = rep_summary[rep_summary["Condition"] == cond]
for j, t in enumerate(times):
time_reps = cond_rep[cond_rep["Time_min"] == t]
x_pos = np.full(len(time_reps), positions[j])
x_pos += np.random.uniform(-0.03, 0.03, len(x_pos))
ax.scatter(
x_pos, time_reps["median_val"],
c=colors[i], edgecolors="black", linewidths=0.8,
s=50, zorder=5,
marker=REPLICATE_MARKERS[i % len(REPLICATE_MARKERS)],
label=cond if j == 0 else None,
)
ax.set_ylabel(measure)
ax.set_xticks(range(len(times)))
ax.set_xticklabels([f"{t} min" for t in times])
ax.set_xlabel("Time Post-Infection")
ax.set_title(style_titles.get(style, f"Single-Cell {measure} Distributions"))
ax.legend(loc="upper left")
ax.text(0.0, 1.02, style_labels.get(style, ""),
transform=ax.transAxes, fontsize=font_size - 2, color="#666666")
# --- Significance brackets ---
if sig_results is not None and "pairwise" in sig_results:
_add_sig_brackets(ax, sig_results["pairwise"], times, offsets, style,
df, measure, rep_summary, font_size, sig_style, show_ns)
fig.subplots_adjust(bottom=0.18) # room for methodology caption
fig.tight_layout()
return fig
# =============================================================================
# PLOT 2: TRAJECTORY PLOT
# =============================================================================
def plot_trajectories(rep_summary, cond_summary, measure, conditions, colors,
stat="median_val", connect_reps=True,
figsize=(6, 4.5), font_size=11):
"""
Per-replicate summary trajectories over time, with condition-level
trend lines and SEM ribbons.
WHY: This is your KEY figure. Each point is one biological replicate's
median — your true independent observation. If infection increases cell
size over time, the treatment lines will diverge from control. The
divergence pattern IS the condition x time interaction.
"""
set_pub_style(font_size)
fig, ax = plt.subplots(figsize=figsize)
times = sorted(rep_summary["Time_min"].unique())
stat_label = "Median" if stat == "median_val" else "Mean"
for i, cond in enumerate(conditions):
cond_rep = rep_summary[rep_summary["Condition"] == cond]
cond_agg = cond_summary[cond_summary["Condition"] == cond].sort_values("Time_min")
# Individual replicate trajectories (dashed)
if connect_reps:
for rep_id in cond_rep["Replicate"].unique():
rep_data = cond_rep[cond_rep["Replicate"] == rep_id].sort_values("Time_min")
ax.plot(
rep_data["Time_min"], rep_data[stat],
color=colors[i], alpha=0.3, linewidth=0.8,
linestyle="--", zorder=2,
)
# Replicate points with distinct markers per replicate
for k, rep_id in enumerate(sorted(cond_rep["Replicate"].unique())):
rep_data = cond_rep[cond_rep["Replicate"] == rep_id]
ax.scatter(
rep_data["Time_min"], rep_data[stat],
c=colors[i], edgecolors="black", linewidths=0.6,
s=45, zorder=4,
marker=REPLICATE_MARKERS[k % len(REPLICATE_MARKERS)],
label=cond if k == 0 else None,
)
# Condition mean ± SEM ribbon
t_vals = cond_agg["Time_min"].values
means = cond_agg["grand_mean"].values
sems = cond_agg["sem"].fillna(0).values
ax.fill_between(
t_vals, means - sems, means + sems,
color=colors[i], alpha=0.12, zorder=1,
)
# Condition trend line
ax.plot(
t_vals, means,
color=colors[i], linewidth=2, zorder=3,
)
ax.set_xticks(times)
ax.set_xlabel("Time Post-Infection (min)")
ax.set_ylabel(f"{stat_label} {measure} per Replicate")
ax.set_title(f"{measure} Trajectory Over Time")
ax.text(
0.0, 1.02,
"Points = biological replicates; ribbon = SEM; dashed = individual trajectories",
transform=ax.transAxes, fontsize=font_size - 2, color="#666666",
)
ax.legend(loc="upper left")
fig.tight_layout()
return fig
# =============================================================================
# PLOT 3: EFFECT SIZE OVER TIME
# =============================================================================
def compute_effect_sizes(rep_summary, ref_condition, stat="median_val"):
"""
Compute effect sizes (absolute difference, % change, Cohen's d)
at each time point, relative to the reference (control) condition.
WHY: P-values say IF an effect exists; effect sizes say HOW BIG.
With N=3 replicates, even real effects may not reach p < 0.05.
A consistent 30% increase is biologically meaningful regardless of p.
"""
times = sorted(rep_summary["Time_min"].unique())
treatments = [c for c in rep_summary["Condition"].unique() if c != ref_condition]
rows = []
for t in times:
ctrl = rep_summary[(rep_summary["Condition"] == ref_condition) &
(rep_summary["Time_min"] == t)][stat].values
for trt in treatments:
trt_vals = rep_summary[(rep_summary["Condition"] == trt) &
(rep_summary["Time_min"] == t)][stat].values
ctrl_mean = np.mean(ctrl)
trt_mean = np.mean(trt_vals)
pooled_sd = np.sqrt((np.var(ctrl, ddof=1) + np.var(trt_vals, ddof=1)) / 2)
rows.append({
"Time_min": t,
"Treatment": trt,
"ctrl_mean": ctrl_mean,
"trt_mean": trt_mean,
"abs_diff": trt_mean - ctrl_mean,
"pct_change": (trt_mean - ctrl_mean) / ctrl_mean * 100 if ctrl_mean != 0 else np.nan,
"cohens_d": (trt_mean - ctrl_mean) / pooled_sd if pooled_sd > 0 else np.nan,
})
return pd.DataFrame(rows)
def plot_effect_sizes(es_df, measure, metric="pct_change", colors=None,
figsize=(6, 4), font_size=11):
"""Plot effect size trajectory over time."""
set_pub_style(font_size)
fig, ax = plt.subplots(figsize=figsize)
labels = {
"abs_diff": f"Δ {measure} (Treatment − Control)",
"pct_change": f"% Change in {measure}",
"cohens_d": "Cohen's d",
}
color = colors[1] if colors else "#D94A4A"
for trt in es_df["Treatment"].unique():
trt_data = es_df[es_df["Treatment"] == trt].sort_values("Time_min")
ax.plot(trt_data["Time_min"], trt_data[metric],
color=color, linewidth=2, marker="o", markersize=8,
markeredgecolor="black", markeredgewidth=0.6, label=trt)
ax.axhline(0, color="#808080", linestyle="--", linewidth=0.8)
if metric == "cohens_d":
ax.axhline(0.8, color="#B3B3B3", linestyle=":", linewidth=0.6)
ax.axhline(-0.8, color="#B3B3B3", linestyle=":", linewidth=0.6)
ax.text(min(es_df["Time_min"]), 0.85, "Large effect (|d|=0.8)",
fontsize=font_size - 3, color="#808080")
ax.set_xticks(sorted(es_df["Time_min"].unique()))
ax.set_xlabel("Time Post-Infection (min)")
ax.set_ylabel(labels.get(metric, metric))
ax.set_title(f"Effect of Infection on {measure} Over Time")
ax.legend()
fig.tight_layout()
return fig
# =============================================================================
# PLOT 4: ECDF
# =============================================================================
def plot_ecdf(df, measure, conditions, colors, figsize=(10, 3.5), font_size=11):
"""
Empirical cumulative distribution function at each time point.
WHY: ECDFs give an unbiased view of the entire distribution with no
binning or smoothing choices. A rightward shift = larger cells.
The vertical gap between curves = the fraction of cells that differ.
"""
set_pub_style(font_size)
times = sorted(df["Time_min"].unique())
fig, axes = plt.subplots(1, len(times), figsize=figsize, sharey=True)
if len(times) == 1:
axes = [axes]
for ax, t in zip(axes, times):
for i, cond in enumerate(conditions):
vals = df[(df["Condition"] == cond) & (df["Time_min"] == t)][measure].dropna()
sorted_vals = np.sort(vals)
ecdf_y = np.arange(1, len(sorted_vals) + 1) / len(sorted_vals)
ax.step(sorted_vals, ecdf_y, color=colors[i], linewidth=1.5, label=cond)
ax.set_title(f"{t} min", fontsize=font_size)
ax.set_xlabel(measure)
if ax == axes[0]:
ax.set_ylabel("Cumulative Proportion")
ax.legend(fontsize=font_size - 2)
fig.suptitle(f"ECDF — {measure}", fontweight="bold", fontsize=font_size + 1)
fig.tight_layout()
return fig
# =============================================================================
# PLOT 5: COMPOSITE PUBLICATION FIGURE
# =============================================================================
def plot_composite(df, rep_summary, cond_summary, es_df, measure,
conditions, colors, figsize=(14, 5), font_size=10,
dist_style="violin", sig_results=None, sig_style="stars",
show_ns=True, stat="median_val"):
"""Multi-panel publication figure: A) Distributions, B) Trajectories, C) Effect sizes."""
set_pub_style(font_size)
fig, (ax_a, ax_b, ax_c) = plt.subplots(1, 3, figsize=figsize,
gridspec_kw={"width_ratios": [1.3, 1, 0.8]})
times = sorted(df["Time_min"].unique())
n_cond = len(conditions)
width = 0.35
offsets = np.linspace(-width / 2, width / 2, n_cond)
# --- Panel A: Distributions (style-dependent) ---
for i, cond in enumerate(conditions):
cond_data = df[df["Condition"] == cond]
positions = np.arange(len(times)) + offsets[i]
if dist_style == "violin":
violin_data = [cond_data[cond_data["Time_min"] == t][measure].dropna().values
for t in times]
parts = ax_a.violinplot(violin_data, positions=positions, widths=width * 0.85,
showmeans=False, showmedians=False, showextrema=False)
for pc in parts["bodies"]:
pc.set_facecolor(colors[i])
pc.set_alpha(0.5)
pc.set_edgecolor("#4D4D4D")
pc.set_linewidth(0.4)
elif dist_style == "strip":
for j, t in enumerate(times):
vals = cond_data[cond_data["Time_min"] == t][measure].dropna().values
jitter = np.random.uniform(-width * 0.35, width * 0.35, len(vals))
ax_a.scatter(positions[j] + jitter, vals,
c=colors[i], alpha=0.10, s=2, zorder=2, rasterized=True)
elif dist_style == "sina":
for j, t in enumerate(times):
vals = cond_data[cond_data["Time_min"] == t][measure].dropna().values
if len(vals) < 3:
jitter = np.random.uniform(-0.05, 0.05, len(vals))
else:
try:
kde = gaussian_kde(vals)
densities = kde(vals)
max_d = densities.max() if densities.max() > 0 else 1
norm_d = densities / max_d
jitter = np.random.uniform(-1, 1, len(vals)) * norm_d * width * 0.4
except Exception:
jitter = np.random.uniform(-width * 0.3, width * 0.3, len(vals))
ax_a.scatter(positions[j] + jitter, vals,
c=colors[i], alpha=0.10, s=2, zorder=2, rasterized=True)
elif dist_style == "box":
box_data = [cond_data[cond_data["Time_min"] == t][measure].dropna().values
for t in times]
bp = ax_a.boxplot(box_data, positions=positions, widths=width * 0.7,
patch_artist=True, showfliers=False, zorder=2)
for patch in bp["boxes"]:
patch.set_facecolor(colors[i])
patch.set_alpha(0.5)
for element in ["whiskers", "caps", "medians"]:
for item in bp[element]:
item.set_color("#4D4D4D")
# Replicate medians (all styles except dotplot)
if dist_style != "dotplot":
cond_rep = rep_summary[rep_summary["Condition"] == cond]
for j, t in enumerate(times):
time_reps = cond_rep[cond_rep["Time_min"] == t]
x_pos = np.full(len(time_reps), positions[j])
x_pos += np.random.uniform(-0.02, 0.02, len(x_pos))
ax_a.scatter(x_pos, time_reps["median_val"], c=colors[i],
edgecolors="black", linewidths=0.6, s=30, zorder=5)
if dist_style == "dotplot":
# Dotplot: only replicate medians
for i, cond in enumerate(conditions):
cond_rep = rep_summary[rep_summary["Condition"] == cond]
positions = np.arange(len(times)) + offsets[i]
for j, t in enumerate(times):
time_reps = cond_rep[cond_rep["Time_min"] == t]
x_pos = np.full(len(time_reps), positions[j])
x_pos += np.random.uniform(-0.02, 0.02, len(x_pos))
ax_a.scatter(x_pos, time_reps["median_val"], c=colors[i],
edgecolors="black", linewidths=0.6, s=50, zorder=5)
group_means = [cond_rep[cond_rep["Time_min"] == t]["median_val"].mean()
for t in times]
bar_hw = width * 0.3
for j, gm in enumerate(group_means):
ax_a.plot([positions[j] - bar_hw, positions[j] + bar_hw],
[gm, gm], color=colors[i], linewidth=1.5, zorder=4)
ax_a.set_ylabel(f"Median {measure}")
else:
ax_a.set_ylabel(measure)
ax_a.set_xticks(range(len(times)))
ax_a.set_xticklabels([f"{t}" for t in times])
ax_a.set_xlabel("Time (min)")
ax_a.set_title("A", loc="left", fontweight="bold", fontsize=font_size + 2)
# Significance brackets on Panel A
if sig_results is not None and "pairwise" in sig_results:
_add_sig_brackets(ax_a, sig_results["pairwise"], times, offsets, dist_style,
df, measure, rep_summary, font_size, sig_style, show_ns)
# --- Panel B: Trajectories ---
stat_label = "Median" if stat == "median_val" else "Mean"
for i, cond in enumerate(conditions):
cond_rep = rep_summary[rep_summary["Condition"] == cond]
cond_agg = cond_summary[cond_summary["Condition"] == cond].sort_values("Time_min")
# Individual replicate lines
for rep_id in cond_rep["Replicate"].unique():
rep_data = cond_rep[cond_rep["Replicate"] == rep_id].sort_values("Time_min")
ax_b.plot(rep_data["Time_min"], rep_data[stat],
color=colors[i], alpha=0.25, linewidth=0.6, linestyle="--")
# Replicate points
for k, rep_id in enumerate(sorted(cond_rep["Replicate"].unique())):
rep_data = cond_rep[cond_rep["Replicate"] == rep_id]
ax_b.scatter(rep_data["Time_min"], rep_data[stat], c=colors[i],
edgecolors="black", linewidths=0.5, s=30, zorder=4,
marker=REPLICATE_MARKERS[k % len(REPLICATE_MARKERS)],
label=cond if k == 0 else None)
# Mean trend + SEM ribbon
t_vals = cond_agg["Time_min"].values
means = cond_agg["grand_mean"].values
sems = cond_agg["sem"].fillna(0).values
ax_b.fill_between(t_vals, means - sems, means + sems,
color=colors[i], alpha=0.1)
ax_b.plot(t_vals, means, color=colors[i], linewidth=1.8)
ax_b.set_xticks(times)
ax_b.set_xlabel("Time (min)")
ax_b.set_ylabel(f"{stat_label} {measure}")
ax_b.legend(fontsize=font_size - 2, loc="upper left")
ax_b.set_title("B", loc="left", fontweight="bold", fontsize=font_size + 2)
# --- Panel C: Effect sizes ---
color_trt = colors[1]
for trt in es_df["Treatment"].unique():
trt_data = es_df[es_df["Treatment"] == trt].sort_values("Time_min")
ax_c.plot(trt_data["Time_min"], trt_data["pct_change"],
color=color_trt, linewidth=2, marker="o", markersize=6,
markeredgecolor="black", markeredgewidth=0.5)
ax_c.axhline(0, color="#808080", linestyle="--", linewidth=0.6)
ax_c.set_xticks(times)
ax_c.set_xlabel("Time (min)")
ax_c.set_ylabel(f"% Δ {measure}")
ax_c.set_title("C", loc="left", fontweight="bold", fontsize=font_size + 2)
fig.suptitle(f"{measure} — Bacteriophage Infection Time Course",
fontweight="bold", fontsize=font_size + 2, y=1.02)
fig.tight_layout()
return fig
# =============================================================================
# STATISTICS
# =============================================================================
def run_statistics(df, rep_summary, measure, ref_condition, stat="median_val"):
"""
Two-way ANOVA on per-replicate summaries + pairwise comparisons.
Validated against R's aov() and t.test() to produce identical results.
Includes ANOVA assumption diagnostics, confidence intervals, and effect
sizes following APA 7th edition and SAMPL reporting guidelines.
Statistical approach:
Step 1: Collapse each replicate to its summary statistic
Step 2: Two-way ANOVA (Condition x Time) on replicate summaries
Step 3: Assumption diagnostics (Shapiro-Wilk, Levene's)
Step 4: Pairwise Welch's t-tests with 95% CIs and Holm correction
"""
results = {}
sig_marker = lambda p: "***" if p < 0.001 else "**" if p < 0.01 else "*" if p < 0.05 else "ns"
# --- Descriptive statistics ---
desc = rep_summary.groupby(["Condition", "Time_min"]).agg(
n_reps=(stat, "count"),
mean=(stat, "mean"),
std=(stat, "std"),
median=(stat, "median"),
).reset_index()
results["descriptive"] = desc
# =====================================================================
# TWO-WAY ANOVA (Type I SS)
# Validated against R's aov() -- identical for balanced designs where
# Type I = Type II = Type III sums of squares.
# =====================================================================
print("\n" + "=" * 78)
print(f"STATISTICAL ANALYSIS -- Per-Replicate Summaries [CellMorphR v{__version__}]")
print("=" * 78)
print(f"Measure: {measure} | Summary: {stat}")
print(f"Reference condition: {ref_condition}")
print(f"Total replicate-level observations: {len(rep_summary)}")
print()
anova_data = rep_summary[["Condition", "Time_min", "Replicate", stat]].copy()
anova_data.columns = ["Condition", "Time", "Replicate", "Y"]
anova_data["Time"] = anova_data["Time"].astype(str)
grand_mean = anova_data["Y"].mean()
N = len(anova_data)
conditions = anova_data["Condition"].unique()
timepoints = anova_data["Time"].unique()
n_cond = len(conditions)
n_time = len(timepoints)
# --- Design balance check ---
# Type I SS = Type III SS only when the design is perfectly balanced.
# If unbalanced, warn the user that results may differ from Type III.
cell_counts = anova_data.groupby(["Condition", "Time"])["Y"].count()
unique_counts = cell_counts.unique()
is_balanced = len(unique_counts) == 1
expected_cells = n_cond * n_time
actual_cells = len(cell_counts)
if not is_balanced or actual_cells != expected_cells:
print(" *** WARNING: UNBALANCED DESIGN DETECTED ***")
print(f" Cell counts per group: {dict(cell_counts)}")
print(f" Expected {expected_cells} groups with equal N; found {actual_cells} groups.")
print(" Type I SS (used here) may differ from Type III SS.")
print(" For unbalanced designs, consider using R's Type III SS via")
print(" car::Anova(model, type='III') or the LMM option in the R Shiny app.")
print()
elif is_balanced:
n_per_cell = unique_counts[0]
print(f" Design: balanced ({n_cond} conditions × {n_time} time points × "
f"{n_per_cell} replicates = {N} observations)")
print(f" Type I SS = Type II SS = Type III SS (balanced design verified)")
print()
# SS Condition
cond_means = anova_data.groupby("Condition")["Y"].mean()
n_per_cond = anova_data.groupby("Condition")["Y"].count()
ss_cond = sum(n_per_cond[c] * (cond_means[c] - grand_mean) ** 2 for c in conditions)
df_cond = n_cond - 1
# SS Time
time_means = anova_data.groupby("Time")["Y"].mean()
n_per_time = anova_data.groupby("Time")["Y"].count()
ss_time = sum(n_per_time[t] * (time_means[t] - grand_mean) ** 2 for t in timepoints)
df_time = n_time - 1
# SS Interaction
cell_means = anova_data.groupby(["Condition", "Time"])["Y"].mean()
cell_counts = anova_data.groupby(["Condition", "Time"])["Y"].count()
ss_interaction = 0
for c in conditions:
for t in timepoints:
if (c, t) in cell_means.index:
expected = cond_means[c] + time_means[t] - grand_mean
n_ct = cell_counts[(c, t)]
ss_interaction += n_ct * (cell_means[(c, t)] - expected) ** 2
df_interaction = df_cond * df_time
# SS Total and Residual
ss_total = sum((anova_data["Y"] - grand_mean) ** 2)
ss_resid = ss_total - ss_cond - ss_time - ss_interaction
df_resid = N - n_cond * n_time
# Mean squares and F values
ms_cond = ss_cond / df_cond if df_cond > 0 else 0
ms_time = ss_time / df_time if df_time > 0 else 0
ms_inter = ss_interaction / df_interaction if df_interaction > 0 else 0
ms_resid = ss_resid / df_resid if df_resid > 0 else 1e-10
f_cond = ms_cond / ms_resid
f_time = ms_time / ms_resid
f_inter = ms_inter / ms_resid
# P-values (sf = survival function, numerically precise for small p)
p_cond = stats.f.sf(f_cond, df_cond, df_resid) if df_resid > 0 else np.nan
p_time = stats.f.sf(f_time, df_time, df_resid) if df_resid > 0 else np.nan
p_inter = stats.f.sf(f_inter, df_interaction, df_resid) if df_resid > 0 else np.nan
# Partial eta-squared (effect size for ANOVA)
# Benchmarks: small = 0.01, medium = 0.06, large = 0.14 (Cohen, 1988)
eta2_cond = ss_cond / (ss_cond + ss_resid) if (ss_cond + ss_resid) > 0 else np.nan
eta2_time = ss_time / (ss_time + ss_resid) if (ss_time + ss_resid) > 0 else np.nan
eta2_inter = ss_interaction / (ss_interaction + ss_resid) if (ss_interaction + ss_resid) > 0 else np.nan
# Print ANOVA table
print("TWO-WAY ANOVA TABLE")
print("-" * 78)
print(f"{'Source':<20} {'SS':>8} {'df':>4} {'MS':>10} {'F':>8} {'p':>10} {'partial eta2':>12}")
print("-" * 78)
print(f"{'Condition':<20} {ss_cond:>8.3f} {df_cond:>4d} {ms_cond:>10.3f} {f_cond:>8.2f} {p_cond:>10.4f} {eta2_cond:>12.3f}")
print(f"{'Time':<20} {ss_time:>8.3f} {df_time:>4d} {ms_time:>10.3f} {f_time:>8.2f} {p_time:>10.4f} {eta2_time:>12.3f}")
print(f"{'Condition x Time':<20} {ss_interaction:>8.3f} {df_interaction:>4d} {ms_inter:>10.3f} {f_inter:>8.2f} {p_inter:>10.4f} {eta2_inter:>12.3f}")
print(f"{'Residual':<20} {ss_resid:>8.3f} {df_resid:>4d} {ms_resid:>10.3f}")
print("-" * 78)
print(f"\nCondition effect: F({df_cond},{df_resid}) = {f_cond:.2f}, p = {p_cond:.4f} {sig_marker(p_cond)}, partial eta2 = {eta2_cond:.3f}")
print(f"Time effect: F({df_time},{df_resid}) = {f_time:.2f}, p = {p_time:.4f} {sig_marker(p_time)}, partial eta2 = {eta2_time:.3f}")
print(f"Condition x Time: F({df_interaction},{df_resid}) = {f_inter:.2f}, p = {p_inter:.4f} {sig_marker(p_inter)}, partial eta2 = {eta2_inter:.3f} <-- KEY TEST")
if p_inter < 0.05:
print("\n [+] Significant interaction: the infection effect CHANGES over time.")
print(" --> Proceed to pairwise comparisons to find WHEN divergence is significant.")
else:
print("\n [-] Interaction not significant (p >= 0.05).")
print(" --> The infection effect does not significantly change over time,")
print(" though with N=3 power is limited. Check effect sizes.")
results["anova"] = {
"Condition": {"SS": ss_cond, "df": df_cond, "MS": ms_cond, "F": f_cond, "p": p_cond, "partial_eta2": eta2_cond},
"Time": {"SS": ss_time, "df": df_time, "MS": ms_time, "F": f_time, "p": p_time, "partial_eta2": eta2_time},
"Interaction": {"SS": ss_interaction, "df": df_interaction, "MS": ms_inter, "F": f_inter, "p": p_inter, "partial_eta2": eta2_inter},
"Residual": {"SS": ss_resid, "df": df_resid, "MS": ms_resid},
}
# =====================================================================
# ANOVA ASSUMPTION DIAGNOSTICS
# =====================================================================
print("\n\nANOVA ASSUMPTION DIAGNOSTICS")
print("-" * 65)
# Compute residuals from the cell means model
residuals = []
for _, row in anova_data.iterrows():
fitted = cell_means[(row["Condition"], row["Time"])]
residuals.append(row["Y"] - fitted)
residuals = np.array(residuals)
results["residuals"] = residuals
# 1. Normality of residuals -- Shapiro-Wilk test
if len(residuals) >= 3:
sw_stat, sw_p = stats.shapiro(residuals)
print(f" Shapiro-Wilk test (normality of residuals):")
print(f" W = {sw_stat:.4f}, p = {sw_p:.4f}")
if sw_p >= 0.05:
print(f" --> Residuals are consistent with normality (p >= 0.05).")
else:
print(f" --> Residuals deviate from normality (p < 0.05).")
print(f" With N=3 per cell, Shapiro-Wilk has low power.")
print(f" ANOVA is robust to moderate non-normality with balanced designs.")
results["shapiro"] = {"W": sw_stat, "p": sw_p}
print()
# 2. Homogeneity of variance -- Levene's test across groups
groups = []
for c in conditions: