def translate_file_to_ontology(handle, **args): if handle.endswith(".json"): g = obograph_util.convert_json_file(handle, **args) return Ontology(handle=handle, payload=g) elif handle.endswith(".ttl"): from ontobio.sparql.rdf2nx import RdfMapper logging.info("RdfMapper: {}".format(args)) m = RdfMapper(**args) return m.convert(handle, 'ttl') else: if not (handle.endswith(".obo") or handle.endswith(".owl")): logging.info( "Attempting to parse non obo or owl file with owltools: " + handle) encoded = hashlib.sha256(handle.encode()).hexdigest() logging.info(" encoded: " + str(encoded)) fn = '/tmp/' + encoded if not os.path.isfile(fn): cmd = ['owltools', handle, '-o', '-f', 'json', fn] cp = subprocess.run(cmd, check=True) logging.info(cp) else: logging.info("using cached file: " + fn) g = obograph_util.convert_json_file(fn, **args) return Ontology(handle=handle, payload=g)
def create_ontology(handle=None, **args): ont = None logging.info("Determining strategy to load '{}' into memory...".format(handle)) if handle.find("+") > -1: handles = handle.split("+") onts = [create_ontology(ont) for ont in handles] ont = onts.pop() ont.merge(onts) return ont # TODO: consider replacing with plugin architecture if handle.find(".") > 0 and os.path.isfile(handle): logging.info("Fetching obograph-json file from filesystem") ont = translate_file_to_ontology(handle, **args) elif handle.startswith("obo:"): logging.info("Fetching from OBO PURL") if handle.find(".") == -1: handle += '.owl' fn = '/tmp/'+handle if not os.path.isfile(fn): url = handle.replace("obo:","http://purl.obolibrary.org/obo/") cmd = ['owltools',url,'-o','-f','json',fn] cp = subprocess.run(cmd, check=True) logging.info(cp) else: logging.info("using cached file: "+fn) g = obograph_util.convert_json_file(fn) ont = Ontology(handle=handle, payload=g) elif handle.startswith("wdq:"): from ontobio.sparql.wikidata_ontology import EagerWikidataOntology logging.info("Fetching from Wikidata") ont = EagerWikidataOntology(handle=handle) elif handle.startswith("scigraph:"): from ontobio.neo.scigraph_ontology import RemoteScigraphOntology logging.info("Fetching from SciGraph") ont = RemoteScigraphOntology(handle=handle) elif handle.startswith("http:"): logging.info("Fetching from Web PURL: "+handle) encoded = hashlib.sha256(handle.encode()).hexdigest() #encoded = binascii.hexlify(bytes(handle, 'utf-8')) #base64.b64encode(bytes(handle, 'utf-8')) logging.info(" encoded: "+str(encoded)) fn = '/tmp/'+encoded if not os.path.isfile(fn): cmd = ['owltools',handle,'-o','-f','json',fn] cp = subprocess.run(cmd, check=True) logging.info(cp) else: logging.info("using cached file: "+fn) g = obograph_util.convert_json_file(fn) ont = Ontology(handle=handle, payload=g) else: logging.info("Fetching from SPARQL") ont = EagerRemoteSparqlOntology(handle=handle) #g = get_digraph(handle, None, True) return ont