Last active
August 29, 2015 14:19
-
-
Save dosumis/54ceeae6bf0d5676459f to your computer and use it in GitHub Desktop.
Simple script taking spec of query as JSON and returning standard JSON report of each term found. Uses Brain
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from uk.ac.ebi.brain.core import Brain | |
import json | |
from owltools.graph import OWLGraphWrapper | |
import re | |
import sys | |
def write_json(json_var, path): | |
"""Writes json_var to file (path) with a nicely serialised layout.""" | |
json_file = open(path, "w") | |
json_file.write(json.dumps(json_var ,sort_keys=True, indent=4, separators=(',', ': '))) | |
json_file.close() | |
query = json.load(sys.argv[1]) | |
# example query = { "query_type": 'descendant_class', "query" : "FBbt_0000001"} | |
def get_syns(ont, ID): | |
ogw = OWLGraphWrapper(fbbto) | |
obo_id = re.sub('_', ':', ID) | |
clazo = ogw.getOWLClassByIdentifier(obo_id) # Needs to swith to get entity from ontology object in order to support individuals | |
return ogw.getOBOSynonymStrings(clazo, []) | |
fbbt = Brain() | |
fbbt.learn("file:///repos/vfb-annotation-tools-gh/OWL/FBbt_query_classes_pre_reasoned.owl") | |
out = [] | |
fbbto = fbbt.getOntology() | |
query_results = [] | |
if query["query_type"] == 'descendant_classes': | |
query_results = fbbt.getSubClasses(query[query], 0) | |
elif query["query_type"] == 'individuals; | |
query_results = fbbt.getInstances(query[query], 0) | |
for ID in query_results: | |
ni= {} | |
ni['name'] = fbbt.getLabel(ID) | |
ni['ID'] = ID | |
synonyms = get_syns(fbbto, ID) | |
ni['synonyms'] = list(synonyms) | |
out.append(ni) | |
write_json(out, sys.argv[2]) | |
fbbt.sleep() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment