Skip to content

Commit

Permalink
temporary try pept2taxa
Browse files Browse the repository at this point in the history
  • Loading branch information
pverscha committed Mar 12, 2024
1 parent 228ec9a commit 5a1e9ec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
58 changes: 36 additions & 22 deletions app/controllers/api/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,38 +61,52 @@ def pept2prot
# 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
# TODO: right now, the parameter "extra" is not yet supported in this branch
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
# Convert the peptides array into a JSON string
json_data = {peptides: peptides}.to_json

# The URL to which the request will be sent
uri = URI.parse("http://localhost:3000/search")

# Create a POST request
request = Net::HTTP::Post.new(uri)
request.content_type = "application/json"
request.body = json_data

# Set up the HTTP session
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(request)
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] }
# Parse the response body as JSON
response_data = JSON.parse(response.body)

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
all_taxa = set()

response_data["result"].each do |item|
all_taxa.merge(item.taxa)
end

ids.delete nil
ids = ids.to_a.sort
# Now, retrieve all information associated with the taxa
taxon_information = Taxon.find(all_taxa)

@query.where(id: ids).find_in_batches do |group|
group.each do |t|
lookup[t.id].each { |s| @result[s] << t }
taxon_id_to_obj = Hash.new

taxon_information.each do |taxon|
taxon_id_to_obj[taxon.id] = taxon
end

response_data["result"].each do |item|
taxon_objs = []
for taxon_id in item.taxa
unless taxon_information.key? taxon_id
taxon_objs.append(taxon_id_to_obj[taxon_information])
end
end
@result[item["sequence"]] = taxon_objs
end

filter_input_order
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/mpa_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ def pept2data
request.content_type = "application/json"
request.body = json_data

puts json_data

# Set up the HTTP session
response = Net::HTTP.start(uri.hostname, uri.port) do |http|
http.request(request)
Expand Down

0 comments on commit 5a1e9ec

Please sign in to comment.