From fc28fdbbb5e121eb94bb06334a30e9b5395e003b Mon Sep 17 00:00:00 2001 From: Pjotr Prins Date: Fri, 1 Jan 2021 08:09:40 +0000 Subject: sparql: make use of pattern matching --- workflows/pull-data/genbank/sparql-fetch-ids | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'workflows') diff --git a/workflows/pull-data/genbank/sparql-fetch-ids b/workflows/pull-data/genbank/sparql-fetch-ids index d4743bd..19b2d82 100755 --- a/workflows/pull-data/genbank/sparql-fetch-ids +++ b/workflows/pull-data/genbank/sparql-fetch-ids @@ -4,11 +4,15 @@ # # sparql-fetch-ids > pubseq_ids.txt # +# Note: requires Ruby 3.x. Older Ruby gives a syntax error require 'net/http' require 'json' require 'ostruct' require 'erb' +require 'pp' + +MAX=5_000 SPARQL_HEADER=" prefix rdfs: @@ -20,24 +24,20 @@ PREFIX pubseq: # Build a SPARQL query, submit and return results. Apply transform # lambda when passed in - -def sparql q, transform = nil - q = SPARQL_HEADER+q - api_url = "http://sparql.genenetwork.org/sparql/?default-graph-uri=&format=application%2Fsparql-results%2Bjson&timeout=0&debug=on&run=+Run+Query+&query=#{ERB::Util.url_encode(q)}" +def sparql query, transform = nil + api_url = "http://sparql.genenetwork.org/sparql/?default-graph-uri=&format=application%2Fsparql-results%2Bjson&timeout=0&debug=on&run=+Run+Query+&query=#{ERB::Util.url_encode(SPARQL_HEADER + query)}" response = Net::HTTP.get_response(URI.parse(api_url)) - data = JSON.parse(response.body) - vars = data['head']['vars'] - results = data['results']['bindings'] + data = JSON.parse(response.body,symbolize_names: true) + data => { head: { vars: }, results: { bindings: results} } + vars = vars.map { |v| v.to_sym } results.map { |rec| # return results after transforming to a Hash and applying the # optional transform lambda. Note the transform can not only # reduce results, or create an array, but also may transform into # an OpenStruct. res = {} - vars.each { |name| - res[name.to_sym] = rec[name]['value'] - } + vars.each { |name| res[name] = rec[name][:value] } if transform transform.call(res) else @@ -46,8 +46,6 @@ def sparql q, transform = nil } end -MAX=5_000 - start = 0 num = MAX begin -- cgit v1.2.3