Coverage for src/python/ensembl/vep/sources/file/gff3/transcript.py: 100%
21 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"""GFF3-backed transcript feature source."""
16from __future__ import annotations
18from typing import ClassVar
20from attrs import define, field
22from ensembl.vep.parsers import GFF3Parser, GFF3Record
23from ensembl.vep.sources.file.gxf.transcript import GXFTranscriptSource
26@define(kw_only=True, auto_attribs=True)
27class GFF3TranscriptSource(GXFTranscriptSource[GFF3Parser]):
28 """Transcript source backed by a GFF3 file.
30 Attributes:
31 adaptor: The parser adaptor used to stream GFF3 records.
32 """
34 transcript_feature_types: ClassVar[frozenset[str]] = frozenset(
35 {"mrna", "transcript", "lnc_rna", "pseudogenic_transcript", "processed_transcript"}
36 )
37 adaptor: GFF3Parser = field(init=False, factory=GFF3Parser)
39 def _query_typed_records(
40 self,
41 region: str,
42 *,
43 feature_types: frozenset[str] | None = None,
44 ) -> list[GFF3Record]:
45 """Seek the persistent GFF3 query parser and read its records."""
47 query_parser = self._open_query_parser()
48 if not query_parser.seek(region):
49 return []
50 return query_parser.records(limit=None, feature_types=feature_types)
52 def get_record_id(self, record: GFF3Record) -> str | None:
53 """Return the GFF3 record identifier.
55 Args:
56 record: The parser record to inspect.
58 Returns:
59 str | None: The record ID, or the `Name` fallback when the record
60 has no `ID` attribute.
61 """
63 _id = self._first_attr(record, "ID") or self._first_attr(record, "Name")
64 if _id is None:
65 _id = f"{record.feature_type.lower()}:{record.seqid}:{record.start}-{record.end}"
66 return _id
68 def get_parent_ids(self, record: GFF3Record) -> tuple[str, ...]:
69 """Return the GFF3 parent identifiers, if present.
71 Args:
72 record: The parser record to inspect.
74 Returns:
75 tuple[str, ...]: A tuple of parent identifiers, or an empty tuple when the record has
76 no parent.
77 """
79 return tuple(record.attribute_values("Parent"))