diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml new file mode 100644 index 0000000..0f82471 --- /dev/null +++ b/.github/workflows/unit-tests.yml @@ -0,0 +1,34 @@ +name: Python unit tests + +on: + pull_request: + branches: [main] + +permissions: + contents: read + +jobs: + unit-tests: + runs-on: ubuntu-latest + timeout-minutes: 10 + env: + MPLBACKEND: Agg + PYTHONDONTWRITEBYTECODE: '1' + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-python@v5 + with: + python-version: '3.12' + - name: Install test dependencies + run: >- + python -m pip install + pytest coverage nbformat requests pyliftover + 'numpy>=2.2' 'pandas>=2.2' 'pysam>=0.23.3' 'matplotlib>=3.10' 'scipy>=1.17' + - name: Run offline unit tests with coverage + run: | + export MPLCONFIGDIR="$RUNNER_TEMP/matplotlib" + export COVERAGE_FILE="$RUNNER_TEMP/encode.coverage" + python -m coverage run \ + --include='downloads/*.py,notebooks/*.py,notebooks/workflow/*.py,data_and_code/picalm/*.py' \ + -m pytest -q -p no:cacheprovider --basetemp="$RUNNER_TEMP/pytest" unit_tests + python -m coverage report --precision=2 --show-missing --fail-under=90 diff --git a/unit_tests/conftest.py b/unit_tests/conftest.py new file mode 100644 index 0000000..0a47614 --- /dev/null +++ b/unit_tests/conftest.py @@ -0,0 +1,121 @@ +"""Synthetic inputs shared by the offline unit suite.""" + +import importlib.util +import sys +from pathlib import Path + +import pytest +import requests + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / "notebooks" / "workflow")) +sys.path.insert(0, str(ROOT / "notebooks")) +import workflow_utils as workflow_utils +import cs3_utils as cs3_utils + +spec = importlib.util.spec_from_file_location( + "prepare_events", ROOT / "downloads" / "prepare_events.py" +) +prepare_events = importlib.util.module_from_spec(spec) +spec.loader.exec_module(prepare_events) + + +@pytest.fixture(autouse=True) +def offline(monkeypatch): + def reject(*args, **kwargs): + raise AssertionError("Unit tests must not contact a remote service") + + monkeypatch.setattr(requests.sessions.Session, "request", reject) + + +@pytest.fixture +def events(): + def make(**changes): + row = dict( + ID="first", + GeneID='"ENSG00000000001.2"', + geneSymbol="GENE", + chr="chr1", + strand="+", + exonStart_0base=100, + exonEnd=120, + upstreamES=10, + upstreamEE=30, + downstreamES=200, + downstreamEE=220, + IJC_SAMPLE_1="10,20", + SJC_SAMPLE_1="2,3", + IJC_SAMPLE_2="5,5", + SJC_SAMPLE_2="10,10", + IncLevel1="0.7,0.9", + IncLevel2="0.4,0.6", + IncLevelDifference=0.2, + FDR=0.01, + PValue=0.01, + ) + row.update(changes) + return row + + return make + + +class OffsetLift: + def convert_coordinate(self, chrom, pos): + return [(chrom, pos + 1000, "+", 100)] + + +@pytest.fixture +def lift(): + return OffsetLift() + + +@pytest.fixture +def locus(): + exons = {k: list(v["grch38"][2:]) for k, v in workflow_utils.EXONS.items()} + a, b = exons["exon13"], exons["exon24nt"] + outside = [[85970000, 85970100], [85996000, 85996100]] + tx = { + "include": dict( + name="PICALM-1", + type="protein_coding", + tags=["MANE_Select"], + exons=sorted(outside + [a, b]), + ), + "skip": dict(name="PICALM-2", type="protein_coding", tags=[], exons=outside), + "short": dict( + name="PICALM-3", + type="protein_coding", + tags=[], + exons=sorted(outside + [[a[0], a[1] - 21]]), + ), + "noncoding": dict( + name="PICALM-4", type="retained_intron", tags=[], exons=outside + ), + } + return dict( + exons=exons, + transcripts=tx, + windows=[ + ["QKI_K562_5007", a[1], a[1] + 100, 4, 0.01], + ["QKI_HepG2_ENCSR570WLM", b[0] - 100, b[0], 2.5, 0.01], + ], + ) + + +@pytest.fixture +def knockdown(): + st = dict( + psi_ctl=[0.3, 0.4], + psi_kd=[0.6, 0.7], + IJC_ctl=[20, 30], + SJC_ctl=[50, 60], + IJC_kd=[60, 70], + SJC_kd=[20, 30], + dpsi=0.3, + fdr=0.01, + pvalue=0.001, + ) + return { + k: dict(log2fc=-2.0, exon13=dict(st), exon24nt=dict(st, dpsi=-0.3, fdr=0.6)) + for k in workflow_utils.KNOCKDOWNS + } diff --git a/unit_tests/test_download_contracts.py b/unit_tests/test_download_contracts.py new file mode 100644 index 0000000..52abf95 --- /dev/null +++ b/unit_tests/test_download_contracts.py @@ -0,0 +1,404 @@ +"""Cached HTTP responses and small synthetic ENCODE/RBP-ARK files.""" + +import gzip +import hashlib +import io +import json +import tarfile +from pathlib import Path +from types import SimpleNamespace +from unittest.mock import Mock + +import numpy as np +import pandas as pd +import pytest +import requests + +from conftest import workflow_utils, cs3_utils, prepare_events + + +def archive(path, members): + with tarfile.open(path, "w:gz") as tar: + for name, data in members.items(): + data = data.encode() + info = tarfile.TarInfo(name) + info.size = len(data) + tar.addfile(info, io.BytesIO(data)) + return path + + +@pytest.mark.parametrize("module", [workflow_utils, cs3_utils]) +def test_fetch_retries_then_atomically_caches(tmp_path, monkeypatch, module): + retry = SimpleNamespace(status_code=503, headers={"Retry-After": "2"}) + success = Mock(status_code=200) + success.iter_content.return_value = [b"abc", b"", b"def"] + get = Mock(side_effect=[retry, success]) + sleep = Mock() + monkeypatch.setattr(module.requests, "get", get) + monkeypatch.setattr(module.time, "sleep", sleep) + out = tmp_path / "nested" / "file.gz" + assert module.fetch("https://unit.invalid/file", out, params={"x": 1}) == out + assert out.read_bytes() == b"abcdef" and not out.with_suffix(".gz.part").exists() + assert get.call_count == 2 + sleep.assert_called_once_with(2) + assert module.fetch("unused", out) == out and get.call_count == 2 + + +@pytest.mark.parametrize("module", [workflow_utils, cs3_utils]) +def test_fetch_errors_and_zero_attempts(tmp_path, monkeypatch, module): + response = Mock(status_code=500) + response.raise_for_status.side_effect = requests.HTTPError("server failure") + monkeypatch.setattr(module.requests, "get", Mock(return_value=response)) + with pytest.raises(requests.HTTPError): + module.fetch("unit", tmp_path / "file") + assert not (tmp_path / "file").exists() + with pytest.raises(RuntimeError, match="could not download"): + module.fetch("unit", tmp_path / "file", retries=0) + + +def test_large_download_checks_size_digest_and_marker(tmp_path, monkeypatch): + out = tmp_path / "file" + digest = hashlib.sha256(b"data").hexdigest() + + def curl(args): + out.write_bytes(b"data") + + call = Mock(side_effect=curl) + monkeypatch.setattr(workflow_utils.subprocess, "check_call", call) + assert workflow_utils.download_large("unit", out, 4, digest) == out + assert workflow_utils.checksum(out, chunk=1) == digest + assert ( + workflow_utils.download_large("unit", out, 4, digest) == out + and call.call_count == 1 + ) + out.with_name("file.sha256ok").unlink() + with pytest.raises(AssertionError, match="checksum mismatch"): + workflow_utils.download_large("unit", out, 4, "bad") + monkeypatch.setattr(workflow_utils.subprocess, "check_call", lambda args: None) + with pytest.raises(AssertionError, match="size"): + workflow_utils.download_large("unit", out, 5) + + +def test_json_helpers_send_parameters_and_read_cache(tmp_path, monkeypatch): + response = Mock() + response.json.return_value = {"value": 1} + get = Mock(return_value=response) + monkeypatch.setattr(cs3_utils.requests, "get", get) + assert cs3_utils.encode_json("/files/A/", field="x") == {"value": 1} + assert get.call_args.kwargs["params"] == {"field": "x", "format": "json"} + payload = tmp_path / "payload.json" + payload.write_text('{"value": 2}') + fetch = Mock(return_value=payload) + monkeypatch.setattr(workflow_utils, "fetch", fetch) + assert workflow_utils.rbpark_json("/api/gene/", tmp_path, fields="a,b") == { + "value": 2 + } + assert fetch.call_args.args[1].name == "api_gene_fields-a+b.json" + assert workflow_utils.encode_json("/files/A/", tmp_path) == {"value": 2} + assert fetch.call_args.kwargs["params"] == {"format": "json"} + + +def test_rbp_ark_records_tables_and_duplicate_files(tmp_path, monkeypatch): + url = "https://host.invalid/a/file.bam" + kv = [ + {"key": "path", "value": url}, + {"key": "filesize", "value": "12"}, + {"key": "sha256", "value": "abc"}, + ] + record = { + "content": { + "summary_data": {"basic_stats": {"mapped": [{"key": "IP_1", "value": 20}]}}, + "filelist": [kv, kv], + } + } + api = Mock(return_value=record) + monkeypatch.setattr(workflow_utils, "rbpark_json", api) + assert workflow_utils.experiment_record("subject", tmp_path) == record + assert workflow_utils.read_stats("subject", tmp_path).loc["IP_1", "mapped"] == 20 + result = workflow_utils.file_list("subject", tmp_path) + assert result.to_dict("records") == [ + dict(url=url, collection_path="/a/file.bam", size=12, sha256="abc") + ] + payload = tmp_path / "files.json" + payload.write_text( + json.dumps( + { + "content": { + "filelist": { + "a": [kv, {"nested": [kv]}], + "ignored": [[{"key": "other"}]], + } + } + } + ) + ) + monkeypatch.setattr(cs3_utils, "fetch", lambda *a, **k: payload) + assert cs3_utils.rbpark_files("subject", tmp_path).url.tolist() == [url] + monkeypatch.setattr( + workflow_utils, + "rbpark_json", + lambda *a, **k: { + "experiments": [{"subject": "s", "rbp": "QKI", "windows": [{"start": 1}]}] + }, + ) + assert workflow_utils.gene_search("G", tmp_path).to_dict("records") == [ + dict(subject="s", rbp="QKI", start=1) + ] + + +@pytest.mark.parametrize( + "module,function", + [(workflow_utils, "experiments_table"), (cs3_utils, "rbpark_experiments")], +) +def test_experiment_catalog_reads_tsv(tmp_path, monkeypatch, module, function): + p = tmp_path / "experiments.tsv" + p.write_text("subject\trbp\ns\tQKI\n") + monkeypatch.setattr(module, "fetch", lambda *a, **k: p) + assert getattr(module, function)(tmp_path).rbp.tolist() == ["QKI"] + + +def test_window_tables_filter_chunks_rename_columns_and_half_open_overlap( + tmp_path, monkeypatch +): + p = tmp_path / "windows.tsv.gz" + table = pd.DataFrame( + [ + dict( + subject="s", + rbp="QKI", + chr="chr1", + strand="+", + gene_name="G", + gene_id="id", + start=10, + end=20, + enrichment_l2or_mean=3, + q_max=0.01, + ), + dict( + subject="s", + rbp="OTHER", + chr="chr1", + strand="+", + gene_name="G", + gene_id="id", + start=20, + end=30, + enrichment_l2or_mean=2, + q_max=0.02, + ), + ] + ) + table.to_csv(p, sep="\t", index=False) + monkeypatch.setattr(cs3_utils, "fetch", lambda *a, **k: p) + assert len(cs3_utils.rbpark_windows(tmp_path, rbps=["QKI"], chunksize=1)) == 1 + assert len(cs3_utils.rbpark_windows(tmp_path, chunksize=1)) == 2 + monkeypatch.setattr( + workflow_utils, + "file_list", + lambda *a: pd.DataFrame( + [ + dict( + url="unit", + collection_path="/reproducible_enriched_windows/s.reproducible_enriched_windows.tsv.gz", + ) + ] + ), + ) + monkeypatch.setattr(workflow_utils, "fetch", lambda *a, **k: p) + result = workflow_utils.reproducible_windows("s", tmp_path) + assert result.log2_enrichment.tolist() == [3, 2] + assert workflow_utils.windows_in(result, "chr1", "+", 10, 20).start.tolist() == [10] + assert workflow_utils.windows_in(result, "chr1", "-", 10, 20).empty + + +def test_bam_selection_prefers_read2_and_deduplicates_reused_input(monkeypatch): + subject = "QKI_K562_5007" + rows = [ + dict( + collection_path=f"/bams/dedup/{folder}/{subject}_{sample}.genome.Aligned.sort.dedup{suffix}.bam", + url="unit", + ) + for sample in ["IP_1", "IN_1", "IP_2", "IN_2"] + for folder, suffix in [("genome", ""), ("genome_R2", ".R2")] + ] + table = pd.DataFrame(rows) + assert ( + "genome_R2" in workflow_utils.pick_bam(table, subject, "IP_1").collection_path + ) + with pytest.raises(AssertionError, match="no deduplicated BAM"): + workflow_utils.pick_bam(table, subject, "IP_9") + monkeypatch.setattr(workflow_utils, "MAP_PAIRS", [(subject, "kd")]) + monkeypatch.setattr(workflow_utils, "file_list", lambda *a: table) + result = workflow_utils.eclip_bams("cache") + assert sorted(result["sample"]) == ["IN_1", "IN_2", "IP_1", "IP_2"] + + +def test_encode_knockdown_parents_accept_strings_or_objects(monkeypatch): + kd = workflow_utils.KNOCKDOWNS["QKI_K562"] + + def metadata(path, cache): + if path == f"/files/{kd['rmats']}/": + return {"derived_from": ["/files/a/", {"@id": "/files/b/"}]} + control = path == "/files/b/" + return dict( + dataset={ + "@id": "/experiments/" + + (kd["control_experiment"] if control else kd["kd_experiment"]) + + "/" + }, + biological_replicates=[2 if control else 1], + accession="b" if control else "a", + assembly="hg19", + file_size=12, + md5sum="md5", + href=path + "bam", + ) + + monkeypatch.setattr(workflow_utils, "encode_json", metadata) + result = workflow_utils.knockdown_bams("QKI_K562", "cache") + assert result.condition.tolist() == ["knockdown", "control"] + assert result.url.str.startswith(workflow_utils.ENCODE).all() + get = Mock(return_value=Path("out")) + monkeypatch.setattr(workflow_utils, "download_large", get) + assert workflow_utils.encode_file("a", "folder", "cache") == Path("out") + assert get.call_args.args[-1] == "md5" + + +def test_legacy_knockdown_catalog_and_smallest_successful_tar(monkeypatch, events): + monkeypatch.setattr( + cs3_utils, + "encode_json", + lambda *a, **k: { + "@graph": [ + dict( + accession="A", + dataset="/experiments/E/", + biosample_ontology={"term_name": "K562"}, + ) + ] + }, + ) + files = cs3_utils.knockdown_files("splicing") + assert files.iloc[0].to_dict() == dict( + accession="A", experiment="E", target=None, cell="K562", size=0 + ) + files = pd.DataFrame( + [ + dict(accession="big", target="QKI", cell="K562", size=2), + dict(accession="small", target="QKI", cell="K562", size=1), + ] + ) + calls = [] + + def load(acc, cache): + calls.append(acc) + return ( + None + if acc == "small" + else pd.DataFrame([events(), events(GeneID="ENSG00000151923.2")]) + ) + + monkeypatch.setattr(cs3_utils, "rmats_se", load) + acc, table = cs3_utils.se_for("QKI", "K562", files, "cache") + assert calls == ["small", "big"] and acc == "big" and len(table) == 1 + assert cs3_utils.se_for("other", "K562", files, "cache") == (None, None) + + +def test_tar_readers_choose_mats_not_normalized_output(tmp_path, monkeypatch): + data = 'geneSymbol\tvalue\n"PICALM"\t7\n' + tar = archive( + tmp_path / "events.tar.gz", + { + "x/MATS_Norm_output/SE.MATS.JunctionCountOnly.txt": "wrong", + "x/MATS_output/SE.MATS.JunctionCountOnly.txt": data, + }, + ) + assert prepare_events.se_table(tar) == data.splitlines() + monkeypatch.setattr(cs3_utils, "fetch", lambda *a, **k: tar) + assert cs3_utils.rmats_se("A", tmp_path).geneSymbol.tolist() == ["PICALM"] + out = tmp_path / "extracted" / "se.txt" + assert workflow_utils.encode_rmats_se(tar, out).read_text() == data + assert workflow_utils.encode_rmats_se("not-used", out) == out + bad = archive(tmp_path / "empty.tar.gz", {"other.txt": "empty"}) + with pytest.raises(SystemExit, match="no MATS_output"): + prepare_events.se_table(bad) + monkeypatch.setattr(cs3_utils, "fetch", lambda *a, **k: bad) + assert cs3_utils.rmats_se("A", tmp_path) is None + + +def test_deseq_tables_match_gene_versions_and_skip_broken_files(tmp_path, monkeypatch): + p = tmp_path / "table.tsv" + p.write_text("gene\tlog2FoldChange\nENSG1.2\t-2.5\n") + assert workflow_utils.deseq2_log2fc(p, "ENSG1") == -2.5 + assert np.isnan(workflow_utils.deseq2_log2fc(p, "missing")) + monkeypatch.setattr( + cs3_utils, + "encode_json", + lambda *a, **k: { + "@graph": [ + dict(accession="bad", file_size=1), + dict(accession="ok", file_size=2), + ] + }, + ) + + def fetch(url, *a, **k): + if "/bad/" in url: + raise requests.HTTPError("not available") + return p + + monkeypatch.setattr(cs3_utils, "fetch", fetch) + assert cs3_utils.deseq2_log2fc("E", "ENSG1", tmp_path) == {"ok": -2.5} + + +@pytest.mark.parametrize("module", [workflow_utils, cs3_utils]) +def test_gencode_transcripts_use_zero_based_sorted_exons(tmp_path, monkeypatch, module): + p = tmp_path / "gencode.gz" + attrs = 'gene_name "PICALM"; transcript_id "T1"; transcript_name "PICALM-1"; transcript_type "protein_coding"; tag "MANE_Select";' + with gzip.open(p, "wt") as f: + f.write("# other gene\n") + for feature, start, end in [ + ("gene", 1, 99), + ("transcript", 1, 99), + ("exon", 31, 40), + ("exon", 11, 20), + ]: + f.write(f"chr1\tG\t{feature}\t{start}\t{end}\t.\t-\t.\t{attrs}\n") + monkeypatch.setattr(module, "fetch", lambda *a, **k: p) + tx = module.gencode_transcripts("PICALM", tmp_path) + assert tx["T1"]["exons"] == [[10, 20], [30, 40]] + assert tx["T1"]["tags"] == ["MANE_Select"] + + +@pytest.mark.parametrize("module", [workflow_utils, cs3_utils]) +def test_lifter_is_initialized_once_from_cached_chain(tmp_path, monkeypatch, module): + import pyliftover + + loader = Mock(return_value=object()) + monkeypatch.setattr(pyliftover, "LiftOver", loader) + monkeypatch.setattr(module, "_LIFT", None) + monkeypatch.setattr(module, "fetch", lambda *a, **k: tmp_path / "chain.gz") + assert module.lifter(tmp_path) is module.lifter(tmp_path) + loader.assert_called_once_with(str(tmp_path / "chain.gz")) + + +@pytest.mark.parametrize("strand,expected", [("+", "ACGUN"), ("-", "NACGU")]) +def test_rna_sequence_both_apis(tmp_path, monkeypatch, strand, expected): + p = tmp_path / "dna.json" + p.write_text('{"dna":"acgtn"}') + monkeypatch.setattr(workflow_utils, "fetch", lambda *a, **k: p) + response = Mock() + response.json.return_value = {"dna": "acgtn"} + monkeypatch.setattr(cs3_utils.requests, "get", Mock(return_value=response)) + assert workflow_utils.rna_sequence("chr1", 0, 5, strand, tmp_path) == expected + assert cs3_utils.ucsc_sequence("chr1", 0, 5, strand) == expected + + +@pytest.mark.parametrize("module", [workflow_utils, cs3_utils]) +def test_folders_honor_explicit_workspace(tmp_path, monkeypatch, module): + monkeypatch.setenv("CASE_STUDY_ROOT", str(tmp_path / "root")) + monkeypatch.setenv("CASE_STUDY_DATA", str(tmp_path / "large")) + root, data = module.folders() + assert root == tmp_path / "root" and data == tmp_path / "large" + assert data.is_dir() and (root / "results").is_dir() diff --git a/unit_tests/test_event_contracts.py b/unit_tests/test_event_contracts.py new file mode 100644 index 0000000..2532252 --- /dev/null +++ b/unit_tests/test_event_contracts.py @@ -0,0 +1,260 @@ +"""Coordinate boundaries and the distinct published event-selection rules.""" + +import gzip +from types import SimpleNamespace + +import numpy as np +import pandas as pd +import pytest + +from conftest import workflow_utils, cs3_utils, prepare_events + + +def as_fields(row): + return [str(v) for v in row.values()], {k: i for i, k in enumerate(row)} + + +@pytest.mark.parametrize("module", [workflow_utils, cs3_utils]) +def test_gene_ids_remove_quotes_and_version(module): + assert module.gene_ids( + pd.DataFrame({"GeneID": ['"ENSG123.4"', "ENSG456"]}) + ).tolist() == ["ENSG123", "ENSG456"] + + +@pytest.mark.parametrize("module", [workflow_utils, cs3_utils]) +def test_one_event_per_exon_merges_touching_spans_and_keeps_stable_ties(module, events): + rows = [ + events(ID="first"), + events(ID="tie", strand="-"), + events(ID="best", upstreamEE=200, downstreamES=400, IJC_SAMPLE_1="40,40"), + events(ID="separate", upstreamEE=401, downstreamES=500), + events(ID="chr2", chr="chr2"), + ] + fields, col = as_fields(rows[0]) + assert module.one_event_per_exon([as_fields(r)[0] for r in rows], col) == [ + "best", + "separate", + "chr2", + ] + assert module.one_event_per_exon([fields, as_fields(rows[1])[0]], col) == ["first"] + assert module.one_event_per_exon([], col) == [] + + +@pytest.mark.parametrize( + "changes,expected", + [ + ({}, 0), + ({"IncLevelDifference": -0.2}, 1), + ({"FDR": 0.8, "PValue": 0.8}, 2), + ({"FDR": 0.1}, None), + ({"IncLevelDifference": 0.05}, None), + ({"PValue": 0.05}, None), + ({"FDR": 0.8, "PValue": 0.5}, None), + ({"FDR": 0.8, "PValue": 0.8, "IncLevel1": "0.05,0.05"}, None), + ({"FDR": 0.8, "PValue": 0.8, "IncLevel1": "NA,"}, None), + ({"GeneID": '"ENSG00000151923.9"'}, None), + ], +) +def test_workflow_classification_strict_thresholds(events, changes, expected): + masks = workflow_utils.classify(pd.DataFrame([events(**changes)])) + assert [s for s in workflow_utils.SETS if masks[s].iloc[0]] == ( + [] if expected is None else [workflow_utils.SETS[expected]] + ) + + +@pytest.mark.parametrize( + "changes,expected", + [ + ({}, 0), + ({"IncLevelDifference": -0.05}, 1), + ({"FDR": 0.5}, 2), + ( + { + "IJC_SAMPLE_1": "0", + "IJC_SAMPLE_2": "0", + "SJC_SAMPLE_1": "0", + "SJC_SAMPLE_2": "0", + }, + None, + ), + ({"GeneID": '"ENSG00000151923.9"'}, None), + ({"FDR": 0.05}, None), + ({"FDR": 0.8, "IncLevel2": "NA,"}, None), + ], +) +def test_download_event_classification_read_floor(events, changes, expected): + f, col = as_fields(events(**changes)) + assert prepare_events.classify(f, col) == ( + None if expected is None else prepare_events.SETS[expected] + ) + + +def test_legacy_event_sets_include_boundary_and_do_not_mutate(events): + table = pd.DataFrame( + [ + events(ID="inc", IncLevelDifference=0.05), + events(ID="exc", IncLevelDifference=-0.05), + events(ID="bg", FDR=0.5), + events(ID="missing", IncLevel1="NA,", IncLevel2="NA,"), + ] + ) + result = cs3_utils.event_sets(table) + assert result["inc"].ID.tolist() == ["inc", "missing"] + assert result["exc"].ID.tolist() == ["exc"] + assert result["bg"].ID.tolist() == ["bg"] + stats = cs3_utils.add_se_stats(table) + assert stats.junction_reads.iloc[0] == 65 + assert stats.psi_kd.iloc[0] == pytest.approx(0.8) + assert np.isnan(stats.psi_ctl.iloc[-1]) + assert "junction_reads" not in table + + +@pytest.mark.parametrize( + "text,expected", [("1,NA,2,", [1, 2]), ("NA,", []), ("2.5", [2.5])] +) +def test_download_numeric_fields(text, expected): + assert prepare_events.nums(text) == expected + + +@pytest.mark.parametrize( + "mode", + ["valid", "missing", "ambiguous", "reverse", "length", "chromosome", "order"], +) +def test_workflow_liftover_requires_whole_ordered_unique_exons(events, mode): + f, col = as_fields(events()) + + def convert(chrom, pos): + if mode == "missing" and pos == 100: + return [] + if mode == "ambiguous" and pos == 100: + return [(chrom, 1100, "+"), (chrom, 2100, "+")] + target = "chr2" if mode == "chromosome" and pos < 50 else chrom + p = pos + (0 if mode == "order" and pos >= 200 else 1000) + if mode == "length" and pos == 119: + p += 1 + return [(target, p, "-" if mode == "reverse" else "+")] + + out = workflow_utils.lift_event(f, col, SimpleNamespace(convert_coordinate=convert)) + if mode == "valid": + assert out[col["exonStart_0base"]] == "1100" + assert out[col["exonEnd"]] == "1120" + assert f[col["exonStart_0base"]] == "100" + else: + assert out is None + + +def test_chain_coordinates_gaps_reverse_and_unknown(tmp_path, events): + path = tmp_path / "chain.gz" + with gzip.open(path, "wt") as stream: + stream.write( + "chain 100 chr1 1000 + 0 50 chr1 2000 + 100 150 1\n20 10 5\n20\n\n" + "chain 50 chr2 1000 + 0 10 chr3 2000 - 100 110 2\n10\n" + ) + chain = prepare_events.Chain(path) + assert chain.convert("chr1", 0) == [("chr1", 100, "+", 100)] + assert chain.convert("chr1", 19)[0][1] == 119 + assert chain.convert("chr1", 20) == [] + assert chain.convert("chr1", 30)[0][1] == 125 + assert chain.convert("chr2", 0) == [("chr3", 1899, "-", 50)] + assert chain.convert("missing", 0) == [] + assert prepare_events.lift_point(chain, "chr1", "0") == ("chr1", 100, "+") + assert prepare_events.lift_point(chain, "chr1", 20) is None + + +@pytest.mark.parametrize("mode", ["valid", "missing", "length", "reverse", "other"]) +def test_download_lift_fields_preserves_length(events, mode): + f, col = as_fields(events()) + + def convert(chrom, pos): + if mode == "missing": + return [] + return [ + ( + "chr2" if mode == "other" else chrom, + pos + 1000 + (1 if mode == "length" and pos == 119 else 0), + "-" if mode == "reverse" else "+", + 100, + ) + ] + + out = prepare_events.lift_fields(SimpleNamespace(convert=convert), f, col) + if mode == "valid": + assert out[col["exonEnd"]] == "1120" + else: + assert out is None + + +def test_legacy_lift_events_rejects_missing_wrong_chromosome_and_resizing( + monkeypatch, events, lift +): + monkeypatch.setattr(cs3_utils, "lifter", lambda cache: lift) + table = pd.DataFrame([events()]) + assert cs3_utils.lift_events(table, "cache").exonEnd_hg38.tolist() == [1120] + for hits in [[], [("other", 100, "+", 1)], [("chr1", 100, "+", 1)]]: + monkeypatch.setattr(lift, "convert_coordinate", lambda *args: hits) + assert cs3_utils.lift_events(table, "cache").empty + + +def test_window_index_is_half_open_strand_specific_and_filters_quality(): + windows = pd.DataFrame( + [ + dict( + subject="QKI", + chr="chr1", + strand="+", + start=100, + end=120, + q_value=0.05, + log2_enrichment=2, + ), + dict( + subject="QKI", + chr="chr1", + strand="+", + start=200, + end=220, + q_value=0.1, + log2_enrichment=4, + ), + dict( + subject="QKI", + chr="chr1", + strand="+", + start=300, + end=320, + q_value=0.01, + log2_enrichment=1, + ), + ] + ) + idx = cs3_utils.WindowIndex(windows) + assert idx.any("QKI", "chr1", "+", 119, 121) + for a, b in [(80, 100), (120, 140), (200, 220), (300, 320)]: + assert not idx.any("QKI", "chr1", "+", a, b) + assert not idx.any("QKI", "chr1", "-", 100, 120) + events = pd.DataFrame( + [dict(chr="chr1", strand="+", exonStart_0base_hg38=100, exonEnd_hg38=120)] + ) + assert cs3_utils.window_map(events, idx, "QKI").tolist() == [0] * 6 + [1] + [0] * 6 + + +@pytest.mark.parametrize( + "strand,first,last", + [("+", (700, 750), (1270, 1320)), ("-", (1270, 1320), (700, 750))], +) +def test_exon_bins_follow_transcription(strand, first, last): + bins = cs3_utils.exon_bins(strand, 1000, 1020) + assert len(bins) == 13 and bins[6] == (1000, 1020) + assert bins[0] == first and bins[-1] == last + + +@pytest.mark.parametrize("strand,left,right", [("+", -1, 1), ("-", 1, -1)]) +def test_relative_position_counts_intronic_nucleotides( + monkeypatch, strand, left, right +): + monkeypatch.setattr( + workflow_utils, "EXONS", {"test": {"grch38": ("chr1", strand, 100, 120)}} + ) + assert workflow_utils.relative_position(90, 100, "test") == left + assert workflow_utils.relative_position(120, 130, "test") == right + assert workflow_utils.relative_position(99, 101, "test") == 0 diff --git a/unit_tests/test_pipeline_contracts.py b/unit_tests/test_pipeline_contracts.py new file mode 100644 index 0000000..07e6180 --- /dev/null +++ b/unit_tests/test_pipeline_contracts.py @@ -0,0 +1,313 @@ +"""Offline orchestration, deterministic event lists, and BAM command contracts.""" + +import gzip +from pathlib import Path +from unittest.mock import Mock + +import pandas as pd +import pysam +import pytest + +from conftest import workflow_utils, prepare_events +from test_download_contracts import archive +import make_lite_data + + +def test_event_lists_deduplicate_lift_and_preserve_unchanged_mtime( + tmp_path, monkeypatch, events, lift +): + table = pd.DataFrame( + [ + events(ID="a"), + events(ID="b", IJC_SAMPLE_1="50,50"), + events(ID="c", chr="chr2", IncLevelDifference=-0.2), + events(ID="bg", chr="chr3", FDR=0.8, PValue=0.8), + ] + ) + src = tmp_path / "se.tsv" + table.to_csv(src, sep="\t", index=False) + monkeypatch.setattr(workflow_utils, "lifter", lambda cache: lift) + counts, paths = workflow_utils.build_event_lists( + "KD", src, tmp_path / "lists", tmp_path + ) + assert counts[workflow_utils.SETS[0]] == { + "rMATS events": 2, + "one per exon": 1, + "lifted to GRCh38": 1, + } + result = pd.read_csv(paths[workflow_utils.SETS[0]], sep="\t") + assert result.ID.tolist() == ["b"] and result.exonStart_0base.tolist() == [1100] + times = {s: p.stat().st_mtime_ns for s, p in paths.items()} + assert ( + workflow_utils.build_event_lists("KD", src, tmp_path / "lists", tmp_path)[1] + == paths + ) + assert {s: p.stat().st_mtime_ns for s, p in paths.items()} == times + assert workflow_utils.event_lists("KD", tmp_path / "lists") == paths + assert workflow_utils.which_set(paths, "exon13") is None + row = events(**dict(workflow_utils.EXONS["exon13"]["hg19"])) + c, strand, a, b = workflow_utils.EXONS["exon13"]["grch38"] + row.update(chr=c, strand=strand, exonStart_0base=a, exonEnd=b) + pd.DataFrame([row]).to_csv(paths[workflow_utils.SETS[1]], sep="\t", index=False) + assert workflow_utils.which_set(paths, "exon13") == workflow_utils.SETS[1] + + +def test_exon_row_exact_and_highest_read_fallback(tmp_path, events): + info = workflow_utils.EXONS["exon13"]["hg19"] + exact = events(**info) + p = tmp_path / "se.tsv" + pd.DataFrame([exact]).to_csv(p, sep="\t", index=False) + row = workflow_utils.exon_row(p, "exon13") + assert row["note"] == "" and workflow_utils.exon_stats(row)["IJC_kd"] == [10, 20] + assert workflow_utils.exon_stats(row)["psi_kd"] == [0.7, 0.9] + high = dict(exact, ID="best", upstreamEE=10, IJC_SAMPLE_1="100,200") + pd.DataFrame([dict(exact, upstreamEE=9), high]).to_csv(p, sep="\t", index=False) + row = workflow_utils.exon_row(p, "exon13") + assert row["ID"] == "best" and "flanking exons differ" in row["note"] + with pytest.raises(AssertionError, match="not in"): + workflow_utils.exon_row(p, "exon24nt") + + +def test_isoform_inclusion_and_skipping(locus): + table = workflow_utils.isoform_table(locus["transcripts"]).set_index("name") + assert table.loc["PICALM-1", "150-nt exon included"] + assert table.loc["PICALM-1", "MANE"] + assert table.loc["PICALM-2", "150-nt exon skipped"] + assert not table.loc["PICALM-3", "150-nt exon skipped"] + + +def manifest(): + return pd.DataFrame( + [ + dict( + knockdown="KD", + rep=rep, + condition=condition, + bam=f"{condition}{rep}.bam", + ) + for rep in [2, 1] + for condition in ["control", "knockdown"] + ] + ) + + +def test_rmats_command_and_reuse_invalidates_changed_settings(tmp_path, monkeypatch): + calls = [] + + def run(cmd, **kwargs): + calls.append(cmd) + od = Path(cmd[cmd.index("--od") + 1]) + (od / "SE.MATS.JC.txt").write_text("result") + return 0 + + monkeypatch.setattr(workflow_utils.subprocess, "call", run) + out = tmp_path / "runs" + result, status = workflow_utils.run_rmats( + "KD", manifest(), "genes.gtf", out, 3, ["python", "rmats.py"], "settings" + ) + assert status == "ran" and result.read_text() == "result" + assert (out / "KD" / "b1.txt").read_text() == "knockdown1.bam,knockdown2.bam\n" + assert (out / "KD" / "b2.txt").read_text() == "control1.bam,control2.bam\n" + cmd = calls[0] + assert ( + cmd[:2] == ["python", "rmats.py"] + and cmd[cmd.index("--libType") + 1] == "fr-firststrand" + ) + assert cmd[cmd.index("--nthread") + 1] == "3" + assert ( + workflow_utils.run_rmats( + "KD", manifest(), "genes.gtf", out, 3, ["rmats.py"], "settings" + )[1] + == "reused" + ) + stale = out / "KD" / "old" + stale.mkdir() + (stale / "file").write_text("old") + assert ( + workflow_utils.run_rmats( + "KD", manifest(), "genes.gtf", out, 3, ["rmats.py"], "changed" + )[1] + == "ran" + ) + assert not stale.exists() and len(calls) == 2 + monkeypatch.setattr(workflow_utils.subprocess, "call", lambda *a, **k: 1) + with pytest.raises(AssertionError, match="rMATS failed"): + workflow_utils.run_rmats( + "KD", manifest(), "genes.gtf", out, 3, ["rmats.py"], "failure" + ) + with pytest.raises(AssertionError): + workflow_utils.run_rmats("KD", manifest().iloc[:2], "g", out, 1, ["r"], "bad") + + +def write_bam(path, flags): + with pysam.AlignmentFile( + path, + "wb", + header={ + "HD": {"VN": "1.6", "SO": "coordinate"}, + "SQ": [{"SN": "chr1", "LN": 10000}], + }, + ) as bam: + for i, flag in enumerate(flags): + read = pysam.AlignedSegment() + read.query_name = f"r{i}" + read.query_sequence = "A" * 20 + read.flag = flag + read.reference_id = 0 + read.reference_start = 105 + i + read.mapping_quality = 60 + read.cigar = [(0, 20)] + read.query_qualities = pysam.qualitystring_to_array("I" * 20) + bam.write(read) + pysam.index(str(path)) + return path + + +def test_bam_read_detection_and_sense_fraction(tmp_path, events): + bam = write_bam(tmp_path / "paired.bam", [65, 129, 145, 256, 2048]) + assert workflow_utils.has_read1(bam) + r2 = write_bam(tmp_path / "r2.bam", [129, 145]) + assert not workflow_utils.has_read1(r2, n=0) and not workflow_utils.has_read1(r2) + p = tmp_path / "events.tsv" + pd.DataFrame([events(), events(chr="missing")]).to_csv(p, sep="\t", index=False) + fraction, total = workflow_utils.sense_fraction(bam, p) + assert total == 3 and fraction == pytest.approx(2 / 3) + empty = write_bam(tmp_path / "empty.bam", []) + assert workflow_utils.sense_fraction(empty, p) == (0, 0) + + +def test_read2_copy_is_created_once(tmp_path, monkeypatch): + original = tmp_path / "a.bam" + monkeypatch.setattr(workflow_utils, "has_read1", lambda p: False) + assert workflow_utils.read2_only(original) == str(original) + monkeypatch.setattr(workflow_utils, "has_read1", lambda p: True) + + def run(args): + if args[1] == "view": + Path(args[args.index("-o") + 1]).touch() + + call = Mock(side_effect=run) + monkeypatch.setattr(workflow_utils.subprocess, "check_call", call) + out = workflow_utils.read2_only(original, threads=2) + assert out.endswith(".R2only.bam") and call.call_count == 2 + assert workflow_utils.read2_only(original) == out and call.call_count == 2 + assert call.call_args_list[0].args[0][2:7] == ["-@", "2", "-f", "128", "-b"] + + +def test_small_bam_helpers_keep_read2_and_report_full_counts(tmp_path, monkeypatch): + monkeypatch.setattr(make_lite_data.shutil, "which", lambda cmd: None) + source = write_bam(tmp_path / "full.bam", [65, 129, 145]) + bed = tmp_path / "regions.bed" + bed.write_text("chr1\t0\t500\n") + out = tmp_path / "lite.bam" + counts = make_lite_data.lite_eclip(source, bed, out) + assert counts == dict( + paired_input=True, mapped_F260=2, mapped_records=2, kept_reads=2 + ) + assert not workflow_utils.has_read1(out) + assert len(make_lite_data.sh("samtools", "view", "-c", out, capture=True)) > 0 + assert make_lite_data.md5(out) == workflow_utils.checksum(out, "md5") + normal = Mock(return_value=" output \n") + monkeypatch.setattr(make_lite_data.subprocess, "check_output", normal) + assert make_lite_data.sh("echo", "x", capture=True) == "output" + call = Mock() + monkeypatch.setattr(make_lite_data.subprocess, "check_call", call) + make_lite_data.sh("echo", 1) + call.assert_called_once_with(["echo", "1"]) + + +def test_full_bam_and_bed_helpers(tmp_path): + (tmp_path / "S_IP_1.genome.Aligned.sort.dedup.R2only.bam").touch() + full = tmp_path / "S_IP_1.genome.Aligned.sort.dedup.bam" + full.touch() + assert make_lite_data.full_eclip_bam(tmp_path, "S", "IP_1") == full + with pytest.raises(AssertionError, match="no full BAM"): + make_lite_data.full_eclip_bam(tmp_path, "missing", "IP_1") + bed = tmp_path / "out.bed" + assert ( + make_lite_data.write_bed( + [("chr2", 0, 5), ("chr1", 20, 30), ("chr1", 0, 20)], bed + ) + == 2 + ) + assert bed.read_text() == "chr1\t0\t30\nchr2\t0\t5\n" + assert make_lite_data.write_bed([], bed) == 0 and bed.read_text() == "" + + +def test_splice_regions_use_every_selected_event_and_single_point_lifts( + tmp_path, monkeypatch, events, lift +): + p = tmp_path / "se.tsv" + pd.DataFrame([events()]).to_csv(p, sep="\t", index=False) + monkeypatch.setattr(workflow_utils, "lifter", lambda cache: lift) + rows = make_lite_data.splice_site_regions(p, tmp_path) + assert ("chr1", 700, 1500) in rows and make_lite_data.PICALM_GRCH38 in rows + monkeypatch.setattr(lift, "convert_coordinate", lambda *a: []) + assert make_lite_data.splice_site_regions(p, tmp_path) == [ + make_lite_data.PICALM_GRCH38 + ] + + +def test_picalm_gtf_and_excluded_gene_copy(tmp_path, events): + gtf = tmp_path / "genes.gtf" + text = 'chr1\tG\tgene\t5000\t6000\t.\t-\t.\tgene_name "PICALM";\n' + gtf.write_text(text + text.replace("PICALM", "OTHER")) + assert make_lite_data.picalm_hg19(gtf) == (("chr1", 3000, 8000), [text]) + src = tmp_path / "all.tsv" + dst = tmp_path / "kept.tsv" + pd.DataFrame([events(), events(GeneID="ENSG00000151923.3")]).to_csv( + src, sep="\t", index=False + ) + make_lite_data.copy_without_excluded(src, dst) + assert len(pd.read_csv(dst, sep="\t")) == 1 + + +def test_download_event_main_writes_merged_regions_and_counts( + tmp_path, monkeypatch, events +): + chain = tmp_path / "chain.gz" + with gzip.open(chain, "wt") as f: + f.write("chain 1 chr1 10000 + 0 10000 chr1 10000 + 0 10000 1\n10000\n") + table = pd.DataFrame( + [ + events(), + events(ID="bg", FDR=0.8), + events(ID="skip", GeneID="ENSG00000151923"), + ] + ) + archive( + tmp_path / "A.tar.gz", + { + "x/MATS_output/SE.MATS.JunctionCountOnly.txt": table.to_csv( + sep="\t", index=False + ) + }, + ) + monkeypatch.setattr(prepare_events, "KNOCKDOWNS", {"KD": "A"}) + monkeypatch.setenv("CS3_SKIP_SELFTEST", "1") + out = tmp_path / "out" + prepare_events.main(tmp_path, chain, out) + assert pd.read_csv( + out / "KD.GRCh38.included-upon-knockdown.txt", sep="\t" + ).ID.tolist() == ["first"] + assert (out / "regions.bed").read_text().startswith("chr1\t0\t600\n") + assert "KD\tincluded-upon-knockdown\t1\t1" in (out / "event_counts.tsv").read_text() + monkeypatch.delenv("CS3_SKIP_SELFTEST") + with pytest.raises(SystemExit, match="liftover self-test failed"): + prepare_events.main(tmp_path, chain, out) + + +def test_lite_paths_and_library_sizes(tmp_path, monkeypatch): + monkeypatch.setattr(workflow_utils, "LITE", False) + assert workflow_utils.data_dirs(tmp_path)["rmats"] == tmp_path / "rmats" / "rerun" + monkeypatch.setattr(workflow_utils, "LITE", True) + monkeypatch.setattr(workflow_utils, "LITE_DIR", tmp_path) + assert workflow_utils.data_dirs(tmp_path)["rmats"] == tmp_path / "rmats_full" + (tmp_path / "eclip").mkdir() + (tmp_path / "eclip" / "library_sizes.tsv").write_text( + "file\tmapped_F260\na.bam\t10\n" + ) + assert workflow_utils.lite_eclip_bams().bam.tolist() == [ + str(tmp_path / "eclip" / "a.bam") + ] + assert workflow_utils.lite_shrna_bam("A") == tmp_path / "shrna" / "A.PICALM.bam" diff --git a/unit_tests/test_render_and_setup.py b/unit_tests/test_render_and_setup.py new file mode 100644 index 0000000..44f409a --- /dev/null +++ b/unit_tests/test_render_and_setup.py @@ -0,0 +1,250 @@ +"""Notebook creation, synthetic figure rendering, and installer isolation.""" + +import runpy +import shutil +import sys +from pathlib import Path +from unittest.mock import Mock + +import matplotlib as mpl +import matplotlib.pyplot as plt +import nbformat +import numpy as np +import pytest + +from conftest import ROOT, workflow_utils +import figure +import build_notebooks +import colab_setup + + +def test_workflow_figure_writes_formats_and_restores_style( + tmp_path, locus, knockdown, monkeypatch +): + line = np.linspace(0.0001, 0.0002, 1400) + maps = [ + dict( + label=label, + means={ + s: line * scale for s, scale in zip(workflow_utils.SETS, [1.2, 0.8, 1]) + }, + band=(line * 0.9, line * 1.1), + traces={"exon13": line, "exon24nt": line[::-1]}, + ) + for label in ["K562", "HepG2"] + ] + before = dict(mpl.rcParams) + saved = [] + close = plt.close + monkeypatch.setattr( + plt, "close", lambda f: saved.append(f) if hasattr(f, "axes") else close(f) + ) + out = tmp_path / "workflow" + try: + assert ( + figure.draw_figure(locus, knockdown, maps, out, "synthetic fixtures") + == str(out) + ".png" + ) + assert dict(mpl.rcParams) == before + assert out.with_suffix(".png").read_bytes().startswith(b"\x89PNG\r\n\x1a\n") + assert out.with_suffix(".pdf").read_bytes().startswith(b"%PDF") + texts = [text.get_text() for ax in saved[0].axes for text in ax.texts] + [ + text.get_text() for text in saved[0].texts + ] + assert set("abcde").issubset(texts) + assert any("synthetic fixtures" in text for text in texts) + assert any("PICALM-1" in text for text in texts) + assert {str(number) for number in range(1, 8)}.issubset(texts) + finally: + for f in saved: + close(f) + + +def assert_clipping_warning(warn, capsys): + fig = plt.figure(figsize=(3, 3)) + try: + fig.add_axes([0.15, 0.2, 0.5, 0.5], title="inside axes") + fig.text(0.5, 0.9, "inside label", ha="center") + warn(fig) + assert capsys.readouterr().out == "" + + fig.add_axes([0.8, 0.2, 0.4, 0.5], title="clipped axes") + fig.text(-0.25, 0.95, "clipped label") + warn(fig) + assert capsys.readouterr().out.splitlines() == [ + "warning: cut off at the figure edge: 'clipped axes'", + "warning: cut off at the figure edge: 'clipped label'", + ] + finally: + plt.close(fig) + + +def test_workflow_figure_warns_when_artists_extend_beyond_canvas(capsys): + assert_clipping_warning(figure.warn_if_cut_off, capsys) + + +def test_standalone_redraw_uses_bundled_small_inputs(tmp_path, monkeypatch, capsys): + src = ROOT / "data_and_code" / "picalm" + for name in ["picalm.json", "picalm_locus.json"]: + shutil.copyfile(src / name, tmp_path / name) + monkeypatch.chdir(tmp_path) + monkeypatch.setenv("MAP_DIR", str(src / "maps_stranded")) + monkeypatch.setenv("MAP_NOTE", "offline redraw") + with plt.rc_context(): + try: + ns = runpy.run_path(str(src / "make_fig.py")) + assert ( + (tmp_path / "CS3_PICALM_QKI_draft.pdf").read_bytes().startswith(b"%PDF") + ) + assert (tmp_path / "CS3_PICALM_QKI_draft.png").stat().st_size > 10000 + assert ns["picalm_trace"]( + "QKI_K562_5007", "QKI_HepG2", "rep1", "exon13" + ).shape == (1400,) + assert any("offline redraw" in text.get_text() for text in ns["fig"].texts) + capsys.readouterr() + assert_clipping_warning(ns["warn_if_cut_off"], capsys) + finally: + plt.close("all") + + +def test_psi_chart_groups_conditions(knockdown): + fig = workflow_utils.plot_psi(knockdown) + try: + assert len(fig.axes) == 2 + assert all(len(ax.patches) == 4 for ax in fig.axes) + assert [p.get_height() for p in fig.axes[0].patches] == pytest.approx( + [0.35, 0.65, 0.35, 0.65] + ) + finally: + plt.close(fig) + + +def test_notebook_builder_embeds_screenshots_and_compiles_cells(tmp_path): + build_notebooks.build(tmp_path) + assert {p.name for p in tmp_path.glob("*.ipynb")} == set( + build_notebooks.TITLES.values() + ) + attachments = 0 + for key, name in build_notebooks.TITLES.items(): + nb = nbformat.read(tmp_path / name, as_version=4) + nbformat.validate(nb) + assert nb.metadata.kernelspec.name == "python3" + setup = nb.cells[2].source + assert "colab_setup.setup" in setup + assert (", rmats=True" in setup) == ( + key in ["05_differential_splicing", "00_colab_run_all"] + ) + for i, cell in enumerate(nb.cells): + if cell.cell_type == "code": + compile(cell.source, f"{key}:{i}", "exec") + for attachment in cell.get("attachments", {}).values(): + attachments += 1 + assert attachment.get("image/jpeg") or attachment.get("image/png") + assert attachments >= 8 + assert build_notebooks.shot("s.png", "description") == ( + "shot", + "s.png", + "description", + ) + + +def test_colab_installers_pin_core_and_reuse_success_marker(tmp_path, monkeypatch): + monkeypatch.setattr(colab_setup, "TOOLS", tmp_path / "tools") + shell = Mock() + monkeypatch.setattr(colab_setup, "_sh", shell) + versions = colab_setup._core_versions() + assert set(versions) == set(colab_setup.CORE) and all(versions.values()) + colab_setup.install_tools(dry_run=True) + assert not (colab_setup.TOOLS / ".done").exists() + assert (colab_setup.TOOLS / "constraints.txt").read_text() == "".join( + f"{p}=={v}\n" for p, v in versions.items() + ) + assert shell.call_count == 4 and all( + call.args[1] is True for call in shell.call_args_list + ) + colab_setup.install_tools() + assert (colab_setup.TOOLS / ".done").read_text() == "ok\n" + before = shell.call_count + colab_setup.install_tools() + assert shell.call_count == before + + +def test_shell_helper_dry_run_never_executes(monkeypatch): + command = Mock() + monkeypatch.setattr(colab_setup.subprocess, "run", command) + colab_setup._sh("echo offline", dry_run=True) + command.assert_not_called() + colab_setup._sh("echo offline") + command.assert_called_once_with("echo offline", shell=True, check=True) + + +def test_rmats_installer_sets_command_and_reuses_environment(tmp_path, monkeypatch): + monkeypatch.setattr(colab_setup, "TOOLS", tmp_path / "tools") + monkeypatch.setattr(colab_setup, "RMATS_ENV", tmp_path / "rmats") + shell = Mock() + monkeypatch.setattr(colab_setup, "_sh", shell) + monkeypatch.setenv("RMATS_CMD", "old") + colab_setup.install_rmats(dry_run=True) + assert shell.call_count == 2 and "rmats=4.3.0" in shell.call_args.args[0] + assert ( + str(colab_setup.RMATS_ENV / "bin" / "python") + in colab_setup.os.environ["RMATS_CMD"] + ) + script = colab_setup.RMATS_ENV / "bin" / "rmats.py" + script.parent.mkdir(parents=True) + script.touch() + colab_setup.install_rmats() + assert shell.call_count == 2 + + +@pytest.mark.parametrize("lite", [True, False]) +def test_colab_setup_redirects_outputs_and_preserves_workflow( + tmp_path, monkeypatch, lite +): + workflow = tmp_path / "workflow" + workflow.mkdir() + (workflow / "workflow_utils.py").touch() + (workflow / "lite_data").mkdir() + (workflow / "lite_data" / "MANIFEST.tsv").touch() + monkeypatch.chdir(tmp_path) + monkeypatch.setattr(sys, "path", list(sys.path)) + for key in [ + "CS3_LITE", + "CS3_LITE_DIR", + "CASE_STUDY_ROOT", + "CASE_STUDY_DATA", + "PATH", + ]: + monkeypatch.setenv(key, colab_setup.os.environ.get(key, "")) + tools = Mock() + rmats = Mock() + limit = Mock() + monkeypatch.setattr(colab_setup, "install_tools", tools) + monkeypatch.setattr(colab_setup, "install_rmats", rmats) + monkeypatch.setattr(colab_setup.resource, "getrlimit", lambda *a: (256, 20000)) + monkeypatch.setattr(colab_setup.resource, "setrlimit", limit) + output = tmp_path / "output" + colab_setup.setup( + workflow, + lite=lite, + data_on_drive=True, + rmats=True, + output_folder=output, + dry_run=True, + ) + suffix = "_lite" if lite else "" + assert Path(colab_setup.os.environ["CASE_STUDY_ROOT"]) == output / ( + "colab_output" + suffix + ) + assert Path(colab_setup.os.environ["CASE_STUDY_DATA"]) == output / ( + "colab_data" + suffix + ) + assert (colab_setup.os.environ.get("CS3_LITE") == "1") is lite + limit.assert_called_once_with(colab_setup.resource.RLIMIT_NOFILE, (10240, 20000)) + tools.assert_called_once_with(True) + rmats.assert_called_once_with(dry_run=True) + + +def test_colab_setup_rejects_missing_workflow(tmp_path): + with pytest.raises(AssertionError, match="workflow_utils.py not found"): + colab_setup.setup(tmp_path, dry_run=True) diff --git a/unit_tests/test_splice_map_contracts.py b/unit_tests/test_splice_map_contracts.py new file mode 100644 index 0000000..60d9cb0 --- /dev/null +++ b/unit_tests/test_splice_map_contracts.py @@ -0,0 +1,206 @@ +"""Splice-map file contracts with rbp-maps isolated behind its public interfaces.""" + +import os +import sys +from pathlib import Path +from types import ModuleType, SimpleNamespace +from unittest.mock import Mock + +import numpy as np +import pandas as pd +import pytest + +from conftest import workflow_utils +from test_pipeline_contracts import write_bam + + +@pytest.fixture +def density_api(monkeypatch): + density = ModuleType("density") + density.__path__ = [] + read = ModuleType("density.ReadDensity") + norm = ModuleType("density.normalization_functions") + maps = ModuleType("density.Map") + handles = [] + + def rd(**kwargs): + handle = SimpleNamespace(pos=Mock(), neg=Mock(), bam=Mock()) + handles.append(handle) + return handle + + read.ReadDensity = Mock(side_effect=rd) + norm.per_region_subtract_and_normalize = Mock() + norm.bottom_top_values_from_dataframe = Mock( + side_effect=lambda frame, *args: (frame.min().tolist(), frame.max().tolist()) + ) + maps.SkippedExon = Mock(return_value=Mock()) + density.ReadDensity = read + density.normalization_functions = norm + density.Map = maps + for name, module in [ + ("density", density), + ("density.ReadDensity", read), + ("density.normalization_functions", norm), + ("density.Map", maps), + ]: + monkeypatch.setitem(sys.modules, name, module) + return SimpleNamespace(read=read, norm=norm, maps=maps, handles=handles) + + +def test_map_output_names_and_freshness(tmp_path): + lists = workflow_utils.event_lists("KD", tmp_path / "events") + for p in lists.values(): + p.parent.mkdir(exist_ok=True) + p.write_text("events") + os.utime(p, (100, 100)) + outputs = workflow_utils.map_outputs(tmp_path, "S", "KD", "rep1", lists) + assert len(outputs) == 9 and len(set(outputs)) == 9 + assert outputs[0] == tmp_path / "KD_KD" / "S_rep1.se.png" + assert not workflow_utils.map_is_finished(tmp_path, "S", "KD", "rep1", lists) + for p in outputs: + p.parent.mkdir(exist_ok=True) + p.touch() + os.utime(p, (200, 200)) + assert workflow_utils.map_is_finished(tmp_path, "S", "KD", "rep1", lists) + os.utime(next(iter(lists.values())), (200, 200)) + assert not workflow_utils.map_is_finished(tmp_path, "S", "KD", "rep1", lists) + + +@pytest.mark.parametrize("full_counts", [False, True]) +def test_splice_map_passes_normalization_annotations_and_closes_handles( + tmp_path, density_api, full_counts +): + api = density_api + ip = dict(pos="ip.pos", neg="ip.neg", bam="ip.bam") + inp = dict(pos="in.pos", neg="in.neg", bam="in.bam") + if full_counts: + ip["mapped"], inp["mapped"] = 1000, 2000 + lists = workflow_utils.event_lists("KD", tmp_path) + png = tmp_path / "new" / "map.png" + assert workflow_utils.run_splice_map(ip, inp, lists, png) == png + args, kwargs = api.maps.SkippedExon.call_args + assert args[2] == str(png) + assert list(args[4].items()) == [ + (str(lists[s]), "rmats") for s in workflow_utils.SETS + ] + assert kwargs == dict( + exon_offset=50, intron_offset=300, min_density_threshold=0, conf=0.95 + ) + m = api.maps.SkippedExon.return_value + assert [call[0] for call in m.method_calls] == [ + "create_matrices", + "normalize_matrix", + "create_lines", + "set_background_and_calculate_significance", + "write_intermediates_to_csv", + "plot", + ] + m.set_background_and_calculate_significance.assert_called_once_with( + [str(lists[s]) for s in workflow_utils.SETS[:2]], + str(lists[workflow_utils.SETS[2]]), + "permutation", + ) + for handle in api.handles: + handle.pos.close.assert_called_once() + handle.neg.close.assert_called_once() + if full_counts: + assert [h.bam.mapped for h in api.handles] == [1000, 2000] + else: + for handle in api.handles: + handle.bam.close.assert_called_once() + + +def test_load_map_combines_background_extrema_and_finds_exon_trace( + tmp_path, density_api +): + lists = workflow_utils.event_lists("KD", tmp_path) + outputs = workflow_utils.map_outputs(tmp_path, "S", "KD", "rep1", lists) + outputs[0].parent.mkdir() + for i, p in enumerate(outputs[-2:]): + pd.DataFrame([[i - 2, i], [i + 3, i + 5]]).to_csv(p, sep="\t") + c, strand, start, end = workflow_utils.EXONS["exon13"]["grch38"] + key = f"id\tgene\tsymbol\t{c}\t{strand}\t{start}\t{end}\trest" + base = str(outputs[0])[:-4] + for s in workflow_utils.SETS: + np.savetxt(f"{base}.{lists[s].name}.means.txt", [0.1, 0.2]) + pd.DataFrame([[0.2, 0.4]], index=[key]).to_csv( + f"{base}.{lists[s].name}.normed_matrix.txt" + ) + result = workflow_utils.load_splice_map(tmp_path, "S", "KD", "rep1", lists) + np.testing.assert_array_equal(result["band"][0], [-2, 0]) + np.testing.assert_array_equal(result["band"][1], [4, 6]) + np.testing.assert_array_equal(result["means"][workflow_utils.SETS[0]], [0.1, 0.2]) + trace, group = workflow_utils.exon_trace(result["matrices"], "exon13") + np.testing.assert_array_equal(trace, [0.2, 0.4]) + assert group == workflow_utils.SETS[0] + assert workflow_utils.exon_trace(result["matrices"], "exon24nt") == (None, None) + + +def test_peak_and_significant_positions_ignore_other_regions(): + trace = np.zeros(1400) + trace[350 + 267] = 5 + trace[700 + 109] = 7 + assert ( + workflow_utils.peak(trace, "3ss") == -33 + and workflow_utils.peak(trace, "5ss") == 60 + ) + line = np.zeros(1400) + line[550:635] = 1 + line[750:836] = 1 + assert workflow_utils.positions_above_band(line, np.zeros(1400), "3ss") == 85 + assert workflow_utils.positions_above_band(line, np.zeros(1400), "5ss") == 86 + assert workflow_utils.positions_above_band(line, line, "3ss") == 0 + + +@pytest.mark.parametrize("library", [None, {"mapped_F260": 100, "mapped_records": 200}]) +def test_signal_tracks_strand_direction_and_full_library_counts( + tmp_path, monkeypatch, library +): + bam = write_bam(tmp_path / "a.bam", [129]) + maps = ModuleType("maps") + maps.plot_map = SimpleNamespace(ensure_density_bigwigs=Mock()) + monkeypatch.setitem(sys.modules, "maps", maps) + monkeypatch.setattr(workflow_utils, "read2_only", lambda *a: str(bam)) + monkeypatch.setattr( + workflow_utils, + "sense_fraction", + lambda *a: (0.8 if library is None else 0.2, 10), + ) + scaled = Mock() + monkeypatch.setattr(workflow_utils, "_bigwigs_scaled", scaled) + result = workflow_utils.signal_tracks( + bam, "bg", tmp_path / "signal", library=library + ) + assert result["sense_fraction"] == (0.8 if library is None else 0.2) + assert (tmp_path / "signal" / "a.bam.chrom.sizes").read_text() == "chr1\t10000\n" + if library is None: + assert maps.plot_map.ensure_density_bigwigs.call_args.kwargs["direction"] == "f" + scaled.assert_not_called() + else: + assert result["mapped"] == 200 and scaled.call_args.args[-2:] == ("r", 100) + Path(result["pos"]).touch() + Path(result["neg"]).touch() + workflow_utils.signal_tracks(bam, "bg", tmp_path / "signal", library=library) + assert scaled.call_count == 1 + + +@pytest.mark.parametrize("direction,strands", [("f", ["+", "-"]), ("r", ["-", "+"])]) +def test_bigwig_command_scales_rpm_and_sorts_each_strand( + tmp_path, monkeypatch, direction, strands +): + processes = [] + + def popen(args, **kwargs): + processes.append(args) + return SimpleNamespace(stdout=Mock(), wait=lambda: 0) + + monkeypatch.setattr(workflow_utils.subprocess, "Popen", popen) + convert = Mock() + monkeypatch.setattr(workflow_utils.subprocess, "check_call", convert) + workflow_utils._bigwigs_scaled( + "a.bam", tmp_path / "pos.bw", tmp_path / "neg.bw", "sizes", direction, 100 + ) + genomecov = [args for args in processes if args[0] == "bedtools"] + assert [a[a.index("-strand") + 1] for a in genomecov] == strands + assert all(a[-1] == "10000.0" for a in genomecov) + assert convert.call_count == 2 and not list(tmp_path.glob("*.bg"))