Coverage for src/python/ensembl/vep/common/vcf.py: 96%
35 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-18 14:41 +0000
« 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"""How VCF spells a set of alleles: the trimming VEP applies, and the padding base an indel needs."""
16from __future__ import annotations
18from collections.abc import Callable, Sequence
20from ensembl.vep.common.constants import EMPTY_ALLELE, VCF_BASES, VCF_PLAIN_ALLELE_CHARACTERS
22__all__ = ["trim_right", "vcf_record_alleles"]
25def trim_right(alleles: Sequence[str]) -> list[str]:
26 """Remove the bases every allele ends with, never trimming an allele away entirely.
28 A port of ensembl-variation's ``Utils::Sequence::trim_right``: trimming stops as soon as any
29 allele is down to one base, or the alleles no longer share their last base.
30 """
31 trimmed = list(alleles)
32 if len(trimmed) < 2:
33 return trimmed
34 while all(len(allele) > 1 for allele in trimmed) and len({allele[-1] for allele in trimmed}) == 1:
35 trimmed = [allele[:-1] for allele in trimmed]
36 return trimmed
39def _is_bases(allele: str) -> bool:
40 """Return whether an allele is empty or plain bases, as opposed to symbolic."""
41 return allele == "" or set(allele) <= VCF_BASES
44def vcf_record_alleles(
45 start: int, alleles: Sequence[str], base_at: Callable[[int], str]
46) -> tuple[int, list[str]]:
47 """Return the ``POS`` and the alleles a VCF record spells a short variant with.
49 A port of ensembl-variation's ``VariationFeature::to_VCF_record``. Alleles of length one --
50 SNVs and MNVs -- are written as they are. Otherwise the common trailing bases are trimmed, and a
51 padding base is added unless every allele already starts with the same base. A symbolic allele
52 is wrapped in angle brackets.
54 One deliberate difference: at position 1 there is no preceding base, and Perl takes whatever the
55 slice returns there. The VCF specification pads with the *following* base instead (VCF v4.5
56 specification, §1.6.1 “Fixed fields”, item 4 “REF”), and so does this.
58 Args:
59 start (int): 1-based start, in VEP's trimmed convention -- an insertion sits before
60 ``start``.
61 alleles (Sequence[str]): The reference first, then the alternates; ``-`` or an empty string
62 for an empty allele.
63 base_at (Callable[[int], str]): Returns the reference base at a 1-based position, and is
64 only called when a padding base is needed.
66 Returns:
67 tuple[int, list[str]]: The record's position, and its alleles with the reference first.
68 """
69 cleaned = [allele.replace(EMPTY_ALLELE, "") for allele in alleles]
70 symbolic = any(allele and not set(allele) <= VCF_PLAIN_ALLELE_CHARACTERS for allele in cleaned)
71 if not symbolic and len({len(allele) for allele in cleaned}) == 1:
72 return start, cleaned
74 if not symbolic:
75 cleaned = trim_right(cleaned)
76 if len(cleaned) == 1: 76 ↛ 77line 76 didn't jump to line 77 because the condition on line 76 was never true
77 cleaned.insert(0, "")
79 first_bases = {allele[:1] for allele in cleaned if "*" not in allele}
80 position = start
81 before = ""
82 after = ""
83 if len(first_bases) > 1:
84 if start > 1:
85 position = start - 1
86 before = base_at(position)
87 else:
88 after = base_at(start + len(cleaned[0]))
90 spelled = []
91 for allele in cleaned:
92 spelled.append(before + allele + after if _is_bases(allele) else f"<{allele}>")
93 return position, spelled