예제 #1
0
파일: sparql.py 프로젝트: fserena/agora-gw
    def remote(q):
        sparql = SPARQLWrapper(sparql_host)
        sparql.setRequestMethod("postdirectly")
        sparql.setMethod('POST')

        log.debug(u'Querying: {}'.format(q))
        sparql.setQuery(q)

        sparql.addCustomParameter('infer', str(infer).lower())
        if not ('construct' in q.lower()):
            sparql.setReturnFormat(JSON)
        else:
            sparql.setReturnFormat(N3)

        try:
            results = sparql.query().convert()
        except Exception as e:
            raise e

        if isinstance(results, str):
            return results.decode('utf-8').encode('utf-8', 'replace')
        else:
            if 'results' in results:
                return json.dumps(
                    results["results"]["bindings"]).decode('utf-8').encode(
                        'utf-8', 'replace')
            else:
                return json.dumps(results['boolean']).decode('utf-8').encode(
                    'utf-8', 'replace')
예제 #2
0
def get_pairs(infer):
	offset = 0
	limit = 100
	total_results = set()
	new_results = -1
	while new_results != 0:
		query_string = """
		PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
		SELECT DISTINCT ?s ?o WHERE {
		?s rdfs:subClassOf ?o.
		FILTER (?s != ?o)
		FILTER (?o != <http://www.w3.org/2000/01/rdf-schema#Resource>)
		} OFFSET %d LIMIT %d
		""" % (offset, limit)
		sparql = SPARQLWrapper(STORE)
		sparql.setReturnFormat(Wrapper.JSON)
		sparql.setQuery(query_string)
		sparql.addCustomParameter('infer', infer)
		results = sparql.query().convert()
		new_results = len(results["results"]["bindings"])
		for result in results["results"]["bindings"]:
			total_results.add((result['s']['value'], result['o']['value']))
		offset = offset + limit
	print "Downloaded %d relations" % len(total_results)
	return total_results
예제 #3
0
def sparqlOneEndpoint( endpoint, query, apikey=None ):

    out = None

    try:

        sparql=SPARQLWrapper(endpoint)

        if apikey: sparql.addCustomParameter("apikey", apikey)

        sparql.setQuery(query)

        sparql.setReturnFormat(JSON)

        sparql.setTimeout( 30 )

        #print "\n", "# " * 7, "\n", query, "\n", "# " * 7, "\n"

        out = sparql.query().convert()

    except:

        print "Could not process formulated query on indicated endpoint."
        pass

    return out
예제 #4
0
def findParents(URI):
    # Returns a pathList which includes all parents per hop in tuples [(child,parent),(child,parent)]
    global iup, pathList, endpoint
    list_out = []
    iup += 1
    if iup == 1:
        sparql = SPARQLWrapper(endpoint)
        sparql.addCustomParameter("infer", "false")
        sparql.setReturnFormat(JSON)
        querystring = 'PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?super WHERE { <' + URI[
            iup - 1][0] + '> rdfs:subClassOf ?super . FILTER isURI(?super) }'
        sparql.setQuery(querystring)
        results = sparql.query().convert()
        for x in results["results"]["bindings"]:
            list_out.append((URI[iup - 1][0], x["super"]["value"]))
    else:
        for i in range(len(URI[iup - 1])):
            sparql = SPARQLWrapper(endpoint)
            sparql.addCustomParameter("infer", "false")
            sparql.setReturnFormat(JSON)
            querystring = 'PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?super WHERE { <' + URI[
                iup -
                1][i][1] + '> rdfs:subClassOf ?super . FILTER isURI(?super) }'
            sparql.setQuery(querystring)
            results = sparql.query().convert()
            for x in results["results"]["bindings"]:
                list_out.append((URI[iup - 1][i][1], x["super"]["value"]))

    if len(list_out) > 0:
        URI.append(list_out)
        findParents(URI)
    else:
        iup = 0
        pathList = URI
        return pathList
예제 #5
0
파일: GDOQuery.py 프로젝트: ChaseBasich/GDO
    def query(self, queryString):
        sparql_service = "http://sparql.bioontology.org/sparql/"

        api_key = "7876def4-91ba-4ef2-85d4-8bf5770329dd"

        sparql = SPARQLWrapper(sparql_service)
        sparql.addCustomParameter("apikey", api_key)
        sparql.setQuery(queryString)
        sparql.setReturnFormat(JSON)
        results = sparql.query().convert()
        return results['results']['bindings']
예제 #6
0
 def _sparql_query(self, dataset, query, format=JSON):
     sparql_endpoint_select = "%s/%s/%s/%s/%s/%s" % (self.endpoint, self.version, self.path, dataset, self.sparql_path, self.sparql_select_path)
     sparql_endpoint_update = "%s/%s/%s/%s/%s/%s" % (self.endpoint, self.version, self.path, dataset, self.sparql_path, self.sparql_update_path)
     sparql = SPARQLWrapper(sparql_endpoint_select, sparql_endpoint_update)
     sparql.addCustomParameter(self.param_key, self.key)
     sparql.agent = __agent__
     sparql.setMethod(POST)
     sparql.setRequestMethod(POSTDIRECTLY)
     sparql.setReturnFormat(format)
     sparql.setQuery(query)
     return sparql.query().convert()
