|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import json |
| 4 | +from pathlib import Path |
| 5 | +from types import SimpleNamespace |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import pytest |
| 9 | + |
| 10 | +from fastmdxplora.report.dashboard import build_dashboard |
| 11 | +from fastmdxplora.report.region_highlights import ( |
| 12 | + RegionHighlight, |
| 13 | + build_pymol_script, |
| 14 | + build_region_highlight_artifacts, |
| 15 | + validate_region_highlights, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +PNG_BYTES = ( |
| 20 | + b"\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01" |
| 21 | + b"\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde" |
| 22 | + b"\x00\x00\x00\x0cIDATx\x9cc```\x00\x00\x00\x04\x00\x01" |
| 23 | + b"\xf6\x178U\x00\x00\x00\x00IEND\xaeB`\x82" |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +def test_static_dashboard_discovers_sections_links_and_dark_assets(tmp_path: Path) -> None: |
| 28 | + root = tmp_path / "run" |
| 29 | + _write_json( |
| 30 | + root / "manifest.json", |
| 31 | + { |
| 32 | + "system": "demo-system", |
| 33 | + "phases": [ |
| 34 | + {"name": "analysis", "status": "ok"}, |
| 35 | + {"name": "report", "status": "ok"}, |
| 36 | + ], |
| 37 | + }, |
| 38 | + ) |
| 39 | + _write_json( |
| 40 | + root / "analysis" / "analysis_manifest.json", |
| 41 | + {"n_frames": 2, "n_atoms": 12, "results": {"rmsd": {"status": "ok"}}}, |
| 42 | + ) |
| 43 | + (root / "report").mkdir(parents=True) |
| 44 | + (root / "report" / "report.md").write_text("# Report\n", encoding="utf-8") |
| 45 | + (root / "report" / "slides.pptx").write_bytes(b"pptx") |
| 46 | + (root / "report" / "project_bundle.zip").write_bytes(b"zip") |
| 47 | + |
| 48 | + _write_plot(root, "analysis/rmsd/rmsd.png", "analysis/rmsd/rmsd.dat", "0 0.10\n1 0.20\n") |
| 49 | + _write_plot(root, "analysis/rg/rg.png", "analysis/rg/rg.dat", "0 1.0\n1 1.2\n") |
| 50 | + _write_plot(root, "analysis/sasa/sasa.png", "analysis/sasa/sasa.dat", "0 20.0\n1 22.0\n") |
| 51 | + _write_plot(root, "analysis/dimred/dimred_pca.png", "analysis/dimred/dimred_pca.dat", "0 0.1 0.2\n1 0.3 0.4\n") |
| 52 | + _write_plot(root, "analysis/cluster/cluster_kmeans.png", "analysis/cluster/cluster_kmeans.dat", "frame,cluster\n0,1\n1,2\n2,1\n") |
| 53 | + _write_plot(root, "analysis/cluster/cluster_kmeans_counts.png", None, None) |
| 54 | + _write_plot(root, "analysis/ss/ss.png", "analysis/ss/ss.dat", "frame,1,2\n0,H,C\n1,E,T\n") |
| 55 | + _write_plot(root, "analysis/qvalue/qvalue.png", "analysis/qvalue/qvalue.dat", "0.9\n0.8\n") |
| 56 | + (root / "report" / "region_highlight_summary.png").write_bytes(PNG_BYTES) |
| 57 | + |
| 58 | + artifacts = build_dashboard( |
| 59 | + orchestrator=SimpleNamespace(output_dir=root, system="demo-system"), |
| 60 | + output_dir=root / "report", |
| 61 | + title="Demo Dashboard", |
| 62 | + include_bundle_link=True, |
| 63 | + ) |
| 64 | + |
| 65 | + html = (root / "report" / "dashboard.html").read_text(encoding="utf-8") |
| 66 | + assert artifacts == ["dashboard.html"] |
| 67 | + for section in ( |
| 68 | + "Core Metrics", |
| 69 | + "SASA", |
| 70 | + "Secondary Structure", |
| 71 | + "Dimensionality Reduction", |
| 72 | + "Clustering", |
| 73 | + "Region Highlights", |
| 74 | + "Other", |
| 75 | + ): |
| 76 | + assert section in html |
| 77 | + for link in ( |
| 78 | + "report.md", |
| 79 | + "slides.pptx", |
| 80 | + "project_bundle.zip", |
| 81 | + "../analysis/analysis_manifest.json", |
| 82 | + "../analysis/rmsd/rmsd.png", |
| 83 | + "../analysis/cluster/cluster_kmeans_counts.png", |
| 84 | + ): |
| 85 | + assert link in html |
| 86 | + for asset in ( |
| 87 | + "rmsd_dashboard.png", |
| 88 | + "rg_dashboard.png", |
| 89 | + "sasa_dashboard.png", |
| 90 | + "pca_dashboard.png", |
| 91 | + "kmeans_trajectory_dashboard.png", |
| 92 | + "kmeans_population_dashboard.png", |
| 93 | + "ss_dashboard.png", |
| 94 | + "qvalue_dashboard.png", |
| 95 | + ): |
| 96 | + assert (root / "report" / "dashboard_assets" / asset).is_file() |
| 97 | + assert f"dashboard_assets/{asset}" in html |
| 98 | + assert "dashboard view" in html |
| 99 | + assert "artifact fallback" in html |
| 100 | + assert "Analysis/report workflow from existing trajectory." in html |
| 101 | + |
| 102 | + |
| 103 | +def test_region_highlights_validate_ranges_and_defaults() -> None: |
| 104 | + regions = validate_region_highlights( |
| 105 | + [{"start": 2, "end": 4}, {"label": "Loop", "start": 5, "end": 5, "color": "red"}], |
| 106 | + np.array([1, 2, 3, 4, 5]), |
| 107 | + ) |
| 108 | + |
| 109 | + assert regions[0] == RegionHighlight("Region 1", 2, 4, "#4E79A7") |
| 110 | + assert regions[1].label == "Loop" |
| 111 | + assert regions[1].color == "red" |
| 112 | + |
| 113 | + with pytest.raises(ValueError, match="end must be >= start"): |
| 114 | + validate_region_highlights([{"start": 4, "end": 3}], np.array([1, 2, 3, 4])) |
| 115 | + with pytest.raises(ValueError, match="outside the RMSF residue range"): |
| 116 | + validate_region_highlights([{"start": 1, "end": 10}], np.array([2, 3, 4])) |
| 117 | + with pytest.raises(ValueError, match="missing required key"): |
| 118 | + validate_region_highlights([{"end": 3}], np.array([1, 2, 3])) |
| 119 | + |
| 120 | + |
| 121 | +def test_region_highlight_artifacts_and_dashboard_metadata(tmp_path: Path) -> None: |
| 122 | + root = tmp_path / "run" |
| 123 | + report = root / "report" |
| 124 | + rmsf = root / "analysis" / "rmsf" / "rmsf.dat" |
| 125 | + rmsf.parent.mkdir(parents=True) |
| 126 | + rmsf.write_text("1 0.1\n2 0.3\n3 0.2\n4 0.5\n", encoding="utf-8") |
| 127 | + _write_json( |
| 128 | + root / "manifest.json", |
| 129 | + {"system": "demo", "phases": [{"name": "analysis", "status": "ok"}]}, |
| 130 | + ) |
| 131 | + _write_json(root / "analysis" / "analysis_manifest.json", {"n_frames": 4}) |
| 132 | + |
| 133 | + artifacts = build_region_highlight_artifacts( |
| 134 | + project_root=root, |
| 135 | + output_dir=report, |
| 136 | + region_highlights=[{"label": "active loop", "start": 2, "end": 3, "color": "#E15759"}], |
| 137 | + ) |
| 138 | + |
| 139 | + manifest = json.loads((report / "region_highlight_manifest.json").read_text(encoding="utf-8")) |
| 140 | + assert "region_highlight_manifest.json" in artifacts |
| 141 | + assert "region_highlight_summary.png" in artifacts |
| 142 | + assert (root / "analysis" / "rmsf" / "rmsf_region_highlights.png").is_file() |
| 143 | + assert (report / "region_highlight_summary.png").is_file() |
| 144 | + assert manifest["status"] == "ok" |
| 145 | + assert manifest["skipped"][0]["artifact"] == "structure_region_highlights.png" |
| 146 | + |
| 147 | + build_dashboard( |
| 148 | + orchestrator=SimpleNamespace(output_dir=root, system="demo"), |
| 149 | + output_dir=report, |
| 150 | + title="Region Dashboard", |
| 151 | + ) |
| 152 | + html = (report / "dashboard.html").read_text(encoding="utf-8") |
| 153 | + assert "Region Highlights" in html |
| 154 | + assert "region_highlight_summary.png" in html |
| 155 | + assert "rmsf_region_highlights.png" in html |
| 156 | + |
| 157 | + |
| 158 | +def test_region_highlight_pymol_script_contains_region_commands(tmp_path: Path) -> None: |
| 159 | + script = build_pymol_script( |
| 160 | + topology_path=tmp_path / "topology with spaces.pdb", |
| 161 | + output_path=tmp_path / "out.png", |
| 162 | + regions=[ |
| 163 | + RegionHighlight("Alpha", 2, 5, "#E15759"), |
| 164 | + RegionHighlight("Beta", 8, 9, "not-a-color"), |
| 165 | + ], |
| 166 | + ) |
| 167 | + |
| 168 | + assert "load " in script |
| 169 | + assert "set_color fastmdx_region_1, [0.8824, 0.3412, 0.3490]" in script |
| 170 | + assert "select fastmdx_sel_1, prot and polymer.protein and resi 2-5" in script |
| 171 | + assert "show sticks, fastmdx_sel_2 and sidechain" in script |
| 172 | + assert "png " in script |
| 173 | + assert "ray 1800, 1200" in script |
| 174 | + |
| 175 | + |
| 176 | +def _write_json(path: Path, data: dict) -> None: |
| 177 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 178 | + path.write_text(json.dumps(data), encoding="utf-8") |
| 179 | + |
| 180 | + |
| 181 | +def _write_plot(root: Path, image_rel: str, data_rel: str | None, data: str | None) -> None: |
| 182 | + image = root / image_rel |
| 183 | + image.parent.mkdir(parents=True, exist_ok=True) |
| 184 | + image.write_bytes(PNG_BYTES) |
| 185 | + if data_rel is not None and data is not None: |
| 186 | + path = root / data_rel |
| 187 | + path.parent.mkdir(parents=True, exist_ok=True) |
| 188 | + path.write_text(data, encoding="utf-8") |
0 commit comments