ensembl.vep.sources package#

File-backed and database-backed feature sources.

Subpackages#

Submodules#

ensembl.vep.sources.build module#

Source construction from run configuration.

Two kinds of source, named apart on purpose: the input source holds the variants being annotated, and the annotation sources hold what they are annotated against. Perl VEP draws the same line – its input is a Parser and its annotation sources come from AnnotationSourceAdaptor::get_all, which reads the configuration and returns whichever set of sources it names (a cache, a database, a GFF3/GTF file, custom files).

These are functions rather than a class holding the set: a run’s sources are built once, wired to each other, and then used directly by whoever asked for them. The one thing they must share is the reference sequence and the synonym map, which is why those are built first and passed in.

There is one builder per kind of annotation source rather than one call returning the whole set. Each annotation track builds the source it annotates against, so nothing has to pick its own source out of a list of everything configured.

Which input format to read is decided here, by name, against the closed set in common/constants.py. One conditional in one place, rather than a registry each source registers with: the configuration package offers the same names as --format choices and cannot import the sources, which import it.

ensembl.vep.sources.build.DEFAULT_INPUT_FORMAT = 'vcf'#

What an input file whose name names no format is read as.

ensembl.vep.sources.build.InputSource#

The sources a run can read its variants from.

What a run asks of one: iter_all_records and records_to_features either side of a process boundary, region_of and record_columns to place and echo a record, and header_lines and record_count for what output and progress need before reading.

ensembl.vep.sources.build.TranscriptSource = ensembl.vep.sources.file.gff3.transcript.GFF3TranscriptSource | ensembl.vep.sources.file.gtf.transcript.GTFTranscriptSource#

The file-backed transcript annotation sources.

ensembl.vep.sources.build.build_chromosome_synonyms(config)[source]#

Return the run’s sequence-region synonyms.

Parameters:

config (SourceConfig) – The configured sources.

Returns:

The map --synonyms names, or an empty one when it was not given.

Return type:

ChromosomeSynonyms

ensembl.vep.sources.build.build_input_source(config, *, sequence_source, synonyms)[source]#

Return the source holding the variants being annotated.

Parameters:
Returns:

The input source the configured format reads with.

Return type:

PicklableVCFVariationSource

Raises:

ValueError – If no input file was configured, or its format has no reader.

ensembl.vep.sources.build.build_sequence_source(config)[source]#

Return the reference sequence source every other source shares.

Parameters:

config (SourceConfig) – The configured sources.

Returns:

The reference source.

Return type:

FastaSequenceSource

Raises:

ValueError – If no FASTA was configured.

ensembl.vep.sources.build.build_transcript_source(config, *, sequence_source, synonyms)[source]#

Return the transcript source a run annotates against, wired to resolve names both ways.

Parameters:
  • config (SourceConfig) – The configured sources.

  • sequence_source (SequenceSource | None) – The shared reference source. None is for a caller that can work without reference sequence – vep explain allows it, a run does not – and leaves the source with no sequence and no reference names.

  • synonyms (ChromosomeSynonyms) – The run’s sequence-region synonyms.

Returns:

The transcript source.

Return type:

GFF3TranscriptSource | GTFTranscriptSource

Raises:
  • ValueError – If the transcript files are ambiguous or absent.

  • NotImplementedError – If the configured transcript provider has no reader yet.

ensembl.vep.sources.build.detect_input_format(input_file, configured_format=None)[source]#

Return the format an input file is read as.

A configured format wins, since only the caller knows what an unqualified file holds. An unrecognised name is refused rather than guessed at, but an unrecognised extension falls back to DEFAULT_INPUT_FORMAT: VEP has always read --input_file of any name as VCF, and --format is how another format is named.

Parameters:
  • input_file (Path | str) – The configured input file.

  • configured_format (str | None, default: None) – What --format named, or None.

Returns:

A registered input format name.

Return type:

str

Raises:

ValueError – If a configured format names no registered input source.

ensembl.vep.sources.feature_source module#

The feature source contract.

