diff --git a/.github/workflows/guard.yml b/.github/workflows/guard.yml index f1a4c1821..5b070deba 100644 --- a/.github/workflows/guard.yml +++ b/.github/workflows/guard.yml @@ -371,6 +371,46 @@ jobs: cp -f "$saved" "$src" trap - EXIT + # The PGx tests moved from Python into cargo test: each planted fault must + # fail the moved test that guards that rule (one per new test target). + - name: PGx test controls (a planted fault fails its moved test in each crate) + run: | + files="crates/og-steps/src/pgx_rules.rs crates/og-pgx/src/sections.rs crates/og-filters/src/mito.rs crates/og-filters/src/refblock.rs crates/og-bam/tests/data/parity/want/check-multimapped_with_alt/files/check.tsv" + saved="${RUNNER_TEMP}/pgx-controls" + mkdir -p "$saved" + for f in $files; do cp -f "$f" "$saved/$(basename "$f")"; done + trap 'for f in $files; do cp -f "$saved/$(basename "$f")" "$f"; done' EXIT + # plant FILE EXPR: apply one fault and fail if it changed nothing. + plant() { + cp -f "$1" "${RUNNER_TEMP}/pgx-before" + perl -pi -e "$2" "$1" + cmp -s "$1" "${RUNNER_TEMP}/pgx-before" && { echo "ERROR: no fault was planted in $1: $2"; exit 1; } + return 0 + } + plant crates/og-steps/src/pgx_rules.rs 's/\} else if !same_pair\(&names, calls\) \{/} else if false \&\& !same_pair(\&names, calls) {/' + plant crates/og-steps/src/pgx_rules.rs 's/if !same_diplotype\(&pg, &cy\.0\) \{/if same_diplotype(\&pg, \&cy.0) {/' + plant crates/og-pgx/src/sections.rs 's/cyp2d6_band\(all\[0\] \* factor\)/cyp2d6_band(all[0])/' + plant crates/og-pgx/src/sections.rs 's/STEP36_GENES\.contains\(&c\.gene\.as_str\(\)\) && !confirmed \{/STEP36_GENES.contains(\&c.gene.as_str()) \&\& false \&\& !confirmed {/' + plant crates/og-filters/src/mito.rs 's/^pub const MIN_AF: f64 = 0\.95;/pub const MIN_AF: f64 = 0.94;/' + plant crates/og-filters/src/refblock.rs 's/^pub const MIN_GQ: i128 = 20;/pub const MIN_GQ: i128 = 19;/' + plant crates/og-bam/tests/data/parity/want/check-multimapped_with_alt/files/check.tsv 's/^status\tunreliable$/status\tok/' + log="${RUNNER_TEMP}/pgx-controls.log" + if cargo test --locked --no-fail-fast -p og-steps -p og-pgx -p og-filters -p og-bam \ + --test pgx_hla_second_caller --test port_tests_pgx_cpic --test port_tests_pgx_mito \ + --test port_tests_pgx_refblock --test cyp2d6_depth --test pgx_outside_calls \ + --test port_tests_pgx_outside > "$log" 2>&1; then + cat "$log" + echo "ERROR: the moved PGx tests passed with the planted faults" + exit 1 + fi + cat "$log" + for test in case2_optitype_disagrees_at_hla_b a_moderate_inhibitor_halves_the_activity_score \ + case2_the_af_bound the_gq_threshold each_depth_table_gives_its_verdict_and_the_shares \ + case3_both_agree_and_the_depth_check_passed case10_a_stale_report; do + grep -E "^test ${test} .*FAILED" "$log" || { echo "ERROR: $test did not catch its fault"; exit 1; } + done + echo "Control: each planted PGx fault failed the moved test that guards it." + - name: cargo test run: cargo test --workspace --locked --no-fail-fast diff --git a/CLAUDE.md b/CLAUDE.md index c713c6cfb..ac34134aa 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -155,8 +155,8 @@ Change its line in `versions.env`, plus the coupled data variable its comment na ### PharmCAT 3.x (pinned: 3.4.0) - **Two-step workflow**: Preprocessor (`pharmcat_vcf_preprocessor` with `-refFna`) then main jar (`pharmcat.jar`). NOTE: since 3.0 the preprocessor script lost its `.py` extension and the Python package was renamed `preprocessor` → `pcat`. The old `-refFasta` flag is long gone. - Preprocessor outputs `.preprocessed.vcf.bgz` (NOT `.vcf`). -- **3.x JSON changes vs 2.15.x**: `wildtypeAllele` → `referenceAllele`; the HTML report is **no longer emitted unless `-reporterHtml` is passed explicitly**. The `genes` map may be flat (`{gene -> data}`) or nested (`{source -> {gene -> data}}`). `sourceDiplotypes` (or `recommendationDiplotypes`) carry `allele1`/`allele2` objects with a `.name`. Both CPIC consumers (`scripts/27-cpic-lookup.sh` and `modules/local/cpic_lookup`) run one parser, `og pgx cpic-report` (`rust/crates/og-pgx`): it **auto-detects both shapes**, gives each gene a status (`normal`, `non-normal`, `ambiguous` when the possible diplotypes have different phenotypes, `not called`, `unclassified` when a called gene's phenotype says neither normal nor changed function, listed without drug guidance and not counted) and **fails loud**: a recognized report yielding zero genes is reported as a parse failure, never "all genes were successfully called". Guarded by `tests/test_cpic_parser.py`, the `pgx-parse` parity cases and `cargo test -p og-pgx` on real 3.2.0 and 3.4.0 reports of the HG002 fixture, `tests/fixtures/pharmcat/report-.json`. -- Pipeline pinned to **3.4.0** (3.4.0 still bundles vcf-parser 0.3.1, so the `##` header rewrite in step 7 and the module stays). Before bumping, revalidate steps 7 and 27 end-to-end against a known sample — JSON structure and preprocessor flags change between major versions — and capture the new version's `report.json` as a parser fixture beside the others (a row in the test's `REAL_REPORTS`). +- **3.x JSON changes vs 2.15.x**: `wildtypeAllele` → `referenceAllele`; the HTML report is **no longer emitted unless `-reporterHtml` is passed explicitly**. The `genes` map may be flat (`{gene -> data}`) or nested (`{source -> {gene -> data}}`). `sourceDiplotypes` (or `recommendationDiplotypes`) carry `allele1`/`allele2` objects with a `.name`. Both CPIC consumers (`scripts/27-cpic-lookup.sh` and `modules/local/cpic_lookup`) run one parser, `og pgx cpic-report` (`rust/crates/og-pgx`): it **auto-detects both shapes**, gives each gene a status (`normal`, `non-normal`, `ambiguous` when the possible diplotypes have different phenotypes, `not called`, `unclassified` when a called gene's phenotype says neither normal nor changed function, listed without drug guidance and not counted) and **fails loud**: a recognized report yielding zero genes is reported as a parse failure, never "all genes were successfully called". Guarded by `cargo test -p og-pgx` (`tests/port_tests_pgx_cpic.rs` runs the verb, `tests/fixtures.rs` the library) on real 3.2.0 and 3.4.0 reports of the HG002 fixture, `tests/fixtures/pharmcat/report-.json`, and by the `pgx-parse` parity cases. +- Pipeline pinned to **3.4.0** (3.4.0 still bundles vcf-parser 0.3.1, so the `##` header rewrite in step 7 and the module stays). Before bumping, revalidate steps 7 and 27 end-to-end against a known sample — JSON structure and preprocessor flags change between major versions — and capture the new version's `report.json` as a parser fixture beside the others (a row in `REAL_REPORTS` of `rust/crates/og-pgx/tests/port_tests_pgx_cpic.rs`). ### pgsc_calc (PRS and ancestry, steps 25 and 26) - **pgsc_calc runs as its own Nextflow pipeline** (`PGSC_CALC_VERSION` in versions.env; the `PGSC_*_IMAGE` lines move with it). `setup.sh` unpacks the release into `tools/pgsc_calc-`; with `--pgsc_calc` pointing there the pipeline runs it offline (without it the PRS task fetches the release), and `run-all.sh` passes it when it exists. diff --git a/assets/pgx_contraindications.tsv b/assets/pgx_contraindications.tsv index 0d6165987..4aee4d508 100644 --- a/assets/pgx_contraindications.tsv +++ b/assets/pgx_contraindications.tsv @@ -25,7 +25,7 @@ # biallelic: the disorder is recessive, so the WARNING needs both copies # changed (a homozygous or hemizygous genotype) or two variants in the gene; # one heterozygous variant makes a carrier and gives a NOTE instead. -# tests/test_cpic_parser.py checks the shape of every row. +# cargo test -p og-pgx (tests/port_tests_pgx_cpic.rs) checks the shape of every row. Gene Drug Names CPIC_level Note Pair_source Label_source Report_if POLG valproic acid valproic,valproate,divalproex A/B The valproate label contraindicates it in people with mitochondrial disorders caused by POLG mutations, and in children under two suspected of a POLG-related disorder: valproate can cause acute liver failure in them. https://api.cpicpgx.org/v1/pair_view?genesymbol=eq.POLG https://dailymed.nlm.nih.gov/dailymed/drugInfo.cfm?setid=08a65cf4-7749-4ceb-6895-8f4805e2b01f any ASL valproic acid valproic,valproate,divalproex B The valproate label contraindicates it in people with a urea cycle disorder: valproate can cause hyperammonemic encephalopathy in them. https://api.cpicpgx.org/v1/pair_view?genesymbol=eq.ASL https://dailymed.nlm.nih.gov/dailymed/drugInfo.cfm?setid=08a65cf4-7749-4ceb-6895-8f4805e2b01f biallelic diff --git a/assets/pgx_inhibitors.tsv b/assets/pgx_inhibitors.tsv index 9cec8daa2..beb25fa01 100644 --- a/assets/pgx_inhibitors.tsv +++ b/assets/pgx_inhibitors.tsv @@ -19,7 +19,7 @@ # moderate one. '-' for every other gene: CPIC gives no rule that converts # their phenotype, so an inhibitor of one gets a note and no converted # phenotype. -# tests/test_cpic_parser.py checks the shape of every row. +# cargo test -p og-pgx (tests/port_tests_pgx_cpic.rs) checks the shape of every row. Drug Gene Strength Activity_score_factor Class_source Rule_source bupropion CYP2D6 strong 0 https://www.fda.gov/drugs/drug-interactions-labeling/healthcare-professionals-fdas-examples-drugs-interact-cyp-enzymes-and-transporter-systems https://cpicpgx.org/guidelines/guideline-for-codeine-and-cyp2d6/ fluoxetine CYP2D6 strong 0 https://www.fda.gov/drugs/drug-interactions-labeling/healthcare-professionals-fdas-examples-drugs-interact-cyp-enzymes-and-transporter-systems https://cpicpgx.org/guidelines/guideline-for-codeine-and-cyp2d6/ diff --git a/docs/27-cpic-lookup.md b/docs/27-cpic-lookup.md index c27c4dfb7..bd94b0d9e 100644 --- a/docs/27-cpic-lookup.md +++ b/docs/27-cpic-lookup.md @@ -10,7 +10,7 @@ PharmCAT produces detailed JSON and HTML reports, but finding the actionable par ## Tool -- `og pgx cpic-report`, the one PharmCAT reader of the pipeline, in OwnGenome's Rust binary `og` (`rust/crates/og-pgx`, [Rust code](rust.md)). The Nextflow `CPIC_LOOKUP` module and the unit test `tests/test_cpic_parser.py` run the same command. `scripts/setup.sh --og` builds `og`. +- `og pgx cpic-report`, the one PharmCAT reader of the pipeline, in OwnGenome's Rust binary `og` (`rust/crates/og-pgx`, [Rust code](rust.md)). The Nextflow `CPIC_LOOKUP` module runs the same command, and `cargo test -p og-pgx` checks it beside its code. `scripts/setup.sh --og` builds `og`. ## Docker Image diff --git a/docs/testing.md b/docs/testing.md index 7302f594c..0a06a31a8 100644 --- a/docs/testing.md +++ b/docs/testing.md @@ -257,7 +257,7 @@ The first port is the reference mask of `setup.sh --masked-reference`. On a 254 On the whole GRCh38 analysis set (3.1 GB) `og` writes the file `GRC_MASKED_REF_SHA256` pins in 29 s on the same machine, 1.2 s of it CPU: writing the copy is what takes the time. -The next four ports are the VCF filters of steps 04, 05 and 07: `og sv inv-filter`, `og sv svlen-fix`, `og pgx refblock-fill` and `og pgx mito-merge`. Their cases run each oracle in the image the step used (`inv-filter` on the Manta image's Python 2.7, and on Python 3 too). `refblock-fill` reads a stream on stdin, so its cases stage a file named `stdin`. e2e does not shadow these four: the step scripts call them from the image's own working directory, `refblock-fill` reads stdin, which the shadow cannot replay, and `mito-merge` writes `--out` outside the call's directory. Their unit tests (`tests/test_annotsv_sv_inputs.py`, `tests/test_pgx_refblock_fill.py`, `tests/test_pgx_mito_merge.py`) and the three step 05 tests run `og`. The unit-tests job has no `og`, so there they skip with a note. The Guard job `rust` runs them with `OG_REQUIRED=1` after its musl builds, and there a skip fails. +The next four ports are the VCF filters of steps 04, 05 and 07: `og sv inv-filter`, `og sv svlen-fix`, `og pgx refblock-fill` and `og pgx mito-merge`. Their cases run each oracle in the image the step used (`inv-filter` on the Manta image's Python 2.7, and on Python 3 too). `refblock-fill` reads a stream on stdin, so its cases stage a file named `stdin`. e2e does not shadow these four: the step scripts call them from the image's own working directory, `refblock-fill` reads stdin, which the shadow cannot replay, and `mito-merge` writes `--out` outside the call's directory. `tests/test_annotsv_sv_inputs.py` and the three step 05 tests run `og`. The unit-tests job has no `og`, so there they skip with a note. The Guard job `rust` runs them with `OG_REQUIRED=1` after its musl builds, and there a skip fails. `refblock-fill` and `mito-merge` are checked by `cargo test -p og-filters` (`tests/port_tests_pgx_refblock.rs`, `tests/port_tests_pgx_mito.rs`). These tests call the verbs' library code in a child copy of the test binary, so they need no built `og`, do not read `OG_REQUIRED` and never skip. The checks CI runs on the pipeline itself are ports too: `og dev contract` (the output contract), `og dev schema` (a `summary.json` against `tests/schema/summary.schema.json`) and `og dev report-only` (the report-only error strategy). Their cargo tests in `rust/crates/og-dev/tests/repo.rs` run them on this checkout and on copies with one planted break each, in the Guard job `rust`. `og dev schema` and the run check of `og dev contract` have parity cases; `--static` and `og dev report-only` read a whole checkout, which a case cannot stage, so their parity was shown on the checkout and the planted copies when they were ported. Each fixes a check that passed on what it should refuse: a schema default of another JSON type (`false` for `0`), `additionalProperties: false`, `true` against `enum: [1]`, NaN against `minimum`, a `$ref` to nothing (now exit 2, a broken schema), a `//` inside a string and a nested brace in a strategy closure. `tests/schema/validate.py` stays only for three Python test files that validate the summaries they build in-process, in the unit-tests job, which has no `og`; it follows the same rules. The fake-docker case `reports-stale-step` checks its `summary.json` with `og dev schema` in the job `rust`; the job `fake-docker` has no `og` and says it skipped that check. diff --git a/modules/local/cpic_lookup/main.nf b/modules/local/cpic_lookup/main.nf index 962036931..f0d90a327 100644 --- a/modules/local/cpic_lookup/main.nf +++ b/modules/local/cpic_lookup/main.nf @@ -3,7 +3,7 @@ CPIC Lookup — Drug-gene recommendations from PharmCAT results ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Reads PharmCAT's report.json with og pgx cpic-report (bin/og, on the task - PATH; the same code scripts/27-cpic-lookup.sh and tests/test_cpic_parser.py + PATH; the same code scripts/27-cpic-lookup.sh and cargo test -p og-pgx run) and writes the gene phenotypes and the medications PharmCAT's own report matches to them. With the PYPGX summary it also writes the PharmCAT/pypgx comparison (published to pypgx/, as the bash steps do) and warns, in the diff --git a/rust/crates/og-bam/tests/cyp2d6_depth.rs b/rust/crates/og-bam/tests/cyp2d6_depth.rs new file mode 100644 index 000000000..383ddf4c2 --- /dev/null +++ b/rust/crates/og-bam/tests/cyp2d6_depth.rs @@ -0,0 +1,247 @@ +//! The CYP2D6 depth check (`og pgx cyp2d6-bed`, `-measure`, `-check`), by +//! meaning. +//! +//! `tests/parity.rs` holds og to the files under `tests/data/parity/`, byte +//! for byte: what the Python tool og replaced wrote on each case +//! (`want//`), or what og writes where it fixes that tool on purpose +//! (`og//`). This test says what those files must mean, so a golden +//! rewritten with another verdict fails here even when og agrees with it. +//! +//! Five depth tables (`tests/fixtures/pgx/depth/.q0.bed` and `.q1.bed`, +//! the mosdepth region files of all reads and of MAPQ >= 1): +//! +//! - `clean_no_alt`: the fixture's CYP2D slice mapped to the no-ALT +//! reference: ok; +//! - `multimapped_with_alt`: the same reads mapped to the Broad hg38 FASTA +//! with ALT contigs: unreliable, multi-mapped; +//! - `deleted_both`: CYP2D6 deleted on both copies (*5/*5), almost no reads +//! at the gene, normal flanks: ok, the deletion is real; +//! - `deleted_one`: one copy deleted, half the flank depth: ok; +//! - `no_flank_reads`: no reads at all: unreliable. +//! +//! Also: the check file has the status, the message, the four depths and the +//! MAPQ shares; `cyp2d6-bed` prints CYP2D6 between its two flanks; an input +//! without a CYP2D6 row, with regions other than those (a zero-length one +//! included), a region listed twice with two depths, or an output that +//! cannot be written exits 2 and writes nothing. The MAPQ shares beside the +//! verdict (`cyp2d6-measure`, then `cyp2d6-check --measure`): on synthetic +//! reads at CYP2D6, reads in REP6 at MAPQ 0 lower the share over Cyrius's +//! region but not over the gene body; without the measure file every share +//! it gives reads 'not measured'; the shares never change the status; a file +//! that is not measure's exits 2. + +use std::collections::BTreeMap; +use std::fs; +use std::path::{Path, PathBuf}; + +const OK: &str = "CYP2D6 depth is fit for a copy-number call"; +const UNRELIABLE: &str = "CYP2D6 copy number unreliable: reads are multi-mapped \ + (was this BAM aligned to a reference with ALT contigs?)"; +const NO_DEPTH: &str = "CYP2D6 copy number unreliable: no reads in the flanks of CYP2D6 \ + (chr22:42.05-42.25 Mb), so its depth cannot be compared"; +const ROWS: [&str; 12] = [ + "metric", + "status", + "message", + "gene_depth_all", + "gene_depth_mapq1", + "flank_depth_all", + "flank_depth_mapq1", + "gene_mapq1_share", + "gene_mapq20_share", + "body_depth_all", + "body_mapq1_share", + "body_mapq20_share", +]; + +const CASES: [(&str, &str, &str); 5] = [ + ("clean_no_alt", "ok", OK), + ("multimapped_with_alt", "unreliable", UNRELIABLE), + ("deleted_both", "ok", OK), + ("deleted_one", "ok", OK), + ("no_flank_reads", "unreliable", NO_DEPTH), +]; + +fn parity() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/data/parity") +} + +/// og's golden of a case when it has one (a deliberate fix), else the +/// oracle's. +fn golden(case: &str) -> PathBuf { + let og = parity().join("og").join(case); + if og.is_dir() { + og + } else { + parity().join("want").join(case) + } +} + +fn exit_code(case: &str) -> i32 { + let text = fs::read_to_string(golden(case).join("exit")).unwrap(); + text.trim().parse().unwrap() +} + +/// The files the case writes, by name. +fn written(case: &str) -> Vec { + let Ok(dir) = fs::read_dir(golden(case).join("files")) else { + return Vec::new(); + }; + let mut names: Vec = dir + .map(|e| e.unwrap().file_name().to_string_lossy().into_owned()) + .collect(); + names.sort(); + names +} + +/// A two-column table: its rows by key, and the keys in file order. +fn table(case: &str, name: &str) -> (BTreeMap, Vec) { + let text = fs::read_to_string(golden(case).join("files").join(name)).unwrap(); + let mut rows = BTreeMap::new(); + let mut order = Vec::new(); + for line in text.lines() { + let (k, v) = line.split_once('\t').unwrap_or((line, "")); + rows.insert(k.to_owned(), v.to_owned()); + order.push(k.to_owned()); + } + (rows, order) +} + +fn get<'a>(rows: &'a BTreeMap, key: &str) -> Option<&'a str> { + rows.get(key).map(String::as_str) +} + +#[test] +fn each_depth_table_gives_its_verdict_and_the_shares() { + for (case, status, message) in CASES { + for (suffix, measured) in [("", false), ("-measured", true)] { + let c = format!("check-{case}{suffix}"); + assert_eq!(exit_code(&c), 0, "{c}: exit code"); + let (rows, order) = table(&c, "check.tsv"); + assert_eq!(get(&rows, "status"), Some(status), "{c}: status"); + assert_eq!(get(&rows, "message"), Some(message), "{c}: message"); + assert_eq!(order, ROWS, "{c}: header and the eleven rows"); + let shares: Vec> = [ + "gene_mapq20_share", + "body_depth_all", + "body_mapq1_share", + "body_mapq20_share", + ] + .iter() + .map(|k| get(&rows, k)) + .collect(); + if measured { + assert!( + !shares.contains(&Some("not measured")), + "{c}: the measure file gives every share: {shares:?}" + ); + } else { + assert_eq!( + shares, + [Some("not measured"); 4], + "{c}: without the measure file the other shares are not measured" + ); + } + } + } +} + +#[test] +fn clean_no_alt_depths_and_share() { + let (rows, _) = table("check-clean_no_alt", "check.tsv"); + assert_eq!( + get(&rows, "gene_mapq1_share"), + Some("0.91"), + "clean_no_alt: the verdict's MAPQ >= 1 share over Cyrius's region (16.79 / 18.53)" + ); + let depths: Vec> = [ + "gene_depth_all", + "gene_depth_mapq1", + "flank_depth_all", + "flank_depth_mapq1", + ] + .iter() + .map(|k| get(&rows, k)) + .collect(); + assert_eq!( + depths, + [Some("18.53"), Some("16.79"), Some("29.68"), Some("29.67")], + "clean_no_alt: depths, length-weighted" + ); +} + +#[test] +fn cyp2d6_bed_prints_the_gene_between_two_flanks() { + let bed = fs::read_to_string(golden("cyp2d6-bed").join("stdout")).unwrap(); + let names: Vec<&str> = bed + .lines() + .map(|l| l.split('\t').nth(3).unwrap_or_default()) + .collect(); + assert_eq!( + names, + ["flank", "CYP2D6", "flank"], + "cyp2d6-bed: CYP2D6 between two flanks" + ); +} + +#[test] +fn bad_inputs_exit_2_and_write_nothing() { + for (c, why) in [ + ("check-no-gene", "an input without a CYP2D6 row"), + ( + "check-other-region", + "regions other than the ones cyp2d6-bed prints", + ), + ("check-zero-length", "a zero-length region"), + ( + "check-duplicate-row", + "a region listed twice with two depths", + ), + ("check-bad-measure", "a measure file that is not measure's"), + ("check-out-unwritable", "an output that cannot be written"), + ] { + assert_eq!( + (exit_code(c), written(c)), + (2, Vec::::new()), + "{why}: exit 2 and nothing written" + ); + } +} + +/// The synthetic reads of `make_parity.py`: Cyrius's region tiled, MAPQ 0 +/// in REP6 and around the body, the body at MAPQ 60 except some at MAPQ 10. +#[test] +fn measure_shares_on_synthetic_reads() { + let text = fs::read_to_string( + golden("measure-cyp2d6_synthetic") + .join("files") + .join("shares.tsv"), + ) + .unwrap(); + let m: BTreeMap<(String, u32), f64> = text + .lines() + .skip(1) + .map(|l| { + let c: Vec<&str> = l.split('\t').collect(); + ( + (c[0].to_owned(), c[1].parse().unwrap()), + c[2].parse().unwrap(), + ) + }) + .collect(); + let at = |r: &str, q: u32| m[&(r.to_owned(), q)]; + assert_eq!( + at("body", 1), + at("body", 0), + "measure: the MAPQ 0 reads outside the body leave the body's MAPQ >= 1 depth whole" + ); + assert_eq!( + ["gene", "body"].map(|k| at(k, 20) < at(k, 1)), + [true, true], + "measure: the MAPQ 10 reads count at MAPQ >= 1, not at >= 20" + ); + assert!( + at("gene", 1) < at("gene", 0), + "measure: over Cyrius's region the MAPQ 0 reads lower the MAPQ >= 1 depth" + ); +} diff --git a/rust/crates/og-filters/tests/port_tests_pgx_mito.rs b/rust/crates/og-filters/tests/port_tests_pgx_mito.rs new file mode 100644 index 000000000..903d44755 --- /dev/null +++ b/rust/crates/og-filters/tests/port_tests_pgx_mito.rs @@ -0,0 +1,687 @@ +//! `og pgx mito-merge`: the MT-RNR1 outside call PharmCAT gets from the +//! chrM calls of step 20 / MITO_VARIANTS (step 07 and PHARMCAT run it). +//! +//! The test builds a stand-in for `pharmcat.jar`, a zip holding +//! `org/pharmgkb/pharmcat/phenotype/MT_RNR1.json` with some of PharmCAT +//! 3.4.0's MT-RNR1 alleles and their function values, writes invented +//! Mutect2-style chrM VCFs (GT 0/1 at every AF, as Mutect2 writes them), runs +//! the verb in a child copy of this test binary ([`og_child`]) so its log on +//! stdout and stderr is read as the `og` binary writes it, and checks: +//! +//! 1. a homoplasmic PASS m.1555A>G (AF 0.99) is the call; step 36's lines +//! are copied before it and an MT-RNR1 line there is dropped; +//! 2. AF 0.95 counts as homoplasmic, 0.949 does not; `--min-af` moves the +//! bound; +//! 3. a heteroplasmic, a filtered or an AF-less m.1555A>G passes nothing and +//! says why; so do a homoplasmic PASS change at a named position that +//! PharmCAT does not name and a PASS deletion near m.961; a filtered one, +//! or a low-AF unnamed change, does not; +//! 4. PASS chrM records with no named allele give Reference when samtools +//! depth shows 20 reads or more at every named position; without a depth +//! file, with one position below 20 (`--min-depth` moves the bound) or +//! absent from it, no call; a homoplasmic named allele needs no depth; a +//! file with no PASS chrM record gives no call; records on other contigs +//! are ignored; +//! 5. two homoplasmic named alleles: the increased-risk one is the call, the +//! other is listed; +//! 6. INFO AF when FORMAT has none, the AF of the second ALT of a +//! multi-allelic record, a bgzip/gzip input and the contig name MT are +//! read; +//! 7. a jar without the MT-RNR1 definitions, or an unreadable VCF, exits 2. + +use std::fs; +use std::io::Write; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +const INC: &str = "Increased risk of aminoglycoside-induced hearing loss"; +const UNC: &str = "Uncertain risk of aminoglycoside-induced hearing loss"; +const NOR: &str = "Normal risk of aminoglycoside-induced hearing loss"; +const ALLELES: [(&str, &str); 8] = [ + ("Reference", NOR), + ("m.827A>G", NOR), + ("m.960C>del", UNC), + ("m.961T>del", UNC), + ("m.961T>del+Cn", UNC), + ("m.1095T>C", INC), + ("m.1189T>C", UNC), + ("m.1555A>G", INC), +]; +const HEADER: &str = "##fileformat=VCFv4.2\n##contig=\n\ + #CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tS\n"; + +const CHILD: &str = "OG_PORT_TESTS_PGX_CHILD"; +const MARK: &str = "\u{1e}og-child\u{1e}"; + +/// Not a test of its own: the child [`og`] starts runs `og pgx mito-merge` +/// here with the argv it was handed, then exits with the verb's code. In +/// the normal test run it returns at once. +#[test] +fn og_child() { + let Some(argv) = std::env::var_os(CHILD) else { + return; + }; + let argv: Vec = argv + .to_string_lossy() + .split('\u{1f}') + .map(str::to_owned) + .collect(); + print!("{MARK}"); + eprint!("{MARK}"); + let code = og_filters::mito::cli(&argv); + let _ = std::io::stdout().flush(); + let _ = std::io::stderr().flush(); + std::process::exit(i32::from(code)); +} + +/// `og pgx mito-merge ARGV` in a child copy of this test binary: (exit +/// code, stdout and stderr). +fn og(argv: &[String]) -> (i32, String) { + let child = Command::new(std::env::current_exe().unwrap()) + .args([ + "--exact", + "og_child", + "--nocapture", + "--test-threads=1", + "-q", + ]) + .env(CHILD, argv.join("\u{1f}")) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + let out = child.wait_with_output().unwrap(); + let after = |b: &[u8]| { + let s = String::from_utf8_lossy(b); + match s.split_once(MARK) { + Some((_, rest)) => rest.to_owned(), + None => panic!("the child never reached the verb: {s}"), + } + }; + ( + out.status.code().unwrap_or(-1), + after(&out.stdout) + &after(&out.stderr), + ) +} + +/// A zip file of stored members, as Python's `zipfile.ZipFile(path, "w")` +/// writes it. +fn write_zip(path: &Path, members: &[(&str, &str)]) { + let mut out = Vec::new(); + let mut central = Vec::new(); + for (name, body) in members { + let mut crc = flate2::Crc::new(); + crc.update(body.as_bytes()); + let offset = u32::try_from(out.len()).unwrap(); + let size = u32::try_from(body.len()).unwrap(); + let name_len = u16::try_from(name.len()).unwrap(); + // Local header: version 20, no flags, stored, 1980-01-01 00:00. + out.extend_from_slice(&0x0403_4b50_u32.to_le_bytes()); + for v in [20_u16, 0, 0, 0, 0x21] { + out.extend_from_slice(&v.to_le_bytes()); + } + for v in [crc.sum(), size, size] { + out.extend_from_slice(&v.to_le_bytes()); + } + out.extend_from_slice(&name_len.to_le_bytes()); + out.extend_from_slice(&0_u16.to_le_bytes()); + out.extend_from_slice(name.as_bytes()); + out.extend_from_slice(body.as_bytes()); + central.extend_from_slice(&0x0201_4b50_u32.to_le_bytes()); + for v in [20_u16, 20, 0, 0, 0, 0x21] { + central.extend_from_slice(&v.to_le_bytes()); + } + for v in [crc.sum(), size, size] { + central.extend_from_slice(&v.to_le_bytes()); + } + for v in [name_len, 0, 0, 0, 0] { + central.extend_from_slice(&v.to_le_bytes()); + } + central.extend_from_slice(&0_u32.to_le_bytes()); + central.extend_from_slice(&offset.to_le_bytes()); + central.extend_from_slice(name.as_bytes()); + } + let cd_offset = u32::try_from(out.len()).unwrap(); + let count = u16::try_from(members.len()).unwrap(); + let cd_len = u32::try_from(central.len()).unwrap(); + out.extend_from_slice(¢ral); + out.extend_from_slice(&0x0605_4b50_u32.to_le_bytes()); + for v in [0_u16, 0, count, count] { + out.extend_from_slice(&v.to_le_bytes()); + } + out.extend_from_slice(&cd_len.to_le_bytes()); + out.extend_from_slice(&cd_offset.to_le_bytes()); + out.extend_from_slice(&0_u16.to_le_bytes()); + fs::write(path, out).unwrap(); +} + +/// One Mutect2-style chrM record. +fn rec(pos: u32, r: &str, alt: &str, af: Option, filt: &str, chrom: &str) -> String { + let (fmt, val) = match af { + Some(af) => ("GT:AF:DP", format!("0/1:{af}:3000")), + None => ("GT:DP", "0/1:3000".to_owned()), + }; + format!("{chrom}\t{pos}\t.\t{r}\t{alt}\t.\t{filt}\t.\t{fmt}\t{val}\n") +} + +fn pass(pos: u32, r: &str, alt: &str, af: f64) -> String { + rec(pos, r, alt, Some(af), "PASS", "chrM") +} + +fn background() -> String { + pass(263, "A", "G", 0.998) + &pass(750, "A", "G", 0.999) +} + +/// The scratch folder of one test, with its stand-in jar. +struct Case { + dir: PathBuf, + jar: String, + n: usize, +} + +/// What one run gave: exit code, the log, the lines of the output file +/// (`None` when it was not written). +struct Out { + rc: i32, + log: String, + lines: Option>, +} + +impl Out { + /// The MT-RNR1 calls of a run that exited 0 and wrote its output, so an + /// empty list never comes from a run that failed early. + fn mt(&self) -> Vec<&str> { + assert!( + self.rc == 0 && self.lines.is_some(), + "the run must exit 0 and write its output (exit {}): {}", + self.rc, + self.log + ); + self.lines + .iter() + .flatten() + .filter_map(|l| l.strip_prefix("MT-RNR1\t")) + .collect() + } +} + +#[derive(Default)] +struct Run<'a> { + outside: Option<&'a str>, + gz: bool, + extra: &'a [&'a str], + jar: Option, + /// Depth at the positions 648-1601: 500, or the value given here + /// (`None`: the position is left out). + depth: Option<&'a [(u32, Option)]>, +} + +impl Case { + fn new(name: &str) -> Self { + let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("pgx-mito-{name}")); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + let named: Vec = ALLELES + .iter() + .map(|(n, f)| { + format!( + "{{\"name\": \"{n}\", \"functionValue\": \"{f}\", \"activityValue\": null}}" + ) + }) + .collect(); + let json = format!( + "{{\"gene\": \"MT-RNR1\", \"namedAlleles\": [{}]}}", + named.join(", ") + ); + let jar = dir.join("pharmcat.jar"); + write_zip( + &jar, + &[("org/pharmgkb/pharmcat/phenotype/MT_RNR1.json", &json)], + ); + Self { + jar: jar.to_string_lossy().into_owned(), + dir, + n: 0, + } + } + + fn path(&self, name: &str) -> String { + self.dir.join(name).to_string_lossy().into_owned() + } + + fn run(&mut self, records: &str, o: &Run) -> Out { + self.n += 1; + let n = self.n; + let mut args: Vec = Vec::new(); + let vcf = self.path(&format!("m{n}.vcf{}", if o.gz { ".gz" } else { "" })); + let text = format!("{HEADER}{records}"); + if o.gz { + let mut enc = flate2::write::GzEncoder::new(Vec::new(), flate2::Compression::default()); + enc.write_all(text.as_bytes()).unwrap(); + fs::write(&vcf, enc.finish().unwrap()).unwrap(); + } else { + fs::write(&vcf, text).unwrap(); + } + let out = self.path(&format!("o{n}.tsv")); + args.extend( + [ + "--jar", + o.jar.as_deref().unwrap_or(&self.jar), + "--mito", + &vcf, + "--out", + &out, + ] + .map(str::to_owned), + ); + args.extend(o.extra.iter().map(|s| (*s).to_owned())); + if let Some(depth) = o.depth { + let path = self.path(&format!("d{n}.depth")); + let mut t = String::new(); + for p in 648..1602 { + let d = depth + .iter() + .find(|(q, _)| *q == p) + .map_or(Some(500), |(_, d)| *d); + if let Some(d) = d { + t.push_str(&format!("chrM\t{p}\t{d}\n")); + } + } + fs::write(&path, t).unwrap(); + args.push("--depth".to_owned()); + args.push(path); + } + if let Some(text) = o.outside { + let path = self.path(&format!("c{n}.tsv")); + fs::write(&path, text).unwrap(); + args.push("--outside".to_owned()); + args.push(path); + } + let (rc, log) = og(&args); + let lines = fs::read_to_string(&out) + .ok() + .map(|t| t.lines().map(str::to_owned).collect()); + Out { rc, log, lines } + } +} + +impl Drop for Case { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.dir); + } +} + +#[test] +fn case1_a_homoplasmic_m1555ag_is_the_call() { + let mut c = Case::new("1"); + let r = c.run( + &(background() + &pass(1555, "A", "G", 0.99)), + &Run { + outside: Some("HLA-A\t*01:01/*26:01\nMT-RNR1\tm.1095T>C\n"), + ..Run::default() + }, + ); + assert_eq!(r.rc, 0, "1: exit 0: {}", r.log); + assert_eq!(r.mt(), ["m.1555A>G"], "1: MT-RNR1 m.1555A>G is the call"); + assert_eq!( + r.lines.as_ref().and_then(|l| l.first()).map(String::as_str), + Some("HLA-A\t*01:01/*26:01"), + "1: step 36's HLA-A line is copied first" + ); + assert_eq!( + r.mt().len(), + 1, + "1: the MT-RNR1 line of step 36's file is dropped" + ); + assert!( + r.log.contains(&format!( + "MT-RNR1 m.1555A>G passed to PharmCAT as an outside call (homoplasmic, chrM:1555 A>G AF 0.990; {INC}" + )), + "1: the log says it passed m.1555A>G, homoplasmic, with the risk: {}", + r.log + ); + assert!( + r.log.contains("dropped the MT-RNR1 line"), + "1: the log names the dropped line: {}", + r.log + ); +} + +#[test] +fn case2_the_af_bound() { + let mut c = Case::new("2"); + let r = c.run( + &(background() + &pass(1555, "A", "G", 0.95)), + &Run::default(), + ); + assert_eq!( + r.mt(), + ["m.1555A>G"], + "2: AF 0.95 is homoplasmic: {}", + r.log + ); + let r = c.run( + &(background() + &pass(1555, "A", "G", 0.949)), + &Run::default(), + ); + assert!(r.mt().is_empty(), "2: AF 0.949 passes nothing: {}", r.log); + let r = c.run( + &(background() + &pass(1555, "A", "G", 0.9)), + &Run { + extra: &["--min-af", "0.85"], + ..Run::default() + }, + ); + assert_eq!( + r.mt(), + ["m.1555A>G"], + "2: --min-af 0.85 takes AF 0.9: {}", + r.log + ); +} + +#[test] +fn case3_what_passes_nothing() { + let mut c = Case::new("3"); + let cases = [ + ( + "heteroplasmic", + pass(1555, "A", "G", 0.4), + "is heteroplasmic (AF below 0.95)", + ), + ( + "filtered", + rec(1555, "A", "G", Some(0.99), "possible_numt", "chrM"), + "is filtered (possible_numt)", + ), + ( + "without an allele fraction", + rec(1555, "A", "G", None, "PASS", "chrM"), + "has no allele fraction", + ), + ( + "a change PharmCAT does not name", + pass(1555, "A", "C", 0.99), + "a change at an MT-RNR1 position that PharmCAT does not name", + ), + ( + "a deletion near m.961", + pass(955, "AC", "A", 0.99), + "at or near m.960", + ), + ( + "an insertion at m.961", + pass(961, "T", "TC", 0.99), + "(chrM:961 T>TC AF 0.990) at or near m.960", + ), + ]; + for (desc, records, want) in cases { + let r = c.run( + &(background() + &records), + &Run { + outside: Some("HLA-B\t*35:01/*44:02\n"), + ..Run::default() + }, + ); + assert_eq!(r.rc, 0, "3: {desc}: exit 0: {}", r.log); + assert!( + r.mt().is_empty(), + "3: {desc}: no MT-RNR1 line: {:?}", + r.lines + ); + assert_eq!( + r.lines, + Some(vec!["HLA-B\t*35:01/*44:02".to_owned()]), + "3: {desc}: step 36's line is still copied" + ); + assert!( + r.log.contains(want), + "3: {desc}: the log says why ({want}): {}", + r.log + ); + assert!( + r.log.contains("it stays No Result"), + "3: {desc}: the log says the gene stays No Result: {}", + r.log + ); + } + // m.960 is named only as a deletion: an unnamed SNV there still holds + // Reference back. + let r = c.run( + &(background() + &pass(960, "C", "T", 0.99)), + &Run { + depth: Some(&[]), + ..Run::default() + }, + ); + assert!( + r.mt().is_empty(), + "3: an unnamed SNV at m.960 (named only as a deletion): no MT-RNR1 line: {}", + r.log + ); + assert!( + r.log + .contains("a change at an MT-RNR1 position that PharmCAT does not name"), + "3: an unnamed SNV at m.960: the log says why: {}", + r.log + ); + let r = c.run( + &(background() + + &rec(1555, "A", "C", Some(0.3), "weak_evidence", "chrM") + + &rec(955, "AC", "A", Some(0.2), "weak_evidence", "chrM")), + &Run { + depth: Some(&[]), + ..Run::default() + }, + ); + assert_eq!( + r.mt(), + ["Reference"], + "3: a filtered unnamed change and a filtered deletion near m.961 hold nothing back: {}", + r.log + ); +} + +#[test] +fn case4_reference_and_no_pass_record() { + let mut c = Case::new("4"); + let r = c.run( + &(background() + + &pass(2706, "A", "G", 0.999) + + &rec(1555, "A", "G", Some(0.99), "PASS", "chr1")), + &Run { + depth: Some(&[]), + ..Run::default() + }, + ); + assert_eq!( + r.mt(), + ["Reference"], + "4: no named allele on chrM, depth 500: Reference: {}", + r.log + ); + assert!( + r.log.contains( + "all 6 named positions have depth 20 or more (lowest 500); 3 PASS chrM records read" + ), + "4: the log says all 6 named positions are covered and counts the PASS records: {}", + r.log + ); + let r = c.run( + &background(), + &Run { + depth: Some(&[(1555, Some(20)), (961, Some(20))]), + ..Run::default() + }, + ); + assert_eq!( + r.mt(), + ["Reference"], + "4: depth exactly 20 counts: {}", + r.log + ); + let r = c.run(&background(), &Run::default()); + assert!(r.mt().is_empty(), "4: no depth file: no call: {}", r.log); + assert!( + r.log.contains("there is no read depth") && r.log.contains("stays No Result"), + "4: no depth file: the log says so: {}", + r.log + ); + let r = c.run( + &background(), + &Run { + depth: Some(&[(1555, Some(5))]), + ..Run::default() + }, + ); + assert!( + r.mt().is_empty(), + "4: chrM:1555 at depth 5: no call: {}", + r.log + ); + assert!( + r.log + .contains("1 of its 6 positions have depth below 20 (chrM:1555 depth 5)"), + "4: chrM:1555 at depth 5: the log names it: {}", + r.log + ); + let r = c.run( + &background(), + &Run { + depth: Some(&[(1555, Some(5))]), + extra: &["--min-depth", "5"], + ..Run::default() + }, + ); + assert_eq!( + r.mt(), + ["Reference"], + "4: --min-depth 5 takes depth 5: {}", + r.log + ); + let r = c.run( + &background(), + &Run { + depth: Some(&[(960, None)]), + ..Run::default() + }, + ); + assert!( + r.mt().is_empty() && r.log.contains("chrM:960 depth 0"), + "4: a named position absent from the depth file counts as depth 0: {}", + r.log + ); + let r = c.run( + &(background() + &pass(1555, "A", "G", 0.99)), + &Run::default(), + ); + assert_eq!( + r.mt(), + ["m.1555A>G"], + "4: a homoplasmic named allele needs no depth file: {}", + r.log + ); + let r = c.run( + &rec(263, "A", "G", Some(0.99), "weak_evidence", "chrM"), + &Run::default(), + ); + assert_eq!(r.rc, 0, "4: no PASS chrM record: exit 0: {}", r.log); + assert!(r.mt().is_empty(), "4: no PASS chrM record: no call"); + assert!( + r.log.contains("the chrM call set has no PASS record"), + "4: no PASS chrM record: the log says so: {}", + r.log + ); + assert_eq!( + r.lines, + Some(Vec::new()), + "4: an empty output file is still written" + ); +} + +#[test] +fn case5_two_homoplasmic_named_alleles() { + let mut c = Case::new("5"); + let r = c.run( + &(background() + &pass(1189, "T", "C", 0.99) + &pass(1555, "A", "G", 0.98)), + &Run::default(), + ); + assert_eq!( + r.mt(), + ["m.1555A>G"], + "5: the increased-risk allele is the call: {}", + r.log + ); + assert!( + r.log.contains("also homoplasmic, not passed") && r.log.contains("m.1189T>C"), + "5: the other is listed: {}", + r.log + ); +} + +#[test] +fn case6_af_sources_multi_allelic_gzip_and_mt() { + let mut c = Case::new("6"); + let r = c.run( + &(background() + "chrM\t1555\t.\tA\tG\t.\tPASS\tAF=0.97\tGT\t0/1\n"), + &Run::default(), + ); + assert_eq!( + r.mt(), + ["m.1555A>G"], + "6: INFO AF is read when FORMAT has none: {}", + r.log + ); + let r = c.run( + &(background() + "chrM\t1555\t.\tA\tC,G\t.\tPASS\t.\tGT:AF\t0/1/2:0.01,0.98\n"), + &Run::default(), + ); + assert_eq!( + r.mt(), + ["m.1555A>G"], + "6: the AF of the second ALT: {}", + r.log + ); + let r = c.run( + &(background() + &pass(1555, "A", "G", 0.99)), + &Run { + gz: true, + ..Run::default() + }, + ); + assert_eq!(r.mt(), ["m.1555A>G"], "6: a gzip VCF: {}", r.log); + let r = c.run( + &(rec(263, "A", "G", Some(0.99), "PASS", "MT") + + &rec(1555, "A", "G", Some(0.99), "PASS", "MT")), + &Run::default(), + ); + assert_eq!(r.mt(), ["m.1555A>G"], "6: contig MT: {}", r.log); +} + +#[test] +fn case7_errors_exit_2() { + let mut c = Case::new("7"); + let bad = c.dir.join("empty.jar"); + write_zip(&bad, &[("logback.xml", "")]); + let r = c.run( + &background(), + &Run { + jar: Some(bad.to_string_lossy().into_owned()), + ..Run::default() + }, + ); + assert_eq!(r.rc, 2, "7: a jar without MT_RNR1.json exits 2: {}", r.log); + assert!( + r.log + .contains("org/pharmgkb/pharmcat/phenotype/MT_RNR1.json"), + "7: and names the resource: {}", + r.log + ); + let r = c.run("chrM\t1555\tA\n", &Run::default()); + assert_eq!( + r.rc, 2, + "7: a record with too few columns exits 2: {}", + r.log + ); +} diff --git a/rust/crates/og-filters/tests/port_tests_pgx_refblock.rs b/rust/crates/og-filters/tests/port_tests_pgx_refblock.rs new file mode 100644 index 000000000..31f326f98 --- /dev/null +++ b/rust/crates/og-filters/tests/port_tests_pgx_refblock.rs @@ -0,0 +1,403 @@ +//! `og pgx refblock-fill`: PharmCAT's multi-base positions called 0/0 from +//! the reference blocks of a gVCF (steps 07 and PHARMCAT_PREPROCESS run it). +//! +//! The fixture gVCF (`tests/fixtures/pharmcat/refblocks.g.vcf`) has +//! reference blocks over nine PharmCAT positions longer than one base, from +//! the subset of PharmCAT 3.4.0's positions file beside it. The test expands +//! the blocks into one-base records as `bcftools convert --gvcf2vcf | +//! bcftools view --trim-alt-alleles -i 'GT!="mis"'` does, runs the verb on +//! that stream in a child copy of this test binary ([`og_child`]), so its +//! stdin, stdout and stderr are the `og` binary's, and checks: +//! +//! 1. five positions are called 0/0, with PharmCAT's ID, REF and ALT, the +//! flag PGX_REFBLOCK, the block's genotype (0 on haploid chrX) and the +//! lowest GQ over the span: DPYD (two blocks meet inside the span), +//! UGT1A1 *28, a TPMT insertion, a RYR1 two-base substitution and a G6PD +//! deletion; +//! 2. four stay missing: a ./. hole inside the span (CYP2C9, 11 bp), a block +//! with GQ 10, a variant record inside the span, a span no block reaches; +//! 3. each filled record takes the place of the one-base record at its +//! position, every other line passes unchanged, and the output stays +//! sorted; +//! 4. the GQ threshold: 20 by default, a block at exactly `--min-gq` counts; +//! 5. a block that stops one base short, a block with a failed FILTER, a ./. +//! block with a high GQ, or a deletion that reaches into the span from +//! before it, fills nothing; a gVCF with two samples or no #CHROM line is +//! an error (exit 2). + +use std::collections::BTreeMap; +use std::fs::{self, File}; +use std::io::Write; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +const CHILD: &str = "OG_PORT_TESTS_PGX_CHILD"; +const MARK: &str = "\u{1e}og-child\u{1e}"; + +fn fx(name: &str) -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")) + .join("../../../tests/fixtures/pharmcat") + .join(name) +} + +fn gvcf() -> String { + fs::read_to_string(fx("refblocks.g.vcf")).unwrap() +} + +/// (chrom, pos) -> (REF, ALT, GT, GQ, ID) of the five filled positions. +const FILLED: [((&str, u64), [&str; 5]); 5] = [ + (("chr1", 97_450_065), ["TG", "T", "0/0", "45", "rs72549303"]), + ( + ("chr2", 233_760_233), + ["CAT", "C,CATAT,CATATAT", "0/0", "50", "rs3064744"], + ), + ( + ("chr6", 18_149_032), + ["C", "CT", "0/0", "40", "rs759836180"], + ), + ( + ("chr19", 38_572_266), + ["TC", "CT", "0/0", "35", "rs193922862"], + ), + (("chrX", 154_532_055), ["CTCT", "C", "0", "30", "."]), +]; +const LEFT: [((&str, u64), &str); 4] = [ + (("chr10", 94_942_205), "a ./. hole inside the REF span"), + (("chr10", 94_949_161), "a block with GQ 10"), + ( + ("chr10", 94_949_281), + "a variant record inside the REF span", + ), + (("chr6", 18_149_103), "no block over the span"), +]; +const UGT1A1: (&str, u64) = ("chr2", 233_760_233); + +/// Not a test of its own: the child [`run`] starts runs `og pgx +/// refblock-fill` here with the argv it was handed, then exits with the +/// verb's code. In the normal test run it returns at once. +#[test] +fn og_child() { + let Some(argv) = std::env::var_os(CHILD) else { + return; + }; + let argv: Vec = argv + .to_string_lossy() + .split('\u{1f}') + .map(str::to_owned) + .collect(); + print!("{MARK}"); + eprint!("{MARK}"); + let _ = std::io::stdout().flush(); + let code = og_filters::refblock::cli(&argv); + let _ = std::io::stdout().flush(); + let _ = std::io::stderr().flush(); + std::process::exit(i32::from(code)); +} + +/// What gvcf2vcf plus `view --trim-alt-alleles -i 'GT!="mis"'` writes: one +/// record per base of each block (REF from the reference: N here, the verb +/// reads only its length), ALT . for a reference call, no ./. records. +fn expand(gvcf: &str) -> String { + let mut out = String::new(); + for line in gvcf.split_inclusive('\n') { + if line.starts_with('#') { + out.push_str(line); + continue; + } + let c: Vec<&str> = line.trim_end_matches('\n').split('\t').collect(); + let gt = c[9].split(':').next().unwrap(); + if gt.split(['/', '|']).any(|a| a == ".") { + continue; + } + let alts: Vec<&str> = c[4].split(',').filter(|a| *a != "<*>").collect(); + let end = c[7].find("END=").and_then(|i| { + let digits: String = c[7][i + 4..] + .chars() + .take_while(char::is_ascii_digit) + .collect(); + digits.parse::().ok() + }); + match end { + Some(end) if alts.is_empty() => { + for p in c[1].parse::().unwrap()..=end { + let p = p.to_string(); + out.push_str( + &[c[0], &p, ".", "N", ".", c[5], c[6], ".", c[8], c[9]].join("\t"), + ); + out.push('\n'); + } + } + _ => { + let alt = if alts.is_empty() { + ".".to_owned() + } else { + alts.join(",") + }; + let mut cols: Vec<&str> = c[..4].to_vec(); + cols.push(&alt); + cols.extend(&c[5..]); + out.push_str(&cols.join("\t")); + out.push('\n'); + } + } + } + out +} + +static RUNS: std::sync::atomic::AtomicUsize = std::sync::atomic::AtomicUsize::new(0); + +/// The verb on `gvcf` (its expansion on stdin, or `stdin`): exit code, +/// stdout, stderr. +fn run(gvcf: &str, opts: &[&str], stdin: Option<&str>) -> (i32, String, String) { + let n = RUNS.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + let dir = Path::new(env!("CARGO_TARGET_TMPDIR")) + .join(format!("pgx-refblock-{}-{n}", std::process::id())); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + let g = dir.join("records.vcf"); + fs::write(&g, gvcf).unwrap(); + let input = dir.join("stdin.vcf"); + fs::write(&input, stdin.map_or_else(|| expand(gvcf), str::to_owned)).unwrap(); + let mut argv: Vec = opts.iter().map(|s| (*s).to_owned()).collect(); + argv.push( + fx("pharmcat_positions-3.4.0-subset.vcf") + .to_string_lossy() + .into_owned(), + ); + argv.push(g.to_string_lossy().into_owned()); + let out = Command::new(std::env::current_exe().unwrap()) + .args([ + "--exact", + "og_child", + "--nocapture", + "--test-threads=1", + "-q", + ]) + .env(CHILD, argv.join("\u{1f}")) + .stdin(Stdio::from(File::open(&input).unwrap())) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap() + .wait_with_output() + .unwrap(); + let _ = fs::remove_dir_all(&dir); + let after = |b: &[u8]| { + let s = String::from_utf8_lossy(b); + match s.split_once(MARK) { + Some((_, rest)) => rest.to_owned(), + None => panic!("the child never reached the verb: {s}"), + } + }; + ( + out.status.code().unwrap_or(-1), + after(&out.stdout), + after(&out.stderr), + ) +} + +/// Stdout of a run on valid input, which must exit 0, so "nothing filled" +/// never comes from a run that failed. +fn ok(gvcf: &str, opts: &[&str]) -> String { + let (rc, out, err) = run(gvcf, opts, None); + assert_eq!(rc, 0, "a valid input exits 0: {err}"); + out +} + +fn records(vcf: &str) -> Vec> { + vcf.lines() + .filter(|l| !l.is_empty() && !l.starts_with('#')) + .map(|l| l.split('\t').map(str::to_owned).collect()) + .collect() +} + +fn key(r: &[String]) -> (String, u64) { + (r[0].clone(), r[1].parse().unwrap()) +} + +fn fills(vcf: &str) -> BTreeMap<(String, u64), Vec> { + records(vcf) + .into_iter() + .filter(|r| r[7] == "PGX_REFBLOCK") + .map(|r| (key(&r), r)) + .collect() +} + +fn filled(vcf: &str, (chrom, pos): (&str, u64)) -> bool { + fills(vcf).contains_key(&(chrom.to_owned(), pos)) +} + +#[test] +fn the_fixture_fills_five_and_leaves_four() { + let g = gvcf(); + let (rc, out, err) = run(&g, &[], None); + assert_eq!(rc, 0, "runs and exits 0: {err}"); + let got = fills(&out); + // 1. filled + let mut want: Vec<(String, u64)> = FILLED + .iter() + .map(|((c, p), _)| ((*c).to_owned(), *p)) + .collect(); + want.sort(); + assert_eq!( + got.keys().cloned().collect::>(), + want, + "five positions called 0/0: DPYD, UGT1A1 *28, TPMT, RYR1, G6PD" + ); + for ((chrom, pos), [r, alt, gt, gq, id]) in FILLED { + let row = got.get(&(chrom.to_owned(), pos)); + assert!( + row.is_some_and(|x| x[2..5] == [id, r, alt] + && x[6] == "PASS" + && x[8] == "GT:GQ" + && x[9] == format!("{gt}:{gq}")), + "{chrom}:{pos}: PharmCAT's ID, REF and ALT, GT {gt}, lowest GQ {gq}: {row:?}" + ); + } + // 2. left missing + for ((chrom, pos), why) in LEFT { + assert!( + !got.contains_key(&(chrom.to_owned(), pos)), + "{chrom}:{pos} stays missing ({why})" + ); + } + assert!( + err.contains( + "5 of 9 PharmCAT positions longer than one base called 0/0 from reference blocks with GQ >= 20" + ), + "the summary on stderr counts 5 of the 9 positions longer than one base: {err}" + ); + // 3. replaced in place, the rest unchanged, sorted + let inp = records(&expand(&g)); + let outr = records(&out); + let mut at: BTreeMap<(String, u64), usize> = BTreeMap::new(); + for r in &outr { + *at.entry(key(r)).or_default() += 1; + } + let filled_keys: Vec<(String, u64)> = FILLED + .iter() + .map(|((c, p), _)| ((*c).to_owned(), *p)) + .collect(); + assert!( + filled_keys.iter().all(|k| at.get(k) == Some(&1)), + "one record at each filled position (the one-base record is replaced)" + ); + let kept: Vec<&Vec> = inp + .iter() + .filter(|r| !filled_keys.contains(&key(r))) + .collect(); + let passed: Vec<&Vec> = outr.iter().filter(|r| r[7] != "PGX_REFBLOCK").collect(); + assert_eq!( + passed, kept, + "every other record passes unchanged and in order" + ); + assert!( + at.contains_key(&("chr2".to_owned(), 233_760_234)) + && at.contains_key(&("chr2".to_owned(), 233_760_235)), + "the bases after a filled position inside its span stay (chr2:233760234-233760235)" + ); + let order = ["chr1", "chr2", "chr6", "chr10", "chr19", "chrX"]; + let keys: Vec<(usize, u64)> = outr + .iter() + .map(|r| { + ( + order.iter().position(|c| *c == r[0]).unwrap(), + r[1].parse().unwrap(), + ) + }) + .collect(); + let mut sorted = keys.clone(); + sorted.sort_unstable(); + assert_eq!(keys, sorted, "the output is sorted"); + let head: Vec<&str> = out.lines().filter(|l| l.starts_with('#')).collect(); + assert!( + head.len() >= 2 + && head[head.len() - 1].starts_with("#CHROM") + && head[head.len() - 2].starts_with("##INFO=>()), + Some(vec!["REFBLOCKS"]), + "the sample column stays" + ); +} + +#[test] +fn the_gq_threshold() { + let g = gvcf(); + let o = ok(&g, &["--min-gq", "50"]); + assert!( + filled(&o, UGT1A1), + "--min-gq 50: UGT1A1 (GQ 50) still filled, a block at exactly the threshold counts" + ); + assert!( + !filled(&o, ("chr1", 97_450_065)), + "--min-gq 50: DPYD (one block GQ 45) no longer filled" + ); + let o = ok(&g, &["--min-gq", "51"]); + assert!(fills(&o).is_empty(), "--min-gq 51: nothing filled"); + // the first GQ-50 block of a filled position: UGT1A1 + let low = g.replacen("0/0:50:30\n", "0/0:19:30\n", 1); + let o = ok(&low, &[]); + assert!( + !filled(&o, UGT1A1), + "default threshold 20: UGT1A1 with a GQ 19 block is not filled" + ); +} + +#[test] +fn edges_that_fill_nothing_and_inputs_that_are_refused() { + let g = gvcf(); + let hole = g.replace("./.:0:0", "./.:50:0"); + let o = ok(&hole, &[]); + assert!( + hole != g && !filled(&o, ("chr10", 94_942_205)), + "the ./. hole stays a hole when its GQ is 50 (the genotype decides, not only the GQ)" + ); + // A deletion that starts before the span and reaches into it, with a + // block over the deleted bases after it (as GATK writes them): the + // block covers the span, the deletion still overlaps it. + let dele = g.replace( + "chr2\t233759900\t.\tT\t<*>\t0\t.\tEND=233761000\tGT:GQ:MIN_DP\t0/0:50:30\n", + "chr2\t233759900\t.\tT\t<*>\t0\t.\tEND=233760230\tGT:GQ:MIN_DP\t0/0:50:30\n\ + chr2\t233760231\t.\tGCCA\tG,<*>\t30\tPASS\t.\tGT:GQ:DP\t0/1:30:28\n\ + chr2\t233760232\t.\tC\t<*>\t0\t.\tEND=233761000\tGT:GQ:MIN_DP\t0/0:50:30\n", + ); + let o = ok(&dele, &[]); + assert!( + dele != g && !filled(&o, UGT1A1), + "a deletion from before the span that overlaps it fills nothing, though blocks cover the span" + ); + let short = g.replace("END=233761000", "END=233760234"); + let o = ok(&short, &[]); + assert!( + !filled(&o, UGT1A1), + "a block ending one base before the span ends fills nothing" + ); + let lowq = g.replace( + "T\t<*>\t0\t.\tEND=233761000", + "T\t<*>\t0\tLowQual\tEND=233761000", + ); + let o = ok(&lowq, &[]); + assert!( + lowq != g && !filled(&o, UGT1A1), + "a block with a failed FILTER fills nothing" + ); + let two = g.replace("\tREFBLOCKS\n", "\tREFBLOCKS\tOTHER\n"); + let (rc, _, e) = run(&two, &[], None); + assert!( + rc == 2 && e.contains("one sample"), + "a gVCF with two samples: exit 2: {rc} {e}" + ); + let nohead: String = g + .split_inclusive('\n') + .filter(|l| !l.starts_with("#CHROM")) + .collect(); + let (rc, _, e) = run(&nohead, &[], Some(&expand(&g))); + assert!( + rc == 2 && e.contains("#CHROM"), + "a gVCF without its #CHROM line: exit 2: {rc} {e}" + ); +} diff --git a/rust/crates/og-pgx/tests/fixtures.rs b/rust/crates/og-pgx/tests/fixtures.rs index 434d2c28f..dfc3a2ec2 100644 --- a/rust/crates/og-pgx/tests/fixtures.rs +++ b/rust/crates/og-pgx/tests/fixtures.rs @@ -1,6 +1,5 @@ //! The PharmCAT reports of `tests/fixtures/pharmcat`, read by the library: -//! the checks of `tests/test_cpic_parser.py` that need more than the -//! command's output. +//! the checks that need more than the command's output. use og_pgx::guidance::drug_guidance; use og_pgx::report::parse_genes; diff --git a/rust/crates/og-pgx/tests/port_tests_pgx_cpic.rs b/rust/crates/og-pgx/tests/port_tests_pgx_cpic.rs new file mode 100644 index 000000000..08684f735 --- /dev/null +++ b/rust/crates/og-pgx/tests/port_tests_pgx_cpic.rs @@ -0,0 +1,2309 @@ +//! `og pgx cpic-report` and `og pgx step36-outside`, the one PharmCAT +//! reader (step 27 and the CPIC_LOOKUP module run the same verbs), run +//! through [`og_pgx::main`] as the `og` binary runs them. The +//! function-level rules (the phenotype classes, the layouts, the activity +//! score bands) are this crate's unit tests; these run the verbs. +//! +//! 1. The real PharmCAT `report.json` of the HG002 fixture, from each +//! PharmCAT release in [`REAL_REPORTS`] +//! (`tests/fixtures/pharmcat/report-.json`), parses to genes; a +//! gene PharmCAT lists several possible diplotypes for, with different +//! phenotypes (CYP2C19, 528 of them), is 'ambiguous' and never read as +//! its first diplotype; a report that parses to zero genes exits 1 with a +//! PARSING FAILED report, never an all-clear. +//! 2. On PharmCAT's own example report exactly 3 genes count as non-normal +//! (CYP2D6, HLA-B, NAT2); the 7 whose phenotype is n/a, 'no phenotype +//! assigned', a genotype or 'Uncertain Susceptibility' are +//! 'unclassified', listed on their own and given no drug list; every +//! non-normal gene gets a drug list, with the recommendation for its own +//! diplotype; synthetic genes check the fallbacks: the report's `drugs` +//! section first, then `relatedDrugs`, then the static table, and +//! otherwise a line saying it is not in the drug table. +//! 3. The flat (3.x), nested (2.x) and list layouts all parse. +//! 4. A gene PharmCAT could not call (or could not resolve) while pypgx did +//! prints a warning naming every drug PharmCAT links to the gene, or a +//! note when pypgx's call is normal, and the comparison marks it 'pypgx +//! only'; an unreadable pypgx summary is reported, not fatal, and makes +//! no comparison. +//! 5. A gene PharmCAT leaves 'Indeterminate' at gene level while its drug +//! annotations give it one phenotype shows that phenotype, with a note, +//! and takes its status; with no annotation, two that disagree, one that +//! is Indeterminate itself, or possible diplotypes whose guidance +//! differs, it stays Indeterminate and unclassified. +//! 6. A single-position gene PharmCAT labels 'rsX reference (C)/rsX variant +//! (T)' matches pypgx's 'Reference/rsX', and a real difference still does +//! not; the comparison carries both tools' phenotypes; a CPIC +//! recommendation over 200 characters prints whole; CYP4F2 (and VKORC1) +//! with no PharmCAT result takes the genotype PharmCAT read at the *3 +//! (-1639) position as a labelled single-position fallback, and a +//! position with no genotype is 'not callable'. +//! 7. An MT-RNR1 outside call is not checked against step 36's consensus +//! table, which never lists it, while an unconfirmed CYP2D6 outside call +//! is still held back; `step36-outside` lists the step 36 genes the +//! report calls from an outside call and never MT-RNR1. +//! 8. Phenoconversion (opt-in `--medications` with +//! `assets/pgx_inhibitors.tsv`): the table's shape, a strong and a +//! moderate CYP2D6 inhibitor, CYP2C19, CYP2C9 and CYP3A notes, a name not +//! in the table, an empty list, and the control: the inhibitor's factor +//! set to 1 must not give a poor metabolizer. +//! 9. Disease-variant contraindications (`--contraindications` with +//! `assets/pgx_contraindications.tsv` and step 06's `--clinvar-hits`): +//! the table's shape, a pathogenic POLG hit and the hits that add +//! nothing, no or an unreadable hits table, and the control: the table +//! without its POLG row gives no valproic acid line for the same hit. + +use std::fs; +use std::io::Write; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +use pycompat::json::{Dump, Value, dumps, loads}; + +/// One real HG002 report per PharmCAT release the reader is tested on: the +/// version, and the genes it lists more than one possible diplotype for, +/// with how many. +const REAL_REPORTS: [(&str, [(&str, usize); 2]); 2] = [ + ("3.2.0", [("CYP2B6", 4), ("CYP2C19", 528)]), + ("3.4.0", [("CYP2B6", 4), ("CYP2C19", 528)]), +]; +const PHENOCONVERSION_GENES: [&str; 5] = ["CYP2D6", "CYP2C19", "CYP2C9", "CYP3A4", "CYP3A5"]; +const CONTRA_COLUMNS: [&str; 8] = [ + "Gene", + "Drug", + "Names", + "CPIC_level", + "Note", + "Pair_source", + "Label_source", + "Report_if", +]; +const FDA_TABLE: &str = "https://www.fda.gov/drugs/drug-interactions-labeling/\ + healthcare-professionals-fdas-examples-drugs-interact-cyp-enzymes-and-transporter-systems"; + +fn repo() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../..") +} + +fn repo_path(rel: &str) -> String { + repo().join(rel).to_string_lossy().into_owned() +} + +fn inhibitors() -> String { + repo_path("assets/pgx_inhibitors.tsv") +} + +fn contra() -> String { + repo_path("assets/pgx_contraindications.tsv") +} + +fn fixture(name: &str) -> Value { + loads(&fs::read_to_string(repo().join("tests/fixtures/pharmcat").join(name)).unwrap()).unwrap() +} + +/// A scratch directory of one test: emptied when made, removed when +/// dropped. +struct Scratch(PathBuf); + +impl Scratch { + fn new(name: &str) -> Self { + let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("pgx-cpic-{name}")); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + Self(dir) + } + + fn path(&self, name: &str) -> String { + self.0.join(name).to_string_lossy().into_owned() + } + + fn file(&self, name: &str, text: &str) -> String { + let p = self.path(name); + fs::write(&p, text).unwrap(); + p + } +} + +impl Drop for Scratch { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } +} + +// --- JSON built as Python's dicts are ----------------------------------------- + +fn s(x: &str) -> Value { + Value::Str(x.to_owned()) +} + +fn o(pairs: Vec<(&str, Value)>) -> Value { + Value::Object(pairs.into_iter().map(|(k, v)| (k.to_owned(), v)).collect()) +} + +fn a(items: Vec) -> Value { + Value::Array(items) +} + +/// `dict(v, key=val)`. +fn with(mut v: Value, key: &str, val: Value) -> Value { + if let Value::Object(m) = &mut v { + m.insert(key.to_owned(), val); + } + v +} + +fn at<'a>(v: &'a mut Value, key: &str) -> &'a mut Value { + match v { + Value::Object(m) => m.get_mut(key).unwrap(), + other => panic!("not an object: {other:?}"), + } +} + +fn dip(a1: &str, a2: &str, phen: &str) -> Value { + o(vec![( + "sourceDiplotypes", + a(vec![o(vec![ + ("allele1", o(vec![("name", s(a1))])), + ("allele2", o(vec![("name", s(a2))])), + ("label", s(&format!("{a1}/{a2}"))), + ("phenotypes", a(vec![s(phen)])), + ])]), + )]) +} + +fn genes(pairs: Vec<(&str, Value)>) -> Value { + o(vec![("genes", o(pairs))]) +} + +/// `{"CPIC Guideline Annotation": {drug: {"name": drug, "guidelines": +/// [{"annotations": anns}]}}}`. +fn cpic_drugs(drug: &str, anns: Vec) -> Value { + o(vec![( + "CPIC Guideline Annotation", + o(vec![( + drug, + o(vec![ + ("name", s(drug)), + ("guidelines", a(vec![o(vec![("annotations", a(anns))])])), + ]), + )]), + )]) +} + +// --- one run of the verb -------------------------------------------------------- + +#[derive(Default)] +struct Opts<'a> { + pypgx: Option>, + consensus: Option>, + medications: Option<&'a str>, + inhibitors: Option, + contraindications: Option, + hits: Option, +} + +/// What one run wrote: exit code, recommendations text, phenotypes rows, +/// comparison rows. +struct Report { + rc: u8, + rec: String, + rows: Vec>, + comp: Vec>, +} + +fn og(args: &[&str]) -> u8 { + let args: Vec = args.iter().map(|s| (*s).to_owned()).collect(); + og_pgx::main(&args) +} + +fn rows_of(path: &str) -> Vec> { + fs::read_to_string(path) + .map(|t| { + t.lines() + .skip(1) + .map(|l| l.split('\t').map(str::to_owned).collect()) + .collect() + }) + .unwrap_or_default() +} + +fn run_report(work: &Scratch, name: &str, data: &Value, o: &Opts) -> Report { + let d = work.0.join(name); + fs::create_dir_all(&d).unwrap(); + let p = |n: &str| d.join(n).to_string_lossy().into_owned(); + let rep = p("report.json"); + fs::write(&rep, dumps(data, &Dump::default()).unwrap()).unwrap(); + let mut args: Vec = [ + "cpic-report", + "--sample", + name, + "--report", + &rep, + "--outdir", + ] + .map(str::to_owned) + .to_vec(); + args.push(d.to_string_lossy().into_owned()); + let mut opt = |k: &str, v: String| { + args.push(k.to_owned()); + args.push(v); + }; + if let Some(rows) = &o.consensus { + let mut text = "Gene\tResult\tOutside_call\tReason\tEvidence\n".to_owned(); + for (g, res, out) in rows { + text.push_str(&format!("{g}\t{res}\t{out}\ttest\ttest\n")); + } + fs::write(p("consensus.tsv"), text).unwrap(); + opt("--consensus", p("consensus.tsv")); + } + if let Some(m) = o.medications { + fs::write(p("medications.txt"), m).unwrap(); + opt("--medications", p("medications.txt")); + opt( + "--inhibitors", + o.inhibitors.clone().unwrap_or_else(inhibitors), + ); + } + if let Some(c) = &o.contraindications { + opt("--contraindications", c.clone()); + } + if let Some(h) = &o.hits { + fs::write(p("clinvar_hits.tsv"), h).unwrap(); + opt("--clinvar-hits", p("clinvar_hits.tsv")); + } + let comp = p("comparison.tsv"); + if let Some(rows) = &o.pypgx { + let mut text = "Gene\tDiplotype\tPhenotype\tCNV_call\tSource\n".to_owned(); + for (g, dl, ph) in rows { + text.push_str(&format!("{g}\t{dl}\t{ph}\t.\tbam\n")); + } + fs::write(p("pypgx.tsv"), text).unwrap(); + opt("--pypgx", p("pypgx.tsv")); + opt("--comparison", comp.clone()); + } + let rc = og_pgx::main(&args); + Report { + rc, + rec: fs::read_to_string(p(&format!("{name}_cpic_recommendations.txt"))).unwrap(), + rows: rows_of(&p(&format!("{name}_phenotypes.tsv"))), + comp: rows_of(&comp), + } +} + +fn plain(work: &Scratch, name: &str, data: &Value) -> Report { + run_report(work, name, data, &Opts::default()) +} + +fn row<'a>(rows: &'a [Vec], gene: &str) -> Option<&'a Vec> { + rows.iter().find(|r| r[0] == gene) +} + +fn cell(rows: &[Vec], gene: &str, i: usize) -> String { + row(rows, gene) + .and_then(|r| r.get(i).cloned()) + .unwrap_or_default() +} + +fn strs(v: &[&str]) -> Vec { + v.iter().map(|s| (*s).to_owned()).collect() +} + +/// The medications block of one gene in the recommendations text. +fn block(rec: &str, gene: &str) -> String { + let mut lines = Vec::new(); + let mut on = false; + for line in rec.lines() { + if line.starts_with(&format!(" {gene} -- ")) { + on = true; + } + if on { + lines.push(line); + if line.contains("Action:") { + break; + } + } + } + lines.join("\n") +} + +/// The text after the first `start` and before the first `end` after it. +fn between<'a>(text: &'a str, start: &str, end: &str) -> &'a str { + match text.split_once(start) { + Some((_, rest)) => rest.split(end).next().unwrap_or_default(), + None => "", + } +} + +/// The Affected Medications section of a recommendations text (the whole +/// text when it has none). +fn medications(text: &str) -> &str { + let rest = text + .split_once("Affected Medications:") + .map_or(text, |(_, r)| r); + rest.split("Calls From Other Tools") + .next() + .unwrap_or_default() +} + +fn genes_named(rows: &[Vec], status: &str) -> Vec { + let mut g: Vec = rows + .iter() + .filter(|r| r[3] == status) + .map(|r| r[0].clone()) + .collect(); + g.sort(); + g +} + +// --- 1. the real reports, one per PharmCAT release ------------------------------- + +#[test] +fn real_hg002_reports_parse_and_ambiguous_genes_stay_ambiguous() { + let work = Scratch::new("real"); + for (version, ambiguous) in REAL_REPORTS { + let real = fixture(&format!("report-{version}.json")); + let tag = format!("{version}:"); + let r = plain(&work, &format!("HG002_{version}"), &real); + let Value::Object(m) = &real else { + panic!("not an object") + }; + assert_eq!( + m.get("pharmcatVersion"), + Some(&s(version)), + "{tag} the report is PharmCAT {version}" + ); + assert_eq!(r.rc, 0, "{tag} cpic-report on the HG002 report exits 0"); + assert!( + r.rows.len() >= 10, + "{tag} the HG002 report yields genes: {}", + r.rows.len() + ); + assert!( + r.rows.iter().any(|r| r[3] != "not called"), + "{tag} the HG002 report calls at least one gene" + ); + let mut names: Vec<&str> = r.rows.iter().map(|r| r[0].as_str()).collect(); + names.sort_unstable(); + names.dedup(); + assert!( + names.len() == r.rows.len() && r.rows.iter().all(|r| r.len() == 4), + "{tag} one phenotypes row per gene, with a Status" + ); + let mut several: Vec<(String, usize)> = r + .rows + .iter() + .filter(|r| r[1].contains(" possible)")) + .map(|r| { + let (_, n) = r[1].rsplit_once("(1 of ").unwrap(); + ( + r[0].clone(), + n.split_whitespace().next().unwrap().parse().unwrap(), + ) + }) + .collect(); + several.sort(); + let want: Vec<(String, usize)> = ambiguous + .iter() + .map(|(g, n)| ((*g).to_owned(), *n)) + .collect(); + assert_eq!( + several, want, + "{tag} the genes with more than one possible diplotype are CYP2B6, CYP2C19" + ); + for (gene, n) in ambiguous { + let g = row(&r.rows, gene); + assert_eq!( + g.map(|g| g[3].as_str()), + Some("ambiguous"), + "{tag} HG002 {gene}: {n} possible diplotypes, status ambiguous" + ); + assert!( + g.is_some_and(|g| g[2].starts_with("ambiguous: ") + && g[1].contains(&format!("(1 of {n} possible)"))), + "{tag} HG002 {gene}: not reported as its first diplotype's phenotype: {g:?}" + ); + assert!( + r.rec.contains("Genes With More Than One Possible Result:") + && r.rec + .contains(&format!(" {gene} -- {n} possible diplotypes")), + "{tag} the recommendations list {gene} with the ambiguous genes" + ); + } + } +} + +#[test] +fn a_report_with_no_gene_fails_loud() { + let work = Scratch::new("empty"); + let real = fixture("report-3.4.0.json"); + let names: Vec = match &real { + Value::Object(m) => match m.get("genes") { + Some(Value::Object(g)) => g.keys().cloned().collect(), + _ => panic!("no genes"), + }, + _ => panic!("not an object"), + }; + let stripped = with( + real, + "genes", + Value::Object( + names + .iter() + .map(|g| (g.clone(), o(vec![("geneSymbol", s(g))]))) + .collect(), + ), + ); + let r = plain(&work, "EMPTY", &stripped); + assert_eq!(r.rc, 1, "a report whose genes carry no diplotype exits 1"); + assert!( + r.rec.contains("PARSING FAILED") + && !r.rec.contains("all genes were successfully called") + && r.rows.is_empty(), + "and writes PARSING FAILED, never an all-clear" + ); + let r = plain(&work, "NOKEYS", &o(vec![("metadata", o(vec![]))])); + assert!( + r.rc == 1 && r.rec.contains("PARSING FAILED"), + "a report with no gene section at all exits 1" + ); +} + +// --- 2. PharmCAT's example report and the drug-list fallbacks ----------------------- + +const UNCLASSIFIED: [&str; 7] = [ + "CACNA1S", "CFTR", "CYP4F2", "IFNL3", "MT-RNR1", "RYR1", "VKORC1", +]; + +#[test] +fn the_example_report_has_three_non_normal_and_seven_unclassified_genes() { + let work = Scratch::new("example"); + let example = fixture("pharmcat-docs-example.json"); + let r = plain(&work, "EXAMPLE", &example); + assert_eq!(r.rc, 0, "cpic-report on the example report exits 0"); + assert_eq!( + genes_named(&r.rows, "non-normal"), + ["CYP2D6", "HLA-B", "NAT2"], + "example report: 3 genes with a non-normal phenotype (CYP2D6, HLA-B, NAT2)" + ); + assert_eq!( + genes_named(&r.rows, "unclassified"), + UNCLASSIFIED, + "example report: n/a, 'no phenotype assigned', a genotype and 'Uncertain Susceptibility' are unclassified" + ); + let after = r.rec.split("Affected Medications:").nth(1).unwrap(); + let meds = after + .split_once("\n\n") + .unwrap() + .1 + .split("Called Genes Without") + .next() + .unwrap(); + let mut listed: Vec<&str> = meds + .lines() + .filter(|l| l.starts_with(" ") && l.contains(" -- ") && !l.starts_with(" ")) + .map(|l| l.split(" -- ").next().unwrap().trim()) + .collect(); + listed.sort_unstable(); + assert_eq!( + listed, + ["CYP2D6", "HLA-B", "NAT2"], + "example: the medications section lists only the 3 non-normal genes" + ); + let own = r + .rec + .rsplit("Called Genes Without a Function Phenotype:") + .next() + .unwrap() + .split("Uncallable Genes:") + .next() + .unwrap(); + assert!( + UNCLASSIFIED + .iter() + .all(|g| own.contains(&format!(" {g} -- "))) + && own.contains(" VKORC1 -- -1639 GG ("), + "example: the unclassified genes are listed in their own section, VKORC1 with its genotype" + ); + assert_eq!( + genes_named(&r.rows, "unclassified"), + UNCLASSIFIED, + "example: the phenotypes table says unclassified for them" + ); + for gene in ["CYP2D6", "HLA-B", "NAT2"] { + let b = block(&r.rec, gene); + assert!( + b.contains(" Drugs") && !b.contains("not in the drug table"), + "example {gene}: a drug list: {b}" + ); + } + assert!( + block(&r.rec, "CYP2D6").contains( + "- codeine [CPIC, Moderate]: Use codeine label recommended age- or weight-specific dosing. \ + If no response and opioid use is warranted, consider a non-tramadol opioid." + ), + "example CYP2D6 *1/*3 (Intermediate): codeine's recommendation for that diplotype" + ); + + let mut ga = example; + let first = match at(at(at(&mut ga, "genes"), "VKORC1"), "sourceDiplotypes") { + Value::Array(v) => &mut v[0], + other => panic!("{other:?}"), + }; + *at(first, "label") = s("rs9923231 reference (C)/rs9923231 variant (T)"); + *at(first, "phenotypes") = a(vec![s("-1639 GA")]); + let r = plain(&work, "EXAMPLE_GA", &ga); + assert_eq!( + cell(&r.rows, "VKORC1", 3), + "non-normal", + "example with VKORC1 -1639 GA: VKORC1 counts as non-normal" + ); + let b = block(&r.rec, "VKORC1"); + assert!( + r.rc == 0 && b.contains(" Drugs") && b.contains("warfarin"), + "example with VKORC1 -1639 GA: its drug list names warfarin: {b}" + ); +} + +#[test] +fn drug_list_fallbacks() { + let work = Scratch::new("fallback"); + let data = with( + genes(vec![ + ("CYP2C19", dip("*2", "*2", "Poor Metabolizer")), + ( + "ABCG2", + with( + dip( + "rs2231142 variant (T)", + "rs2231142 variant (T)", + "Poor Function", + ), + "relatedDrugs", + a(vec![o(vec![("name", s("rosuvastatin"))])]), + ), + ), + ("NEWGENE", dip("*1", "*7", "Decreased Function")), + ("CYP4F2", dip("*1", "*3", "Decreased Function")), + ("SLCO1B1", dip("*1", "*1", "Normal Function")), + ]), + "drugs", + cpic_drugs( + "warfarin", + vec![o(vec![ + ("phenotypes", o(vec![("CYP4F2", s("Decreased Function"))])), + ("classification", s("Optional")), + ("drugRecommendation", s("Increase the dose by 5-10%.")), + ])], + ), + ); + let r = plain(&work, "FALLBACK", &data); + assert_eq!(r.rc, 0, "fallbacks: exits 0"); + assert!( + block(&r.rec, "CYP4F2") + .contains("- warfarin [CPIC, Optional]: Increase the dose by 5-10%."), + "CYP4F2: the report's own guidance (warfarin, CPIC Optional)" + ); + let b = block(&r.rec, "ABCG2"); + assert!( + b.contains("Drugs PharmCAT links to ABCG2") && b.contains("rosuvastatin"), + "ABCG2: relatedDrugs when the report has no guidance for it" + ); + let b = block(&r.rec, "CYP2C19"); + assert!( + b.contains("pipeline fallback table") && b.contains("clopidogrel"), + "CYP2C19: the static table when the report names no drug" + ); + assert!( + block(&r.rec, "NEWGENE") + .contains("NEWGENE has a non-normal phenotype but is not in the drug table"), + "NEWGENE: a line saying it is not in the drug table, not silence" + ); + assert_eq!( + block(&r.rec, "SLCO1B1"), + "", + "SLCO1B1 normal: no medications block" + ); +} + +/// PharmCAT lists an annotation for every diplotype the sample may have: +/// the recommendation shown must be the one for the called diplotype. +#[test] +fn the_recommendation_is_the_called_diplotypes() { + let work = Scratch::new("match"); + let ann = |label: &str, phen: &str, text: &str| { + o(vec![ + ("phenotypes", o(vec![("CYP2C19", s(phen))])), + ("classification", s("Strong")), + ("drugRecommendation", s(text)), + ( + "genotypes", + a(vec![o(vec![( + "diplotypes", + a(vec![o(vec![("gene", s("CYP2C19")), ("label", s(label))])]), + )])]), + ), + ]) + }; + let data = with( + genes(vec![( + "CYP2C19", + dip("*1", "*2", "Intermediate Metabolizer"), + )]), + "drugs", + cpic_drugs( + "clopidogrel", + vec![ + ann("*1/*1", "Normal Metabolizer", "Standard dose."), + ann( + "*1/*2", + "Intermediate Metabolizer", + "Use an alternative antiplatelet.", + ), + ann("*2/*2", "Poor Metabolizer", "Avoid clopidogrel."), + ], + ), + ); + let r = plain(&work, "MATCH", &data); + let b = block(&r.rec, "CYP2C19"); + assert!( + b.contains("- clopidogrel [CPIC, Strong]: Use an alternative antiplatelet.") + && !b.contains("Standard dose.") + && !b.contains("Avoid clopidogrel."), + "the recommendation is the called diplotype's (*1/*2), not another possible one's: {b}" + ); + + // The phenotype fallback (annotations naming no diplotype) matches the + // called phenotype exactly. + let pann = |phen: &str, text: &str| { + o(vec![ + ("phenotypes", o(vec![("CYP2C19", s(phen))])), + ("classification", s("Strong")), + ("drugRecommendation", s(text)), + ]) + }; + let data = with( + genes(vec![( + "CYP2C19", + dip("*1", "*9", "Likely Intermediate Metabolizer"), + )]), + "drugs", + cpic_drugs( + "clopidogrel", + vec![ + pann("Intermediate Metabolizer", "Guidance for IM."), + pann("Likely Intermediate Metabolizer", "Guidance for likely IM."), + ], + ), + ); + let r = plain(&work, "PHENMATCH", &data); + let b = block(&r.rec, "CYP2C19"); + assert!( + b.contains("Guidance for likely IM.") && !b.contains("Guidance for IM."), + "phenotype fallback: the exact phenotype's guidance only, not a substring match: {b}" + ); +} + +// --- 3. layouts ---------------------------------------------------------------- + +#[test] +fn flat_nested_list_and_recommendation_layouts_parse() { + let work = Scratch::new("layouts"); + let g2 = || { + o(vec![ + ("CYP2C19", dip("*1", "*2", "Intermediate Metabolizer")), + ("CYP2D6", dip("*1", "*1", "Normal Metabolizer")), + ]) + }; + let cases = [ + ("flat 3.x", o(vec![("genes", g2())]), 2), + ( + "nested 2.x", + o(vec![("genes", o(vec![("CPIC", g2()), ("DPWG", g2())]))]), + 2, + ), + ( + "list", + o(vec![( + "genes", + a(vec![with( + dip("*1", "*2", "Intermediate Metabolizer"), + "gene", + s("CYP2C19"), + )]), + )]), + 1, + ), + ( + "recommendationDiplotypes", + genes(vec![( + "DPYD", + o(vec![( + "recommendationDiplotypes", + a(vec![o(vec![ + ("label", s("c.1905+1G>A/Reference")), + ("phenotypes", a(vec![s("Intermediate Metabolizer")])), + ])]), + )]), + )]), + 1, + ), + ]; + for (label, d, want) in cases { + let r = plain(&work, &format!("LAYOUT{}", label.len()), &d); + assert!( + r.rc == 0 && r.rows.len() == want, + "layout {label}: {want} gene(s): {} {:?}", + r.rc, + r.rows + ); + } +} + +// --- 4. PharmCAT no result, a pypgx call ------------------------------------------- + +#[test] +fn pypgx_call_where_pharmcat_has_no_result() { + let work = Scratch::new("pypgx"); + let data = with( + genes(vec![ + ( + "CYP2D6", + with( + dip("Unknown", "Unknown", "No Result"), + "relatedDrugs", + a(vec![ + o(vec![("name", s("codeine"))]), + o(vec![("name", s("tramadol"))]), + ]), + ), + ), + ("CYP2C9", dip("Unknown", "Unknown", "No Result")), + ("CYP2C19", dip("*1", "*1", "Normal Metabolizer")), + ]), + // a two-gene annotation that names CYP2D6 for one drug only + "drugs", + cpic_drugs( + "amitriptyline", + vec![o(vec![ + ( + "phenotypes", + o(vec![ + ("CYP2C19", s("Normal Metabolizer")), + ("CYP2D6", s("No Result")), + ]), + ), + ("drugRecommendation", s("x")), + ("classification", s("Optional")), + ])], + ), + ); + let r = run_report( + &work, + "PYPGX", + &data, + &Opts { + pypgx: Some(vec![ + ("CYP2D6", "*1/*4", "Intermediate Metabolizer"), + ("CYP2C9", "*1/*1", "Normal Metabolizer"), + ("CYP2C19", "*1/*1", "Normal Metabolizer"), + ]), + ..Opts::default() + }, + ); + assert_eq!(r.rc, 0, "pypgx: exits 0"); + assert!( + r.rec.contains( + "WARNING: PharmCAT has no result for CYP2D6, but pypgx (step 32) called *1/*4" + ), + "pypgx: the warning names CYP2D6 and pypgx's call" + ); + assert!( + r.rec + .contains("Drugs affected by CYP2D6: codeine, tramadol"), + "pypgx: the warning lists every drug PharmCAT links to CYP2D6, not only the one another gene matched" + ); + assert!( + r.rec.contains( + "NOTE: PharmCAT has no result for CYP2C9; pypgx (step 32) called *1/*1 (Normal Metabolizer)." + ) && !r.rec.contains("WARNING: PharmCAT has no result for CYP2C9"), + "pypgx: a normal pypgx call is a NOTE, not a WARNING" + ); + assert!( + r.rec.contains("CYP2D6 -- No Result (not callable"), + "pypgx: CYP2D6 is in the uncallable list" + ); + assert!( + r.comp.contains(&strs(&[ + "CYP2D6", + "Unknown/Unknown", + "*1/*4", + "pypgx only", + "pypgx only", + "No Result", + "Intermediate Metabolizer" + ])) && r.comp.contains(&strs(&[ + "CYP2C19", + "*1/*1", + "*1/*1", + "Yes", + "both", + "Normal Metabolizer", + "Normal Metabolizer" + ])), + "pypgx: comparison marks CYP2D6 'pypgx only' and CYP2C19 concordant: {:?}", + r.comp + ); +} + +/// An ambiguous PharmCAT gene is not compared as if it were its first +/// diplotype. +#[test] +fn an_ambiguous_gene_is_compared_as_ambiguous() { + let work = Scratch::new("ambig"); + let one = |a2: &str, phen: &str| { + o(vec![ + ("allele1", o(vec![("name", s("*1"))])), + ("allele2", o(vec![("name", s(a2))])), + ("label", s(&format!("*1/{a2}"))), + ("phenotypes", a(vec![s(phen)])), + ]) + }; + let data = genes(vec![( + "CYP2C19", + o(vec![( + "sourceDiplotypes", + a(vec![ + one("*1", "Normal Metabolizer"), + one("*2", "Intermediate Metabolizer"), + ]), + )]), + )]); + let r = run_report( + &work, + "AMBIG", + &data, + &Opts { + pypgx: Some(vec![("CYP2C19", "*1/*2", "Intermediate Metabolizer")]), + ..Opts::default() + }, + ); + assert_eq!( + r.rows.first().map(|r| r[3].as_str()), + Some("ambiguous"), + "ambiguous: the phenotypes row says ambiguous" + ); + assert!( + r.comp + .first() + .is_some_and(|c| c[1] == "ambiguous (2 possible diplotypes)" && c[3] == "pypgx only") + && r.rec.contains( + "WARNING: PharmCAT has no single result (2 possible diplotypes) for CYP2C19" + ), + "ambiguous: compared as ambiguous, pypgx's call warned about: {:?}", + r.comp + ); +} + +/// An unreadable pypgx summary: the CPIC report is still written, it says +/// the comparison could not be made, and no comparison file is written. +#[test] +fn an_unreadable_pypgx_summary_is_reported_not_fatal() { + let work = Scratch::new("badpypgx"); + let rep = work.file( + "report.json", + &dumps( + &genes(vec![("CYP2C19", dip("*2", "*2", "Poor Metabolizer"))]), + &Dump::default(), + ) + .unwrap(), + ); + let bad = work.path("pypgx.tsv"); + fs::write(&bad, b"Gene\tDiplotype\n\xff\xfe\x00broken\n").unwrap(); + // a comparison left by an earlier run + let comp = work.file( + "comparison.tsv", + "Gene\tPharmCAT_diplotype\tpypgx_diplotype\tMatch\tCalled_by\nCYP2C19\t*1/*1\t*1/*1\tYes\tboth\n", + ); + let outdir = work.0.to_string_lossy().into_owned(); + let rc = og(&[ + "cpic-report", + "--sample", + "BADPYPGX", + "--report", + &rep, + "--outdir", + &outdir, + "--pypgx", + &bad, + "--comparison", + &comp, + ]); + let rec = + fs::read_to_string(work.path("BADPYPGX_cpic_recommendations.txt")).unwrap_or_default(); + assert!( + rc == 0 && rec.contains("CYP2C19 -- Poor"), + "unreadable pypgx summary: exits 0 with the CPIC report written: {rc}" + ); + assert!( + rec.contains("Could not read the pypgx summary") && !Path::new(&comp).exists(), + "unreadable pypgx summary: the report says no comparison was made, and an earlier one is removed" + ); +} + +// --- 5. Indeterminate at gene level, a phenotype in the drug annotations ------------ + +const INDET_NOTE: &str = "[gene level Indeterminate; phenotype from PharmCAT's drug annotations]"; + +fn iann(phen: &str, label: &str, rec: Option<&str>) -> Value { + o(vec![ + ("phenotypes", o(vec![("UGT1A1", s(phen))])), + ("classification", s("Strong")), + ( + "drugRecommendation", + s(&rec.map_or_else(|| format!("Guidance for {phen}."), str::to_owned)), + ), + ( + "genotypes", + a(vec![o(vec![( + "diplotypes", + a(vec![o(vec![("gene", s("UGT1A1")), ("label", s(label))])]), + )])]), + ), + ]) +} + +fn ugt1a1_report(gene: Value, anns: Option>) -> Value { + let g = genes(vec![("UGT1A1", gene)]); + match anns { + Some(anns) => with(g, "drugs", cpic_drugs("atazanavir", anns)), + None => g, + } +} + +#[test] +fn an_indeterminate_gene_takes_one_drug_level_phenotype() { + let work = Scratch::new("indet"); + let gene = || { + with( + dip("*1", "*80", "Indeterminate"), + "relatedDrugs", + a(vec![o(vec![("name", s("atazanavir"))])]), + ) + }; + let r = plain( + &work, + "INDET", + &ugt1a1_report( + gene(), + Some(vec![iann("Intermediate Metabolizer", "*1/*80", None)]), + ), + ); + assert_eq!( + r.rc, 0, + "Indeterminate gene, one drug-level phenotype: exits 0" + ); + let note = format!("Intermediate Metabolizer {INDET_NOTE}"); + assert_eq!( + r.rows, + [strs(&["UGT1A1", "*1/*80", ¬e, "non-normal"])], + "Indeterminate gene: the table shows the drug-level phenotype with its note, and its status" + ); + assert!( + r.rec + .split("Affected Medications:") + .next() + .unwrap() + .contains(¬e), + "Indeterminate gene: the gene results show the note" + ); + assert!( + r.rec.contains(&format!(" UGT1A1 -- {note}:")) + && block(&r.rec, "UGT1A1") + .contains("- atazanavir [CPIC, Strong]: Guidance for Intermediate Metabolizer."), + "Indeterminate gene: listed with its drugs, the guidance for the called diplotype" + ); + assert!( + !r.rec.contains("Called Genes Without a Function Phenotype:"), + "Indeterminate gene: not in the section of genes without a function phenotype" + ); + let cases: [(&str, Option>); 4] = [ + ("no drug annotation", None), + ( + "two drug annotations that disagree", + Some(vec![ + iann("Intermediate Metabolizer", "*1/*80", None), + iann("Poor Metabolizer", "*1/*80", None), + ]), + ), + ( + "a drug annotation that is Indeterminate too", + Some(vec![iann("Indeterminate", "*1/*80", None)]), + ), + ( + "one drug annotation with a phenotype and one Indeterminate", + Some(vec![ + iann("Intermediate Metabolizer", "*1/*80", None), + iann("Indeterminate", "*1/*80", None), + ]), + ), + ]; + for (label, anns) in cases { + let r = plain( + &work, + &format!("INDET{}", label.len()), + &ugt1a1_report(gene(), anns), + ); + assert!( + r.rc == 0 + && r.rows == [strs(&["UGT1A1", "*1/*80", "Indeterminate", "unclassified"])] + && !r.rec.contains(INDET_NOTE), + "Indeterminate gene, {label}: stays Indeterminate and unclassified, no note: {:?}", + r.rows + ); + } + let r = plain( + &work, + "INDETNORMAL", + &ugt1a1_report( + gene(), + Some(vec![iann("Normal Metabolizer", "*1/*80", None)]), + ), + ); + assert_eq!( + r.rows, + [strs(&[ + "UGT1A1", + "*1/*80", + &format!("Normal Metabolizer {INDET_NOTE}"), + "normal" + ])], + "Indeterminate gene, drug level Normal Metabolizer: normal, with the note" + ); +} + +#[test] +fn an_indeterminate_gene_with_two_possible_diplotypes() { + let work = Scratch::new("indettwo"); + let two = || { + let mut d = dip("*1", "*80", "Indeterminate"); + let extra = match dip("*80", "*80", "Indeterminate") { + Value::Object(mut m) => m.shift_remove("sourceDiplotypes").unwrap(), + _ => unreachable!(), + }; + if let (Value::Array(v), Value::Array(x)) = (at(&mut d, "sourceDiplotypes"), extra) { + v.extend(x); + } + d + }; + let undecided = strs(&[ + "UGT1A1", + "*1/*80 (1 of 2 possible)", + "Indeterminate", + "unclassified", + ]); + // two possible diplotypes, both Indeterminate, whose drug annotations + // disagree: the gene is undecided and is not read as its first one + let r = plain( + &work, + "INDETTWO", + &ugt1a1_report( + two(), + Some(vec![ + iann("Intermediate Metabolizer", "*1/*80", None), + iann("Normal Metabolizer", "*80/*80", None), + ]), + ), + ); + assert!( + r.rc == 0 && r.rows == [undecided.clone()] && !r.rec.contains(INDET_NOTE), + "Indeterminate gene, two possible diplotypes whose drug annotations disagree: stays Indeterminate, no note: {:?}", + r.rows + ); + // the same phenotype for both, but different guidance: the drug list + // would show the first diplotype's, so the gene stays undecided + let r = plain( + &work, + "INDETTWOREC", + &ugt1a1_report( + two(), + Some(vec![ + iann("Intermediate Metabolizer", "*1/*80", None), + iann( + "Intermediate Metabolizer", + "*80/*80", + Some("Other guidance."), + ), + ]), + ), + ); + assert!( + r.rc == 0 && r.rows == [undecided] && !r.rec.contains(INDET_NOTE), + "Indeterminate gene, two possible diplotypes with one phenotype but different guidance: stays Indeterminate: {:?}", + r.rows + ); + // the same phenotype and the same guidance for both: it takes the phenotype + let r = plain( + &work, + "INDETTWOSAME", + &ugt1a1_report( + two(), + Some(vec![ + iann("Intermediate Metabolizer", "*1/*80", None), + iann("Intermediate Metabolizer", "*80/*80", None), + ]), + ), + ); + assert_eq!( + r.rows, + [strs(&[ + "UGT1A1", + "*1/*80 (1 of 2 possible)", + &format!("Intermediate Metabolizer {INDET_NOTE}"), + "non-normal" + ])], + "Indeterminate gene, two possible diplotypes with the same phenotype and guidance: takes it, with the note" + ); +} + +// --- 6. label-only differences, long text, single-position fallback ------------------ + +#[test] +fn single_position_labels_compare_by_meaning() { + let work = Scratch::new("labels"); + let vk = "rs9923231 reference (C)/rs9923231 variant (T)"; + let one = |label: &str, phen: &str| { + o(vec![( + "sourceDiplotypes", + a(vec![o(vec![ + ("label", s(label)), + ("phenotypes", a(vec![s(phen)])), + ])]), + )]) + }; + let data = genes(vec![ + ("VKORC1", one(vk, "-1639 GA")), + ( + "IFNL3", + one( + "rs12979860 variant (T)/rs12979860 variant (T)", + "unfavorable response genotype", + ), + ), + ]); + let r = run_report( + &work, + "LABELS", + &data, + &Opts { + pypgx: Some(vec![ + ("VKORC1", "Reference/rs9923231", "no pypgx phenotype rule"), + ("IFNL3", "Reference/rs12979860", "Unfavorable Response"), + ]), + ..Opts::default() + }, + ); + assert!( + r.comp.contains(&strs(&[ + "VKORC1", + vk, + "Reference/rs9923231", + "Yes", + "both", + "-1639 GA", + "no pypgx phenotype rule" + ])), + "VKORC1 'rsX reference (C)/rsX variant (T)' against pypgx 'Reference/rsX' is a Match: {:?}", + r.comp + ); + let ifnl3: Vec<&str> = r + .comp + .iter() + .filter(|c| c[0] == "IFNL3") + .map(|c| c[3].as_str()) + .collect(); + assert_eq!( + ifnl3, + ["No"], + "IFNL3 'rsX variant (T)/rsX variant (T)' against pypgx 'Reference/rsX' is still a conflict" + ); + assert!( + r.comp + .iter() + .all(|c| !c.join("\t").contains("Indeterminate")), + "the comparison shows PharmCAT's phenotype beside pypgx's 'no pypgx phenotype rule', no Indeterminate" + ); + let text = fs::read_to_string(work.0.join("LABELS/comparison.tsv")).unwrap(); + let head: Vec<&str> = text.lines().next().unwrap().split('\t').collect(); + assert_eq!( + head, + [ + "Gene", + "PharmCAT_diplotype", + "pypgx_diplotype", + "Match", + "Called_by", + "PharmCAT_phenotype", + "pypgx_phenotype" + ], + "the comparison header ends with the two phenotype columns" + ); +} + +#[test] +fn a_long_recommendation_prints_whole() { + let work = Scratch::new("long"); + let long = format!( + "{}If plasma concentrations are not available, consider a lower dose of a tricyclic in this patient.", + "Initiate therapy with 50% of the starting dose followed by titration of dose based on \ + toxicity or therapeutic drug monitoring. " + .repeat(4) + ); + let data = with( + genes(vec![("CYP2C19", dip("*2", "*2", "Poor Metabolizer"))]), + "drugs", + cpic_drugs( + "amitriptyline", + vec![o(vec![ + ("phenotypes", o(vec![("CYP2C19", s("Poor Metabolizer"))])), + ("classification", s("Optional")), + ("drugRecommendation", s(&long)), + ])], + ), + ); + let r = plain(&work, "LONG", &data); + assert!( + long.len() > 400 + && r.rec + .contains(&format!("- amitriptyline [CPIC, Optional]: {long}")) + && !block(&r.rec, "CYP2C19").contains("..."), + "a recommendation of {} characters prints whole", + long.len() + ); +} + +/// A gene PharmCAT leaves Unknown/Unknown, with the genotypes it read. +/// The call comes before the rsID: an rsID next to a genotype reads as a +/// leak. +fn variants(call: Option<&str>, rsid: &str) -> Value { + o(vec![ + ( + "sourceDiplotypes", + a(vec![o(vec![ + ("allele1", o(vec![("name", s("Unknown"))])), + ("allele2", o(vec![("name", s("Unknown"))])), + ("label", s("Unknown/Unknown")), + ("phenotypes", a(vec![])), + ])]), + ), + ( + "variants", + a(vec![ + o(vec![ + ("position", Value::Int(1)), + ("dbSnpId", s("rs0000001")), + ("call", s("G/A")), + ]), + o(vec![ + ("position", Value::Int(2)), + ("dbSnpId", s(rsid)), + ("call", call.map_or(Value::Null, s)), + ("referenceAllele", s("C")), + ]), + ]), + ), + ]) +} + +const FB: &str = "Single-Position Fallback (not a PharmCAT call):"; + +#[test] +fn cyp4f2_and_vkorc1_single_position_fallback() { + let work = Scratch::new("fallback1"); + let r = plain( + &work, + "CYP4F2HET", + &genes(vec![("CYP4F2", variants(Some("C/T"), "rs2108622"))]), + ); + let tail = r.rec.rsplit(FB).next().unwrap(); + assert!( + r.rc == 0 + && r.rec.contains(FB) + && tail.contains( + "WARNING: PharmCAT has no result for CYP4F2; single-position fallback, not a PharmCAT call: \ + rs2108622 reads C/T, one copy of the *3 variant (T, p.V433M)." + ) + && tail.contains("Warfarin (CPIC): the CPIC warfarin dosing algorithm") + && tail.contains("5-10%"), + "CYP4F2 No Result with C/T at rs2108622: the fallback names the genotype and the *3 rule, labelled" + ); + assert_eq!( + r.rows, + [strs(&[ + "CYP4F2", + "Unknown/Unknown", + "No Result", + "not called" + ])], + "CYP4F2 fallback: PharmCAT's own result stays not called in the phenotypes table" + ); + let r = plain( + &work, + "CYP4F2HOM", + &genes(vec![("CYP4F2", variants(Some("T|T"), "rs2108622"))]), + ); + assert!( + r.rec + .contains("rs2108622 reads T|T, two copies of the *3 variant"), + "CYP4F2 T|T (phased): two copies" + ); + let r = plain( + &work, + "CYP4F2REF", + &genes(vec![("CYP4F2", variants(Some("C/C"), "rs2108622"))]), + ); + assert!( + r.rec.contains( + "rs2108622 reads C/C, no copy of the *3 variant (T, p.V433M). The CPIC warfarin rule for this position does not apply." + ) && !r.rec.contains("WARNING: PharmCAT has no result for CYP4F2"), + "CYP4F2 C/C: no copy, the rule does not apply, and no WARNING" + ); + let r = plain( + &work, + "CYP4F2MISS", + &genes(vec![("CYP4F2", variants(None, "rs2108622"))]), + ); + assert!( + r.rec.contains( + "CYP4F2 -- PharmCAT has no result; rs2108622 has no genotype in the VCF PharmCAT read: not callable." + ) && !r.rec.rsplit(FB).next().unwrap().contains("WARNING"), + "CYP4F2 with no genotype at rs2108622: not callable" + ); + let r = plain( + &work, + "VKORC1HET", + &genes(vec![("VKORC1", variants(Some("C/T"), "rs9923231"))]), + ); + assert!( + r.rec.contains( + "WARNING: PharmCAT has no result for VKORC1; single-position fallback, not a PharmCAT call: \ + rs9923231 reads C/T, -1639 GA." + ) && r.rec.contains( + "Warfarin (CPIC): the CPIC warfarin guideline doses by a pharmacogenetic algorithm \ + that takes VKORC1 -1639G>A with CYP2C9; each A allele lowers the expected dose." + ), + "VKORC1 No Result with C/T at rs9923231: a WARNING reads -1639 GA, with the VKORC1 warfarin rule" + ); + let called = genes(vec![( + "CYP4F2", + with( + dip("*1", "*3", "Decreased Function"), + "variants", + a(vec![o(vec![ + ("dbSnpId", s("rs2108622")), + ("call", s("C/T")), + ])]), + ), + )]); + let r = plain(&work, "CYP4F2CALLED", &called); + assert!( + !r.rec.contains(FB), + "CYP4F2 called by PharmCAT: no fallback section" + ); +} + +// --- 7. MT-RNR1 from step 20's chrM calls next to step 36's consensus --------------- + +const CHILD: &str = "OG_PORT_TESTS_PGX_CHILD"; +const MARK: &str = "\u{1e}og-child\u{1e}"; + +/// Not a test of its own: the child [`og_out`] starts runs `og pgx ARGV` +/// here, then exits with the verb's code. In the normal test run it +/// returns at once. +#[test] +fn og_child() { + let Some(argv) = std::env::var_os(CHILD) else { + return; + }; + let argv: Vec = argv + .to_string_lossy() + .split('\u{1f}') + .map(str::to_owned) + .collect(); + print!("{MARK}"); + eprint!("{MARK}"); + let code = og_pgx::main(&argv); + let _ = std::io::stdout().flush(); + let _ = std::io::stderr().flush(); + std::process::exit(i32::from(code)); +} + +/// `og pgx ARGV` in a child copy of this test binary: (exit code, stdout). +fn og_out(argv: &[&str]) -> (i32, String) { + let child = Command::new(std::env::current_exe().unwrap()) + .args([ + "--exact", + "og_child", + "--nocapture", + "--test-threads=1", + "-q", + ]) + .env(CHILD, argv.join("\u{1f}")) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + .unwrap(); + let done = child.wait_with_output().unwrap(); + let code = done.status.code().unwrap_or(-1); + let out = String::from_utf8_lossy(&done.stdout); + match out.split_once(MARK) { + Some((_, rest)) => (code, rest.to_owned()), + None => panic!("the child never reached the verb: {out}"), + } +} + +#[test] +fn an_mt_rnr1_outside_call_is_not_checked_against_step36() { + let work = Scratch::new("mtrnr1"); + let outside = |a1: &str, a2: Option<&str>, phen: &str, drugs: &[&str]| { + let g = match a2 { + Some(a2) => dip(a1, a2, phen), + None => o(vec![( + "sourceDiplotypes", + a(vec![o(vec![ + ("allele1", o(vec![("name", s(a1))])), + ("allele2", Value::Null), + ("label", s(a1)), + ("phenotypes", a(vec![s(phen)])), + ])]), + )]), + }; + let g = with(g, "callSource", s("OUTSIDE")); + with( + g, + "relatedDrugs", + a(drugs.iter().map(|n| o(vec![("name", s(n))])).collect()), + ) + }; + let mt = outside( + "m.1555A>G", + None, + "increased risk of aminoglycoside-induced hearing loss", + &["amikacin", "gentamicin"], + ); + let mito = genes(vec![ + ("MT-RNR1", mt.clone()), + ( + "CYP2D6", + outside("*1", Some("*4"), "Intermediate Metabolizer", &["codeine"]), + ), + ]); + let r = run_report( + &work, + "MTRNR1CONS", + &mito, + &Opts { + consensus: Some(vec![("CYP2D6", "*1/*1", "yes")]), + ..Opts::default() + }, + ); + assert_eq!( + r.rc, 0, + "MT-RNR1 outside call, consensus without it: exits 0" + ); + assert!( + r.rows.contains(&strs(&[ + "MT-RNR1", + "m.1555A>G", + "increased risk of aminoglycoside-induced hearing loss", + "non-normal" + ])), + "MT-RNR1 outside call, consensus without it: stays called and non-normal: {:?}", + r.rows + ); + let b = block(&r.rec, "MT-RNR1"); + assert!( + r.rec.contains(" MT-RNR1 -- ") && b.contains("amikacin") && b.contains("gentamicin"), + "MT-RNR1 outside call, consensus without it: listed with its drugs: {b}" + ); + assert!( + !r.rec.contains("outside call MT-RNR1"), + "MT-RNR1 outside call, consensus without it: no stale warning for MT-RNR1" + ); + assert!( + r.rec + .contains("WARNING: PharmCAT's report has the outside call CYP2D6 *1/*4") + && !medications(&r.rec).contains(" CYP2D6 -- "), + "control: the CYP2D6 outside call the consensus passes as another diplotype is still held back" + ); + let rep = work.0.join("MTRNR1CONS/report.json"); + let (rc, out) = og_out(&["step36-outside", "--report", &rep.to_string_lossy()]); + assert!( + rc == 0 && out.split_whitespace().collect::>() == ["CYP2D6"], + "step36-outside lists CYP2D6 and not MT-RNR1: {rc} {out:?}" + ); + let only = work.file( + "MTRNR1ONLY.json", + &dumps(&genes(vec![("MT-RNR1", mt)]), &Dump::default()).unwrap(), + ); + let (rc, out) = og_out(&["step36-outside", "--report", &only]); + assert!( + rc == 0 && out.is_empty(), + "step36-outside lists nothing for a report whose only outside call is MT-RNR1: {rc} {out:?}" + ); +} + +// --- 8. phenoconversion -------------------------------------------------------------- + +/// A synthetic report: CYP2D6 *1/*2 normal metabolizer (activity score +/// `nm_score`, none for `None`) and CYP2C19 *1/*1 normal metabolizer. +fn pheno_report(nm_score: Option<&str>) -> Value { + let mut d6 = dip("*1", "*2", "Normal Metabolizer"); + if let Value::Array(v) = at(&mut d6, "sourceDiplotypes") + && let Value::Object(m) = &mut v[0] + { + m.insert("activityScore".to_owned(), nm_score.map_or(Value::Null, s)); + } + o(vec![ + ("pharmcatVersion", s("3.4.0")), + ( + "genes", + o(vec![ + ("CYP2D6", d6), + ("CYP2C19", dip("*1", "*1", "Normal Metabolizer")), + ]), + ), + ]) +} + +/// The phenoconversion section of the recommendations text. +fn section(rec: &str) -> &str { + between( + rec, + "Medications and Phenoconversion (CYP2D6, CYP2C19, CYP2C9, CYP3A4, CYP3A5):", + "Affected Medications:", + ) +} + +fn meds<'a>(m: &'a str) -> Opts<'a> { + Opts { + medications: Some(m), + ..Opts::default() + } +} + +fn meds_with<'a>(m: &'a str, table: String) -> Opts<'a> { + Opts { + medications: Some(m), + inhibitors: Some(table), + ..Opts::default() + } +} + +/// The non-comment lines of a shipped table: its header and its rows. +fn table_rows(path: &str) -> (Vec, Vec>) { + let text = fs::read_to_string(path).unwrap(); + let mut lines = text.lines().filter(|l| !l.starts_with('#')); + let header = lines + .next() + .unwrap() + .split('\t') + .map(str::to_owned) + .collect(); + let rows = lines + .filter(|l| !l.trim().is_empty()) + .map(|l| l.split('\t').map(str::to_owned).collect()) + .collect(); + (header, rows) +} + +/// The inhibitor table with `from` replaced by `to` on every line. +fn edited_inhibitors(work: &Scratch, name: &str, from: &str, to: &str) -> String { + let text = fs::read_to_string(inhibitors()).unwrap(); + work.file(name, &text.replace(from, to)) +} + +#[test] +fn inhibitor_table_has_the_shape_the_reader_reads() { + let work = Scratch::new("inhibitors"); + let (header, rows) = table_rows(&inhibitors()); + assert_eq!( + header, + [ + "Drug", + "Gene", + "Strength", + "Activity_score_factor", + "Class_source", + "Rule_source" + ], + "inhibitors: the header" + ); + // The strengths each gene has rows for: the FDA's table lists no strong + // CYP2C9 inhibitor, and the CYP3A rows are its strong inhibitors only. + let strengths: [(&str, &[&str]); 5] = [ + ("CYP2D6", &["strong", "moderate"]), + ("CYP2C19", &["strong", "moderate"]), + ("CYP2C9", &["moderate"]), + ("CYP3A4", &["strong"]), + ("CYP3A5", &["strong"]), + ]; + let factor = |g: &str, st: &str| -> Option<&'static str> { + let known = strengths + .iter() + .any(|(k, sts)| *k == g && sts.contains(&st)); + known.then_some(match (g, st) { + ("CYP2D6", "strong") => "0", + ("CYP2D6", _) => "0.5", + _ => "-", + }) + }; + let bad: Vec<&Vec> = rows + .iter() + .filter(|r| { + r.len() != 6 + || factor(&r[1], &r[2]) != Some(r[3].as_str()) + || r[0] != r[0].trim().to_lowercase() + || r[0].is_empty() + || r[4] != FDA_TABLE + || r[5] + != if r[1] == "CYP2D6" { + "https://cpicpgx.org/guidelines/guideline-for-codeine-and-cyp2d6/" + } else { + "-" + } + }) + .collect(); + assert!( + bad.is_empty(), + "inhibitors: every row has a gene, a strength, CPIC's factor for it and its sources: {bad:?}" + ); + let keys: Vec<(&str, &str)> = rows + .iter() + .map(|r| (r[0].as_str(), r[1].as_str())) + .collect(); + let mut unique = keys.clone(); + unique.sort_unstable(); + unique.dedup(); + assert_eq!( + unique.len(), + keys.len(), + "inhibitors: no drug twice for one gene" + ); + let mut table_genes: Vec<&str> = strengths.iter().map(|(g, _)| *g).collect(); + table_genes.sort_unstable(); + let mut parser_genes = PHENOCONVERSION_GENES.to_vec(); + parser_genes.sort_unstable(); + assert_eq!( + table_genes, parser_genes, + "inhibitors: the genes of the table are the parser's" + ); + for (gene, sts) in strengths { + for st in sts { + assert!( + rows.iter().any(|r| r[1] == gene && r[2] == *st), + "inhibitors: {gene} has {st} inhibitors" + ); + } + } + let drugs_of = |g: &str| -> std::collections::BTreeSet<&str> { + rows.iter() + .filter(|r| r[1] == g) + .map(|r| r[0].as_str()) + .collect() + }; + assert_eq!( + drugs_of("CYP3A4"), + drugs_of("CYP3A5"), + "inhibitors: CYP3A4 and CYP3A5 list the same strong inhibitors" + ); + let r = run_report( + &work, + "PHENOREAD", + &pheno_report(Some("2.0")), + &meds("paroxetine\n"), + ); + let sec = section(&r.rec); + assert!( + !sec.contains("Could not read") && sec.contains("phenoconverted by paroxetine"), + "inhibitors: the parser reads the table: {sec}" + ); + let boosted = [ + "elvitegravir", + "indinavir", + "lopinavir", + "paritaprevir", + "saquinavir", + "tipranavir", + ]; + assert!( + boosted.iter().all(|d| ["CYP3A4", "CYP3A5"] + .iter() + .all(|g| keys.contains(&(*d, *g)))), + "inhibitors: each ritonavir-boosted partner the FDA classes as strong has a CYP3A4 and a CYP3A5 row" + ); +} + +#[test] +fn a_strong_cyp2d6_inhibitor_phenoconverts_and_cyp2c19_gets_a_note() { + let work = Scratch::new("pheno"); + let r = run_report( + &work, + "PHENO", + &pheno_report(Some("2.0")), + &meds("Paroxetine 20 mg daily\nfluconazole\nnotadrug\n# a comment\n\n"), + ); + assert_eq!(r.rc, 0, "phenoconversion: exits 0"); + assert_eq!( + cell(&r.rows, "CYP2D6", 4), + "Poor Metabolizer (phenoconverted by paroxetine)", + "phenoconversion: strong CYP2D6 inhibitor turns the NM into 'Poor Metabolizer (phenoconverted by paroxetine)'" + ); + assert_eq!( + row(&r.rows, "CYP2D6").map(|r| r[2..4].to_vec()), + Some(strs(&["Normal Metabolizer", "normal"])), + "phenoconversion: the genotype phenotype is kept beside it" + ); + let sec = section(&r.rec); + assert!( + sec.contains( + "WARNING: CYP2D6 genotype phenotype Normal Metabolizer; phenoconverted phenotype \ + Poor Metabolizer (phenoconverted by paroxetine)" + ), + "phenoconversion: a WARNING line with both phenotypes: {sec}" + ); + assert!( + sec.contains( + "the CYP2D6 activity score is adjusted to 0 and the predicted phenotype is a poor metabolizer" + ), + "phenoconversion: CPIC's wording for a strong inhibitor" + ); + assert!( + cell(&r.rows, "CYP2C19", 4).starts_with("not computed: fluconazole") + && sec.contains( + "CYP2C19 genotype phenotype Normal Metabolizer; on fluconazole (strong CYP2C19 inhibitor)" + ), + "phenoconversion: CYP2C19 inhibitor gets a note and no converted phenotype" + ); + assert!( + sec.contains("Not in the inhibitor table: notadrug."), + "phenoconversion: a name not in the table is listed, not dropped" + ); + assert!( + sec.contains("an aid for your clinician, not advice"), + "phenoconversion: the docs line says it is an aid for the clinician" + ); + + // a line that starts with a separator still names its drug + let r = run_report( + &work, + "PHENOSEP", + &pheno_report(Some("2.0")), + &meds(", paroxetine\n;\n"), + ); + assert!( + cell(&r.rows, "CYP2D6", 4) == "Poor Metabolizer (phenoconverted by paroxetine)" + && !section(&r.rec).contains("Not in the inhibitor table"), + "phenoconversion: ', paroxetine' is read as paroxetine" + ); +} + +#[test] +fn a_moderate_inhibitor_halves_the_activity_score() { + let work = Scratch::new("phenomod"); + let r = run_report( + &work, + "PHENOMOD", + &pheno_report(Some("2.0")), + &meds("duloxetine\n"), + ); + assert!( + cell(&r.rows, "CYP2D6", 4) == "Intermediate Metabolizer (phenoconverted by duloxetine)" + && section(&r.rec).contains("activity score 2 x 0.5 = 1"), + "phenoconversion: moderate inhibitor, AS 2 x 0.5 = 1 is an intermediate metabolizer" + ); + assert_eq!( + cell(&r.rows, "CYP2C19", 4), + "-", + "phenoconversion: a gene without an inhibitor on the list gets '-'" + ); + let r = run_report( + &work, + "PHENONOAS", + &pheno_report(None), + &meds("duloxetine\n"), + ); + assert_eq!( + cell(&r.rows, "CYP2D6", 4), + "not converted: the report gives no activity score", + "phenoconversion: moderate inhibitor without an activity score converts nothing" + ); +} + +#[test] +fn a_list_that_names_no_drug_changes_nothing() { + let work = Scratch::new("phenonone"); + let r0 = plain(&work, "PHENONONE", &pheno_report(Some("2.0"))); + let r1 = run_report( + &work, + "PHENOEMPTY", + &pheno_report(Some("2.0")), + &meds("# nothing taken\n\n"), + ); + let t0 = fs::read_to_string(work.0.join("PHENONONE/PHENONONE_phenotypes.tsv")).unwrap(); + let t1 = fs::read_to_string(work.0.join("PHENOEMPTY/PHENOEMPTY_phenotypes.tsv")).unwrap(); + assert_eq!( + t0, t1, + "phenoconversion: an empty list leaves the phenotypes table as without one" + ); + assert!( + section(&r1.rec).contains("The medication list names no drug: no phenotype is converted.") + && !r1.rec.replace("PHENOEMPTY", "").contains("phenoconverted"), + "phenoconversion: an empty list says so and converts nothing" + ); + assert!( + !r0.rec.contains("Medications and Phenoconversion"), + "phenoconversion: no list, no section" + ); +} + +/// Control: the inhibitor's factor set to 1 must not make a poor +/// metabolizer. +#[test] +fn control_a_factor_of_1_converts_nothing() { + let work = Scratch::new("phenobroken"); + let broken = edited_inhibitors( + &work, + "inhibitors-factor-1.tsv", + "paroxetine\tCYP2D6\tstrong\t0\t", + "paroxetine\tCYP2D6\tstrong\t1\t", + ); + let r = run_report( + &work, + "PHENOBROKEN", + &pheno_report(Some("2.0")), + &meds_with("paroxetine\n", broken), + ); + assert!( + !cell(&r.rows, "CYP2D6", 4).starts_with("Poor Metabolizer"), + "control: with the factor set to 1 the NM is not converted to a poor metabolizer: {:?}", + row(&r.rows, "CYP2D6") + ); +} + +#[test] +fn a_nan_score_or_a_broken_inhibitor_row_converts_nothing() { + let work = Scratch::new("phenonan"); + let r = run_report( + &work, + "PHENONAN", + &pheno_report(Some("NaN")), + &meds("duloxetine\n"), + ); + assert_eq!( + cell(&r.rows, "CYP2D6", 4), + "not converted: the report gives no activity score", + "phenoconversion: a NaN activity score converts nothing" + ); + let unreadable = "Could not read the medication list or the inhibitor table"; + let short = work.file( + "inhibitors-short-row.tsv", + "Drug\tGene\tStrength\tActivity_score_factor\tClass_source\tRule_source\nparoxetine\n", + ); + let r = run_report( + &work, + "PHENOSHORT", + &pheno_report(Some("2.0")), + &meds_with("paroxetine\n", short), + ); + assert!( + r.rc == 0 && section(&r.rec).contains(unreadable), + "phenoconversion: a table row with no Gene cell is reported as unreadable, the report still written" + ); + let blank = edited_inhibitors( + &work, + "inhibitors-blank-gene.tsv", + "paroxetine\tCYP2D6\t", + "paroxetine\t \t", + ); + let r = run_report( + &work, + "PHENOBLANK", + &pheno_report(Some("2.0")), + &meds_with("paroxetine\n", blank), + ); + assert!( + r.rc == 0 && section(&r.rec).contains(unreadable), + "phenoconversion: a row with a blank Gene cell is reported as unreadable, never read as a known drug" + ); + let nofac = edited_inhibitors( + &work, + "inhibitors-no-factor.tsv", + "paroxetine\tCYP2D6\tstrong\t0\t", + "paroxetine\tCYP2D6\tstrong\t\t", + ); + let r = run_report( + &work, + "PHENONOFAC", + &pheno_report(Some("2.0")), + &meds_with("paroxetine\n", nofac), + ); + assert!( + r.rc == 0 && section(&r.rec).contains(unreadable), + "phenoconversion: a CYP2D6 row with an empty factor is reported as unreadable, the report still written" + ); + let outdir = work.0.to_string_lossy().into_owned(); + assert_eq!( + og(&[ + "cpic-report", + "--sample", + "X", + "--report", + "r.json", + "--outdir", + &outdir, + "--medications", + "m.txt" + ]), + 2, + "--medications without --inhibitors is refused" + ); +} + +#[test] +fn cyp2c9_and_cyp3a5_inhibitors_give_a_note() { + let work = Scratch::new("pheno2c9"); + let mut rep = pheno_report(Some("2.0")); + if let Value::Object(m) = at(&mut rep, "genes") { + m.insert("CYP2C9".to_owned(), dip("*1", "*1", "Normal Metabolizer")); + m.insert("CYP3A5".to_owned(), dip("*3", "*3", "Poor Metabolizer")); + } + let r = run_report( + &work, + "PHENO2C9", + &rep, + &meds("fluconazole\nketoconazole\n"), + ); + assert_eq!( + cell(&r.rows, "CYP2C9", 4), + "not computed: fluconazole inhibits CYP2C9; CPIC gives no rule that converts the CYP2C9 phenotype", + "CYP2C9: a moderate inhibitor (fluconazole) changes the reported column to a note" + ); + assert_eq!( + row(&r.rows, "CYP2C9").map(|r| r[2..4].to_vec()), + Some(strs(&["Normal Metabolizer", "normal"])), + "CYP2C9: the genotype phenotype is kept" + ); + let sec = section(&r.rec); + assert!( + sec.contains( + "WARNING: CYP2C9 genotype phenotype Normal Metabolizer; on fluconazole (moderate CYP2C9 inhibitor)." + ), + "CYP2C9: a WARNING line names the inhibitor: {sec}" + ); + assert!( + cell(&r.rows, "CYP3A5", 4).starts_with("not computed: ketoconazole inhibits CYP3A5"), + "CYP3A5: a strong CYP3A inhibitor (ketoconazole) changes the reported column to a note" + ); + assert!( + !sec.contains("can lower CYP3A5 activity") && sec.contains("can lower CYP2C9 activity"), + "CYP3A5 *3/*3 (poor metabolizer): no line says the inhibitor can lower its activity; CYP2C9 keeps it" + ); + let r = run_report( + &work, + "PHENOBOOSTED", + &rep, + &meds("lopinavir/ritonavir 400/100 mg\n"), + ); + assert!( + cell(&r.rows, "CYP3A5", 4).starts_with("not computed: lopinavir inhibits CYP3A5") + && !section(&r.rec).contains("Not in the inhibitor table"), + "a boosted combination written 'lopinavir/ritonavir' is matched as lopinavir" + ); + let r = run_report(&work, "PHENO2C9NONE", &rep, &meds("paroxetine\n")); + assert!( + cell(&r.rows, "CYP2C9", 4) == "-" && cell(&r.rows, "CYP3A5", 4) == "-", + "CYP2C9 and CYP3A5: without an inhibitor of theirs on the list the column stays '-'" + ); +} + +// --- 9. disease-variant contraindications ------------------------------------------ + +const HITS_HEADER: &str = + "chrom\tpos\tref\talt\tgenotype\tclinvar_id\tgeneinfo\tclnsig\tclnrevstat\n"; + +/// One synthetic row of step 06's hits table (no one's variant). +fn hit(gene: &str, clnsig: &str, gt: &str) -> String { + format!( + "{HITS_HEADER}chr15\t89300000\tC\tT\t{gt}\t9900001\t{gene}:1\t{clnsig}\tcriteria_provided,_single_submitter\n" + ) +} + +fn contra_section(rec: &str) -> &str { + between( + rec, + "Drug Contraindications From Disease Variants (ClinVar hits, step 06):", + "Uncallable Genes:", + ) +} + +fn contra_opts<'a>(table: String, hits: Option, m: Option<&'a str>) -> Opts<'a> { + Opts { + contraindications: Some(table), + hits, + medications: m, + ..Opts::default() + } +} + +#[test] +fn contraindication_table_has_the_shape_the_reader_reads() { + let work = Scratch::new("contraread"); + let (header, rows) = table_rows(&contra()); + assert_eq!(header, CONTRA_COLUMNS, "contraindications: the header"); + let bad: Vec<&Vec> = rows + .iter() + .filter(|r| { + r.len() != 8 + || !r.iter().all(|c| !c.trim().is_empty()) + || !["A", "B", "A/B"].contains(&r[3].as_str()) + || r[5] + != format!( + "https://api.cpicpgx.org/v1/pair_view?genesymbol=eq.{}", + r[0] + ) + || !r[6].starts_with("https://dailymed.nlm.nih.gov/dailymed/drugInfo.cfm?setid=") + || !["any", "biallelic"].contains(&r[7].as_str()) + || r[2] + .split(',') + .any(|n| n != n.trim().to_lowercase() || n.is_empty()) + || !r[2] + .split(',') + .any(|n| Some(n) == r[1].split_whitespace().next()) + }) + .collect(); + assert!( + bad.is_empty(), + "contraindications: every row has a gene, a drug, its names, a CPIC level, a note and its sources: {bad:?}" + ); + let keys: Vec<(&str, &str)> = rows + .iter() + .map(|r| (r[0].as_str(), r[1].as_str())) + .collect(); + let mut unique = keys.clone(); + unique.sort_unstable(); + unique.dedup(); + assert_eq!( + unique.len(), + keys.len(), + "contraindications: no gene-drug pair twice" + ); + for pair in [ + ("POLG", "valproic acid"), + ("ASL", "valproic acid"), + ("ASS1", "valproic acid"), + ("CPS1", "valproic acid"), + ("NAGS", "valproic acid"), + ("OTC", "valproic acid"), + ("HPRT1", "mycophenolic acid"), + ] { + assert!( + keys.contains(&pair), + "contraindications: POLG with valproic acid, the urea-cycle genes and HPRT1 are there: {pair:?}" + ); + } + assert!( + !rows.iter().any(|r| r[0] == "SCN1A"), + "contraindications: SCN1A is left out (no label or guideline names it)" + ); + let r = run_report( + &work, + "CONTRAREAD", + &pheno_report(Some("2.0")), + &contra_opts(contra(), None, None), + ); + let mut order: Vec<&str> = Vec::new(); + for r in &rows { + if !order.contains(&r[0].as_str()) { + order.push(&r[0]); + } + } + let sec = contra_section(&r.rec); + assert!( + !sec.contains("Could not read") + && sec.contains(&format!("Genes it would check: {}.", order.join(", "))), + "contraindications: the parser reads the table: {sec}" + ); +} + +#[test] +fn a_pathogenic_polg_hit_adds_the_valproate_warning() { + let work = Scratch::new("contraplp"); + let rep = pheno_report(Some("2.0")); + let r = run_report( + &work, + "CONTRAPLP", + &rep, + &contra_opts(contra(), Some(hit("POLG", "Pathogenic", "0/1")), None), + ); + let sec = contra_section(&r.rec); + assert_eq!(r.rc, 0, "contraindications: exits 0"); + assert!( + sec.contains( + "WARNING: valproic acid -- POLG chr15:89300000 C>T, genotype 0/1, ClinVar Pathogenic." + ), + "contraindications: a pathogenic POLG hit adds the valproic acid WARNING: {sec}" + ); + assert!( + sec.contains("mitochondrial disorders caused by POLG mutations") + && sec.contains("(provisional CPIC level A/B: no CPIC guideline covers this pair)") + && sec.contains("dailymed.nlm.nih.gov"), + "contraindications: the line carries the label's note, the CPIC level and the label" + ); + assert!( + sec.contains("Review it with your clinician"), + "contraindications: the section says to review it with a clinician" + ); + let r = run_report( + &work, + "CONTRALP", + &rep, + &contra_opts( + contra(), + Some(hit("POLG", "Pathogenic/Likely_pathogenic", "1/1")), + None, + ), + ); + assert!( + contra_section(&r.rec).contains("WARNING: valproic acid -- POLG"), + "contraindications: Pathogenic/Likely_pathogenic counts" + ); + for (name, sig) in [ + ("CONTRABEN", "Benign"), + ("CONTRACONF", "Conflicting_classifications_of_pathogenicity"), + ("CONTRARISK", "risk_factor"), + ] { + let r = run_report( + &work, + name, + &rep, + &contra_opts(contra(), Some(hit("POLG", sig, "0/1")), None), + ); + let sec = contra_section(&r.rec); + assert!( + !sec.contains("WARNING") + && sec.contains("No pathogenic or likely pathogenic ClinVar hit in POLG"), + "contraindications: a POLG hit classed {sig} adds no drug line: {sec}" + ); + } + let r = run_report( + &work, + "CONTRAOTHER", + &rep, + &contra_opts(contra(), Some(hit("BRCA2", "Pathogenic", "0/1")), None), + ); + assert!( + !contra_section(&r.rec).contains("WARNING"), + "contraindications: a pathogenic hit in a gene outside the table adds no drug line" + ); + let r = run_report( + &work, + "CONTRAMED", + &rep, + &contra_opts( + contra(), + Some(hit("POLG", "Likely_pathogenic", "0/1")), + Some("Valproate 500 mg\n"), + ), + ); + assert!( + contra_section(&r.rec).contains("ClinVar Likely_pathogenic; ON THE MEDICATION LIST."), + "contraindications: valproate on the medication list is marked" + ); + let r = run_report( + &work, + "CONTRANOTON", + &rep, + &contra_opts( + contra(), + Some(hit("POLG", "Pathogenic", "0/1")), + Some("paroxetine\n"), + ), + ); + assert!( + contra_section(&r.rec).contains("WARNING: valproic acid") + && !r.rec.contains("ON THE MEDICATION LIST"), + "contraindications: a drug not on the list is not marked" + ); +} + +#[test] +fn no_or_an_unreadable_hits_table() { + let work = Scratch::new("contranohits"); + let rep = pheno_report(Some("2.0")); + let r = run_report( + &work, + "CONTRANOHITS", + &rep, + &contra_opts(contra(), None, None), + ); + let sec = contra_section(&r.rec); + assert!( + sec.contains("Not checked: no ClinVar hits table") && sec.contains("POLG"), + "contraindications: without a hits table the genes are listed as not checked" + ); + let r = run_report( + &work, + "CONTRABADHITS", + &rep, + &contra_opts(contra(), Some("nothing\there\n".to_owned()), None), + ); + assert!( + r.rc == 0 && contra_section(&r.rec).contains("Could not read the ClinVar hits table"), + "contraindications: an unreadable hits table is reported, the report still written" + ); + let r = plain(&work, "CONTRANONE", &rep); + assert!( + !r.rec.contains("Drug Contraindications"), + "contraindications: without the table there is no section" + ); + let outdir = work.0.to_string_lossy().into_owned(); + assert_eq!( + og(&[ + "cpic-report", + "--sample", + "X", + "--report", + "r.json", + "--outdir", + &outdir, + "--clinvar-hits", + "h.tsv" + ]), + 2, + "--clinvar-hits without --contraindications is refused" + ); +} + +/// Control: the same hit with the POLG row taken out of the table gives no +/// valproate line. +#[test] +fn control_without_the_polg_row_no_valproate_line() { + let work = Scratch::new("contractrl"); + let text = fs::read_to_string(contra()).unwrap(); + let kept: String = text + .split_inclusive('\n') + .filter(|l| !l.starts_with("POLG\t")) + .collect(); + assert_ne!(kept, text, "the control took the POLG row out"); + let nopolg = work.file("contraindications-no-polg.tsv", &kept); + let r = run_report( + &work, + "CONTRACTRL", + &pheno_report(Some("2.0")), + &contra_opts(nopolg, Some(hit("POLG", "Pathogenic", "0/1")), None), + ); + assert!( + !contra_section(&r.rec).contains("valproic acid"), + "control: without the POLG row the same hit gives no valproic acid line" + ); +} diff --git a/rust/crates/og-pgx/tests/port_tests_pgx_outside.rs b/rust/crates/og-pgx/tests/port_tests_pgx_outside.rs new file mode 100644 index 000000000..304d10e74 --- /dev/null +++ b/rust/crates/og-pgx/tests/port_tests_pgx_outside.rs @@ -0,0 +1,259 @@ +//! What step 27 (`og pgx cpic-report --consensus`) makes of step 36's +//! consensus table (`og pgx outside-calls`): +//! +//! 8. CYP2D6 held back: the CPIC report says so, names the drugs it +//! affects, and drops the pypgx-only warning it prints without the table; +//! 9. HLA passed on: the CPIC report lists it in the outside-call section +//! and marks the gene PharmCAT reports with callSource OUTSIDE; +//! 10. a stale report: PharmCAT's report has outside calls the consensus +//! does not pass on (another HLA-B, a CYP2D6 now held back, or no +//! readable table at all); they give no drug guidance and the report +//! says to rerun step 07. +//! +//! The consensus tables are the literal text `og pgx outside-calls` writes +//! for the named fixtures of `tests/fixtures/pgx/`; `og-steps`'s +//! `tests/pgx_outside_calls.rs` holds the verb to exactly these tables. + +use std::fs; +use std::path::{Path, PathBuf}; + +const HEADER: &str = "Gene\tResult\tOutside_call\tReason\tEvidence\n"; +const NO_HLA: &str = "HLA-A\tnot typed\tno\tHLA typing (step 08) did not run\t-\n\ + HLA-B\tnot typed\tno\tHLA typing (step 08) did not run\t-\n"; +const NO_CYP2D6: &str = "CYP2D6\tindeterminate\tno\tno caller made a call\ + \tpypgx: not run; Cyrius: not run; depth check: not run\n"; + +/// `--pypgx pypgx_cyp2d6_1_4.tsv --depth-check depth_check_ok.tsv`. +fn held_back() -> String { + format!( + "{HEADER}{NO_HLA}CYP2D6\tindeterminate\tno\tone caller only: a second caller must agree (Cyrius: setup.sh --cyrius)\ + \tpypgx: *1/*4; Cyrius: not run; depth check: ok\n" + ) +} + +/// `--hla t1k_genotype_docs_example.tsv`. +fn hla_docs_example() -> String { + format!( + "{HEADER}HLA-A\t*02:01/*24:02\tyes\tT1K (step 08)\tHLA-A*02:01:01 (quality 60); HLA-A*24:02:01 (quality 60)\n\ + HLA-B\t*15:02/*57:01\tyes\tT1K (step 08)\tHLA-B*15:02:01 (quality 60); HLA-B*57:01:01 (quality 48)\n\ + {NO_CYP2D6}" + ) +} + +/// `--hla t1k_genotype.tsv`. +fn hla_other() -> String { + format!( + "{HEADER}HLA-A\t*02:01/*24:02\tyes\tT1K (step 08)\tHLA-A*02:01:01 (quality 60); HLA-A*24:02:01 (quality 60)\n\ + HLA-B\t*07:02/*44:02\tyes\tT1K (step 08)\tHLA-B*07:02:01 (quality 60); HLA-B*44:02:01 (quality 48)\n\ + {NO_CYP2D6}" + ) +} + +fn repo() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../..") +} + +/// A scratch directory of one test: emptied when made, removed when +/// dropped. +struct Scratch(PathBuf); + +impl Scratch { + fn new(name: &str) -> Self { + let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("pgx-cons-{name}")); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + Self(dir) + } +} + +impl Drop for Scratch { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } +} + +enum Table { + None, + Text(String), + Missing, +} + +/// The recommendations text of `og pgx cpic-report` on a PharmCAT fixture +/// report, with pypgx's fixture summary and a consensus table when given. +fn cpic(tag: &str, report: &str, pypgx: Option<&str>, table: Table) -> String { + let tmp = Scratch::new(tag); + let dir = tmp.0.to_string_lossy().into_owned(); + let mut args: Vec = vec![ + "cpic-report".into(), + "--sample".into(), + "T".into(), + "--outdir".into(), + dir, + "--report".into(), + repo() + .join("tests/fixtures/pharmcat") + .join(report) + .to_string_lossy() + .into_owned(), + ]; + if let Some(p) = pypgx { + args.push("--pypgx".into()); + args.push( + repo() + .join("tests/fixtures/pgx") + .join(p) + .to_string_lossy() + .into_owned(), + ); + } + let cons = tmp.0.join(match table { + Table::Missing => "missing.tsv", + _ => "consensus.tsv", + }); + match &table { + Table::None => {} + Table::Text(t) => { + fs::write(&cons, t).unwrap(); + args.push("--consensus".into()); + args.push(cons.to_string_lossy().into_owned()); + } + Table::Missing => { + args.push("--consensus".into()); + args.push(cons.to_string_lossy().into_owned()); + } + } + let rc = og_pgx::main(&args); + if rc != 0 { + return format!("exit {rc}"); + } + fs::read_to_string(tmp.0.join("T_cpic_recommendations.txt")).unwrap() +} + +/// The Affected Medications section of a recommendations text. +fn medications(text: &str) -> &str { + let rest = text + .split_once("Affected Medications:") + .map_or(text, |(_, r)| r); + rest.split("Calls From Other Tools") + .next() + .unwrap_or_default() +} + +const DOCS: &str = "pharmcat-docs-example.json"; + +/// Case 8: the 3.4.0 report of the fixture: PharmCAT has no CYP2D6 result. +#[test] +fn case8_cyp2d6_held_back() { + let plain = cpic( + "8plain", + "report-3.4.0.json", + Some("pypgx_cyp2d6_1_4.tsv"), + Table::None, + ); + assert!( + plain.contains("WARNING: PharmCAT has no result for CYP2D6, but pypgx"), + "8 control: without the table, pypgx's call alone draws a warning" + ); + let held = cpic( + "8held", + "report-3.4.0.json", + Some("pypgx_cyp2d6_1_4.tsv"), + Table::Text(held_back()), + ); + assert!( + held.contains("Calls From Other Tools (outside calls, step 36):"), + "8 the report has the outside-call section" + ); + assert!( + held.contains("CYP2D6 indeterminate not passed to PharmCAT: one caller only"), + "8 CYP2D6 is indeterminate and why" + ); + assert!( + held.contains("No drug guidance is given for CYP2D6 here. Drugs affected by CYP2D6: "), + "8 it names the drugs CYP2D6 affects" + ); + assert!( + !held.contains("PharmCAT has no result for CYP2D6"), + "8 no pypgx-only warning for the held-back gene" + ); +} + +/// Case 9: PharmCAT's example report has HLA-B from an outside call (and CYP2D6 +/// and MT-RNR1, which this consensus does not pass on: case 10). +#[test] +fn case9_hla_passed_on() { + let passed = cpic("9", DOCS, None, Table::Text(hla_docs_example())); + assert!( + passed.contains( + "HLA-B *15:02/*57:01 passed to PharmCAT from T1K (step 08); PharmCAT reports it as an outside call." + ), + "9 HLA-B is listed as passed from T1K" + ); + assert!( + passed.contains("[outside call]"), + "9 the gene line marks PharmCAT's outside call" + ); + assert!( + passed.contains(" HLA-B -- "), + "9 the confirmed HLA-B keeps its drug guidance" + ); + assert!( + !passed.contains("outside call HLA-B"), + "9 no stale warning for the confirmed HLA-B" + ); +} + +/// Case 10: outside calls the consensus does not pass on. +#[test] +fn case10_a_stale_report() { + let plain = cpic("10plain", DOCS, None, Table::None); + assert!( + medications(&plain).contains(" CYP2D6 -- "), + "10 control: without the table, the example's CYP2D6 has drug guidance" + ); + assert!( + medications(&plain).contains(" HLA-B -- "), + "10 control: without the table, the example's HLA-B has drug guidance" + ); + let other = cpic("10other", DOCS, None, Table::Text(hla_other())); + assert!( + !medications(&other).contains(" HLA-B -- "), + "10 HLA-B of another call gets no drug guidance" + ); + assert!( + other.contains( + "WARNING: PharmCAT's report has the outside call HLA-B *15:02/*57:01, which the consensus does not pass on" + ), + "10 the report says the HLA-B outside call is stale" + ); + assert!( + other.contains( + "HLA-B *07:02/*44:02 passed to PharmCAT from T1K (step 08); PharmCAT's report does not list it" + ), + "10 the consensus HLA-B is not shown as PharmCAT's" + ); + let held = cpic("10held", DOCS, None, Table::Text(held_back())); + assert!( + !medications(&held).contains(" CYP2D6 -- "), + "10 a held-back CYP2D6 gets no drug guidance from an older report" + ); + assert!( + held.contains( + "WARNING: PharmCAT's report has the outside call CYP2D6 *1/*3, which the consensus does not pass on" + ), + "10 the report says the CYP2D6 outside call is stale" + ); + assert!( + held.contains("Rerun step 07, then this step. No drug guidance is given for CYP2D6 here."), + "10 and says how to fix it" + ); + let gone = cpic("10gone", DOCS, None, Table::Missing); + assert!( + !medications(&gone).contains(" HLA-B -- "), + "10 with the table missing, no outside call gets drug guidance" + ); + assert!( + gone.contains("Run step 36 again, then steps 07 and 27."), + "10 with the table missing, the report says to run step 36 again" + ); +} diff --git a/rust/crates/og-steps/tests/pgx_hla_second_caller.rs b/rust/crates/og-steps/tests/pgx_hla_second_caller.rs new file mode 100644 index 000000000..07a2d484f --- /dev/null +++ b/rust/crates/og-steps/tests/pgx_hla_second_caller.rs @@ -0,0 +1,304 @@ +//! `og pgx outside-calls` with OptiType, the opt-in second HLA caller. +//! +//! With `HLA_QC=true` (bash) or `--hla_qc` (Nextflow) step 08 also runs +//! OptiType on the HLA-A, -B and -C reads, and step 36 passes HLA-A or HLA-B +//! to PharmCAT only when T1K and OptiType give the same two alleles at two +//! fields. Each case runs the verb on synthetic files: +//! +//! 1. agree: both callers agree, HLA-A and HLA-B reach PharmCAT; +//! 2. disagree: OptiType differs at HLA-B, HLA-B stays 'indeterminate', both +//! calls are printed, it is not in the outside-call file; HLA-A still +//! passes; +//! 3. homozygous: T1K's one allele and OptiType's A1 == A2 agree; +//! 4. no call: a file step 08 wrote instead of a result (`# OptiType not +//! run: ...`), both genes withheld; +//! 5. asked, missing: `--require-optitype` without `--optitype`, both +//! withheld; +//! 6. T1K alone: no OptiType asked for, T1K's call passes, as before; +//! 7. quality 0: T1K's quality-0 rule still wins over an agreement; +//! 8. concordance: the `hla-concordance` verb, HLA-A, -B, -C, yes/no; +//! 9. control: the same inputs without OptiType's result, so without the +//! agreement rule: the disagreeing HLA-B reaches the outside-call file +//! and case 2's checks report it, so case 2 can fail (the crate's own +//! planted control is the unit test +//! `optitype_agreeing_passes_and_disagreeing_withholds`). + +use std::collections::BTreeMap; +use std::fs; +use std::path::{Path, PathBuf}; + +use og_steps::pgx_rules::{hla_concordance_cli, outside_calls_cli}; + +const T1K: &str = "HLA-A\t2\tHLA-A*01:01:01\t110.5\t60\tHLA-A*26:01:01\t95.2\t60\t\n\ + HLA-B\t2\tHLA-B*57:01:01\t101.0\t60\tHLA-B*38:01:01\t88.7\t48\t\n\ + HLA-C\t1\tHLA-C*06:02:01\t150.3\t60\t.\t0\t-1\t\n"; +const T1K_HOM: &str = "HLA-A\t1\tHLA-A*02:01:01\t200.0\t60\t.\t0\t-1\t\n\ + HLA-B\t2\tHLA-B*57:01:01\t101.0\t60\tHLA-B*38:01:01\t88.7\t48\t\n"; +const T1K_Q0: &str = "HLA-A\t2\tHLA-A*01:01:01\t110.5\t60\tHLA-A*26:01:01\t95.2\t60\t\n\ + HLA-B\t2\tHLA-B*57:01:01\t101.0\t0\tHLA-B*38:01:01\t88.7\t48\t\n"; +const OT_HEAD: &str = "\tA1\tA2\tB1\tB2\tC1\tC2\tReads\tObjective\n"; +const OT_AGREE: &str = "0\tA*26:01\tA*01:01\tB*38:01\tB*57:01\tC*06:02\tC*06:02\t812\t790.4\n"; +const OT_DISAGREE: &str = "0\tA*01:01\tA*26:01\tB*58:01\tB*38:01\tC*06:02\tC*07:01\t812\t790.4\n"; +const OT_HOM: &str = "0\tA*02:01\tA*02:01\tB*57:01\tB*38:01\t\t\t500\t480.0\n"; +const OT_NONE: &str = "# OptiType not run: no reads at HLA-A, HLA-B or HLA-C in this BAM\n"; +const BOTH: &str = "T1K and OptiType (step 08), which agree at two fields"; + +/// A scratch directory of one test: emptied when made, removed when +/// dropped. +struct Scratch(PathBuf); + +impl Scratch { + fn new(name: &str) -> Self { + let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("pgx-hla-{name}")); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + Self(dir) + } + + fn file(&self, name: &str, text: &str) -> String { + let p = self.path(name); + fs::write(&p, text).unwrap(); + p + } + + fn path(&self, name: &str) -> String { + self.0.join(name).to_string_lossy().into_owned() + } +} + +impl Drop for Scratch { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } +} + +fn ot(body: &str) -> String { + format!("{OT_HEAD}{body}") +} + +/// One run of the verb: the outside-call text and the consensus rows by +/// gene, or the error when it failed. +struct Run { + calls: Option, + rows: BTreeMap>, +} + +fn run(tmp: &Scratch, t1k: &str, optitype: Option<&str>, require: bool) -> Run { + let hla = tmp.file("t1k.tsv", t1k); + let (calls, cons) = (tmp.path("calls.tsv"), tmp.path("cons.tsv")); + let _ = fs::remove_file(&calls); + let _ = fs::remove_file(&cons); + let mut args = vec![ + "--calls".to_owned(), + calls.clone(), + "--consensus".to_owned(), + cons.clone(), + "--hla".to_owned(), + hla, + ]; + if let Some(text) = optitype { + args.push("--optitype".to_owned()); + args.push(tmp.file("optitype.tsv", text)); + } + if require { + args.push("--require-optitype".to_owned()); + } + let (mut out, mut err) = (Vec::new(), Vec::new()); + if outside_calls_cli(&args, &mut out, &mut err) != 0 { + let mut rows = BTreeMap::new(); + rows.insert( + "error".to_owned(), + vec![String::from_utf8_lossy(&err).into_owned()], + ); + return Run { calls: None, rows }; + } + let rows = fs::read_to_string(&cons) + .unwrap() + .lines() + .skip(1) + .map(|l| { + let c: Vec = l.split('\t').map(str::to_owned).collect(); + (c[0].clone(), c) + }) + .collect(); + Run { + calls: Some(fs::read_to_string(&calls).unwrap()), + rows, + } +} + +impl Run { + fn passed(&self, gene: &str, dip: &str) -> bool { + self.calls + .as_deref() + .is_some_and(|c| c.contains(&format!("{gene}\t{dip}\n"))) + } + + fn row(&self, gene: &str) -> Vec { + self.rows.get(gene).cloned().unwrap_or_default() + } + + fn cell(&self, gene: &str, i: usize) -> String { + self.row(gene).get(i).cloned().unwrap_or_default() + } + + /// The gene is not in the outside-call file, its row is + /// 'indeterminate', 'no', and its reason passes `reason`. + fn withheld(&self, gene: &str, reason: impl Fn(&str) -> bool) -> bool { + let r = self.row(gene); + self.calls + .as_deref() + .is_some_and(|c| !c.lines().any(|l| l.starts_with(&format!("{gene}\t")))) + && r.get(1..3) == Some(&["indeterminate".to_owned(), "no".to_owned()][..]) + && r.get(3).is_some_and(|s| reason(s)) + } +} + +/// Case 2's checks; the names of the ones that failed. +fn disagree_checks(tmp: &Scratch, optitype: Option<&str>) -> Vec<&'static str> { + let r = run(tmp, T1K, optitype, false); + let mut failed = Vec::new(); + if !r.withheld("HLA-B", |s| s == "T1K and OptiType disagree at two fields") { + failed.push("HLA-B withheld when OptiType disagrees"); + } + if r.cell("HLA-B", 4) + != "T1K: HLA-B*57:01:01 (quality 60); HLA-B*38:01:01 (quality 48); OptiType: *58:01/*38:01" + { + failed.push("both HLA-B calls are printed"); + } + if !r.passed("HLA-A", "*01:01/*26:01") { + failed.push("HLA-A, where they agree, still passes"); + } + failed +} + +#[test] +fn case1_both_callers_agree() { + let tmp = Scratch::new("agree"); + let r = run(&tmp, T1K, Some(&ot(OT_AGREE)), false); + assert!( + r.passed("HLA-A", "*01:01/*26:01"), + "1 HLA-A passes when both agree (either order): {:?}", + r.calls + ); + assert!( + r.passed("HLA-B", "*57:01/*38:01"), + "1 HLA-B passes when both agree: {:?}", + r.calls + ); + assert_eq!(r.cell("HLA-B", 3), BOTH, "1 the reason names both callers"); + assert!( + r.calls.as_deref().is_some_and(|c| !c.contains("HLA-C")), + "1 HLA-C is never an outside call: {:?}", + r.calls + ); +} + +#[test] +fn case2_optitype_disagrees_at_hla_b() { + let tmp = Scratch::new("disagree"); + let failed = disagree_checks(&tmp, Some(&ot(OT_DISAGREE))); + assert!(failed.is_empty(), "2 failed: {failed:?}"); +} + +#[test] +fn case3_homozygous_agreement() { + let tmp = Scratch::new("hom"); + let r = run(&tmp, T1K_HOM, Some(&ot(OT_HOM)), false); + assert!( + r.passed("HLA-A", "*02:01/*02:01"), + "3 T1K's one allele agrees with OptiType's A1 == A2: {:?}", + r.calls + ); + assert_eq!( + r.cell("HLA-A", 3), + BOTH, + "3 the homozygous HLA-A row says both callers" + ); +} + +#[test] +fn case4_optitype_made_no_call() { + let tmp = Scratch::new("nocall"); + let r = run(&tmp, T1K, Some(OT_NONE), false); + for g in ["HLA-A", "HLA-B"] { + assert!( + r.withheld(g, |s| s == "OptiType, the second caller, made no call"), + "4 {g} withheld: OptiType made no call: {:?} {:?}", + r.calls, + r.row(g) + ); + } +} + +#[test] +fn case5_asked_for_without_a_result() { + let tmp = Scratch::new("missing"); + let r = run(&tmp, T1K, None, true); + for g in ["HLA-A", "HLA-B"] { + assert!( + r.withheld(g, |s| s + .starts_with("the second caller was asked for (HLA_QC)")), + "5 {g} withheld: OptiType asked for, no result: {:?} {:?}", + r.calls, + r.row(g) + ); + } +} + +#[test] +fn case6_t1k_alone_as_before() { + let tmp = Scratch::new("alone"); + let r = run(&tmp, T1K, None, false); + assert!( + r.passed("HLA-B", "*57:01/*38:01"), + "6 without OptiType, T1K's HLA-B passes: {:?}", + r.calls + ); + assert_eq!(r.cell("HLA-B", 3), "T1K (step 08)", "6 the reason is T1K's"); +} + +#[test] +fn case7_t1k_quality_0_still_withholds() { + let tmp = Scratch::new("q0"); + let r = run(&tmp, T1K_Q0, Some(&ot(OT_AGREE)), false); + assert!( + r.withheld("HLA-B", |s| s.contains("quality 0 or below")), + "7 a T1K quality-0 allele withholds HLA-B even when OptiType agrees: {:?} {:?}", + r.calls, + r.row("HLA-B") + ); +} + +#[test] +fn case8_the_concordance_table_byte_for_byte() { + let tmp = Scratch::new("concordance"); + let t1k = tmp.file("c_t1k.tsv", T1K); + let ot_path = tmp.file("c_ot.tsv", &ot(OT_DISAGREE)); + let out = tmp.path("c_out.tsv"); + let args: Vec = ["--t1k", &t1k, "--optitype", &ot_path, "--out", &out] + .map(str::to_owned) + .to_vec(); + let (mut so, mut se) = (Vec::new(), Vec::new()); + let code = hla_concordance_cli(&args, &mut so, &mut se); + let want = "Gene\tT1K\tOptiType\tAgree\tNote\n\ + HLA-A\t*01:01/*26:01\t*01:01/*26:01\tyes\t-\n\ + HLA-B\t*57:01/*38:01\t*58:01/*38:01\tno\tthe callers disagree at two fields; step 36 does not pass it to PharmCAT\n\ + HLA-C\t*06:02/*06:02\t*06:02/*07:01\tno\tthe callers disagree at two fields\n"; + let got = if code == 0 { + fs::read_to_string(&out).unwrap_or_default() + } else { + String::from_utf8_lossy(&se).into_owned() + }; + assert_eq!(got, want, "8 the concordance table, byte for byte"); +} + +#[test] +fn case9_control_without_the_agreement_rule_case2_fails() { + let tmp = Scratch::new("control"); + let failed = disagree_checks(&tmp, None); + assert!( + !failed.is_empty(), + "9 with the agreement rule disabled, case 2 rejects the disagreeing HLA-B" + ); +} diff --git a/rust/crates/og-steps/tests/pgx_outside_calls.rs b/rust/crates/og-steps/tests/pgx_outside_calls.rs new file mode 100644 index 000000000..8301355f8 --- /dev/null +++ b/rust/crates/og-steps/tests/pgx_outside_calls.rs @@ -0,0 +1,742 @@ +//! `og pgx outside-calls`: what reaches PharmCAT as an outside call, and +//! `og pgx pypgx-summary`, step 32's summary. +//! +//! CYP2D6 reaches PharmCAT only when pypgx and a second caller (Cyrius) give +//! the same diplotype and the depth check passed; HLA-A and HLA-B come from +//! T1K. Each case runs the verb on synthetic inputs (`tests/fixtures/pgx/`) +//! and compares both files it writes, byte for byte: +//! +//! 1. disagree: pypgx and Cyrius differ, no outside call, 'indeterminate'; +//! 2. one missing: pypgx only (Cyrius not run), no outside call; +//! 3. agree: both give the same diplotype, depth ok, the call is passed; +//! 4. depth: both agree, but the depth check found multi-mapped reads (4b: +//! no depth check at all); +//! 5. no call: Cyrius ran but made no call, one caller only; +//! 6. HLA: T1K's HLA-A and HLA-B, truncated to two fields; +//! 7. HLA edges: one allele (read as homozygous); an allele of quality 0. +//! +//! What step 27 (`og pgx cpic-report --consensus`) makes of these tables +//! (cases 8 to 10) is tested in `og-pgx` +//! (`tests/port_tests_pgx_outside.rs`), on the tables +//! [`consensus_tables_the_cpic_report_tests_read`] holds to this verb. +//! +//! Step 32's summary and the probable CYP2D6 call: +//! +//! 11. the star alleles of a diplotype and the defining-variant evidence of +//! one call are the crate's own unit tests (`pypgx_summary::tests`); +//! 13. pypgx alone with the depth check ok and PASS defining variants: the +//! outside-call file stays empty and step 36's row stays indeterminate +//! (the reports' probable call itself is `og summary collect`'s rule, +//! tested in `og-summary`'s `collect/pgx.rs`); +//! 14. the whole verb on pypgx results archives, pypgx's own tables (rows +//! of pypgx 0.26.0's gene and allele tables, +//! `tests/fixtures/pgx/summary/`) and an indexed VCF: GSTT1 not +//! callable on this reference (not FAILED), a gene without results +//! FAILED, pypgx's CNV call for a BAM gene only, the evidence columns +//! per gene, byte for byte what the Python it replaced wrote with the +//! real pypgx and pysam; the tables found beside the pypgx on PATH; +//! 15. both files or neither: a consensus table that cannot be written +//! leaves no outside-call file behind. +//! +//! The PATH cases run the verb in a child copy of this test binary +//! ([`og_child`]), so the child's PATH is its own. + +use std::fs; +use std::io::{Read, Write}; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; + +use og_steps::pgx_rules::outside_calls_cli; + +const HEADER: &str = "Gene\tResult\tOutside_call\tReason\tEvidence\n"; +const NO_HLA: &str = "HLA-A\tnot typed\tno\tHLA typing (step 08) did not run\t-\n\ + HLA-B\tnot typed\tno\tHLA typing (step 08) did not run\t-\n"; +const MULTIMAPPED: &str = "CYP2D6 copy number unreliable: reads are multi-mapped \ + (was this BAM aligned to a reference with ALT contigs?)"; +const NO_CYP2D6: &str = "CYP2D6\tindeterminate\tno\tno caller made a call\ + \tpypgx: not run; Cyrius: not run; depth check: not run\n"; + +fn fixtures() -> PathBuf { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../tests/fixtures/pgx") +} + +fn fx(name: &str) -> String { + fixtures().join(name).to_string_lossy().into_owned() +} + +/// A scratch directory of one test: emptied when made, removed when +/// dropped. +struct Scratch(PathBuf); + +impl Scratch { + fn new(name: &str) -> Self { + let dir = Path::new(env!("CARGO_TARGET_TMPDIR")).join(format!("pgx-outside-{name}")); + let _ = fs::remove_dir_all(&dir); + fs::create_dir_all(&dir).unwrap(); + Self(dir) + } + + fn path(&self, name: &str) -> String { + self.0.join(name).to_string_lossy().into_owned() + } +} + +impl Drop for Scratch { + fn drop(&mut self) { + let _ = fs::remove_dir_all(&self.0); + } +} + +/// The two files `og pgx outside-calls` writes for these inputs (option, +/// fixture name), or the exit code and stderr when it failed. +fn run(tag: &str, inputs: &[(&str, &str)]) -> (String, String) { + let tmp = Scratch::new(tag); + let (calls, cons) = (tmp.path("calls.tsv"), tmp.path("consensus.tsv")); + let mut args = vec![ + "--calls".to_owned(), + calls.clone(), + "--consensus".to_owned(), + cons.clone(), + ]; + for (opt, name) in inputs { + args.push(format!("--{opt}")); + args.push(fx(name)); + } + let (mut out, mut err) = (Vec::new(), Vec::new()); + let code = outside_calls_cli(&args, &mut out, &mut err); + if code != 0 { + let e = String::from_utf8_lossy(&err); + return (format!("exit {code}: {}", e.trim()), String::new()); + } + ( + fs::read_to_string(calls).unwrap(), + fs::read_to_string(cons).unwrap(), + ) +} + +fn case(desc: &str, want_calls: &str, want_consensus: &str, inputs: &[(&str, &str)]) { + let (calls, cons) = run(&desc.replace(' ', "_"), inputs); + assert_eq!(calls, want_calls, "{desc}: outside calls"); + assert_eq!(cons, want_consensus, "{desc}: consensus"); +} + +const PYPGX_12: (&str, &str) = ("pypgx", "pypgx_cyp2d6_1_2.tsv"); +const CYRIUS_12: (&str, &str) = ("cyrius", "cyrius_1_2.tsv"); +const DEPTH_OK: (&str, &str) = ("depth-check", "depth_check_ok.tsv"); + +/// Written first: a naive "take pypgx" passes pypgx's call on and fails +/// here. +#[test] +fn case1_pypgx_and_cyrius_disagree() { + case( + "1 disagree", + "", + &format!( + "{HEADER}{NO_HLA}CYP2D6\tindeterminate\tno\tpypgx and Cyrius disagree\ + \tpypgx: *1/*4; Cyrius: *1/*2; depth check: ok\n" + ), + &[("pypgx", "pypgx_cyp2d6_1_4.tsv"), CYRIUS_12, DEPTH_OK], + ); +} + +#[test] +fn case2_one_caller_missing() { + case( + "2 one missing", + "", + &format!( + "{HEADER}{NO_HLA}CYP2D6\tindeterminate\tno\tone caller only: a second caller must agree (Cyrius: setup.sh --cyrius)\ + \tpypgx: *1/*2; Cyrius: not run; depth check: ok\n" + ), + &[PYPGX_12, DEPTH_OK], + ); +} + +#[test] +fn case3_both_agree_and_the_depth_check_passed() { + case( + "3 agree", + "CYP2D6\t*1/*2\n", + &format!( + "{HEADER}{NO_HLA}CYP2D6\t*1/*2\tyes\tpypgx and Cyrius agree; the depth check passed\ + \tpypgx: *1/*2; Cyrius: *1/*2; depth check: ok\n" + ), + &[PYPGX_12, CYRIUS_12, DEPTH_OK], + ); +} + +#[test] +fn case4_depth_unreliable_or_not_run() { + case( + "4 depth unreliable", + "", + &format!( + "{HEADER}{NO_HLA}CYP2D6\tindeterminate\tno\t{MULTIMAPPED}\ + \tpypgx: *1/*2; Cyrius: *1/*2; depth check: unreliable\n" + ), + &[ + PYPGX_12, + CYRIUS_12, + ("depth-check", "depth_check_multimapped.tsv"), + ], + ); + case( + "4b no depth check", + "", + &format!( + "{HEADER}{NO_HLA}CYP2D6\tindeterminate\tno\tthe CYP2D6 depth check did not run\ + \tpypgx: *1/*2; Cyrius: *1/*2; depth check: not run\n" + ), + &[PYPGX_12, CYRIUS_12], + ); +} + +#[test] +fn case5_cyrius_made_no_call() { + case( + "5 Cyrius made no call", + "", + &format!( + "{HEADER}{NO_HLA}CYP2D6\tindeterminate\tno\tone caller only: a second caller must agree (Cyrius: setup.sh --cyrius)\ + \tpypgx: *1/*2; Cyrius: no call (None, Not_assigned_to_haplotypes); depth check: ok\n" + ), + &[PYPGX_12, ("cyrius", "cyrius_none.tsv"), DEPTH_OK], + ); +} + +#[test] +fn case6_hla_from_t1k() { + case( + "6 HLA from T1K", + "HLA-A\t*02:01/*24:02\nHLA-B\t*07:02/*44:02\n", + &format!( + "{HEADER}HLA-A\t*02:01/*24:02\tyes\tT1K (step 08)\tHLA-A*02:01:01 (quality 60); HLA-A*24:02:01 (quality 60)\n\ + HLA-B\t*07:02/*44:02\tyes\tT1K (step 08)\tHLA-B*07:02:01 (quality 60); HLA-B*44:02:01 (quality 48)\n\ + {NO_CYP2D6}" + ), + &[("hla", "t1k_genotype.tsv")], + ); +} + +#[test] +fn case7_hla_one_allele_and_an_allele_of_quality_0() { + case( + "7 HLA one allele, and an allele of quality 0", + "HLA-A\t*02:01/*02:01\n", + &format!( + "{HEADER}HLA-A\t*02:01/*02:01\tyes\tT1K (step 08): one allele, read as homozygous\tHLA-A*02:01:01 (quality 60)\n\ + HLA-B\tindeterminate\tno\tT1K gives an allele quality 0 or below (T1K says to ignore it)\ + \tHLA-B*07:02:01 (quality 60); HLA-B*44:02:01 (quality 0)\n\ + {NO_CYP2D6}" + ), + &[("hla", "t1k_genotype_homozygous_lowqual.tsv")], + ); +} + +/// The consensus tables `og-pgx`'s `tests/port_tests_pgx_outside.rs` hands +/// to `og pgx cpic-report` (cases 8 to 10), as literal text there: they +/// must be what this verb writes for those inputs. +#[test] +fn consensus_tables_the_cpic_report_tests_read() { + let held = format!( + "{HEADER}{NO_HLA}CYP2D6\tindeterminate\tno\tone caller only: a second caller must agree (Cyrius: setup.sh --cyrius)\ + \tpypgx: *1/*4; Cyrius: not run; depth check: ok\n" + ); + case( + "8 CYP2D6 held back", + "", + &held, + &[("pypgx", "pypgx_cyp2d6_1_4.tsv"), DEPTH_OK], + ); + case( + "9 HLA of PharmCAT's example", + "HLA-A\t*02:01/*24:02\nHLA-B\t*15:02/*57:01\n", + &format!( + "{HEADER}HLA-A\t*02:01/*24:02\tyes\tT1K (step 08)\tHLA-A*02:01:01 (quality 60); HLA-A*24:02:01 (quality 60)\n\ + HLA-B\t*15:02/*57:01\tyes\tT1K (step 08)\tHLA-B*15:02:01 (quality 60); HLA-B*57:01:01 (quality 48)\n\ + {NO_CYP2D6}" + ), + &[("hla", "t1k_genotype_docs_example.tsv")], + ); +} + +const SUMMARY_HEADER: [&str; 11] = [ + "Gene", + "Diplotype", + "Phenotype", + "CNV_call", + "Source", + "Status", + "Defining_variants", + "Min_GQ", + "Weakest_variant", + "Defining_filter", + "Quality", +]; + +#[test] +fn case13_pypgx_alone_passes_nothing() { + let tmp = Scratch::new("13"); + let summ = tmp.path("pypgx.tsv"); + fs::write( + &summ, + format!( + "{}\nCYP2D6\t*1/*2\tNormal Metabolizer\tNormal\tBAM\tcalled\t1/1 in the VCF\t50\ + \tchr22:42126611 C>G GQ 50 AD 20,22\tPASS\tok\n", + SUMMARY_HEADER.join("\t") + ), + ) + .unwrap(); + let (calls, cons) = (tmp.path("c.tsv"), tmp.path("k.tsv")); + let args: Vec = [ + "--calls", + &calls, + "--consensus", + &cons, + "--pypgx", + &summ, + "--depth-check", + &fx("depth_check_ok.tsv"), + ] + .map(str::to_owned) + .to_vec(); + assert_eq!( + outside_calls_cli(&args, &mut Vec::new(), &mut Vec::new()), + 0 + ); + assert_eq!( + fs::read_to_string(&calls).unwrap(), + "", + "13 the outside-call file stays empty for a probable call" + ); + let cons = fs::read_to_string(&cons).unwrap(); + let last: Vec<&str> = cons.lines().last().unwrap().split('\t').take(4).collect(); + assert_eq!( + last, + [ + "CYP2D6", + "indeterminate", + "no", + "one caller only: a second caller must agree (Cyrius: setup.sh --cyrius)" + ], + "13 step 36's row stays indeterminate, one caller only" + ); +} + +// --- 14: the verb on pypgx's results archives and tables and an indexed VCF + +/// `sample.vcf.gz` (bgzip and tabix of bcftools 1.24) holds these records. +const SUMMARY_VCF: &str = "##fileformat=VCFv4.2 +##contig= +##contig= +##FILTER= +##FORMAT= +##FORMAT= +##FORMAT= +#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tS +chr10\t94761900\t.\tC\tT\t50\tPASS\t.\tGT:GQ:AD\t0/1:60:0,31 +chr10\t94775360\t.\tAGGGGGGGG\tA\t50\tPASS\t.\tGT:GQ:AD\t0/1:40:10,10 +chr10\t94775367\t.\tA\tG\t50\tPASS\t.\tGT:GQ:AD\t0/1:48:20,19 +chr10\t94781859\t.\tG\tA,C\t50\tPASS\t.\tGT:GQ:AD\t0/1:12:26,3,. +chr22\t42126611\t.\tC\tG\t50\tRefCall\t.\tGT:GQ:AD\t0/0:50:20,22 +"; + +/// What `bin/pgx_outside_calls.py pypgx-summary` wrote for these inputs in +/// the pypgx 0.26.0 image, with the real pypgx and pysam (the parent of the +/// commit that deleted it). +fn summary_want() -> Vec> { + let rows: [&[&str]; 7] = [ + &[ + "GSTM1", + "*0/*0", + "no pypgx phenotype rule", + "WholeDel2", + "BAM", + "called", + "none", + "N/A", + "N/A", + "none", + "N/A", + ], + &[ + "GSTT1", + "N/A", + "N/A", + "N/A", + "BAM", + "not callable on this reference", + "N/A", + "N/A", + "N/A", + "N/A", + "N/A", + ], + &[ + "CYP2D6", + "*1/*2x2", + "Normal Metabolizer", + "Duplication", + "BAM", + "called", + "1/2 in the VCF", + "50", + "chr22:42126611 C>G GQ 50 AD 20,22", + "not PASS: chr22:42126611 C>G RefCall; not in the VCF: chr22:42127941 G>A", + "ok", + ], + &[ + "CYP2B6", "FAILED", "N/A", "N/A", "BAM", "failed", "N/A", "N/A", "N/A", "N/A", "N/A", + ], + &[ + "CYP2C19", + "*2/*17", + "Intermediate Metabolizer", + "N/A", + "VCF", + "called", + "3/3 in the VCF", + "12", + "chr10:94781859 G>A GQ 12 AD 26,3,.", + "PASS", + "low-confidence: check the reads", + ], + &[ + "CYP2C9", + "Indeterminate", + "Indeterminate", + "N/A", + "VCF", + "no call", + "N/A", + "N/A", + "N/A", + "N/A", + "N/A", + ], + &[ + "NUDT15", + "*1/*99", + "N/A", + "N/A", + "VCF", + "called", + "unknown", + "N/A", + "N/A", + "not in pypgx's table: *99", + "N/A", + ], + ]; + std::iter::once(&SUMMARY_HEADER[..]) + .chain(rows) + .map(|r| r.iter().map(|s| (*s).to_owned()).collect()) + .collect() +} + +const SUMMARY_LOG: &str = "Genes called: 4/7\nWARNING: star alleles resting on a variant below GQ 20, check the reads: \ + CYP2C19 *2/*17 (chr10:94781859 G>A GQ 12 AD 26,3,.)\n"; + +/// A zip file of `(name, content)` members, deflated, as Python's +/// `zipfile.ZipFile(..., ZIP_DEFLATED)` writes it. +fn write_zip(path: &Path, members: &[(&str, &str)]) { + use flate2::Compression; + use flate2::write::DeflateEncoder; + let mut out = Vec::new(); + let mut central = Vec::new(); + for (name, body) in members { + let mut crc = flate2::Crc::new(); + crc.update(body.as_bytes()); + let mut enc = DeflateEncoder::new(Vec::new(), Compression::default()); + enc.write_all(body.as_bytes()).unwrap(); + let data = enc.finish().unwrap(); + let offset = u32::try_from(out.len()).unwrap(); + let sizes = [ + crc.sum(), + u32::try_from(data.len()).unwrap(), + u32::try_from(body.len()).unwrap(), + ]; + let name_len = u16::try_from(name.len()).unwrap(); + // Local header: version 20, no flags, deflated, 1980-01-01 00:00. + out.extend_from_slice(&0x0403_4b50_u32.to_le_bytes()); + for v in [20_u16, 0, 8, 0, 0x21] { + out.extend_from_slice(&v.to_le_bytes()); + } + for v in sizes { + out.extend_from_slice(&v.to_le_bytes()); + } + out.extend_from_slice(&name_len.to_le_bytes()); + out.extend_from_slice(&0_u16.to_le_bytes()); + out.extend_from_slice(name.as_bytes()); + out.extend_from_slice(&data); + central.extend_from_slice(&0x0201_4b50_u32.to_le_bytes()); + for v in [20_u16, 20, 0, 8, 0, 0x21] { + central.extend_from_slice(&v.to_le_bytes()); + } + for v in sizes { + central.extend_from_slice(&v.to_le_bytes()); + } + for v in [name_len, 0, 0, 0, 0] { + central.extend_from_slice(&v.to_le_bytes()); + } + central.extend_from_slice(&0_u32.to_le_bytes()); + central.extend_from_slice(&offset.to_le_bytes()); + central.extend_from_slice(name.as_bytes()); + } + let cd_offset = u32::try_from(out.len()).unwrap(); + let count = u16::try_from(members.len()).unwrap(); + let cd_len = u32::try_from(central.len()).unwrap(); + out.extend_from_slice(¢ral); + out.extend_from_slice(&0x0605_4b50_u32.to_le_bytes()); + for v in [0_u16, 0, count, count] { + out.extend_from_slice(&v.to_le_bytes()); + } + out.extend_from_slice(&cd_len.to_le_bytes()); + out.extend_from_slice(&cd_offset.to_le_bytes()); + out.extend_from_slice(&0_u16.to_le_bytes()); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, out).unwrap(); +} + +/// A pypgx results archive: `metadata.txt` and the sample table `data.tsv` +/// in one folder, as pypgx's `Archive.to_file` writes it. +fn results_zip(path: &Path, genotype: &str, phenotype: &str, cnv: &str) { + write_zip( + path, + &[ + ( + "abc123/metadata.txt", + "Gene=X\nAssembly=GRCh38\nSemanticType=SampleTable[Results]\n", + ), + ( + "abc123/data.tsv", + &format!( + "\tVariantData\tCNV\tCandidates\tGenotype\tPhenotype\n\ + S\tx\t{cnv}\t*1\t{genotype}\t{phenotype}\n" + ), + ), + ], + ); +} + +const CHILD: &str = "OG_PORT_TESTS_PGX_CHILD"; +const MARK: &str = "\u{1e}og-child\u{1e}"; + +/// Not a test of its own: the child [`og`] starts runs +/// `og pgx pypgx-summary` here with the argv it was handed, then exits with +/// the verb's code. In the normal test run it returns at once. +#[test] +fn og_child() { + let Some(argv) = std::env::var_os(CHILD) else { + return; + }; + let argv: Vec = argv + .to_string_lossy() + .split('\u{1f}') + .map(str::to_owned) + .collect(); + let (mut out, mut err) = (std::io::stdout().lock(), std::io::stderr().lock()); + let _ = write!(out, "{MARK}"); + let _ = write!(err, "{MARK}"); + let code = og_steps::pypgx_summary::cli(&argv, &mut out, &mut err); + let _ = out.flush(); + let _ = err.flush(); + std::process::exit(i32::from(code)); +} + +/// `og pgx pypgx-summary ARGV` in a child copy of this test binary, with +/// `path` as its PATH: (exit code, stdout, stderr). +fn og(argv: &[String], path: &str) -> (i32, String, String) { + let child = Command::new(std::env::current_exe().unwrap()) + .args([ + "--exact", + "og_child", + "--nocapture", + "--test-threads=1", + "-q", + ]) + .env(CHILD, argv.join("\u{1f}")) + .env("PATH", path) + .stdin(Stdio::null()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .unwrap(); + let out = child.wait_with_output().unwrap(); + let code = out.status.code().unwrap_or(-1); + let (so, se) = ( + String::from_utf8_lossy(&out.stdout), + String::from_utf8_lossy(&out.stderr), + ); + let after = |s: &str| match s.split_once(MARK) { + Some((_, rest)) => rest.to_owned(), + None => panic!("the child never reached the verb: {s}"), + }; + (code, after(&so), after(&se)) +} + +/// The verb on the five results archives: (exit code, stdout, stderr, +/// rows of the summary). +fn summary( + tmp: &Scratch, + extra: &[&str], + path: Option<&str>, +) -> (i32, String, String, Vec>) { + let outbase = tmp.0.join("out"); + let out = tmp.path("summary.tsv"); + for (gene, g, p, c) in [ + ("CYP2C19", "*2/*17", "Intermediate Metabolizer", "Normal"), + ("GSTM1", "*0/*0", "Indeterminate", "WholeDel2"), + ("CYP2D6", "*1/*2x2", "Normal Metabolizer", "Duplication"), + ("CYP2C9", "Indeterminate", "Indeterminate", "NA"), + ("NUDT15", "*1/*99", "", "Normal"), + ] { + results_zip(&outbase.join(gene).join("results.zip"), g, p, c); + } + let mut argv: Vec = [ + "--outbase", + &outbase.to_string_lossy(), + "--bam-genes", + "GSTM1 GSTT1 CYP2D6 CYP2B6", + "--vcf-genes", + "CYP2C19 CYP2C9 NUDT15", + "--vcf", + &fx("summary/sample.vcf.gz"), + "--not-callable", + "GSTT1", + "--out", + &out, + ] + .map(str::to_owned) + .to_vec(); + argv.extend(extra.iter().map(|s| (*s).to_owned())); + let (code, so, se) = match path { + Some(p) => og(&argv, p), + None => { + let (mut so, mut se) = (Vec::new(), Vec::new()); + let code = og_steps::pypgx_summary::cli(&argv, &mut so, &mut se); + ( + i32::from(code), + String::from_utf8(so).unwrap(), + String::from_utf8(se).unwrap(), + ) + } + }; + let rows = fs::read_to_string(&out) + .map(|t| { + t.lines() + .map(|l| l.split('\t').map(str::to_owned).collect()) + .collect() + }) + .unwrap_or_default(); + (code, so, se, rows) +} + +#[test] +fn case14_the_fixture_vcf_holds_the_records_the_test_names() { + let raw = fs::read(fx("summary/sample.vcf.gz")).unwrap(); + let mut text = String::new(); + flate2::read::MultiGzDecoder::new(&raw[..]) + .read_to_string(&mut text) + .unwrap(); + assert_eq!( + text, SUMMARY_VCF, + "14 the fixture VCF holds the records the test names" + ); +} + +#[test] +fn case14_the_summary_row_by_row_as_the_python_wrote_it() { + let tmp = Scratch::new("14a"); + let data = fx("summary"); + let (code, so, se, rows) = summary(&tmp, &["--pypgx-data", &data], None); + assert_eq!( + (code, se.as_str()), + (0, ""), + "14 the subcommand exits 0, without a warning" + ); + assert_eq!( + rows, + summary_want(), + "14 the summary, row by row, as the Python wrote it" + ); + assert_eq!(so, SUMMARY_LOG, "14 the log names the low-confidence call"); +} + +#[test] +fn case14_pypgx_tables_beside_the_pypgx_on_path() { + // pypgx's tables found beside the pypgx on PATH, for the Python its #! + // line names (the image also has a python3.1 link to python3.12). + let tmp = Scratch::new("14b"); + let prefix = tmp.0.join("prefix"); + let data = prefix.join("lib/python3.12/site-packages/pypgx/api/data"); + fs::create_dir_all(&data).unwrap(); + for name in ["gene-table.csv", "allele-table.csv"] { + fs::copy(fixtures().join("summary").join(name), data.join(name)).unwrap(); + } + std::os::unix::fs::symlink("python3.12", prefix.join("lib/python3.1")).unwrap(); + fs::create_dir_all(prefix.join("bin")).unwrap(); + let pypgx = prefix.join("bin/pypgx"); + fs::write(&pypgx, "#!/usr/local/bin/python3.12\n").unwrap(); + { + use std::os::unix::fs::PermissionsExt; + fs::set_permissions(&pypgx, fs::Permissions::from_mode(0o755)).unwrap(); + } + let path = format!( + "{}:{}", + prefix.join("bin").display(), + std::env::var("PATH").unwrap_or_default() + ); + let (code, _, _, rows) = summary(&tmp, &[], Some(&path)); + assert_eq!( + (code, rows), + (0, summary_want()), + "14 pypgx's tables are found beside the pypgx on PATH" + ); +} + +#[test] +fn case14_without_pypgx_tables_the_evidence_says_not_looked_up() { + let tmp = Scratch::new("14c"); + // An empty folder as PATH: no pypgx, so no tables beside it. + let empty = tmp.0.join("empty-path"); + fs::create_dir_all(&empty).unwrap(); + let (code, _, se, rows) = summary(&tmp, &[], Some(&empty.to_string_lossy())); + let evidence = rows.get(3).map(|r| r[6..].to_vec()); + assert_eq!( + (code, evidence, se.contains("WARNING: no pypgx tables")), + ( + 0, + Some( + ["not looked up", "N/A", "N/A", "N/A", "N/A"] + .map(str::to_owned) + .to_vec() + ), + true + ), + "14 without pypgx's tables the evidence says not looked up, and why" + ); +} + +#[test] +fn case15_both_files_or_neither() { + let tmp = Scratch::new("15"); + let calls = tmp.path("calls.tsv"); + let args: Vec = [ + "--calls", + &calls, + "--consensus", + &tmp.path("no-such-dir/consensus.tsv"), + "--hla", + &fx("t1k_genotype.tsv"), + ] + .map(str::to_owned) + .to_vec(); + let code = outside_calls_cli(&args, &mut Vec::new(), &mut Vec::new()); + let left: Vec<_> = fs::read_dir(&tmp.0).unwrap().collect(); + assert_eq!( + (code, Path::new(&calls).exists(), left.len()), + (1, false, 0), + "15 the consensus cannot be written: exit 1, and no outside-call file" + ); +} diff --git a/tests/fixtures/pharmcat/README.md b/tests/fixtures/pharmcat/README.md index 9e15c7b00..6c0efa56c 100644 --- a/tests/fixtures/pharmcat/README.md +++ b/tests/fixtures/pharmcat/README.md @@ -1,6 +1,6 @@ # PharmCAT report fixtures -Real PharmCAT output for `tests/test_cpic_parser.py` (one `report-.json` per release in its `REAL_REPORTS`), trimmed with +Real PharmCAT output for `rust/crates/og-pgx/tests/port_tests_pgx_cpic.rs` (one `report-.json` per release in its `REAL_REPORTS`), trimmed with `og dev fixture trim-pharmcat` (formerly `trim_report.py`, which wrote the files below) to the fields `og pgx` reads (every gene with all its listed diplotypes and `relatedDrugs`; up to 4 matched annotations per drug guideline). @@ -13,7 +13,7 @@ guideline). To refresh one or add a release: take the new `report.json`, then run this with `VERSION` replaced by the PharmCAT release (for example `3.4.0`), and add -the release to `REAL_REPORTS` in `tests/test_cpic_parser.py`: +the release to `REAL_REPORTS` in `rust/crates/og-pgx/tests/port_tests_pgx_cpic.rs`: ```bash bin/og dev fixture trim-pharmcat report.json tests/fixtures/pharmcat/report-VERSION.json @@ -21,7 +21,7 @@ bin/og dev fixture trim-pharmcat report.json tests/fixtures/pharmcat/report-VERS ## Reference blocks over PharmCAT's positions longer than one base -For `tests/test_pgx_refblock_fill.py` and the e2e case `tests/e2e/pharmcat-indel-refblocks-1.sh`. +For `rust/crates/og-filters/tests/port_tests_pgx_refblock.rs` and the e2e case `tests/e2e/pharmcat-indel-refblocks-1.sh`. | File | Source | |---|---| diff --git a/tests/test_cpic_parser.py b/tests/test_cpic_parser.py deleted file mode 100644 index 9fc7d96f5..000000000 --- a/tests/test_cpic_parser.py +++ /dev/null @@ -1,819 +0,0 @@ -#!/usr/bin/env python3 -"""`og pgx cpic-report` and `og pgx step36-outside`, the one PharmCAT reader -(rust/crates/og-pgx; step 27 and the CPIC_LOOKUP module run the same binary). -The function-level rules (the phenotype classes, the layouts, the activity -score bands) are unit tests in that crate; this file runs the binary. - -Checks: - 1. the real PharmCAT report.json of the HG002 fixture, from each PharmCAT - release in REAL_REPORTS (tests/fixtures/pharmcat/report-.json), - parses to genes; a gene PharmCAT lists several possible diplotypes for, - with different phenotypes (CYP2C19, 528 of them in 3.2.0), is 'ambiguous' - and never read as its first diplotype; a report that parses to zero genes - makes `og pgx cpic-report` exit 1 with a PARSING FAILED report, - never an all-clear; - 2. on PharmCAT's own example report (pharmcat-docs-example.json) exactly 3 - genes count as non-normal (CYP2D6, HLA-B, NAT2); the 7 whose phenotype is - n/a, 'no phenotype assigned', a genotype or 'Uncertain Susceptibility' are - 'unclassified', listed on their own and given no drug list; every - non-normal gene gets a drug list, with the recommendation for its own - diplotype; synthetic genes check the fallbacks: - the report's `drugs` section first, then `relatedDrugs`, then the static - table, and otherwise a line saying it is not in the drug table; - 3. the flat (3.x), nested (2.x) and list layouts all parse; - 4. a gene PharmCAT could not call (or could not resolve) while pypgx did - prints a warning naming every drug PharmCAT links to the gene, or a note - when pypgx's call is normal, and the comparison marks it 'pypgx only'; - an unreadable pypgx summary is reported, not fatal, and makes no comparison; - 5. a gene PharmCAT leaves 'Indeterminate' at gene level while its drug - annotations give it one phenotype shows that phenotype, with a note saying - where it came from, and takes its status; with no annotation, two that - disagree, one that is Indeterminate itself, or possible diplotypes whose - guidance differs, it stays Indeterminate and unclassified; - 6. a single-position gene PharmCAT labels 'rsX reference (C)/rsX variant - (T)' matches pypgx's 'Reference/rsX', and a real difference still does - not; the comparison carries both tools' phenotypes, so a gene pypgx has - no phenotype rule for shows PharmCAT's; a CPIC recommendation over 200 - characters prints whole; CYP4F2 (and VKORC1) with no PharmCAT result - takes the genotype PharmCAT read at the *3 (-1639) position as a - labelled single-position fallback, and a position with no genotype is - 'not callable'. - 7. an MT-RNR1 outside call (step 07 makes it from step 20's chrM calls) is - not checked against step 36's consensus table, which never lists it: - with a table that passes CYP2D6 only, MT-RNR1 stays called, gets its - drug list and no stale warning, while an unconfirmed CYP2D6 outside call - is still held back; `step36-outside` lists the step 36 genes the report - calls from an outside call and never MT-RNR1. - 8. phenoconversion (opt-in --medications with assets/pgx_inhibitors.tsv): - every row of the inhibitor table has the shape the parser reads (gene, - strength, CPIC's factor for that strength, an FDA source, a CPIC source - for CYP2D6); a strong CYP2D6 inhibitor on the list turns a normal - metabolizer into 'Poor Metabolizer (phenoconverted by )' beside the - unchanged genotype phenotype, a moderate one multiplies the activity - score by 0.5, a moderate one without an activity score converts nothing, - a CYP2C19 inhibitor gets a note and no converted phenotype, a line - naming no drug of the table is listed as not checked, and a list that - names no drug leaves the phenotypes table byte for byte as without one. - Control: the same table with the inhibitor's factor set to 1 must not - give a poor metabolizer. A moderate CYP2C9 inhibitor (the FDA lists no - strong one) and a strong CYP3A inhibitor change the - Phenoconverted_phenotype column of CYP2C9 and CYP3A5 to a note, and a - list without one leaves it '-'. A CYP3A5 poor metabolizer gets no line - saying the inhibitor can lower its activity, and a line written - 'lopinavir/ritonavir' is matched as lopinavir, a boosted partner. - 9. disease-variant contraindications (--contraindications with - assets/pgx_contraindications.tsv and step 06's --clinvar-hits): every - row of the table has the shape the parser reads; a pathogenic POLG hit - adds the valproic acid WARNING, marked when valproate is on the - medication list; a benign or conflicting POLG hit, or a pathogenic hit - in a gene outside the table, adds none; without a hits table the - section says the genes were not checked; an unreadable hits table is - reported, not fatal. Control: the table without its POLG row gives no - valproic acid line for the same hit. - -Run: python3 tests/test_cpic_parser.py, with OG set to an og binary or -rust/target/release/og built (cd rust && cargo build --release -p og). -""" -import json -import os -import shutil -import subprocess -import sys -import tempfile - -REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -sys.path.insert(0, os.path.join(REPO, "tests")) -from og_binary import og_binary # noqa: E402 - -OG = og_binary() -# The genes a medication list is checked for, and the contraindication -# table's columns, as og reads them. -PHENOCONVERSION_GENES = ("CYP2D6", "CYP2C19", "CYP2C9", "CYP3A4", "CYP3A5") -CONTRA_COLUMNS = ["Gene", "Drug", "Names", "CPIC_level", "Note", "Pair_source", "Label_source", "Report_if"] - - -def og(*args): - """Run og; returns (exit code, stdout, stderr).""" - r = subprocess.run([OG, *args], capture_output=True, text=True) - return r.returncode, r.stdout, r.stderr - -FIXTURES = os.path.join(REPO, "tests", "fixtures", "pharmcat") -# One real HG002 report per PharmCAT release the parser is tested on: the -# version, and the genes it lists more than one possible diplotype for, with -# how many. -REAL_REPORTS = ( - ("3.2.0", {"CYP2C19": 528, "CYP2B6": 4}), - ("3.4.0", {"CYP2C19": 528, "CYP2B6": 4}), -) -EXAMPLE = os.path.join(REPO, "tests", "fixtures", "pharmcat", "pharmcat-docs-example.json") -FAILS = 0 - - -def check(desc, ok, detail=""): - global FAILS - print(f"[{'PASS' if ok else 'FAIL'}] {desc}{'' if ok else ' -- ' + str(detail)[:400]}") - if not ok: - FAILS += 1 - - -def dip(a1, a2, phen): - return {"sourceDiplotypes": [{"allele1": {"name": a1}, "allele2": {"name": a2}, - "label": f"{a1}/{a2}", "phenotypes": [phen]}]} - - -INHIBITORS = os.path.join(REPO, "assets", "pgx_inhibitors.tsv") -FDA_TABLE = ("https://www.fda.gov/drugs/drug-interactions-labeling/" - "healthcare-professionals-fdas-examples-drugs-interact-cyp-enzymes-and-transporter-systems") - - -def run_report(work, name, data, pypgx=None, consensus=None, medications=None, inhibitors=INHIBITORS, - contraindications=None, hits=None): - """Run the cpic-report command; returns (exit code, recommendations, phenotypes rows, comparison rows). - consensus: rows (Gene, Result, Outside_call) of a step 36 table to pass.""" - d = os.path.join(work, name) - os.makedirs(d) - rep = os.path.join(d, "report.json") - with open(rep, "w") as f: - json.dump(data, f) - args = ["pgx", "cpic-report", "--sample", name, "--report", rep, "--outdir", d] - if consensus is not None: - cp = os.path.join(d, "consensus.tsv") - with open(cp, "w") as f: - f.write("Gene\tResult\tOutside_call\tReason\tEvidence\n") - for g, res, out in consensus: - f.write(f"{g}\t{res}\t{out}\ttest\ttest\n") - args += ["--consensus", cp] - if medications is not None: - mp = os.path.join(d, "medications.txt") - with open(mp, "w") as f: - f.write(medications) - args += ["--medications", mp, "--inhibitors", inhibitors] - if contraindications is not None: - args += ["--contraindications", contraindications] - if hits is not None: - hp = os.path.join(d, "clinvar_hits.tsv") - with open(hp, "w") as f: - f.write(hits) - args += ["--clinvar-hits", hp] - comp = os.path.join(d, "comparison.tsv") - if pypgx is not None: - p = os.path.join(d, "pypgx.tsv") - with open(p, "w") as f: - f.write("Gene\tDiplotype\tPhenotype\tCNV_call\tSource\n") - for g, (dl, ph) in pypgx.items(): - f.write(f"{g}\t{dl}\t{ph}\t.\tbam\n") - args += ["--pypgx", p, "--comparison", comp] - rc, _, _ = og(*args) - rec = open(os.path.join(d, f"{name}_cpic_recommendations.txt")).read() - rows = [l.rstrip("\n").split("\t") for l in open(os.path.join(d, f"{name}_phenotypes.tsv"))][1:] - crow = [l.rstrip("\n").split("\t") for l in open(comp)][1:] if os.path.exists(comp) else [] - return rc, rec, rows, crow - - -def block(rec, gene): - """The medications block of one gene in the recommendations text.""" - lines, on = [], False - for line in rec.splitlines(): - if line.startswith(f" {gene} -- "): - on = True - if on: - lines.append(line) - if "Action:" in line: - break - return "\n".join(lines) - - -def pheno_report(nm_score="2.0"): - """A synthetic report: CYP2D6 *1/*2 normal metabolizer (activity score - nm_score, None for none) and CYP2C19 *1/*1 normal metabolizer.""" - d6 = dip("*1", "*2", "Normal Metabolizer") - d6["sourceDiplotypes"][0]["activityScore"] = nm_score - return {"pharmcatVersion": "3.4.0", "genes": {"CYP2D6": d6, "CYP2C19": dip("*1", "*1", "Normal Metabolizer")}} - - -def section(rec): - """The phenoconversion section of the recommendations text.""" - part = rec.split("Medications and Phenoconversion (CYP2D6, CYP2C19, CYP2C9, CYP3A4, CYP3A5):", 1) - return part[1].split("Affected Medications:", 1)[0] if len(part) == 2 else "" - - -def phenoconversion_checks(work): - # 8a. the inhibitor table - with open(INHIBITORS) as f: - lines = [line for line in f if not line.startswith("#")] - header = lines[0].rstrip("\n").split("\t") - check("inhibitors: the header", header == ["Drug", "Gene", "Strength", "Activity_score_factor", - "Class_source", "Rule_source"], header) - rows = [line.rstrip("\n").split("\t") for line in lines[1:] if line.strip()] - # The strengths each gene has rows for: the FDA's table lists no strong - # CYP2C9 inhibitor, and the CYP3A rows are its strong inhibitors only. - strengths = {"CYP2D6": ("strong", "moderate"), "CYP2C19": ("strong", "moderate"), - "CYP2C9": ("moderate",), "CYP3A4": ("strong",), "CYP3A5": ("strong",)} - factor = {(g, st): ("0" if st == "strong" else "0.5") if g == "CYP2D6" else "-" - for g, sts in strengths.items() for st in sts} - bad = [r for r in rows if len(r) != 6 or (r[1], r[2]) not in factor or r[3] != factor[(r[1], r[2])] - or r[0] != r[0].strip().lower() or not r[0] or r[4] != FDA_TABLE - or r[5] != ("https://cpicpgx.org/guidelines/guideline-for-codeine-and-cyp2d6/" - if r[1] == "CYP2D6" else "-")] - check("inhibitors: every row has a gene, a strength, CPIC's factor for it and its sources", not bad, bad) - keys = [(r[0], r[1]) for r in rows] - check("inhibitors: no drug twice for one gene", len(keys) == len(set(keys)), keys) - check("inhibitors: the genes of the table are the parser's", set(strengths) == set(PHENOCONVERSION_GENES), - PHENOCONVERSION_GENES) - for gene, sts in strengths.items(): - for st in sts: - check(f"inhibitors: {gene} has {st} inhibitors", any(r[1] == gene and r[2] == st for r in rows)) - check("inhibitors: CYP3A4 and CYP3A5 list the same strong inhibitors", - {r[0] for r in rows if r[1] == "CYP3A4"} == {r[0] for r in rows if r[1] == "CYP3A5"}) - _, rec, _, _ = run_report(work, "PHENOREAD", pheno_report(), medications="paroxetine\n") - check("inhibitors: the parser reads the table", "Could not read" not in section(rec) - and "phenoconverted by paroxetine" in section(rec), section(rec)) - boosted = {"elvitegravir", "indinavir", "lopinavir", "paritaprevir", "saquinavir", "tipranavir"} - check("inhibitors: each ritonavir-boosted partner the FDA classes as strong has a CYP3A4 and a CYP3A5 row", - all((d, g) in keys for d in boosted for g in ("CYP3A4", "CYP3A5")), boosted) - - # 8b. a strong CYP2D6 inhibitor, a CYP2C19 inhibitor, a name not in the table - rc, rec, rows, _ = run_report(work, "PHENO", pheno_report(), - medications="Paroxetine 20 mg daily\nfluconazole\nnotadrug\n# a comment\n\n") - got = {r[0]: r for r in rows} - check("phenoconversion: exits 0", rc == 0, rc) - check("phenoconversion: strong CYP2D6 inhibitor turns the NM into 'Poor Metabolizer (phenoconverted by paroxetine)'", - got.get("CYP2D6", [""] * 5)[4:] == ["Poor Metabolizer (phenoconverted by paroxetine)"], got.get("CYP2D6")) - check("phenoconversion: the genotype phenotype is kept beside it", - got.get("CYP2D6", [""] * 5)[2:4] == ["Normal Metabolizer", "normal"], got.get("CYP2D6")) - sec = section(rec) - check("phenoconversion: a WARNING line with both phenotypes", - "WARNING: CYP2D6 genotype phenotype Normal Metabolizer; phenoconverted phenotype " - "Poor Metabolizer (phenoconverted by paroxetine)" in sec, sec) - check("phenoconversion: CPIC's wording for a strong inhibitor", - "the CYP2D6 activity score is adjusted to 0 and the predicted phenotype is a poor metabolizer" in sec, sec) - check("phenoconversion: CYP2C19 inhibitor gets a note and no converted phenotype", - got.get("CYP2C19", [""] * 5)[4].startswith("not computed: fluconazole") - and "CYP2C19 genotype phenotype Normal Metabolizer; on fluconazole (strong CYP2C19 inhibitor)" in sec, - (got.get("CYP2C19"), sec)) - check("phenoconversion: a name not in the table is listed, not dropped", - "Not in the inhibitor table: notadrug." in sec, sec) - check("phenoconversion: the docs line says it is an aid for the clinician", - "an aid for your clinician, not advice" in sec, sec) - - # 8b'. a line that starts with a separator still names its drug - _, rec, rows, _ = run_report(work, "PHENOSEP", pheno_report(), medications=", paroxetine\n;\n") - got = {r[0]: r for r in rows} - check("phenoconversion: ', paroxetine' is read as paroxetine", - got.get("CYP2D6", [""] * 5)[4:] == ["Poor Metabolizer (phenoconverted by paroxetine)"] - and "Not in the inhibitor table" not in section(rec), (got.get("CYP2D6"), section(rec))) - - # 8c. moderate: activity score x 0.5; without a score nothing is converted - _, rec, rows, _ = run_report(work, "PHENOMOD", pheno_report("2.0"), medications="duloxetine\n") - got = {r[0]: r for r in rows} - check("phenoconversion: moderate inhibitor, AS 2 x 0.5 = 1 is an intermediate metabolizer", - got["CYP2D6"][4] == "Intermediate Metabolizer (phenoconverted by duloxetine)" - and "activity score 2 x 0.5 = 1" in section(rec), (got["CYP2D6"], section(rec))) - check("phenoconversion: a gene without an inhibitor on the list gets '-'", got["CYP2C19"][4] == "-", got["CYP2C19"]) - _, rec, rows, _ = run_report(work, "PHENONOAS", pheno_report(None), medications="duloxetine\n") - got = {r[0]: r for r in rows} - check("phenoconversion: moderate inhibitor without an activity score converts nothing", - got["CYP2D6"][4] == "not converted: the report gives no activity score", got["CYP2D6"]) - - # 8d. a list that names no drug changes nothing - _, rec0, _, _ = run_report(work, "PHENONONE", pheno_report()) - _, rec1, _, _ = run_report(work, "PHENOEMPTY", pheno_report(), medications="# nothing taken\n\n") - t0 = open(os.path.join(work, "PHENONONE", "PHENONONE_phenotypes.tsv")).read() - t1 = open(os.path.join(work, "PHENOEMPTY", "PHENOEMPTY_phenotypes.tsv")).read() - check("phenoconversion: an empty list leaves the phenotypes table as without one", t0 == t1, (t0, t1)) - check("phenoconversion: an empty list says so and converts nothing", - "The medication list names no drug: no phenotype is converted." in section(rec1) - and "phenoconverted" not in rec1.replace("PHENOEMPTY", ""), section(rec1)) - check("phenoconversion: no list, no section", "Medications and Phenoconversion" not in rec0, rec0[:600]) - - # 8e. control: the inhibitor's factor set to 1 must not make a poor metabolizer - broken = os.path.join(work, "inhibitors-factor-1.tsv") - with open(INHIBITORS) as f, open(broken, "w") as out: - for line in f: - out.write(line.replace("paroxetine\tCYP2D6\tstrong\t0\t", "paroxetine\tCYP2D6\tstrong\t1\t")) - _, _, rows, _ = run_report(work, "PHENOBROKEN", pheno_report(), medications="paroxetine\n", inhibitors=broken) - got = {r[0]: r for r in rows} - check("control: with the factor set to 1 the NM is not converted to a poor metabolizer", - not got["CYP2D6"][4].startswith("Poor Metabolizer"), got["CYP2D6"]) - # 8f. a NaN activity score converts nothing; a table row with a missing cell is reported, not a crash - _, rec, rows, _ = run_report(work, "PHENONAN", pheno_report("NaN"), medications="duloxetine\n") - got = {r[0]: r for r in rows} - check("phenoconversion: a NaN activity score converts nothing", - got["CYP2D6"][4] == "not converted: the report gives no activity score", got["CYP2D6"]) - short = os.path.join(work, "inhibitors-short-row.tsv") - with open(short, "w") as out: - out.write("Drug\tGene\tStrength\tActivity_score_factor\tClass_source\tRule_source\nparoxetine\n") - rc, rec, rows, _ = run_report(work, "PHENOSHORT", pheno_report(), medications="paroxetine\n", inhibitors=short) - check("phenoconversion: a table row with no Gene cell is reported as unreadable, the report still written", - rc == 0 and "Could not read the medication list or the inhibitor table" in section(rec), (rc, section(rec))) - blank = os.path.join(work, "inhibitors-blank-gene.tsv") - with open(INHIBITORS) as f, open(blank, "w") as out: - for line in f: - out.write(line.replace("paroxetine\tCYP2D6\t", "paroxetine\t \t")) - rc, rec, rows, _ = run_report(work, "PHENOBLANK", pheno_report(), medications="paroxetine\n", inhibitors=blank) - check("phenoconversion: a row with a blank Gene cell is reported as unreadable, never read as a known drug", - rc == 0 and "Could not read the medication list or the inhibitor table" in section(rec), (rc, section(rec))) - nofac = os.path.join(work, "inhibitors-no-factor.tsv") - with open(INHIBITORS) as f, open(nofac, "w") as out: - for line in f: - out.write(line.replace("paroxetine\tCYP2D6\tstrong\t0\t", "paroxetine\tCYP2D6\tstrong\t\t")) - rc, rec, rows, _ = run_report(work, "PHENONOFAC", pheno_report(), medications="paroxetine\n", inhibitors=nofac) - check("phenoconversion: a CYP2D6 row with an empty factor is reported as unreadable, the report still written", - rc == 0 and "Could not read the medication list or the inhibitor table" in section(rec), (rc, section(rec))) - code, _, _ = og("pgx", "cpic-report", "--sample", "X", "--report", "r.json", "--outdir", work, - "--medications", "m.txt") - check("--medications without --inhibitors is refused", code == 2, code) - - # 8g. CYP2C9 and CYP3A5: an inhibitor changes the reported column to a note, its absence leaves '-' - rep = pheno_report() - rep["genes"]["CYP2C9"] = dip("*1", "*1", "Normal Metabolizer") - rep["genes"]["CYP3A5"] = dip("*3", "*3", "Poor Metabolizer") - _, rec, rows, _ = run_report(work, "PHENO2C9", rep, medications="fluconazole\nketoconazole\n") - got = {r[0]: r for r in rows} - check("CYP2C9: a moderate inhibitor (fluconazole) changes the reported column to a note", - got["CYP2C9"][4] == "not computed: fluconazole inhibits CYP2C9; " - "CPIC gives no rule that converts the CYP2C9 phenotype", got["CYP2C9"]) - check("CYP2C9: the genotype phenotype is kept", got["CYP2C9"][2:4] == ["Normal Metabolizer", "normal"], got["CYP2C9"]) - check("CYP2C9: a WARNING line names the inhibitor", - "WARNING: CYP2C9 genotype phenotype Normal Metabolizer; on fluconazole (moderate CYP2C9 inhibitor)." - in section(rec), section(rec)) - check("CYP3A5: a strong CYP3A inhibitor (ketoconazole) changes the reported column to a note", - got["CYP3A5"][4].startswith("not computed: ketoconazole inhibits CYP3A5"), got["CYP3A5"]) - check("CYP3A5 *3/*3 (poor metabolizer): no line says the inhibitor can lower its activity; CYP2C9 keeps it", - "can lower CYP3A5 activity" not in section(rec) and "can lower CYP2C9 activity" in section(rec), - section(rec)) - _, rec, rows, _ = run_report(work, "PHENOBOOSTED", rep, medications="lopinavir/ritonavir 400/100 mg\n") - got = {r[0]: r for r in rows} - check("a boosted combination written 'lopinavir/ritonavir' is matched as lopinavir", - got["CYP3A5"][4].startswith("not computed: lopinavir inhibits CYP3A5") - and "Not in the inhibitor table" not in section(rec), (got["CYP3A5"], section(rec))) - _, rec, rows, _ = run_report(work, "PHENO2C9NONE", rep, medications="paroxetine\n") - got = {r[0]: r for r in rows} - check("CYP2C9 and CYP3A5: without an inhibitor of theirs on the list the column stays '-'", - got["CYP2C9"][4] == "-" and got["CYP3A5"][4] == "-", (got["CYP2C9"], got["CYP3A5"])) - - -CONTRA = os.path.join(REPO, "assets", "pgx_contraindications.tsv") -HITS_HEADER = "chrom\tpos\tref\talt\tgenotype\tclinvar_id\tgeneinfo\tclnsig\tclnrevstat\n" - - -def hit(gene, clnsig, pos="89300000", gt="0/1"): - """One synthetic row of step 06's hits table (no one's variant).""" - return f"chr15\t{pos}\tC\tT\t{gt}\t9900001\t{gene}:1\t{clnsig}\tcriteria_provided,_single_submitter\n" - - -def contra_section(rec): - part = rec.split("Drug Contraindications From Disease Variants (ClinVar hits, step 06):", 1) - return part[1].split("Uncallable Genes:", 1)[0] if len(part) == 2 else "" - - -def contraindication_checks(work): - # 9a. the table - with open(CONTRA) as f: - lines = [line for line in f if not line.startswith("#")] - header = lines[0].rstrip("\n").split("\t") - check("contraindications: the header", header == CONTRA_COLUMNS, header) - rows = [line.rstrip("\n").split("\t") for line in lines[1:] if line.strip()] - bad = [r for r in rows if len(r) != 8 or not all(c.strip() for c in r) - or r[3] not in ("A", "B", "A/B") - or r[5] != f"https://api.cpicpgx.org/v1/pair_view?genesymbol=eq.{r[0]}" - or not r[6].startswith("https://dailymed.nlm.nih.gov/dailymed/drugInfo.cfm?setid=") - or r[7] not in ("any", "biallelic") - or any(n != n.strip().lower() or not n for n in r[2].split(",")) - or r[1].split()[0] not in r[2].split(",")] - check("contraindications: every row has a gene, a drug, its names, a CPIC level, a note and its sources", - not bad, bad) - keys = [(r[0], r[1]) for r in rows] - check("contraindications: no gene-drug pair twice", len(keys) == len(set(keys)), keys) - check("contraindications: POLG with valproic acid, the urea-cycle genes and HPRT1 are there", - {("POLG", "valproic acid"), ("ASL", "valproic acid"), ("ASS1", "valproic acid"), ("CPS1", "valproic acid"), - ("NAGS", "valproic acid"), ("OTC", "valproic acid"), ("HPRT1", "mycophenolic acid")} <= set(keys), keys) - check("contraindications: SCN1A is left out (no label or guideline names it)", - not any(r[0] == "SCN1A" for r in rows), keys) - _, rec, _, _ = run_report(work, "CONTRAREAD", pheno_report(), contraindications=CONTRA) - check("contraindications: the parser reads the table", "Could not read" not in contra_section(rec) - and "Genes it would check: " + ", ".join(dict.fromkeys(r[0] for r in rows)) + "." in contra_section(rec), - contra_section(rec)) - - # 9b. a pathogenic POLG hit adds the valproate line; benign, conflicting or another gene do not - rep = pheno_report() - rc, rec, _, _ = run_report(work, "CONTRAPLP", rep, contraindications=CONTRA, - hits=HITS_HEADER + hit("POLG", "Pathogenic")) - sec = contra_section(rec) - check("contraindications: exits 0", rc == 0, rc) - check("contraindications: a pathogenic POLG hit adds the valproic acid WARNING", - "WARNING: valproic acid -- POLG chr15:89300000 C>T, genotype 0/1, ClinVar Pathogenic." in sec, sec) - check("contraindications: the line carries the label's note, the CPIC level and the label", - "mitochondrial disorders caused by POLG mutations" in sec - and "(provisional CPIC level A/B: no CPIC guideline covers this pair)" in sec - and "dailymed.nlm.nih.gov" in sec, sec) - check("contraindications: the section says to review it with a clinician", - "Review it with your clinician" in sec, sec) - _, rec, _, _ = run_report(work, "CONTRALP", rep, contraindications=CONTRA, - hits=HITS_HEADER + hit("POLG", "Pathogenic/Likely_pathogenic", gt="1/1")) - check("contraindications: Pathogenic/Likely_pathogenic counts", "WARNING: valproic acid -- POLG" in contra_section(rec), - contra_section(rec)) - for name, sig in (("CONTRABEN", "Benign"), ("CONTRACONF", "Conflicting_classifications_of_pathogenicity"), - ("CONTRARISK", "risk_factor")): - _, rec, _, _ = run_report(work, name, rep, contraindications=CONTRA, hits=HITS_HEADER + hit("POLG", sig)) - sec = contra_section(rec) - check(f"contraindications: a POLG hit classed {sig} adds no drug line", - "WARNING" not in sec and "No pathogenic or likely pathogenic ClinVar hit in POLG" in sec, sec) - _, rec, _, _ = run_report(work, "CONTRAOTHER", rep, contraindications=CONTRA, - hits=HITS_HEADER + hit("BRCA2", "Pathogenic")) - check("contraindications: a pathogenic hit in a gene outside the table adds no drug line", - "WARNING" not in contra_section(rec), contra_section(rec)) - _, rec, _, _ = run_report(work, "CONTRAMED", rep, contraindications=CONTRA, medications="Valproate 500 mg\n", - hits=HITS_HEADER + hit("POLG", "Likely_pathogenic")) - check("contraindications: valproate on the medication list is marked", - "ClinVar Likely_pathogenic; ON THE MEDICATION LIST." in contra_section(rec), contra_section(rec)) - _, rec, _, _ = run_report(work, "CONTRANOTON", rep, contraindications=CONTRA, medications="paroxetine\n", - hits=HITS_HEADER + hit("POLG", "Pathogenic")) - check("contraindications: a drug not on the list is not marked", - "WARNING: valproic acid" in contra_section(rec) and "ON THE MEDICATION LIST" not in rec, contra_section(rec)) - - # 9c. no hits table, an unreadable one, no table - _, rec, _, _ = run_report(work, "CONTRANOHITS", rep, contraindications=CONTRA) - check("contraindications: without a hits table the genes are listed as not checked", - "Not checked: no ClinVar hits table" in contra_section(rec) and "POLG" in contra_section(rec), - contra_section(rec)) - rc, rec, _, _ = run_report(work, "CONTRABADHITS", rep, contraindications=CONTRA, hits="nothing\there\n") - check("contraindications: an unreadable hits table is reported, the report still written", - rc == 0 and "Could not read the ClinVar hits table" in contra_section(rec), (rc, contra_section(rec))) - _, rec, _, _ = run_report(work, "CONTRANONE", rep) - check("contraindications: without the table there is no section", "Drug Contraindications" not in rec, rec[:600]) - code, _, _ = og("pgx", "cpic-report", "--sample", "X", "--report", "r.json", "--outdir", work, - "--clinvar-hits", "h.tsv") - check("--clinvar-hits without --contraindications is refused", code == 2, code) - - # 9d. control: the same hit with the POLG row taken out of the table gives no valproate line - nopolg = os.path.join(work, "contraindications-no-polg.tsv") - with open(CONTRA) as f, open(nopolg, "w") as out: - out.writelines(line for line in f if not line.startswith("POLG\t")) - _, rec, _, _ = run_report(work, "CONTRACTRL", rep, contraindications=nopolg, - hits=HITS_HEADER + hit("POLG", "Pathogenic")) - check("control: without the POLG row the same hit gives no valproic acid line", - "valproic acid" not in contra_section(rec), contra_section(rec)) - - -def main(): - work = tempfile.mkdtemp() - try: - # 1. the real reports, one per PharmCAT release - for version, ambiguous in REAL_REPORTS: - with open(os.path.join(FIXTURES, f"report-{version}.json")) as f: - real = json.load(f) - tag = f"{version}:" - rc, rec, rows, _ = run_report(work, f"HG002_{version}", real) - print(f"fixture: PharmCAT {real.get('pharmcatVersion')}, {len(rows)} genes: " - + ", ".join(f"{r[0]} {r[1]} ({r[3]})" for r in rows)) - check(f"{tag} the report is PharmCAT {version}", real.get("pharmcatVersion") == version, real.get("pharmcatVersion")) - check(f"{tag} cpic-report on the HG002 report exits 0", rc == 0, rc) - check(f"{tag} the HG002 report yields genes", len(rows) >= 10, len(rows)) - check(f"{tag} the HG002 report calls at least one gene", any(r[3] != "not called" for r in rows), rows) - check(f"{tag} one phenotypes row per gene, with a Status", - len({r[0] for r in rows}) == len(rows) and all(len(r) == 4 for r in rows), rows[:3]) - by = {r[0]: r for r in rows} - several = {r[0]: int(r[1].rsplit("(1 of ", 1)[1].split()[0]) for r in rows if " possible)" in r[1]} - check(f"{tag} the genes with more than one possible diplotype are {sorted(ambiguous)}", - several == ambiguous, several) - for gene, n in sorted(ambiguous.items()): - g = by.get(gene) - check(f"{tag} HG002 {gene}: {n} possible diplotypes, status ambiguous", - g is not None and g[3] == "ambiguous", g) - check(f"{tag} HG002 {gene}: not reported as its first diplotype's phenotype", - g is not None and g[2].startswith("ambiguous: ") and f"(1 of {n} possible)" in g[1], g) - check(f"{tag} the recommendations list {gene} with the ambiguous genes", - "Genes With More Than One Possible Result:" in rec and f" {gene} -- {n} possible diplotypes" in rec, - rec[:2000]) - - with open(EXAMPLE) as f: - example = json.load(f) - rc, rec, erows, _ = run_report(work, "EXAMPLE", example) - check("cpic-report on the example report exits 0", rc == 0, rc) - nonnormal = [r for r in erows if r[3] == "non-normal"] - check("example report: 3 genes with a non-normal phenotype (CYP2D6, HLA-B, NAT2)", - sorted(r[0] for r in nonnormal) == ["CYP2D6", "HLA-B", "NAT2"], [r[0] for r in nonnormal]) - unclassified = ["CACNA1S", "CFTR", "CYP4F2", "IFNL3", "MT-RNR1", "RYR1", "VKORC1"] - check("example report: n/a, 'no phenotype assigned', a genotype and 'Uncertain Susceptibility' are unclassified", - sorted(r[0] for r in erows if r[3] == "unclassified") == unclassified, erows) - meds = rec.split("Affected Medications:")[1].split("\n\n", 1)[1].split("Called Genes Without")[0] - check("example: the medications section lists only the 3 non-normal genes", - sorted(l.split(" -- ")[0].strip() for l in meds.splitlines() if l.startswith(" ") and " -- " in l - and not l.startswith(" ")) == ["CYP2D6", "HLA-B", "NAT2"], meds[:1500]) - own = rec.split("Called Genes Without a Function Phenotype:")[-1].split("Uncallable Genes:")[0] - check("example: the unclassified genes are listed in their own section, VKORC1 with its genotype", - all(f" {g} -- " in own for g in unclassified) and " VKORC1 -- -1639 GG (" in own, own) - check("example: the phenotypes table says unclassified for them", - sorted(r[0] for r in erows if r[3] == "unclassified") == unclassified, erows) - ga = json.loads(json.dumps(example)) - ga["genes"]["VKORC1"]["sourceDiplotypes"][0].update( - label="rs9923231 reference (C)/rs9923231 variant (T)", phenotypes=["-1639 GA"]) - rc, grec, grows, _ = run_report(work, "EXAMPLE_GA", ga) - check("example with VKORC1 -1639 GA: VKORC1 counts as non-normal", - [r[3] for r in grows if r[0] == "VKORC1"] == ["non-normal"], [r for r in grows if r[0] == "VKORC1"]) - check("example with VKORC1 -1639 GA: its drug list names warfarin", - rc == 0 and " Drugs" in block(grec, "VKORC1") and "warfarin" in block(grec, "VKORC1"), - block(grec, "VKORC1")) - for gene, _, phen, _ in nonnormal: - b = block(rec, gene) - check(f"example {gene} ({phen}): a drug list", " Drugs" in b and "not in the drug table" not in b, b) - check("example CYP2D6 *1/*3 (Intermediate): codeine's recommendation for that diplotype", - "- codeine [CPIC, Moderate]: Use codeine label recommended age- or weight-specific dosing. " - "If no response and opioid use is warranted, consider a non-tramadol opioid." in block(rec, "CYP2D6"), - block(rec, "CYP2D6")) - - stripped = dict(real, genes={g: {"geneSymbol": g} for g in real.get("genes", {})}) - rc, rec, rows, _ = run_report(work, "EMPTY", stripped) - check("a report whose genes carry no diplotype exits 1", rc == 1, rc) - check("and writes PARSING FAILED, never an all-clear", - "PARSING FAILED" in rec and "all genes were successfully called" not in rec and rows == [], rec[:200]) - rc, rec, _, _ = run_report(work, "NOKEYS", {"metadata": {}}) - check("a report with no gene section at all exits 1", rc == 1 and "PARSING FAILED" in rec, rc) - - # 2. drug-list fallbacks - data = {"genes": { - "CYP2C19": dip("*2", "*2", "Poor Metabolizer"), - "ABCG2": dict(dip("rs2231142 variant (T)", "rs2231142 variant (T)", "Poor Function"), - relatedDrugs=[{"name": "rosuvastatin"}]), - "NEWGENE": dip("*1", "*7", "Decreased Function"), - "CYP4F2": dip("*1", "*3", "Decreased Function"), - "SLCO1B1": dip("*1", "*1", "Normal Function")}, - "drugs": {"CPIC Guideline Annotation": {"warfarin": {"name": "warfarin", "guidelines": [{"annotations": [ - {"phenotypes": {"CYP4F2": "Decreased Function"}, "classification": "Optional", - "drugRecommendation": "Increase the dose by 5-10%."}]}]}}}} - rc, rec, rows, _ = run_report(work, "FALLBACK", data) - check("fallbacks: exits 0", rc == 0, rc) - check("CYP4F2: the report's own guidance (warfarin, CPIC Optional)", - "- warfarin [CPIC, Optional]: Increase the dose by 5-10%." in block(rec, "CYP4F2"), block(rec, "CYP4F2")) - check("ABCG2: relatedDrugs when the report has no guidance for it", - "Drugs PharmCAT links to ABCG2" in block(rec, "ABCG2") and "rosuvastatin" in block(rec, "ABCG2")) - check("CYP2C19: the static table when the report names no drug", - "pipeline fallback table" in block(rec, "CYP2C19") and "clopidogrel" in block(rec, "CYP2C19")) - check("NEWGENE: a line saying it is not in the drug table, not silence", - "NEWGENE has a non-normal phenotype but is not in the drug table" in block(rec, "NEWGENE"), rec) - check("SLCO1B1 normal: no medications block", block(rec, "SLCO1B1") == "") - - # PharmCAT lists an annotation for every diplotype the sample may have: - # the recommendation shown must be the one for the called diplotype. - ann = lambda label, phen, text: {"phenotypes": {"CYP2C19": phen}, "classification": "Strong", - "drugRecommendation": text, - "genotypes": [{"diplotypes": [{"gene": "CYP2C19", "label": label}]}]} - data = {"genes": {"CYP2C19": dip("*1", "*2", "Intermediate Metabolizer")}, - "drugs": {"CPIC Guideline Annotation": {"clopidogrel": {"name": "clopidogrel", "guidelines": [{"annotations": [ - ann("*1/*1", "Normal Metabolizer", "Standard dose."), - ann("*1/*2", "Intermediate Metabolizer", "Use an alternative antiplatelet."), - ann("*2/*2", "Poor Metabolizer", "Avoid clopidogrel.")]}]}}}} - rc, rec, _, _ = run_report(work, "MATCH", data) - b = block(rec, "CYP2C19") - check("the recommendation is the called diplotype's (*1/*2), not another possible one's", - "- clopidogrel [CPIC, Strong]: Use an alternative antiplatelet." in b - and "Standard dose." not in b and "Avoid clopidogrel." not in b, b) - - # The phenotype fallback (annotations naming no diplotype) matches the - # called phenotype exactly: "Intermediate Metabolizer" guidance is not - # the guidance for "Likely Intermediate Metabolizer". - pann = lambda phen, text: {"phenotypes": {"CYP2C19": phen}, "classification": "Strong", - "drugRecommendation": text} - data = {"genes": {"CYP2C19": dip("*1", "*9", "Likely Intermediate Metabolizer")}, - "drugs": {"CPIC Guideline Annotation": {"clopidogrel": {"name": "clopidogrel", "guidelines": [{"annotations": [ - pann("Intermediate Metabolizer", "Guidance for IM."), - pann("Likely Intermediate Metabolizer", "Guidance for likely IM.")]}]}}}} - rc, rec, _, _ = run_report(work, "PHENMATCH", data) - b = block(rec, "CYP2C19") - check("phenotype fallback: the exact phenotype's guidance only, not a substring match", - "Guidance for likely IM." in b and "Guidance for IM." not in b, b) - - # 3. layouts - g2 = {"CYP2C19": dip("*1", "*2", "Intermediate Metabolizer"), "CYP2D6": dip("*1", "*1", "Normal Metabolizer")} - for label, d, want in ( - ("flat 3.x", {"genes": g2}, 2), - ("nested 2.x", {"genes": {"CPIC": g2, "DPWG": g2}}, 2), - ("list", {"genes": [dict(g2["CYP2C19"], gene="CYP2C19")]}, 1), - ("recommendationDiplotypes", {"genes": {"DPYD": {"recommendationDiplotypes": [ - {"label": "c.1905+1G>A/Reference", "phenotypes": ["Intermediate Metabolizer"]}]}}}, 1)): - rc, _, rows, _ = run_report(work, "LAYOUT" + str(len(label)), d) - check(f"layout {label}: {want} gene(s)", rc == 0 and len(rows) == want, (rc, rows)) - - # 4. PharmCAT no result, pypgx call - data = {"genes": {"CYP2D6": dict(dip("Unknown", "Unknown", "No Result"), - relatedDrugs=[{"name": "codeine"}, {"name": "tramadol"}]), - "CYP2C9": dip("Unknown", "Unknown", "No Result"), - "CYP2C19": dip("*1", "*1", "Normal Metabolizer")}, - # a two-gene annotation that names CYP2D6 for one drug only - "drugs": {"CPIC Guideline Annotation": {"amitriptyline": {"name": "amitriptyline", "guidelines": [ - {"annotations": [{"phenotypes": {"CYP2C19": "Normal Metabolizer", "CYP2D6": "No Result"}, - "drugRecommendation": "x", "classification": "Optional"}]}]}}}} - rc, rec, rows, comp = run_report(work, "PYPGX", data, - pypgx={"CYP2D6": ("*1/*4", "Intermediate Metabolizer"), - "CYP2C9": ("*1/*1", "Normal Metabolizer"), - "CYP2C19": ("*1/*1", "Normal Metabolizer")}) - check("pypgx: exits 0", rc == 0, rc) - check("pypgx: the warning names CYP2D6 and pypgx's call", - "WARNING: PharmCAT has no result for CYP2D6, but pypgx (step 32) called *1/*4" in rec, rec) - check("pypgx: the warning lists every drug PharmCAT links to CYP2D6, not only the one another gene matched", - "Drugs affected by CYP2D6: codeine, tramadol" in rec, rec) - check("pypgx: a normal pypgx call is a NOTE, not a WARNING", - "NOTE: PharmCAT has no result for CYP2C9; pypgx (step 32) called *1/*1 (Normal Metabolizer)." in rec - and "WARNING: PharmCAT has no result for CYP2C9" not in rec, rec) - check("pypgx: CYP2D6 is in the uncallable list", "CYP2D6 -- No Result (not callable" in rec) - check("pypgx: comparison marks CYP2D6 'pypgx only' and CYP2C19 concordant", - ["CYP2D6", "Unknown/Unknown", "*1/*4", "pypgx only", "pypgx only", - "No Result", "Intermediate Metabolizer"] in comp - and ["CYP2C19", "*1/*1", "*1/*1", "Yes", "both", "Normal Metabolizer", "Normal Metabolizer"] in comp, - comp) - - # an ambiguous PharmCAT gene is not compared as if it were its first diplotype - data = {"genes": {"CYP2C19": {"sourceDiplotypes": [ - {"allele1": {"name": "*1"}, "allele2": {"name": "*1"}, "label": "*1/*1", "phenotypes": ["Normal Metabolizer"]}, - {"allele1": {"name": "*1"}, "allele2": {"name": "*2"}, "label": "*1/*2", "phenotypes": ["Intermediate Metabolizer"]}]}}} - rc, rec, rows, comp = run_report(work, "AMBIG", data, pypgx={"CYP2C19": ("*1/*2", "Intermediate Metabolizer")}) - check("ambiguous: the phenotypes row says ambiguous", rows and rows[0][3] == "ambiguous", rows) - check("ambiguous: compared as ambiguous, pypgx's call warned about", - comp and comp[0][1] == "ambiguous (2 possible diplotypes)" and comp[0][3] == "pypgx only" - and "WARNING: PharmCAT has no single result (2 possible diplotypes) for CYP2C19" in rec, (comp, rec)) - - # an unreadable pypgx summary: the CPIC report is still written, it says - # the comparison could not be made, and no comparison file is written - d = os.path.join(work, "BADPYPGX") - os.makedirs(d) - rep, bad, comp = (os.path.join(d, n) for n in ("report.json", "pypgx.tsv", "comparison.tsv")) - with open(rep, "w") as f: - json.dump({"genes": {"CYP2C19": dip("*2", "*2", "Poor Metabolizer")}}, f) - with open(bad, "wb") as f: - f.write(b"Gene\tDiplotype\n\xff\xfe\x00broken\n") - with open(comp, "w") as f: # a comparison left by an earlier run - f.write("Gene\tPharmCAT_diplotype\tpypgx_diplotype\tMatch\tCalled_by\nCYP2C19\t*1/*1\t*1/*1\tYes\tboth\n") - rc, _, _ = og("pgx", "cpic-report", "--sample", "BADPYPGX", "--report", rep, "--outdir", d, - "--pypgx", bad, "--comparison", comp) - rp = os.path.join(d, "BADPYPGX_cpic_recommendations.txt") - rec = open(rp).read() if os.path.exists(rp) else "" - check("unreadable pypgx summary: exits 0 with the CPIC report written", rc == 0 and "CYP2C19 -- Poor" in rec, rc) - check("unreadable pypgx summary: the report says no comparison was made, and an earlier one is removed", - "Could not read the pypgx summary" in rec and not os.path.exists(comp), rec[-600:]) - - # 5. Indeterminate at gene level, a phenotype in the drug annotations - iann = lambda phen, label="*1/*80", rec=None: {"phenotypes": {"UGT1A1": phen}, "classification": "Strong", - "drugRecommendation": rec or f"Guidance for {phen}.", - "genotypes": [{"diplotypes": [{"gene": "UGT1A1", "label": label}]}]} - drugs = lambda *anns: {"CPIC Guideline Annotation": {"atazanavir": {"name": "atazanavir", "guidelines": [ - {"annotations": list(anns)}]}}} - gene = {"UGT1A1": dict(dip("*1", "*80", "Indeterminate"), relatedDrugs=[{"name": "atazanavir"}])} - note = "[gene level Indeterminate; phenotype from PharmCAT's drug annotations]" - rc, rec, rows, _ = run_report(work, "INDET", {"genes": gene, "drugs": drugs(iann("Intermediate Metabolizer"))}) - check("Indeterminate gene, one drug-level phenotype: exits 0", rc == 0, rc) - check("Indeterminate gene: the table shows the drug-level phenotype with its note, and its status", - rows == [["UGT1A1", "*1/*80", f"Intermediate Metabolizer {note}", "non-normal"]], rows) - check("Indeterminate gene: the gene results show the note", - f"Intermediate Metabolizer {note}" in rec.split("Affected Medications:")[0], rec[:1500]) - check("Indeterminate gene: listed with its drugs, the guidance for the called diplotype", - f" UGT1A1 -- Intermediate Metabolizer {note}:" in rec - and "- atazanavir [CPIC, Strong]: Guidance for Intermediate Metabolizer." in block(rec, "UGT1A1"), - block(rec, "UGT1A1")) - check("Indeterminate gene: not in the section of genes without a function phenotype", - "Called Genes Without a Function Phenotype:" not in rec, rec) - for label, d in (("no drug annotation", {}), - ("two drug annotations that disagree", - drugs(iann("Intermediate Metabolizer"), iann("Poor Metabolizer"))), - ("a drug annotation that is Indeterminate too", drugs(iann("Indeterminate"))), - ("one drug annotation with a phenotype and one Indeterminate", - drugs(iann("Intermediate Metabolizer"), iann("Indeterminate")))): - rc, rec, rows, _ = run_report(work, "INDET" + str(len(label)), dict({"genes": gene}, **({"drugs": d} if d else {}))) - check(f"Indeterminate gene, {label}: stays Indeterminate and unclassified, no note", - rc == 0 and rows == [["UGT1A1", "*1/*80", "Indeterminate", "unclassified"]] and note not in rec, rows) - rc, rec, rows, _ = run_report(work, "INDETNORMAL", {"genes": gene, "drugs": drugs(iann("Normal Metabolizer"))}) - check("Indeterminate gene, drug level Normal Metabolizer: normal, with the note", - rows == [["UGT1A1", "*1/*80", f"Normal Metabolizer {note}", "normal"]], rows) - # two possible diplotypes, both Indeterminate, whose drug annotations - # disagree: the gene is undecided and is not read as its first one - two = dip("*1", "*80", "Indeterminate") - two["sourceDiplotypes"] += dip("*80", "*80", "Indeterminate")["sourceDiplotypes"] - rc, rec, rows, _ = run_report(work, "INDETTWO", {"genes": {"UGT1A1": two}, "drugs": drugs( - iann("Intermediate Metabolizer", "*1/*80"), iann("Normal Metabolizer", "*80/*80"))}) - check("Indeterminate gene, two possible diplotypes whose drug annotations disagree: stays Indeterminate, no note", - rc == 0 and rows == [["UGT1A1", "*1/*80 (1 of 2 possible)", "Indeterminate", "unclassified"]] - and note not in rec, rows) - # the same phenotype for both, but different guidance: the drug list - # would show the first diplotype's, so the gene stays undecided - rc, rec, rows, _ = run_report(work, "INDETTWOREC", {"genes": {"UGT1A1": two}, "drugs": drugs( - iann("Intermediate Metabolizer", "*1/*80"), - iann("Intermediate Metabolizer", "*80/*80", "Other guidance."))}) - check("Indeterminate gene, two possible diplotypes with one phenotype but different guidance: stays Indeterminate", - rc == 0 and rows == [["UGT1A1", "*1/*80 (1 of 2 possible)", "Indeterminate", "unclassified"]] - and note not in rec, rows) - # the same phenotype and the same guidance for both: it takes the phenotype - rc, rec, rows, _ = run_report(work, "INDETTWOSAME", {"genes": {"UGT1A1": two}, "drugs": drugs( - iann("Intermediate Metabolizer", "*1/*80"), iann("Intermediate Metabolizer", "*80/*80"))}) - check("Indeterminate gene, two possible diplotypes with the same phenotype and guidance: takes it, with the note", - rows == [["UGT1A1", "*1/*80 (1 of 2 possible)", f"Intermediate Metabolizer {note}", "non-normal"]], rows) - - # 6. label-only differences, phenotypes, long text, single-position fallback - vk = "rs9923231 reference (C)/rs9923231 variant (T)" - data = {"genes": {"VKORC1": {"sourceDiplotypes": [{"label": vk, "phenotypes": ["-1639 GA"]}]}, - "IFNL3": {"sourceDiplotypes": [{"label": "rs12979860 variant (T)/rs12979860 variant (T)", - "phenotypes": ["unfavorable response genotype"]}]}}} - rc, rec, _, comp = run_report(work, "LABELS", data, pypgx={ - "VKORC1": ("Reference/rs9923231", "no pypgx phenotype rule"), - "IFNL3": ("Reference/rs12979860", "Unfavorable Response")}) - check("VKORC1 'rsX reference (C)/rsX variant (T)' against pypgx 'Reference/rsX' is a Match", - ["VKORC1", vk, "Reference/rs9923231", "Yes", "both", "-1639 GA", "no pypgx phenotype rule"] in comp, comp) - check("IFNL3 'rsX variant (T)/rsX variant (T)' against pypgx 'Reference/rsX' is still a conflict", - [r[3] for r in comp if r[0] == "IFNL3"] == ["No"], comp) - check("the comparison shows PharmCAT's phenotype beside pypgx's 'no pypgx phenotype rule', no Indeterminate", - all("Indeterminate" not in "\t".join(r) for r in comp), comp) - with open(os.path.join(work, "LABELS", "comparison.tsv")) as f: - head = f.readline().rstrip("\n").split("\t") - check("the comparison header ends with the two phenotype columns", - head == ["Gene", "PharmCAT_diplotype", "pypgx_diplotype", "Match", "Called_by", - "PharmCAT_phenotype", "pypgx_phenotype"], head) - - long = ("Initiate therapy with 50% of the starting dose followed by titration of dose based on " - "toxicity or therapeutic drug monitoring. " * 4 + "If plasma concentrations are not available, " - "consider a lower dose of a tricyclic in this patient.") - data = {"genes": {"CYP2C19": dip("*2", "*2", "Poor Metabolizer")}, - "drugs": {"CPIC Guideline Annotation": {"amitriptyline": {"name": "amitriptyline", "guidelines": [ - {"annotations": [{"phenotypes": {"CYP2C19": "Poor Metabolizer"}, "classification": "Optional", - "drugRecommendation": long}]}]}}}} - rc, rec, _, _ = run_report(work, "LONG", data) - check(f"a recommendation of {len(long)} characters prints whole", - len(long) > 400 and f"- amitriptyline [CPIC, Optional]: {long}" in rec and "..." not in block(rec, "CYP2C19"), - block(rec, "CYP2C19")) - - def variants(gene, call, rsid): # call before rsid: an rsID next to a genotype reads as a leak - return {"sourceDiplotypes": [{"allele1": {"name": "Unknown"}, "allele2": {"name": "Unknown"}, - "label": "Unknown/Unknown", "phenotypes": []}], - "variants": [{"position": 1, "dbSnpId": "rs0000001", "call": "G/A"}, - {"position": 2, "dbSnpId": rsid, "call": call, "referenceAllele": "C"}]} - fb = "Single-Position Fallback (not a PharmCAT call):" - rc, rec, rows, _ = run_report(work, "CYP4F2HET", {"genes": {"CYP4F2": variants("CYP4F2", "C/T", "rs2108622")}}) - tail = rec.split(fb)[-1] - check("CYP4F2 No Result with C/T at rs2108622: the fallback names the genotype and the *3 rule, labelled", - rc == 0 and fb in rec - and "WARNING: PharmCAT has no result for CYP4F2; single-position fallback, not a PharmCAT call: " - "rs2108622 reads C/T, one copy of the *3 variant (T, p.V433M)." in tail - and "Warfarin (CPIC): the CPIC warfarin dosing algorithm" in tail and "5-10%" in tail, rec[-1500:]) - check("CYP4F2 fallback: PharmCAT's own result stays not called in the phenotypes table", - rows == [["CYP4F2", "Unknown/Unknown", "No Result", "not called"]], rows) - rc, rec, _, _ = run_report(work, "CYP4F2HOM", {"genes": {"CYP4F2": variants("CYP4F2", "T|T", "rs2108622")}}) - check("CYP4F2 T|T (phased): two copies", "rs2108622 reads T|T, two copies of the *3 variant" in rec, rec[-1200:]) - rc, rec, _, _ = run_report(work, "CYP4F2REF", {"genes": {"CYP4F2": variants("CYP4F2", "C/C", "rs2108622")}}) - check("CYP4F2 C/C: no copy, the rule does not apply, and no WARNING", - "rs2108622 reads C/C, no copy of the *3 variant (T, p.V433M). The CPIC warfarin rule for this position does not apply." - in rec and "WARNING: PharmCAT has no result for CYP4F2" not in rec, rec[-1200:]) - rc, rec, _, _ = run_report(work, "CYP4F2MISS", {"genes": {"CYP4F2": variants("CYP4F2", None, "rs2108622")}}) - check("CYP4F2 with no genotype at rs2108622: not callable", - "CYP4F2 -- PharmCAT has no result; rs2108622 has no genotype in the VCF PharmCAT read: not callable." - in rec and "WARNING" not in rec.split(fb)[-1], rec[-1200:]) - rc, rec, _, _ = run_report(work, "VKORC1HET", {"genes": {"VKORC1": variants("VKORC1", "C/T", "rs9923231")}}) - check("VKORC1 No Result with C/T at rs9923231: a WARNING reads -1639 GA, with the VKORC1 warfarin rule", - "WARNING: PharmCAT has no result for VKORC1; single-position fallback, not a PharmCAT call: " - "rs9923231 reads C/T, -1639 GA." in rec - and "Warfarin (CPIC): the CPIC warfarin guideline doses by a pharmacogenetic algorithm " - "that takes VKORC1 -1639G>A with CYP2C9; each A allele lowers the expected dose." in rec, - rec[-1200:]) - called = {"genes": {"CYP4F2": dict(dip("*1", "*3", "Decreased Function"), - variants=[{"dbSnpId": "rs2108622", "call": "C/T"}])}} - rc, rec, _, _ = run_report(work, "CYP4F2CALLED", called) - check("CYP4F2 called by PharmCAT: no fallback section", fb not in rec, rec[-1200:]) - - # 7. MT-RNR1 from step 20's chrM calls next to step 36's consensus - def outside(gene, a1, a2, phen, drugs): - g = dip(a1, a2, phen) if a2 else {"sourceDiplotypes": [ - {"allele1": {"name": a1}, "allele2": None, "label": a1, "phenotypes": [phen]}]} - return dict(g, callSource="OUTSIDE", relatedDrugs=[{"name": n} for n in drugs]) - mito = {"genes": { - "MT-RNR1": outside("MT-RNR1", "m.1555A>G", None, - "increased risk of aminoglycoside-induced hearing loss", ["amikacin", "gentamicin"]), - "CYP2D6": outside("CYP2D6", "*1", "*4", "Intermediate Metabolizer", ["codeine"])}} - rc, rec, rows, _ = run_report(work, "MTRNR1CONS", mito, consensus=[("CYP2D6", "*1/*1", "yes")]) - check("MT-RNR1 outside call, consensus without it: exits 0", rc == 0, rc) - check("MT-RNR1 outside call, consensus without it: stays called and non-normal", - ["MT-RNR1", "m.1555A>G", "increased risk of aminoglycoside-induced hearing loss", "non-normal"] in rows, rows) - check("MT-RNR1 outside call, consensus without it: listed with its drugs", - " MT-RNR1 -- " in rec and "amikacin" in block(rec, "MT-RNR1") and "gentamicin" in block(rec, "MT-RNR1"), - block(rec, "MT-RNR1") or rec[:1500]) - check("MT-RNR1 outside call, consensus without it: no stale warning for MT-RNR1", - "outside call MT-RNR1" not in rec, rec[-1500:]) - check("control: the CYP2D6 outside call the consensus passes as another diplotype is still held back", - "WARNING: PharmCAT's report has the outside call CYP2D6 *1/*4" in rec - and " CYP2D6 -- " not in rec.split("Affected Medications:", 1)[-1].split("Calls From Other Tools", 1)[0], - rec[-1500:]) - rep = os.path.join(work, "MTRNR1CONS", "report.json") - rc, out, _ = og("pgx", "step36-outside", "--report", rep) - check("step36-outside lists CYP2D6 and not MT-RNR1", rc == 0 and out.split() == ["CYP2D6"], (rc, out)) - only = os.path.join(work, "MTRNR1ONLY.json") - with open(only, "w") as f: - json.dump({"genes": {"MT-RNR1": mito["genes"]["MT-RNR1"]}}, f) - rc, out, _ = og("pgx", "step36-outside", "--report", only) - check("step36-outside lists nothing for a report whose only outside call is MT-RNR1", - rc == 0 and out == "", (rc, out)) - phenoconversion_checks(work) - contraindication_checks(work) - finally: - shutil.rmtree(work) - print("\nRESULT:", "ALL PASS" if FAILS == 0 else f"{FAILS} FAILED") - return 1 if FAILS else 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tests/test_cyp2d6_depth.py b/tests/test_cyp2d6_depth.py deleted file mode 100644 index 083367f09..000000000 --- a/tests/test_cyp2d6_depth.py +++ /dev/null @@ -1,137 +0,0 @@ -#!/usr/bin/env python3 -"""The CYP2D6 depth check (og pgx cyp2d6-bed, -measure, -check), by meaning. - -`cargo test -p og-bam` (tests/parity.rs) holds og to the files under -rust/crates/og-bam/tests/data/parity/, byte for byte: what the Python tool -og replaced wrote on each case (want//), or what og writes where it -fixes that tool on purpose (og//). This test says what those files -must mean, so a golden rewritten with another verdict fails here even when -og agrees with it. - -Five depth tables (tests/fixtures/pgx/depth/.q0.bed and .q1.bed, the -mosdepth region files of all reads and of MAPQ >= 1): - - clean_no_alt the fixture's CYP2D slice mapped to the no-ALT - reference (ALT depth A/B run 37177862776): ok - multimapped_with_alt the same reads mapped to the Broad hg38 FASTA with - ALT contigs (same run): unreliable, multi-mapped - deleted_both CYP2D6 deleted on both copies (*5/*5): almost no reads - at the gene, normal flanks: ok, the deletion is real - deleted_one one copy deleted: half the flank depth: ok - no_flank_reads no reads at all: unreliable - -Also: the check file has the status, the message, the four depths and the -MAPQ shares; `cyp2d6-bed` prints CYP2D6 between its two flanks; an input -without a CYP2D6 row, with regions other than those (a zero-length one -included), a region listed twice with two depths, or an output that cannot -be written exits 2 and writes nothing. The MAPQ shares beside the verdict -(`cyp2d6-measure`, then `cyp2d6-check --measure`): on synthetic reads at -CYP2D6, reads in REP6 at MAPQ 0 lower the share over Cyrius's region but not -over the gene body; without the measure file every share it gives reads 'not -measured'; the shares never change the status; a file that is not -measure's exits 2. - -Run: python3 tests/test_cyp2d6_depth.py -""" -import os -import sys - -REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -PARITY = os.path.join(REPO, "rust", "crates", "og-bam", "tests", "data", "parity") -FAILS = [] - -OK = "CYP2D6 depth is fit for a copy-number call" -UNRELIABLE = ("CYP2D6 copy number unreliable: reads are multi-mapped " - "(was this BAM aligned to a reference with ALT contigs?)") -NO_DEPTH = ("CYP2D6 copy number unreliable: no reads in the flanks of CYP2D6 " - "(chr22:42.05-42.25 Mb), so its depth cannot be compared") -ROWS = ["metric", "status", "message", "gene_depth_all", "gene_depth_mapq1", "flank_depth_all", - "flank_depth_mapq1", "gene_mapq1_share", "gene_mapq20_share", "body_depth_all", - "body_mapq1_share", "body_mapq20_share"] - - -def expect(desc, got, want): - if got == want: - print(f"PASS {desc}") - else: - print(f"FAIL {desc}: got {got!r}, want {want!r}") - FAILS.append(desc) - - -def golden(case): - """og's golden of a case when it has one (a deliberate fix), else the oracle's.""" - d = os.path.join(PARITY, "og", case) - return d if os.path.isdir(d) else os.path.join(PARITY, "want", case) - - -def exit_code(case): - return int(open(os.path.join(golden(case), "exit")).read().strip()) - - -def written(case): - """The files the case writes, by name.""" - d = os.path.join(golden(case), "files") - return sorted(os.listdir(d)) if os.path.isdir(d) else [] - - -def table(case, name): - path = os.path.join(golden(case), "files", name) - rows = [line.rstrip("\n").split("\t", 1) for line in open(path)] - return {k: v for k, v in rows}, [k for k, _ in rows] - - -CASES = [ - ("clean_no_alt", "ok", OK), - ("multimapped_with_alt", "unreliable", UNRELIABLE), - ("deleted_both", "ok", OK), - ("deleted_one", "ok", OK), - ("no_flank_reads", "unreliable", NO_DEPTH), -] - -for case, status, message in CASES: - for suffix, measured in (("", False), ("-measured", True)): - c = f"check-{case}{suffix}" - expect(f"{c}: exit code", exit_code(c), 0) - rows, order = table(c, "check.tsv") - expect(f"{c}: status", rows.get("status"), status) - expect(f"{c}: message", rows.get("message"), message) - expect(f"{c}: header and the eleven rows", order, ROWS) - shares = [rows.get(k) for k in ("gene_mapq20_share", "body_depth_all", "body_mapq1_share", "body_mapq20_share")] - if measured: - expect(f"{c}: the measure file gives every share", "not measured" in shares, False) - else: - expect(f"{c}: without the measure file the other shares are not measured", shares, ["not measured"] * 4) - -rows, _ = table("check-clean_no_alt", "check.tsv") -expect("clean_no_alt: the verdict's MAPQ >= 1 share over Cyrius's region (16.79 / 18.53)", - rows.get("gene_mapq1_share"), "0.91") -expect("clean_no_alt: depths, length-weighted", - [rows.get(k) for k in ("gene_depth_all", "gene_depth_mapq1", "flank_depth_all", "flank_depth_mapq1")], - ["18.53", "16.79", "29.68", "29.67"]) - -bed = open(os.path.join(golden("cyp2d6-bed"), "stdout")).read().splitlines() -expect("cyp2d6-bed: CYP2D6 between two flanks", [line.split("\t")[3] for line in bed], ["flank", "CYP2D6", "flank"]) - -for c, why in (("check-no-gene", "an input without a CYP2D6 row"), - ("check-other-region", "regions other than the ones cyp2d6-bed prints"), - ("check-zero-length", "a zero-length region"), - ("check-duplicate-row", "a region listed twice with two depths"), - ("check-bad-measure", "a measure file that is not measure's"), - ("check-out-unwritable", "an output that cannot be written")): - expect(f"{why}: exit 2 and nothing written", (exit_code(c), written(c)), (2, [])) - -# The synthetic reads of make_parity.py: Cyrius's region tiled, MAPQ 0 in -# REP6 and around the body, the body at MAPQ 60 except some at MAPQ 10. -rows = [line.rstrip("\n").split("\t") for line in open(os.path.join(golden("measure-cyp2d6_synthetic"), "files", "shares.tsv"))] -m = {(r, int(q)): float(d) for r, q, d in rows[1:]} -expect("measure: the MAPQ 0 reads outside the body leave the body's MAPQ >= 1 depth whole", - m[("body", 1)], m[("body", 0)]) -expect("measure: the MAPQ 10 reads count at MAPQ >= 1, not at >= 20", - [m[(k, 20)] < m[(k, 1)] for k in ("gene", "body")], [True, True]) -expect("measure: over Cyrius's region the MAPQ 0 reads lower the MAPQ >= 1 depth", - m[("gene", 1)] < m[("gene", 0)], True) - -if FAILS: - print(f"{len(FAILS)} check(s) failed") - sys.exit(1) -print("all checks passed") diff --git a/tests/test_hla_second_caller.py b/tests/test_hla_second_caller.py deleted file mode 100644 index acde4f5bc..000000000 --- a/tests/test_hla_second_caller.py +++ /dev/null @@ -1,176 +0,0 @@ -#!/usr/bin/env python3 -# og-leg: OG_REQUIRED -"""og pgx outside-calls with OptiType, the opt-in second HLA caller. - -With HLA_QC=true (bash) or --hla_qc (Nextflow) step 08 also runs OptiType on -the HLA-A, -B and -C reads, and step 36 passes HLA-A or HLA-B to PharmCAT -only when T1K and OptiType give the same two alleles at two fields. Each case -runs og on synthetic files: - - 1. agree both callers agree: HLA-A and HLA-B reach PharmCAT - 2. disagree OptiType differs at HLA-B: HLA-B stays 'indeterminate', - both calls are printed, it is not in the outside-call - file; HLA-A still passes - 3. homozygous T1K's one allele and OptiType's A1 == A2 agree - 4. no call a file step 08 wrote instead of a result ('# OptiType - not run: ...'): both genes withheld - 5. asked, missing --require-optitype without --optitype: both withheld - 6. T1K alone no OptiType asked for: T1K's call passes, as before - 7. quality 0 T1K's quality-0 rule still wins over an agreement - 8. concordance the hla-concordance subcommand: HLA-A, -B, -C, yes/no - 9. red control the same inputs without OptiType's result, so without - the agreement rule: the disagreeing HLA-B reaches the - outside-call file and case 2's check reports it, so - case 2 can fail (og's own planted control is the cargo - test optitype_agreeing_passes_and_disagreeing_withholds) - -Run: python3 tests/test_hla_second_caller.py (OG names og, else -tests/og_binary.py finds the build) -""" -import os -import re -import subprocess -import sys -import tempfile - -REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -sys.path.insert(0, os.path.join(REPO, "tests")) -from og_binary import og_binary # noqa: E402 - -OG = [og_binary(), "pgx"] -FAILS = [] - -T1K = ("HLA-A\t2\tHLA-A*01:01:01\t110.5\t60\tHLA-A*26:01:01\t95.2\t60\t\n" - "HLA-B\t2\tHLA-B*57:01:01\t101.0\t60\tHLA-B*38:01:01\t88.7\t48\t\n" - "HLA-C\t1\tHLA-C*06:02:01\t150.3\t60\t.\t0\t-1\t\n") -T1K_HOM = ("HLA-A\t1\tHLA-A*02:01:01\t200.0\t60\t.\t0\t-1\t\n" - "HLA-B\t2\tHLA-B*57:01:01\t101.0\t60\tHLA-B*38:01:01\t88.7\t48\t\n") -T1K_Q0 = ("HLA-A\t2\tHLA-A*01:01:01\t110.5\t60\tHLA-A*26:01:01\t95.2\t60\t\n" - "HLA-B\t2\tHLA-B*57:01:01\t101.0\t0\tHLA-B*38:01:01\t88.7\t48\t\n") -OT_HEAD = "\tA1\tA2\tB1\tB2\tC1\tC2\tReads\tObjective\n" -OT_AGREE = OT_HEAD + "0\tA*26:01\tA*01:01\tB*38:01\tB*57:01\tC*06:02\tC*06:02\t812\t790.4\n" -OT_DISAGREE = OT_HEAD + "0\tA*01:01\tA*26:01\tB*58:01\tB*38:01\tC*06:02\tC*07:01\t812\t790.4\n" -OT_HOM = OT_HEAD + "0\tA*02:01\tA*02:01\tB*57:01\tB*38:01\t\t\t500\t480.0\n" -OT_NONE = "# OptiType not run: no reads at HLA-A, HLA-B or HLA-C in this BAM\n" - - -def check(name, ok, detail=""): - print(("PASS " if ok else "FAIL ") + name + ("" if ok else f": {detail}")) - if not ok: - FAILS.append(name) - - -def run(tmp, t1k, optitype=None, require=False): - """(outside-call text, {gene: consensus row}) of one run.""" - hla = os.path.join(tmp, "t1k.tsv") - with open(hla, "w") as f: - f.write(t1k) - calls, cons = os.path.join(tmp, "calls.tsv"), os.path.join(tmp, "cons.tsv") - cmd = [*OG, "outside-calls", "--calls", calls, "--consensus", cons, "--hla", hla] - if optitype is not None: - ot = os.path.join(tmp, "optitype.tsv") - with open(ot, "w") as f: - f.write(optitype) - cmd += ["--optitype", ot] - if require: - cmd.append("--require-optitype") - r = subprocess.run(cmd, capture_output=True, text=True) - if r.returncode != 0: - return None, {"error": r.stderr} - rows = {} - with open(cons) as f: - for line in f.read().splitlines()[1:]: - c = line.split("\t") - rows[c[0]] = c - with open(calls) as f: - return f.read(), rows - - -def passed(calls, gene, dip): - return calls is not None and f"{gene}\t{dip}\n" in calls - - -def withheld(calls, rows, gene, reason_re): - r = rows.get(gene) or [] - return (calls is not None and not re.search(rf"^{gene}\t", calls, re.M) - and r[1:3] == ["indeterminate", "no"] and re.search(reason_re, r[3]) is not None) - - -def disagree_checks(tag, tmp, optitype=OT_DISAGREE): - """Case 2's checks; True when every one passed.""" - calls, rows = run(tmp, T1K, optitype) - before = len(FAILS) - check(f"{tag} HLA-B withheld when OptiType disagrees", - withheld(calls, rows, "HLA-B", r"^T1K and OptiType disagree at two fields$"), f"{calls!r} {rows.get('HLA-B')}") - check(f"{tag} both HLA-B calls are printed", - (rows.get("HLA-B") or [""] * 5)[4] == "T1K: HLA-B*57:01:01 (quality 60); HLA-B*38:01:01 (quality 48); " - "OptiType: *58:01/*38:01", rows.get("HLA-B")) - check(f"{tag} HLA-A, where they agree, still passes", passed(calls, "HLA-A", "*01:01/*26:01"), calls) - return len(FAILS) == before - - -with tempfile.TemporaryDirectory() as tmp: - # 1. agree - calls, rows = run(tmp, T1K, OT_AGREE) - check("1 HLA-A passes when both agree (either order)", passed(calls, "HLA-A", "*01:01/*26:01"), calls) - check("1 HLA-B passes when both agree", passed(calls, "HLA-B", "*57:01/*38:01"), calls) - check("1 the reason names both callers", - (rows.get("HLA-B") or [""] * 4)[3] == "T1K and OptiType (step 08), which agree at two fields", rows.get("HLA-B")) - check("1 HLA-C is never an outside call", calls is not None and "HLA-C" not in calls, calls) - - # 2. disagree - disagree_checks("2", tmp) - - # 3. homozygous - calls, rows = run(tmp, T1K_HOM, OT_HOM) - check("3 T1K's one allele agrees with OptiType's A1 == A2", passed(calls, "HLA-A", "*02:01/*02:01"), calls) - check("3 the homozygous HLA-A row says both callers", - (rows.get("HLA-A") or [""] * 4)[3] == "T1K and OptiType (step 08), which agree at two fields", rows.get("HLA-A")) - - # 4. OptiType did not run (the file step 08 writes instead) - calls, rows = run(tmp, T1K, OT_NONE) - for g in ("HLA-A", "HLA-B"): - check(f"4 {g} withheld: OptiType made no call", - withheld(calls, rows, g, r"^OptiType, the second caller, made no call$"), f"{calls!r} {rows.get(g)}") - - # 5. asked for, no result - calls, rows = run(tmp, T1K, None, require=True) - for g in ("HLA-A", "HLA-B"): - check(f"5 {g} withheld: OptiType asked for, no result", - withheld(calls, rows, g, r"^the second caller was asked for \(HLA_QC\)"), f"{calls!r} {rows.get(g)}") - - # 6. T1K alone, as before the second caller - calls, rows = run(tmp, T1K) - check("6 without OptiType, T1K's HLA-B passes", passed(calls, "HLA-B", "*57:01/*38:01"), calls) - check("6 the reason is T1K's", (rows.get("HLA-B") or [""] * 4)[3] == "T1K (step 08)", rows.get("HLA-B")) - - # 7. T1K's quality 0 still withholds - calls, rows = run(tmp, T1K_Q0, OT_AGREE) - check("7 a T1K quality-0 allele withholds HLA-B even when OptiType agrees", - withheld(calls, rows, "HLA-B", r"quality 0 or below"), f"{calls!r} {rows.get('HLA-B')}") - - # 8. the concordance table of step 08 - t1k, ot, out = (os.path.join(tmp, n) for n in ("c_t1k.tsv", "c_ot.tsv", "c_out.tsv")) - with open(t1k, "w") as f: - f.write(T1K) - with open(ot, "w") as f: - f.write(OT_DISAGREE) - r = subprocess.run([*OG, "hla-concordance", "--t1k", t1k, "--optitype", ot, "--out", out], - capture_output=True, text=True) - want = ("Gene\tT1K\tOptiType\tAgree\tNote\n" - "HLA-A\t*01:01/*26:01\t*01:01/*26:01\tyes\t-\n" - "HLA-B\t*57:01/*38:01\t*58:01/*38:01\tno\tthe callers disagree at two fields; step 36 does not pass it to PharmCAT\n" - "HLA-C\t*06:02/*06:02\t*06:02/*07:01\tno\tthe callers disagree at two fields\n") - got = open(out).read() if r.returncode == 0 and os.path.exists(out) else r.stderr - check("8 the concordance table, byte for byte", got == want, f"\n{got}") - - # 9. red control: case 2 must fail when the disagreeing HLA-B gets through - saved = list(FAILS) - caught = not disagree_checks("9 (control, must FAIL)", tmp, optitype=None) - FAILS[:] = saved - check("9 with the agreement rule disabled, case 2 rejects the disagreeing HLA-B", caught) - -if FAILS: - print(f"{len(FAILS)} check(s) failed") - sys.exit(1) -print("all checks passed") diff --git a/tests/test_pgx_mito_merge.py b/tests/test_pgx_mito_merge.py deleted file mode 100644 index 6473c9e64..000000000 --- a/tests/test_pgx_mito_merge.py +++ /dev/null @@ -1,227 +0,0 @@ -#!/usr/bin/env python3 -# og-leg: OG_REQUIRED -"""og pgx mito-merge: the MT-RNR1 outside call PharmCAT gets from the chrM -calls of step 20 / MITO_VARIANTS (step 07 and PHARMCAT run it). - -The test builds a stand-in for pharmcat.jar, a zip holding -org/pharmgkb/pharmcat/phenotype/MT_RNR1.json with some of PharmCAT 3.4.0's -MT-RNR1 alleles and their function values, writes invented Mutect2-style chrM -VCFs (GT 0/1 at every AF, as Mutect2 writes them), and checks: - 1. a homoplasmic PASS m.1555A>G (AF 0.99) is the call; step 36's lines are - copied before it and an MT-RNR1 line there is dropped; - 2. AF 0.95 counts as homoplasmic, 0.949 does not; --min-af moves the bound; - 3. a heteroplasmic, a filtered or an AF-less m.1555A>G passes nothing and - says why; so do a homoplasmic PASS change at a named position that - PharmCAT does not name and a PASS deletion near m.961; a filtered one, - or a low-AF unnamed change, does not; - 4. PASS chrM records with no named allele give Reference when samtools - depth shows 20 reads or more at every named position; without a depth - file, with one position below 20 (--min-depth moves the bound) or absent - from it, no call; a homoplasmic named allele needs no depth; a file with - no PASS chrM record gives no call; records on other contigs are ignored; - 5. two homoplasmic named alleles: the increased-risk one is the call, the - other is listed; - 6. INFO AF when FORMAT has none, the AF of the second ALT of a multi-allelic - record, a bgzip/gzip input and the contig name MT are read; - 7. a jar without the MT-RNR1 definitions, or an unreadable VCF, exits 2. - -Run: OG=rust/target/release/og python3 tests/test_pgx_mito_merge.py -""" -import gzip -import json -import os -import subprocess -import sys -import tempfile -import zipfile - -REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -OG = os.environ.get("OG", "") -FAILS = [] - -INC = "Increased risk of aminoglycoside-induced hearing loss" -UNC = "Uncertain risk of aminoglycoside-induced hearing loss" -NOR = "Normal risk of aminoglycoside-induced hearing loss" -ALLELES = [("Reference", NOR), ("m.827A>G", NOR), ("m.960C>del", UNC), ("m.961T>del", UNC), - ("m.961T>del+Cn", UNC), ("m.1095T>C", INC), ("m.1189T>C", UNC), ("m.1555A>G", INC)] -HEADER = ("##fileformat=VCFv4.2\n##contig=\n" - "#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tS\n") - - -def check(desc, ok, detail=""): - print(f"[{'PASS' if ok else 'FAIL'}] {desc}{'' if ok else ' -- ' + str(detail)[:600]}") - if not ok: - FAILS.append(desc) - - -def rec(pos, ref, alt, af, filt="PASS", chrom="chrM", info="."): - fmt, val = ("GT:AF:DP", f"0/1:{af}:3000") if af is not None else ("GT:DP", "0/1:3000") - return f"{chrom}\t{pos}\t.\t{ref}\t{alt}\t.\t{filt}\t{info}\t{fmt}\t{val}\n" - - -BACKGROUND = rec(263, "A", "G", 0.998) + rec(750, "A", "G", 0.999) - - -class Case: - def __init__(self, tmp): - self.tmp = tmp - self.jar = os.path.join(tmp, "pharmcat.jar") - with zipfile.ZipFile(self.jar, "w") as z: - z.writestr("org/pharmgkb/pharmcat/phenotype/MT_RNR1.json", json.dumps({ - "gene": "MT-RNR1", - "namedAlleles": [{"name": n, "functionValue": f, "activityValue": None} for n, f in ALLELES]})) - self.n = 0 - - def run(self, records, outside=None, gz=False, extra=(), jar=None, depth=None): - self.n += 1 - if depth is not None: - path = os.path.join(self.tmp, f"d{self.n}.depth") - with open(path, "w") as f: - f.writelines(f"chrM\t{p}\t{depth.get(p, 500)}\n" for p in range(648, 1602) if depth.get(p, 500) is not None) - extra = (*extra, "--depth", path) - vcf = os.path.join(self.tmp, f"m{self.n}.vcf" + (".gz" if gz else "")) - text = HEADER + records - if gz: - with gzip.open(vcf, "wt") as f: - f.write(text) - else: - with open(vcf, "w") as f: - f.write(text) - out = os.path.join(self.tmp, f"o{self.n}.tsv") - cmd = [OG, "pgx", "mito-merge", "--jar", jar or self.jar, "--mito", vcf, "--out", out, *extra] - if outside is not None: - path = os.path.join(self.tmp, f"c{self.n}.tsv") - with open(path, "w") as f: - f.write(outside) - cmd += ["--outside", path] - p = subprocess.run(cmd, capture_output=True, text=True) - lines = open(out).read().splitlines() if os.path.exists(out) else None - return p.returncode, p.stdout + p.stderr, lines - - def mt(self, lines): - return [x.split("\t", 1)[1] for x in (lines or []) if x.startswith("MT-RNR1\t")] - - -def og_or_skip(): - """OG names the og binary (the rust job of guard.yml sets it). Without - it the test is skipped with a note; with OG_REQUIRED set too, it fails.""" - if os.environ.get("OG"): - return None - print("skip: OG is not set (OG=rust/target/release/og)") - return 1 if os.environ.get("OG_REQUIRED") else 0 - - -def main(): - skip = og_or_skip() - if skip is not None: - return skip - with tempfile.TemporaryDirectory() as tmp: - c = Case(tmp) - - # 1. homoplasmic m.1555A>G - rc, log, lines = c.run(BACKGROUND + rec(1555, "A", "G", 0.99), - outside="HLA-A\t*01:01/*26:01\nMT-RNR1\tm.1095T>C\n") - check("1: exit 0", rc == 0, log) - check("1: MT-RNR1 m.1555A>G is the call", c.mt(lines) == ["m.1555A>G"], lines) - check("1: step 36's HLA-A line is copied first", (lines or [""])[0] == "HLA-A\t*01:01/*26:01", lines) - check("1: the MT-RNR1 line of step 36's file is dropped", len(c.mt(lines)) == 1, lines) - check("1: the log says it passed m.1555A>G, homoplasmic, with the risk", - "MT-RNR1 m.1555A>G passed to PharmCAT as an outside call (homoplasmic, chrM:1555 A>G AF 0.990; " - + INC in log, log) - check("1: the log names the dropped line", "dropped the MT-RNR1 line" in log, log) - - # 2. the AF bound - rc, log, lines = c.run(BACKGROUND + rec(1555, "A", "G", 0.95)) - check("2: AF 0.95 is homoplasmic", c.mt(lines) == ["m.1555A>G"], log) - rc, log, lines = c.run(BACKGROUND + rec(1555, "A", "G", 0.949)) - check("2: AF 0.949 passes nothing", c.mt(lines) == [], log) - rc, log, lines = c.run(BACKGROUND + rec(1555, "A", "G", 0.9), extra=("--min-af", "0.85")) - check("2: --min-af 0.85 takes AF 0.9", c.mt(lines) == ["m.1555A>G"], log) - - # 3. what passes nothing - for desc, records, want in [ - ("heteroplasmic", rec(1555, "A", "G", 0.4), "is heteroplasmic (AF below 0.95)"), - ("filtered", rec(1555, "A", "G", 0.99, filt="possible_numt"), "is filtered (possible_numt)"), - ("without an allele fraction", rec(1555, "A", "G", None), "has no allele fraction"), - ("a change PharmCAT does not name", rec(1555, "A", "C", 0.99), - "a change at an MT-RNR1 position that PharmCAT does not name"), - ("a deletion near m.961", rec(955, "AC", "A", 0.99), "at or near m.960"), - ("an insertion at m.961", rec(961, "T", "TC", 0.99), "(chrM:961 T>TC AF 0.990) at or near m.960"), - ]: - rc, log, lines = c.run(BACKGROUND + records, outside="HLA-B\t*35:01/*44:02\n") - check(f"3: {desc}: exit 0", rc == 0, log) - check(f"3: {desc}: no MT-RNR1 line", c.mt(lines) == [], lines) - check(f"3: {desc}: step 36's line is still copied", lines == ["HLA-B\t*35:01/*44:02"], lines) - check(f"3: {desc}: the log says why ({want})", want in log, log) - check(f"3: {desc}: the log says the gene stays No Result", "it stays No Result" in log, log) - # m.960 is named only as a deletion: an unnamed SNV there still holds Reference back. - rc, log, lines = c.run(BACKGROUND + rec(960, "C", "T", 0.99), depth={}) - check("3: an unnamed SNV at m.960 (named only as a deletion): no MT-RNR1 line", c.mt(lines) == [], log) - check("3: an unnamed SNV at m.960: the log says why", - "a change at an MT-RNR1 position that PharmCAT does not name" in log, log) - rc, log, lines = c.run(BACKGROUND + rec(1555, "A", "C", 0.3, filt="weak_evidence") - + rec(955, "AC", "A", 0.2, filt="weak_evidence"), depth={}) - check("3: a filtered unnamed change and a filtered deletion near m.961 hold nothing back", - c.mt(lines) == ["Reference"], log) - - # 4. Reference, and no PASS record - rc, log, lines = c.run(BACKGROUND + rec(2706, "A", "G", 0.999) + rec(1555, "A", "G", 0.99, chrom="chr1"), - depth={}) - check("4: no named allele on chrM, depth 500: Reference", c.mt(lines) == ["Reference"], log) - check("4: the log says all 6 named positions are covered and counts the PASS records", - "all 6 named positions have depth 20 or more (lowest 500); 3 PASS chrM records read" in log, log) - rc, log, lines = c.run(BACKGROUND, depth={1555: 20, 961: 20}) - check("4: depth exactly 20 counts", c.mt(lines) == ["Reference"], log) - rc, log, lines = c.run(BACKGROUND) - check("4: no depth file: no call", c.mt(lines) == [], log) - check("4: no depth file: the log says so", "there is no read depth" in log and "stays No Result" in log, log) - rc, log, lines = c.run(BACKGROUND, depth={1555: 5}) - check("4: chrM:1555 at depth 5: no call", c.mt(lines) == [], log) - check("4: chrM:1555 at depth 5: the log names it", "1 of its 6 positions have depth below 20 (chrM:1555 depth 5)" in log, log) - rc, log, lines = c.run(BACKGROUND, depth={1555: 5}, extra=("--min-depth", "5")) - check("4: --min-depth 5 takes depth 5", c.mt(lines) == ["Reference"], log) - rc, log, lines = c.run(BACKGROUND, depth={960: None}) - check("4: a named position absent from the depth file counts as depth 0", - c.mt(lines) == [] and "chrM:960 depth 0" in log, log) - rc, log, lines = c.run(BACKGROUND + rec(1555, "A", "G", 0.99)) - check("4: a homoplasmic named allele needs no depth file", c.mt(lines) == ["m.1555A>G"], log) - rc, log, lines = c.run(rec(263, "A", "G", 0.99, filt="weak_evidence")) - check("4: no PASS chrM record: exit 0", rc == 0, log) - check("4: no PASS chrM record: no call", c.mt(lines) == [], lines) - check("4: no PASS chrM record: the log says so", "the chrM call set has no PASS record" in log, log) - check("4: an empty output file is still written", lines == [], lines) - - # 5. two homoplasmic named alleles - rc, log, lines = c.run(BACKGROUND + rec(1189, "T", "C", 0.99) + rec(1555, "A", "G", 0.98)) - check("5: the increased-risk allele is the call", c.mt(lines) == ["m.1555A>G"], log) - check("5: the other is listed", "also homoplasmic, not passed" in log and "m.1189T>C" in log, log) - - # 6. AF sources, multi-allelic, gzip, MT - rc, log, lines = c.run(BACKGROUND + "chrM\t1555\t.\tA\tG\t.\tPASS\tAF=0.97\tGT\t0/1\n") - check("6: INFO AF is read when FORMAT has none", c.mt(lines) == ["m.1555A>G"], log) - rc, log, lines = c.run(BACKGROUND + "chrM\t1555\t.\tA\tC,G\t.\tPASS\t.\tGT:AF\t0/1/2:0.01,0.98\n") - check("6: the AF of the second ALT", c.mt(lines) == ["m.1555A>G"], log) - rc, log, lines = c.run(BACKGROUND + rec(1555, "A", "G", 0.99), gz=True) - check("6: a gzip VCF", c.mt(lines) == ["m.1555A>G"], log) - rc, log, lines = c.run(rec(263, "A", "G", 0.99, chrom="MT") + rec(1555, "A", "G", 0.99, chrom="MT")) - check("6: contig MT", c.mt(lines) == ["m.1555A>G"], log) - - # 7. errors - bad = os.path.join(tmp, "empty.jar") - with zipfile.ZipFile(bad, "w") as z: - z.writestr("logback.xml", "") - rc, log, lines = c.run(BACKGROUND, jar=bad) - check("7: a jar without MT_RNR1.json exits 2", rc == 2, log) - check("7: and names the resource", "org/pharmgkb/pharmcat/phenotype/MT_RNR1.json" in log, log) - rc, log, lines = c.run("chrM\t1555\tA\n") - check("7: a record with too few columns exits 2", rc == 2, log) - - if FAILS: - print(f"\n{len(FAILS)} check(s) failed") - return 1 - print("\nall checks passed") - return 0 - - -if __name__ == "__main__": - sys.exit(main()) diff --git a/tests/test_pgx_outside_calls.py b/tests/test_pgx_outside_calls.py deleted file mode 100644 index 579cb6cc1..000000000 --- a/tests/test_pgx_outside_calls.py +++ /dev/null @@ -1,364 +0,0 @@ -#!/usr/bin/env python3 -# og-leg: OG_REQUIRED -"""og pgx outside-calls: what reaches PharmCAT as an outside call. - -CYP2D6 reaches PharmCAT only when pypgx and a second caller (Cyrius) give the -same diplotype and the depth check passed; HLA-A and HLA-B come from T1K. -Each case runs og on synthetic inputs (tests/fixtures/pgx/) and compares -both files it writes, byte for byte: - - 1. disagree pypgx and Cyrius differ: no outside call, 'indeterminate' - 2. one missing pypgx only (Cyrius not run): no outside call - 3. agree both give the same diplotype, depth ok: the call is passed - 4. depth both agree, but the depth check found multi-mapped reads - 5. no call Cyrius ran but made no call: one caller only - 6. HLA T1K's HLA-A and HLA-B, truncated to two fields - 7. HLA edges one allele (read as homozygous); an allele of quality 0 - -and what step 27 (og pgx cpic-report --consensus) makes of it: - - 8. CYP2D6 held back: the CPIC report says so, names the drugs it affects, - and drops the pypgx-only warning it prints without the table - 9. HLA passed on: the CPIC report lists it in the outside-call section and - marks the gene PharmCAT reports with callSource OUTSIDE - 10. a stale report: PharmCAT's report has outside calls the consensus does - not pass on (another HLA-B, a CYP2D6 now held back, or no readable - table at all); they give no drug guidance and the report says to rerun - step 07 - -and step 32's summary (og pgx pypgx-summary) and the probable CYP2D6 call -the reports show: - - 11. (the star alleles of a diplotype and the defining-variant evidence of - one call are og's own unit tests: cargo test -p og-steps pypgx_summary) - 13. pypgx alone with the depth check ok and PASS defining variants: the - outside-call file stays empty and step 36's row stays indeterminate - (the reports' probable call itself is og summary collect's rule, - tested in rust/crates/og-summary/src/collect/pgx.rs) - 14. the whole subcommand on pypgx results archives, pypgx's own tables - (rows of pypgx 0.26.0's gene and allele tables, tests/fixtures/pgx/ - summary/) and an indexed VCF: GSTT1 not callable on this reference - (not FAILED), a gene without results FAILED, pypgx's CNV call for a - BAM gene only, the evidence columns per gene, byte for byte what the - Python it replaced wrote with the real pypgx and pysam; the tables - found beside the pypgx on PATH; both files or neither - -Run: python3 tests/test_pgx_outside_calls.py (OG names og, else -tests/og_binary.py finds the build) -""" -import gzip -import os -import shutil -import subprocess -import sys -import tempfile -import zipfile - -REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -sys.path.insert(0, os.path.join(REPO, "tests")) -from og_binary import og_binary # noqa: E402 - -OG = og_binary() -FX = os.path.join(REPO, "tests", "fixtures", "pgx") -FAILS = [] - -HEADER = "Gene\tResult\tOutside_call\tReason\tEvidence\n" -NO_HLA = ("HLA-A\tnot typed\tno\tHLA typing (step 08) did not run\t-\n" - "HLA-B\tnot typed\tno\tHLA typing (step 08) did not run\t-\n") -MULTIMAPPED = ("CYP2D6 copy number unreliable: reads are multi-mapped " - "(was this BAM aligned to a reference with ALT contigs?)") - - -def run(**inputs): - """(calls file, consensus file) og writes for these inputs.""" - with tempfile.TemporaryDirectory() as tmp: - calls, cons = os.path.join(tmp, "calls.tsv"), os.path.join(tmp, "consensus.tsv") - cmd = [OG, "pgx", "outside-calls", "--calls", calls, "--consensus", cons] - for opt, name in inputs.items(): - cmd += ["--" + opt.replace("_", "-"), os.path.join(FX, name)] - r = subprocess.run(cmd, capture_output=True, text=True) - if r.returncode != 0: - return f"exit {r.returncode}: {r.stderr.strip()}", "" - return open(calls).read(), open(cons).read() - - -def case(desc, want_calls, want_consensus, **inputs): - calls, cons = run(**inputs) - for what, got, want in (("outside calls", calls, want_calls), ("consensus", cons, want_consensus)): - if got == want: - print(f"PASS {desc}: {what}") - else: - print(f"FAIL {desc}: {what}\n--- got\n{got}--- want\n{want}---") - FAILS.append(f"{desc}: {what}") - - -# 1. Written first: a naive "take pypgx" passes pypgx's call on and fails here. -case("1 disagree", "", - HEADER + NO_HLA + - "CYP2D6\tindeterminate\tno\tpypgx and Cyrius disagree\tpypgx: *1/*4; Cyrius: *1/*2; depth check: ok\n", - pypgx="pypgx_cyp2d6_1_4.tsv", cyrius="cyrius_1_2.tsv", depth_check="depth_check_ok.tsv") - -case("2 one missing", "", - HEADER + NO_HLA + - "CYP2D6\tindeterminate\tno\tone caller only: a second caller must agree (Cyrius: setup.sh --cyrius)" - "\tpypgx: *1/*2; Cyrius: not run; depth check: ok\n", - pypgx="pypgx_cyp2d6_1_2.tsv", depth_check="depth_check_ok.tsv") - -case("3 agree", "CYP2D6\t*1/*2\n", - HEADER + NO_HLA + - "CYP2D6\t*1/*2\tyes\tpypgx and Cyrius agree; the depth check passed" - "\tpypgx: *1/*2; Cyrius: *1/*2; depth check: ok\n", - pypgx="pypgx_cyp2d6_1_2.tsv", cyrius="cyrius_1_2.tsv", depth_check="depth_check_ok.tsv") - -case("4 depth unreliable", "", - HEADER + NO_HLA + - f"CYP2D6\tindeterminate\tno\t{MULTIMAPPED}\tpypgx: *1/*2; Cyrius: *1/*2; depth check: unreliable\n", - pypgx="pypgx_cyp2d6_1_2.tsv", cyrius="cyrius_1_2.tsv", depth_check="depth_check_multimapped.tsv") - -case("4b no depth check", "", - HEADER + NO_HLA + - "CYP2D6\tindeterminate\tno\tthe CYP2D6 depth check did not run" - "\tpypgx: *1/*2; Cyrius: *1/*2; depth check: not run\n", - pypgx="pypgx_cyp2d6_1_2.tsv", cyrius="cyrius_1_2.tsv") - -case("5 Cyrius made no call", "", - HEADER + NO_HLA + - "CYP2D6\tindeterminate\tno\tone caller only: a second caller must agree (Cyrius: setup.sh --cyrius)" - "\tpypgx: *1/*2; Cyrius: no call (None, Not_assigned_to_haplotypes); depth check: ok\n", - pypgx="pypgx_cyp2d6_1_2.tsv", cyrius="cyrius_none.tsv", depth_check="depth_check_ok.tsv") - -case("6 HLA from T1K", "HLA-A\t*02:01/*24:02\nHLA-B\t*07:02/*44:02\n", - HEADER + - "HLA-A\t*02:01/*24:02\tyes\tT1K (step 08)\tHLA-A*02:01:01 (quality 60); HLA-A*24:02:01 (quality 60)\n" - "HLA-B\t*07:02/*44:02\tyes\tT1K (step 08)\tHLA-B*07:02:01 (quality 60); HLA-B*44:02:01 (quality 48)\n" - "CYP2D6\tindeterminate\tno\tno caller made a call\tpypgx: not run; Cyrius: not run; depth check: not run\n", - hla="t1k_genotype.tsv") - -case("7 HLA one allele, and an allele of quality 0", "HLA-A\t*02:01/*02:01\n", - HEADER + - "HLA-A\t*02:01/*02:01\tyes\tT1K (step 08): one allele, read as homozygous\tHLA-A*02:01:01 (quality 60)\n" - "HLA-B\tindeterminate\tno\tT1K gives an allele quality 0 or below (T1K says to ignore it)" - "\tHLA-B*07:02:01 (quality 60); HLA-B*44:02:01 (quality 0)\n" - "CYP2D6\tindeterminate\tno\tno caller made a call\tpypgx: not run; Cyrius: not run; depth check: not run\n", - hla="t1k_genotype_homozygous_lowqual.tsv") - -# --- step 27 with the consensus table ------------------------------------------ -PARSE = [OG, "pgx"] -REPORTS = os.path.join(REPO, "tests", "fixtures", "pharmcat") - - -def cpic(report, **inputs): - """The recommendations text of og pgx cpic-report, with a consensus - table from og pgx outside-calls when consensus_from is given.""" - with tempfile.TemporaryDirectory() as tmp: - cmd = [*PARSE, "cpic-report", "--sample", "T", "--outdir", tmp, - "--report", os.path.join(REPORTS, report)] - if "pypgx" in inputs: - cmd += ["--pypgx", os.path.join(FX, inputs["pypgx"])] - if "consensus_from" in inputs: - cons = os.path.join(tmp, "consensus.tsv") - gen = [OG, "pgx", "outside-calls", "--calls", os.path.join(tmp, "calls.tsv"), "--consensus", cons] - for opt, name in inputs["consensus_from"].items(): - gen += ["--" + opt.replace("_", "-"), os.path.join(FX, name)] - subprocess.run(gen, check=True, capture_output=True) - cmd += ["--consensus", cons] - r = subprocess.run(cmd, capture_output=True, text=True) - if r.returncode != 0: - return f"exit {r.returncode}: {r.stderr}" - return open(os.path.join(tmp, "T_cpic_recommendations.txt")).read() - - -def report_has(desc, text, needle, present=True): - if (needle in text) == present: - print(f"PASS {desc}") - else: - print(f"FAIL {desc}: {'missing' if present else 'unexpected'} {needle!r}\n{text}") - FAILS.append(desc) - - -# 8. The 3.4.0 report of the fixture: PharmCAT has no CYP2D6 result. -plain = cpic("report-3.4.0.json", pypgx="pypgx_cyp2d6_1_4.tsv") -report_has("8 control: without the table, pypgx's call alone draws a warning", plain, - "WARNING: PharmCAT has no result for CYP2D6, but pypgx") -held = cpic("report-3.4.0.json", pypgx="pypgx_cyp2d6_1_4.tsv", - consensus_from={"pypgx": "pypgx_cyp2d6_1_4.tsv", "depth_check": "depth_check_ok.tsv"}) -report_has("8 the report has the outside-call section", held, "Calls From Other Tools (outside calls, step 36):") -report_has("8 CYP2D6 is indeterminate and why", held, - "CYP2D6 indeterminate not passed to PharmCAT: " + "one caller only") -report_has("8 it names the drugs CYP2D6 affects", held, "No drug guidance is given for CYP2D6 here. Drugs affected by CYP2D6: ") -report_has("8 no pypgx-only warning for the held-back gene", held, "PharmCAT has no result for CYP2D6", False) - -# 9. PharmCAT's example report has HLA-B from an outside call (and CYP2D6 and -# MT-RNR1, which this consensus does not pass on: case 10). -passed = cpic("pharmcat-docs-example.json", consensus_from={"hla": "t1k_genotype_docs_example.tsv"}) -report_has("9 HLA-B is listed as passed from T1K", passed, - "HLA-B *15:02/*57:01 passed to PharmCAT from T1K (step 08); PharmCAT reports it as an outside call.") -report_has("9 the gene line marks PharmCAT's outside call", passed, "[outside call]") -report_has("9 the confirmed HLA-B keeps its drug guidance", passed, " HLA-B -- ") -report_has("9 no stale warning for the confirmed HLA-B", passed, "outside call HLA-B", False) - - -def medications(text): - """The Affected Medications section of a recommendations text.""" - return text.split("Affected Medications:", 1)[-1].split("Calls From Other Tools", 1)[0] - - -# 10. Outside calls the consensus does not pass on. -plain = cpic("pharmcat-docs-example.json") -report_has("10 control: without the table, the example's CYP2D6 has drug guidance", medications(plain), " CYP2D6 -- ") -report_has("10 control: without the table, the example's HLA-B has drug guidance", medications(plain), " HLA-B -- ") -other = cpic("pharmcat-docs-example.json", consensus_from={"hla": "t1k_genotype.tsv"}) -report_has("10 HLA-B of another call gets no drug guidance", medications(other), " HLA-B -- ", False) -report_has("10 the report says the HLA-B outside call is stale", other, - "WARNING: PharmCAT's report has the outside call HLA-B *15:02/*57:01, which the consensus does not pass on") -report_has("10 the consensus HLA-B is not shown as PharmCAT's", other, - "HLA-B *07:02/*44:02 passed to PharmCAT from T1K (step 08); PharmCAT's report does not list it") -held = cpic("pharmcat-docs-example.json", consensus_from={"pypgx": "pypgx_cyp2d6_1_4.tsv", "depth_check": "depth_check_ok.tsv"}) -report_has("10 a held-back CYP2D6 gets no drug guidance from an older report", medications(held), " CYP2D6 -- ", False) -report_has("10 the report says the CYP2D6 outside call is stale", held, - "WARNING: PharmCAT's report has the outside call CYP2D6 *1/*3, which the consensus does not pass on") -report_has("10 and says how to fix it", held, "Rerun step 07, then this step. No drug guidance is given for CYP2D6 here.") -with tempfile.TemporaryDirectory() as tmp: - r = subprocess.run([*PARSE, "cpic-report", "--sample", "T", "--outdir", tmp, - "--report", os.path.join(REPORTS, "pharmcat-docs-example.json"), - "--consensus", os.path.join(tmp, "missing.tsv")], capture_output=True, text=True) - gone = open(os.path.join(tmp, "T_cpic_recommendations.txt")).read() if r.returncode == 0 else f"exit {r.returncode}" -report_has("10 with the table missing, no outside call gets drug guidance", medications(gone), " HLA-B -- ", False) -report_has("10 with the table missing, the report says to run step 36 again", gone, "Run step 36 again, then steps 07 and 27.") - -# --- step 32's summary and the probable CYP2D6 call ------------------------------- -# The rule for pypgx's call alone (probable_cyp2d6) is og summary collect's, -# tested in rust/crates/og-summary/src/collect/pgx.rs. - -SUMMARY_HEADER = ["Gene", "Diplotype", "Phenotype", "CNV_call", "Source", "Status", - "Defining_variants", "Min_GQ", "Weakest_variant", "Defining_filter", "Quality"] - - -def same(desc, got, want): - if got == want: - print(f"PASS {desc}") - else: - print(f"FAIL {desc}: got {got!r}, want {want!r}") - FAILS.append(desc) - - -# 13. -with tempfile.TemporaryDirectory() as tmp: - summ = os.path.join(tmp, "pypgx.tsv") - with open(summ, "w") as f: - f.write("\t".join(SUMMARY_HEADER) + "\n" - "CYP2D6\t*1/*2\tNormal Metabolizer\tNormal\tBAM\tcalled\t1/1 in the VCF\t50" - "\tchr22:42126611 C>G GQ 50 AD 20,22\tPASS\tok\n") - calls, cons = os.path.join(tmp, "c.tsv"), os.path.join(tmp, "k.tsv") - subprocess.run([OG, "pgx", "outside-calls", "--calls", calls, "--consensus", cons, "--pypgx", summ, - "--depth-check", os.path.join(FX, "depth_check_ok.tsv")], check=True, capture_output=True) - same("13 the outside-call file stays empty for a probable call", open(calls).read(), "") - same("13 step 36's row stays indeterminate, one caller only", open(cons).read().splitlines()[-1].split("\t")[:4], - ["CYP2D6", "indeterminate", "no", "one caller only: a second caller must agree (Cyrius: setup.sh --cyrius)"]) - -# 14. The subcommand on pypgx's results archives and tables and an indexed VCF. -SUMMARY_FX = os.path.join(FX, "summary") -# sample.vcf.gz (bgzip and tabix of bcftools 1.24) holds these records. -SUMMARY_VCF = """##fileformat=VCFv4.2 -##contig= -##contig= -##FILTER= -##FORMAT= -##FORMAT= -##FORMAT= -#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\tFORMAT\tS -chr10\t94761900\t.\tC\tT\t50\tPASS\t.\tGT:GQ:AD\t0/1:60:0,31 -chr10\t94775360\t.\tAGGGGGGGG\tA\t50\tPASS\t.\tGT:GQ:AD\t0/1:40:10,10 -chr10\t94775367\t.\tA\tG\t50\tPASS\t.\tGT:GQ:AD\t0/1:48:20,19 -chr10\t94781859\t.\tG\tA,C\t50\tPASS\t.\tGT:GQ:AD\t0/1:12:26,3,. -chr22\t42126611\t.\tC\tG\t50\tRefCall\t.\tGT:GQ:AD\t0/0:50:20,22 -""" -# What bin/pgx_outside_calls.py pypgx-summary wrote for these inputs in the -# pypgx 0.26.0 image, with the real pypgx and pysam (the parent of the -# commit that deleted it). -SUMMARY_WANT = [ - SUMMARY_HEADER, - ["GSTM1", "*0/*0", "no pypgx phenotype rule", "WholeDel2", "BAM", "called", "none", "N/A", "N/A", "none", "N/A"], - ["GSTT1", "N/A", "N/A", "N/A", "BAM", "not callable on this reference", "N/A", "N/A", "N/A", "N/A", "N/A"], - ["CYP2D6", "*1/*2x2", "Normal Metabolizer", "Duplication", "BAM", "called", "1/2 in the VCF", "50", - "chr22:42126611 C>G GQ 50 AD 20,22", "not PASS: chr22:42126611 C>G RefCall; not in the VCF: chr22:42127941 G>A", - "ok"], - ["CYP2B6", "FAILED", "N/A", "N/A", "BAM", "failed", "N/A", "N/A", "N/A", "N/A", "N/A"], - ["CYP2C19", "*2/*17", "Intermediate Metabolizer", "N/A", "VCF", "called", "3/3 in the VCF", "12", - "chr10:94781859 G>A GQ 12 AD 26,3,.", "PASS", "low-confidence: check the reads"], - ["CYP2C9", "Indeterminate", "Indeterminate", "N/A", "VCF", "no call", "N/A", "N/A", "N/A", "N/A", "N/A"], - ["NUDT15", "*1/*99", "N/A", "N/A", "VCF", "called", "unknown", "N/A", "N/A", "not in pypgx's table: *99", "N/A"], -] -SUMMARY_LOG = ("Genes called: 4/7\nWARNING: star alleles resting on a variant below GQ 20, check the reads: " - "CYP2C19 *2/*17 (chr10:94781859 G>A GQ 12 AD 26,3,.)\n") - - -def results_zip(path, genotype, phenotype, cnv): - """A pypgx results archive: metadata.txt and the sample table data.tsv - in one folder, as pypgx's Archive.to_file writes it.""" - os.makedirs(os.path.dirname(path), exist_ok=True) - with zipfile.ZipFile(path, "w", zipfile.ZIP_DEFLATED) as z: - z.writestr("abc123/metadata.txt", "Gene=X\nAssembly=GRCh38\nSemanticType=SampleTable[Results]\n") - z.writestr("abc123/data.tsv", "\tVariantData\tCNV\tCandidates\tGenotype\tPhenotype\n" - f"S\tx\t{cnv}\t*1\t{genotype}\t{phenotype}\n") - - -def summary(tmp, *extra, env=None): - outbase, out = os.path.join(tmp, "out"), os.path.join(tmp, "summary.tsv") - for gene, g, p, c in (("CYP2C19", "*2/*17", "Intermediate Metabolizer", "Normal"), - ("GSTM1", "*0/*0", "Indeterminate", "WholeDel2"), - ("CYP2D6", "*1/*2x2", "Normal Metabolizer", "Duplication"), - ("CYP2C9", "Indeterminate", "Indeterminate", "NA"), - ("NUDT15", "*1/*99", "", "Normal")): - results_zip(os.path.join(outbase, gene, "results.zip"), g, p, c) - r = subprocess.run([OG, "pgx", "pypgx-summary", "--outbase", outbase, - "--bam-genes", "GSTM1 GSTT1 CYP2D6 CYP2B6", "--vcf-genes", "CYP2C19 CYP2C9 NUDT15", - "--vcf", os.path.join(SUMMARY_FX, "sample.vcf.gz"), "--not-callable", "GSTT1", - "--out", out, *extra], capture_output=True, text=True, env=env) - rows = [line.rstrip("\n").split("\t") for line in open(out)] if os.path.exists(out) else [] - return r, rows - - -same("14 the fixture VCF holds the records the test names", - gzip.decompress(open(os.path.join(SUMMARY_FX, "sample.vcf.gz"), "rb").read()).decode(), SUMMARY_VCF) -with tempfile.TemporaryDirectory() as tmp: - r, rows = summary(tmp, "--pypgx-data", SUMMARY_FX) - same("14 the subcommand exits 0, without a warning", (r.returncode, r.stderr), (0, "")) - same("14 the summary, row by row, as the Python wrote it", rows, SUMMARY_WANT) - same("14 the log names the low-confidence call", r.stdout, SUMMARY_LOG) -with tempfile.TemporaryDirectory() as tmp: - # pypgx's tables found beside the pypgx on PATH, for the Python its #! - # line names (the image also has a python3.1 link to python3.12). - data = os.path.join(tmp, "prefix", "lib", "python3.12", "site-packages", "pypgx", "api", "data") - os.makedirs(data) - for name in ("gene-table.csv", "allele-table.csv"): - shutil.copy(os.path.join(SUMMARY_FX, name), data) - os.symlink("python3.12", os.path.join(tmp, "prefix", "lib", "python3.1")) - os.makedirs(os.path.join(tmp, "prefix", "bin")) - with open(os.path.join(tmp, "prefix", "bin", "pypgx"), "w") as f: - f.write("#!/usr/local/bin/python3.12\n") - os.chmod(os.path.join(tmp, "prefix", "bin", "pypgx"), 0o755) - env = dict(os.environ, PATH=os.path.join(tmp, "prefix", "bin") + os.pathsep + os.environ["PATH"]) - r, rows = summary(tmp, env=env) - same("14 pypgx's tables are found beside the pypgx on PATH", (r.returncode, rows), (0, SUMMARY_WANT)) -with tempfile.TemporaryDirectory() as tmp: - env = dict(os.environ, PATH="/usr/bin:/bin") - r, rows = summary(tmp, env=env) - same("14 without pypgx's tables the evidence says not looked up, and why", - (r.returncode, rows[3][6:] if len(rows) > 3 else None, "WARNING: no pypgx tables" in r.stderr), - (0, ["not looked up", "N/A", "N/A", "N/A", "N/A"], True)) - -# 15. Both files or neither: a consensus table that cannot be written leaves -# no outside-call file behind. -with tempfile.TemporaryDirectory() as tmp: - calls = os.path.join(tmp, "calls.tsv") - r = subprocess.run([OG, "pgx", "outside-calls", "--calls", calls, - "--consensus", os.path.join(tmp, "no-such-dir", "consensus.tsv"), - "--hla", os.path.join(FX, "t1k_genotype.tsv")], capture_output=True, text=True) - same("15 the consensus cannot be written: exit 1, and no outside-call file", - (r.returncode, os.path.exists(calls), sorted(os.listdir(tmp))), (1, False, [])) - -if FAILS: - print(f"{len(FAILS)} check(s) failed") - sys.exit(1) -print("all checks passed") diff --git a/tests/test_pgx_refblock_fill.py b/tests/test_pgx_refblock_fill.py deleted file mode 100644 index 077cbedca..000000000 --- a/tests/test_pgx_refblock_fill.py +++ /dev/null @@ -1,200 +0,0 @@ -#!/usr/bin/env python3 -# og-leg: OG_REQUIRED -"""og pgx refblock-fill: PharmCAT's multi-base positions called 0/0 from -the reference blocks of a gVCF (steps 07 and PHARMCAT_PREPROCESS run it). - -The fixture gVCF (tests/fixtures/pharmcat/refblocks.g.vcf) has reference -blocks over nine PharmCAT positions longer than one base, from the subset of -PharmCAT 3.4.0's positions file beside it. The test expands the blocks into -one-base records as `bcftools convert --gvcf2vcf | bcftools view ---trim-alt-alleles -i 'GT!="mis"'` does, runs the script on that stream, and -checks: - 1. five positions are called 0/0, with PharmCAT's ID, REF and ALT, the flag - PGX_REFBLOCK, the block's genotype (0 on haploid chrX) and the lowest GQ - over the span: DPYD (two blocks meet inside the span), UGT1A1 *28, a TPMT - insertion, a RYR1 two-base substitution and a G6PD deletion; - 2. four stay missing: a ./. hole inside the span (CYP2C9, 11 bp), a block - with GQ 10, a variant record inside the span, a span no block reaches; - 3. each filled record takes the place of the one-base record at its - position, every other line passes unchanged, and the output stays sorted; - 4. the GQ threshold: 20 by default, a block at exactly --min-gq counts; - 5. a block that stops one base short, a block with a failed FILTER, a ./. - block with a high GQ, or a deletion that reaches into the span from - before it, fills nothing; - a gVCF with two samples or no #CHROM line is an error (exit 2). - -Run: OG=rust/target/release/og python3 tests/test_pgx_refblock_fill.py -""" -import os -import re -import subprocess -import sys -import tempfile - -REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -OG = os.environ.get("OG", "") -FX = os.path.join(REPO, "tests", "fixtures", "pharmcat") -GVCF = os.path.join(FX, "refblocks.g.vcf") -POSITIONS = os.path.join(FX, "pharmcat_positions-3.4.0-subset.vcf") -FAILS = [] - -FILLED = { # (chrom, pos): (REF, ALT, GT, GQ, ID) - ("chr1", 97450065): ("TG", "T", "0/0", "45", "rs72549303"), - ("chr2", 233760233): ("CAT", "C,CATAT,CATATAT", "0/0", "50", "rs3064744"), - ("chr6", 18149032): ("C", "CT", "0/0", "40", "rs759836180"), - ("chr19", 38572266): ("TC", "CT", "0/0", "35", "rs193922862"), - ("chrX", 154532055): ("CTCT", "C", "0", "30", "."), -} -LEFT = { - ("chr10", 94942205): "a ./. hole inside the REF span", - ("chr10", 94949161): "a block with GQ 10", - ("chr10", 94949281): "a variant record inside the REF span", - ("chr6", 18149103): "no block over the span", -} - - -def check(desc, ok, detail=""): - print(f"[{'PASS' if ok else 'FAIL'}] {desc}{'' if ok else ' -- ' + str(detail)[:600]}") - if not ok: - FAILS.append(desc) - - -def expand(gvcf_text): - """What gvcf2vcf plus `view --trim-alt-alleles -i 'GT!="mis"'` writes: - one record per base of each block (REF from the reference: N here, the - script reads only its length), ALT . for a reference call, no ./. records.""" - out = [] - for line in gvcf_text.splitlines(keepends=True): - if line.startswith("#"): - out.append(line) - continue - c = line.rstrip("\n").split("\t") - gt = c[9].split(":")[0] - if "." in re.split(r"[/|]", gt): - continue - alts = [a for a in c[4].split(",") if a != "<*>"] - m = re.search(r"END=(\d+)", c[7]) - if m and not alts: - for p in range(int(c[1]), int(m.group(1)) + 1): - out.append("\t".join([c[0], str(p), ".", "N", ".", c[5], c[6], ".", c[8], c[9]]) + "\n") - else: - out.append("\t".join(c[:4] + [",".join(alts) or "."] + c[5:]) + "\n") - return "".join(out) - - -def run(gvcf_text, *opts, positions=POSITIONS, stdin=None): - with tempfile.TemporaryDirectory() as tmp: - g = os.path.join(tmp, "records.vcf") - with open(g, "w") as f: - f.write(gvcf_text) - p = subprocess.run([OG, "pgx", "refblock-fill", *opts, positions, g], text=True, capture_output=True, - input=expand(gvcf_text) if stdin is None else stdin) - return p.returncode, p.stdout, p.stderr - - -def records(vcf): - return [l.rstrip("\n").split("\t") for l in vcf.splitlines() if l and not l.startswith("#")] - - -def fills(vcf): - return {(r[0], int(r[1])): r for r in records(vcf) if r[7] == "PGX_REFBLOCK"} - - -def og_or_skip(): - """OG names the og binary (the rust job of guard.yml sets it). Without - it the test is skipped with a note; with OG_REQUIRED set too, it fails.""" - if os.environ.get("OG"): - return None - print("skip: OG is not set (OG=rust/target/release/og)") - return 1 if os.environ.get("OG_REQUIRED") else 0 - - -def main(): - skip = og_or_skip() - if skip is not None: - return skip - with open(GVCF) as f: - gvcf = f.read() - rc, out, err = run(gvcf) - print(err.strip()) - check("runs and exits 0", rc == 0, err) - got = fills(out) - # 1. filled - check("five positions called 0/0: DPYD, UGT1A1 *28, TPMT, RYR1, G6PD", - sorted(got) == sorted(FILLED), sorted(got)) - for key, (ref, alt, gt, gq, vid) in FILLED.items(): - r = got.get(key) - check(f"{key[0]}:{key[1]}: PharmCAT's ID, REF and ALT, GT {gt}, lowest GQ {gq}", - r is not None and r[2:5] == [vid, ref, alt] and r[6] == "PASS" and r[8] == "GT:GQ" - and r[9] == f"{gt}:{gq}", r) - # 2. left missing - for key, why in LEFT.items(): - check(f"{key[0]}:{key[1]} stays missing ({why})", key not in got, got.get(key)) - check("the summary on stderr counts 5 of the 9 positions longer than one base", - "5 of 9 PharmCAT positions longer than one base called 0/0 from reference blocks with GQ >= 20" in err, err) - # 3. replaced in place, the rest unchanged, sorted - inp = records(expand(gvcf)) - outr = records(out) - at = {} - for r in outr: - at.setdefault((r[0], int(r[1])), []).append(r) - check("one record at each filled position (the one-base record is replaced)", - all(len(at.get(k, [])) == 1 for k in FILLED), {k: len(at.get(k, [])) for k in FILLED}) - kept = [r for r in inp if (r[0], int(r[1])) not in FILLED] - check("every other record passes unchanged and in order", - [r for r in outr if r[7] != "PGX_REFBLOCK"] == kept, len(kept)) - check("the bases after a filled position inside its span stay (chr2:233760234-233760235)", - ("chr2", 233760234) in at and ("chr2", 233760235) in at) - order = ["chr1", "chr2", "chr6", "chr10", "chr19", "chrX"] - keys = [(order.index(r[0]), int(r[1])) for r in outr] - check("the output is sorted", keys == sorted(keys)) - head = [l for l in out.splitlines() if l.startswith("#")] - check("the header declares PGX_REFBLOCK before #CHROM", - len(head) >= 2 and head[-1].startswith("#CHROM") and head[-2].startswith("##INFO=\t0\t.\tEND=233761000\tGT:GQ:MIN_DP\t0/0:50:30\n", - "chr2\t233759900\t.\tT\t<*>\t0\t.\tEND=233760230\tGT:GQ:MIN_DP\t0/0:50:30\n" - "chr2\t233760231\t.\tGCCA\tG,<*>\t30\tPASS\t.\tGT:GQ:DP\t0/1:30:28\n" - "chr2\t233760232\t.\tC\t<*>\t0\t.\tEND=233761000\tGT:GQ:MIN_DP\t0/0:50:30\n") - _, o, _ = run(dele) - check("a deletion from before the span that overlaps it fills nothing, though blocks cover the span", - dele != gvcf and ("chr2", 233760233) not in fills(o), sorted(fills(o))) - short = gvcf.replace("END=233761000", "END=233760234") - _, o, _ = run(short) - check("a block ending one base before the span ends fills nothing", ("chr2", 233760233) not in fills(o)) - lowq = gvcf.replace("T\t<*>\t0\t.\tEND=233761000", "T\t<*>\t0\tLowQual\tEND=233761000") - _, o, _ = run(lowq) - check("a block with a failed FILTER fills nothing", lowq != gvcf and ("chr2", 233760233) not in fills(o)) - two = gvcf.replace("\tREFBLOCKS\n", "\tREFBLOCKS\tOTHER\n") - rc2, _, e2 = run(two) - check("a gVCF with two samples: exit 2", rc2 == 2 and "one sample" in e2, (rc2, e2)) - nohead = "".join(l for l in gvcf.splitlines(keepends=True) if not l.startswith("#CHROM")) - rc3, _, e3 = run(nohead, stdin=expand(gvcf)) - check("a gVCF without its #CHROM line: exit 2", rc3 == 2 and "#CHROM" in e3, (rc3, e3)) - - print("\nRESULT:", "ALL PASS" if not FAILS else f"{len(FAILS)} FAILED") - return 1 if FAILS else 0 - - -if __name__ == "__main__": - sys.exit(main())