def _populate_published(curation_export, graphs): # datasets = [list(g[:rdf.type:sparc.Dataset]) for g in graphs] published_graphs = [ g for g, doi in [(g, list(g[ds:TEMP.hasDoi])) for g in graphs for ds in g[:rdf.type:sparc.Dataset]] if doi ] merged = OntGraph() for g in published_graphs: merged.namespace_manager.populate_from({ k: v for k, v in dict(g.namespace_manager).items() if k not in ('contributor', 'sample', 'subject') }) merged.populate_from_triples( g.data) # g.data excludes the owl:Ontology section # TODO switch the rdf:type of metadata section on combination to preserve export related metadata mg = curation_export.metadata().graph mg.namespace_manager.populate(merged) new_bi = rdflib.URIRef( mg.boundIdentifier.replace('ontologies/', 'ontologies/published/')) new_vi = rdflib.URIRef( mg.versionIdentifier.replace('ontologies/', 'ontologies/published/')) replace_pairs = ( (rdflib.Literal("SPARC Consortium curation export published graph"), rdflib.Literal("SPARC Consortium curation export graph")), (new_bi, mg.boundIdentifier), (new_vi, mg.versionIdentifier)) new_meta = mg.replaceIdentifiers(replace_pairs) new_meta.populate(merged) return merged
def main(): olr = auth.get_path('ontology-local-repo') ori = OntResIri('http://purl.obolibrary.org/obo/doid.owl') orp = OntResPath(olr / 'ttl/external/doid.owl') ort = ori g = ori.graph query = """ SELECT DISTINCT ?s ?o ?l WHERE { ?s a owl:Class . ?s rdfs:subClassOf* <http://purl.obolibrary.org/obo/DOID_4> . ?s rdfs:subClassOf ?o . ?s rdfs:label ?l . }""" res = list(g.query(query)) filt = [r for r in res if not isinstance(r[1], rdflib.BNode)] spath = 'ttl/generated/doidslim.ttl' go = OntGraph(path=olr / spath) # TODO prov record like the one we have for chebi go.bind('DOID', 'http://purl.obolibrary.org/obo/DOID_') s = rdflib.URIRef('http://ontology.neuinfo.org/NIF/' + spath) go.populate_from_triples( ((s, p, o) for p, o in ((rdf.type, owl.Ontology), (rdfs.label, rdflib.Literal("NIF DOID slim")),))) ds = rdflib.URIRef('http://purl.obolibrary.org/obo/DOID_4') go.add((ds, rdf.type, owl.Class)) go.add((ds, rdfs.label, rdflib.Literal('disease'))) go.populate_from_triples( (t for s, o, l in filt for t in ((s, rdf.type, owl.Class), (s, rdfs.subClassOf, o), (s, rdfs.label, l)))) go.write()
def make_graphs(g, pids, published): sgs = [] for i in pids: ng = OntGraph() ng.namespace_manager.populate_from(g) ng.namespace_manager.bind( 'spjl', 'https://uilx.org/tgbugs/u/sparcur-protcur-json-ld/') ng.populate_from_triples(tobn(g.subjectGraphClosure(i), published)) sgs.append(ng) return sgs
def main(): url = ( 'http://data.bioontology.org/ontologies/' f'PCL/submissions/7/download?apikey={auth.user_config.secrets("bioportal")}' ) g = OntGraph().parse(url, format='application/rdf+xml') g = fixns(g) og = OntGraph() g.namespace_manager.populate(og) og.populate_from_triples(to_ebm(g)) og.write('/tmp/pCL.ttl')
def substitute_graph(graph, base_graph=None): # command triples # random literals # random urirefs g = OntGraph(namespace_manager=base_graph) trips = [[mkrand(e) for e in t] for t in graph if '__COMMAND' not in t[0]] g.populate_from_triples(trips) if base_graph is not None: cmd_trips = [[mkrand(e) for e in t] for t_command in graph if '__COMMAND' in t_command[0] for t in process_command(t_command, base_graph)] g.populate_from_triples(cmd_trips) return g
def fixns(g): ns = rdflib.Namespace(dict(g.namespace_manager)['']) nsb = ns + 'pCL_' nsu = ns + 'UBERON_' nsc = ns + 'CL_' ng = OntGraph() ng.namespace_manager.populate_from(g) ng.bind('ilxtr', ilxtr) ng.bind('CL', CL) ng.bind('pCL', pCL) ng.bind('UBERON', UBERON) ng.bind('NCBITaxon', NCBITaxon) ng.bind('HGNC', HGNC) ng.bind('LOC', LOC) ng.populate_from_triples( (rdflib.URIRef(str(e.replace(nsb, pCL))) if e.startswith(nsb) else (rdflib.URIRef(str(e.replace(nsu, UBERON))) if e.startswith(nsu) else (rdflib.URIRef(str(e.replace(nsc, CL))) if e.startswith(nsc) else e)) for e in t) for t in g) return ng