예제 #7
0
    def doQuery(self, endpoint, query, name=''):
        sparql = SPARQLWrapper(endpoint)
        sparql.setReturnFormat(JSON)
        sparql.addCustomParameter('soft-limit', '-1')
        sparql.setQuery(self.prefixes + "\n\n" + query)
        self.log.info("Starting query {0}...".format(name))

        tstart = time()
        res = sparql.query().convert()
        tend = time()
        self.log.debug("... done ({0}us)".format(tend - tstart))
        resultsno = len(res["results"]["bindings"])
        self.log.info("Query returned {0} results.".format(resultsno))

        return res
예제 #8
0
 def _sparql_query(self, dataset, query, format=JSON):
     sparql_endpoint_select = "%s/%s/%s/%s/%s/%s" % (
         self.endpoint, self.version, self.path, dataset, self.sparql_path,
         self.sparql_select_path)
     sparql_endpoint_update = "%s/%s/%s/%s/%s/%s" % (
         self.endpoint, self.version, self.path, dataset, self.sparql_path,
         self.sparql_update_path)
     sparql = SPARQLWrapper(sparql_endpoint_select, sparql_endpoint_update)
     sparql.addCustomParameter(self.param_key, self.key)
     sparql.agent = __agent__
     sparql.setMethod(POST)
     sparql.setRequestMethod(POSTDIRECTLY)
     sparql.setReturnFormat(format)
     sparql.setQuery(query)
     return sparql.query().convert()
예제 #9
0
    def doQuery(self, endpoint, query, name=''):
        sparql = SPARQLWrapper(endpoint)
        sparql.setReturnFormat(JSON)
        sparql.addCustomParameter('soft-limit', '-1')
        sparql.setQuery(self.prefixes+"\n\n"+query)
        self.log.info("Starting query {0}...".format(name))

        tstart = time()
        res = sparql.query().convert()
        tend = time()
        self.log.debug("... done ({0}us)".format(tend-tstart))
        resultsno = len(res["results"]["bindings"])
        self.log.info("Query returned {0} results.".format(resultsno))
        
        return res
예제 #10
0
def sparql():
    app.logger.debug('You arrived at ' + url_for('sparql'))
    app.logger.debug('I received the following arguments' + str(request.args) )
    
    endpoint = request.args.get('endpoint', None)
    query = request.args.get('query', None)
    
    return_format = request.args.get('format','JSON')
    app.logger.debug(return_format)
    
    if endpoint and query :
        sparql = SPARQLWrapper(endpoint)
        
        sparql.setQuery(query)
        
        if return_format == 'RDF':
            sparql.setReturnFormat(RDF)
            sparql.addCustomParameter('Accept','application/sparql-results+xml')

        else :
            sparql.setReturnFormat('JSON')
            sparql.addCustomParameter('Accept','application/sparql-results+json')
        
        app.logger.debug('Query:\n{}'.format(query))
        
        app.logger.debug('Querying endpoint {}'.format(endpoint))
        
        try :
            response = sparql.query().convert()
            app.logger.debug('Results were returned, yay!')
            
            app.logger.debug(response)
            
            if return_format == 'RDF':
                app.logger.debug('Serializing to Turtle format')
                app.logger.debug(response)
                return response.serialize(format='turtle')
            else :
                app.logger.debug('Directly returning JSON format')
                return jsonify(response)
        except Exception as e:
            app.logger.error('Something went wrong')
            app.logger.error(e)
            return jsonify({'result': 'Error'})
            
        
    else :
        return jsonify({'result': 'Error'})
예제 #11
0
def sparql():
    app.logger.debug('You arrived at ' + url_for('sparql'))
    app.logger.debug('I received the following arguments' + str(request.args))

    endpoint = request.args.get('endpoint', None)
    query = request.args.get('query', None)

    return_format = request.args.get('format', 'JSON')

    if endpoint and query:
        sparql = SPARQLWrapper(endpoint)

        sparql.setQuery(query)

        if return_format == 'RDF':
            sparql.setReturnFormat(RDF)
        else:
            sparql.setReturnFormat('JSON')
            sparql.addCustomParameter('Accept',
                                      'application/sparql-results+json')

        app.logger.debug('Query:\n{}'.format(query))

        app.logger.debug('Querying endpoint {}'.format(endpoint))

        try:
            response = sparql.query().convert()

            app.logger.debug('Results were returned, yay!')

            app.logger.debug(response)

            if return_format == 'RDF':
                app.logger.debug('Serializing to Turtle format')
                return response.serialize(format='turtle')
            else:
                app.logger.debug('Directly returning JSON format')
                return jsonify(response)
        except Exception as e:
            app.logger.error('Something went wrong')
            app.logger.error(e)
            return jsonify({'result': 'Error'})

    else:
        return jsonify({'result': 'Error'})
