ensembl.vep.annotators.consequence.context package#
Evaluation contexts grouped by lifetime.
FactContext belongs to one variant-feature overlap. AnnotatorContext belongs to an
annotation run and owns caches shared across overlaps.
Submodules#
ensembl.vep.annotators.consequence.context.annotator module#
Run-level state shared across overlap annotations.
FactContext belongs to one variant-feature overlap. AnnotatorContext spans the annotation
run and enables cross-overlap cache reuse.
- class ensembl.vep.annotators.consequence.context.annotator.AnnotatorContext(*, zone_map_cache=None, codon_cache=None, readthrough=Readthrough.NONE, upstream_distance=5000, downstream_distance=5000, mapper_index_cache=None)[source]#
Bases:
objectManage run-scoped caches shared across overlap annotations.
- Variables:
zone_map_cache (ZoneMapCache | None) – Per-transcript positional-fact zone maps or None to disables zone-map prefill.
codon_cache (CodonCache | None) – Shared-codon edit cache or None to disable codon reuse; the relevant codons are then computed per overlap.
readthrough (Readthrough) – How far translation may run past a destroyed stop codon, for the whole run.
upstream_distance (int) – Bases before a feature’s 5’ end that still count as upstream.
downstream_distance (int) – Bases past a feature’s 3’ end that still count as downstream.
mapper_index_cache (MapperIndexCache | None) – Byte-budget LRU of per-transcript genomic<->CDS offset accelerators.
digest_candidate_cache (dict[str, tuple[FactRule, ...]]) – Cheap-guard digest string -> the rules that survive it. Used by the annotators (e.g., linear) that build a guard digest per overlap. Always present – it costs one dict and is never a correctness risk, since the key is a total function of the guard values.
bitmask_candidate_cache (dict[int, tuple[FactRule, ...]]) – Cheap-guard bitmask -> the rules that survive it. The same idea keyed by an int rather than a string, for the annotators that already compute guard flags as bits.
cache_hits (int) – Candidate-cache hits, summed across both candidate caches.
cache_misses (int) – Candidate-cache misses, summed across both candidate caches.
- cache_hits: int#
- cache_misses: int#
- codon_cache: CodonCache | None#
- downstream_distance: int#
- mapper_index_cache: MapperIndexCache | None#
- prepare(fact_context, *, apply_zone_map=True)[source]#
Apply every enabled cache to one overlap, before its facts are computed.
The single place the caches’ ordering is written down. Annotators call this once per overlap and are otherwise cache-agnostic. Turning a cache on or off is a change to the context a caller will make, not to the annotator.
Order is load-bearing and not arbitrary:
Readthrough level first. It changes what the sequence tier will compute, and assigning it invalidates the overlap’s cached CDS alleles – so it has to land before anything reads them, for the same reason the codon cache has to land before the sequence tier are evaluated.
Zone map second. It prefills the positional tier, including
overlap_featureandoverlap_exon, which is exactly what the codon cache’s gate reads next, so running it first makes that gate free rather than evaluated.Codon cache third, and still before any rule is evaluated. Injecting once the sequence tier has already been reached is too late: the evaluation cost has been charged and the cache saves nothing.
- Parameters:
fact_context (
FactContext[TranscriptVariationAllele]) – The overlap about to be evaluated.apply_zone_map (
bool, default:True) – False for a caller that will take the zone map’s answers over a whole batch of overlaps instead (ZoneMapCache.columns), for example, a batch annotator (columnar). Doing both would compute every zone fact twice.
- readthrough: Readthrough#
- reset_candidate_caches()[source]#
Drop both candidate caches and their counters.
The candidate caches are keyed on cheap-guard values for a particular rule set, so a caller that swaps the rule set under a shared context must clear them or the second rule set will be served the first one’s surviving rules.
- upstream_distance: int#
- zone_map_cache: ZoneMapCache | None#
ensembl.vep.annotators.consequence.context.fact module#
Per-overlap fact evaluation and memoisation.
- class ensembl.vep.annotators.consequence.context.fact.FactContext[OverlapT_co: FeatureOverlap](*, overlap, recorder=None, annotator_context=None)[source]#
Bases:
Generic[OverlapT_co]Represent one variant-feature overlap during fact evaluation.
Wraps an overlap and stores its computed facts and other memoised values.
- Variables:
overlap (OverlapT_co) – The variant-feature overlap being annotated. Callers should not replace it because the type parameter is covariant. Runtime immutability is not enforced because
attrs.setters.frozenadds substantial access cost.recorder (TraceRecorder | None) – Opt-in trace gatherer, or None to disable tracing entirely. It collects only what a later reconstruction cannot recover – read counts and which cache served which fact – so the whole cost of tracing on this class is one
is not Nonecheck per fact lookup. See ensembl.vep.trace.annotator_context (AnnotatorContext | None) – The run-scoped state this overlap is being annotated under or None when the context is evaluated standalone (e.g., unit tests).
- annotator_context: AnnotatorContext | None#
- property biotype: str | None#
Return the memoised biotype of the overlapped feature.
If transcript/feature carries no
biotypeof its own in this model then falls back to gene. Resolved once per overlap- Returns:
The biotype, or None if neither the feature nor its gene declares one.
- Return type:
str | None
- classmethod for_overlaps(overlaps)[source]#
Return one context per overlap.
What every caller that evaluates a whole corpus needs – profiling, validation, benchmark gates – rather than each rebuilding the same comprehension.
- Return type:
list[FactContext[OverlapT_co]]
- property memo: dict[str, object]#
Return the fact memo table.
- Returns:
The mutable memo table mapping fact name to computed value.
- Return type:
dict[str, object]
- overlap: OverlapT_co#
- prefill(values, *, origin=FactOrigin.PREFILLED)[source]#
Seed the memo table with already-known values, without charging their cost.
Useful for an annotator that has derived some facts by cheaper means than calling their registered
computefunction, and wants later fact lookups to see those values as already memoised.- Parameters:
values (
Mapping[str,object]) – Fact name -> value pairs to seed the memo table with.origin (
FactOrigin, default:FactOrigin.PREFILLED) – Which uncharged source these values came from, for example FactOrigin.PREFILLED for a zone map or FactOrigin.CACHED for the shared-codon cache. Ignored entirely when self.recorder is None.
- recorder: TraceRecorder | None#
- reset()[source]#
Clear the memo table and any other memoised state.
Lets the context be reused for a new overlap.
- property variant_span: tuple[int, int]#
Return the memoised genomic bounds of the variant.
An insertion is represented with
end == start - 1, sostartalone is not reliably the lower bound, hence the min/max rather than a direct read.- Returns:
variant genomic (min, max) span.
- Return type:
tuple[int, int]
- class ensembl.vep.annotators.consequence.context.fact.OverlapT_co#
The kind of overlap a context carries.
Covariance allows a
FactContext[TranscriptVariationAllele]to be passed to a fact declared overFactContext[FeatureOverlap].alias of TypeVar(‘OverlapT_co’, bound=
FeatureOverlap, covariant=True)