Skip to content

Instantly share code, notes, and snippets.

@epaule
Last active November 23, 2023 13:21
Show Gist options
  • Save epaule/338be9381de1eb01c771eabd90b38ce1 to your computer and use it in GitHub Desktop.
Save epaule/338be9381de1eb01c771eabd90b38ce1 to your computer and use it in GitHub Desktop.
get taxonomic lineage from ENA for a species name
#!/usr/bin/env crystal
require "http/client"
require "json"
species = ARGV[0]
species = species.gsub(/\s/, "%20")
r = HTTP::Client.get("https://www.ebi.ac.uk/ena/taxonomy/rest/scientific-name/#{species}", headers: HTTP::Headers{"Accept" => "application/json"})
raise "cannot get the taxonomy" unless r.success?
json = JSON.parse(r.body)
if json.as_a.size < 1
r = HTTP::Client.get("https://www.ebi.ac.uk/ena/taxonomy/rest/any-name/#{species}", headers: HTTP::Headers{"Accept" => "application/json"})
raise "cannot get the taxonomy" unless r.success?
json = JSON.parse(r.body)
end
raise "cannot get the taxonomy" if json.as_a.size < 1
puts "#{json[0]["lineage"]}#{ARGV[0]}"
#!/nfs/users/nfs_m/mh6/.linuxbrew/homebrew/bin/ruby
require "net/http"
require "uri"
require "json"
require "cgi"
species = ARGV[0]
species = CGI.escapeURIComponent(species)
uri = URI("https://www.ebi.ac.uk/ena/taxonomy/rest/scientific-name/#{species}")
r = Net::HTTP.get_response(uri, {"Accept" => "application/json"})
raise "cannot get the taxonomy" unless r.is_a?(Net::HTTPSuccess)
json = JSON.parse(r.body)
if json.size < 1
uri =URI("https://www.ebi.ac.uk/ena/taxonomy/rest/any-name/#{species}")
r = Net::HTTP.get_response(uri,{"Accept" => "application/json"})
raise "cannot get the taxonomy" unless r.is_a?(Net::HTTPSuccess)
json = JSON.parse(r.body)
end
raise "cannot get the taxonomy" if json.size < 1
print "#{json[0]["lineage"]}#{ARGV[0]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment