From 049408c79769d16d6a532be92a7a792aaa97d595 Mon Sep 17 00:00:00 2001 From: tibvdm Date: Mon, 17 Jun 2024 13:48:23 +0200 Subject: [PATCH] remaining pept endpoints --- app/controllers/api/pept2funct_controller.rb | 2 - app/controllers/api/pept2lca_controller.rb | 1 + app/controllers/api/pept2prot_controller.rb | 7 +--- app/controllers/api/pept2taxa_controller.rb | 39 ++----------------- app/controllers/api/peptinfo_controller.rb | 5 +-- app/controllers/api/protinfo_controller.rb | 2 - app/helpers/taxonomy_helper.rb | 23 +++++++++++ .../api/pept2prot/pept2prot.json.jbuilder | 24 ++++++------ 8 files changed, 44 insertions(+), 59 deletions(-) diff --git a/app/controllers/api/pept2funct_controller.rb b/app/controllers/api/pept2funct_controller.rb index 381a220..52cbe1d 100644 --- a/app/controllers/api/pept2funct_controller.rb +++ b/app/controllers/api/pept2funct_controller.rb @@ -18,8 +18,6 @@ def pept2funct interpro_result = pept2interpro_helper @input_order.each do |seq| - seq_index = @equate_il ? seq.tr('I', 'L') : seq - next unless go_result.key? seq_index @result[seq_index] = { diff --git a/app/controllers/api/pept2lca_controller.rb b/app/controllers/api/pept2lca_controller.rb index 6b4e8fc..b44a948 100644 --- a/app/controllers/api/pept2lca_controller.rb +++ b/app/controllers/api/pept2lca_controller.rb @@ -13,5 +13,6 @@ class Api::Pept2lcaController < Api::ApiController # param[names]: "true" or "false", Include the lineage names def pept2lca @result = pept2lca_helper + filter_input_order end end diff --git a/app/controllers/api/pept2prot_controller.rb b/app/controllers/api/pept2prot_controller.rb index 52702c7..b6fd765 100644 --- a/app/controllers/api/pept2prot_controller.rb +++ b/app/controllers/api/pept2prot_controller.rb @@ -1,7 +1,6 @@ class Api::Pept2protController < Api::ApiController before_action :set_cors_headers before_action :set_params - # before_action :set_sequences before_action :search_input # Returns a list of Uniprot entries containing a given tryptic peptide @@ -45,15 +44,13 @@ def pept2prot end end - @result = {} + @result = Hash.new { |h, k| h[k] = Set.new } @sequences.each do |sequence| sequence["uniprot_accession_numbers"].each do |uniprot_id| - @result[sequence["sequence"]] = uniprot_info[uniprot_id] + @result[sequence["sequence"]] << uniprot_info[uniprot_id] end end filter_input_order - - respond_with(@result) end end diff --git a/app/controllers/api/pept2taxa_controller.rb b/app/controllers/api/pept2taxa_controller.rb index b7c0047..51bbca0 100644 --- a/app/controllers/api/pept2taxa_controller.rb +++ b/app/controllers/api/pept2taxa_controller.rb @@ -1,7 +1,10 @@ class Api::Pept2taxaController < Api::ApiController + include TaxonomyHelper + before_action :set_cors_headers before_action :set_params before_action :set_query + before_action :search_input # Returns a list of taxa retrieved from the Uniprot entries containing a given tryptic peptide # param[input]: Array, required, List of input peptides @@ -9,41 +12,7 @@ class Api::Pept2taxaController < Api::ApiController # param[extra]: "true" or "false", Include lineage # param[names]: "true" or "false", Include the lineage names def pept2taxa - @result = {} - lookup = Hash.new { |h, k| h[k] = Set.new } - ids = Set.new - - seqid2seq = {} - Sequence.where(sequence: @input).select(:id, :sequence).each do |seq| - seqid2seq[seq[:id]] = seq[:sequence] - @result[seq[:sequence]] = Set.new - end - - rel_name = @equate_il ? :sequence_id : :original_sequence_id - Peptide.where(rel_name => seqid2seq.keys).select(:id, rel_name, :uniprot_entry_id).find_in_batches do |items| - uniprot2seqids = Hash.new { |h, k| h[k] = [] } - items.each { |i| uniprot2seqids[i[:uniprot_entry_id]] << i[rel_name] } - - UniprotEntry.where(id: uniprot2seqids.keys).select(:id, :taxon_id).each do |entry| - uniprot2seqids[entry[:id]].each do |seqid| - sequence = seqid2seq[seqid] - lookup[entry[:taxon_id]] << sequence - ids << entry[:taxon_id] - end - end - end - - ids.delete nil - ids = ids.to_a.sort - - @query.where(id: ids).find_in_batches do |group| - group.each do |t| - lookup[t.id].each { |s| @result[s] << t } - end - end - + @result = pept2taxa_helper filter_input_order - - respond_with(@result) end end diff --git a/app/controllers/api/peptinfo_controller.rb b/app/controllers/api/peptinfo_controller.rb index 7530b9e..303f44e 100644 --- a/app/controllers/api/peptinfo_controller.rb +++ b/app/controllers/api/peptinfo_controller.rb @@ -5,6 +5,7 @@ class Api::PeptinfoController < Api::ApiController before_action :set_cors_headers before_action :set_params before_action :set_query + before_action :search_input # Returns both the lca, ec and go information for a given tryptic peptide # param[input]: Array, required, List of input peptides @@ -20,8 +21,6 @@ def peptinfo interpro_result = pept2interpro_helper @input_order.each do |seq| - seq_index = @equate_il ? seq.tr('I', 'L') : seq - next unless go_result.key? seq_index @result[seq_index] = { @@ -32,7 +31,5 @@ def peptinfo lca: lca_result[seq_index] } end - - respond_with(@result) end end diff --git a/app/controllers/api/protinfo_controller.rb b/app/controllers/api/protinfo_controller.rb index f444a26..4ed58a6 100644 --- a/app/controllers/api/protinfo_controller.rb +++ b/app/controllers/api/protinfo_controller.rb @@ -20,7 +20,5 @@ def protinfo } end end - - respond_with(@result) end end diff --git a/app/helpers/taxonomy_helper.rb b/app/helpers/taxonomy_helper.rb index d14d3c0..8802ccf 100644 --- a/app/helpers/taxonomy_helper.rb +++ b/app/helpers/taxonomy_helper.rb @@ -21,4 +21,27 @@ def pept2lca_helper output end + + def pept2taxa_helper + output = Hash.new { |h, k| h[k] = Set.new } + lookup = Hash.new { |h, k| h[k] = Set.new } + ids = [] + + @sequences.each do |seq| + seq["taxa"].each do |taxon| + ids.append taxon + lookup[taxon] << seq["sequence"] + end + end + + ids = ids.uniq.compact.sort + + @query.where(id: ids).find_in_batches do |group| + group.each do |t| + lookup[t.id].each { |s| output[s] << t } + end + end + + output + end end diff --git a/app/views/api/pept2prot/pept2prot.json.jbuilder b/app/views/api/pept2prot/pept2prot.json.jbuilder index 4233118..3091e00 100644 --- a/app/views/api/pept2prot/pept2prot.json.jbuilder +++ b/app/views/api/pept2prot/pept2prot.json.jbuilder @@ -1,13 +1,15 @@ -json.array! @input_order do |peptide| - json.peptide peptide - json.uniprot_id @result[peptide][:uniprot_id] - json.protein_name @result[peptide][:name] - json.taxon_id @result[peptide][:taxon_id] - json.protein @result[peptide][:protein] - if @extra_info - json.taxon_name @result[peptide][:taxon][:name] - json.ec_references @result[peptide][:ec_cross_references] - json.go_references @result[peptide][:go_cross_references] - json.interpro_references @result[peptide][:interpro_cross_references] +@input_order.map do |peptide| + json.array! @result[peptide].map do |prot| + json.peptide peptide + json.uniprot_id prot[:uniprot_id] + json.protein_name prot[:name] + json.taxon_id prot[:taxon_id] + json.protein prot[:protein] + if @extra_info + json.taxon_name prot[:taxon][:name] + json.ec_references prot[:ec_cross_references] + json.go_references prot[:go_cross_references] + json.interpro_references prot[:interpro_cross_references] + end end end