Reading and model building are two halves. The record getters answer in whatever the source’s own records are – what the format holds, before anything has been interpreted – and records_to_features turns a set of them into models. The feature getters are those two composed, which is why they are concrete here: a source implements the record half and gets the feature half. A format that assembles one feature from several records (GFF3, GTF) overrides the feature getters, because a record at a time is not enough to build anything complete.

class ensembl.vep.sources.feature_source.FeatureSource[R, F: Feature](*, source_type, sequence_source=None, slice_cache=WeakSliceCache())[source]#

Bases: ABC, Generic[R, F]

Source of domain feature objects.

Variables:
  • source_type (SourceType) – Kind of backing source.

  • sequence_source (SequenceSource | None) – Optional reference sequence source.

  • slice_cache (SliceCache) – Shared genomic slice cache.

abstractmethod close()[source]#

Release resources held by this source (e.g., close file handles or DB connections).

get_all_features()[source]#

Return all features from this source.

Return type:

list[F]

abstractmethod get_all_records()[source]#

Return every unconverted record held by this source.

Return type:

list[R]

get_features_by_region(region=None, *, seq_region_name=None, start=None, end=None)[source]#

Return features overlapping a genomic region.

Parameters:
  • region (str | None, default: None) – The region (e.g., “chr1:1000-2000”) to get features from.

  • seq_region_name (str | None, default: None) – The name of the region to get features from.

  • start (int | None, default: None) – The start position of the region to get features from.

  • end (int | None, default: None) – The end position of the region to get features from.

Returns:

A list of features overlapping the specified region.

Return type:

list[F]

abstractmethod get_records_by_region(region=None, *, seq_region_name=None, start=None, end=None, **kwargs)[source]#

Return unconverted records overlapping a genomic region.

Parameters:
  • region (str | None, default: None) – The region (e.g., “chr1:1000-2000”) to get records from.

  • seq_region_name (str | None, default: None) – The name of the region to get records from.

  • start (int | None, default: None) – The start position of the region to get records from.

  • end (int | None, default: None) – The end position of the region to get records from.

  • **kwargs (object) – What a format’s own query accepts, such as the record types a GXF query may filter to before a line is parsed.

Returns:

The records overlapping the specified region.

Return type:

list[R]

iter_all_features()[source]#

Yield all features from this source.

One record at a time, which only a format whose record carries a whole feature can do. A format that assembles a feature from several records overrides this.

Return type:

Iterator[F]

abstractmethod iter_all_records()[source]#

Yield every unconverted record held by this source.

Return type:

Iterator[R]

iter_features_by_region(region=None, *, seq_region_name=None, start=None, end=None)[source]#

Yield features overlapping a genomic region.

Parameters:
  • region (str | None, default: None) – The region (e.g., “chr1:1000-2000”) to get features from.

  • seq_region_name (str | None, default: None) – The name of the region to get features from.

  • start (int | None, default: None) – The start position of the region to get features from.

  • end (int | None, default: None) – The end position of the region to get features from.

Returns:

An iterator over features overlapping the specified region.

Return type:

Iterator[F]

abstractmethod iter_records_by_region(region=None, *, seq_region_name=None, start=None, end=None, **kwargs)[source]#

Yield unconverted records overlapping a genomic region.

Parameters:
  • region (str | None, default: None) – The region (e.g., “chr1:1000-2000”) to get records from.

  • seq_region_name (str | None, default: None) – The name of the region to get records from.

  • start (int | None, default: None) – The start position of the region to get records from.

  • end (int | None, default: None) – The end position of the region to get records from.

  • **kwargs (object) – What a format’s own query accepts.

Returns:

The records overlapping the specified region.

Return type:

Iterator[R]

record_count(*, scan=False)[source]#

Return the declared record count, if available.

Concrete and None by default: a count is optional metadata, not something every source can answer. A source whose format carries one – an indexed file, a table a query can count – overrides this; the rest say they do not know, and a caller such as progress reporting does without rather than paying for a count.

Parameters:

scan (bool, default: False) – Whether to count the hard way when nothing declares it, which for a file-backed source means reading the whole input. Default is False.

Returns:

The record count, or None when this source cannot answer.

Return type:

int | None

abstractmethod records_to_features(records)[source]#

Return the features described by records from this source.

Parameters:

records (list[R]) – The records to convert, in the order the source read them.

Returns:

