ensembl.vep.models.sequence package#

Sequence related data models.

Submodules#

ensembl.vep.models.sequence.circular_slice module#

CircularSlice class.

class ensembl.vep.models.sequence.circular_slice.CircularSlice(*, coord_system, seq_region_name, start, end, strand, sequence_source=None)[source]#

Bases: Slice

Represents a circular genomic region.

is_circular()[source]#

Indicates whether the slice is circular.

Returns:

True, indicating the slice is circular.

Return type:

bool

ensembl.vep.models.sequence.coord_system module#

CoordSystem class.

class ensembl.vep.models.sequence.coord_system.CoordSystem(*, name, level, default=False, alias_to=None, rank=None)[source]#

Bases: object

Represents a coordinate system in the Ensembl database.

Variables:
  • name (str) – Name of the coordinate system.

  • level (CoordSystemLevel) – Level of the coordinate system (e.g. chromosome, contig, toplevel).

  • default (bool) – Whether this is the default coordinate system.

  • alias_to (str | None) – Name of another coordinate system this is an alias to, if applicable.

  • rank (int | None) – Rank of the coordinate system.

alias_to: str | None#
default: bool#
property is_chromosome_level: bool#

Returns True if the coordinate system is chromosome-level, False otherwise.

Returns:

True if chromosome-level, False otherwise.

Return type:

bool

property is_sequence_level: bool#

Returns True if the coordinate system is sequence-level (e.g. contig), False otherwise.

Returns:

True if sequence-level, False otherwise.

Return type:

bool

property is_toplevel: bool#

Returns True if the coordinate system is toplevel, False otherwise.

Returns:

True if toplevel, False otherwise.

Return type:

bool

level: CoordSystemLevel#
name: str#
rank: int | None#

ensembl.vep.models.sequence.mapper module#

Genomic <-> CDS <-> protein coordinate mapping for a coding region of a transcript.

Built from a transcript’s CDS genomic spans and strand only. Stays reusable outside a Transcript entirely.

Always builds a small (O(exon count)) bisect index at construction: - prefix sums over the spans, walked in transcript order (reversed on the reverse strand) - the span in genomic ascending order and related prefix sums.

Can optionally use a fully-expanded offset<->position index for O(1) lookups, at the cost of higher memory usage. Measured ~15MB for a titin-scale (109 kb, 363-exon) CDS, against ~52 KiB for using bisect. A byte-budget LRU caches/mapper_cache.py can limit memory usage for the index.

class ensembl.vep.models.sequence.mapper.Mapper(*, genomic_spans, strand, coding_start_offset, coding_end_offset, padding=0)[source]#

Bases: object

Genomic <-> offset coordinate mapping over an ordered list of genomic spans.

Deliberately generic: the spans can be a transcript’s CDS blocks (Transcript.cds_mapper), its whole spliced exon set (Transcript.cdna_mapper), or any other ordered block list. “Offset” therefore means whatever the caller built the mapper from, CDS-relative in the first case, cDNA-relative in the second.

The exception is the codon and protein methods (codon_window, codon_genomic_spans, offset_to_protein_offset, protein_offset_to_span). Those divide offsets by three and so only mean anything on a mapper built over CDS spans; calling them on a cDNA mapper is a caller error, not something this class can detect.

padding offsets come before the first genomic base and map to none. A CDS whose 5’ end is incomplete starts part-way through a codon; VEP pads it with that many N so the reading frame, the codon numbering and the CDS positions it reports all count from a whole codon.

Variables:
  • genomic_spans (list[tuple[int, int]]) – The genomic spans, in transcript (5’ to 3’) order. Index 0 is always the first span. Genomic-descending on the reverse strand.

  • strand (Strand) – The owning transcript’s strand.

  • coding_start_offset (int) – 0-based offset to the first coding base

  • coding_end_offset (int) – 0-based offset to the last coding base

  • padding (int) – Offsets before the first genomic base, 0 for a CDS starting on a whole codon.

build_index()[source]#

Build the O(length) accelerator.

Returns:

The fully-expanded offset<->genomic position structures.

Return type:

MapperIndex

coding_end_offset: int#
coding_start_offset: int#
codon_genomic_spans(offset)[source]#

Genomic spans of the whole codon containing offset.

A single (min, max) envelope silently excludes an intron the codon straddles. Therefore the three individual genomic positions are grouped into maximal runs of consecutive positions, so a codon split across an exon-exon junction comes back as two (or in an extreme case, three) disjoint spans.

Parameters:

offset (int) – Any 0-based CDS offset within the codon.

Returns:

Ascending genomic spans, or None if none of the three bases

map to a genomic position.

Return type:

list[tuple[int, int]] | None

codon_window(offsets)[source]#

0-based CDS half-open offsets [start, end) extending to whole codon(s) window.

Parameters:

offsets (tuple[int, int] | None) – (low, high) 0-based CDS offsets.

Returns:

The codon-aligned window, or None if offsets is None or

the window is empty (e.g. the offsets range is entirely past length).

Return type:

tuple[int, int] | None

genomic_span_to_offsets(min_position, max_position)[source]#

Map a genomic span [min_position, max_position] to CDS/cDNA offsets [low, high].

Equivalent to calling genomic_to_offset on every genomic position in the span and taking the min/max of whatever is overlapping the CDS/cDNA. But instead of calling for every genomic position in the span, this uses a bisect search to find the overlapping coding spans and then calculates the offsets from the span’s prefix sums and the portion overlapping with the span.

Suitable for a variant’s genomic span, which may be very large if it is structural.

Parameters:
  • min_position (int) – Lower bound (inclusive) of the genomic span, 1-based.

  • max_position (int) – Upper bound (inclusive) of the genomic span, 1-based.

Returns:

(low, high) 0-based CDS/cDNA offsets, or None if the span touches

no CDS/cDNA spans at all.

Return type:

tuple[int, int] | None

genomic_spans: list[tuple[int, int]]#
genomic_to_offset(genomic_position, *, accelerator=None)[source]#

Map a genomic position to its 0-based CDS/cDNA offset.

Parameters:
  • genomic_position (int) – A genomic position, 1-based.

  • accelerator (MapperIndex | None, default: None) – If given and it covers this CDS/cDNA, answers in O(1) via dict lookup; otherwise falls back to an O(log exon count) bisect search.

Returns:

The 0-based offset into the CDS/cDNA, or None if the position is not in the CDS/cDNA.

Return type:

int | None

property length: int#

Total number of nucleotides covered by genomic_spans.

Returns:

Length in bases, padding included (just padding if genomic_spans is

empty). CDS length on a CDS mapper, cDNA length on a cDNA mapper.

Return type:

int

offset_to_genomic(offset, *, accelerator=None)[source]#

Map a 0-based CDS/cDNA offset to its genomic position.

Parameters:
  • offset (int) – 0-based offset into the CDS/cDNA.

  • accelerator (MapperIndex | None, default: None) – If given, answers in O(1) via list index; otherwise falls back to an O(log exon count) bisect search.

Returns:

The genomic position, or None if the offset is out of range or padding.

Return type:

int | None

offset_to_protein_offset(offset)[source]#

Map a 0-based CDS offset to its 0-based protein (residue) offset.

Parameters:

offset (int) – 0-based offset into the CDS.

Returns:

0-based residue index, or None if offset is out of range.

Return type:

int | None

padding: int#
protein_offset_to_span(protein_offset)[source]#

Map a 0-based protein (residue) offset to its codon’s CDS-offset span.

Parameters:

protein_offset (int) – 0-based residue index.

Returns:

Inclusive 0-based (low, high) CDS-offset span of the

residue’s codon (clipped to length for a partial final codon), or None if protein_offset is negative or starts beyond the CDS.

Return type:

tuple[int, int] | None

strand: Strand#
class ensembl.vep.models.sequence.mapper.MapperIndex(*, offset_to_position, position_to_offset)[source]#

Bases: object

Index holding the offset<->genomic position mapping.

Turns O(log exon count) bisect lookup of Mapper into an O(1) lookup, at the cost of holding this O(length) structure in memory.

Variables:
  • offset_to_position (list[int]) – 0-based CDS offset -> Genomic position, index i is the genomic position of CDS offset padding + i (see Mapper.padding).

  • position_to_offset (dict[int, int]) – Genomic position -> 0-based CDS offset, inverse of offset_to_position.

offset_to_position: list[int]#
position_to_offset: dict[int, int]#

ensembl.vep.models.sequence.slice module#

Slice class.

class ensembl.vep.models.sequence.slice.Slice(*, coord_system, seq_region_name, start, end, strand, sequence_source=None)[source]#

Bases: UnsafeSetterMixin

Represents a genomic region on a sequence.

Variables:
  • coord_system (CoordSystem) – Coordinate system.

  • seq_region_name (str) – Sequence region name.

  • start (int) – Start position relative to sequence region.

  • end (int) – End position relative to sequence region.

  • strand (Strand) – Slice orientation relative to sequence region.

  • seq (str | None) – Sequence of the slice.

  • seq_region_length (int | None) – Sequence region length.

  • sequence_source (SequenceSource | None) – Lazy sequence provider.

coord_system: CoordSystem#
end: int#
is_circular()[source]#

Indicate whether the slice is circular.

Returns:

False for regular slices.

Return type:

bool

property seq: str | None#

Public accessor for the _seq

Returns:

The resolved sequence, or None if unavailable.

Return type:

str | None

property seq_region_length: int | None#

Public accessor for the _seq_region_length.

Returns:

The sequence region length, or None if unavailable.

Return type:

int | None

seq_region_name: str#
sequence_source: SequenceSource | None#
start: int#
strand: Strand#
subseq(start, end, strand=Strand.FORWARD)[source]#

Return the sequence of a span given in slice-relative coordinates.

A span inside a linear, source-backed slice is read straight from the source, so a feature on a whole-chromosome slice never materialises the chromosome. Anything else – a circular slice, whose span may wrap the origin, or a span reaching outside this one – is cut from seq.

Parameters:
  • start (int) – Span start relative to the slice, 1-based inclusive.

  • end (int) – Span end relative to the slice, 1-based inclusive.

  • strand (Strand, default: Strand.FORWARD) – Orientation of the returned sequence relative to the slice.

Returns:

The span sequence, or None if the slice has no sequence or no strand to

measure the span against.

Return type:

str | None