Skip to content

Commit

Permalink
Merge pull request #30 from monarch-initiative/29-remove-dependency-o…
Browse files Browse the repository at this point in the history
…n-ontobio

29 remove dependency on ontobio
  • Loading branch information
yaseminbridges authored May 22, 2024
2 parents 8a96e98 + 182ca59 commit 3685ed8
Show file tree
Hide file tree
Showing 6 changed files with 286 additions and 970 deletions.
1,202 changes: 272 additions & 930 deletions poetry.lock

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ phenopackets = "^2.0.2"
polars = "^0.19"
oaklib = "^0.5.1"
click = "^8.1.3"
ontobio = "^2.8.5"
pheval = {git = "https://github.com/monarch-initiative/pheval.git"}
pheval = "^0.3.6"

[tool.poetry.dev-dependencies]
pytest = "^7.2.0"
Expand Down
12 changes: 3 additions & 9 deletions src/phenotype2phenopacket/create/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import polars as pl
from oaklib.implementations import ProntoImplementation
from ontobio import Ontology

from phenotype2phenopacket.utils.phenopacket_utils import (
PhenotypeAnnotationToPhenopacketConverter,
Expand All @@ -12,15 +11,13 @@
from phenotype2phenopacket.utils.utils import (
filter_diseases,
load_ontology,
load_ontology_factory,
return_phenotype_annotation_data,
)


def create_synthetic_patient_phenopacket(
human_phenotype_ontology: ProntoImplementation,
omim_disease: pl.DataFrame,
ontology_factory: Ontology,
output_dir: Path,
pt_id: str,
hpoa_version: str,
Expand All @@ -31,14 +28,12 @@ def create_synthetic_patient_phenopacket(
Args:
human_phenotype_ontology: An instance of ProntoImplementation containing the loaded HPO.
omim_disease (pl.DataFrame): DataFrame containing phenotype entries for a specific OMIM disease.
ontology_factory: Created HPO ontology from OntologyFactory
output_dir (Path): The directory path to write the generated phenopacket.
pt_id (str): The patient ID.
hpoa_version (str): The version of the Human Phenotype Ontology Annotation.
"""
synthetic_patient_generator = SyntheticPatientGenerator(
omim_disease, human_phenotype_ontology, ontology_factory
)
synthetic_patient_generator = SyntheticPatientGenerator(omim_disease, human_phenotype_ontology)
patient_terms = synthetic_patient_generator.patient_term_annotation_set()
phenopacket_file = PhenotypeAnnotationToPhenopacketConverter(
human_phenotype_ontology
Expand Down Expand Up @@ -70,15 +65,14 @@ def create_synthetic_patients(
"""
phenotype_annotation_data = return_phenotype_annotation_data(phenotype_annotation)
human_phenotype_ontology = load_ontology()
ontology_factory = load_ontology_factory()
grouped_omim_diseases = filter_diseases(
num_disease, omim_id, omim_id_list, phenotype_annotation_data
)
for omim_disease in grouped_omim_diseases:
create_synthetic_patient_phenopacket(
human_phenotype_ontology,
omim_disease,
ontology_factory,
output_dir,
None,
phenotype_annotation_data.version,
)
12 changes: 4 additions & 8 deletions src/phenotype2phenopacket/utils/phenopacket_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import polars as pl
from google.protobuf.timestamp_pb2 import Timestamp
from oaklib.implementations import ProntoImplementation
from ontobio import Ontology
from phenopackets import (
Age,
Diagnosis,
Expand Down Expand Up @@ -150,21 +149,17 @@ def write_phenopacket(phenopacket: Phenopacket, output_file: Path) -> None:
class SyntheticPatientGenerator:
"""Class for generating synthetic patients."""

def __init__(
self, disease_df: pl.DataFrame, ontology: ProntoImplementation, ontology_factory: Ontology
):
def __init__(self, disease_df: pl.DataFrame, ontology: ProntoImplementation):
"""
Initialise the SyntheticPatientGenerator class
Args:
disease_df (pl.DataFrame): The dataframe containing the annotation data for a specific disease.
ontology (ProntoImplementation): An instance of ProntoImplementation containing the loaded HPO.
ontology_factory (Ontology): Created ontology from OntologyFactory
"""
self.disease_df = disease_df
self.ontology = ontology
self.ontology_factory = ontology_factory
self.lower_age = 0
self.upper_age = 0
self.filtered_df = []
Expand Down Expand Up @@ -452,9 +447,9 @@ def get_children_of_term(self, phenotype_entry: dict, steps: int) -> dict:
"""
term_id = phenotype_entry["hpo_id"]
for _i in range(steps):
descendants = self.ontology_factory.children(term_id)
descendants = list(self.ontology.incoming_relationships(term_id))
if descendants:
term_id = self.secret_rand.choice(descendants)
term_id = self.secret_rand.choice(descendants)[1]
else:
break
phenotype_entry["hpo_id"] = term_id
Expand Down Expand Up @@ -602,6 +597,7 @@ def create_individual(
Args:
onset_range (OnsetTerm, optional): An OnsetTerm object representing the age range of onset.
Defaults to None.
pt_id (str, optional): An identifier for the patient.
Returns:
Individual: An instance of the Individual class.
Expand Down
22 changes: 4 additions & 18 deletions src/phenotype2phenopacket/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

import pandas as pd
import polars as pl
from oaklib import OntologyResource
from oaklib.implementations import ProntoImplementation
from ontobio import Ontology, OntologyFactory
from oaklib import get_adapter


def is_float(element: any) -> bool:
Expand Down Expand Up @@ -65,21 +63,9 @@ def load_ontology():
Load the Human Phenotype Ontology (HPO).
Returns:
ProntoImplementation: An instance of ProntoImplementation containing the loaded HPO.
An instantiated interface.
"""
resource = OntologyResource(slug="hp.obo", local=False)
return ProntoImplementation(resource)


def load_ontology_factory() -> Ontology:
"""
Load human phenotype ontology factory.
Returns:
Ontology: An instance of the hp Ontology class.
"""
ontology_factory = OntologyFactory()
return ontology_factory.create("hp")
return get_adapter("sqlite:obo:hp")


def read_hgnc_data(hgnc_data_file: Path) -> pd.DataFrame:
Expand Down Expand Up @@ -114,7 +100,7 @@ def read_phenotype_annotation_file(phenotype_annotation_file_path: Path) -> pl.D

def filter_phenotype_annotation(phenotype_annotation_file_path: Path) -> pl.DataFrame:
"""
Filter the phenotype annotation, retaining data for OMIM diseases describing phenotypye information.
Filter the phenotype annotation, retaining data for OMIM diseases describing phenotype information.
Args:
phenotype_annotation_file_path (Path): The path to the phenotype annotation file.
Expand Down
5 changes: 2 additions & 3 deletions tests/test_phenopacket_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
SyntheticPatientGenerator,
create_phenopacket_file_name_from_disease,
)
from phenotype2phenopacket.utils.utils import load_ontology, load_ontology_factory
from phenotype2phenopacket.utils.utils import load_ontology

disease_df = pl.from_dicts(
[
Expand Down Expand Up @@ -298,7 +298,6 @@ def setUpClass(cls) -> None:
cls.synthetic_patient_generator = SyntheticPatientGenerator(
disease_df=disease_df,
ontology=load_ontology(),
ontology_factory=load_ontology_factory(),
)

def tearDown(self) -> None:
Expand Down Expand Up @@ -746,7 +745,7 @@ def mock_choice(lst):
"database_id": "OMIM:612567",
"disease_name": "Inflammatory bowel disease 25, early onset, autosomal recessive",
"qualifier": None,
"hpo_id": "HP:0009789",
"hpo_id": "HP:0001047",
"reference": "PMID:21519361",
"evidence": "PCS",
"onset": None,
Expand Down

0 comments on commit 3685ed8

Please sign in to comment.