lib/genevalidator/arg_validation.rb in genevalidator-1.6.1 vs lib/genevalidator/arg_validation.rb in genevalidator-1.6.2
- old
+ new
@@ -1,30 +1,43 @@
+require 'forwardable'
+
+require 'genevalidator/blast'
+
# A module to validate the command line Arguments
## CREDIT: some of these methods have been adapted from SequenceServer
module GeneValidator
# TODO: If a tabular file is provided, ensure that a tabular file has the
# right number of columns
# TODO: assert_if_ruby_version_is_supported
- # A module to validate the arguments passed to the Validation Class
- module GVArgValidation
+ # A class to validate the arguments passed to the Validation Class
+ class GVArgValidation
class << self
- def validate_args(opt)
+ extend Forwardable
+ def_delegators GeneValidator, :opt
+
+ def validate_args
@opt = opt
assert_output_dir_does_not_exist
assert_file_present('input file', opt[:input_fasta_file])
assert_input_file_probably_fasta
- assert_input_contains_single_type_sequence
+ assert_input_sequence
assert_BLAST_output_files
assert_validations_arg
check_num_threads
+ export_bin_dirs unless @opt[:bin].nil?
+
Blast.validate(opt) unless @opt[:test]
- Mafft.assert_mafft_installation(opt)
- @opt
+ assert_mafft_installation
end
+ # Return `true` if the given command exists and is executable.
+ def command?(command)
+ system("which #{command} > /dev/null 2>&1")
+ end
+
private
def assert_validations_arg
validations = %w(lenc lenr frame merge dup orf align)
if @opt[:validations]
@@ -35,14 +48,14 @@
end
def check_num_threads
@opt[:num_threads] = Integer(@opt[:num_threads])
unless @opt[:num_threads] > 0
- puts 'Number of threads can not be lower than 0'
+ $stderr.puts 'Number of threads can not be lower than 0'
end
return unless @opt[:num_threads] > 256
- puts "Number of threads set at #{@opt[:num_threads]} is unusually high."
+ $stderr.puts "Number of threads set at #{@opt[:num_threads]} is unusually high."
end
def assert_BLAST_output_files
return unless @opt[:blast_xml_file] || @opt[:blast_tabular_file]
if @opt[:blast_xml_file]
@@ -54,22 +67,22 @@
end
def assert_output_dir_does_not_exist
output_dir = "#{@opt[:input_fasta_file]}.html"
return unless File.exist?(output_dir)
- puts "The output directory already exists for this fasta file.\n"
- puts "Please remove the following directory: #{output_dir}\n"
- puts "You can run the following command to remove the folder.\n"
- puts "\n $ rm -r #{output_dir} \n"
+ $stderr.puts "The output directory already exists for this fasta file.\n"
+ $stderr.puts "Please remove the following directory: #{output_dir}\n"
+ $stderr.puts "You can run the following command to remove the folder.\n"
+ $stderr.puts "\n $ rm -r #{output_dir} \n"
exit 1
end
def assert_tabular_options_exists
return if @opt[:blast_tabular_options]
- puts '*** Error: BLAST tabular options (-o) have not been set.'
- puts ' Please set the "-o" option with the custom format'
- puts ' used in the BLAST -outfmt argument'
+ $stderr.puts '*** Error: BLAST tabular options (-o) have not been set.'
+ $stderr.puts ' Please set the "-o" option with the custom format'
+ $stderr.puts ' used in the BLAST -outfmt argument'
exit 1
end
def assert_input_file_probably_fasta
File.open(@opt[:input_fasta_file], 'r') do |file_stream|
@@ -77,24 +90,49 @@
end
end
def assert_file_present(desc, file, exit_code = 1)
return if file && File.exist?(File.expand_path(file))
- puts "*** Error: Couldn't find the #{desc}: #{file}."
+ $stderr.puts "*** Error: Couldn't find the #{desc}: #{file}."
exit exit_code
end
alias_method :assert_dir_present, :assert_file_present
- def assert_input_contains_single_type_sequence
+ def assert_input_sequence
fasta_content = IO.binread(@opt[:input_fasta_file])
type = BlastUtils.type_of_sequences(fasta_content)
return if type == :nucleotide || type == :protein
- puts '*** Error: The input files does not contain just protein or'
- puts ' nucleotide data. Please correct this and try again.'
+ $stderr.puts '*** Error: The input files does not contain just protein or'
+ $stderr.puts ' nucleotide data. Please correct this and try again.'
exit 1
end
+
+ def export_bin_dirs
+ @opt[:bin].each do |bin|
+ if File.directory?(bin)
+ add_to_path(bin)
+ else
+ $stderr.puts '*** The following bin directory does not exist:'
+ $stderr.puts " #{bin}"
+ end
+ end
+ end
+
+ ## Checks if dir is in $PATH and if not, it adds the dir to the $PATH.
+ def add_to_path(bin_dir)
+ return if ENV['PATH'].split(':').include?(bin_dir)
+ ENV['PATH'] = "#{bin_dir}:#{ENV['PATH']}"
+ end
+
+ def assert_mafft_installation
+ return if command?('mafft')
+ $stderr.puts '*** Could not find Mafft binaries.'
+ $stderr.puts ' Ignoring error and continuing - Please note that' \
+ ' some validations may be skipped.'
+ $stderr.puts # a blank line
+ end
end
# Validates BLAST Installation (And BLAST databases)
class Blast
class << self
@@ -104,121 +142,54 @@
EXIT_BLAST_NOT_INSTALLED = 2
EXIT_BLAST_NOT_COMPATIBLE = 3
EXIT_NO_BLAST_DATABASE = 4
def validate(opt)
- @opt = opt
assert_blast_installation
- assert_blast_database_provided
- assert_local_blast_database_exists if @opt[:db] !~ /remote/
+ warn_if_remote_database(opt[:db])
+ assert_local_blast_database_exists(opt[:db]) if opt[:db] !~ /remote/
end
def assert_blast_installation
# Validate BLAST installation
- if @opt[:blast_bin].nil?
- assert_blast_installed
- assert_blast_compatible
- else
- export_bin_dir
- end
+ assert_blast_installed
+ assert_blast_compatible
end
- def assert_blast_database_provided
- return unless @opt[:db].nil?
- puts '*** Error: A BLAST database is required. Please pass a local or'
- puts ' remote BLAST database to GeneValidator as follows:'
- puts # a blank line
- puts " $ genevalidator -d '~/blastdb/SwissProt' Input_File"
- puts # a blank line
- puts ' Or use a remote database:'
- puts # a blank line
- puts " $ genevalidator -d 'swissprot -remote' Input_File"
- exit 1
+ def warn_if_remote_database(db)
+ return if db !~ /remote/
+ $stderr.puts # a blank line
+ $stderr.puts 'Warning: BLAST will be carried out on remote servers.'
+ $stderr.puts 'This may take quite a bit of time.'
+ $stderr.puts 'You may want to install a local BLAST database for' \
+ ' faster analyses.'
+ $stderr.puts # a blank line
end
- def assert_local_blast_database_exists
- return if system("blastdbcmd -db #{@opt[:db]} -info > /dev/null 2>&1")
- puts '*** No BLAST database found at the provided path.'
- puts ' Please ensure that the provided path is correct and then' \
- ' try again.'
+ def assert_local_blast_database_exists(db)
+ return if system("blastdbcmd -db #{db} -info > /dev/null 2>&1")
+ $stderr.puts '*** No BLAST database found at the provided path.'
+ $stderr.puts ' Please ensure that the provided path is correct' \
+ ' and then try again.'
exit EXIT_NO_BLAST_DATABASE
end
private
def assert_blast_installed
return if GVArgValidation.command?('blastdbcmd')
- puts '*** Could not find BLAST+ binaries.'
+ $stderr.puts '*** Could not find BLAST+ binaries.'
exit EXIT_BLAST_NOT_INSTALLED
end
def assert_blast_compatible
version = `blastdbcmd -version`.split[1]
return if version >= MINIMUM_BLAST_VERSION
- puts "*** Your BLAST+ version #{version} is outdated."
- puts ' GeneValidator needs NCBI BLAST+ version' \
- " #{MINIMUM_BLAST_VERSION} or higher."
+ $stderr.puts "*** Your BLAST+ version #{version} is outdated."
+ $stderr.puts ' GeneValidator needs NCBI BLAST+ version' \
+ " #{MINIMUM_BLAST_VERSION} or higher."
exit EXIT_BLAST_NOT_COMPATIBLE
end
-
- def export_bin_dir
- if File.directory?(@opt[:blast_bin])
- GVArgValidation.add_to_path(@opt[:blast_bin])
- else
- puts '*** The provided BLAST bin directory does not exist.'
- puts ' Please ensure that the provided BLAST bin directory is' \
- ' correct and try again.'
- exit EXIT_BLAST_NOT_INSTALLED
- end
- end
- end
- end
-
- # Validates Mafft installation
- class Mafft
- class << self
- def assert_mafft_installation(opt)
- @opt = opt
- if @opt[:mafft_bin].nil?
- assert_mafft_installed
- else
- export_bin_dir
- end
- end
-
- private
-
- def assert_mafft_installed
- return if GVArgValidation.command?('mafft')
- puts '*** Could not find Mafft binaries.'
- puts ' Ignoring error and continuing - Please note that some' \
- ' validations may be skipped.'
- puts # a blank line
- end
-
- def export_bin_dir
- if File.directory?(@opt[:mafft_bin])
- GVArgValidation.add_to_path(@opt[:mafft_bin])
- else
- puts '*** The provided Mafft bin directory does not exist.'
- puts ' Ignoring error and continuing - Please note that some' \
- ' validations may be skipped.'
- puts # a blank line
- end
- end
- end
- end
-
- class << self
- ## Checks if dir is in $PATH and if not, it adds the dir to the $PATH.
- def add_to_path(bin_dir)
- return if ENV['PATH'].split(':').include?(bin_dir)
- ENV['PATH'] = "#{bin_dir}:#{ENV['PATH']}"
- end
-
- # Return `true` if the given command exists and is executable.
- def command?(command)
- system("which #{command} > /dev/null 2>&1")
end
end
end
end