# See the NOTICE file distributed with this work for additional information
# regarding copyright ownership.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Completed annotation traces and text rendering.
An ``AnnotationTrace`` is immutable and identifies both its overlap and annotator. Reconstruction
builds it after annotation from the fact memo, rule results, and the small amount of live state in
``TraceRecorder``. Optional event collections use empty tuples so consumers can iterate without
special cases.
"""
from __future__ import annotations
import logging
from collections.abc import Iterator
from contextlib import contextmanager
from attrs import field, frozen
from ensembl.vep.common import CostTier
from ensembl.vep.common.constants import UNKNOWN_VALUE
from ensembl.vep.trace.events import FactEvent, FactOrigin, PruneEvent, RuleEvent
__all__ = [
"AnnotationTrace",
"annotation_tracing",
"describe_overlap",
]
[docs]
@frozen(kw_only=True, auto_attribs=True)
class AnnotationTrace:
"""Record one overlap's derivation.
Attributes:
fact_events (tuple[FactEvent, ...]): Facts touched by the derivation, in materialisation
order, followed by facts proved false through ``requires`` propagation.
rule_events (tuple[RuleEvent, ...]): Every rule's verdict, in rank order.
prune_events (tuple[PruneEvent, ...]): Every subsumption decision.
emitted_terms (tuple[str, ...]): Terms remaining after subsumption.
annotator_name (str): Registered annotator name.
overlap_label (str): One-line description of the annotated overlap.
"""
fact_events: tuple[FactEvent, ...] = field(default=())
rule_events: tuple[RuleEvent, ...] = field(default=())
prune_events: tuple[PruneEvent, ...] = field(default=())
emitted_terms: tuple[str, ...] = field(default=())
annotator_name: str = field(default="")
overlap_label: str = field(default="")
[docs]
def events_by_origin(self, origin: FactOrigin) -> tuple[FactEvent, ...]:
"""Return the fact events with one origin, in trace order."""
selected = []
for event in self.fact_events:
if event.origin is origin:
selected.append(event)
return tuple(selected)
@property
def computed_cost(self) -> int:
"""Return the declared cost charged by this derivation."""
return sum(event.cost for event in self.fact_events if event.origin is FactOrigin.COMPUTED)
@property
def avoided_cost(self) -> int:
"""Return the declared cost avoided through ``requires`` propagation."""
return sum(event.cost for event in self.fact_events if event.origin is FactOrigin.FORCED_FALSE)
class _SuppressAnnotatorDebugFilter(logging.Filter):
"""Suppress annotator debug records when the caller renders traces."""
def filter(self, record: logging.LogRecord) -> bool:
"""Return whether a record should be emitted."""
return not (record.levelno == logging.DEBUG and record.name.startswith("ensembl.vep.annotators"))
[docs]
@contextmanager
def annotation_tracing(*, quiet: bool) -> Iterator[None]:
"""Enable annotator tracing for the duration of a context.
Tracing uses the annotator logger level instead of adding a second flag to fact evaluation.
Quiet mode suppresses the annotator's debug rendering when the caller renders the same trace.
Args:
quiet (bool): Whether to suppress annotator debug records.
"""
annotators_logger = logging.getLogger("ensembl.vep.annotators")
previous_level = annotators_logger.level
annotators_logger.setLevel(logging.DEBUG)
# The filter goes on the handlers, not the logger: a logger's filters apply only to records
# logged through that logger itself, never to its children's.
handlers = logging.getLogger("ensembl.vep").handlers
log_filter = _SuppressAnnotatorDebugFilter()
if quiet:
for handler in handlers:
handler.addFilter(log_filter)
try:
yield
finally:
for handler in handlers:
handler.removeFilter(log_filter)
annotators_logger.setLevel(previous_level)
[docs]
def describe_overlap(overlap: object) -> str:
"""Return a one-line label for a transcript and variant overlap.
The object is inspected without importing ``TranscriptVariationAllele`` so the trace event
layer remains independent of ``models``. The allele half is its ``display_name``, so a trace
header names the variant the way every other command names it.
Args:
overlap (object): Object exposing transcript and overlapping-allele attributes.
Returns:
str: Label containing the feature and the allele's name when available.
"""
feature = getattr(overlap, "feature", None)
feature_label = getattr(feature, "stable_id", None) or repr(feature)
allele = getattr(overlap, "overlapping_feature", None)
return f"{feature_label} / {getattr(allele, 'display_name', None) or UNKNOWN_VALUE}"