Coverage for src/python/ensembl/vep/config/sections/common/source.py: 100%
44 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"""Variant and annotation source configuration."""
16from __future__ import annotations
18from collections.abc import Iterable
19from pathlib import Path
20from typing import ClassVar
22from attrs import define, field
24from ensembl.vep.common.constants import DEFAULT_DISTANCE, INPUT_FORMAT_EXTENSIONS
25from ensembl.vep.config.option_spec import option
26from ensembl.vep.config.sections.base import ConfigSection
28__all__ = ["SourceConfig"]
31def _input_formats() -> tuple[str, ...]:
32 """Return the input formats a run can read."""
33 return tuple(sorted(INPUT_FORMAT_EXTENSIONS))
36def _as_distances(value: int | str | Iterable[int]) -> tuple[int, ...]:
37 """Return a distance given as one number, a comma-separated string, or a pair, as a tuple."""
39 if isinstance(value, str):
40 return tuple(int(piece.strip()) for piece in value.split(",") if piece.strip())
41 if isinstance(value, int):
42 return (value,)
43 return tuple(int(piece) for piece in value)
46@define(frozen=True, kw_only=True, auto_attribs=True)
47class SourceConfig(ConfigSection):
48 """Input variant and annotation source configuration.
50 Attributes:
51 input_file (Path | None): Variant input, or None to use a synthetic corpus instead.
52 format (str | None): Input format, or None to infer from the file extension. Perl VEP
53 spells it ``--format``; ``--input_format`` is accepted as a synonym.
54 gff (Path | None): GFF3 file of transcripts.
55 gtf (Path | None): GTF file of transcripts.
56 fasta (Path | None): Reference FASTA, required alongside ``gff``/``gtf`` for sequence.
57 synonyms (Path | None): File of sequence-region synonyms, one region per line: a reference
58 name followed by a synonym, whitespace-separated. Perl VEP's ``--synonyms`` format.
59 cache (bool): Annotate from a VEP cache, a switch as in Perl VEP; the directory is
60 ``dir_cache``. Accepted, not implemented yet.
61 dir_cache (Path | None): Directory of the VEP cache.
62 database (bool): Annotate from an Ensembl database. Accepted, not implemented yet.
63 custom (tuple[str, ...]): Custom annotation specs, one per ``--custom`` occurrence and each
64 kept whole (Perl VEP's spec is itself comma-separated). Accepted, not implemented yet.
65 distance (tuple[int, ...]): How far outside a feature a variant may fall and still be
66 paired with it, as Perl VEP's ``--distance``: one value for both directions, or
67 ``upstream,downstream``. ``--flank`` is accepted as a synonym. Read through
68 ``upstream_distance`` and ``downstream_distance`` rather than directly.
69 """
71 section_name: ClassVar[str] = "source"
73 input_file: Path | None = field(default=None, metadata=option("variant input file", short="i"))
74 format: str | None = field(
75 default=None,
76 metadata=option(
77 "input format, inferred from the file extension when omitted",
78 synonyms=("input_format",),
79 choices=_input_formats(),
80 ),
81 )
82 gff: Path | None = field(default=None, metadata=option("GFF3 file of transcripts"))
83 gtf: Path | None = field(default=None, metadata=option("GTF file of transcripts"))
84 fasta: Path | None = field(default=None, metadata=option("reference FASTA"))
85 synonyms: Path | None = field(
86 default=None, metadata=option("file of sequence-region synonyms, one region per line")
87 )
88 cache: bool = field(default=False, metadata=option("annotate from a VEP cache (not implemented yet)"))
89 dir_cache: Path | None = field(
90 default=None, metadata=option("directory of the VEP cache (not implemented yet)")
91 )
92 database: bool = field(
93 default=False, metadata=option("annotate from an Ensembl database (not implemented yet)")
94 )
95 custom: tuple[str, ...] = field(
96 default=(),
97 metadata=option(
98 "custom annotation spec, repeatable (not implemented yet)", metavar="SPEC", repeatable=True
99 ),
100 )
101 distance: tuple[int, ...] = field(
102 default=(DEFAULT_DISTANCE,),
103 converter=_as_distances,
104 metadata=option(
105 "distance outside a feature still counted as overlapping, as one value or up,down",
106 synonyms=("flank",),
107 metavar="BP[,DOWNSTREAM]",
108 ),
109 )
111 @property
112 def upstream_distance(self) -> int:
113 """Return bases before a feature's 5' end that still pair with it."""
115 return self.distance[0]
117 @property
118 def downstream_distance(self) -> int:
119 """Return bases past a feature's 3' end that still pair with it."""
121 return self.distance[-1]
123 def __attrs_post_init__(self) -> None:
124 """Reject a distance that names neither one nor two non-negative values."""
126 if not 1 <= len(self.distance) <= 2:
127 raise ValueError("distance takes one value or upstream,downstream")
128 if any(value < 0 for value in self.distance):
129 raise ValueError("distance must be zero or greater")
131 def has_features(self) -> bool:
132 """Return whether a transcript source has been configured.
134 Returns:
135 bool: True if either ``gff`` or ``gtf`` is set.
136 """
137 return self.gff is not None or self.gtf is not None