Ejemplo n.º 1
0
    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]
Ejemplo n.º 2
0
 def __init__(self, handle=None):
     """
     initializes based on an ontology name
     """
     self.id = get_named_graph(handle)
     self.handle = handle
     logging.info("Creating eager-remote-sparql from "+str(handle))
     g = get_digraph(handle, None, True)
     logging.info("Graph:"+str(g))
     if len(g.nodes()) == 0 and len(g.edges()) == 0:
         logging.error("Empty graph for '{}' - did you use the correct id?".
                       format(handle))
     self.graph = g
     self.graph_name = get_named_graph(handle)
     self.xref_graph = get_xref_graph(handle)
     self.all_logical_definitions = []
     self.all_synonyms_cache = None
     self.all_text_definitions_cache = None
     self.all_obsoletes_cache = None
     logging.info("Graph: {} LDs: {}".format(self.graph, self.all_logical_definitions))
Ejemplo n.º 3
0
 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]