Skip to content

Commit

Permalink
Merge pull request #20 from unipept/feature/protinfo-endpoint
Browse files Browse the repository at this point in the history
Fetch taxonomic and functional information for UniProt id's
  • Loading branch information
pverscha authored Jun 14, 2023
2 parents 341f974 + 03c0762 commit 2c663f9
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
26 changes: 24 additions & 2 deletions app/controllers/api/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions app/views/api/api/protinfo.json.jbuilder
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 2c663f9

Please sign in to comment.