Coverage for src/python/ensembl/vep/corpus/overlap.py: 88%

22 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"""Construct corpus transcript overlaps.""" 

15 

16from __future__ import annotations 

17 

18from collections.abc import Sequence 

19 

20from ensembl.vep.corpus.base import NamedOverlap, NamedVariant 

21from ensembl.vep.models.genic import Transcript 

22from ensembl.vep.models.overlap import TranscriptVariationAllele 

23 

24__all__ = ["build_transcript_overlaps"] 

25 

26 

27def build_transcript_overlaps( 

28 named_variants: Sequence[NamedVariant], 

29 transcripts: Sequence[Transcript], 

30 *, 

31 window: int = 5000, 

32) -> tuple[NamedOverlap, ...]: 

33 """Return same-region variant-transcript pairs within ``window`` bases. 

34 

35 Args: 

36 named_variants (Sequence[NamedVariant]): Named alleles in stable corpus order. 

37 transcripts (Sequence[Transcript]): Candidate transcripts in stable corpus order. 

38 window (int): Flanking bases included on either side of each transcript. 

39 

40 Returns: 

41 tuple[NamedOverlap, ...]: Matching pairs in variant then transcript order. 

42 

43 Raises: 

44 ValueError: If ``window`` is negative. 

45 """ 

46 if window < 0: 

47 raise ValueError("window must be zero or greater") 

48 

49 named_overlaps = [] 

50 for name, variant_allele in named_variants: 

51 if variant_allele.slice is None: 51 ↛ 52line 51 didn't jump to line 52 because the condition on line 51 was never true

52 continue 

53 variant_min = min(variant_allele.start, variant_allele.end) 

54 variant_max = max(variant_allele.start, variant_allele.end) 

55 for transcript in transcripts: 

56 if transcript.slice is not variant_allele.slice: 56 ↛ 57line 56 didn't jump to line 57 because the condition on line 56 was never true

57 continue 

58 if variant_max < transcript.start - window or variant_min > transcript.end + window: 

59 continue 

60 named_overlaps.append( 

61 ( 

62 name, 

63 TranscriptVariationAllele( 

64 feature=transcript, 

65 overlapping_feature=variant_allele, 

66 ), 

67 ) 

68 ) 

69 return tuple(named_overlaps)