Class: HTTPClients::ENATaxaClient

Inherits:
BaseClient show all
Defined in:
lib/http_clients/ena_taxa_client.rb

Overview

Retrieves taxonomic information from the ENA taxonomy database.

Usage: “`rb client = HTTPClients::ENATaxaClient.new client.id_from_text('human') # returns 9606 client.name_from_id(9606) # returns 'homo sapiens' ““

API usage guide: ena-docs.readthedocs.io/en/latest/retrieval/programmatic-access/taxon-based-search.html Swagger docs: www.ebi.ac.uk/ena/taxonomy/rest/swagger-ui/index.html

Instance Method Summary collapse

Instance Method Details

#connObject



18
19
20
21
22
23
24
25
26
# File 'lib/http_clients/ena_taxa_client.rb', line 18

def conn
  @conn ||= Faraday.new(
    url: configatron.ena_taxon_lookup_url,
    headers: default_headers,
    proxy: proxy
  ) do |f|
    f.response :json
  end
end

#taxon_from_id(id) ⇒ Hash

Returns the taxon information for a given ENA taxon ID.

Parameters:

  • id (Integer, String)

    The ENA taxon ID (eg: 9606)

Returns:

  • (Hash)

    A hash with 'taxId', 'scientificName', and 'commonName'.



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/http_clients/ena_taxa_client.rb', line 52

def taxon_from_id(id)
  response = conn.get("tax-id/#{id}")
  results = response.body

  # extract taxId, scientificName, commonName from the results and return as a hash
  {
    'taxId' => results['taxId'],
    'scientificName' => results['scientificName'],
    'commonName' => results['commonName']
  }
end

#taxon_from_text(suggestion) ⇒ Hash?

Returns the ENA taxon information for a given organism suggestion string.

Parameters:

  • suggestion (String)

    The organism name or suggestion (e.g., 'human')

Returns:

  • (Hash, nil)

    A hash with 'taxId', 'scientificName', and 'commonName' if found, or nil if not found.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/http_clients/ena_taxa_client.rb', line 33

def taxon_from_text(suggestion)
  suggestion = ERB::Util.url_encode(suggestion)
  response = conn.get("any-name/#{suggestion}")
  first_taxon = response.body.first
  return unless first_taxon

  # extract relevant fields and return as a hash
  {
    'taxId' => first_taxon['taxId'],
    'scientificName' => first_taxon['scientificName'],
    'commonName' => first_taxon['commonName'],
    'submittable' => first_taxon['submittable']
  }
end