| |
| import argparse |
| import json |
| from pathlib import Path |
|
|
| import matplotlib.pyplot as plt |
| import pandas as pd |
| import seaborn as sns |
|
|
|
|
| def load_json(path: Path): |
| if not path.exists(): |
| return None |
| with open(path, "r", encoding="utf-8") as f: |
| return json.load(f) |
|
|
|
|
| def savefig(path: Path): |
| path.parent.mkdir(parents=True, exist_ok=True) |
| plt.tight_layout() |
| plt.savefig(path, dpi=260, bbox_inches="tight") |
| plt.close() |
|
|
|
|
| def plot_generator_comparison(rows_csv: Path, output_dir: Path): |
| df = pd.read_csv(rows_csv) |
| methods = [m for m in ["reference", "ar_unconditional", "ar_conditioned", "diffusion_conditioned"] if m in set(df["method"])] |
| df = df[df["method"].isin(methods)].copy() |
|
|
| plt.figure(figsize=(8.5, 4.2)) |
| sns.violinplot(data=df, x="method", y="gc_content", order=methods, inner="quartile", cut=0) |
| plt.xticks(rotation=18, ha="right") |
| plt.title("GC Content Distribution") |
| savefig(output_dir / "fig_generation_gc_content.png") |
|
|
| plt.figure(figsize=(8.5, 4.2)) |
| sns.boxplot(data=df, x="method", y="max_homopolymer", order=methods, showfliers=False) |
| plt.xticks(rotation=18, ha="right") |
| plt.title("Homopolymer Length Distribution") |
| savefig(output_dir / "fig_generation_homopolymer.png") |
|
|
| plt.figure(figsize=(8.5, 4.2)) |
| sns.boxplot(data=df, x="method", y="nearest_reference_hamming", order=methods, showfliers=False) |
| plt.xticks(rotation=18, ha="right") |
| plt.title("Nearest Reference Hamming Distance") |
| savefig(output_dir / "fig_nearest_reference_distance.png") |
|
|
| scored = df[df["prediction_sum"].notna()].copy() |
| if not scored.empty: |
| plt.figure(figsize=(8.5, 4.2)) |
| sns.violinplot(data=scored, x="method", y="prediction_sum", order=[m for m in methods if m in set(scored["method"])], inner="quartile", cut=0) |
| plt.xticks(rotation=18, ha="right") |
| plt.title("Predicted Enhancer Activity by Method") |
| savefig(output_dir / "fig_predicted_activity_by_method.png") |
|
|
| two_dim = df[df["prediction_label_0"].notna() & df["prediction_label_1"].notna()].copy() |
| if not two_dim.empty: |
| plt.figure(figsize=(6.2, 5.2)) |
| sns.scatterplot( |
| data=two_dim, |
| x="prediction_label_0", |
| y="prediction_label_1", |
| hue="method", |
| style="activity_bucket" if "activity_bucket" in two_dim else None, |
| s=18, |
| alpha=0.65, |
| ) |
| plt.title("Predicted Activity Space") |
| savefig(output_dir / "fig_activity_2d_scatter.png") |
|
|
| pll = df[df["diffusion_pll"].notna()].copy() |
| if not pll.empty: |
| plt.figure(figsize=(7.2, 4.2)) |
| sns.violinplot(data=pll, x="source", y="diffusion_pll", hue="activity_bucket", inner="quartile", cut=0) |
| plt.title("Diffusion PLL by Source and Bucket") |
| savefig(output_dir / "fig_diffusion_pll.png") |
|
|
|
|
| def plot_summary_table(summary_json: Path, output_dir: Path): |
| summary = load_json(summary_json) |
| if not summary: |
| return |
| rows = [] |
| for method, values in summary.get("methods", {}).items(): |
| row = {"method": method} |
| for key in [ |
| "valid_dna_rate", |
| "unique_rate", |
| "mean_gc_content", |
| "mean_max_homopolymer", |
| "mean_nearest_reference_hamming", |
| "kmer3_js_to_reference", |
| "kmer4_js_to_reference", |
| "mean_prediction_sum", |
| "mean_diffusion_pll", |
| ]: |
| if key in values: |
| row[key] = values[key] |
| rows.append(row) |
| if not rows: |
| return |
| table = pd.DataFrame(rows) |
| table.to_csv(output_dir / "table_generator_comparison.csv", index=False) |
|
|
| plot_cols = [c for c in ["valid_dna_rate", "unique_rate", "kmer4_js_to_reference", "mean_nearest_reference_hamming"] if c in table] |
| long = table.melt(id_vars="method", value_vars=plot_cols, var_name="metric", value_name="value") |
| plt.figure(figsize=(9.2, 4.4)) |
| sns.barplot(data=long, x="metric", y="value", hue="method") |
| plt.xticks(rotation=18, ha="right") |
| plt.title("Generator Quality Summary") |
| savefig(output_dir / "fig_generator_quality_summary.png") |
|
|
|
|
| def plot_motif(motif_csv: Path, output_dir: Path): |
| if not motif_csv.exists(): |
| return |
| df = pd.read_csv(motif_csv) |
| if df.empty: |
| return |
| top = ( |
| df.groupby("motif_name")["hit_rate"] |
| .max() |
| .sort_values(ascending=False) |
| .head(20) |
| .index |
| ) |
| sub = df[df["motif_name"].isin(top)] |
| pivot = sub.pivot_table(index="motif_name", columns="method", values="hit_rate", fill_value=0) |
| plt.figure(figsize=(8.5, 6.5)) |
| sns.heatmap(pivot, cmap="viridis") |
| plt.title("Top Motif Hit Rates") |
| savefig(output_dir / "fig_motif_hit_rate_heatmap.png") |
|
|
|
|
| def main(): |
| parser = argparse.ArgumentParser(description="Build paper-ready figures from experiment outputs.") |
| parser.add_argument("--result_root", required=True) |
| parser.add_argument("--output_dir", required=True) |
| args = parser.parse_args() |
|
|
| result_root = Path(args.result_root) |
| output_dir = Path(args.output_dir) |
| output_dir.mkdir(parents=True, exist_ok=True) |
| sns.set_theme(style="whitegrid") |
|
|
| plot_generator_comparison(result_root / "sequence_metrics" / "sequence_metrics_rows.csv", output_dir) |
| plot_summary_table(result_root / "sequence_metrics" / "sequence_metrics_summary.json", output_dir) |
| plot_motif(result_root / "motif_analysis" / "motif_scan_summary.csv", output_dir) |
| print(output_dir) |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|
|
|