Coverage for src/python/ensembl/vep/sources/file/gtf/transcript.py: 100%
26 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"""GTF-backed transcript feature source."""
16from __future__ import annotations
18from typing import ClassVar
20from attrs import define, field
22from ensembl.vep.parsers import GTFParser, GTFRecord
23from ensembl.vep.sources.file.gxf.transcript import GXFTranscriptSource
26@define(kw_only=True, auto_attribs=True)
27class GTFTranscriptSource(GXFTranscriptSource[GTFParser]):
28 """Transcript source backed by a GTF file.
30 Attributes:
31 adaptor: The parser adaptor used to stream GTF records.
32 _PARENTS: Mapping of feature types to their parent feature types.
33 """
35 transcript_feature_types: ClassVar[frozenset[str]] = frozenset({"transcript"})
36 coding_feature_types: ClassVar[frozenset[str]] = frozenset({"cds", "stop_codon"})
37 adaptor: GTFParser = field(init=False, factory=GTFParser)
38 _PARENTS: ClassVar[dict[str, str]] = {
39 "exon": "transcript",
40 "cds": "transcript",
41 "stop_codon": "transcript",
42 "transcript": "gene",
43 }
45 def _query_typed_records(
46 self,
47 region: str,
48 *,
49 feature_types: frozenset[str] | None = None,
50 ) -> list[GTFRecord]:
51 """Seek the persistent GTF query parser and read its records."""
53 query_parser = self._open_query_parser()
54 if not query_parser.seek(region):
55 return []
56 return query_parser.records(limit=None, feature_types=feature_types)
58 def get_record_id(self, record: GTFRecord) -> str:
59 """Return the GTF record identifier for a record.
61 GTF stores identifiers in attributes named after the feature type, such
62 as `gene_id`, `transcript_id`, or `exon_id`.
64 Args:
65 record: The record to inspect.
67 Returns:
68 str: The feature-specific identifier.
69 """
71 _id = self._first_attr(record, f"{record.feature_type.lower()}_id")
72 if _id is None:
73 _id = f"{record.feature_type.lower()}:{record.seqid}:{record.start}-{record.end}"
74 return _id
76 def get_parent_ids(self, record: GTFRecord) -> tuple[str, ...]:
77 """Return the GTF parent identifiers for a record, if any exist.
79 Args:
80 record: The record to inspect.
82 Returns:
83 tuple[str, ...]: A tuple of parent identifiers, or an empty tuple when the record has
84 no parent feature.
85 """
87 parent_feature_type = self._PARENTS.get(record.feature_type.lower())
88 if parent_feature_type is None:
89 return ()
90 return tuple(record.attribute_values(f"{parent_feature_type}_id"))