Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify protein variant viewer, bugfixes #407

Merged
merged 4 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/user-guide/exploratory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ This code will produce the following table on the basis of a cohort of individua
with variants in the *TBX5* gene:

>>> from gpsea.view import ProteinVariantViewer
>>> cpd_viewer = ProteinVariantViewer(tx_id=tx_id, protein_metadata=protein_meta)
>>> cpd_viewer = ProteinVariantViewer(protein_metadata=protein_meta)
>>> report = cpd_viewer.process(cohort)
>>> report # doctest: +SKIP

Expand Down
13 changes: 5 additions & 8 deletions src/gpsea/view/_protein_visualizable.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ def __init__(
variant_regions_on_protein: typing.List[Region] = list()
self._variant_effect = list()
for tx_ann in transcript_annotations:
if tx_ann is None:
continue
variant_effects = tx_ann.variant_effects
if len(variant_effects) == 0:
continue
Expand Down Expand Up @@ -82,24 +84,19 @@ def __init__(
def _get_tx_anns(
variants: typing.Iterable[Variant],
protein_id: str,
) -> typing.Sequence[TranscriptAnnotation]:
) -> typing.Sequence[typing.Optional[TranscriptAnnotation]]:
"""
By default, the API returns transcript annotations for many transcripts.
We would like to store the annotations only for our protein of interest (protein_id)
"""
tx_anns = []
for i, v in enumerate(variants):
for v in variants:
tx_ann = None
for ann in v.tx_annotations:
if ann.protein_id is not None and ann.protein_id == protein_id:
tx_ann = ann
break
if tx_ann is None:
raise ValueError(
f"The transcript annotation for {protein_id} was not found!"
)
else:
tx_anns.append(tx_ann)
tx_anns.append(tx_ann)

return tx_anns

Expand Down
4 changes: 1 addition & 3 deletions src/gpsea/view/_viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,12 +522,10 @@ class ProteinVariantViewer(BaseViewer):
def __init__(
self,
protein_metadata: ProteinMetadata,
tx_id: str,
):
super().__init__()
self._cohort_template = self._environment.get_template("protein.html")
self._protein_meta = protein_metadata
self._tx_id = tx_id

def process(self, cohort: Cohort) -> GpseaReport:
"""
Expand Down Expand Up @@ -561,7 +559,7 @@ def _prepare_context(self, cohort: Cohort) -> typing.Mapping[str, typing.Any]:
# not over *unique* `VariantInfo`s
for var in cohort.all_variants():
target_annot = next(
(x for x in var.tx_annotations if x.transcript_id == self._tx_id), None
(x for x in var.tx_annotations if x.protein_id == self._protein_meta.protein_id), None
)
if target_annot is None:
# structural variants do not have a transcript id, and we skip them
Expand Down
20 changes: 0 additions & 20 deletions tests/view/test_protein_visualizer.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import io
import matplotlib.pyplot as plt
import pytest

from gpsea.model import Cohort, ProteinMetadata
from gpsea.view import (
GpseaReport,
configure_default_protein_visualizer,
BaseProteinVisualizer,
ProteinVariantViewer,
)


Expand All @@ -32,20 +29,3 @@ def test_protein_visualizer(

fig.savefig("protein.png")

def test_protein_viewable(
self,
suox_cohort: Cohort,
suox_protein_metadata: ProteinMetadata,
suox_mane_tx_id: str,
):
protein_viewable = ProteinVariantViewer(
protein_metadata=suox_protein_metadata, tx_id=suox_mane_tx_id
)
report = protein_viewable.process(suox_cohort)
assert isinstance(report, GpseaReport)

buf = io.StringIO()
report.write(buf)
val = buf.getvalue()

assert "gpsea-body" in val
38 changes: 30 additions & 8 deletions tests/view/test_viewers.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
import math
import io
import os

import hpotk
import pandas as pd
import pytest

from gpsea.analysis import StatisticResult
from gpsea.analysis.pcats import HpoTermAnalysisResult
from gpsea.analysis.pcats.stats import FisherExactTest
from gpsea.analysis.clf import GenotypeClassifier, HpoClassifier
from gpsea.analysis.mtc_filter import PhenotypeMtcResult
from gpsea.model import Cohort
from gpsea.model import Cohort, ProteinMetadata
from gpsea.view import (
CohortViewer,
CohortVariantViewer,
GpseaReport,
MtcStatsViewer,
summarize_hpo_analysis,
)
from gpsea.view._viewers import ProteinVariantViewer


@pytest.mark.skip("Just for manual testing and debugging")
Expand Down Expand Up @@ -86,3 +82,29 @@ def test_process(
report = stats_viewer.process(result=hpo_term_analysis_result)
with open("mtc_stats.html", "w") as fh:
report.write(fh)


class TestProteinVariantViewer:

@pytest.fixture(scope="class")
def protein_variant_viewer(
self,
suox_protein_metadata: ProteinMetadata,
) -> ProteinVariantViewer:
return ProteinVariantViewer(
protein_metadata=suox_protein_metadata,
)

def test_process(
self,
suox_cohort: Cohort,
protein_variant_viewer: ProteinVariantViewer,
):
report = protein_variant_viewer.process(suox_cohort)
assert isinstance(report, GpseaReport)

buf = io.StringIO()
report.write(buf)
val = buf.getvalue()

assert "gpsea-body" in val
Loading