Skip to content

Commit

Permalink
add: using sparql as interface to workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholsn committed Aug 28, 2014
1 parent b007078 commit 86608fb
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
21 changes: 17 additions & 4 deletions niquery/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from flask import Flask, jsonify, make_response
from flask.ext.restful import Api, Resource, reqparse

from niquery.utils import NS
from niquery.query import AskQuery, SelectQuery


Expand Down Expand Up @@ -93,6 +94,8 @@ def bet(in_file_uri):
return result.provenance.rdf().serialize(format='json-ld')

parser = reqparse.RequestParser()
parser.add_argument('turtle_file')
parser.add_argument('query_uuid')


class Validate(Resource):
Expand Down Expand Up @@ -127,10 +130,20 @@ def get(self, task_id):

class Compute(Resource):
def get(self):
in_file_uri = "http://openfmri.s3.amazonaws.com/ds001/sub001/anatomy/highres001.nii.gz"
res = bet.apply_async([in_file_uri])
result = {"task_id": res.task_id, "task": "Run FSL BET", "in_file_uri": in_file_uri}
return jsonify(result=result)
select = SelectQuery()
compute = select.sparql_meta.query_type == str(NS.niq.ComputeQuery)
result = select.sparql_meta[compute]
return result.to_dict(outtype='records')

def post(self):
args = parser.parse_args()
select = SelectQuery()
input = select.execute(args['query_uuid'], turtle_str=args['turtle_file'])
return input.to_dict(outtype='records')
#in_file_uri = "http://openfmri.s3.amazonaws.com/ds001/sub001/anatomy/highres001.nii.gz"
#res = bet.apply_async([in_file_uri])
#result = {"task_id": res.task_id, "task": "Run FSL BET", "in_file_uri": in_file_uri}
#return jsonify(result=result)


class ComputeResult(Resource):
Expand Down
9 changes: 7 additions & 2 deletions niquery/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ def _get_sparql_meta(self):
format='turtle')
result = self._graph.query(self._queries['meta.rq'])
df = utils.result_to_dataframe(result)
return df.set_index(df.uri)
uuid_series = df.uri.str.slice(start=27)
uuid_series.name = 'uuid'
uuid = df.join(uuid_series)
return uuid.set_index('uuid')

def describe_query(self, index):
"""
Expand Down Expand Up @@ -81,7 +84,7 @@ def __init__(self):
super(SelectQuery, self).__init__()
self.sparql_meta = self._filter_queries(utils.NS.niq.Select)

def execute(self, query_index, turtle_file=None, turtle_url=None):
def execute(self, query_index, turtle_file=None, turtle_str=None, turtle_url=None):
"""
Execute a query using the index
Expand All @@ -98,6 +101,7 @@ def execute(self, query_index, turtle_file=None, turtle_url=None):
"""
query = self.get_query_string(query_index)
self._graph.parse(source=turtle_file,
data=turtle_str,
location=turtle_url,
format='turtle')
result = self._graph.query(query)
Expand Down Expand Up @@ -131,3 +135,4 @@ def execute(self, query_index, turtle_file=None, turtle_url=None):
result = self._graph.query(query)
return result.askAnswer


5 changes: 3 additions & 2 deletions niquery/sparql/4A5D1346-DDAA-450B-BEA0-B50815FA9766
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
PREFIX dcat: <http://www.w3.org/ns/dcat#>
PREFIX nidm: <http://purl.org/nidash/nidm#>

SELECT DISTINCT ?t1_uri
SELECT DISTINCT ?t1_uri ?task
WHERE {
?uri a nidm:MriAnatomicalT1 ;
dcat:downloadURL ?t1_uri ;
dcat:format nidm:NIFTI1Gzip .
}
}
VALUES (?task) {("tasks.bet")}
4 changes: 2 additions & 2 deletions niquery/sparql/meta.rq
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX nidm: <http://purl.org/nidash/nidm#>
PREFIX niq: <http://purl.org/niquery#>

SELECT DISTINCT ?uri ?title ?description ?creator ?format ?downloadURL (GROUP_CONCAT(?keyword;SEPARATOR=";") AS ?keywords) ?model
SELECT DISTINCT ?uri ?title ?description ?query_type ?creator ?format ?downloadURL (GROUP_CONCAT(?keyword;SEPARATOR=";") AS ?keywords) ?model
WHERE {
?uri a ?query ;
?uri a ?query_type ;
dct:title ?title ;
dct:description ?description ;
dct:creator ?creator ;
Expand Down
6 changes: 3 additions & 3 deletions niquery/sparql/meta.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ rq:5F3F3F83-B3D3-4351-90CD-24DA52AE7FE9
niq:columns "?subjectID ?tag ?vol ?segID ?annotation ?max ?mean" .

rq:5084B377-F0EC-404F-B18C-942756CC610E
a niq:AskQuery ;
a niq:ValidationQuery ;
dct:title "Validate NIDM Dataset Descriptor Summary-level" ;
dct:description "Query a graph to Validate the MUST attributes for the summary level of a descriptor." ;
dct:creator <http://orcid.org/0000-0003-1099-3328> ;
Expand All @@ -110,8 +110,8 @@ rq:4A5D1346-DDAA-450B-BEA0-B50815FA9766
dct:title "Compute BET for every T1 image in the given rdf file" ;
dct:description "Query a graph for T1 file URIs in NIfTI GZipped format." ;
dct:creator <http://orcid.org/0000-0003-1099-3328> ;
dcat:format niq:SELECT ;
dcat:format niq:Select ;
dcat:downloadURL <https://raw.githubusercontent.com/nicholsn/niquery/master/niquery/sparql/4A5D1346-DDAA-450B-BEA0-B50815FA9766> ;
dcat:keyword "query", "compute", "fsl", "bet" ;
niq:model niq:Experiment ;
niq:columns "?t1_uri" .
niq:columns "?t1_uri ?task" .
2 changes: 1 addition & 1 deletion niquery/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def get_meta_path():
def get_sparql_queries():
queries = dict()
for rq_file in os.listdir(_sparql_path):
if rq_file.endswith('.rq'):
if not rq_file.endswith('.ttl'):
rq_path = os.path.join(_sparql_path, rq_file)
with open(rq_path) as rq:
queries.update({rq_file: rq.read()})
Expand Down

0 comments on commit 86608fb

Please sign in to comment.