<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Perform enrichment test using phenotype ontology
Given a file of genes
Step1:  Given that the initial gene set is for retinitis pigmentosa genes, it's not surprising that enriched phenotype
terms are related to retinal degeneration
Viewing Results
We can use different visualization options to see the enriched terms. First we will show a simple tree view
Step2:  visualization
Now we will show enriched terms in a graph using graphviz
<ASSISTANT_TASK:>
Python Code:
## Parse ids from file
file = open("data/rp-genes.tsv", "r")
gene_ids = [row.split("\t")[0] for row in file]
## show first 10 IDs:
gene_ids[:10]
## Create an ontology factory in order to fetch HPO
from ontobio.ontol_factory import OntologyFactory
ofactory = OntologyFactory()
ont = ofactory.create("hp")  ## Load HP. Note the first time this runs Jupyter will show '*' - be patient
## Create an association factory to get gene-phenotype associations
from ontobio.assoc_factory import AssociationSetFactory
afactory = AssociationSetFactory()
## Load Associations from Monarch. Note the first time this runs Jupyter will show '*' - be patient
aset = afactory.create(ontology=ont, subject_category='gene', object_category='phenotype', taxon='NCBITaxon:9606')
## Run enrichment tests using all classes in ontology
enr = aset.enrichment_test(subjects=gene_ids, threshold=0.00005, labels=True)
## Show first 20 results
for r in enr[:20]:
        print("{:8.3g} {} {:40s}".format(r['p'],r['c'],str(r['n'])))
Explanation: Perform enrichment test using phenotype ontology
Given a file of genes:
$ head data/rp-genes.tsv
NCBIGene:6295   SAG
NCBIGene:1258   CNGB1
NCBIGene:3614   IMPDH1
NCBIGene:26121  PRPF31
The example file here is derived from a Monarch query for all retinitis pigmentosa genes.
We want to test each class in HPO to see if it is enriched for genes in this set.
First we need to parse the gene Ids in the file
End of explanation
## Get all enriched class Ids
terms = [r['c'] for r in enr]
## Create a minimal slim of HPO consisting of enriched terms,
## with non-informative intermediate nodes removed
from ontobio.slimmer import get_minimal_subgraph
g = get_minimal_subgraph(ont.get_graph(), terms)
## Render as ascii-tree
from ontobio.graph_io import GraphRenderer
w = GraphRenderer.create('tree')
w.write(g, query_ids=terms)
Explanation: Given that the initial gene set is for retinitis pigmentosa genes, it's not surprising that enriched phenotype
terms are related to retinal degeneration
Viewing Results
We can use different visualization options to see the enriched terms. First we will show a simple tree view
End of explanation
terms = [r['c'] for r in enr[:30]]
g = get_minimal_subgraph(ont.get_graph(), terms)
w = GraphRenderer.create('png')
w.outfile = "output/enr.png"
w.write(g, query_ids=terms)
Explanation: visualization
Now we will show enriched terms in a graph using graphviz
End of explanation
<END_TASK>