示例#1
0
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:
            if handle == 'chebi' or handle == 'ncbitaxon' or handle == 'pr':
                handle += '.obo'
                logging.info("using obo for large ontology: {}".format(handle))
            else:
                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("skos:"):
        fn = handle.replace('skos:', '')
        from ontobio.sparql.skos import Skos
        logging.info("Fetching from Skos file")
        skos = Skos()
        ont = skos.process_file(fn)
    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