def sparql(self, select='*', body=None, inject_prefixes=None, single_column=False): """ Execute a SPARQL query. The query is specified using `select` and `body` parameters. The argument for the Named Graph is injected into the query. The select parameter should be either '*' or a list of vars (not prefixed with '?'). - If '*' is passed, then the result is a list of dicts, { $var: {value: $val } } - If a list of vars is passed, then the result is a list of lists - Unless single_column=True, in which case the results are a simple list of values from the first var The inject_prefixes argument can be used to inject a list of prefixes - these are expanded using the prefixcommons library """ if inject_prefixes is None: inject_prefixes = [] namedGraph = get_named_graph(self.handle) cols = [] select_val = None if select is None or select=='*': if not single_column: cols=None select_val='*' else: if isinstance(cols,list): cols = [select] else: cols = select select_val = ", ".join(['?'+c for c in cols]) prefixes = "" if inject_prefixes is not None: plist = ["prefix {}: <{}> ".format(p,expand_uri(p+":")) for p in inject_prefixes if p != "" and p is not None] prefixes = "\n".join(plist) query = """ {prefixes} SELECT {s} WHERE {{ GRAPH <{g}> {{ {b} }} }} """.format(prefixes=prefixes, s=select_val, b=body, g=namedGraph) bindings = run_sparql(query) if len(bindings) == 0: return [] if cols is None: return bindings else: if single_column: c = list(bindings[0].keys())[0] return [r[c]['value'] for r in bindings] else: return [r[c]['value'] for c in cols for r in bindings]
def subsets(self): """ Find all subsets for an ontology """ # note subsets have an unusual encoding query = """ prefix oboInOwl: <http://www.geneontology.org/formats/oboInOwl#> SELECT DISTINCT ?s WHERE {{ GRAPH <{g}> {{ ?c oboInOwl:inSubset ?s }} }} """.format(g=self.graph_name) bindings = run_sparql(query) return [r['s']['value'] for r in bindings]
def _search(self, searchterm, pred, **args): """ Search for things using labels """ # TODO: DRY with sparql_ontol_utils searchterm = searchterm.replace('%','.*') namedGraph = get_named_graph(self.handle) query = """ prefix oboInOwl: <http://www.geneontology.org/formats/oboInOwl#> SELECT ?c WHERE {{ GRAPH <{g}> {{ ?c {pred} ?l FILTER regex(?l,'{s}','i') }} }} """.format(pred=pred, s=searchterm, g=namedGraph) bindings = run_sparql(query) return [r['c']['value'] for r in bindings]
def extract_subset(self, subset): """ Find all nodes in a subset. We assume the oboInOwl encoding of subsets, and subset IDs are IRIs """ # note subsets have an unusual encoding query = """ prefix oboInOwl: <http://www.geneontology.org/formats/oboInOwl#> SELECT ?c WHERE {{ GRAPH <{g}> {{ ?c oboInOwl:inSubset ?s FILTER regex(?s,'#{s}$','i') }} }} """.format(s=subset, g=self.graph_name) bindings = run_sparql(query) return [r['c']['value'] for r in bindings]