예제 #12
0
def runQuery(q_template,
             param_value_dict,
             return_vars,
             params={},
             endpoint="http://sparql.bioontology.org/sparql"):
    t = Template(q_template)
    q = t.substitute(**param_value_dict)
    sparql = SPARQLWrapper(endpoint)
    for key in params:
        sparql.addCustomParameter(key, params[key])
    sparql.setQuery(q)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    output = [[] for v in return_vars]
    for result in results["results"]["bindings"]:
        i = 0
        for v in return_vars:
            output[i].append(result[v]["value"])
            i += 1
    return output
예제 #13
0
파일: cyttron.py 프로젝트: RedSunCMX/thesis
def getDescs():
    global desc,sparql,endpoint
    sparql = SPARQLWrapper(endpoint)
    sparql.addCustomParameter("infer","false")
    sparql.setReturnFormat(JSON)
    # NCI
    sparql.setQuery("""
        PREFIX owl: <http://www.w3.org/2002/07/owl#>
        PREFIX nci:<http://ncicb.nci.nih.gov/xml/owl/EVS/Thesaurus.owl#>

        SELECT ?URI ?def
        WHERE {
            ?URI a owl:Class .
            ?URI nci:DEFINITION ?def .
        }
    """)

    results = sparql.query().convert()
    for x in results["results"]["bindings"]:
        ### Strip tags
        p = re.compile(r'<.*?>')
        cleanDesc = p.sub('',x["def"]["value"])
        desc.append((cleanDesc,x["URI"]["value"]))

    sparql.setQuery("""
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX owl: <http://www.w3.org/2002/07/owl#>
        PREFIX nci:<http://ncicb.nci.nih.gov/xml/owl/EVS/Thesaurus.owl#>

        SELECT ?URI ?def
        WHERE {
            ?URI a owl:ObjectProperty .        
            ?URI nci:DEFINITION ?def .
            }""")
    results = sparql.query().convert()
    for x in results["results"]["bindings"]:
        if 'part_of' in x["def"]["value"].lower():
            desc.append([x["def"]["value"],x["URI"]["value"]])        

    print "Filled lists: desc. With:",str(len(desc)),"entries"
    cPickle.dump(desc,open('pickle\\desc.list','w'))
예제 #14
0
파일: cyttron.py 프로젝트: RedSunCMX/thesis
def getDescs():
    global desc, sparql, endpoint
    sparql = SPARQLWrapper(endpoint)
    sparql.addCustomParameter("infer", "false")
    sparql.setReturnFormat(JSON)
    # NCI
    sparql.setQuery("""
        PREFIX owl: <http://www.w3.org/2002/07/owl#>
        PREFIX nci:<http://ncicb.nci.nih.gov/xml/owl/EVS/Thesaurus.owl#>

        SELECT ?URI ?def
        WHERE {
            ?URI a owl:Class .
            ?URI nci:DEFINITION ?def .
        }
    """)

    results = sparql.query().convert()
    for x in results["results"]["bindings"]:
        ### Strip tags
        p = re.compile(r'<.*?>')
        cleanDesc = p.sub('', x["def"]["value"])
        desc.append((cleanDesc, x["URI"]["value"]))

    sparql.setQuery("""
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX owl: <http://www.w3.org/2002/07/owl#>
        PREFIX nci:<http://ncicb.nci.nih.gov/xml/owl/EVS/Thesaurus.owl#>

        SELECT ?URI ?def
        WHERE {
            ?URI a owl:ObjectProperty .        
            ?URI nci:DEFINITION ?def .
            }""")
    results = sparql.query().convert()
    for x in results["results"]["bindings"]:
        if 'part_of' in x["def"]["value"].lower():
            desc.append([x["def"]["value"], x["URI"]["value"]])

    print "Filled lists: desc. With:", str(len(desc)), "entries"
    cPickle.dump(desc, open('pickle\\desc.list', 'w'))
예제 #15
0
파일: cyttron.py 프로젝트: RedSunCMX/thesis
def getLabels():
    global label, sparql, endpoint
    label = []
    print endpoint
    sparql = SPARQLWrapper(endpoint)
    sparql.addCustomParameter("infer", "false")
    sparql.setReturnFormat(JSON)
    sparql.setQuery("""
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX owl: <http://www.w3.org/2002/07/owl#>

        SELECT ?URI ?label
        WHERE {
            ?URI a owl:Class .        
            ?URI rdfs:label ?label .
            }
    """)

    results = sparql.query().convert()
    for x in results["results"]["bindings"]:
        label.append([x["label"]["value"], x["URI"]["value"]])
    print "LABEL | Filled list: label. With:", str(len(label)), "entries"

    # Fetch 'Part_Of' properties
    sparql.setQuery("""
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX owl: <http://www.w3.org/2002/07/owl#>

        SELECT ?URI ?label
        WHERE {
            ?URI a owl:ObjectProperty .        
            ?URI rdfs:label ?label .
            }""")
    results = sparql.query().convert()
    for x in results["results"]["bindings"]:
        if 'part_of' in x["label"]["value"].lower():
            label.append([x["label"]["value"], x["URI"]["value"]])
    print "LABEL | Filled list: label. With:", str(len(label)), "entries"

    cPickle.dump(label, open('pickle\\label.list', 'w'))
