Spaces:
Running
Running
Create virtuosoQueryRest.py
Browse files- virtuosoQueryRest.py +203 -0
virtuosoQueryRest.py
ADDED
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
from requests.auth import HTTPDigestAuth, HTTPBasicAuth
|
3 |
+
import ssl
|
4 |
+
import json
|
5 |
+
|
6 |
+
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
|
11 |
+
def execute_query(endpoint, query, auth):
|
12 |
+
headers = {
|
13 |
+
'Content-Type': 'application/x-www-form-urlencoded',
|
14 |
+
'Accept': 'application/sparql-results+json'
|
15 |
+
}
|
16 |
+
|
17 |
+
params = {
|
18 |
+
'query': query
|
19 |
+
}
|
20 |
+
|
21 |
+
response = requests.post(endpoint, headers=headers, params=params, auth=auth)
|
22 |
+
|
23 |
+
if response.status_code != 200:
|
24 |
+
raise Exception(f"Failed to execute query: {response.text}")
|
25 |
+
|
26 |
+
return response.text
|
27 |
+
|
28 |
+
|
29 |
+
def execute_query_no_cache(endpoint, query, auth):
|
30 |
+
headers = {
|
31 |
+
'Content-Type': 'application/x-www-form-urlencoded',
|
32 |
+
'Accept': 'application/sparql-results+json'
|
33 |
+
}
|
34 |
+
|
35 |
+
params = {
|
36 |
+
'query': query
|
37 |
+
}
|
38 |
+
|
39 |
+
response = requests.post(endpoint, headers=headers, params=params, auth=auth)
|
40 |
+
|
41 |
+
if response.status_code != 200:
|
42 |
+
raise Exception(f"Failed to execute query: {response.text}")
|
43 |
+
|
44 |
+
return response.text
|
45 |
+
|
46 |
+
|
47 |
+
def sparqlQuery(endpoint, query, usernameVirt, passwordVirt, USE_CACHE=True):
|
48 |
+
"""
|
49 |
+
Make a SPARQL query to a Virtuoso SPARQL endpoint.
|
50 |
+
|
51 |
+
Args:
|
52 |
+
- endpoint (str): The URL of the Virtuoso SPARQL endpoint.
|
53 |
+
- query (str): The SPARQL query to execute.
|
54 |
+
- username (str): The username for authentication.
|
55 |
+
- password (str): The password for authentication.
|
56 |
+
|
57 |
+
Returns:
|
58 |
+
- responseText (requests.Response): The responseText from the Virtuoso SPARQL endpoint.
|
59 |
+
"""
|
60 |
+
|
61 |
+
# Use SSL context to establish a secure connection
|
62 |
+
ssl_context = ssl.create_default_context()
|
63 |
+
|
64 |
+
# Try HTTP Digest authentication
|
65 |
+
try:
|
66 |
+
auth = HTTPDigestAuth(usernameVirt, passwordVirt)
|
67 |
+
if USE_CACHE:
|
68 |
+
responseText = execute_query(endpoint, query, auth)
|
69 |
+
else:
|
70 |
+
responseText = execute_query_no_cache(endpoint, query, auth)
|
71 |
+
except Exception as e:
|
72 |
+
print(f"HTTP Digest authentication failed: {str(e)}")
|
73 |
+
|
74 |
+
# Fallback to Basic Auth
|
75 |
+
try:
|
76 |
+
auth = HTTPBasicAuth(usernameVirt, passwordVirt)
|
77 |
+
if USE_CACHE:
|
78 |
+
responseText = execute_query(endpoint, query, auth)
|
79 |
+
else:
|
80 |
+
responseText = execute_query_no_cache(endpoint, query, auth)
|
81 |
+
except Exception as e:
|
82 |
+
print(f"Basic Auth failed: {str(e)}")
|
83 |
+
return None
|
84 |
+
|
85 |
+
return responseText
|
86 |
+
|
87 |
+
|
88 |
+
#
|
89 |
+
# def sparqlQuery(endpoint, query, usernameVirt, passwordVirt):
|
90 |
+
# """
|
91 |
+
# Make a SPARQL query to a Virtuoso SPARQL endpoint.
|
92 |
+
#
|
93 |
+
# Args:
|
94 |
+
# - endpoint (str): The URL of the Virtuoso SPARQL endpoint.
|
95 |
+
# - query (str): The SPARQL query to execute.
|
96 |
+
# - username (str): The username for authentication.
|
97 |
+
# - password (str): The password for authentication.
|
98 |
+
#
|
99 |
+
# Returns:
|
100 |
+
# - response (requests.Response): The response from the Virtuoso SPARQL endpoint.
|
101 |
+
# """
|
102 |
+
# headers = {
|
103 |
+
# 'Content-Type': 'application/x-www-form-urlencoded',
|
104 |
+
# 'Accept': 'application/sparql-results+json'
|
105 |
+
# }
|
106 |
+
#
|
107 |
+
# params = {
|
108 |
+
# 'query': query
|
109 |
+
# }
|
110 |
+
#
|
111 |
+
# # Use SSL context to establish a secure connection
|
112 |
+
# ssl_context = ssl.create_default_context()
|
113 |
+
#
|
114 |
+
# # Try HTTP Digest authentication
|
115 |
+
# try:
|
116 |
+
# auth = HTTPDigestAuth(usernameVirt, passwordVirt)
|
117 |
+
# response = requests.post(endpoint, headers=headers, params=params, auth=auth)
|
118 |
+
# except Exception as e:
|
119 |
+
# print(f"HTTP Digest authentication failed: {str(e)}")
|
120 |
+
#
|
121 |
+
# # Fallback to Basic Auth
|
122 |
+
# try:
|
123 |
+
# auth = HTTPBasicAuth(usernameVirt, passwordVirt)
|
124 |
+
# response = requests.post(endpoint, headers=headers, params=params, auth=auth)
|
125 |
+
# except Exception as e:
|
126 |
+
# print(f"Basic Auth failed: {str(e)}")
|
127 |
+
# return None
|
128 |
+
#
|
129 |
+
# if response.status_code != 200:
|
130 |
+
# raise Exception(f"Failed to execute query: {response.text}")
|
131 |
+
#
|
132 |
+
# return response
|
133 |
+
|
134 |
+
|
135 |
+
if __name__ == '__main__':
|
136 |
+
# Example usage
|
137 |
+
endpoint = 'https://api-vast.jrc.service.ec.europa.eu/sparql'
|
138 |
+
|
139 |
+
VirtuosoUsername = 'dba'
|
140 |
+
VirtuosoPassword = ''
|
141 |
+
Virtuosokey_filename = 'VIRTUOSO-dba.key'
|
142 |
+
|
143 |
+
USE_CACHE = False # True or False
|
144 |
+
|
145 |
+
#############
|
146 |
+
|
147 |
+
#query = 'SELECT * WHERE { ?s ?p ?o } LIMIT 100'
|
148 |
+
|
149 |
+
# word ="acute sinusitis"
|
150 |
+
# query = f"""
|
151 |
+
# SELECT ?concept ?label (COUNT(?edge) AS ?score)
|
152 |
+
# WHERE {{
|
153 |
+
# ?concept skos:prefLabel|rdfs:label|skos:altLabel|obo:hasRelatedSynonym ?label .
|
154 |
+
# FILTER (LCASE(STR(?label)) = "{word}")
|
155 |
+
# ?concept ?edge ?o .
|
156 |
+
# }}
|
157 |
+
# GROUP BY ?concept ?label
|
158 |
+
# ORDER BY DESC(?score)
|
159 |
+
# """
|
160 |
+
|
161 |
+
choices = ['SNOMED', 'LOINC', 'ICD10', 'MESH', 'NCIT'] # restricts the input to these values only
|
162 |
+
|
163 |
+
# Construct the FROM clauses
|
164 |
+
from_clauses = ' '.join([f"FROM <{choice}>" for choice in choices])
|
165 |
+
|
166 |
+
word = "acute sinusitis"
|
167 |
+
# Construct the full SPARQL query
|
168 |
+
query = f"""
|
169 |
+
SELECT ?concept ?label (COUNT(?edge) AS ?score)
|
170 |
+
{from_clauses}
|
171 |
+
WHERE {{
|
172 |
+
?concept skos:prefLabel|rdfs:label|skos:altLabel|obo:hasRelatedSynonym ?label .
|
173 |
+
FILTER (LCASE(STR(?label)) = "{word.lower()}")
|
174 |
+
?concept ?edge ?o .
|
175 |
+
}}
|
176 |
+
GROUP BY ?concept ?label
|
177 |
+
ORDER BY DESC(?score)
|
178 |
+
"""
|
179 |
+
|
180 |
+
print(query)
|
181 |
+
|
182 |
+
|
183 |
+
|
184 |
+
|
185 |
+
###############
|
186 |
+
|
187 |
+
if Virtuosokey_filename:
|
188 |
+
fkeyname = Virtuosokey_filename
|
189 |
+
with open(fkeyname) as f:
|
190 |
+
VirtuosoPassword = f.read()
|
191 |
+
|
192 |
+
responseText = sparqlQuery(endpoint, query, VirtuosoUsername, VirtuosoPassword, USE_CACHE)
|
193 |
+
|
194 |
+
# Parse the response as JSON
|
195 |
+
|
196 |
+
results = json.loads(responseText)
|
197 |
+
|
198 |
+
# Print the results
|
199 |
+
if len(results) > 0 and results['results']['bindings']:
|
200 |
+
for result in results['results']['bindings']:
|
201 |
+
print(result)
|
202 |
+
else:
|
203 |
+
print("!!! VIRTUOSO NO RESULTS !!!")
|