Python rdflib.namespace.RDFS.subClassOf() Examples
The following are 9
code examples of rdflib.namespace.RDFS.subClassOf().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
rdflib.namespace.RDFS
, or try the search function
.
Example #1
Source File: treeify-hpo.py From dipper with BSD 3-Clause "New" or "Revised" License | 6 votes |
def hpo_to_tree(cls, hpo_terms, hpo_graph, tree, path): tree_path = copy.copy(path) tree_path.append(cls) curie_util = CurieUtil(curie_map.get()) if cls not in hpo_terms: hpo_terms[cls] = { 'label': hpo_graph.label(URIRef(curie_util.get_uri(cls))) } parents = hpo_graph.objects(URIRef(curie_util.get_uri(cls)), RDFS.subClassOf) hpo_terms[cls]['parents'] = len(list(parents)) lay_person = get_lay_person(cls, hpo_graph) hpo_terms[cls]["lay_person"] = lay_person # Traverse the tree to get to the input class position = tree[tree_path[0]] for term in tree_path[1:]: position = position[term] for sub_class in hpo_graph.subjects(RDFS.subClassOf, URIRef(curie_util.get_uri(tree_path[-1]))): curie = curie_util.get_curie(sub_class).replace("OBO:HP_", "HP:") position[curie] = {} hpo_to_tree(curie, hpo_terms, hpo_graph, tree, tree_path)
Example #2
Source File: rdflib_bridge.py From ontobio with BSD 3-Clause "New" or "Revised" License | 5 votes |
def rdfgraph_to_ontol(rg): """ Return an Ontology object from an rdflib graph object Status: Incomplete """ digraph = networkx.MultiDiGraph() from rdflib.namespace import RDF label_map = {} for c in rg.subjects(RDF.type, OWL.Class): cid = contract_uri_wrap(c) logger.info("C={}".format(cid)) for lit in rg.objects(c, RDFS.label): label_map[cid] = lit.value digraph.add_node(cid, label=lit.value) for s in rg.objects(c, RDFS.subClassOf): # todo - blank nodes sid = contract_uri_wrap(s) digraph.add_edge(sid, cid, pred='subClassOf') logger.info("G={}".format(digraph)) payload = { 'graph': digraph, #'xref_graph': xref_graph, #'graphdoc': obographdoc, #'logical_definitions': logical_definitions } ont = Ontology(handle='wd', payload=payload) return ont
Example #3
Source File: models.py From open-context-py with GNU General Public License v3.0 | 5 votes |
def update_load_vocabulary(self, vocab_file_uri, vocab_uri=False): """ Loads, parses, and stores a vocabulary or ontology Saves labels for entities referenced in the vocabulary, also saves hierarchical relationships for these entities in the database for use in formulating Solr index """ output = {} if vocab_uri is not False: self.vocabulary_uri = vocab_uri self.load_parse_vocabulary(vocab_file_uri) output['labels'] = self.save_entity_labels() output['subClasses'] = self.save_hierarchy('rdfs:subClassOf') output['subProperties'] = self.save_hierarchy('rdfs:subPropertyOf') output['hasIcons'] = self.save_icons() output['sorting'] = self.save_sort() return output
Example #4
Source File: models.py From open-context-py with GNU General Public License v3.0 | 5 votes |
def save_icons(self, predicate_uri='oc-gen:hasIcon'): """ Saves icons in the general Open Context namespace """ data = False if(self.graph is not False and self.vocabulary_uri is not False): data = [] if(self.replace_old): # delete old relations from this vocabulary using this predicate LinkAnnotation.objects.filter(source_id=self.vocabulary_uri, predicate_uri=predicate_uri).delete() if(predicate_uri == 'oc-gen:hasIcon'): # for subClassOf predicates full_pred_uri = URImanagement.convert_prefix_to_full_uri(predicate_uri) icon_pred = URIRef(full_pred_uri) for s, p, o in self.graph.triples((None, icon_pred, None)): subject_uri = s.__str__() # get the URI of the subject as a string object_uri = o.__str__() # get the URI of the object as a string act_t = {'s': subject_uri, 'o': object_uri} if(subject_uri != object_uri): data.append(act_t) if(len(data) > 0): for act_t in data: newr = LinkAnnotation() # make the subject a prefixed URI if common newr.subject = URImanagement.prefix_common_uri(act_t['s']) newr.subject_type = 'uri' newr.project_uuid = '0' newr.source_id = self.vocabulary_uri newr.predicate_uri = predicate_uri newr.object_uri = act_t['o'] newr.save() return data
Example #5
Source File: rdf_utils.py From kgx with BSD 3-Clause "New" or "Revised" License | 5 votes |
def infer_category(iri: URIRef, rdfgraph:rdflib.Graph) -> List[str]: """ Infer category for a given iri by traversing rdfgraph. Parameters ---------- iri: rdflib.term.URIRef IRI rdfgraph: rdflib.Graph A graph to traverse Returns ------- List[str] A list of category corresponding to the given IRI """ category = None subj = None closure = list(rdfgraph.transitive_objects(iri, URIRef(RDFS.subClassOf))) category = [top_level_terms[x] for x in closure if x in top_level_terms.keys()] if category: logging.debug("Inferred category as {} based on transitive closure over 'subClassOf' relation".format(category)) else: subj = closure[-1] if subj == iri: return category subject_curie = contract(subj) if '_' in subject_curie: fixed_curie = subject_curie.split(':', 1)[1].split('_', 1)[1] logging.warning("Malformed CURIE {} will be fixed to {}".format(subject_curie, fixed_curie)) subject_curie = fixed_curie cls = get_curie_lookup_service() category = get_category_via_superclass(cls.ontology_graph, subject_curie) return category
Example #6
Source File: test_infer.py From Skosify with MIT License | 5 votes |
def test_rdfs_classes(): rdf = Graph() a, b, c, x = BNode(), BNode(), BNode(), BNode() rdf.add((a, RDFS.subClassOf, b)) rdf.add((b, RDFS.subClassOf, c)) rdf.add((x, RDF.type, a)) skosify.infer.rdfs_classes(rdf) assert (x, RDF.type, b) in rdf assert (x, RDF.type, c) in rdf
Example #7
Source File: models.py From open-context-py with GNU General Public License v3.0 | 4 votes |
def save_hierarchy(self, predicate_uri='rdfs:subClassOf'): """ Saves hierarchic relations from a vocabulary, defaulting to subClassOf predicates """ data = False if(self.graph is not False and self.vocabulary_uri is not False): data = [] if(self.replace_old): # delete old relations from this vocabulary using this predicate LinkAnnotation.objects.filter(source_id=self.vocabulary_uri, predicate_uri=predicate_uri).delete() if(predicate_uri == 'rdfs:subClassOf'): # for subClassOf predicates for s, p, o in self.graph.triples((None, RDFS.subClassOf, None)): subject_uri = s.__str__() # get the URI of the subject as a string object_uri = o.__str__() # get the URI of the object as a string act_t = {'s': subject_uri, 'o': object_uri} if(subject_uri != object_uri): data.append(act_t) elif(predicate_uri == 'rdfs:subPropertyOf'): # for subPropertyOf predicates for s, p, o in self.graph.triples((None, RDFS.subPropertyOf, None)): subject_uri = s.__str__() # get the URI of the subject as a string object_uri = o.__str__() # get the URI of the object as a string act_t = {'s': subject_uri, 'o': object_uri} if(subject_uri != object_uri): data.append(act_t) if(len(data) > 0): for act_t in data: newr = LinkAnnotation() # make the subject a prefixed URI if common newr.subject = URImanagement.prefix_common_uri(act_t['s']) newr.subject_type = 'uri' newr.project_uuid = '0' newr.source_id = self.vocabulary_uri newr.predicate_uri = predicate_uri newr.object_uri = act_t['o'] newr.save() return data
Example #8
Source File: rdf_transformer.py From kgx with BSD 3-Clause "New" or "Revised" License | 4 votes |
def load_networkx_graph(self, rdfgraph: rdflib.Graph = None, predicates: Set[URIRef] = None, **kwargs) -> None: """ Walk through the rdflib.Graph and load all required triples into networkx.MultiDiGraph By default this method loads the following predicates, - ``RDFS.subClassOf`` - ``OWL.sameAs`` - ``OWL.equivalentClass`` - ``is_about`` (IAO:0000136) - ``has_subsequence`` (RO:0002524) - ``is_subsequence_of`` (RO:0002525) This behavior can be overridden by providing a list of rdflib.URIRef that ought to be loaded via the ``predicates`` parameter. Parameters ---------- rdfgraph: rdflib.Graph Graph containing nodes and edges predicates: list A list of rdflib.URIRef representing predicates to be loaded kwargs: dict Any additional arguments """ if predicates is None: predicates = set() predicates = predicates.union(self.OWL_PREDICATES, [self.is_about, self.is_subsequence_of, self.has_subsequence]) triples = rdfgraph.triples((None, None, None)) logging.info("Loading from rdflib.Graph to networkx.MultiDiGraph") with click.progressbar(list(triples), label='Progress') as bar: for s, p, o in bar: if (p == self.is_about) and (p in predicates): logging.debug("Loading is_about predicate") # if predicate is 'is_about' then treat object as publication self.add_node_attribute(o, key=s, value='publications') elif (p == self.is_subsequence_of) and (p in predicates): logging.debug("Loading is_subsequence_of predicate") # if predicate is 'is_subsequence_of' self.add_edge(s, o, self.is_subsequence_of) elif (p == self.has_subsequence) and (p in predicates): logging.debug("Loading has_subsequence predicate") # if predicate is 'has_subsequence', interpret the inverse relation 'is_subsequence_of' self.add_edge(o, s, self.is_subsequence_of) elif any(p.lower() == x.lower() for x in predicates): logging.debug("Loading {} predicate".format(p)) self.add_edge(s, o, p)
Example #9
Source File: rdf_transformer.py From kgx with BSD 3-Clause "New" or "Revised" License | 4 votes |
def load_networkx_graph(self, rdfgraph: rdflib.Graph = None, predicates: Set[URIRef] = None, **kwargs) -> None: """ Walk through the rdflib.Graph and load all triples into networkx.MultiDiGraph Parameters ---------- rdfgraph: rdflib.Graph Graph containing nodes and edges predicates: list A list of rdflib.URIRef representing predicates to be loaded kwargs: dict Any additional arguments """ triples = rdfgraph.triples((None, RDFS.subClassOf, None)) logging.info("Loading RDFS:subClassOf triples from rdflib.Graph to networkx.MultiDiGraph") with click.progressbar(list(triples), label='Progress') as bar: for s, p, o in bar: # ignoring blank nodes if isinstance(s, rdflib.term.BNode): continue pred = None parent = None if isinstance(o, rdflib.term.BNode): # C SubClassOf R some D for x in rdfgraph.objects(o, OWL.onProperty): pred = x for x in rdfgraph.objects(o, OWL.someValuesFrom): parent = x if pred is None or parent is None: logging.warning("Do not know how to handle BNode: {}".format(o)) continue else: # C SubClassOf D (C and D are named classes) pred = p parent = o self.add_edge(s, parent, pred) triples = rdfgraph.triples((None, OWL.equivalentClass, None)) logging.info("Loading OWL:equivalentClass triples from rdflib.Graph to networkx.MultiDiGraph") with click.progressbar(list(triples), label='Progress') as bar: for s, p, o in bar: self.add_edge(s, o, p) relations = rdfgraph.subjects(RDF.type, OWL.ObjectProperty) logging.debug("Loading relations") with click.progressbar(relations, label='Progress') as bar: for relation in bar: for _, p, o in rdfgraph.triples((relation, None, None)): if o.startswith('http://purl.obolibrary.org/obo/RO_'): self.add_edge(relation, o, p) else: self.add_node_attribute(relation, key=p, value=o) self.add_node_attribute(relation, key='category', value='relation') triples = rdfgraph.triples((None, None, None)) with click.progressbar(list(triples), label='Progress') as bar: for s, p, o in bar: if p in property_mapping.keys(): self.add_node_attribute(s, key=p, value=o)