diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index ee70272..6156b15 100644 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -3,8 +3,8 @@ class Api::ApiController < ApplicationController respond_to :json - before_action :set_headers, only: %i[pept2taxa pept2lca pept2prot pept2funct pept2ec pept2go pept2interpro peptinfo taxa2lca taxonomy taxa2tree] - before_action :set_params, only: %i[pept2taxa pept2lca pept2prot pept2funct pept2ec pept2go pept2interpro peptinfo taxa2lca taxonomy taxa2tree] + before_action :set_headers, only: %i[pept2taxa pept2lca pept2prot pept2funct pept2ec pept2go pept2interpro peptinfo taxa2lca taxonomy taxa2tree protinfo] + before_action :set_params, only: %i[pept2taxa pept2lca pept2prot pept2funct pept2ec pept2go pept2interpro peptinfo taxa2lca taxonomy taxa2tree protinfo] before_action :set_query, only: %i[pept2taxa pept2lca peptinfo taxonomy] before_action :set_sequences, only: %i[pept2prot] @@ -270,6 +270,28 @@ def taxonomy respond_with(@result) end + # Returns the taxonomic and functional information for given uniprot id's + # param[input]: Array, required, List of input uniprot id's + def protinfo + @result = {} + + UniprotEntry + .includes(:taxon, :ec_numbers, :go_terms, :interpro_entries) + .where(uniprot_accession_number: @input_order) + .find_in_batches do |batch| + batch.each do |uniprot_id| + @result[uniprot_id.uniprot_accession_number] = { + taxon: uniprot_id.taxon, + ec: uniprot_id.ec_numbers.map { |ec| { ec_number: ec.code } }, + go: uniprot_id.go_terms.map { |go| { go_term: go.code } }, + ipr: uniprot_id.interpro_entries.map { |interpro| { code: interpro.code } } + } + end + end + + respond_with(@result) + end + private # log all api calls to stathat diff --git a/app/views/api/api/protinfo.json.jbuilder b/app/views/api/api/protinfo.json.jbuilder new file mode 100644 index 0000000..b4ad701 --- /dev/null +++ b/app/views/api/api/protinfo.json.jbuilder @@ -0,0 +1,13 @@ +json.array! @input_order do |uniprot_id| + if @result.key? uniprot_id + json.protein uniprot_id + + json.ec @result[uniprot_id][:ec] + json.go @result[uniprot_id][:go] + json.ipr @result[uniprot_id][:ipr] + + json.taxon_id @result[uniprot_id][:taxon][:id] + json.taxon_name @result[uniprot_id][:taxon][:name] + json.taxon_rank @result[uniprot_id][:taxon][:rank] + end +end diff --git a/config/routes.rb b/config/routes.rb index 314c552..ac09757 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,6 +33,7 @@ match 'peptinfo' => 'api#peptinfo', via: %i[get post] match 'taxonomy' => 'api#taxonomy', via: %i[get post] match 'messages' => 'api#messages', via: %i[get post] + match 'protinfo' => 'api#protinfo', via: %i[get post] end namespace :api, path: 'api/v2' do @@ -49,5 +50,6 @@ match 'peptinfo' => 'api#peptinfo', via: %i[get post] match 'taxonomy' => 'api#taxonomy', via: %i[get post] match 'messages' => 'api#messages', via: %i[get post] + match 'protinfo' => 'api#protinfo', via: %i[get post] end end