Coverage for src/python/ensembl/vep/corpus/exporter/formats/file/vcf.py: 100%

32 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"""VCF rendering for corpora.""" 

15 

16from __future__ import annotations 

17 

18from collections.abc import Iterator, Sequence 

19 

20from ensembl.vep.common.vcf import vcf_record_alleles 

21from ensembl.vep.corpus.base import Corpus 

22from ensembl.vep.models.variation import VariationAllele 

23 

24__all__ = ["VCF_NAME", "vcf_alleles", "vcf_lines"] 

25 

26VCF_NAME = "corpus.vcf" 

27 

28_SOURCE = "ensembl_vep_synthetic" 

29 

30 

31def distinct_variants(corpus: Corpus) -> list[tuple[str, VariationAllele]]: 

32 """Return each variant once, under its name, in generated order.""" 

33 return list(corpus.named_variants) 

34 

35 

36def vcf_alleles(allele: VariationAllele, sequence: str) -> tuple[int, str, str]: 

37 """Return the ``(POS, REF, ALT)`` a VCF record spells an allele with.""" 

38 position, (reference_allele, alternate_allele) = vcf_record_alleles( 

39 allele.start, 

40 (allele.reference_allele or "", allele.alternate_allele or ""), 

41 lambda base_position: sequence[base_position - 1], 

42 ) 

43 return position, reference_allele, alternate_allele 

44 

45 

46def vcf_lines(corpora: Sequence[Corpus]) -> Iterator[str]: 

47 """Yield the VCF records for corpora.""" 

48 yield "##fileformat=VCFv4.2" 

49 yield f"##source={_SOURCE}" 

50 for corpus in corpora: 

51 for sequence_region in corpus.sequence_regions: 

52 yield f"##contig=<ID={sequence_region.seq_region_name},length={sequence_region.end}>" 

53 yield "\t".join(("#CHROM", "POS", "ID", "REF", "ALT", "QUAL", "FILTER", "INFO")) 

54 for corpus in corpora: 

55 for sequence_region in corpus.sequence_regions: 

56 sequence = sequence_region.seq or "" 

57 records = [] 

58 for name, allele in distinct_variants(corpus): 

59 if allele.slice is not sequence_region: 

60 continue 

61 position, reference_allele, alternate_allele = vcf_alleles(allele, sequence) 

62 records.append((position, name, reference_allele, alternate_allele)) 

63 records.sort(key=lambda record: (record[0], record[1])) 

64 for position, name, reference_allele, alternate_allele in records: 

65 yield "\t".join( 

66 ( 

67 sequence_region.seq_region_name, 

68 str(position), 

69 name, 

70 reference_allele, 

71 alternate_allele, 

72 ".", 

73 ".", 

74 ".", 

75 ) 

76 )