def get_digraph(ont, relations=None, writecache=False): """ Creates a basic graph object corresponding to a remote ontology """ digraph = networkx.MultiDiGraph() logger.info("Getting edges (may be cached)") for (s, p, o) in get_edges(ont): p = map_legacy_pred(p) if relations is None or p in relations: digraph.add_edge(o, s, pred=p) logger.info("Getting labels (may be cached)") for (n, label) in fetchall_labels(ont): digraph.add_node(n, **{'label': label}) return digraph
def add_obograph_digraph( self, og, node_type=None, predicates=None, xref_graph=None, logical_definitions=None, property_chain_axioms=None, parse_meta=True, reverse_edges=True, **args): """ Converts a single obograph to Digraph edges and adds to an existing networkx DiGraph """ digraph = self.digraph logging.info("NODES: {}".format(len(og['nodes']))) # if client passes an xref_graph we must parse metadata if xref_graph is not None: parse_meta = True for node in og['nodes']: is_obsolete = 'is_obsolete' in node and node['is_obsolete'] == 'true' if is_obsolete: continue if node_type is not None and ('type' not in node or node['type'] != node_type): continue id = self.contract_uri(node['id']) digraph.add_node(id, **node) if 'lbl' in node: digraph.node[id]['label'] = node['lbl'] if parse_meta and 'meta' in node: if node['meta'] is None: node['meta'] = {} meta = self.transform_meta(node['meta']) if xref_graph is not None and 'xrefs' in meta: for x in meta['xrefs']: xref_graph.add_edge(self.contract_uri(x['val']), id, source=id) logging.info("EDGES: {}".format(len(og['edges']))) for edge in og['edges']: sub = self.contract_uri(edge['sub']) obj = self.contract_uri(edge['obj']) pred = self.contract_uri(edge['pred']) pred = map_legacy_pred(pred) if pred == 'is_a': pred = 'subClassOf' if predicates is None or pred in predicates: meta = edge['meta'] if 'meta' in edge else {} if reverse_edges: digraph.add_edge(obj, sub, pred=pred, **meta) else: digraph.add_edge(sub, obj, pred=pred, **meta) if 'equivalentNodesSets' in og: nslist = og['equivalentNodesSets'] logging.info("CLIQUES: {}".format(len(nslist))) for ns in nslist: equivNodeIds = ns['nodeIds'] for i in ns['nodeIds']: ix = self.contract_uri(i) for j in ns['nodeIds']: if i != j: jx = self.contract_uri(j) digraph.add_edge(ix, jx, pred='equivalentTo') if logical_definitions is not None and 'logicalDefinitionAxioms' in og: for a in og['logicalDefinitionAxioms']: ld = LogicalDefinition(self.contract_uri(a['definedClassId']), [self.contract_uri(x) for x in a['genusIds']], [(self.contract_uri(x['propertyId']), self.contract_uri(x['fillerId'])) for x in a['restrictions'] if x is not None]) logical_definitions.append(ld) if property_chain_axioms is not None and 'propertyChainAxioms' in og: for a in og['propertyChainAxioms']: pca = PropertyChainAxiom(predicate_id=self.contract_uri(a['predicateId']), chain_predicate_ids=[self.contract_uri(x) for x in a['chainPredicateIds']]) property_chain_axioms.append(pca)