Skip to content

Commit

Permalink
adjusted query examples;added method to direct onto uris to bioportal
Browse files Browse the repository at this point in the history
  • Loading branch information
gebele committed Oct 28, 2016
1 parent e65a0bb commit 69b88a6
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 28 deletions.
17 changes: 11 additions & 6 deletions application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,16 @@
end

# sparql endpoint access
get '/sparql/:query?' do
#get '/sparql/:query?' do
get '/sparql/?' do
halt 400, "Missing query parameter.\n" unless params[:query]
halt 400, "Missing query parameter.\n" if params[:query].empty?
sparqlstring = params[:query]
halt 400, "Not allowed SPARQL keyword.\n" if !!(sparqlstring =~ /\b(delete|insert|load|clear)\b/i)
type = request.env["HTTP_ACCEPT"]
if (sparqlstring =~ /^select/i ? @accepted_select.include?(type) : @accepted_construct.include?(type) )

#if (sparqlstring =~ /^select/i ? @accepted_select.include?(type) : @accepted_construct.include?(type) )
if ( @accepted_select.include?(type) || @accepted_construct.include?(type) )
RestClient::Resource.new(URI.encode("#{$service_uri}/sparql/?query=#{sparqlstring}"), :verify_ssl => 0, :headers => {:accept => type } ).get do |response,request,result|
if response.code == 400
halt response.code, "malformed query\n"
Expand All @@ -91,12 +96,12 @@

# sparql result as file download in various formats
get "/download" do
type = params[:query_type]
sparqlstring = params[:queryfield]
type = params[:queryType]
sparqlstring = params[:queryField]
halt 400, "Not allowed SPARQL keyword.\n" if !!(sparqlstring =~ /\b(delete|insert|load|clear)\b/i)
file = Tempfile.new("enm")

if (sparqlstring =~ /\b(select)\b/i ? @accepted_select.include?(type) : @accepted_construct.include?(type) )
#TODO check for SELECT keyword else return a message instead wrong type
if ( @accepted_select.include?(type) || @accepted_construct.include?(type) )
RestClient::Resource.new(URI.encode("#{$service_uri}/sparql/?query=#{sparqlstring}"), :verify_ssl => 0, :headers => {:accept => type}).get do |response, request, result|
if response.code == 400
halt response.code, "malformed query\n"
Expand Down
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ require File.expand_path './application.rb'
require "sinatra/reloader" if development?
FileUtils.mkdir_p 'log' unless File.exists?('log')
log = File.new("log/enm-ontoviewer.log", "a")
$stdout.reopen(log)
#$stdout.reopen(log)
#$stderr.reopen(log)
run Sinatra::Application
2 changes: 1 addition & 1 deletion help.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Please also note the part `{,5}` which is a [property path](https://www.w3.org/T

---
#### Sparql
This page allows you to write [SPARQL](https://www.w3.org/TR/rdf-sparql-query/) queries without any restrictions and with full [SPARQL](https://www.w3.org/TR/rdf-sparql-query/) syntax support including subqueries. The result can be downloaded to your computer. Please choose one of the output formats from the drop-down list.
This page allows you to write [SPARQL](https://www.w3.org/TR/rdf-sparql-query/) queries without any restrictions and with full [SPARQL](https://www.w3.org/TR/rdf-sparql-query/) syntax support including subqueries. The result can be downloaded to your computer. Please choose one of the output formats from the drop-down list. There is also an option to render results as HTML table to the page.
3 changes: 2 additions & 1 deletion public/examples/example1
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ SELECT DISTINCT ?resource ?epLabel ?value ?unit WHERE {
obo:OBI_0000299 ?endpoint .
?endpoint sio:has-value ?value ;
sio:has-unit ?unit ;
rdfs:label ?epLabel.
rdfs:label ?endpLabel.
}
BIND("PARTICLE SIZE" AS ?epLabel)
FILTER(BOUND(?endpoint))
FILTER(?unit ="nm")
}
5 changes: 3 additions & 2 deletions public/examples/example3
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ SELECT DISTINCT ?resource ?epLabel ?value ?unit WHERE {
obo:OBI_0000299 ?endpoint .
?endpoint sio:has-value ?value ;
sio:has-unit ?unit ;
rdfs:label ?epLabel.
rdfs:label ?endpLabel.
}
BIND("ZETA POTENTIAL" AS ?epLabel)
FILTER(!BOUND(?endpoint))
FILTER(BOUND(?endpoint))
FILTER(?unit ="mV")
}
53 changes: 36 additions & 17 deletions views/query.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
:javascript
$(document).ready(function() {
$('#selType').on('change', function() {
var el = document.getElementById('selType');
var button = document.getElementById('submitRender');
if (el.value == "text/html"){
button.style.display='inline';
}else{
button.style.display='none';
}
});
});
var HttpClient = function() {
this.get = function(aUrl, aCallback) {
var anHttpRequest = new XMLHttpRequest();
Expand All @@ -14,14 +25,23 @@
function renderResponse() {
var type = "text/html";
var query = d3.select("#sparqlDownload").property("value");
var uri = "#{to("/download?query_type=")}"+type+"&queryfield="+escape(query);
var uri = "#{to("/download?queryType=")}"+type+"&queryField="+escape(query);
var div = document.getElementById("render");
div.innerHTML = "";
aClient = new HttpClient();
aClient.get(uri, function(response) {
var el = document.createElement("response");
el.innerHTML = response;
document.getElementById("render").appendChild(el);
aClient = new HttpClient();
aClient.get(uri, function(response) {
var el = document.createElement("response");
el.innerHTML = response;
document.getElementById("render").appendChild(el);
$(".sparql a").click(function() {
var t = $(this).text();
$(this).attr('alt', 'Link opens in new window.');
$(this).attr('title', 'Link opens in new window.');
$(this).attr('target', '_blank');
if (t.includes("onto")){
$(this).attr('href', 'http://bioportal.bioontology.org/ontologies/ENM/?p=classes&conceptid='+escape(t));
};
});
});
}

Expand Down Expand Up @@ -89,11 +109,11 @@
#query{:style => "margin: 10px"}
%ul.nav.nav-tabs
%li.active
%a{:href=>"#viewer", :data=>{:toggle=>"tab"}, :onclick=>"clearView();"} Viewer
%a{:href=>"#viewer", :data=>{:toggle=>"tab"}, :onclick=>"clearView();"} VIEWER
%li
%a{:href=>"#download", :data=>{:toggle=>"tab"}, :onclick=>"clearView();"} Sparql
%a{:href=>"#download", :data=>{:toggle=>"tab"}, :onclick=>"clearView();"} SPARQL
%li
%a{:href=>"#help", :data=>{:toggle=>"tab"}, :onclick=>"clearView();"} Help
%a{:href=>"#help", :data=>{:toggle=>"tab"}, :onclick=>"clearView();"} HELP

%div.tab-content{:style=>"padding-top:2em;"}
%input#endpoint{:size=>"50",:type => "text", :value => to("/search/"), :disabled=>"disabled", :style=>"display:none;"}
Expand Down Expand Up @@ -146,16 +166,17 @@
#{File.read(File.join("public/examples/"+example[0]))}
%form.form-inline.col-md-12{:id=>"form2", :role=>"form", :action=> to("/download"), :method=>"get", :style=>"padding:0;"}
%div.form-group
%select.form-control{:id=>"selType", :autocomplete=>"off", :name=>"query_type",:value=>""}
%select.form-control{:id=>"selType", :autocomplete=>"off", :name=>"queryType",:value=>""}
%optgroup{:label=>"SELECT"}
- @accepted_select.sort.each do |type|
%option{:value => type}= type
%option{:value => type, :style => ("color:green;" if type == "text/html")}= type
%optgroup{:label=>"CONSTRUCT"}
- @accepted_construct.sort.each do |type|
%option{:value => type}= type
%input#inputquery{:type=>"hidden",:name=>"queryfield", :value=>""}
%input#inputquery{:type=>"hidden",:name=>"queryField", :value=>""}
%input#renderType{:type=>"hidden",:name=>"renderType", :value=>""}
%button.btn.btn-default{:id=>"submitSubmit", :type=>"button", :title=>"render as HTML table", :onclick=>"renderResponse();"} render
%button.btn.btn-default{:id=>"submitRender", :type=>"button", :title=>"render as HTML table", :onclick=>"renderResponse();", :style=>"display:none;color:green;"} render
-#%button.btn.btn-default{:id=>"submitRender", :type=>"submit", :title=>"download as file", :onclick=>"sparqlValue('inline');"} render
%button.btn.btn-default{:id=>"submitDownload", :type=>"submit", :title=>"download as file", :onclick=>"sparqlValue('attachment');"} download

%div.col-md-6{:style=>"padding:0;"}
Expand All @@ -164,10 +185,8 @@
%p Physico-Chemical Characteristics
%fieldset
- @examples.each_with_index do |example, idx|
%input{:id=> idx, :type=>"radio", :name=>"examples", :value=>"", :onclick=>"refresh_template(#{idx})"}
= example[0]
%br
%label{:for=>example[0]}
%input{:id=> idx, :type=>"radio", :name=>"examples", :value=>idx, :onclick=>"refresh_template(#{idx})"}
%label{:for=>idx}
= example[1]
%br
#render.col-md-12{:style=>"padding:0;margin-top:50px;minwidth:100%;maxwidth:100%;"}
Expand Down

0 comments on commit 69b88a6

Please sign in to comment.