def process_mesh(graph: nx.DiGraph, iri: Optional[str] = None): """Process the MeSH NetworkX graph.""" import owlready assert nx.is_directed_acyclic_graph(graph) o = owlready.Ontology(iri if iri is not None else MESH_OWL_IRI) kwd = {"ontology": o} node_cls = { ROOT_NAME: types.new_class(ROOT_NAME, (owlready.Thing, ), kwds=kwd) } def recur(pnode): for neighbor in graph.successors(pnode): if neighbor not in node_cls: node_cls[neighbor] = types.new_class(neighbor, (node_cls[pnode], ), kwds=kwd) owlready.ANNOTATIONS[node_cls[neighbor]].add_annotation( owlready.rdfs.label, graph.node[pnode]['label']) recur(neighbor) recur(ROOT_NAME) return o
def nx_to_ontology(graph, source_node, output_path, base_iri): """Graph nodes are ID's, and have a 'label' in the node data with the right label :param graph: :param source_node: :param str output_path: :param base_iri: """ ontology = owlready.Ontology(base_iri) parent_lookup = { source_node: types.new_class(source_node, (owlready.Thing, ), kwds={"ontology": ontology}) } def recur(pnode): for neighbor in graph.neighbors(pnode): data = graph.node[neighbor] neighbor_class = types.new_class(neighbor, (parent_lookup[pnode], ), kwds={"ontology": ontology}) owlready.ANNOTATIONS[neighbor_class].add_annotation( owlready.rdfs.label, data['label']) recur(neighbor) recur(source_node) ontology.save(filename=output_path)
import owlready as owr import os owr.onto_path.append(os.getcwd()) onto = owr.Ontology("http://pyonto.com/ontologies/pyonto.owl") class Drug(owr.Thing): ontology = onto class DrugAssociation(Drug): pass class has_for_cost(owr.FunctionalProperty): ontology = onto domain = [Drug] range = [float] onto.save()
Translation.find_binary_predicates(a[0], binary_predicates) print('-----------') #pp.pprint(s) print('') #print(is_all_unary(translated[1])) #pp.pprint(translated) print(unary_predicates) print(binary_predicates) for u in unary_predicates: unary.add(u[0]) for b in binary_predicates: binary.add(b[0]) print('=======') onto = owlready.Ontology("http://1337.io/onto.owl") classes = {u: owl_class(u, onto) for u in unary} properties = {owl_property(b, onto) for b in binary} print(onto.classes) print(onto.properties) print('=======') print(owlready.to_owl(onto))