Coverage for src/python/ensembl/vep/annotators/consequence/facts/kind.py: 100%

93 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"""Facts describing variant and feature kinds.""" 

15 

16from __future__ import annotations 

17 

18from ensembl.vep.annotators.consequence.facts.base import get_fact, register_fact 

19from ensembl.vep.annotators.consequence.facts.helper import ( 

20 reference_allele, 

21 alternate_allele, 

22 alternate_allele_length, 

23 reference_allele_length, 

24 variant_class_so_term, 

25) 

26from ensembl.vep.annotators.consequence.context.fact import FactContext 

27from ensembl.vep.common import ClassSOTerm, CostTier 

28from ensembl.vep.common.constants import EMPTY_ALLELE 

29from ensembl.vep.models.genic import Transcript 

30from ensembl.vep.models.regulatory import Motif, Regulatory 

31from ensembl.vep.models.variation import StructuralVariationAllele 

32from ensembl.vep.models.overlap import FeatureOverlap, TranscriptVariationAllele 

33 

34__all__ = [ 

35 "biotype_miRNA", 

36 "biotype_nmd", 

37 "biotype_non_coding", 

38 "biotype_protein_coding", 

39 "has_translation", 

40 "is_chromosomal_breakpoint", 

41 "is_copy_number_gain", 

42 "is_copy_number_loss", 

43 "is_deletion", 

44 "is_duplication", 

45 "is_indel", 

46 "is_insertion", 

47 "is_motif", 

48 "is_regulatory", 

49 "is_short", 

50 "is_short_deletion", 

51 "is_short_indel", 

52 "is_short_insertion", 

53 "is_snv", 

54 "is_structural", 

55 "is_structural_deletion", 

56 "is_structural_indel", 

57 "is_structural_insertion", 

58 "is_transcript", 

59] 

60 

61# -------------------------------------------------------------------------- 

62# Feature class: what kind of feature is overlapping. 

63# -------------------------------------------------------------------------- 

64 

65 

66@register_fact(CostTier.FREE) 

67def is_transcript(fact_context: FactContext[FeatureOverlap]) -> bool: 

68 """Return whether the overlapped feature is a transcript.""" 

69 return isinstance(fact_context.overlap.feature, Transcript) 

70 

71 

72@register_fact(CostTier.FREE) 

73def is_regulatory(fact_context: FactContext[FeatureOverlap]) -> bool: 

74 """Return whether the overlapped feature is regulatory.""" 

75 return isinstance(fact_context.overlap.feature, Regulatory) 

76 

77 

78@register_fact(CostTier.FREE) 

79def is_motif(fact_context: FactContext[FeatureOverlap]) -> bool: 

80 """Return whether the overlapped feature is a motif.""" 

81 return isinstance(fact_context.overlap.feature, Motif) 

82 

83 

84@register_fact(CostTier.FREE) 

85def is_structural(fact_context: FactContext[FeatureOverlap]) -> bool: 

86 """Return whether the overlapping allele is structural.""" 

87 return isinstance(fact_context.overlap.overlapping_feature, StructuralVariationAllele) 

88 

89 

90@register_fact(CostTier.FREE) 

91def is_short(fact_context: FactContext[FeatureOverlap]) -> bool: 

92 """Return whether the overlapping allele is a short variant.""" 

93 return not isinstance(fact_context.overlap.overlapping_feature, StructuralVariationAllele) 

94 

95 

96# -------------------------------------------------------------------------- 

97# Biotype: what kind of transcript this is. 

98# -------------------------------------------------------------------------- 

99 

100 

101@register_fact(CostTier.FREE, requires=("is_transcript",)) 

102def has_translation(fact_context: FactContext[TranscriptVariationAllele]) -> bool: 

103 """Return whether the transcript has a translation.""" 

104 return fact_context.overlap.feature.translation is not None 

105 

106 

107@register_fact(CostTier.FREE, requires=("is_transcript",)) 

108def biotype_protein_coding(fact_context: FactContext[TranscriptVariationAllele]) -> bool: 

109 """Return whether the transcript has a protein-coding biotype.""" 

110 return fact_context.biotype == "protein_coding" 

111 

112 

113@register_fact(CostTier.FREE, requires=("is_transcript",)) 

