简体   繁体   中英

An error in the sparql query when processing with Python

I am processing a sparql query with Python.

def query(var):
query_with_placeholder="""
SELECT ?same_context as ?context  str(?context_label) as ?context_label ?same_s as ?s str(?s_label) as ?s_label ?s_type ?p "$ENTITY_CODE$" as ?gene  str(?text) as ?text 
WHERE {
?transcript <http://purl.obolibrary.org/obo/SO_transcribed_from> <http://rdf.ebi.ac.uk/resource/ensembl/$ENTITY_CODE$> .
?transcript <http://purl.obolibrary.org/obo/SO_translates_to> ?ensembl_protein .
?reflect_protein owl:sameAs ?ensembl_protein
graph ?event {
?s ?p ?reflect_protein .
}.
{
?s a <http://lcsb.uni.lu/biokb#Chemical> .
?s owl:sameAs ?same_s .
?same_s rdfs:label ?s_label .
} UNION {
?s a <http://lcsb.uni.lu/biokb#Protein> .
?s owl:sameAs ?same_s .
?ensembl_transcript <http://purl.obolibrary.org/obo/SO_translates_to> ?same_s .
?ensembl_transcript rdfs:label ?s_label
}
?s a ?s_type .

?sentence <http://lcsb.uni.lu/biokb#hasEvent> ?event .
?sentence <http://lcsb.uni.lu/biokb#containsEntity> ?context .
?sentence <http://lcsb.uni.lu/biokb#text> ?text .
?context a <http://lcsb.uni.lu/biokb#Tissue> .
?context owl:sameAs ?same_context .
?same_context rdfs:label ?context_label .
}"""

new_query=query_with_placeholder.replace("$ENTITY_CODE$",var)

sparql = SPARQLWrapper("http://biokb.lcsb.uni.lu/sparql")

sparql.setQuery(new_query)

sparql.setReturnFormat(JSON)

result_of_query = sparql.query().convert()

return result_of_query

Var is an element from a list I am iterating through. As a result of using this function I got a mistake

HTTPError: HTTP Error 404: File not found

and

EndPointNotFound: EndPointNotFound: it was impossible to connect with the endpoint in that address, check if it is correct. 

Response:
b'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">\n<html>\n  <head>\n    <title>Error HTTP/1.1 404 File not found</title>\n  </head>\n  <body>\n    <h3>Error HTTP/1.1 404 File not found</h3><pre>\nThe requested URL was not found    URI  = \'/sparql\'\n  </pre></body></html>\n'

And thus my query stops iterating at 65th element. But I still can run the query for that elements and further ones so I suppose it must work.

Where is the mistake? And how it could be corrected?

This mistake appears when you run the query to DataBase too often during short period of time.

EndPointNotFound: EndPointNotFound: it was impossible to connect with the endpoint in that address, check if it is correct. 

Response:
b'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">\n<html>\n  <head>\n    <title>Error HTTP/1.1 404 File not found</title>\n  </head>\n  <body>\n    <h3>Error HTTP/1.1 404 File not found</h3><pre>\nThe requested URL was not found    URI  = \'/sparql\'\n  </pre></body></html>\n'

Add to python code the following:

import time
time.sleep(2)

Where 2 - time in seconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM