Skip to content

Commit 675b279

Browse files
authored
Merge pull request #407 from monarch-initiative/simplify-protein-variant-viewer
Simplify protein variant viewer, bugfixes
2 parents ae863ee + a6ff4ee commit 675b279

File tree

5 files changed

+37
-40
lines changed

5 files changed

+37
-40
lines changed

docs/user-guide/exploratory.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ This code will produce the following table on the basis of a cohort of individua
148148
with variants in the *TBX5* gene:
149149

150150
>>> from gpsea.view import ProteinVariantViewer
151-
>>> cpd_viewer = ProteinVariantViewer(tx_id=tx_id, protein_metadata=protein_meta)
151+
>>> cpd_viewer = ProteinVariantViewer(protein_metadata=protein_meta)
152152
>>> report = cpd_viewer.process(cohort)
153153
>>> report # doctest: +SKIP
154154

src/gpsea/view/_protein_visualizable.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def __init__(
3939
variant_regions_on_protein: typing.List[Region] = list()
4040
self._variant_effect = list()
4141
for tx_ann in transcript_annotations:
42+
if tx_ann is None:
43+
continue
4244
variant_effects = tx_ann.variant_effects
4345
if len(variant_effects) == 0:
4446
continue
@@ -82,24 +84,19 @@ def __init__(
8284
def _get_tx_anns(
8385
variants: typing.Iterable[Variant],
8486
protein_id: str,
85-
) -> typing.Sequence[TranscriptAnnotation]:
87+
) -> typing.Sequence[typing.Optional[TranscriptAnnotation]]:
8688
"""
8789
By default, the API returns transcript annotations for many transcripts.
8890
We would like to store the annotations only for our protein of interest (protein_id)
8991
"""
9092
tx_anns = []
91-
for i, v in enumerate(variants):
93+
for v in variants:
9294
tx_ann = None
9395
for ann in v.tx_annotations:
9496
if ann.protein_id is not None and ann.protein_id == protein_id:
9597
tx_ann = ann
9698
break
97-
if tx_ann is None:
98-
raise ValueError(
99-
f"The transcript annotation for {protein_id} was not found!"
100-
)
101-
else:
102-
tx_anns.append(tx_ann)
99+
tx_anns.append(tx_ann)
103100

104101
return tx_anns
105102

src/gpsea/view/_viewers.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -522,12 +522,10 @@ class ProteinVariantViewer(BaseViewer):
522522
def __init__(
523523
self,
524524
protein_metadata: ProteinMetadata,
525-
tx_id: str,
526525
):
527526
super().__init__()
528527
self._cohort_template = self._environment.get_template("protein.html")
529528
self._protein_meta = protein_metadata
530-
self._tx_id = tx_id
531529

532530
def process(self, cohort: Cohort) -> GpseaReport:
533531
"""
@@ -561,7 +559,7 @@ def _prepare_context(self, cohort: Cohort) -> typing.Mapping[str, typing.Any]:
561559
# not over *unique* `VariantInfo`s
562560
for var in cohort.all_variants():
563561
target_annot = next(
564-
(x for x in var.tx_annotations if x.transcript_id == self._tx_id), None
562+
(x for x in var.tx_annotations if x.protein_id == self._protein_meta.protein_id), None
565563
)
566564
if target_annot is None:
567565
# structural variants do not have a transcript id, and we skip them
Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import io
21
import matplotlib.pyplot as plt
32
import pytest
43

54
from gpsea.model import Cohort, ProteinMetadata
65
from gpsea.view import (
7-
GpseaReport,
86
configure_default_protein_visualizer,
97
BaseProteinVisualizer,
10-
ProteinVariantViewer,
118
)
129

1310

@@ -32,20 +29,3 @@ def test_protein_visualizer(
3229

3330
fig.savefig("protein.png")
3431

35-
def test_protein_viewable(
36-
self,
37-
suox_cohort: Cohort,
38-
suox_protein_metadata: ProteinMetadata,
39-
suox_mane_tx_id: str,
40-
):
41-
protein_viewable = ProteinVariantViewer(
42-
protein_metadata=suox_protein_metadata, tx_id=suox_mane_tx_id
43-
)
44-
report = protein_viewable.process(suox_cohort)
45-
assert isinstance(report, GpseaReport)
46-
47-
buf = io.StringIO()
48-
report.write(buf)
49-
val = buf.getvalue()
50-
51-
assert "gpsea-body" in val

tests/view/test_viewers.py

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
import math
1+
import io
22
import os
33

44
import hpotk
5-
import pandas as pd
65
import pytest
76

8-
from gpsea.analysis import StatisticResult
97
from gpsea.analysis.pcats import HpoTermAnalysisResult
10-
from gpsea.analysis.pcats.stats import FisherExactTest
11-
from gpsea.analysis.clf import GenotypeClassifier, HpoClassifier
12-
from gpsea.analysis.mtc_filter import PhenotypeMtcResult
13-
from gpsea.model import Cohort
8+
from gpsea.model import Cohort, ProteinMetadata
149
from gpsea.view import (
1510
CohortViewer,
1611
CohortVariantViewer,
12+
GpseaReport,
1713
MtcStatsViewer,
18-
summarize_hpo_analysis,
1914
)
15+
from gpsea.view._viewers import ProteinVariantViewer
2016

2117

2218
@pytest.mark.skip("Just for manual testing and debugging")
@@ -86,3 +82,29 @@ def test_process(
8682
report = stats_viewer.process(result=hpo_term_analysis_result)
8783
with open("mtc_stats.html", "w") as fh:
8884
report.write(fh)
85+
86+
87+
class TestProteinVariantViewer:
88+
89+
@pytest.fixture(scope="class")
90+
def protein_variant_viewer(
91+
self,
92+
suox_protein_metadata: ProteinMetadata,
93+
) -> ProteinVariantViewer:
94+
return ProteinVariantViewer(
95+
protein_metadata=suox_protein_metadata,
96+
)
97+
98+
def test_process(
99+
self,
100+
suox_cohort: Cohort,
101+
protein_variant_viewer: ProteinVariantViewer,
102+
):
103+
report = protein_variant_viewer.process(suox_cohort)
104+
assert isinstance(report, GpseaReport)
105+
106+
buf = io.StringIO()
107+
report.write(buf)
108+
val = buf.getvalue()
109+
110+
assert "gpsea-body" in val

0 commit comments

Comments
 (0)