diff options
author | Pjotr Prins | 2021-01-01 08:09:40 +0000 |
---|---|---|
committer | Pjotr Prins | 2021-01-01 09:35:48 +0000 |
commit | fc28fdbbb5e121eb94bb06334a30e9b5395e003b (patch) | |
tree | 5e20454a8cd3b4a13c259da7722e0ce39ba01e8e /workflows/pull-data/genbank | |
parent | 42642e7b13ad79bde76b6fefad825675dad9c61e (diff) | |
download | bh20-seq-resource-fc28fdbbb5e121eb94bb06334a30e9b5395e003b.tar.gz bh20-seq-resource-fc28fdbbb5e121eb94bb06334a30e9b5395e003b.tar.lz bh20-seq-resource-fc28fdbbb5e121eb94bb06334a30e9b5395e003b.zip |
sparql: make use of pattern matching
Diffstat (limited to 'workflows/pull-data/genbank')
-rwxr-xr-x | workflows/pull-data/genbank/sparql-fetch-ids | 22 |
1 files changed, 10 insertions, 12 deletions
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: <http://www.w3.org/2000/01/rdf-schema#> @@ -20,24 +24,20 @@ PREFIX pubseq: <http://biohackathon.org/bh20-seq-schema#MainSchema/> # 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 |