<jupyter_start><jupyter_text>Querying an OWL ontology using SPARQL commands We will use owlready2 to access and query our ontology. Specifically, we will query for treatments matching variants in cBioPortal.We implement this functionality into functions in `query_ontology.py`, taking into account modularity and task decomposition. This notebook format is included just for demonstration purposes.  Import Modules<jupyter_code>import os
import re

import numpy as np
import pandas as pd
from owlready2 import *

from sample_patients import sample_patient_records<jupyter_output><empty_output><jupyter_text>Load Ontology**Note:** Update path to local ontology matching your local file structure.<jupyter_code>local_ontology = "github/bmi-210-final-project/ontology/oncokb.owl"
onto = get_ontology(local_ontology).load()<jupyter_output><empty_output><jupyter_text>Load cBioPortal Mutation Data**Note:** Update path to cBioPortal mutation data to match your local file structure.<jupyter_code># Sample mutation data for a subset of patients.
csv_path = "mutations.csv"
out_path = "mutations_1000.csv"
n_patients = 1000
sample_patient_records(csv_path, out_path)
# Load subset of patient data
cBioPortal_mutations = pd.read_csv(out_path)
cBioPortal_mutations = cBioPortal_mutations.loc[:, ["patientId", "proteinChange", "entrezGeneId"]]
cBioPortal_mutations.head()<jupyter_output><empty_output><jupyter_text>Load Gene List<jupyter_code># Load genes matching Entrez Gene ID
gene_list = pd.read_csv("CancerGeneList.tsv", sep="\t", usecols=[0, 1])
cBioPortal_mutations = cBioPortal_mutations.merge(gene_list, left_on="entrezGeneId", right_on="Entrez_Id", how="left").drop("Entrez_Id", axis=1)
cBioPortal_mutations.head()<jupyter_output><empty_output><jupyter_text>Generate SPARQL Query<jupyter_code>example_gene = "KRAS"
example_variant = "G12C"
evidence_level = 4

example_regimen = list(
    default_world.sparql(
        f"""
        SELECT distinct ?regimen
        {{
            ?biomarker rdfs:subClassOf oncokb:Biomarker.
            ?biomarker rdfs:subClassOf ?r1.
            ?r1 owl:onProperty oncokb:hasGene.
            ?r1 owl:someValuesFrom oncokb:{re.escape(example_gene)}.

            ?biomarker rdfs:subClassOf ?R2.
            ?R2 owl:onProperty oncokb:hasVariant.
            ?R2 owl:someValuesFrom oncokb:{re.escape(example_variant)}.
            
            ?regimen rdfs:subClassOf oncokb:TherapyRegimen.
            ?regimen rdfs:subClassOf ?restriction2.
            ?restriction2 owl:onProperty oncokb:hasEvidenceLevel{evidence_level}.
            ?restriction2 owl:someValuesFrom ?biomarker.
        }}
        """
    )
)

example_regimen = [regimen[0] for regimen in example_regimen]
print(example_regimen)<jupyter_output>[oncokb.Trametinib, oncokb.Binimetinib, oncokb.Cobimetinib]<jupyter_text>Custom Command Line ToolQuery for and save the therapy regimen associated with each mutated gene in a specific patient. We will use "Patient1035" – the 1000th patient in our cBioPortal mutation output file – as an example.**Usage of command line tool:** `python query_therapy_regimen.py     `<jupyter_code>!python query_therapy_regimen.py "github/bmi-210-final-project/ontology/oncokb.owl" "mutations.csv" "CancerGeneList.tsv" "Patient1035" "example_regimen_list.csv"<jupyter_output>Patient 'Patient1035' found!
{'fraction_non_alphanumeric': 0.08976283398378865, 'fraction_numerical': 0.01441008706094266, 'mean_word_length': 5.204841713221602, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '105178', 'license': 'MIT', 'n_tokens_mistral': 1175, 'n_tokens_neox': 1070, 'n_words': 255, 'path': 'docs/build/html/query_therapy_regimen.ipynb', 'repo_name': 'jmichuda/bmi-214-final-project', 'size': '3331'}