예제 #16
0
파일: cyttron.py 프로젝트: RedSunCMX/thesis
def getLabels():
    global label,sparql,endpoint
    label = []
    print endpoint
    sparql = SPARQLWrapper(endpoint)
    sparql.addCustomParameter("infer","false")
    sparql.setReturnFormat(JSON)
    sparql.setQuery("""
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX owl: <http://www.w3.org/2002/07/owl#>

        SELECT ?URI ?label
        WHERE {
            ?URI a owl:Class .        
            ?URI rdfs:label ?label .
            }
    """)
    
    results = sparql.query().convert()
    for x in results["results"]["bindings"]:
        label.append([x["label"]["value"],x["URI"]["value"]])
    print "LABEL | Filled list: label. With:",str(len(label)),"entries"

    # Fetch 'Part_Of' properties
    sparql.setQuery("""
        PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
        PREFIX owl: <http://www.w3.org/2002/07/owl#>

        SELECT ?URI ?label
        WHERE {
            ?URI a owl:ObjectProperty .        
            ?URI rdfs:label ?label .
            }""")
    results = sparql.query().convert()
    for x in results["results"]["bindings"]:
        if 'part_of' in x["label"]["value"].lower():
            label.append([x["label"]["value"],x["URI"]["value"]])
    print "LABEL | Filled list: label. With:",str(len(label)),"entries"

    cPickle.dump(label,open('pickle\\label.list','w'))
예제 #17
0
파일: semsim.py 프로젝트: graus/thesis
def findParents(URI):
    # Returns a pathList which includes all parents per hop in tuples [(child,parent),(child,parent)]
    global iup, pathList, endpoint
    list_out = []
    iup += 1
    if iup == 1:
        sparql = SPARQLWrapper(endpoint)
        sparql.addCustomParameter("infer", "false")
        sparql.setReturnFormat(JSON)
        querystring = (
            "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?super WHERE { <"
            + URI[iup - 1][0]
            + "> rdfs:subClassOf ?super . FILTER isURI(?super) }"
        )
        sparql.setQuery(querystring)
        results = sparql.query().convert()
        for x in results["results"]["bindings"]:
            list_out.append((URI[iup - 1][0], x["super"]["value"]))
    else:
        for i in range(len(URI[iup - 1])):
            sparql = SPARQLWrapper(endpoint)
            sparql.addCustomParameter("infer", "false")
            sparql.setReturnFormat(JSON)
            querystring = (
                "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> SELECT DISTINCT ?super WHERE { <"
                + URI[iup - 1][i][1]
                + "> rdfs:subClassOf ?super . FILTER isURI(?super) }"
            )
            sparql.setQuery(querystring)
            results = sparql.query().convert()
            for x in results["results"]["bindings"]:
                list_out.append((URI[iup - 1][i][1], x["super"]["value"]))

    if len(list_out) > 0:
        URI.append(list_out)
        findParents(URI)
    else:
        iup = 0
        pathList = URI
        return pathList
예제 #18
0
def runQuery(query, return_format, endpoint=SPARQL_ENDPOINT, jsoni=True):
    if endpoint and query :
        sparql = SPARQLWrapper(endpoint)
        
        sparql.setQuery(query)
        
        if return_format == 'RDF':
            sparql.setReturnFormat('RDF')
        else :
            sparql.setReturnFormat('JSON')
            sparql.addCustomParameter('Accept','application/sparql-results+json')
        
        #app.logger.debug('Query:\n{}'.format(query))
        #app.logger.debug('Querying endpoint {}'.format(endpoint))
        
        try :
            response = sparql.query().convert()
            
            # app.logger.debug('Results were returned, yay!')
            
            # app.logger.debug(response)
            
            if return_format == 'RDF':
                # app.logger.debug('Serializing to Turtle format')
                return response.serialize(format='turtle')
            else :
                # app.logger.debug('Directly returning JSON format')
                if jsoni == True:
					return jsonify(response)
                else:
					return json.dumps(response)
        except Exception as e:
            app.logger.error('Something went wrong')
            app.logger.error(e)
            return jsonify({'result': 'Error'})
            
        
    else :
        return jsonify({'result': 'Error'})
예제 #19
0
# Get Graph URIs

q = """PREFIX dcterms: <http://purl.org/dc/terms/> 
PREFIX metalex: <http://www.metalex.eu/schema/1.0#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX bwb: <http://doc.metalex.eu/bwb/ontology/>

SELECT DISTINCT ?x WHERE {
   ?x bwb:bwb-id ?id .
   ?x a metalex:BibliographicExpression .
}"""

sparql = SPARQLWrapper("http://doc.metalex.eu:8000/sparql/")
sparql.setQuery(q)
sparql.addCustomParameter("soft-limit", "-1")

