Coverage for src/python/ensembl/vep/corpus/synthetic/default.py: 100%
66 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
1# See the NOTICE file distributed with this work for additional information
2# regarding copyright ownership.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14"""Build the default synthetic corpus."""
16from __future__ import annotations
18import random
19from collections.abc import Sequence
21from ensembl.vep.common import CoordSystemLevel, Strand
22from ensembl.vep.common.codons import translate_dna
23from ensembl.vep.corpus.overlap import build_transcript_overlaps
24from ensembl.vep.corpus.registry import register_corpus
25from ensembl.vep.corpus.spec import CorpusSpec
26from ensembl.vep.corpus.synthetic.base import SyntheticCorpus
27from ensembl.vep.corpus.synthetic.components.sequence import fill_locus_sequence
28from ensembl.vep.corpus.synthetic.components.transcript import (
29 build_cds_genomic_bounds,
30 build_transcript,
31 patch_terminal_codons,
32)
33from ensembl.vep.corpus.synthetic.components.variation import (
34 boundary_variant_alleles,
35 fuzz_variant_alleles,
36)
37from ensembl.vep.models.genic import Gene, Transcript
38from ensembl.vep.models.sequence import CoordSystem, Slice
39from ensembl.vep.models.variation import VariationAllele
41__all__ = [
42 "build_genome",
43 "build_variation_alleles",
44 "default_corpus",
45]
47_CHROMOSOME_NAME = "chrSynthetic"
48_CHROMOSOME_LENGTH = 60000
50_ENST0001_EXONS: tuple[tuple[int, int], ...] = ((1000, 1200), (2000, 2200), (3000, 3300))
51_ENST0002_EXONS: tuple[tuple[int, int], ...] = ((10000, 10300), (11000, 11200), (12000, 12150))
52_ENST0003_EXONS: tuple[tuple[int, int], ...] = ((20000, 20200), (21000, 21300))
53_ENST0004_EXONS: tuple[tuple[int, int], ...] = ((30000, 30200), (31000, 31250))
55_CDS_START_OFFSET = 50
56_ENST0001_CDS_FRAME_INDEX = 1
57_ENST0001_CDS_STOP_OFFSET = 101
58_ENST0002_CDS_FRAME_INDEX = 2
59_ENST0002_CDS_STOP_OFFSET = 102
60_ENST0004_CDS_FRAME_INDEX = 1
61_ENST0004_CDS_STOP_OFFSET = 51
64def build_genome(seed: int = 7) -> tuple[Slice, tuple[Transcript, ...]]:
65 """Build the synthetic chromosome and its four transcripts.
67 Mirrors ``socore.data.build_genome``: one forward-strand protein-coding transcript, one
68 reverse-strand protein-coding transcript, one non-coding (lincRNA-like) transcript, and one
69 nonsense_mediated_decay transcript.
71 The biotypes are declared on purposely different features so that both branches of
72 ``FactContext.biotype`` (see consequence/context/fact.py) are covered: ENST0001 and ENST0003
73 carry their own, ENST0002 and ENST0004 leave theirs None and inherit the gene's.
75 Args:
76 seed (int): Random seed, for reproducibility.
78 Returns:
79 tuple[Slice, tuple[Transcript, ...]]: The shared chromosome slice (with real, CDS-patched
80 sequence) and the four transcripts.
81 """
82 random_generator = random.Random(seed)
83 chromosome_chars = [""] * _CHROMOSOME_LENGTH
84 fill_locus_sequence(
85 chromosome_chars,
86 1,
87 _CHROMOSOME_LENGTH,
88 random_generator,
89 bases="ACGT",
90 )
92 synthetic_slice = Slice(
93 coord_system=CoordSystem(name="chromosome", level=CoordSystemLevel.CHROMOSOME),
94 seq_region_name=_CHROMOSOME_NAME,
95 start=1,
96 end=_CHROMOSOME_LENGTH,
97 strand=Strand.FORWARD,
98 )
99 with synthetic_slice.unsafe() as unsafe_slice:
100 setattr(unsafe_slice, "_seq", "".join(chromosome_chars))
102 # ENST0002 deliberately declares no biotype of its own, so that it exercises
103 # FactContext.biotype's fall back to the gene; ENST0001 declares its own and carries no gene.
104 coding_gene = Gene(
105 start=_ENST0002_EXONS[0][0],
106 end=_ENST0002_EXONS[-1][1],
107 strand=Strand.REVERSE,
108 stable_id="ENSG0002",
109 slice=synthetic_slice,
110 )
111 coding_gene.biotype = "protein_coding"
113 nmd_gene = Gene(
114 start=_ENST0004_EXONS[0][0],
115 end=_ENST0004_EXONS[-1][1],
116 strand=Strand.FORWARD,
117 stable_id="ENSG0004",
118 slice=synthetic_slice,
119 )
120 nmd_gene.biotype = "nonsense_mediated_decay"
122 transcripts = (
123 build_transcript(
124 stable_id="ENST0001",
125 synthetic_slice=synthetic_slice,
126 strand=Strand.FORWARD,
127 exons_genomic_ascending=_ENST0001_EXONS,
128 cds_genomic_bounds=build_cds_genomic_bounds(
129 _ENST0001_EXONS,
130 strand=Strand.FORWARD,
131 frame_index=_ENST0001_CDS_FRAME_INDEX,
132 start_offset=_CDS_START_OFFSET,
133 stop_offset=_ENST0001_CDS_STOP_OFFSET,
134 ),
135 gene=None,
136 biotype="protein_coding",
137 ),
138 build_transcript(
139 stable_id="ENST0002",
140 synthetic_slice=synthetic_slice,
141 strand=Strand.REVERSE,
142 exons_genomic_ascending=_ENST0002_EXONS,
143 cds_genomic_bounds=build_cds_genomic_bounds(
144 _ENST0002_EXONS,
145 strand=Strand.REVERSE,
146 frame_index=_ENST0002_CDS_FRAME_INDEX,
147 start_offset=_CDS_START_OFFSET,
148 stop_offset=_ENST0002_CDS_STOP_OFFSET,
149 ),
150 gene=coding_gene,
151 ),
152 build_transcript(
153 stable_id="ENST0003",
154 synthetic_slice=synthetic_slice,
155 strand=Strand.FORWARD,
156 exons_genomic_ascending=_ENST0003_EXONS,
157 cds_genomic_bounds=None,
158 gene=None,
159 biotype="lincRNA",
160 ),
161 build_transcript(
162 stable_id="ENST0004",
163 synthetic_slice=synthetic_slice,
164 strand=Strand.FORWARD,
165 exons_genomic_ascending=_ENST0004_EXONS,
166 cds_genomic_bounds=build_cds_genomic_bounds(
167 _ENST0004_EXONS,
168 strand=Strand.FORWARD,
169 frame_index=_ENST0004_CDS_FRAME_INDEX,
170 start_offset=_CDS_START_OFFSET,
171 stop_offset=_ENST0004_CDS_STOP_OFFSET,
172 ),
173 gene=nmd_gene,
174 ),
175 )
177 for transcript in transcripts:
178 fill_locus_sequence(
179 chromosome_chars,
180 transcript.start,
181 transcript.end,
182 random_generator,
183 )
184 patch_terminal_codons(chromosome_chars, transcripts)
186 with synthetic_slice.unsafe() as unsafe_slice:
187 setattr(unsafe_slice, "_seq", "".join(chromosome_chars))
189 for transcript in transcripts:
190 if transcript.translation is None:
191 continue
192 written_coding_sequence = transcript.coding_sequence
193 assert written_coding_sequence is not None
194 translated = translate_dna(written_coding_sequence)
195 assert translated.startswith("M"), f"{transcript.stable_id} does not start with M: {translated[:5]}"
196 assert translated.endswith("*"), f"{transcript.stable_id} does not end with a stop"
197 assert "*" not in translated[:-1], f"{transcript.stable_id} has an internal stop"
199 return synthetic_slice, transcripts
202def build_variation_alleles(
203 synthetic_slice: Slice,
204 transcripts: Sequence[Transcript],
205 *,
206 fuzz_count: int = 4000,
207 boundary_seed: int = 11,
208 fuzz_seed: int = 23,
209) -> tuple[tuple[str, VariationAllele], ...]:
210 """Build named boundary and random alleles for the default corpus.
212 Args:
213 synthetic_slice (Slice): Sequence region holding the variants.
214 transcripts (Sequence[Transcript]): Transcripts used to derive boundary variants.
215 fuzz_count (int): Number of additional random variants.
216 boundary_seed (int): Boundary-variant random seed.
217 fuzz_seed (int): Random-variant seed.
219 Returns:
220 tuple[tuple[str, VariationAllele], ...]: Boundary variants followed by random variants.
221 """
222 return tuple(
223 boundary_variant_alleles(synthetic_slice, transcripts, seed=boundary_seed)
224 + fuzz_variant_alleles(synthetic_slice, fuzz_count, seed=fuzz_seed)
225 )
228def default_corpus(
229 fuzz_count: int = 4000,
230 seed: int = 7,
231 *,
232 boundary_seed: int = 11,
233 fuzz_seed: int = 23,
234 spec: CorpusSpec | None = None,
235) -> SyntheticCorpus:
236 """Build the default synthetic corpus: four transcripts, boundary variants, and fuzz variants.
238 Mirrors ``socore.data.default_dataset``.
240 Args:
241 fuzz_count (int): Number of additional random ("fuzz") variants to add.
242 seed (int): Random seed for the underlying chromosome, for reproducibility.
243 boundary_seed (int): Random seed for the boundary variants' alternate bases.
244 fuzz_seed (int): Random seed for the fuzz variants.
245 spec (CorpusSpec | None): Construction specification retained by the corpus.
247 Returns:
248 SyntheticCorpus: The synthetic slice, its transcripts, variants, and overlaps.
249 """
250 synthetic_slice, transcripts = build_genome(seed=seed)
251 named_variants = build_variation_alleles(
252 synthetic_slice,
253 transcripts,
254 fuzz_count=fuzz_count,
255 boundary_seed=boundary_seed,
256 fuzz_seed=fuzz_seed,
257 )
258 named_overlaps = build_transcript_overlaps(named_variants, transcripts)
259 return SyntheticCorpus(
260 synthetic_slice=synthetic_slice,
261 transcripts=transcripts,
262 named_variants=named_variants,
263 named_overlaps=named_overlaps,
264 spec=spec or CorpusSpec(kind="synthetic.default"),
265 )
268@register_corpus("synthetic.default")
269def _registered_default_corpus(specification: CorpusSpec) -> SyntheticCorpus:
270 """Build the registered default synthetic corpus."""
271 return default_corpus(
272 fuzz_count=specification.fuzz,
273 seed=specification.genome_seed,
274 boundary_seed=specification.boundary_seed,
275 fuzz_seed=specification.fuzz_seed,
276 spec=specification,
277 )