Coverage for src/python/ensembl/vep/corpus/annotate.py: 100%

27 statements  

« 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"""A whole corpus in the shape an annotated file has: one variation per variant, its overlaps attached. 

15 

16A corpus holds variants plus overlaps against their standalone alleles. A run -- and anything 

17reading a run's output, like the summary -- works on ``Variation`` objects whose alleles carry 

18their overlaps. This rebuilds that shape, including variants with no overlaps, so a corpus can be 

19summarised or written exactly as an annotated file would be. 

20""" 

21 

22from __future__ import annotations 

23 

24from collections.abc import Iterator, Sequence 

25 

26from ensembl.vep.annotators.base import Annotator 

27from ensembl.vep.common import Locus 

28from ensembl.vep.corpus.base import Corpus 

29from ensembl.vep.models.overlap import OverlapConsequence, TranscriptVariationAllele 

30from ensembl.vep.models.variation import Variation, VariationAllele 

31 

32__all__ = ["annotate_and_attach", "annotated_variations", "variation_for"] 

33 

34 

35def variation_for(name: str, allele: VariationAllele) -> Variation: 

36 """Return a single-allele variation with a corpus allele's coordinates and alleles. 

37 

38 An empty allele stays an empty string, the corpus's own spelling, so the rebuilt allele is the 

39 same allele the corpus overlap was annotated against. 

40 """ 

41 allele_string = f"{allele.reference_allele or ''}/{allele.alternate_allele or ''}" 

42 return Variation( 

43 start=allele.start, 

44 end=allele.end, 

45 strand=allele.strand, 

46 slice=allele.slice, 

47 stable_id=name, 

48 allele_string=allele_string, 

49 # A corpus is the input in the commands that read one, so its own slice name is the 

50 # uploaded name -- the one a summary counts by. 

51 uploaded_locus=Locus( 

52 seq_region_name=allele.slice.seq_region_name if allele.slice is not None else None, 

53 start=allele.start, 

54 end=allele.end, 

55 allele_string=allele_string, 

56 ), 

57 ) 

58 

59 

60def annotate_and_attach( 

61 annotator: Annotator[OverlapConsequence], 

62 allele: VariationAllele, 

63 overlaps: Sequence[TranscriptVariationAllele], 

64) -> None: 

65 """Annotate an allele's overlaps in one batch and attach them, consequences first. 

66 

67 The order is load-bearing. ``VariationAllele.transcript_variation_alleles`` carries 

68 ``on_setattr=invalidates("_overlap_consequences")``, and that aggregate is built lazily from 

69 the overlaps' own consequences. Assigning the consequences first means the aggregate is always 

70 computed from complete overlaps; the other way round, anything reading it in between would 

71 cache a stale, empty answer. 

72 

73 Args: 

74 annotator (Annotator[OverlapConsequence]): The annotator to run. 

75 allele (VariationAllele): The allele the overlaps belong to. An allele with no overlaps is 

76 attached an empty list, which is what makes it intergenic downstream. 

77 overlaps (Sequence[TranscriptVariationAllele]): The allele's overlaps, each already built 

78 against ``allele``. 

79 """ 

80 batch = list(overlaps) 

81 results = annotator.annotate_feature_overlaps(batch) if batch else [] 

82 for overlap, consequences in zip(batch, results, strict=True): 

83 overlap.overlap_consequences = list(consequences) 

84 allele.transcript_variation_alleles = batch 

85 

86 

87def annotated_variations(corpus: Corpus, annotator: Annotator[OverlapConsequence]) -> Iterator[Variation]: 

88 """Yield one annotated variation per generated variant, in corpus order. 

89 

90 A variant's overlaps are contiguous in a corpus -- one per feature in its flank window -- so 

91 grouping by name preserves their order. Variants with no overlaps are still yielded with an 

92 empty attached-overlap collection. 

93 

94 Args: 

95 corpus (Corpus): The corpus to annotate. 

96 annotator (Annotator[OverlapConsequence]): The annotator to run. 

97 

98 Yields: 

99 Variation: Each variant, its allele's overlaps rebuilt against it and annotated. 

100 """ 

101 grouped: dict[str, list[TranscriptVariationAllele]] = {} 

102 for name, overlap in corpus.named_overlaps: 

103 grouped.setdefault(name, []).append(overlap) 

104 for name, source_allele in corpus.named_variants: 

105 variation = variation_for(name, source_allele) 

106 allele = variation.variation_alleles[0] 

107 rebuilt = [ 

108 TranscriptVariationAllele(feature=overlap.feature, overlapping_feature=allele) 

109 for overlap in grouped.get(name, []) 

110 ] 

111 annotate_and_attach(annotator, allele, rebuilt) 

112 yield variation