Skip to content

Commit

Permalink
Fail gracefully if BLAST+ database(s) is corrupt or has incorrect per…
Browse files Browse the repository at this point in the history
…mission.

see #145

Signed-off-by: Anurag Priyam <anurag08priyam@gmail.com>
  • Loading branch information
yeban committed Mar 10, 2015
1 parent 5120389 commit 4f2b2da
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
3 changes: 2 additions & 1 deletion bin/sequenceserver
Expand Up @@ -137,7 +137,8 @@ BANNER
# error scenarios either when creating a new SequenceServer (first
# time or later), or updating config values using -s CLI option.

rescue SequenceServer::CONFIG_FILE_ERROR => e
rescue SequenceServer::CONFIG_FILE_ERROR,
SequenceServer::BLAST_DATABASE_ERROR => e

puts e
exit!
Expand Down
11 changes: 0 additions & 11 deletions lib/sequenceserver.rb
Expand Up @@ -128,9 +128,7 @@ def init_database
fail DATABASE_DIR_NOT_FOUND, config[:database_dir]
end

assert_blast_databases_present_in_database_dir
logger.debug("Will use BLAST+ databases at: #{config[:database_dir]}")

Database.scan_databases_dir
Database.each do |database|
logger.debug("Found #{database.type} database '#{database.title}'" \
Expand Down Expand Up @@ -187,15 +185,6 @@ def assert_blast_installed_and_compatible
fail BLAST_NOT_COMPATIBLE, version unless version >= MINIMUM_BLAST_VERSION
end

def assert_blast_databases_present_in_database_dir
cmd = "blastdbcmd -recursive -list #{config[:database_dir]}"
out = `#{cmd}`
errpat = /BLAST Database error/
fail NO_BLAST_DATABASE_FOUND, config[:database_dir] if out.empty?
fail BLAST_DATABASE_ERROR, cmd, out if out.match(errpat) ||
!$CHILD_STATUS.success?
end

def open_default_browser(url)
return if using_ssh? || verbose?
if RUBY_PLATFORM =~ /linux/ && xdg?
Expand Down
27 changes: 19 additions & 8 deletions lib/sequenceserver/database.rb
@@ -1,4 +1,5 @@
require 'find'
require 'open3'
require 'digest/md5'
require 'forwardable'

Expand Down Expand Up @@ -96,17 +97,27 @@ def clear

# Recurisvely scan `database_dir` for blast databases.
def scan_databases_dir
database_dir = config[:database_dir]
cmd = "blastdbcmd -recursive -list #{database_dir}" \
' -list_outfmt "%f %t %p %n %l %d" 2>&1'
list = `#{cmd}`
list.each_line do |line|
name = line.split(' ')[0]
next if multipart_database_name?(name)
self << Database.new(*line.split(' '))
cmd = "blastdbcmd -recursive -list #{config[:database_dir]}" \
' -list_outfmt "%f %t %p %n %l %d"'
Open3.popen3(cmd) do |_, out, err|
out = out.read
err = err.read
throw_scan_error(cmd, out, err, $CHILD_STATUS)
out.each_line do |line|
name = line.split(' ')[0]
next if multipart_database_name?(name)
self << Database.new(*line.split(' '))
end
end
end

def throw_scan_error(cmd, out, err, child_status)
errpat = /BLAST Database error/
fail NO_BLAST_DATABASE_FOUND, config[:database_dir] if out.empty?
fail BLAST_DATABASE_ERROR.new(cmd, err) if !child_status.success? ||
err.match(errpat)
end

# Recursively scan `database_dir` for un-formatted FASTA and format them
# for use with BLAST+.
def make_blast_databases
Expand Down
2 changes: 1 addition & 1 deletion lib/sequenceserver/exceptions.rb
Expand Up @@ -140,7 +140,7 @@ def to_s
end

# Raised if there was an error determining BLAST+ databases in database_dir.
class BLAST_DATABASE_ERROR
class BLAST_DATABASE_ERROR < StandardError
def initialize(cmd, out)
@cmd = cmd
@out = out
Expand Down
4 changes: 3 additions & 1 deletion spec/database_spec.rb
Expand Up @@ -91,7 +91,9 @@ module SequenceServer

it 'can tell FASTA files that are yet to be made into a BLAST+ database' do
SequenceServer.config[:database_dir] = database_dir_unformatted
Database.scan_databases_dir
# rubocop:disable Style/RescueModifier
Database.scan_databases_dir rescue NO_BLAST_DATABASE_FOUND
# rubocop:enable Style/RescueModifier
Database.unformatted_fastas.should_not be_empty
end

Expand Down

0 comments on commit 4f2b2da

Please sign in to comment.