Skip to content
This repository has been archived by the owner on Aug 19, 2023. It is now read-only.

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
* Removed lib/quorum/data_export.rb - replaced by job_serializer.rb
* Added Quorum::JobData object for data export.
* Added Quorum::JobQueueObserver and Service.
* Fixed rake task bug.
* Shortened job query urls.
* Disabled all observers in specs. Enable when needed.
* Created JobFetchData object to contain fetch blast sequence data.
* Moved send blast sequence to sequence module.
* Update README.rdoc
* Updated *.js and specs for refactoring.

Renamed routes.

* Renamed job controller route search_results => search.
* Added default format values for json results.
* Updated js ajax calls.

Added LSS information to comments in show view.

Print "Not enqueued" if algorithm wasn't enqueued.

Removed deprecated attributes.
  • Loading branch information
hunzinker authored and Ken Seal committed Nov 30, 2012
1 parent 4af4dcd commit 4839fcf
Show file tree
Hide file tree
Showing 33 changed files with 502 additions and 265 deletions.
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
--colour
--profile
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ source "http://rubygems.org"
gemspec

group :test do
gem 'rake', '~> 0.9.2'
gem 'rake', '~> 10.0.0'
end
2 changes: 1 addition & 1 deletion README.rdoc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
= Quorum {<img src="https://secure.travis-ci.org/ncgr/quorum.png?branch=data_export"alt="Build Status" />}[http://travis-ci.org/ncgr/quorum]
= Quorum {<img src="https://secure.travis-ci.org/ncgr/quorum.png?branch=data_export_refactor"alt="Build Status" />}[http://travis-ci.org/ncgr/quorum]

A flexible bioinformatics search tool.

Expand Down
8 changes: 4 additions & 4 deletions app/assets/javascripts/quorum/quorum.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ QUORUM.algorithms = ["blastn", "blastx", "tblastn", "blastp"];
QUORUM.pollResults = function(callback, callback_obj, interval, algos) {

var self = this,
url = document.URL + '/get_quorum_search_results.json',
url = document.URL + '/search',
interval = interval || 5000,
algos = algos || self.algorithms,
error = false,
Expand Down Expand Up @@ -97,7 +97,7 @@ QUORUM.pollResults = function(callback, callback_obj, interval, algos) {
QUORUM.viewDetailedReport = function(focus_id, query, algo) {

var self = this,
url = document.URL + '/get_quorum_search_results.json';
url = document.URL + '/search';

// Create the modal box.
$('#detailed_report_dialog').html(
Expand Down Expand Up @@ -327,7 +327,7 @@ QUORUM.displayHspLinks = function(focus, group, data) {
QUORUM.downloadSequence = function(algo_id, algo, el) {

var self = this,
url = document.URL + '/get_quorum_blast_hit_sequence.json';
url = document.URL + '/get_blast_hit_sequence';

$(el).html('Fetching sequence...');

Expand Down Expand Up @@ -357,7 +357,7 @@ QUORUM.downloadSequence = function(algo_id, algo, el) {
QUORUM.getSequenceFile = function(meta_id, el) {

var self = this,
url = document.URL + '/send_quorum_blast_hit_sequence?meta_id=' + meta_id,
url = document.URL + '/send_blast_hit_sequence?meta_id=' + meta_id,
timeoutId = 0;

function downloadFile(data) {
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/quorum/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Quorum
class ApplicationController < ActionController::Base
include Quorum::Helpers
include Quorum::DataExport
include Quorum::Sequence::SendSequence
end
end
142 changes: 51 additions & 91 deletions app/controllers/quorum/jobs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,16 @@ def index
end

def new
@job = Job.new
@job.build_blastn_job
@job.build_blastx_job
@job.build_tblastn_job
@job.build_blastp_job
build_blast_jobs
end

def create
if params[:job][:sequence_file]
file = params[:job][:sequence_file].read
params[:job].delete(:sequence_file)
end

read_sequence_file
@job = Job.new(params[:job])

if file
@job.sequence = ""
@job.sequence << file
end
set_sequence

unless @job.save
@job.build_blastn_job if @job.blastn_job.nil?
@job.build_blastx_job if @job.blastx_job.nil?
@job.build_tblastn_job if @job.tblastn_job.nil?
@job.build_blastp_job if @job.blastp_job.nil?
build_blast_jobs
render :action => "new"
return
end
Expand All @@ -55,76 +40,31 @@ def show
# This method should be used to gather Resque worker results, or user
# supplied query params.
#
def get_quorum_search_results
empty = [{ :results => false }]
not_enqueued = { :enqueued => false }.merge(empty[0])
data = []

# Allow for multiple algos and search params.
# Ex: /quorum/jobs/:id/get_quorum_search_results.json?algo=blastn,blastp
if params[:algo]
params[:algo].split(",").each do |a|
if Quorum::BLAST_ALGORITHMS.include?(a)
queued = "#{a}_job".to_sym
report = "#{a}_job_reports".to_sym

begin
job = Job.find(params[:id])
rescue ActiveRecord::RecordNotFound => e
logger.error e.message
else
if job.method(queued).call.present?
if job.method(report).call.present?
data << job.method(report).call.search(params).default_order
else
data = []
end
else
data << not_enqueued
end
end
end
end
else
data << empty
end

# Flatten the array of arrays if necessary.
data.flatten!(1)
def search
data = Job.search(params)

# Respond with :json, :txt (tab delimited Blast results), or GFF3.
respond_with data do |format|
respond_with data.flatten!(1) do |format|
format.json {
render :json => data
render :json => Quorum::JobSerializer.as_json(data)
}
format.gff {
render :text => to_gff(data)
render :text => Quorum::JobSerializer.as_gff(data)
}
format.txt {
render :text => to_txt(data)
render :text => Quorum::JobSerializer.as_txt(data)
}
end
end

#
# Find hit sequence, queue worker and return worker meta_id
# for lookup.
#
def get_quorum_blast_hit_sequence
if Quorum::BLAST_ALGORITHMS.include?(params[:algo])
begin
job = Job.find(params[:id])
rescue ActiveRecord::RecordNotFound => e
logger.error e.message
else
data = job.fetch_quorum_blast_sequence(
params[:algo], params[:algo_id]
)
json = [{ :meta_id => data.meta_id }]
end
end

respond_with json || []
# Find hit sequence and return worker meta_id for lookup. If the method
# returns [], try again.
#
def get_blast_hit_sequence
fetch_data = Job.set_blast_hit_sequence_lookup_values(params)
data = Quorum::JobQueueService.queue_fetch_worker(fetch_data)
respond_with data || []
end

#
Expand All @@ -133,25 +73,24 @@ def get_quorum_blast_hit_sequence
#
# See lib/generators/templates/blast_db.rb for more info.
#
def send_quorum_blast_hit_sequence
def send_blast_hit_sequence
data = Workers::System.get_meta(params[:meta_id])
if data.succeeded?
if data.result.downcase.include?("error")
render :text => data.result
return
else
send_data data.result,
:filename => "#{params[:meta_id]}.fa",
:type => "text/plain",
:disposition => "attachment"
return
end
end
render :text => ""
sequence(data)
end

private

#
# Create new Job and build associations.
#
def build_blast_jobs
@job ||= Job.new
@job.build_blastn_job if @job.blastn_job.nil?
@job.build_blastx_job if @job.blastx_job.nil?
@job.build_tblastn_job if @job.tblastn_job.nil?
@job.build_blastp_job if @job.blastp_job.nil?
end

#
# Blast Database options for select.
#
Expand All @@ -164,5 +103,26 @@ def set_blast_dbs
}
end

#
# Read and remove uploaded sequence file from params.
#
def read_sequence_file
if params[:job][:sequence_file]
@file = params[:job][:sequence_file].read
params[:job].delete(:sequence_file)
end
end

#
# Set uploaded sequence file.
#
def set_sequence
if @file
@job.sequence = ""
@job.sequence << @file
end
@file = nil
end

end
end
118 changes: 63 additions & 55 deletions app/models/quorum/job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ class Job < ActiveRecord::Base

include Quorum::Sequence

after_save :queue_workers

has_one :blastn_job, :dependent => :destroy
has_many :blastn_job_reports, :through => :blastn_job,
:dependent => :destroy
Expand Down Expand Up @@ -34,33 +32,72 @@ class Job < ActiveRecord::Base
validate :filter_input_sequences, :algorithm_selected, :sequence_size

#
# Fetch Blast hit_id, hit_display_id, queue Resque worker and
# return worker's meta_id.
# Return search results (Resque worker results).
#
def fetch_quorum_blast_sequence(algo, algo_id)
job = "#{algo}_job".to_sym
job_reports = "#{algo}_job_reports".to_sym

blast_dbs = self.method(job).call.blast_dbs

job_report = self.method(job_reports).call.where(
"quorum_#{algo}_job_reports.id = ?", algo_id
).first

hit_id = job_report.hit_id
hit_display_id = job_report.hit_display_id

cmd = Workers::System.create_blast_fetch_command(
blast_dbs, hit_id, hit_display_id, algo
)
def self.search(params)
data = JobData.new

# Allow for multiple algos and search params.
# Ex: /quorum/jobs/:id/search?algo=blastn,blastp
if params[:algo]
params[:algo].split(",").each do |a|
if Quorum::BLAST_ALGORITHMS.include?(a)
enqueued = "#{a}_job".to_sym
report = "#{a}_job_reports".to_sym
begin
job = Job.find(params[:id])
rescue ActiveRecord::RecordNotFound => e
logger.error e.message
else
if job.try(enqueued).present?
if job.try(report).present?
data.results << job.try(report).search(params).default_order
else
data = JobData.new
end
else
data.not_enqueued
end
end
end
end
else
data.no_results
end

data = Workers::System.enqueue(
cmd, Quorum.blast_remote,
Quorum.blast_ssh_host, Quorum.blast_ssh_user,
Quorum.blast_ssh_options, true
)
data.results
end

Workers::System.get_meta(data.meta_id)
#
# Fetch Blast hit_id, hit_display_id, queue Resque worker and
# return worker's meta_id.
#
def self.set_blast_hit_sequence_lookup_values(params)
fetch_data = JobFetchData.new
algo = params[:algo]
algo_id = params[:algo_id]

if Quorum::BLAST_ALGORITHMS.include?(algo)
fetch_data.algo = algo
begin
job = Job.find(params[:id])
rescue ActiveRecord::RecordNotFound => e
logger.error e.message
else
algo_job = "#{algo}_job".to_sym
algo_job_reports = "#{algo}_job_reports".to_sym

fetch_data.blast_dbs = job.try(algo_job).blast_dbs

job_report = job.try(algo_job_reports).where(
"quorum_#{algo}_job_reports.id = ?", algo_id
).first

fetch_data.hit_id = job_report.hit_id
fetch_data.hit_display_id = job_report.hit_display_id
end
end
fetch_data
end

#
Expand Down Expand Up @@ -164,34 +201,5 @@ def sequence_size
end
end

#
# Queue Resque workers.
#
def queue_workers
jobs = []
if self.blastn_job && self.blastn_job.queue
jobs << Workers::System.create_search_command("blastn", self.id)
end
if self.blastx_job && self.blastx_job.queue
jobs << Workers::System.create_search_command("blastx", self.id)
end
if self.tblastn_job && self.tblastn_job.queue
jobs << Workers::System.create_search_command("tblastn", self.id)
end
if self.blastp_job && self.blastp_job.queue
jobs << Workers::System.create_search_command("blastp", self.id)
end

unless jobs.blank?
jobs.each do |j|
Workers::System.enqueue(
j, Quorum.blast_remote,
Quorum.blast_ssh_host, Quorum.blast_ssh_user,
Quorum.blast_ssh_options
)
end
end
end

end
end
Loading

0 comments on commit 4839fcf

Please sign in to comment.