sparql.setReturnFormat(JSON)

results = sparql.query().convert()

for result in results['results']['bindings']:
    uri = result['x']['value']

    construct_query = """PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX ml: <http://www.metalex.eu/schema/1.0#>
    PREFIX sem: <http://semanticweb.cs.vu.nl/2009/11/sem/>
    
    CONSTRUCT {
      ?e rdf:type sem:Event .
예제 #20
0
            print("seed_query didn't retrieve any classes.")

    entity_graph = Graph()
    return entity_graph + extract_bioportal_property_paths(
        seeds=seeds,
        bioportal=bioportal,
        properties=properties,
        verbose=verbose,
        **extract_params)


if __name__ == '__main__':
    #Adapted from https://github.com/ncbo/sparql-code-examples/blob/master/python/sparql1.py
    BIOPORTAL_API_KEY = os.environ['BIOPORTAL_API_KEY']
    bioportal = SPARQLWrapper('http://sparql.bioontology.org/sparql/')
    bioportal.addCustomParameter("apikey", BIOPORTAL_API_KEY)

    #We're going to use CHEAR as the base context
    CHEAR_LOCATION = "/home/s/projects/TWC/chear-ontology/chear.ttl"
    ## This seed query will select all ChEBI classes present in CHEAR.
    seed_query = """
		PREFIX owl: <http://www.w3.org/2002/07/owl#>
		SELECT DISTINCT ?c WHERE{
			?c a owl:Class .
			FILTER(regex(str(?c), "http://purl.obolibrary.org/obo/CHEBI"))
		}
		"""
    graph = Graph()
    graph.parse(CHEAR_LOCATION, format='turtle')
    seeds = {row[0] for row in graph.query(seed_query)}
prefixes = """
some long prefixes here
"""

AERS = Namespace("http://aers.data2semantics.org/resource/")
DBPEDIA = Namespace("http://dbpedia.org/resource/")
SIDER = Namespace("http://www4.wiwiss.fu-berlin.de/sider/resource/sider/")
OWL = Namespace("http://www.w3.org/2002/07/owl#")
SKOS = Namespace("http://www.w3.org/2004/02/skos/core#")


if __name__ == "__main__":
    wrapper = SPARQLWrapper(AERS_SPARQL_ENDPOINT)
    wrapper.setReturnFormat(JSON)
    wrapper.addCustomParameter("soft-limit", "-1")

    q = "SELECT DISTINCT ?x1 ?x2 WHERE { ?x1 skos:exactMatch ?y . ?x2 skos:exactMatch ?y .} "

    cg = ConjunctiveGraph()

    print "Querying for " + q
    wrapper.setQuery(prefixes + q)
    raw = wrapper.query()
    print "Results are in! Now converting from JSON..."
    results = raw.convert()
    print "Done converting ..."

    resultsno = len(results["results"]["bindings"])
    print "Query returned {0} results.".format(resultsno)
    for res in results["results"]["bindings"]:
예제 #22
0
        http://sparql-wrapper.sourceforge.net/
"""

from SPARQLWrapper import SPARQLWrapper, JSON, XML, N3, RDF
import pdb

if __name__ == "__main__":
    sparql_service = "http://sparql.bioontology.org/sparql/"

    #To get your API key register at http://bioportal.bioontology.org/accounts/new
    api_key = "YOUR API KEY"

    #Some sample query.
    query_string = """ 
PREFIX omv: <http://omv.ontoware.org/2005/05/ontology#>

SELECT ?ont ?name ?acr
WHERE { ?ont a omv:Ontology;
             omv:acronym ?acr;
             omv:name ?name .
} 
"""
    sparql = SPARQLWrapper(sparql_service)
    sparql.addCustomParameter("apikey", api_key)
    sparql.setQuery(query_string)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    for result in results["results"]["bindings"]:
        print result["ont"]["value"], result["name"]["value"], result["acr"][
            "value"]
예제 #23
0
import re
from urllib import unquote_plus
import networkx as nx
from networkx.readwrite import json_graph
import json
from rdflib import Graph, Namespace, RDF, RDFS, Literal, URIRef
from rdflib.serializer import Serializer
import rdfextras

from app import app

endpoint = app.config['SPARQL_ENDPOINT']

sparql = SPARQLWrapper(endpoint)
sparql.setReturnFormat(JSON)
sparql.addCustomParameter('reasoning','SL')


