Skip to content

Commit

Permalink
pept2prot endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
tibvdm committed Jun 10, 2024
1 parent 7cd3500 commit 69658a3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 55 deletions.
59 changes: 39 additions & 20 deletions app/controllers/api/pept2prot_controller.rb
Original file line number Diff line number Diff line change
@@ -1,35 +1,54 @@
class Api::Pept2protController < Api::ApiController
before_action :set_cors_headers
before_action :set_params
before_action :set_sequences
# before_action :set_sequences
before_action :search_input

# Returns a list of Uniprot entries containing a given tryptic peptide
# params[:input]: Array, required, List of input peptides
# params[:equate_il]: "true" or "false" (default), optional, Equate I and L?
# params[:extra]: "true" or "false" (default), optional, Output extra info?
def pept2prot
lookup = Hash.new { |h, k| h[k] = Set.new }
if @extra_info
@result = {}
# Perform joins and load objects (expensive)
ids = []
@sequences.pluck(:sequence, 'uniprot_entries.id').each do |sequence, uniprot_id|
ids.append uniprot_id
lookup[uniprot_id] << sequence
@result[sequence] = Set.new
end
uniprot_accession_numbers = @sequences.map do |sequence|
sequence["uniprot_accession_numbers"]
end.flatten.uniq

ids = ids.uniq.compact.sort
UniprotEntry.includes(:taxon, :ec_cross_references, :go_cross_references, :interpro_cross_references)
.where(id: ids).find_in_batches do |group|
group.each do |uni|
lookup[uni.id].each { |s| @result[s] << uni }
uniprot_info = {}

if @extra_info
UniprotEntry
.includes(:taxon, :ec_cross_references, :go_cross_references, :interpro_cross_references)
.where(uniprot_accession_number: uniprot_accession_numbers)
.each do |item|
uniprot_info[item.uniprot_accession_number] = {
uniprot_id: item.uniprot_accession_number,
name: item.name,
taxon_id: item.taxon_id,
protein: item.protein,
taxon: item.taxon,
ec_cross_references: item.ec_cross_references.map(&:ec_number_code).join(" "),
go_cross_references: item.go_cross_references.map(&:go_term_code).join(" "),
interpro_cross_references: item.interpro_cross_references.map(&:interpro_entry_code).join(" ")
}
end
end
else
@result = Hash.new { |h, k| h[k] = Set.new }
@sequences.pluck(:sequence, 'uniprot_entries.uniprot_accession_number', 'uniprot_entries.name', 'uniprot_entries.taxon_id', 'uniprot_entries.protein').each do |sequence, uniprot_id, protein_name, taxon_id, protein|
@result[sequence] << [uniprot_id, protein_name, taxon_id, protein]
UniprotEntry
.where(uniprot_accession_number: uniprot_accession_numbers)
.pluck(:uniprot_accession_number, :name, :taxon_id, :protein)
.each do |item|
uniprot_info[item[0]] = {
uniprot_id: item[0],
name: item[1],
taxon_id: item[2],
protein: item[3]
}
end
end

@result = {}
@sequences.each do |sequence|
sequence["uniprot_accession_numbers"].each do |uniprot_id|
@result[sequence["sequence"]] = uniprot_info[uniprot_id]
end
end

Expand Down
35 changes: 0 additions & 35 deletions app/views/api/pept2prot/pept2prot.json.erb

This file was deleted.

13 changes: 13 additions & 0 deletions app/views/api/pept2prot/pept2prot.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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]
end
end

0 comments on commit 69658a3

Please sign in to comment.