|
| 1 | +""" |
| 2 | +Issue #169: three-tier protein-coding biotype ontology. |
| 3 | +
|
| 4 | +* ``is_protein_coding`` — strict, the canonical ``"protein_coding"`` only. |
| 5 | + Unchanged from previous releases for back-compat with downstream effect |
| 6 | + predictors (varcode). |
| 7 | +* ``is_protein_coding_extended`` — widens to IG/TR gene segments and |
| 8 | + translated pseudogenes. Excludes NMD/NSD because those are degraded. |
| 9 | +* ``is_translated`` — widens further to include NMD/NSD; covers any |
| 10 | + biotype that goes through the ribosome at all. |
| 11 | +""" |
| 12 | + |
| 13 | +from pyensembl.locus_with_genome import ( |
| 14 | + EXTENDED_PROTEIN_CODING_BIOTYPES, |
| 15 | + LocusWithGenome, |
| 16 | + PROTEIN_CODING_BIOTYPES, |
| 17 | + TRANSLATED_BIOTYPES, |
| 18 | +) |
| 19 | + |
| 20 | +from .common import eq_ |
| 21 | +from .data import ( |
| 22 | + custom_mouse_genome_grcm38_subset, |
| 23 | + setup_init_custom_mouse_genome, |
| 24 | +) |
| 25 | + |
| 26 | + |
| 27 | +class _BiotypeFixture: |
| 28 | + """Stand-in that exposes only ``.biotype`` so we can invoke the three |
| 29 | + LocusWithGenome property accessors without constructing a Genome.""" |
| 30 | + |
| 31 | + is_protein_coding = LocusWithGenome.is_protein_coding |
| 32 | + is_protein_coding_extended = LocusWithGenome.is_protein_coding_extended |
| 33 | + is_translated = LocusWithGenome.is_translated |
| 34 | + |
| 35 | + def __init__(self, biotype): |
| 36 | + self.biotype = biotype |
| 37 | + |
| 38 | + |
| 39 | +# ----------------------------- |
| 40 | +# Set constants |
| 41 | +# ----------------------------- |
| 42 | + |
| 43 | + |
| 44 | +def test_biotype_set_ontology_is_monotonic(): |
| 45 | + """strict ⊂ extended ⊂ translated.""" |
| 46 | + assert PROTEIN_CODING_BIOTYPES.issubset(EXTENDED_PROTEIN_CODING_BIOTYPES) |
| 47 | + assert EXTENDED_PROTEIN_CODING_BIOTYPES.issubset(TRANSLATED_BIOTYPES) |
| 48 | + |
| 49 | + |
| 50 | +def test_extended_set_adds_immunoglobulin_and_tcr_and_translated_pseudogenes(): |
| 51 | + extras = EXTENDED_PROTEIN_CODING_BIOTYPES - PROTEIN_CODING_BIOTYPES |
| 52 | + eq_( |
| 53 | + extras, |
| 54 | + { |
| 55 | + "IG_C_gene", "IG_D_gene", "IG_J_gene", "IG_V_gene", |
| 56 | + "TR_C_gene", "TR_D_gene", "TR_J_gene", "TR_V_gene", |
| 57 | + "polymorphic_pseudogene", |
| 58 | + "translated_processed_pseudogene", |
| 59 | + "translated_unprocessed_pseudogene", |
| 60 | + }, |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def test_translated_set_only_adds_nmd_and_nsd_over_extended(): |
| 65 | + extras = TRANSLATED_BIOTYPES - EXTENDED_PROTEIN_CODING_BIOTYPES |
| 66 | + eq_(extras, {"nonsense_mediated_decay", "non_stop_decay"}) |
| 67 | + |
| 68 | + |
| 69 | +def test_strict_excludes_everything_but_canonical(): |
| 70 | + eq_(PROTEIN_CODING_BIOTYPES, {"protein_coding"}) |
| 71 | + |
| 72 | + |
| 73 | +# ----------------------------- |
| 74 | +# Property dispatch |
| 75 | +# ----------------------------- |
| 76 | + |
| 77 | + |
| 78 | +def test_canonical_protein_coding_is_true_in_every_tier(): |
| 79 | + f = _BiotypeFixture("protein_coding") |
| 80 | + assert f.is_protein_coding |
| 81 | + assert f.is_protein_coding_extended |
| 82 | + assert f.is_translated |
| 83 | + |
| 84 | + |
| 85 | +def test_ig_gene_segments_are_extended_and_translated_but_not_strict(): |
| 86 | + for biotype in ("IG_C_gene", "IG_D_gene", "IG_J_gene", "IG_V_gene"): |
| 87 | + f = _BiotypeFixture(biotype) |
| 88 | + assert not f.is_protein_coding, biotype |
| 89 | + assert f.is_protein_coding_extended, biotype |
| 90 | + assert f.is_translated, biotype |
| 91 | + |
| 92 | + |
| 93 | +def test_tcr_gene_segments_are_extended_and_translated_but_not_strict(): |
| 94 | + for biotype in ("TR_C_gene", "TR_D_gene", "TR_J_gene", "TR_V_gene"): |
| 95 | + f = _BiotypeFixture(biotype) |
| 96 | + assert not f.is_protein_coding, biotype |
| 97 | + assert f.is_protein_coding_extended, biotype |
| 98 | + assert f.is_translated, biotype |
| 99 | + |
| 100 | + |
| 101 | +def test_translated_pseudogenes_are_extended_and_translated_but_not_strict(): |
| 102 | + for biotype in ( |
| 103 | + "polymorphic_pseudogene", |
| 104 | + "translated_processed_pseudogene", |
| 105 | + "translated_unprocessed_pseudogene", |
| 106 | + ): |
| 107 | + f = _BiotypeFixture(biotype) |
| 108 | + assert not f.is_protein_coding, biotype |
| 109 | + assert f.is_protein_coding_extended, biotype |
| 110 | + assert f.is_translated, biotype |
| 111 | + |
| 112 | + |
| 113 | +def test_nmd_and_nsd_are_translated_only(): |
| 114 | + """NMD/NSD biotypes are translated by the ribosome but don't yield a |
| 115 | + stable protein product, so they're True for `is_translated` and |
| 116 | + False for the other two tiers.""" |
| 117 | + for biotype in ("nonsense_mediated_decay", "non_stop_decay"): |
| 118 | + f = _BiotypeFixture(biotype) |
| 119 | + assert not f.is_protein_coding, biotype |
| 120 | + assert not f.is_protein_coding_extended, biotype |
| 121 | + assert f.is_translated, biotype |
| 122 | + |
| 123 | + |
| 124 | +def test_noncoding_biotypes_are_false_at_every_tier(): |
| 125 | + """Sanity: typical non-coding biotypes (lncRNA, miRNA, snRNA, plain |
| 126 | + pseudogenes, regulatory) are False across all three tiers.""" |
| 127 | + for biotype in ( |
| 128 | + "lncRNA", |
| 129 | + "miRNA", |
| 130 | + "snRNA", |
| 131 | + "snoRNA", |
| 132 | + "rRNA", |
| 133 | + "processed_pseudogene", |
| 134 | + "unprocessed_pseudogene", |
| 135 | + "transcribed_processed_pseudogene", |
| 136 | + "transcribed_unprocessed_pseudogene", |
| 137 | + "transcribed_unitary_pseudogene", |
| 138 | + "TEC", |
| 139 | + "retained_intron", |
| 140 | + "processed_transcript", |
| 141 | + "antisense", |
| 142 | + ): |
| 143 | + f = _BiotypeFixture(biotype) |
| 144 | + assert not f.is_protein_coding, biotype |
| 145 | + assert not f.is_protein_coding_extended, biotype |
| 146 | + assert not f.is_translated, biotype |
| 147 | + |
| 148 | + |
| 149 | +def test_none_biotype_is_false_at_every_tier(): |
| 150 | + """Some GTFs (older Ensembl releases, TAIR) don't carry biotype |
| 151 | + attributes at all. `biotype=None` must not crash and must be False |
| 152 | + across the board.""" |
| 153 | + f = _BiotypeFixture(None) |
| 154 | + assert not f.is_protein_coding |
| 155 | + assert not f.is_protein_coding_extended |
| 156 | + assert not f.is_translated |
| 157 | + |
| 158 | + |
| 159 | +# ----------------------------- |
| 160 | +# Backward compat with existing callers |
| 161 | +# ----------------------------- |
| 162 | + |
| 163 | + |
| 164 | +def setup_module(module): |
| 165 | + setup_init_custom_mouse_genome() |
| 166 | + |
| 167 | + |
| 168 | +def test_existing_mouse_protein_coding_transcript_is_strict_protein_coding(): |
| 169 | + """The mouse partial fixture has exactly one transcript with |
| 170 | + biotype=='protein_coding' and one with biotype=='processed_transcript'. |
| 171 | + is_protein_coding must continue to return True for the first and |
| 172 | + False for the second; downstream effect prediction depends on this.""" |
| 173 | + coding = custom_mouse_genome_grcm38_subset.transcripts(biotype="protein_coding") |
| 174 | + eq_(len(coding), 1) |
| 175 | + assert coding[0].is_protein_coding |
| 176 | + assert coding[0].is_protein_coding_extended |
| 177 | + assert coding[0].is_translated |
| 178 | + |
| 179 | + all_transcripts = custom_mouse_genome_grcm38_subset.transcripts() |
| 180 | + noncoding = [t for t in all_transcripts if t.biotype != "protein_coding"] |
| 181 | + eq_(len(noncoding), 1) |
| 182 | + assert not noncoding[0].is_protein_coding |
| 183 | + assert not noncoding[0].is_protein_coding_extended |
| 184 | + assert not noncoding[0].is_translated |
0 commit comments