concept_type_color_dict = {'popg': '#9edae5', 'inpo': '#ffbb78', 'elii': '#dbdb8d', 'idcn': '#9edae5', 'neop': '#2ca02c', 'vita': '#9467bd', 'inpr': '#c5b0d5', 'phsu': '#c5b0d5', 'blor': '#98df8a', 'hops': '#c7c7c7', 'menp': '#f7b6d2', 'phsf': '#d62728', 'ftcn': '#e377c2', 'anim': '#ff9896', 'food': '#bcbd22', 'grpa': '#ffbb78', 'geoa': '#2ca02c', 'hcpp': '#98df8a', 'lbtr': '#c7c7c7', 'ocdi': '#17becf', 'tisu': '#17becf', 'orch': '#7f7f7f', 'tmco': '#dbdb8d', 'clas': '#bcbd22', 'lipd': '#c49c94', 'dsyn': '#f7b6d2', 'horm': '#aec7e8', 'bact': '#2ca02c', 'grup': '#e377c2', 'bacs': '#ffbb78', 'enty': '#c5b0d5', 'resa': '#98df8a', 'medd': '#9467bd', 'cell': '#bcbd22', 'fndg': '#ff7f0e', 'sbst': '#ff9896', 'prog': '#ff9896', 'celf': '#aec7e8', 'chvf': '#1f77b4', 'diap': '#aec7e8', 'celc': '#8c564b', 'hcro': '#ff7f0e', 'inbe': '#9467bd', 'clna': '#ffbb78', 'acab': '#d62728', 'bodm': '#9467bd', 'patf': '#e377c2', 'carb': '#c7c7c7', 'bpoc': '#d62728', 'dora': '#8c564b', 'moft': '#7f7f7f', 'plnt': '#7f7f7f', 'ortf': '#f7b6d2', 'bmod': '#9edae5', 'sosy': '#dbdb8d', 'enzy': '#d62728', 'qnco': '#1f77b4', 'imft': '#7f7f7f', 'antb': '#1f77b4', 'bdsy': '#c5b0d5', 'nnon': '#9467bd', 'socb': '#c49c94', 'ocac': '#8c564b', 'bdsu': '#8c564b', 'rcpt': '#ff9896', 'nsba': '#c5b0d5', 'mnob': '#e377c2', 'orga': '#1f77b4', 'orgf': '#c7c7c7', 'lbpr': '#d62728', 'orgt': '#aec7e8', 'gngm': '#f7b6d2', 'virs': '#17becf', 'fngs': '#98df8a', 'aapp': '#17becf', 'opco': '#c49c94', 'irda': '#98df8a', 'famg': '#2ca02c', 'acty': '#ff7f0e', 'inch': '#bcbd22', 'cnce': '#9edae5', 'topp': '#ffbb78', 'spco': '#2ca02c', 'lang': '#dbdb8d', 'podg': '#aec7e8', 'mobd': '#ff9896', 'qlco': '#c49c94', 'npop': '#ff7f0e', 'hlca': '#1f77b4', 'phpr': '#ff7f0e', 'strd': '#8c564b'}


def uri_to_label(uri):
    return unquote_plus(uri.encode('utf-8').replace('http://eligibility.data2semantics.org/resource/','')).replace('_',' ').lstrip('-').lstrip(' ').title()


def get_trials():
    q = render_template('trials.q')
    
    sparql.setQuery(q)

    results = sparql.query().convert()
예제 #24
0
from SPARQLWrapper import SPARQLWrapper, JSON

sparql = SPARQLWrapper('http://sparql.bioontology.org/sparql/')
api_key = '5dd5cf8a-bdc9-4d46-9c3d-10aadff89444'

sparql.addCustomParameter('apikey', api_key)

q = '''
PREFIX umls: <http://bioportal.bioontology.org/ontologies/umls/> 
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> 
PREFIX ndfrt: <http://purl.bioontology.org/ontology/NDFRT/> 
SELECT *
WHERE {
  ?s ndfrt:NDFRT_KIND ?o;
  skos:prefLabel ?label;
  umls:cui ?cui;
  ndfrt:SEVERITY ?severity. FILTER (regex(str(?o), "interaction", "i"))
  ?s ndfrt:has_participant ?targetDrug.
 }
'''

sparql.setQuery(q)
sparql.setReturnFormat(JSON)
resultset = sparql.query().convert()

