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

21 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"""The corpus contract.""" 

15 

16from __future__ import annotations 

17 

18from abc import ABC, abstractmethod 

19from ensembl.vep.corpus.spec import CorpusSpec 

20from ensembl.vep.models.genic import Transcript 

21from ensembl.vep.models.overlap import TranscriptVariationAllele 

22from ensembl.vep.models.sequence import Slice 

23from ensembl.vep.models.variation import VariationAllele 

24 

25__all__ = ["Corpus", "NamedOverlap", "NamedVariant"] 

26 

27NamedVariant = tuple[str, VariationAllele] 

28"""A stable corpus name and its variation allele.""" 

29 

30NamedOverlap = tuple[str, TranscriptVariationAllele] 

31"""A corpus variant name and one transcript overlap for its allele.""" 

32 

33 

34class Corpus(ABC): 

35 """Provide inputs for transcript consequence annotation across sequence regions. 

36 

37 Implementations may generate or load their data from any backend. The biological contract is 

38 intentionally transcript-oriented for the annotators available today; adding regulatory or 

39 motif annotation will require a corresponding feature-level extension to this interface. 

40 

41 Attributes: 

42 spec (CorpusSpec): Specification used to construct the corpus. 

43 sequence_regions (tuple[Slice, ...]): Referenced sequence regions in stable order. 

44 named_variants (tuple[NamedVariant, ...]): Stable names paired with variation alleles. 

45 transcripts (tuple[Transcript, ...]): Transcripts in stable order. 

46 named_overlaps (tuple[NamedOverlap, ...]): Named overlaps in stable order. 

47 description (str): Short human-readable corpus description. 

48 """ 

49 

50 @property 

51 @abstractmethod 

52 def spec(self) -> CorpusSpec: 

53 """Return the specification that constructed the corpus.""" 

54 

55 @property 

56 @abstractmethod 

57 def sequence_regions(self) -> tuple[Slice, ...]: 

58 """Return every sequence region referenced by the corpus.""" 

59 

60 @property 

61 @abstractmethod 

62 def named_variants(self) -> tuple[NamedVariant, ...]: 

63 """Return every variant in stable corpus order.""" 

64 

65 @property 

66 @abstractmethod 

67 def transcripts(self) -> tuple[Transcript, ...]: 

68 """Return every transcript in stable corpus order.""" 

69 

70 @property 

71 @abstractmethod 

72 def named_overlaps(self) -> tuple[NamedOverlap, ...]: 

73 """Return every named transcript overlap in stable corpus order.""" 

74 

75 @property 

76 def description(self) -> str: 

77 """Return a short description of the corpus.""" 

78 return f"corpus ({self.spec.kind})" 

79 

80 def close(self) -> None: 

81 """Release resources owned by the corpus.""" 

82 

83 def __enter__(self) -> Corpus: 

84 """Return the corpus for use as a context manager.""" 

85 return self 

86 

87 def __exit__(self, *_exc_info: object) -> None: 

88 """Release resources when leaving a context manager.""" 

89 self.close()