forked from Rhizobium-gits/seq2pipe
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalysis.py
More file actions
1469 lines (1306 loc) · 62.3 KB
/
Copy pathanalysis.py
File metadata and controls
1469 lines (1306 loc) · 62.3 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
"""
analysis.py
===========
QIIME2 エクスポートデータから包括的な微生物叢解析図を
LLM に依存せず確定的に生成するモジュール。
使い方:
from analysis import run_comprehensive_analysis
figs = run_comprehensive_analysis(export_dir, figure_dir)
"""
import warnings
from pathlib import Path
from typing import Callable, Optional
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
warnings.filterwarnings("ignore", category=FutureWarning)
try:
import seaborn as sns
sns.set_theme(style="white", context="paper", font_scale=1.2)
_HAS_SNS = True
except ImportError:
_HAS_SNS = False
try:
from sklearn.manifold import MDS
_HAS_SKL = True
except ImportError:
_HAS_SKL = False
try:
import networkx as nx
_HAS_NX = True
except ImportError:
_HAS_NX = False
try:
from scipy import stats as sp_stats
from scipy.cluster import hierarchy as sp_hierarchy
from scipy.spatial.distance import squareform
_HAS_SCIPY = True
except ImportError:
_HAS_SCIPY = False
DPI = 200
PALETTE = [
"#4C72B0", "#DD8452", "#55A868", "#C44E52", "#8172B3",
"#937860", "#DA8BC3", "#8C8C8C", "#CCB974", "#64B5CD",
]
def _save(fig_dir: Path, name: str) -> str:
path = fig_dir / name
plt.savefig(path, dpi=DPI, bbox_inches="tight")
plt.close()
return str(path)
# ══════════════════════════════════════════════════════════════════════
# 個別図の生成関数
# ══════════════════════════════════════════════════════════════════════
def _fig_dada2_stats(fig_dir: Path, export_dir: Path, session_dir: Path) -> Optional[str]:
"""fig01: DADA2 denoising statistics"""
stats_path = export_dir / "denoising_stats" / "stats.tsv"
if not stats_path.exists():
return None
stats = pd.read_csv(stats_path, sep="\t", index_col=0)
fig, ax = plt.subplots(figsize=(10, 5))
x = np.arange(len(stats))
w = 0.18
cols = [c for c in ["input", "filtered", "denoised", "merged", "non-chimeric"] if c in stats.columns]
for i, col in enumerate(cols):
ax.bar(x + i * w, stats[col], width=w, label=col, color=PALETTE[i % len(PALETTE)], alpha=0.85, edgecolor="white")
ax.set_xticks(x + w * (len(cols) - 1) / 2)
ax.set_xticklabels(stats.index, rotation=45, ha="right", fontsize=9)
ax.set_ylabel("Read Count", fontsize=12, labelpad=6)
ax.set_title("DADA2 Denoising Statistics per Sample", fontsize=14, fontweight="bold", pad=10)
ax.legend(frameon=False, fontsize=9)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig01_dada2_stats.png")
def _fig_sequencing_depth(fig_dir: Path, ft: pd.DataFrame) -> Optional[str]:
"""fig02: Sequencing depth per sample"""
read_depth = ft.sum(axis=0).sort_values(ascending=False)
fig, ax = plt.subplots(figsize=(10, 5))
colors = [PALETTE[i % len(PALETTE)] for i in range(len(read_depth))]
ax.bar(range(len(read_depth)), read_depth.values, color=colors, edgecolor="white", alpha=0.85)
ax.set_xticks(range(len(read_depth)))
ax.set_xticklabels(read_depth.index, rotation=45, ha="right", fontsize=9)
ax.set_ylabel("Total Read Count", fontsize=12, labelpad=6)
ax.set_title("Sequencing Depth per Sample", fontsize=14, fontweight="bold", pad=10)
ax.axhline(read_depth.mean(), color="#C44E52", lw=1.5, ls="--", label=f"Mean: {read_depth.mean():.0f}")
ax.legend(frameon=False, fontsize=9)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig02_sequencing_depth.png")
def _fig_alpha_diversity(fig_dir: Path, alpha: pd.DataFrame) -> Optional[str]:
"""fig03: Alpha diversity boxplots (3 metrics)"""
cols = [c for c in alpha.columns if alpha[c].notna().sum() > 0]
n = len(cols)
if n == 0:
return None
colors_a = ["#4C72B0", "#55A868", "#DD8452", "#C44E52"]
fig, axes = plt.subplots(1, n, figsize=(5 * n, 5))
if n == 1:
axes = [axes]
for ax, col, color in zip(axes, cols, colors_a):
vals = alpha[col].dropna()
if _HAS_SNS:
sns.boxplot(y=vals, ax=ax, color=color, width=0.4, linewidth=1.5,
flierprops=dict(marker="o", markersize=5, alpha=0.6))
sns.stripplot(y=vals, ax=ax, color="#333333", size=5, alpha=0.6, jitter=True)
else:
ax.boxplot(vals, patch_artist=True, boxprops=dict(facecolor=color, alpha=0.7))
ax.set_title(col, fontsize=13, fontweight="bold", pad=8)
ax.set_ylabel(col, fontsize=11, labelpad=6)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
fig.suptitle("Alpha Diversity Metrics", fontsize=15, fontweight="bold", y=1.02)
plt.tight_layout()
return _save(fig_dir, "fig03_alpha_diversity.png")
def _fig_shannon_per_sample(fig_dir: Path, alpha: pd.DataFrame) -> Optional[str]:
"""fig04: Shannon per sample strip plot"""
if "Shannon" not in alpha.columns:
return None
samples = alpha.index.tolist()
x = np.arange(len(samples))
vals = alpha["Shannon"].values
fig, ax = plt.subplots(figsize=(11, 5))
colors = [PALETTE[i % len(PALETTE)] for i in range(len(samples))]
ax.scatter(x, vals, c=colors, s=90, zorder=3, edgecolors="white", lw=0.8)
ax.plot(x, vals, color="#999999", lw=1, zorder=2)
for i, (xi, yi, sid) in enumerate(zip(x, vals, samples)):
ax.annotate(sid, (xi, yi), textcoords="offset points", xytext=(0, 8),
ha="center", fontsize=7, color="#444444")
ax.set_xticks(x)
ax.set_xticklabels(samples, rotation=45, ha="right", fontsize=9)
ax.set_ylabel("Shannon Diversity Index", fontsize=12, labelpad=6)
ax.set_title("Shannon Diversity Index per Sample", fontsize=14, fontweight="bold", pad=10)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig04_shannon_per_sample.png")
def _fig_pcoa(fig_dir: Path, export_dir: Path) -> list:
"""fig05-08: Beta diversity PCoA (4 metrics)"""
if not _HAS_SKL:
return []
metrics = [
("braycurtis_distance_matrix", "Bray-Curtis", "#4C72B0"),
("jaccard_distance_matrix", "Jaccard", "#DD8452"),
("unweighted_unifrac_distance_matrix", "Unweighted UniFrac", "#55A868"),
("weighted_unifrac_distance_matrix", "Weighted UniFrac", "#C44E52"),
]
saved = []
for i, (fname, label, color) in enumerate(metrics, start=5):
dm_path = export_dir / "beta" / fname / "distance-matrix.tsv"
if not dm_path.exists():
continue
try:
dm = pd.read_csv(dm_path, sep="\t", index_col=0)
n = len(dm)
coords = MDS(n_components=2, dissimilarity="precomputed",
random_state=42, max_iter=500).fit_transform(dm.values)
# variance explained via eigendecomposition of centered distance matrix
A = -0.5 * dm.values ** 2
row_mean = A.mean(axis=1, keepdims=True)
col_mean = A.mean(axis=0, keepdims=True)
grand_mean = A.mean()
G = A - row_mean - col_mean + grand_mean
eigvals = np.linalg.eigvalsh(G)
eigvals = np.sort(eigvals)[::-1]
eigvals = np.maximum(eigvals, 0)
total_var = eigvals.sum()
var_exp = eigvals[:2] / total_var * 100 if total_var > 0 else [0, 0]
fig, ax = plt.subplots(figsize=(7, 6))
ax.scatter(coords[:, 0], coords[:, 1],
c=[color] * n, s=100, edgecolors="white", lw=0.8, zorder=3, alpha=0.9)
for j, sid in enumerate(dm.index):
ax.annotate(sid, (coords[j, 0], coords[j, 1]),
textcoords="offset points", xytext=(6, 4), fontsize=8, color="#444444")
ax.set_title(f"{label} PCoA", fontsize=14, fontweight="bold", pad=10)
ax.set_xlabel(f"PC 1 ({var_exp[0]:.1f}%)", fontsize=12, labelpad=6)
ax.set_ylabel(f"PC 2 ({var_exp[1]:.1f}%)", fontsize=12, labelpad=6)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
short = fname.split("_distance")[0]
saved.append(_save(fig_dir, f"fig0{i}_pcoa_{short}.png"))
except Exception:
pass
return saved
def _fig_beta_heatmaps(fig_dir: Path, export_dir: Path) -> Optional[str]:
"""fig09: Beta diversity distance matrix heatmaps (2x2)"""
metrics = [
("braycurtis_distance_matrix", "Bray-Curtis"),
("jaccard_distance_matrix", "Jaccard"),
("unweighted_unifrac_distance_matrix", "Unweighted UniFrac"),
("weighted_unifrac_distance_matrix", "Weighted UniFrac"),
]
dms = []
for fname, label in metrics:
p = export_dir / "beta" / fname / "distance-matrix.tsv"
if p.exists():
dms.append((label, pd.read_csv(p, sep="\t", index_col=0)))
if not dms:
return None
rows = (len(dms) + 1) // 2
fig, axes = plt.subplots(rows, 2, figsize=(14, 6 * rows))
axes = np.array(axes).flatten()
for idx, (label, dm) in enumerate(dms):
ax = axes[idx]
if _HAS_SNS:
sns.heatmap(dm, ax=ax, cmap="YlOrRd", square=True, linewidths=0.3,
linecolor="white", annot=True, fmt=".2f", annot_kws={"size": 7},
cbar_kws={"shrink": 0.7})
else:
im = ax.imshow(dm.values, cmap="YlOrRd", aspect="auto")
plt.colorbar(im, ax=ax, shrink=0.7)
ax.set_xticks(range(len(dm)))
ax.set_xticklabels(dm.columns, rotation=45, ha="right", fontsize=7)
ax.set_yticks(range(len(dm)))
ax.set_yticklabels(dm.index, fontsize=7)
ax.set_title(label, fontsize=12, fontweight="bold", pad=8)
for idx in range(len(dms), len(axes)):
axes[idx].set_visible(False)
fig.suptitle("Beta Diversity Distance Matrices", fontsize=15, fontweight="bold", y=1.01)
plt.tight_layout()
return _save(fig_dir, "fig09_beta_distance_heatmaps.png")
def _fig_top_asv_heatmap(fig_dir: Path, ft: pd.DataFrame) -> Optional[str]:
"""fig10: Top 30 ASV relative abundance heatmap"""
ft_rel = ft.div(ft.sum(axis=0), axis=1) * 100
top30 = ft_rel.mean(axis=1).nlargest(30).index
top_df = ft_rel.loc[top30]
fig, ax = plt.subplots(figsize=(12, 10))
if _HAS_SNS:
sns.heatmap(top_df, ax=ax, cmap="Blues", linewidths=0.2, linecolor="white",
xticklabels=True, yticklabels=True,
cbar_kws={"label": "Relative Abundance (%)", "shrink": 0.6})
else:
im = ax.imshow(top_df.values, cmap="Blues", aspect="auto")
plt.colorbar(im, ax=ax, label="Relative Abundance (%)", shrink=0.6)
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha="right", fontsize=9)
ax.set_yticklabels([f"ASV{i+1}" for i in range(len(top30))], fontsize=8)
ax.set_title("Top 30 ASVs — Relative Abundance Heatmap", fontsize=14, fontweight="bold", pad=10)
ax.set_xlabel("Sample", fontsize=12, labelpad=6)
ax.set_ylabel("ASV", fontsize=12, labelpad=6)
plt.tight_layout()
return _save(fig_dir, "fig10_top30_asv_heatmap.png")
def _fig_alpha_correlations(fig_dir: Path, alpha: pd.DataFrame) -> Optional[str]:
"""fig11: Alpha diversity correlations"""
pairs = [("Shannon", "Observed ASVs"), ("Shannon", "Faith PD")]
valid = [(cx, cy) for cx, cy in pairs if cx in alpha.columns and cy in alpha.columns]
if not valid:
return None
fig, axes = plt.subplots(1, len(valid), figsize=(6 * len(valid), 5))
if len(valid) == 1:
axes = [axes]
for ax, (cx, cy) in zip(axes, valid):
x_vals, y_vals = alpha[cx].values, alpha[cy].values
colors = [PALETTE[j % len(PALETTE)] for j in range(len(alpha))]
ax.scatter(x_vals, y_vals, c=colors, s=80, edgecolors="white", lw=0.8, zorder=3)
for j, sid in enumerate(alpha.index):
ax.annotate(sid, (x_vals[j], y_vals[j]),
textcoords="offset points", xytext=(5, 3), fontsize=7, color="#555555")
m, b = np.polyfit(x_vals, y_vals, 1)
xline = np.linspace(x_vals.min(), x_vals.max(), 50)
ax.plot(xline, m * xline + b, color="#C44E52", lw=1.5, ls="--", alpha=0.7)
ax.set_xlabel(cx, fontsize=12, labelpad=6)
ax.set_ylabel(cy, fontsize=12, labelpad=6)
ax.set_title(f"{cx} vs {cy}", fontsize=13, fontweight="bold", pad=8)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
fig.suptitle("Alpha Diversity Correlations", fontsize=15, fontweight="bold", y=1.02)
plt.tight_layout()
return _save(fig_dir, "fig11_alpha_correlations.png")
def _fig_richness_vs_depth(fig_dir: Path, ft: pd.DataFrame) -> Optional[str]:
"""fig12: ASV richness vs sequencing depth"""
asv_rich = (ft > 0).sum(axis=0)
depth = ft.sum(axis=0)
fig, ax = plt.subplots(figsize=(8, 6))
colors = [PALETTE[i % len(PALETTE)] for i in range(len(depth))]
ax.scatter(depth, asv_rich, c=colors, s=90, edgecolors="white", lw=0.8, zorder=3)
for sid in depth.index:
ax.annotate(sid, (depth[sid], asv_rich[sid]),
textcoords="offset points", xytext=(6, 3), fontsize=8, color="#444444")
m, b = np.polyfit(depth.values, asv_rich.values, 1)
xline = np.linspace(depth.min(), depth.max(), 50)
ax.plot(xline, m * xline + b, color="#C44E52", lw=1.5, ls="--", alpha=0.8)
ax.set_xlabel("Sequencing Depth (reads)", fontsize=12, labelpad=6)
ax.set_ylabel("ASV Richness", fontsize=12, labelpad=6)
ax.set_title("ASV Richness vs Sequencing Depth", fontsize=14, fontweight="bold", pad=10)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig12_richness_vs_depth.png")
def _fig_genus_composition(fig_dir: Path, ft: pd.DataFrame, tax: pd.DataFrame, top_n: int = 15) -> Optional[str]:
"""fig13: Genus-level stacked bar chart"""
tax["Genus"] = tax["Taxon"].str.extract(r"g__([^;]+)")[0].fillna("Unknown").str.strip()
tax["Genus"] = tax["Genus"].replace("", "Unknown")
common = ft.index.intersection(tax.index)
merged = ft.loc[common].copy()
merged["Genus"] = tax.loc[common, "Genus"]
genus_counts = merged.groupby("Genus").sum()
genus_rel = genus_counts.div(genus_counts.sum(axis=0), axis=1) * 100
top = genus_rel.mean(axis=1).sort_values(ascending=False).head(top_n).index.tolist()
plot_df = genus_rel.loc[top].copy()
plot_df.loc["Other"] = genus_rel.drop(index=top, errors="ignore").sum(axis=0)
plot_df = plot_df.T
colors = list(plt.cm.tab20.colors[:top_n]) + [(0.75, 0.75, 0.75)]
fig, ax = plt.subplots(figsize=(12, 6))
plot_df.plot(kind="bar", stacked=True, ax=ax, color=colors, width=0.8, edgecolor="white", linewidth=0.3)
ax.set_xlabel("Sample ID", fontsize=12, labelpad=6)
ax.set_ylabel("Relative Abundance (%)", fontsize=12, labelpad=6)
ax.set_title(f"Genus-Level Composition (Top {top_n})", fontsize=14, fontweight="bold", pad=10)
ax.tick_params(axis="x", rotation=45)
ax.legend(title="Genus", bbox_to_anchor=(1.01, 1), loc="upper left", fontsize=8, title_fontsize=9, frameon=False)
ax.set_ylim(0, 100)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig13_genus_composition.png")
def _fig_phylum_composition(fig_dir: Path, ft: pd.DataFrame, tax: pd.DataFrame) -> Optional[str]:
"""fig14: Phylum-level stacked bar chart"""
tax["Phylum"] = tax["Taxon"].str.extract(r"p__([^;]+)")[0].fillna("Unknown").str.strip()
common = ft.index.intersection(tax.index)
merged = ft.loc[common].copy()
merged["Phylum"] = tax.loc[common, "Phylum"]
phylum_counts = merged.groupby("Phylum").sum()
phylum_rel = phylum_counts.div(phylum_counts.sum(axis=0), axis=1) * 100
top = phylum_rel.mean(axis=1).sort_values(ascending=False).head(10).index.tolist()
plot_df = phylum_rel.loc[top].copy()
plot_df.loc["Other"] = phylum_rel.drop(index=top, errors="ignore").sum(axis=0)
plot_df = plot_df.T
colors = list(plt.cm.Set3.colors[:10]) + [(0.75, 0.75, 0.75)]
fig, ax = plt.subplots(figsize=(12, 6))
plot_df.plot(kind="bar", stacked=True, ax=ax, color=colors, width=0.8, edgecolor="white", linewidth=0.3)
ax.set_xlabel("Sample ID", fontsize=12, labelpad=6)
ax.set_ylabel("Relative Abundance (%)", fontsize=12, labelpad=6)
ax.set_title("Phylum-Level Composition", fontsize=14, fontweight="bold", pad=10)
ax.tick_params(axis="x", rotation=45)
ax.legend(title="Phylum", bbox_to_anchor=(1.01, 1), loc="upper left", fontsize=9, title_fontsize=10, frameon=False)
ax.set_ylim(0, 100)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig14_phylum_composition.png")
def _fig_genus_heatmap(fig_dir: Path, ft: pd.DataFrame, tax: pd.DataFrame) -> Optional[str]:
"""fig15: Top 20 genera heatmap"""
if not _HAS_SNS:
return None
tax["Genus"] = tax["Taxon"].str.extract(r"g__([^;]+)")[0].fillna("Unknown").str.strip()
tax["Genus"] = tax["Genus"].replace("", "Unknown")
common = ft.index.intersection(tax.index)
merged = ft.loc[common].copy()
merged["Genus"] = tax.loc[common, "Genus"]
genus_counts = merged.groupby("Genus").sum()
genus_rel = genus_counts.div(genus_counts.sum(axis=0), axis=1) * 100
top20 = genus_rel.mean(axis=1).sort_values(ascending=False).head(20).index
hm_df = genus_rel.loc[top20]
fig, ax = plt.subplots(figsize=(12, 8))
sns.heatmap(hm_df, ax=ax, cmap="YlOrRd", linewidths=0.3, linecolor="white",
annot=True, fmt=".1f", annot_kws={"size": 7},
cbar_kws={"label": "Relative Abundance (%)", "shrink": 0.7})
ax.set_title("Top 20 Genera — Relative Abundance (%)", fontsize=14, fontweight="bold", pad=10)
ax.set_xlabel("Sample", fontsize=12, labelpad=6)
ax.set_ylabel("Genus", fontsize=12, labelpad=6)
plt.tight_layout()
return _save(fig_dir, "fig15_genus_heatmap.png")
# ══════════════════════════════════════════════════════════════════════
# 拡張解析図 (fig16-fig25)
# ══════════════════════════════════════════════════════════════════════
def _fig_rarefaction(fig_dir: Path, ft: pd.DataFrame) -> Optional[str]:
"""fig16: Rarefaction curves per sample"""
rng = np.random.default_rng(42)
counts = ft.values.astype(int) # ASV x Samples
n_asv, n_samples = counts.shape
fig, ax = plt.subplots(figsize=(10, 6))
for s_idx in range(n_samples):
col = counts[:, s_idx]
total = col.sum()
if total == 0:
continue
depths = np.linspace(100, total, 10, dtype=int)
depths = depths[depths > 0]
medians = []
pool = np.repeat(np.arange(n_asv), col)
for d in depths:
obs_list = []
for _ in range(10):
sub = rng.choice(pool, size=min(d, len(pool)), replace=False)
obs_list.append(len(np.unique(sub)))
medians.append(np.median(obs_list))
color = PALETTE[s_idx % len(PALETTE)]
ax.plot(depths, medians, marker="o", markersize=3, lw=1.5,
color=color, alpha=0.8, label=ft.columns[s_idx])
ax.set_xlabel("Sequencing Depth", fontsize=12, labelpad=6)
ax.set_ylabel("Observed ASVs", fontsize=12, labelpad=6)
ax.set_title("Rarefaction Curves", fontsize=14, fontweight="bold", pad=10)
ax.legend(fontsize=8, frameon=False, bbox_to_anchor=(1.01, 1), loc="upper left")
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig16_rarefaction_curves.png")
def _fig_nmds(fig_dir: Path, export_dir: Path) -> Optional[str]:
"""fig17: NMDS ordination (Bray-Curtis)"""
if not _HAS_SKL:
return None
dm_path = export_dir / "beta" / "braycurtis_distance_matrix" / "distance-matrix.tsv"
if not dm_path.exists():
return None
dm = pd.read_csv(dm_path, sep="\t", index_col=0)
mds = MDS(n_components=2, dissimilarity="precomputed", metric=False,
random_state=42, max_iter=1000, normalized_stress="auto")
coords = mds.fit_transform(dm.values)
stress = mds.stress_
fig, ax = plt.subplots(figsize=(8, 7))
colors = [PALETTE[i % len(PALETTE)] for i in range(len(dm))]
ax.scatter(coords[:, 0], coords[:, 1], c=colors, s=120,
edgecolors="white", lw=0.8, zorder=3, alpha=0.9)
for j, sid in enumerate(dm.index):
ax.annotate(sid, (coords[j, 0], coords[j, 1]),
textcoords="offset points", xytext=(6, 4), fontsize=8, color="#444444")
ax.set_title(f"NMDS (Bray-Curtis) stress={stress:.3f}",
fontsize=14, fontweight="bold", pad=10)
ax.set_xlabel("NMDS1", fontsize=12, labelpad=6)
ax.set_ylabel("NMDS2", fontsize=12, labelpad=6)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig17_nmds_braycurtis.png")
def _fig_rank_abundance(fig_dir: Path, ft: pd.DataFrame) -> Optional[str]:
"""fig18: Rank-abundance curves"""
fig, ax = plt.subplots(figsize=(10, 6))
for s_idx, sid in enumerate(ft.columns):
abundances = ft[sid].values.copy()
abundances = abundances[abundances > 0]
abundances = np.sort(abundances)[::-1]
rel = abundances / abundances.sum() * 100
color = PALETTE[s_idx % len(PALETTE)]
ax.semilogy(np.arange(1, len(rel) + 1), rel, lw=1.5,
color=color, alpha=0.7, label=sid)
ax.set_xlabel("Species Rank", fontsize=12, labelpad=6)
ax.set_ylabel("Relative Abundance (%, log)", fontsize=12, labelpad=6)
ax.set_title("Rank-Abundance Curves", fontsize=14, fontweight="bold", pad=10)
ax.legend(fontsize=8, frameon=False, bbox_to_anchor=(1.01, 1), loc="upper left")
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig18_rank_abundance.png")
def _fig_taxonomic_alluvial(fig_dir: Path, ft: pd.DataFrame, tax: pd.DataFrame) -> Optional[str]:
"""fig19: Taxonomic alluvial plot (Phylum -> Class -> Order)"""
from matplotlib.patches import PathPatch
from matplotlib.path import Path as MplPath
levels = [("Phylum", r"p__([^;]+)"), ("Class", r"c__([^;]+)"), ("Order", r"o__([^;]+)")]
tax_levels = {}
for lvl_name, pattern in levels:
tax_levels[lvl_name] = tax["Taxon"].str.extract(pattern)[0].fillna("Unknown").str.strip()
tax_levels[lvl_name] = tax_levels[lvl_name].replace("", "Unknown")
common = ft.index.intersection(tax.index)
total_reads = ft.loc[common].sum(axis=1)
df = pd.DataFrame({k: v.loc[common] for k, v in tax_levels.items()})
df["reads"] = total_reads.values
top_phyla = df.groupby("Phylum")["reads"].sum().nlargest(8).index.tolist()
df.loc[~df["Phylum"].isin(top_phyla), "Phylum"] = "Other"
cmap = plt.cm.tab20
phylum_colors = {p: cmap(i / max(len(top_phyla), 1)) for i, p in enumerate(top_phyla)}
phylum_colors["Other"] = (0.75, 0.75, 0.75, 1.0)
fig, ax = plt.subplots(figsize=(14, 8))
n_levels = len(levels)
x_positions = np.linspace(0, 1, n_levels)
strip_width = 0.12
level_names = [l[0] for l in levels]
node_data = {}
for li, lvl in enumerate(level_names):
groups = df.groupby(lvl)["reads"].sum().sort_values(ascending=False)
total = groups.sum()
y_offset = 0
nd = {}
for name, val in groups.items():
h = val / total
nd[name] = (y_offset, y_offset + h)
y_offset += h + 0.005
node_data[lvl] = nd
for li, lvl in enumerate(level_names):
x = x_positions[li]
for name, (y0, y1) in node_data[lvl].items():
color = phylum_colors.get(name, (0.6, 0.6, 0.6, 0.8))
ax.barh(y=(y0 + y1) / 2, width=strip_width, height=y1 - y0,
left=x - strip_width / 2, color=color, edgecolor="white", lw=0.5)
if y1 - y0 > 0.02:
label = name if len(name) < 18 else name[:15] + "..."
ax.text(x, (y0 + y1) / 2, label, ha="center", va="center",
fontsize=6, fontweight="bold", color="white")
for li in range(n_levels - 1):
lvl_from, lvl_to = level_names[li], level_names[li + 1]
x0, x1 = x_positions[li] + strip_width / 2, x_positions[li + 1] - strip_width / 2
flows = df.groupby([lvl_from, lvl_to])["reads"].sum()
total = df["reads"].sum()
from_offsets = {k: v[0] for k, v in node_data[lvl_from].items()}
to_offsets = {k: v[0] for k, v in node_data[lvl_to].items()}
for (src, dst), val in flows.items():
h = val / total
y0_src = from_offsets.get(src, 0)
y0_dst = to_offsets.get(dst, 0)
from_offsets[src] = y0_src + h
to_offsets[dst] = y0_dst + h
verts = [
(x0, y0_src), (x0 + (x1 - x0) / 3, y0_src),
(x1 - (x1 - x0) / 3, y0_dst), (x1, y0_dst),
(x1, y0_dst + h), (x1 - (x1 - x0) / 3, y0_dst + h),
(x0 + (x1 - x0) / 3, y0_src + h), (x0, y0_src + h),
(x0, y0_src),
]
codes = [MplPath.MOVETO, MplPath.CURVE4, MplPath.CURVE4, MplPath.CURVE4,
MplPath.LINETO, MplPath.CURVE4, MplPath.CURVE4, MplPath.CURVE4,
MplPath.CLOSEPOLY]
color = phylum_colors.get(src, (0.6, 0.6, 0.6, 0.5))
patch = PathPatch(MplPath(verts, codes), facecolor=(*color[:3], 0.3),
edgecolor="none")
ax.add_patch(patch)
ax.set_xlim(-0.15, 1.15)
ax.set_ylim(-0.02, max(sum(y1 - y0 + 0.005 for y0, y1 in nd.values())
for nd in node_data.values()) + 0.02)
for li, lvl in enumerate(level_names):
ax.text(x_positions[li], -0.03, lvl, ha="center", va="top",
fontsize=13, fontweight="bold")
ax.set_title("Taxonomic Flow (Phylum > Class > Order)",
fontsize=14, fontweight="bold", pad=10)
ax.axis("off")
plt.tight_layout()
return _save(fig_dir, "fig19_taxonomic_alluvial.png")
def _fig_cooccurrence_network(fig_dir: Path, ft: pd.DataFrame, tax: pd.DataFrame) -> Optional[str]:
"""fig20: Co-occurrence network of top genera (Spearman)"""
if not _HAS_NX or not _HAS_SCIPY:
return None
tax_g = tax["Taxon"].str.extract(r"g__([^;]+)")[0].fillna("Unknown").str.strip()
tax_g = tax_g.replace("", "Unknown")
common = ft.index.intersection(tax.index)
merged = ft.loc[common].copy()
merged["Genus"] = tax_g.loc[common]
genus_counts = merged.groupby("Genus").sum()
genus_counts = genus_counts.drop("Unknown", errors="ignore")
top = genus_counts.sum(axis=1).nlargest(30).index
genus_sub = genus_counts.loc[top].T
G = nx.Graph()
mean_abd = genus_sub.mean()
for g in top:
G.add_node(g, size=mean_abd[g])
for i, g1 in enumerate(top):
for g2 in top[i + 1:]:
r, p = sp_stats.spearmanr(genus_sub[g1], genus_sub[g2])
if abs(r) > 0.6 and p < 0.05:
G.add_edge(g1, g2, weight=r)
if G.number_of_edges() == 0:
for i, g1 in enumerate(top):
for g2 in top[i + 1:]:
r, _ = sp_stats.spearmanr(genus_sub[g1], genus_sub[g2])
if abs(r) > 0.4:
G.add_edge(g1, g2, weight=r)
if G.number_of_edges() == 0:
return None
fig, ax = plt.subplots(figsize=(10, 10))
pos = nx.spring_layout(G, seed=42, k=2.0)
sizes = [max(G.nodes[n].get("size", 1), 0.1) for n in G.nodes()]
max_s = max(sizes) if sizes else 1
node_sizes = [s / max_s * 800 + 100 for s in sizes]
edges = G.edges(data=True)
edge_colors = ["#C44E52" if e[2]["weight"] < 0 else "#55A868" for e in edges]
edge_widths = [abs(e[2]["weight"]) * 3 for e in edges]
nx.draw_networkx_edges(G, pos, ax=ax, edge_color=edge_colors,
width=edge_widths, alpha=0.5)
nx.draw_networkx_nodes(G, pos, ax=ax, node_size=node_sizes,
node_color="#4C72B0", alpha=0.8, edgecolors="white", linewidths=0.8)
nx.draw_networkx_labels(G, pos, ax=ax, font_size=7, font_color="#333333")
ax.set_title("Genus Co-occurrence Network (Spearman)",
fontsize=14, fontweight="bold", pad=10)
ax.axis("off")
plt.tight_layout()
return _save(fig_dir, "fig20_cooccurrence_network.png")
def _fig_family_composition(fig_dir: Path, ft: pd.DataFrame, tax: pd.DataFrame) -> Optional[str]:
"""fig21: Family-level stacked bar chart (top 15)"""
tax["Family"] = tax["Taxon"].str.extract(r"f__([^;]+)")[0].fillna("Unknown").str.strip()
tax["Family"] = tax["Family"].replace("", "Unknown")
common = ft.index.intersection(tax.index)
merged = ft.loc[common].copy()
merged["Family"] = tax.loc[common, "Family"]
family_counts = merged.groupby("Family").sum()
family_rel = family_counts.div(family_counts.sum(axis=0), axis=1) * 100
top = family_rel.mean(axis=1).sort_values(ascending=False).head(15).index.tolist()
plot_df = family_rel.loc[top].copy()
plot_df.loc["Other"] = family_rel.drop(index=top, errors="ignore").sum(axis=0)
plot_df = plot_df.T
colors = list(plt.cm.tab20.colors[:15]) + [(0.75, 0.75, 0.75)]
fig, ax = plt.subplots(figsize=(12, 6))
plot_df.plot(kind="bar", stacked=True, ax=ax, color=colors, width=0.8,
edgecolor="white", linewidth=0.3)
ax.set_xlabel("Sample ID", fontsize=12, labelpad=6)
ax.set_ylabel("Relative Abundance (%)", fontsize=12, labelpad=6)
ax.set_title("Family-Level Composition (Top 15)", fontsize=14, fontweight="bold", pad=10)
ax.tick_params(axis="x", rotation=45)
ax.legend(title="Family", bbox_to_anchor=(1.01, 1), loc="upper left",
fontsize=8, title_fontsize=9, frameon=False)
ax.set_ylim(0, 100)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig21_family_composition.png")
def _fig_core_microbiome(fig_dir: Path, ft: pd.DataFrame, tax: pd.DataFrame) -> Optional[str]:
"""fig22: Core microbiome (prevalence vs mean abundance)"""
tax_g = tax["Taxon"].str.extract(r"g__([^;]+)")[0].fillna("Unknown").str.strip()
tax_g = tax_g.replace("", "Unknown")
common = ft.index.intersection(tax.index)
merged = ft.loc[common].copy()
merged["Genus"] = tax_g.loc[common]
genus_counts = merged.groupby("Genus").sum()
genus_counts = genus_counts.drop("Unknown", errors="ignore")
genus_rel = genus_counts.div(genus_counts.sum(axis=0), axis=1) * 100
n_samples = genus_rel.shape[1]
prevalence = (genus_rel > 0).sum(axis=1) / n_samples
mean_abd = genus_rel.mean(axis=1)
fig, ax = plt.subplots(figsize=(10, 7))
is_core = prevalence >= 0.8
ax.scatter(prevalence[~is_core], mean_abd[~is_core],
c="#8C8C8C", s=40, alpha=0.5, edgecolors="white", lw=0.5, label="Non-core")
ax.scatter(prevalence[is_core], mean_abd[is_core],
c="#C44E52", s=80, alpha=0.8, edgecolors="white", lw=0.8, label="Core (prevalence >= 80%)", zorder=3)
for g in prevalence[is_core].index:
if mean_abd[g] > mean_abd.quantile(0.8):
ax.annotate(g, (prevalence[g], mean_abd[g]),
textcoords="offset points", xytext=(5, 3), fontsize=7, color="#333333")
ax.axvline(0.8, color="#C44E52", ls="--", lw=1, alpha=0.5)
ax.set_xlabel("Prevalence (fraction of samples)", fontsize=12, labelpad=6)
ax.set_ylabel("Mean Relative Abundance (%)", fontsize=12, labelpad=6)
ax.set_title("Core Microbiome Analysis", fontsize=14, fontweight="bold", pad=10)
ax.legend(frameon=False, fontsize=9)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig22_core_microbiome.png")
def _fig_volcano(fig_dir: Path, ft: pd.DataFrame, tax: pd.DataFrame) -> Optional[str]:
"""fig23: Differential abundance volcano plot (Mann-Whitney U)"""
if not _HAS_SCIPY:
return None
tax_g = tax["Taxon"].str.extract(r"g__([^;]+)")[0].fillna("Unknown").str.strip()
tax_g = tax_g.replace("", "Unknown")
common = ft.index.intersection(tax.index)
merged = ft.loc[common].copy()
merged["Genus"] = tax_g.loc[common]
genus_counts = merged.groupby("Genus").sum()
genus_counts = genus_counts.drop("Unknown", errors="ignore")
genus_rel = genus_counts.div(genus_counts.sum(axis=0), axis=1) * 100
samples = genus_rel.columns.tolist()
n = len(samples)
if n < 4:
return None
mid = n // 2
grp1 = samples[:mid]
grp2 = samples[mid:]
results = []
for genus in genus_rel.index:
v1 = genus_rel.loc[genus, grp1].values
v2 = genus_rel.loc[genus, grp2].values
mean1, mean2 = v1.mean(), v2.mean()
pseudo = 0.001
log2fc = np.log2((mean2 + pseudo) / (mean1 + pseudo))
try:
_, pval = sp_stats.mannwhitneyu(v1, v2, alternative="two-sided")
except ValueError:
pval = 1.0
results.append((genus, log2fc, pval))
res_df = pd.DataFrame(results, columns=["Genus", "log2FC", "pvalue"])
# Benjamini-Hochberg FDR correction
n_tests = len(res_df)
ranked = res_df["pvalue"].rank()
res_df["fdr"] = res_df["pvalue"] * n_tests / ranked
res_df["fdr"] = res_df["fdr"].clip(upper=1.0)
# ensure monotonicity
res_df = res_df.sort_values("pvalue")
res_df["fdr"] = res_df["fdr"].cummin()
res_df = res_df.sort_index()
res_df["neg_log10p"] = -np.log10(res_df["pvalue"].clip(lower=1e-10))
fig, ax = plt.subplots(figsize=(10, 7))
sig = (res_df["fdr"] < 0.05) & (res_df["log2FC"].abs() > 1)
ax.scatter(res_df.loc[~sig, "log2FC"], res_df.loc[~sig, "neg_log10p"],
c="#8C8C8C", s=30, alpha=0.5, edgecolors="none")
up = sig & (res_df["log2FC"] > 0)
down = sig & (res_df["log2FC"] < 0)
ax.scatter(res_df.loc[up, "log2FC"], res_df.loc[up, "neg_log10p"],
c="#C44E52", s=60, alpha=0.8, edgecolors="white", lw=0.5, label="Up")
ax.scatter(res_df.loc[down, "log2FC"], res_df.loc[down, "neg_log10p"],
c="#4C72B0", s=60, alpha=0.8, edgecolors="white", lw=0.5, label="Down")
for _, row in res_df[sig].iterrows():
ax.annotate(row["Genus"], (row["log2FC"], row["neg_log10p"]),
textcoords="offset points", xytext=(5, 3), fontsize=7, color="#333333")
ax.axhline(-np.log10(0.05), color="#999999", ls="--", lw=0.8, alpha=0.5)
ax.axvline(-1, color="#999999", ls="--", lw=0.8, alpha=0.5)
ax.axvline(1, color="#999999", ls="--", lw=0.8, alpha=0.5)
ax.set_xlabel("log2 Fold Change", fontsize=12, labelpad=6)
ax.set_ylabel("-log10(p-value)", fontsize=12, labelpad=6)
grp1_label = f"Group1 ({grp1[0]}..{grp1[-1]})"
grp2_label = f"Group2 ({grp2[0]}..{grp2[-1]})"
ax.set_title(f"Differential Abundance (BH-corrected): {grp1_label} vs {grp2_label}",
fontsize=13, fontweight="bold", pad=10)
ax.legend(frameon=False, fontsize=9)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig23_differential_abundance.png")
def _fig_sample_dendrogram(fig_dir: Path, export_dir: Path) -> Optional[str]:
"""fig24: Sample dendrogram (Bray-Curtis UPGMA)"""
if not _HAS_SCIPY:
return None
dm_path = export_dir / "beta" / "braycurtis_distance_matrix" / "distance-matrix.tsv"
if not dm_path.exists():
return None
dm = pd.read_csv(dm_path, sep="\t", index_col=0)
condensed = squareform(dm.values)
linkage = sp_hierarchy.linkage(condensed, method="average")
fig, ax = plt.subplots(figsize=(10, 6))
sp_hierarchy.dendrogram(linkage, labels=dm.index.tolist(), ax=ax,
leaf_rotation=45, leaf_font_size=10,
color_threshold=0, above_threshold_color="#4C72B0")
ax.set_ylabel("Bray-Curtis Distance", fontsize=12, labelpad=6)
ax.set_title("Sample Dendrogram (UPGMA, Bray-Curtis)",
fontsize=14, fontweight="bold", pad=10)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig24_sample_dendrogram.png")
def _fig_genus_correlation(fig_dir: Path, ft: pd.DataFrame, tax: pd.DataFrame) -> Optional[str]:
"""fig25: Genus Spearman correlation clustermap"""
if not _HAS_SNS or not _HAS_SCIPY:
return None
tax_g = tax["Taxon"].str.extract(r"g__([^;]+)")[0].fillna("Unknown").str.strip()
tax_g = tax_g.replace("", "Unknown")
common = ft.index.intersection(tax.index)
merged = ft.loc[common].copy()
merged["Genus"] = tax_g.loc[common]
genus_counts = merged.groupby("Genus").sum()
genus_counts = genus_counts.drop("Unknown", errors="ignore")
top20 = genus_counts.sum(axis=1).nlargest(20).index
genus_sub = genus_counts.loc[top20].T
n = len(top20)
corr_matrix = np.zeros((n, n))
for i in range(n):
for j in range(n):
if i == j:
corr_matrix[i, j] = 1.0
elif i < j:
r, _ = sp_stats.spearmanr(genus_sub.iloc[:, i], genus_sub.iloc[:, j])
corr_matrix[i, j] = r
corr_matrix[j, i] = r
corr_df = pd.DataFrame(corr_matrix, index=top20, columns=top20)
g = sns.clustermap(corr_df, cmap="RdBu_r", center=0, linewidths=0.3,
linecolor="white", figsize=(12, 10),
annot=True, fmt=".2f", annot_kws={"size": 6},
cbar_kws={"label": "Spearman r", "shrink": 0.6},
dendrogram_ratio=0.12)
g.fig.suptitle("Genus Spearman Correlation (Top 20)",
fontsize=14, fontweight="bold", y=1.01)
path = fig_dir / "fig25_genus_correlation.png"
g.savefig(path, dpi=DPI, bbox_inches="tight")
plt.close()
return str(path)
def _fig_class_composition(fig_dir: Path, ft: pd.DataFrame, tax: pd.DataFrame) -> Optional[str]:
"""fig26: Class-level stacked bar chart (top 15)"""
tax["Class"] = tax["Taxon"].str.extract(r"c__([^;]+)")[0].fillna("Unknown").str.strip()
tax["Class"] = tax["Class"].replace("", "Unknown")
common = ft.index.intersection(tax.index)
merged = ft.loc[common].copy()
merged["Class"] = tax.loc[common, "Class"]
class_counts = merged.groupby("Class").sum()
class_rel = class_counts.div(class_counts.sum(axis=0), axis=1) * 100
top = class_rel.mean(axis=1).sort_values(ascending=False).head(15).index.tolist()
plot_df = class_rel.loc[top].copy()
plot_df.loc["Other"] = class_rel.drop(index=top, errors="ignore").sum(axis=0)
plot_df = plot_df.T
colors = list(plt.cm.tab20.colors[:15]) + [(0.75, 0.75, 0.75)]
fig, ax = plt.subplots(figsize=(12, 6))
plot_df.plot(kind="bar", stacked=True, ax=ax, color=colors, width=0.8,
edgecolor="white", linewidth=0.3)
ax.set_xlabel("Sample ID", fontsize=12, labelpad=6)
ax.set_ylabel("Relative Abundance (%)", fontsize=12, labelpad=6)
ax.set_title("Class-Level Composition (Top 15)", fontsize=14, fontweight="bold", pad=10)
ax.tick_params(axis="x", rotation=45)
ax.legend(title="Class", bbox_to_anchor=(1.01, 1), loc="upper left",
fontsize=8, title_fontsize=9, frameon=False)
ax.set_ylim(0, 100)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig26_class_composition.png")
def _fig_order_composition(fig_dir: Path, ft: pd.DataFrame, tax: pd.DataFrame) -> Optional[str]:
"""fig27: Order-level stacked bar chart (top 15)"""
tax["Order"] = tax["Taxon"].str.extract(r"o__([^;]+)")[0].fillna("Unknown").str.strip()
tax["Order"] = tax["Order"].replace("", "Unknown")
common = ft.index.intersection(tax.index)
merged = ft.loc[common].copy()
merged["Order"] = tax.loc[common, "Order"]
order_counts = merged.groupby("Order").sum()
order_rel = order_counts.div(order_counts.sum(axis=0), axis=1) * 100
top = order_rel.mean(axis=1).sort_values(ascending=False).head(15).index.tolist()
plot_df = order_rel.loc[top].copy()
plot_df.loc["Other"] = order_rel.drop(index=top, errors="ignore").sum(axis=0)
plot_df = plot_df.T
colors = list(plt.cm.tab20.colors[:15]) + [(0.75, 0.75, 0.75)]
fig, ax = plt.subplots(figsize=(12, 6))
plot_df.plot(kind="bar", stacked=True, ax=ax, color=colors, width=0.8,
edgecolor="white", linewidth=0.3)
ax.set_xlabel("Sample ID", fontsize=12, labelpad=6)
ax.set_ylabel("Relative Abundance (%)", fontsize=12, labelpad=6)
ax.set_title("Order-Level Composition (Top 15)", fontsize=14, fontweight="bold", pad=10)
ax.tick_params(axis="x", rotation=45)
ax.legend(title="Order", bbox_to_anchor=(1.01, 1), loc="upper left",
fontsize=8, title_fontsize=9, frameon=False)
ax.set_ylim(0, 100)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig27_order_composition.png")
def _fig_simpson_pielou(fig_dir: Path, ft: pd.DataFrame) -> Optional[str]:
"""fig28: Simpson diversity + Pielou evenness (computed from feature table)"""
ft_rel = ft.div(ft.sum(axis=0), axis=1)
simpson = 1 - (ft_rel ** 2).sum(axis=0)
richness = (ft > 0).sum(axis=0)
shannon = -(ft_rel * np.log(ft_rel + 1e-10)).sum(axis=0)
pielou = shannon / np.log(richness.clip(lower=2))
fig, axes = plt.subplots(1, 2, figsize=(12, 5))
colors_s = [PALETTE[i % len(PALETTE)] for i in range(len(simpson))]
ax = axes[0]
ax.bar(range(len(simpson)), simpson.values, color=colors_s, edgecolor="white", alpha=0.85)
ax.set_xticks(range(len(simpson)))
ax.set_xticklabels(simpson.index, rotation=45, ha="right", fontsize=9)
ax.set_ylabel("Simpson Diversity (1 - D)", fontsize=11, labelpad=6)
ax.set_title("Simpson Diversity Index", fontsize=13, fontweight="bold", pad=8)
ax.axhline(simpson.mean(), color="#C44E52", lw=1.5, ls="--", alpha=0.7)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
ax = axes[1]
ax.bar(range(len(pielou)), pielou.values, color=colors_s, edgecolor="white", alpha=0.85)
ax.set_xticks(range(len(pielou)))
ax.set_xticklabels(pielou.index, rotation=45, ha="right", fontsize=9)
ax.set_ylabel("Pielou's Evenness (J')", fontsize=11, labelpad=6)
ax.set_title("Pielou's Evenness Index", fontsize=13, fontweight="bold", pad=8)
ax.axhline(pielou.mean(), color="#C44E52", lw=1.5, ls="--", alpha=0.7)
ax.set_ylim(0, 1)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
fig.suptitle("Diversity & Evenness Metrics", fontsize=15, fontweight="bold", y=1.02)
plt.tight_layout()
return _save(fig_dir, "fig28_simpson_pielou.png")
def _fig_asv_overlap(fig_dir: Path, ft: pd.DataFrame) -> Optional[str]:
"""fig29: ASV overlap UpSet-style horizontal bar chart"""
presence = (ft > 0).astype(int)
n_samples = presence.shape[1]
samples = presence.columns.tolist()
# compute intersection sizes for all non-empty subsets (top combinations)
from itertools import combinations
combo_sizes = {}
for size in range(1, min(n_samples, 4) + 1):
for combo in combinations(range(n_samples), size):
mask = presence.iloc[:, list(combo)].all(axis=1)
# exclusive to this combination
others = [j for j in range(n_samples) if j not in combo]
if others:
mask = mask & ~presence.iloc[:, others].any(axis=1)
count = mask.sum()
if count > 0:
combo_sizes[combo] = count
# also add: shared by ALL samples, shared by >= 80%
shared_all = presence.all(axis=1).sum()
shared_80 = (presence.sum(axis=1) >= n_samples * 0.8).sum()
# top 15 intersections by size
sorted_combos = sorted(combo_sizes.items(), key=lambda x: -x[1])[:15]
if not sorted_combos:
return None
fig, ax = plt.subplots(figsize=(10, 8))
labels = []
sizes = []
for combo, size in reversed(sorted_combos):
label = " & ".join(samples[j] for j in combo)
if len(label) > 30:
label = f"{len(combo)} samples"
labels.append(label)
sizes.append(size)
colors_bar = [PALETTE[i % len(PALETTE)] for i in range(len(sizes))]
ax.barh(range(len(sizes)), sizes, color=colors_bar, edgecolor="white", alpha=0.85)
ax.set_yticks(range(len(sizes)))
ax.set_yticklabels(labels, fontsize=8)
ax.set_xlabel("Number of ASVs", fontsize=12, labelpad=6)
ax.set_title(f"ASV Sharing Patterns (shared by all: {shared_all}, by >=80%: {shared_80})",
fontsize=13, fontweight="bold", pad=10)
ax.spines["top"].set_visible(False)
ax.spines["right"].set_visible(False)
plt.tight_layout()
return _save(fig_dir, "fig29_asv_overlap.png")
# ══════════════════════════════════════════════════════════════════════