114def biotype_nmd(fact_context: FactContext[TranscriptVariationAllele]) -> bool: 

115 """Return whether the transcript has a nonsense-mediated-decay biotype.""" 

116 return fact_context.biotype == "nonsense_mediated_decay" 

117 

118 

119@register_fact(CostTier.FREE, requires=("is_transcript",)) 

120def biotype_non_coding(fact_context: FactContext[TranscriptVariationAllele]) -> bool: 

121 """Return whether the transcript is non-coding.""" 

122 return fact_context.biotype != "protein_coding" and fact_context.overlap.feature.translation is None 

123 

124 

125@register_fact(CostTier.FREE, requires=("is_transcript",)) 

126def biotype_miRNA(fact_context: FactContext[TranscriptVariationAllele]) -> bool: 

127 """Return whether the transcript has a microRNA biotype.""" 

128 return fact_context.biotype == "miRNA" 

129 

130 

131# -------------------------------------------------------------------------- 

132# Variant shape: what kind of change the allele describes. 

133# -------------------------------------------------------------------------- 

134 

135 

136_DUPLICATION_TERMS = frozenset({ClassSOTerm.DUPLICATION, ClassSOTerm.TANDEM_DUPLICATION}) 

137"""Class SO terms that describe a duplicated segment.""" 

138 

139_INSERTION_TERMS = frozenset( 

140 { 

141 ClassSOTerm.INSERTION, 

142 ClassSOTerm.MOBILE_ELEMENT_INSERTION, 

143 ClassSOTerm.ALU_INSERTION, 

144 ClassSOTerm.HERV_INSERTION, 

145 ClassSOTerm.LINE1_INSERTION, 

146 ClassSOTerm.SVA_INSERTION, 

147 } 

148) 

149"""Class SO terms that add sequence without being a duplication or a copy number gain.""" 

150 

151_DELETION_TERMS = frozenset( 

152 { 

153 ClassSOTerm.DELETION, 

154 ClassSOTerm.MOBILE_ELEMENT_DELETION, 

155 ClassSOTerm.ALU_DELETION, 

156 ClassSOTerm.HERV_DELETION, 

157 ClassSOTerm.LINE1_DELETION, 

158 ClassSOTerm.SVA_DELETION, 

159 } 

160) 

161"""Class SO terms that remove sequence without being a copy number loss.""" 

162 

163_CHROMOSOMAL_BREAKPOINT_TERMS = frozenset( 

164 { 

165 ClassSOTerm.CHROMOSOME_BREAKPOINT, 

166 ClassSOTerm.INTERCHROMOSOMAL_BREAKPOINT, 

167 ClassSOTerm.INTRACHROMOSOMAL_BREAKPOINT, 

168 } 

169) 

170"""Class SO terms that describe a chromosomal breakpoint, undirected and both directed forms.""" 

171 

172 

173@register_fact(CostTier.FREE, requires=("is_short",)) 

174def is_short_insertion(fact_context: FactContext[FeatureOverlap]) -> bool: 

175 """Return whether the variant is a short insertion.""" 

176 return reference_allele(fact_context) == EMPTY_ALLELE or alternate_allele_length( 

177 fact_context 

178 ) > reference_allele_length(fact_context) 

179 

180 

181@register_fact(CostTier.FREE, requires=("is_short",)) 

182def is_short_deletion(fact_context: FactContext[FeatureOverlap]) -> bool: 

183 """Return whether the variant is a short deletion.""" 

184 return alternate_allele(fact_context) == EMPTY_ALLELE or reference_allele_length( 

185 fact_context 

186 ) > alternate_allele_length(fact_context) 

187 

188 

189@register_fact(CostTier.FREE, deps=("is_short_insertion", "is_short_deletion"), requires=("is_short",)) 

190def is_short_indel(fact_context: FactContext[FeatureOverlap]) -> bool: 

191 """Return whether the short variant changes allele length.""" 

192 return bool(get_fact(fact_context, "is_short_insertion")) or bool( 

193 get_fact(fact_context, "is_short_deletion") 

194 ) 

195 

196 

197@register_fact(CostTier.FREE, requires=("is_structural",)) 

198def is_structural_insertion(fact_context: FactContext[FeatureOverlap]) -> bool: 

