Skip to content

Commit

Permalink
Merge pull request #208 from monarch-initiative/fix-some-bugs
Browse files Browse the repository at this point in the history
Fix some bugs
  • Loading branch information
ielis authored Aug 12, 2024
2 parents 078ed03 + aefa055 commit 56a6192
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 55 deletions.
50 changes: 32 additions & 18 deletions src/genophenocorr/model/_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TranscriptAnnotation(TranscriptInfoAware):
variant_effects (Iterable[string]): An iterable of predicted effects given by VEP
affected_exons (Iterable[integer]): An iterable of exons affected by the variant. Returns None if none are affected.
protein_id (string): The protein ID for the protein encoded by the transcript.
hgvsp (Optional[str]): Predicted effect on the encoded protein or `None` if not available`.
protein_effect_coordinates (Region, optional): An optional :class:`Region` with start and end coordinates
of the effect on the protein sequence.
"""
Expand Down Expand Up @@ -158,32 +159,45 @@ def protein_effect_location(self) -> typing.Optional[Region]:
return self._protein_effect_location

def __str__(self) -> str:
return f"TranscriptAnnotation(gene_id:{self.gene_id}," \
f"transcript_id:{self.transcript_id}," \
f"hgvs_cdna:{self.hgvs_cdna}," \
f"is_preferred:{self.is_preferred}," \
f"variant_effects:{self.variant_effects}," \
f"overlapping_exons:{self.overlapping_exons}," \
f"protein_id:{self.protein_id}," \
f"protein_effect_location:{self.protein_effect_location})"
return f"TranscriptAnnotation(gene_id:{self._gene_id}," \
f"transcript_id:{self._tx_id}," \
f"hgvs_cdna:{self._hgvs_cdna}," \
f"is_preferred:{self._is_preferred}," \
f"variant_effects:{self._variant_effects}," \
f"overlapping_exons:{self._affected_exons}," \
f"protein_id:{self._protein_id}," \
f"hgvsp:{self._hgvsp}," \
f"protein_effect_location:{self._protein_effect_location})"

def __eq__(self, other) -> bool:
return isinstance(other, TranscriptAnnotation) \
and self.gene_id == other.gene_id \
and self.hgvs_cdna == other.hgvs_cdna \
and self.is_preferred == other.is_preferred \
and self.transcript_id == other.transcript_id \
and self.variant_effects == other.variant_effects \
and self.overlapping_exons == other.overlapping_exons \
and self.protein_id == other.protein_id \
and self.protein_effect_location == other.protein_effect_location
and self._gene_id == other._gene_id \
and self._tx_id == other._tx_id \
and self._hgvs_cdna == other._hgvs_cdna \
and self._is_preferred == other._is_preferred \
and self._variant_effects == other._variant_effects \
and self._affected_exons == other._affected_exons \
and self._protein_id == other._protein_id \
and self._hgvsp == other._hgvsp \
and self._protein_effect_location == other._protein_effect_location

def __repr__(self) -> str:
return str(self)

def __hash__(self) -> int:
return hash((self.gene_id, self.hgvs_cdna, self.is_preferred, self.transcript_id, self.overlapping_exons, self.variant_effects,
self.protein_id, self.protein_effect_location))
return hash(
(
self._gene_id,
self._tx_id,
self._hgvs_cdna,
self._is_preferred,
self._variant_effects,
self._affected_exons,
self._protein_id,
self._hgvsp,
self._protein_effect_location,
)
)


class VariantClass(enum.Enum):
Expand Down
99 changes: 62 additions & 37 deletions src/genophenocorr/view/_cohort.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from ._formatter import VariantFormatter


ToDisplay = namedtuple('ToDisplay', ['hgvs_cdna', 'hgvsp', 'variant_effects'])

class CohortViewable:
"""
Class to create a viewable object that is uses a Jinja2 template to create an HTML element
Expand All @@ -33,9 +35,9 @@ def __init__(
self._cohort_template = environment.get_template("cohort.html")

def process(
self,
cohort: Cohort,
transcript_id: typing.Optional[str] = None
self,
cohort: Cohort,
transcript_id: typing.Optional[str] = None,
) -> str:
"""
Create an HTML that should be shown with display(HTML(..)) of the ipython package.
Expand All @@ -51,9 +53,9 @@ def process(
return self._cohort_template.render(context)

def _prepare_context(
self,
cohort: Cohort,
transcript_id: typing.Optional[str]
self,
cohort: Cohort,
transcript_id: typing.Optional[str],
) -> typing.Mapping[str, typing.Any]:

hpo_counts = list()
Expand All @@ -63,31 +65,54 @@ def _prepare_context(
hpo_label = "n/a"
if hpo_id in self._hpo:
hpo_label = self._hpo.get_term(hpo_id).name
hpo_counts.append({"HPO": hpo_label, "ID": hpo_id, "Count": individual_count})

var_counts = list()
variant_to_display_d = self.get_variant_description(cohort, transcript_id)
for var in cohort.list_all_variants(top=self._top_variant_count):
chrom_var = var[0]
var_count = var[1]
hpo_counts.append(
{
"HPO": hpo_label,
"ID": hpo_id,
"Count": individual_count,
}
)

variant_counts = list()
variant_to_display_d = CohortViewable._get_variant_description(cohort, transcript_id)
for variant_key, count in cohort.list_all_variants(top=self._top_variant_count):
# get HGVS or human readable variant
display = variant_to_display_d.get(chrom_var, chrom_var)
var_counts.append({"variant": chrom_var, "variant_name": display.hgvs_cdna,\
"protein_name": display.hgvsp, \
"variant_effects": ", ".join(display.variant_effects) if display.variant_effects is not None else None,\
"Count": var_count})

diseases = cohort.list_all_diseases()
n_diseases = len(diseases)
if variant_key in variant_to_display_d:
display = variant_to_display_d[variant_key]
hgvs_cdna = display.hgvs_cdna
protein_name = display.hgvsp
effects = '' if display.variant_effects is None else ", ".join(display.variant_effects)
else:
display = variant_key
hgvs_cdna = ''
protein_name = ''
effects = ''

variant_counts.append(
{
"variant": variant_key,
"variant_name": hgvs_cdna,
"protein_name": protein_name,
"variant_effects": effects,
"Count": count,
}
)

disease_counts = list()
for d in diseases:
disease_id = d[0]
disease_count = d[1]
for disease_id, disease_count in cohort.list_all_diseases():
disease_name = "Unknown"
for dis in cohort.all_diseases():
if dis.identifier == d[0]:
disease_name = dis.name
disease_counts.append({"disease_id": disease_id, "disease_name": disease_name, "count": disease_count})
for disease in cohort.all_diseases():
if disease.identifier == disease_id:
disease_name = disease.name
disease_counts.append(
{
"disease_id": disease_id,
"disease_name": disease_name,
"count": disease_count,
}
)

n_diseases = len(disease_counts)

var_effects_list = list()
if transcript_id is not None:
Expand All @@ -111,7 +136,7 @@ def _prepare_context(
"hpo_counts": hpo_counts,
"unique_variant_count": len(cohort.all_variants()),
"top_var_count": self._top_variant_count,
"var_counts": var_counts,
"var_counts": variant_counts,
"n_diseases": n_diseases,
"disease_counts": disease_counts,
"has_transcript": has_transcript,
Expand All @@ -120,11 +145,11 @@ def _prepare_context(
}

@staticmethod
def get_variant_description(
cohort: Cohort,
transcript_id: typing.Optional[str],
only_hgvs: bool = True,
) -> typing.Mapping[str, namedtuple]:
def _get_variant_description(
cohort: Cohort,
transcript_id: typing.Optional[str],
only_hgvs: bool = True,
) -> typing.Mapping[str, ToDisplay]:
"""
Get user-friendly strings (e.g., HGVS for our target transcript) to match to the chromosomal strings
Args:
Expand All @@ -133,11 +158,11 @@ def get_variant_description(
only_hgvs (bool): do not show the transcript ID part of the HGVS annotation, just the annotation.
Returns:
typing.Mapping[str, namedtuple]: key: chromosomal, value: namedtuple(display (e.g. HGVS) string of variant, hgvsp protein string of variant)
typing.Mapping[str, ToDisplay]: key: variant key, value: namedtuple(display (e.g. HGVS) string of variant, hgvsp protein string of variant)
"""
chrom_to_display = dict()
var_formatter = VariantFormatter(transcript_id)
to_display = namedtuple('to_display', ['hgvs_cdna', 'hgvsp', 'variant_effects'])

for var in cohort.all_variants():
variant_key = var.variant_info.variant_key
display = var_formatter.format_as_string(var)
Expand All @@ -160,6 +185,6 @@ def get_variant_description(
hgvsp = fields_ps[1]
else:
hgvsp = fields_ps[0]
chrom_to_display[variant_key] = to_display(display, hgvsp, var_effects)
chrom_to_display[variant_key] = ToDisplay(display, hgvsp, var_effects)

return chrom_to_display

0 comments on commit 56a6192

Please sign in to comment.