Skip to content

Commit

Permalink
Merge pull request #5 from unipept/feature/pept2filtered
Browse files Browse the repository at this point in the history
Add new "pept2filtered" endpoint to the Unipept API
  • Loading branch information
pverscha authored Nov 16, 2022
2 parents d397f3e + 3c6fd4e commit 87e10f2
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
28 changes: 28 additions & 0 deletions app/controllers/mpa_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,34 @@ def pept2data
end
end

def pept2filtered
peptides = params[:peptides] || []
# missed = params[:missed] || false
taxa_filter_ids = (params[:taxa] || []).map(&:to_i)

@equate_il = params[:equate_il].nil? ? true : params[:equate_il]

@sequences = Sequence
.joins(peptides: [:uniprot_entry])
.includes(peptides: [:uniprot_entry])
.where(sequence: peptides)
.where(uniprot_entry: { taxon_id: taxa_filter_ids })
.uniq

uniprot_ids = @sequences.map { |s| s.peptides.map(&:uniprot_entry_id) }.flatten.uniq

@go_terms = GoCrossReference
.where(uniprot_entry_id: uniprot_ids)

@ec_numbers = EcCrossReference
.where(uniprot_entry_id: uniprot_ids)

@ipr_entries = InterproCrossReference
.where(uniprot_entry_id: uniprot_ids)

@seq_entries = @sequences.map { |s| [s, s.peptides.map(&:uniprot_entry).select { |e| taxa_filter_ids.include? e.taxon_id }] }
end

private

def default_format_json
Expand Down
5 changes: 5 additions & 0 deletions app/models/lineage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ def to_a
array.map { |x| x.nil? ? '' : x.name }
end

def to_a_idx
array = ORDER_T.map { |rank| send(rank) }
array.map { |x| x.nil? ? nil : x.id }
end

def self.ranks
ORDER
end
Expand Down
9 changes: 9 additions & 0 deletions app/views/mpa/pept2filtered.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
json.peptides @seq_entries do |seq_entry|
json.sequence seq_entry[0].sequence
json.taxa seq_entry[1].map(&:taxon_id).uniq
json.fa do
json.go_terms @go_terms.map(&:go_term_code).uniq
json.ec_numbers(@ec_numbers.map(&:ec_number_code).reject(&:empty?).uniq.map { |ec| "EC:#{ec}" })
json.interpro_entries(@ipr_entries.map(&:interpro_entry_code).uniq.map { |ipr| ipr.sub('IPR', 'IPR:') })
end
end
20 changes: 11 additions & 9 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@
match "metadata", via: [:get, :post], :to => "private_api#metadata"
end

scope :mpa, as: 'mpa' do
match '/*path', via: [:options], to: 'handle_options#handle_options_request'
match 'pept2data', via: %i[get post], to: 'mpa#pept2data'
match 'pept2filtered', via: %i[get post], to: 'mpa#pept2filtered'
end

scope :datasets, as: 'datasets' do
match 'sampledata', via: [:post], to: 'datasets#sampledata'
end

namespace :api, path: 'api/v1' do
match 'pept2taxa' => "api#pept2taxa", via: %i[get post]
match 'pept2lca' => "api#pept2lca", via: %i[get post]
Expand All @@ -34,18 +44,10 @@
match 'pept2ec' => 'api#pept2ec', via: %i[get post]
match 'pept2go' => 'api#pept2go', via: %i[get post]
match 'pept2interpro' => 'api#pept2interpro', via: %i[get post]
match 'pept2gm' => 'api#pept2gm', via: %i[get post]
match 'taxa2tree' => 'api#taxa2tree', via: %i[get post]
match 'peptinfo' => 'api#peptinfo', via: %i[get post]
match 'taxonomy' => 'api#taxonomy', via: %i[get post]
match 'messages' => 'api#messages', via: %i[get post]
end

scope :mpa, as: 'mpa' do
match '/*path', via: [:options], to: 'handle_options#handle_options_request'
match 'pept2data', via: %i[get post], to: 'mpa#pept2data'
end

scope :datasets, as: 'datasets' do
match 'sampledata', via: [:post], to: 'datasets#sampledata'
end
end

0 comments on commit 87e10f2

Please sign in to comment.