The features they describe.

Return type:

list[F]

sequence_source: SequenceSource | None#
slice_cache: SliceCache#
source_type: SourceType#
class ensembl.vep.sources.feature_source.R#

One of the source’s own records, as its format holds it.

alias of TypeVar(‘R’)

ensembl.vep.sources.sequence_source module#

Abstract base for sequence sources.

class ensembl.vep.sources.sequence_source.SequenceSource(*, cache=None)[source]#

Bases: ABC

Unified interface for sources that provide sequence for slices.

cache: SequenceCache | None#
clear_cache()[source]#

Clear derived sequence caches.

abstractmethod close()[source]#

Release any resources owned by the source.

get_seq_region_length(seq_region_name)[source]#

Return the length of a sequence region, caching when available.

Parameters:

seq_region_name (str) – The sequence region name.

Returns:

The sequence region length, or None if unavailable.

Return type:

int | None

get_sequence_by_start_end_strand(seq_region_name, start, end, *, strand=Strand.FORWARD)[source]#

Return sequence from a sequence region for the specified coordinates and strand.

Parameters:
  • seq_region_name (str) – The sequence region name.

  • start (int) – Start coordinate for the sequence span.

  • end (int) – End coordinate for the sequence span.

  • strand (Strand, default: Strand.FORWARD) – Requested strand orientation.

Returns:

The requested sequence, or None if unavailable.

Return type:

str | None

property sequence_region_names: frozenset[str]#

Return the sequence-region names this source declares.

These are the reference spelling of a run: every model slice is built in it, and this source is asked for sequence by it. A source that can list its regions up front – a FASTA’s index, a table of them – overrides this; the rest declare none, which leaves every uploaded name resolving to itself.

Returns:

The declared names, empty when the source lists none.

Return type:

frozenset[str]

ensembl.vep.sources.synonym module#

Sequence-region synonyms, and resolving a name against one source’s own spellings.

class ensembl.vep.sources.synonym.ChromosomeSynonyms(*, by_name=dict())[source]#

Bases: object

Sequence-region names that name the same region, held once for the whole run.

Perl VEP keeps one synonym map per run and asks it for every source (BaseVEP:: chromosome_synonyms); a name is not a synonym of a source, it is a synonym full stop, and which spelling a given source wants is decided by that source’s own valid names. Keeping one map is what lets 22 -> NC_000022.11 serve the FASTA and the transcript file at once, in whichever direction each of them needs.

Variables:

by_name (Mapping[str, frozenset[str]]) – Every name, mapped to the other names for it.

by_name: Mapping[str, frozenset[str]]#
classmethod from_file(path)[source]#

Read a synonyms file in Perl VEP’s format.

Each line lists whitespace-separated names for one region. The first name is the reference spelling and every later name is a synonym of it, recorded in both directions – as Perl does, so two synonyms of one reference name are not made synonyms of each other.

Parameters:

path (Path | str) – The file to read.

Returns:

The map the file describes.

Return type:

ChromosomeSynonyms

Raises:

OSError – If the file cannot be read. Allowed to propagate: its message names the path, and a synonyms file the caller asked for and that is not there is not a run to continue silently.

synonyms_of(name)[source]#

Return the other names for a region.

Parameters:

name (str) – The name to look up.

Returns:

Its synonyms, empty when the map has none.

Return type:

frozenset[str]

class ensembl.vep.sources.synonym.SequenceRegionResolver(*, source_names, synonyms=ChromosomeSynonyms())[source]#

Bases: object

Resolve uploaded sequence-region names against one source.

Perl’s BaseVEP::get_source_chr_name in our shape: an exact source name wins, then a synonym the source declares, then the conventional case-insensitive chr prefix adjustment, and an unresolved name keeps its uploaded spelling. Resolution is memoised per instance, which is why each source gets its own resolver over the shared synonym map.

Variables:
  • source_names (frozenset[str]) – Names this source declares.

  • synonyms (ChromosomeSynonyms) – The run’s synonym map, shared by every source.

resolve(uploaded_name)[source]#

Return the source sequence-region name for an uploaded name.

Return type:

str

source_names: frozenset[str]#
synonyms: ChromosomeSynonyms#