from __future__ import annotations import json import subprocess import sys from pathlib import Path ROOT = Path(__file__).resolve().parents[2] def test_msa_region_plot_inherits_scope_from_source_manifest(tmp_path): workspace = tmp_path / "job_msa_scope" final_dir = workspace / "final" final_dir.mkdir(parents=True) prefix = "Target.SPL.target-Lc.ref-At.subfamily_prediction" msa = final_dir / f"{prefix}.pep.msa.fa" msa.write_text( ">LcGene1\nACDEFGHIKL\n" ">LcGene2\nACDEYGHIKL\n" ">AtGene1\nACDEFGHIKL\n", encoding="utf-8", ) source_manifest = final_dir / f"{prefix}.phylogeny.manifest.json" source_manifest.write_text( json.dumps({ "manifest_version": "1.0", "stage": "phylogeny", "status": "success", "prefix": "Lc.SPL", "output_prefix": prefix, "scope_id": "SPL.target-Lc.ref-At.target_plus_reference", "scope_mode": "target_plus_reference", "target_species": ["Lc"], "reference_species": ["At"], "comparative_species": [], "selected_species": ["Lc", "At"], "analysis_route": "subfamily_prediction", "member_set_id": "SPL.target-Lc.ref-At.target_plus_reference:subfamily_prediction:all_members", "artifacts": {"msa": {"full_path": str(msa), "type": "fasta"}}, }), encoding="utf-8", ) fake_r = tmp_path / "fake_msa_plot.sh" fake_r.write_text( "#!/usr/bin/env bash\n" "outbase=\"$2\"\n" "printf fake > \"${outbase}_msa.png\"\n" "printf fake > \"${outbase}_msa.pdf\"\n", encoding="utf-8", ) fake_r.chmod(0o755) proc = subprocess.run( [ sys.executable, str(ROOT / "apps/cli/run_msa_region_plot.py"), "--workspace", str(workspace), "--prefix", prefix, "--msa-file", str(msa), "--auto-conserved", "--window-len", "4", "--min-nongap", "0", "--source-manifest", str(source_manifest), "--rscript", "/bin/bash", "--r-script", str(fake_r), "--force", ], cwd=str(ROOT), capture_output=True, text=True, check=False, ) assert proc.returncode == 0, proc.stderr or proc.stdout manifests = list(final_dir.glob(f"{prefix}.msa_conserved_region_*.manifest.json")) assert len(manifests) == 1 manifest = json.loads(manifests[0].read_text(encoding="utf-8")) assert manifest["scope_id"] == "SPL.target-Lc.ref-At.target_plus_reference" assert manifest["target_species"] == ["Lc"] assert manifest["reference_species"] == ["At"] assert manifest["analysis_route"] == "subfamily_prediction" assert manifest["member_set_id"] == "SPL.target-Lc.ref-At.target_plus_reference:subfamily_prediction:all_members" assert manifest["stage_context"]["scope_id"] == "SPL.target-Lc.ref-At.target_plus_reference" assert manifest["parameters"]["source_manifest"] == str(source_manifest.resolve()) assert manifest["source_manifest"] == str(source_manifest.resolve()) assert any(path.endswith("_msa.png") for path in manifest["artifacts"]["output_files"])