199 """Return whether the structural variant adds sequence.""" 

200 return variant_class_so_term(fact_context) in ( 

201 _INSERTION_TERMS | _DUPLICATION_TERMS | {ClassSOTerm.COPY_NUMBER_GAIN} 

202 ) 

203 

204 

205@register_fact(CostTier.FREE, requires=("is_structural",)) 

206def is_duplication(fact_context: FactContext[FeatureOverlap]) -> bool: 

207 """Return whether the variant represents a duplicated segment.""" 

208 return variant_class_so_term(fact_context) in (_DUPLICATION_TERMS | {ClassSOTerm.COPY_NUMBER_GAIN}) 

209 

210 

211@register_fact(CostTier.FREE, requires=("is_structural",)) 

212def is_copy_number_gain(fact_context: FactContext[FeatureOverlap]) -> bool: 

213 """Return whether the variant represents a copy-number gain.""" 

214 return variant_class_so_term(fact_context) == ClassSOTerm.COPY_NUMBER_GAIN 

215 

216 

217@register_fact(CostTier.FREE, requires=("is_structural",)) 

218def is_structural_deletion(fact_context: FactContext[FeatureOverlap]) -> bool: 

219 """Return whether the structural variant removes sequence.""" 

220 return variant_class_so_term(fact_context) in (_DELETION_TERMS | {ClassSOTerm.COPY_NUMBER_LOSS}) 

221 

222 

223@register_fact(CostTier.FREE, requires=("is_structural",)) 

224def is_copy_number_loss(fact_context: FactContext[FeatureOverlap]) -> bool: 

225 """Return whether the variant represents a copy-number loss.""" 

226 return variant_class_so_term(fact_context) == ClassSOTerm.COPY_NUMBER_LOSS 

227 

228 

229@register_fact(CostTier.FREE, requires=("is_structural",)) 

230def is_chromosomal_breakpoint(fact_context: FactContext[FeatureOverlap]) -> bool: 

231 """Return whether the variant represents a chromosomal breakpoint.""" 

232 return variant_class_so_term(fact_context) in _CHROMOSOMAL_BREAKPOINT_TERMS 

233 

234 

235@register_fact( 

236 CostTier.FREE, 

237 deps=("is_structural_insertion", "is_structural_deletion"), 

238 requires=("is_structural",), 

239) 

240def is_structural_indel(fact_context: FactContext[FeatureOverlap]) -> bool: 

241 """Return whether the structural variant changes sequence length.""" 

242 return bool(get_fact(fact_context, "is_structural_insertion")) or bool( 

243 get_fact(fact_context, "is_structural_deletion") 

244 ) 

245 

246 

247@register_fact(CostTier.FREE, requires=("is_short",)) 

248def is_snv(fact_context: FactContext[FeatureOverlap]) -> bool: 

249 """Return whether the variant is a single-nucleotide substitution.""" 

250 reference_length = reference_allele_length(fact_context) 

251 alternate_length = alternate_allele_length(fact_context) 

252 return reference_length == alternate_length == 1 

253 

254 

255@register_fact(CostTier.FREE, deps=("is_short_insertion", "is_structural_insertion")) 

256def is_insertion(fact_context: FactContext[FeatureOverlap]) -> bool: 

257 """Return whether the variant adds sequence.""" 

258 return bool(get_fact(fact_context, "is_short_insertion")) or bool( 

259 get_fact(fact_context, "is_structural_insertion") 

260 ) 

261 

262 

263@register_fact(CostTier.FREE, deps=("is_short_deletion", "is_structural_deletion")) 

264def is_deletion(fact_context: FactContext[FeatureOverlap]) -> bool: 

265 """Return whether the variant removes sequence.""" 

266 return bool(get_fact(fact_context, "is_short_deletion")) or bool( 

267 get_fact(fact_context, "is_structural_deletion") 

268 ) 

269 

270 

271@register_fact(CostTier.FREE, deps=("is_short_indel", "is_structural_indel")) 

272def is_indel(fact_context: FactContext[FeatureOverlap]) -> bool: 

273 """Return whether the variant changes sequence length.""" 

274 return bool(get_fact(fact_context, "is_short_indel")) or bool( 

275 get_fact(fact_context, "is_structural_indel") 

276 )