def test_remote_sparql(): """ reconstitution test """ factory = OntologyFactory() # default method is sparql ont = factory.create('pato') g = ont.get_graph() info = g.node[PLOIDY] print(str(info)) nodes = g.nodes() print(len(nodes)) assert len(nodes) > 100 nbrs = g.successors(PLOIDY) print("SUCC:" + str(nbrs)) parents = g.predecessors(PLOIDY) print("PRED:" + str(parents)) assert parents == ['PATO:0001396'] ancs = ancestors(g, PLOIDY) print("ANCS:" + str(ancs)) assert 'PATO:0000001' in ancs print(g) Q = ['.*shape.*'] w = GraphRenderer.create('tree') shapes1 = ont.resolve_names(Q, is_regex=True, is_remote=False) print("SHAPE Q:" + str(shapes1)) show_nodes(w, ont, shapes1) assert Y_SHAPED in shapes1 shapes2 = ont.resolve_names(Q, is_regex=True, is_remote=True) print("SHAPE Q:" + str(shapes2)) show_nodes(w, ont, shapes2) assert Y_SHAPED in shapes2
def test_write(): """ write obo from json """ factory = OntologyFactory() print("Creating ont") ont = factory.create('tests/resources/nucleus.json') w = GraphRenderer.create('obo') w.write(ont)
def render(ont, query_ids, args): """ Writes or displays graph """ if args.slim.find('m') > -1: logging.info("SLIMMING") g = get_minimal_subgraph(g, query_ids) w = GraphRenderer.create(args.to) if args.outfile is not None: w.outfile = args.outfile w.write(ont, query_ids=query_ids, container_predicates=args.container_properties)
def test_local_json_parse(): """ Load ontology from JSON """ factory = OntologyFactory() print("Creating ont") #tbox_ontology = factory.create('go') # TODO: create test module for this example tbox_ontology = factory.create('tests/resources/go-truncated-pombase.json') ont = factory.create('tests/resources/gocam-example.ttl', tbox_ontology=tbox_ontology) g = ont.get_graph() nodes = ont.search('%') print("NODES: {}".format(nodes)) w = GraphRenderer.create(None) w.write_subgraph(ont, nodes) i = 'http://model.geneontology.org/0000000300000001/0000000300000007' ni = g[i] print(str(ni)) ['GO:0060070'] == ni['types'] nbrs = ont.neighbors(i) print("NEIGHBORS: {}".format(nbrs)) subont = tbox_ontology.subontology(nodes, minimal=False) w = GraphRenderer.create('obo') print(w.render(subont))
def render(ont, query_ids, args): """ Writes or displays graph """ if args.slim.find('m') > -1: logging.info("SLIMMING") g = get_minimal_subgraph(g, query_ids) w = GraphRenderer.create(args.to) if args.showdefs: w.config.show_text_definition = True if args.render: if 'd' in args.render: logging.info("Showing text defs") w.config.show_text_definition = True if args.outfile is not None: w.outfile = args.outfile w.write(ont, query_ids=query_ids, container_predicates=args.container_properties)
def todo_subontology(): """ subontology """ factory = OntologyFactory() print("Creating ont") ont = factory.create('scigraph:ontology') print("ONT NODES: {}".format(ont.nodes())) # TODO: implement this for SciGraph: subont = ont.subontology(relations=['subClassOf']) PERM = 'GO:1990578' print("NODES: {}".format(subont.nodes())) ancs = subont.ancestors(PERM, reflexive=True) print(str(ancs)) for a in ancs: print(" ANC: {} '{}'".format(a,subont.label(a))) assert len(ancs) > 0 from ontobio.io.ontol_renderers import GraphRenderer w = GraphRenderer.create('tree') w.write_subgraph(ont, ancs)
def test_factory(): """ test ontology factory using wikidata as source and using PTSD. """ f = OntologyFactory() ont = f.create('wdq:Q544006') for n in ont.nodes(): print('{} "{}"'.format(n, ont.label(n))) qids = ont.search('anxiety%') assert len(qids) > 0 print(qids) nodes = ont.traverse_nodes(qids, up=True, down=True) print(nodes) assert len(nodes) > 0 labels = [ont.label(n) for n in nodes] print(labels) # Note: it's possible wd may change rendering this false assert 'Fear of frogs' in labels from ontobio.io.ontol_renderers import GraphRenderer w = GraphRenderer.create('tree') w.write(ont, query_ids=qids)
def extract_ontology(ont, aset, args): ont = aset.subontology(minimal=args.minimal) w = GraphRenderer.create('obo') w.outfile = args.outfile w.write(ont)
import logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) # Make ontology from wikidata onto_factory = OntologyFactory() wd_ontology = onto_factory.create('wdq:Q185034') # Sickle cell anemia qids = wd_ontology.search('Sickle%') # Traverse up and down from query node in our sub-ontology nodes = wd_ontology.traverse_nodes(qids, up=True, down=True) renderer = GraphRenderer.create('obo') renderer.outfile = './output/wd-ontology.obo' # renderer.write(wd_ontology) # >> AttributeError: 'EagerWikidataOntology' object has no attribute 'all_logical_definitions' renderer.write_subgraph(wd_ontology, nodes, query_ids=qids) # Get GO terms outfile = open('./output/go-terms.tsv', 'w') [ptsd] = wd_ontology.search('Sickle Cell Anemia') proteins = wd.canned_query('disease2protein', ptsd) go = onto_factory.create('go') afactory = AssociationSetFactory()
all_terms = [] term_to_ancestors = {} for t in common_terms: subont = ont.subontology(ont.ancestors(t), relations=RELATIONS) term_to_ancestors[t] = subont.nodes( ) # Keep ancestor list in case we want to only include common ancestors for n in subont.nodes(): if n not in all_terms: all_terms.append(n) print("Grabbed all ancestors") if ONLY_SHARED_ANCESTORS: shared_ancestors = [] for t in common_terms: for anc in term_to_ancestors[t]: ## x-y term matrix checking for shared ancestors in multiple ancestor sets for other_t in common_terms: if other_t == t: continue else: if anc in term_to_ancestors[other_t]: shared_ancestors.append(anc) all_terms = set(shared_ancestors + list(common_terms.keys())) print("Filtered for shared ancestors only") print("{} terms included in panther_slim".format(len(all_terms))) hierarchy_ont = ont.subontology(all_terms, relations=RELATIONS) # hierarchy_ont = fill_in_relations(hierarchy_ont, ont) renderer = GraphRenderer.create("obo") renderer.outfile = OUTFILE renderer.write(hierarchy_ont)