Skip to content

Commit

Permalink
remaining pept endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
tibvdm committed Jun 17, 2024
1 parent 69658a3 commit 049408c
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 59 deletions.
2 changes: 0 additions & 2 deletions app/controllers/api/pept2funct_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] = {
Expand Down
1 change: 1 addition & 0 deletions app/controllers/api/pept2lca_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 2 additions & 5 deletions app/controllers/api/pept2prot_controller.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
39 changes: 4 additions & 35 deletions app/controllers/api/pept2taxa_controller.rb
Original file line number Diff line number Diff line change
@@ -1,49 +1,18 @@
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
# param[equate_il]: "true" or "false", Indicate if you want to equate I and L
# 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
5 changes: 1 addition & 4 deletions app/controllers/api/peptinfo_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] = {
Expand All @@ -32,7 +31,5 @@ def peptinfo
lca: lca_result[seq_index]
}
end

respond_with(@result)
end
end
2 changes: 0 additions & 2 deletions app/controllers/api/protinfo_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,5 @@ def protinfo
}
end
end

respond_with(@result)
end
end
23 changes: 23 additions & 0 deletions app/helpers/taxonomy_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
24 changes: 13 additions & 11 deletions app/views/api/pept2prot/pept2prot.json.jbuilder
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 049408c

Please sign in to comment.