cache = [None, None]
for i in range(0, len(resultset['results']['bindings'])):
    uri = resultset['results']['bindings'][i]['s']['value']
    if uri == cache[0]:
        drug1 = cache[1].rpartition('/')[2]
        drug2 = resultset['results']['bindings'][i]['targetDrug'][
예제 #25
0
# Get Graph URIs

q = """PREFIX dcterms: <http://purl.org/dc/terms/> 
PREFIX metalex: <http://www.metalex.eu/schema/1.0#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX bwb: <http://doc.metalex.eu/bwb/ontology/>

SELECT DISTINCT ?x WHERE {
   ?x bwb:bwb-id ?id .
   ?x a metalex:BibliographicExpression .
}"""

sparql = SPARQLWrapper("http://doc.metalex.eu:8000/sparql/")
sparql.setQuery(q)
sparql.addCustomParameter("soft-limit", "-1")
    
sparql.setReturnFormat(JSON)

results = sparql.query().convert()

for result in results['results']['bindings'] :
    uri = result['x']['value']
    
    construct_query = """PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
    PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
    PREFIX ml: <http://www.metalex.eu/schema/1.0#>
    PREFIX sem: <http://semanticweb.cs.vu.nl/2009/11/sem/>
    
    CONSTRUCT {
      ?e rdf:type sem:Event .
예제 #26
0
        http://sparql-wrapper.sourceforge.net/
"""

from SPARQLWrapper import SPARQLWrapper, JSON, XML, N3, RDF
import pdb

if __name__ == "__main__":
    # Eventually alphasparql will be moved to http://sparql.bioontology.org/sparql/
    sparql_service = "http://sparql.bioontology.org/sparql/"

    # To get your API key register at http://bioportal.bioontology.org/accounts/new
    api_key = "YOUR API KEY"

    # Some sample query.
    query_string = """ 
PREFIX omv: <http://omv.ontoware.org/2005/05/ontology#>

SELECT ?ont ?name ?acr
WHERE { ?ont a omv:Ontology;
             omv:acronym ?acr;
             omv:name ?name .
} 
"""
    sparql = SPARQLWrapper(sparql_service)
    sparql.addCustomParameter("apikey", api_key)
    sparql.setQuery(query_string)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    for result in results["results"]["bindings"]:
        print result["ont"]["value"], result["name"]["value"], result["acr"]["value"]
예제 #27
0
logHandler.setFormatter(logFormatter)

log.addHandler(logHandler)


if __name__ == '__main__':
	parser = argparse.ArgumentParser()
	parser.add_argument("endpoint", nargs='?', help="The URL of the sparql endpoint (without /sparql suffix)", default='http://eculture2.cs.vu.nl:5020')
	parser.add_argument("--empty", help="Empty the 4Store model completely", action="store_true")
	args = parser.parse_args()
	    
	SPARQL_BASE = args.endpoint
	log.info('Using {} as 4Store location.'.format(SPARQL_BASE))
	sw = SPARQLWrapper('{}/sparql/'.format(SPARQL_BASE))
	sw.setReturnFormat(JSON)
	sw.addCustomParameter('soft-limit','-1')
    
	if args.empty :
		yn = raw_input("Are you sure you want to delete all graphs from {} (y/n): ".format(SPARQL_BASE))
        
        if yn == "y" :
			sw.setReturnFormat(JSON)
			sw.addCustomParameter('soft-limit','-1')
	        
			q = """SELECT DISTINCT ?g WHERE { GRAPH ?g { ?s ?p ?o .}} """
	         
			sw.setQuery(q)
	            
			results = sw.query().convert()
			
			for result in results["results"]["bindings"] :
예제 #28
0
def extract_from_contexts(seed_iri,
                          properties,
                          property_f,
                          extract_params,
                          dest_dir,
                          verbose=False,
                          error=None):
    '''
	Loads in our seed graph from the provided IRI.
	Then iterates through our provided Java property
	file, which contains prefixes and IRIs for
	context ontologies. Entities with the associated
	prefix are pulled from the seed ontology via the seed query.
	These seed entities are then used as roots for the property 
	crawl paths in the associated context ontology. 
	'''
    #Read in the graph we'll pull seeds from
    seed_graph = Graph().parse(seed_iri)
    if verbose:
        print("Loaded seed graph.")

    #Connect to BioPortal
    BIOPORTAL_API_KEY = os.environ['BIOPORTAL_API_KEY']
    bioportal = SPARQLWrapper('http://sparql.bioontology.org/sparql/')
    bioportal.addCustomParameter("apikey", BIOPORTAL_API_KEY)
    print("Connected to BioPortal.")

    #Create template for retrieving seed classes based on prefixes
    SEED_QUERY_TEMPLATE = """
		PREFIX owl: <http://www.w3.org/2002/07/owl#>
		SELECT DISTINCT ?c WHERE{
			?c a owl:Class .
			FILTER(regex(str(?c), "%s"))
		}
		"""

    #Read in properties file
    with open(property_f, 'r') as fp:
        javaprops = javaproperties.load(fp)
    if verbose:
        print("Read properties file.")

    #Iterate through rows of properties file
    for k in javaprops.keys():
        if verbose:
            print("==========================================")
            print("Reading graph: ", str(k))
        #Pull the ontology IRI and associated prefix
        row = javaprops[k].split(',')
        prefix = row[0]
        iri = row[2]
        #Check whether we have an IRI for the row
        if iri == '':
            if verbose:
                print("No IRI provided.")
            continue
        if iri == seed_iri:
            if verbose:
                print("Context graph is same as seed graph. Skipping.")
            continue

        #Read in context graph from IRI, use as context
        context = Graph()
        FORMATS = ['xml', 'n3', 'nt', 'trix', 'turtle', 'rdfa']
        read_success = False
        for form in FORMATS:
            try:
                #If we successfully read our ontology, recurse
                context = Graph().parse(iri, format=form)
                read_success = True
                if verbose:
                    print("Read as ", form, ".")
                break
            except Exception as e:
                pass
        if not read_success:
            #Last-ditch effort: try to guess the file extension
            try:
                if verbose:
                    print(
                        "Exhausted format list. Attempting to guess format...")
                context = Graph().parse(iri, format=guess_format(iri))
                if verbose:
                    print("Read as ", guess_format(iri), ".")
                break
            except Exception as e:
                pass
            #Error handling, quiet or fast failing
            if error is None:
                raise Exception("Exhausted format list. Failing quickly.")
            if error == 'ignore':
                print("Exhausted format list. Quietly ignoring failure.")
                continue

        #Expand property paths from context
        gout = retrieve_crawl_paths_from_context(
            seed_graph=seed_graph,
            context=context,
            properties=properties,
            seed_query=SEED_QUERY_TEMPLATE % (prefix, ),
            expand_ontologies=True,
            import_error=error,
            verbose=verbose,
            inplace=False,
            extract_params=extract_params)

        #Get the BioPortal extract as well
        bio_seeds = {
            row[0]
            for row in seed_graph.query(SEED_QUERY_TEMPLATE % (prefix, ))
        }
        bio_gout = bioportal_retrieve_crawl_paths(
            properties=properties,
            bioportal=bioportal,
            seeds=bio_seeds,
            verbose=verbose,
            extract_params=extract_params)

        #Write out
        gout.serialize(path.join(dest_dir, k + '.ttl'), format='turtle')
        bio_gout.serialize(path.join(path.join(dest_dir, 'bioportal/'),
                                     k + '_bioportal.ttl'),
                           format='turtle')
        print("Wrote out extracts for " + k + ".")
        del gout
예제 #29
0
    PREFIX calbc-group: <http://linkedlifedata.com/resource/calbc/group/>
    PREFIX drugbank: <http://www4.wiwiss.fu-berlin.de/drugbank/resource/drugbank/>
    PREFIX ctcae: <http://ncicb.nci.nih.gov/xml/owl/EVS/ctcae.owl#>
    
"""

AERS = Namespace("http://aers.data2semantics.org/resource/")
DBPEDIA = Namespace("http://dbpedia.org/resource/")
SIDER = Namespace("http://www4.wiwiss.fu-berlin.de/sider/resource/sider/")
OWL = Namespace("http://www.w3.org/2002/07/owl#")
SKOS = Namespace("http://www.w3.org/2004/02/skos/core#")

if __name__ == '__main__':
    wrapper = SPARQLWrapper(AERS_SPARQL_ENDPOINT)
    wrapper.setReturnFormat(JSON)
    wrapper.addCustomParameter('soft-limit', '-1')

    q = "SELECT ?x ?y WHERE { ?x skos:exactMatch ?y . } "

    cg = ConjunctiveGraph()

    print "Querying for " + q
    wrapper.setQuery(prefixes + q)
    results = wrapper.query().convert()

    for res in results["results"]["bindings"]:
        x = res["x"]["value"]
        y = res["y"]["value"]
        print x, y
        cg.add((URIRef(y), SKOS["exactMatch"], URIRef(x)))
prefixes = """
some long prefixes
"""


AERS = Namespace("http://aers.data2semantics.org/resource/")
DBPEDIA = Namespace("http://dbpedia.org/resource/")
SIDER = Namespace("http://www4.wiwiss.fu-berlin.de/sider/resource/sider/")
OWL = Namespace("http://www.w3.org/2002/07/owl#")
SKOS = Namespace("http://www.w3.org/2004/02/skos/core#")

if __name__ == '__main__':
	wrapper = SPARQLWrapper(AERS_SPARQL_ENDPOINT)
	wrapper.setReturnFormat(JSON)
	wrapper.addCustomParameter('soft-limit','-1')
    
	q = "SELECT ?x ?y WHERE { ?x skos:exactMatch ?y . } "
    
	cg = ConjunctiveGraph()
    
	print "Querying for "+ q
	wrapper.setQuery(prefixes+q)
	results = wrapper.query().convert()
    
	for res in results["results"]["bindings"] :
		x = res["x"]["value"]
		y = res["y"]["value"]
		print x, y
		cg.add(URIRef(y),SKOS["exactMatch"],URIRef(x))
        
예제 #31
0
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "endpoint",
        nargs='?',
        help="The URL of the sparql endpoint (without /sparql suffix)",
        default='http://eculture2.cs.vu.nl:5020')
    parser.add_argument("--empty",
                        help="Empty the 4Store model completely",
                        action="store_true")
    args = parser.parse_args()

    SPARQL_BASE = args.endpoint
    log.info('Using {} as 4Store location.'.format(SPARQL_BASE))
    sw = SPARQLWrapper('{}/sparql/'.format(SPARQL_BASE))
    sw.setReturnFormat(JSON)
    sw.addCustomParameter('soft-limit', '-1')

    if args.empty:
        yn = raw_input(
            "Are you sure you want to delete all graphs from {} (y/n): ".
            format(SPARQL_BASE))

        if yn == "y":
            sw.setReturnFormat(JSON)
            sw.addCustomParameter('soft-limit', '-1')

            q = """SELECT DISTINCT ?g WHERE {
             GRAPH ?g {
               ?s ?p ?o .
             }
            } """