Skip to content

Commit

Permalink
MOI enum
Browse files Browse the repository at this point in the history
  • Loading branch information
pnrobinson committed Jul 13, 2024
1 parent 65026f8 commit 1cc586f
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 76 deletions.
6 changes: 3 additions & 3 deletions docs/user-guide/python_notebook.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The following sections explain how to use Python code to create phenopackets fro
We first import the TemplateImporter to import the data and create phenopackets, and several classes to visualize the data.

```python
from pyphetools.creation import TemplateImporter
from pyphetools.creation import TemplateImporter, Moi
from pyphetools.visualization import IndividualTable, QcVisualizer
from IPython.display import display, HTML
import pyphetools
Expand Down Expand Up @@ -78,11 +78,11 @@ the mode of inheritance (MOI) and then indicate the MOI. If multiple distinct di
Check results of variant encoding.
```python
pmid = "PMID:36333996"
timporter.create_hpoa_from_phenopackets(pmid=pmid, moi="Autosomal recessive")
df = timporter.create_hpoa_from_phenopackets(pmid=pmid, mode_of_inheritance=Moi.AR)
```
or

```python
pmid = "PMID:36333996"
timporter.create_hpoa_from_phenopackets(pmid=pmid, moi="Autosomal recessive", target="OMIM:620427")
df = timporter.create_hpoa_from_phenopackets(pmid=pmid, mode_of_inheritance=Moi.AD, target="OMIM:620427")
```
2 changes: 1 addition & 1 deletion src/pyphetools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from . import validation


__version__ = "0.9.92"
__version__ = "0.9.93"


__all__ = [
Expand Down
1 change: 1 addition & 0 deletions src/pyphetools/creation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .import_template import TemplateImporter
from .individual import Individual
from .metadata import MetaData
from .mode_of_inheritance import Moi
from .option_column_mapper import OptionColumnMapper
from .pyphetools_age import PyPheToolsAge, IsoAge, HpoAge, GestationalAge, HPO_ONSET_TERMS
from .sex_column_mapper import SexColumnMapper
Expand Down
20 changes: 5 additions & 15 deletions src/pyphetools/creation/import_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections import defaultdict
import typing
import phenopackets as PPKt
from .mode_of_inheritance import Moi


class TemplateImporter:
Expand Down Expand Up @@ -245,15 +246,15 @@ def filter_diseases(disease_id, ppkt_list):

def create_hpoa_from_phenopackets(self,
pmid:str,
moi:str,
mode_of_inheritance:Moi,
ppkt_dir:str="phenopackets",
target:str=None) -> pd.DataFrame:
"""Create an HPO annotation (HPOA) file from the current cohort
:param pmid: PubMed id for the mode of inheritance
:type pmid: str
:param moi: Mode of inheritance (Autosomal dominant, Autosomal recessive, etc)
:type moi: str
:param mode_of_inheritance: Mode of inheritance (enumeration)
:type mode_of_inheritance: Moi
:param ppkt_dir: Directory with phenopackets Defaults to "phenopackets".
:param target: Disease id (e.g., OMIM:600123) to select only phenopackets with this disease. Defaults to None.
:type target: str
Expand All @@ -271,18 +272,7 @@ def create_hpoa_from_phenopackets(self,
ppkt_list = TemplateImporter.filter_diseases(target, ppkt_list)
TemplateImporter.check_disease_entries(ppkt_list)
builder = HpoaTableBuilder(phenopacket_list=ppkt_list, created_by=self._created_by)
if moi == "Autosomal dominant":
builder.autosomal_dominant(pmid)
elif moi == "Autosomal recessive":
builder.autosomal_recessive(pmid)
elif moi == "X-linked inheritance":
builder.x_linked(pmid)
elif moi == "X-linked recessive inheritance":
builder.x_linked_recessive(pmid)
elif moi == "X-linked dominant inheritance":
builder.x_linked_dominant(pmid)
else:
raise ValueError(f"Did not recognize mode of inheritance {moi}")
builder.add_moi(mode_of_inheritance=mode_of_inheritance, pmid=pmid)
hpoa_creator = builder.build()
hpoa_creator.write_data_frame()
return hpoa_creator.get_dataframe()
31 changes: 31 additions & 0 deletions src/pyphetools/creation/mode_of_inheritance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from enum import Enum
from .hp_term import HpTerm

class Moi(Enum):
AD="Autosomal dominant inheritance"
AR="Autosomal recessive inheritance"
XLI="X-linked inheritance"
XLR="X-linked recessive inheritance"
XLD="X-linked dominant inheritance"
MITO="Mitochondrial inheritance"
YLI="Y-linked inheritance"


def to_HPO(self):
if self == Moi.AD:
return HpTerm(hpo_id="HP:0000006", label="Autosomal dominant inheritance")
elif self == Moi.AR:
return HpTerm(hpo_id="HP:0000007", label="Autosomal recessive inheritance")
elif self == Moi.XLI:
return HpTerm(hpo_id="HP:0001417", label="X-linked inheritance")
elif self == Moi.XLR:
return HpTerm(hpo_id="HP:0001419", label="X-linked recessive inheritance")
elif self == Moi.XLD:
return HpTerm(hpo_id="HP:0001423", label="X-linked dominant inheritance")
elif self == Moi.MITO:
return HpTerm(hpo_id="HP:0001427", label="Mitochondrial inheritance")
elif self == Moi.YLI:
return HpTerm(hpo_id="HP:0001450", label="Y-linked inheritance")

else:
raise ValueError(f"Unrecognized Moi enum (should never happen)")
8 changes: 8 additions & 0 deletions src/pyphetools/visualization/hpoa_table_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from ..creation.hp_term import HpTerm
from ..creation.individual import Individual
from ..creation.metadata import MetaData
from ..creation.mode_of_inheritance import Moi
from .counted_hpo_term import CountedHpoTerm, CohortTermCounter
from .onset_calculator import OnsetCalculator

Expand Down Expand Up @@ -347,6 +348,13 @@ def filter_diseases(disease_id, ppkt_list):
break
print(f"[INFO] Extracted {(len(target_list))} from {(len(ppkt_list))} phenopackets with {disease_id}\n")
return target_list

def add_moi(self, mode_of_inheritance:Moi, pmid:str):
"""
Use this method to add mode of inheritance (MOI) data from a publication with the indicated pmid
"""
self._moi_d[pmid].append(mode_of_inheritance)


def autosomal_recessive(self, pmid):
moi_term = HpTerm(hpo_id="HP:0000007", label="Autosomal recessive inheritance")
Expand Down
102 changes: 45 additions & 57 deletions test/test_age_isoformater.py
Original file line number Diff line number Diff line change
@@ -1,66 +1,54 @@
import unittest
import pytest

from pyphetools.creation import AgeIsoFormater


class TestAgeIsoFormater:

@pytest.mark.parametrize(
'year, month, day, iso',
[
( 2, 3, 5, "P2Y3M5D"),
( 29, 5, 25, "P29Y5M25D"),
( 99,1,24,"P99Y1M24D"),
]
)
def test_ymd(
self,
year: int,
month: int,
day: int,
iso:str,
):
iso_age = AgeIsoFormater.to_string(y=year, m=month, d=day)
assert iso == iso_age

@pytest.mark.parametrize(
'month, iso',
[
( 5, "P5M"),
( 0.5,"P15D"),
(0.8, "P24D"),
(11, "P11M"),
(12, "P1Y"),
(16, "P1Y4M"),
("n.a.", "NOT_PROVIDED"),
(None, "NOT_PROVIDED"),
(float("nan"), "NOT_PROVIDED"),
(0, "P0D")
]
)
def test_numerical_month(
self,
month: float,
iso: str
):
iso_age = AgeIsoFormater.from_numerical_month(month=month)
assert iso == iso_age



class TestAgeIsoFormater(unittest.TestCase):

def test_basic1(self):
iso_age = AgeIsoFormater.to_string(y=2, m=3, d=5)
self.assertEqual("P2Y3M5D", iso_age)

def test_basic2(self):
"""
test that 13 months are normalized to 1 year 1 month
"""
iso_age = AgeIsoFormater.to_string(y=42, m=13, d=5)
self.assertEqual("P43Y1M5D", iso_age)

def test_5m(self):
iso_age = AgeIsoFormater.from_numerical_month(5)
self.assertEqual("P5M", iso_age)

def test_15d(self):
iso_age = AgeIsoFormater.from_numerical_month(0.5)
self.assertEqual("P15D", iso_age)

def test_24d(self):
iso_age = AgeIsoFormater.from_numerical_month(0.8)
self.assertEqual("P24D", iso_age)

def test_12m(self):
iso_age = AgeIsoFormater.from_numerical_month(12)
self.assertEqual("P1Y", iso_age)

def test_16m(self):
iso_age = AgeIsoFormater.from_numerical_month(16)
self.assertEqual("P1Y4M", iso_age)

def test_na(self):
"""
Test we return NOT_PROVIDED (from the Contants class) if we cannot parse the cell contents
"""
iso_age = AgeIsoFormater.from_numerical_month("n.a.")
self.assertEqual("NOT_PROVIDED", iso_age)

def test_none(self):
"""
Test we return NOT_PROVIDED (from the Contants class) if we cannot parse the cell contents
"""
iso_age = AgeIsoFormater.from_numerical_month(None)
self.assertEqual("NOT_PROVIDED", iso_age)

def test_nan(self):
"""
Test we return NOT_PROVIDED (from the Contants class) if we cannot parse the cell contents
"""
iso_age = AgeIsoFormater.from_numerical_month(float("nan"))
self.assertEqual("NOT_PROVIDED", iso_age)

def test_newborn(self):
iso_age = AgeIsoFormater.from_numerical_month(0)
self.assertEqual("P0D", iso_age)



59 changes: 59 additions & 0 deletions test/test_moi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import pytest

from pyphetools.creation import Moi


class TestMoi:

@pytest.mark.parametrize(
'moi, hpterm_id, hpterm_label',
[
(
Moi.AD,
'HP:0000006',
'Autosomal dominant inheritance',
),
(
Moi.AR,
'HP:0000007',
'Autosomal recessive inheritance',
),
(
Moi.XLI,
'HP:0001417',
'X-linked inheritance'
),
(
Moi.XLR,
'HP:0001419',
'X-linked recessive inheritance'
),
(
Moi.XLD,
'HP:0001423',
'X-linked dominant inheritance'
),
(
Moi.MITO,
'HP:0001427',
'Mitochondrial inheritance'
),
(
Moi.YLI,
'HP:0001450',
'Y-linked inheritance'
)
]
)
def test_moi(
self,
moi: Moi,
hpterm_id: str,
hpterm_label: str,
):
hpterm = moi.to_HPO()

assert hpterm_id == hpterm.id
assert hpterm_label == hpterm.label

0 comments on commit 1cc586f

Please sign in to comment.