module NeuroHmmerApp

Define Config class.

This file defines all possible exceptions that can be thrown by NeuroHmmerApp on startup.

Exceptions only ever inform another entity (downstream code or users) of an issue. Exceptions may or may not be recoverable.

Error classes should be seen as: the error code (class name), human readable message (to_s method), and necessary attributes to act on the error.

We define as many error classes as needed to be precise about the issue, thus making it easy for downstream code (bin/genevalidatorapp or config.ru) to act on them.

Constants

MINIMUM_HMMER_VERSION

Use a fixed minimum version of BLAST+

VERSION

Attributes

config[R]
public_dir[R]
temp_dir[R]

Public Class Methods

call(env) click to toggle source

Rack-interface.

Inject our logger in the env and dispatch request to our controller.

# File lib/neurohmmerapp.rb, line 80
def call(env)
  env['rack.logger'] = logger
  Routes.call(env)
end
environment() click to toggle source
# File lib/neurohmmerapp.rb, line 17
def environment
  ENV['RACK_ENV']
end
init(config = {}) click to toggle source

Setting up the environment before running the app…

# File lib/neurohmmerapp.rb, line 34
def init(config = {})
  @config = Config.new(config)

  init_binaries
  init_dirs
  check_num_threads
  check_max_characters
  self
end
logger() click to toggle source
# File lib/neurohmmerapp.rb, line 29
def logger
  @logger ||= Logger.new(STDERR, verbose?)
end
on_start() click to toggle source
# File lib/neurohmmerapp.rb, line 62
def on_start
  puts '** NeuroHmmer is ready.'
  puts "   Go to #{server_url} in your browser and start analysing genes!"
  puts '   Press CTRL+C to quit.'
  open_in_browser(server_url)
end
on_stop() click to toggle source
# File lib/neurohmmerapp.rb, line 69
def on_stop
  puts
  puts '** Thank you for using NeuroHmmerApp :).'
  puts '   Please cite: '
  puts '        Moghul M.I., Elphick M & Wurm Y (in prep).'
  puts '        NeuroHmmer: identify Neuropeptide Precursors.'
end
root() click to toggle source
# File lib/neurohmmerapp.rb, line 25
def root
  File.dirname(File.dirname(__FILE__))
end
run() click to toggle source

Starting the app manually

# File lib/neurohmmerapp.rb, line 47
def run
  check_host
  Server.run(self)
rescue Errno::EADDRINUSE
  puts "** Could not bind to port #{config[:port]}."
  puts "   Is NeuroHmmer already accessible at #{server_url}?"
  puts '   No? Try running NeuroHmmer on another port, like so:'
  puts
  puts '       neurohmmerapp -p 4570.'
rescue Errno::EACCES
  puts "** Need root privilege to bind to port #{config[:port]}."
  puts '   It is not advisable to run NeuroHmmer as root.'
  puts '   Please use Apache/Nginx to bind to a privileged port.'
end
verbose?() click to toggle source
# File lib/neurohmmerapp.rb, line 21
def verbose?
  @verbose ||= (environment == 'development')
end

Private Class Methods

assert_blast_installed_and_compatible() click to toggle source
# File lib/neurohmmerapp.rb, line 149
def assert_blast_installed_and_compatible
  fail HMMER_NOT_INSTALLED unless command? 'hmmscan'
  # version = `hmmscan -version`.split[1]
  # fail HMMER_NOT_COMPATIBLE, version unless version >= MINIMUM_HMMER_VERSION
end
assert_mafft_installed() click to toggle source
# File lib/neurohmmerapp.rb, line 155
def assert_mafft_installed
  fail MAFFT_NOT_INSTALLED unless command? 'mafft'
end
check_host() click to toggle source

Check and warn user if host is 0.0.0.0 (default).

# File lib/neurohmmerapp.rb, line 160
def check_host
  return unless config[:host] == '0.0.0.0'
  logger.warn 'Will listen on all interfaces (0.0.0.0).' \
              ' Consider using 127.0.0.1 (--host option).'
end
check_max_characters() click to toggle source
# File lib/neurohmmerapp.rb, line 122
def check_max_characters
  if config[:max_characters] != 'undefined'
    config[:max_characters] = Integer(config[:max_characters])
  end
rescue
  raise MAX_CHARACTERS_INCORRECT
end
check_num_threads() click to toggle source
# File lib/neurohmmerapp.rb, line 110
def check_num_threads
  num_threads = Integer(config[:num_threads])
  fail NUM_THREADS_INCORRECT unless num_threads > 0

  logger.debug "Will use #{num_threads} threads to run BLAST."
  if num_threads > 256
    logger.warn "Number of threads set at #{num_threads} is unusually high."
  end
rescue
  raise NUM_THREADS_INCORRECT
end
command?(command) click to toggle source

Return ‘true` if the given command exists and is executable.

# File lib/neurohmmerapp.rb, line 190
def command?(command)
  system("which #{command} > /dev/null 2>&1")
end
export_bin_dir(bin_dir) click to toggle source

Checks if dir is in $PATH and if not, it adds the dir to the $PATH.

# File lib/neurohmmerapp.rb, line 143
def export_bin_dir(bin_dir)
  return unless bin_dir
  return if ENV['PATH'].split(':').include?(bin_dir)
  ENV['PATH'] = "#{bin_dir}:#{ENV['PATH']}"
end
init_binaries() click to toggle source
# File lib/neurohmmerapp.rb, line 104
def init_binaries
  config[:bin] = init_bins if config[:bin]
  assert_blast_installed_and_compatible
  assert_mafft_installed
end
init_bins() click to toggle source
# File lib/neurohmmerapp.rb, line 130
def init_bins
  bins = []
  config[:bin].each do |bin|
    bins << File.expand_path(bin)
    unless File.exist?(bin) && File.directory?(bin)
      fail BIN_DIR_NOT_FOUND, config[:bin]
    end
    export_bin_dir(bin)
  end
  bins
end
init_dirs() click to toggle source
# File lib/neurohmmerapp.rb, line 87
def init_dirs
  config[:public_dir] = File.expand_path(config[:public_dir])
  unique_start_id     = 'NH_' + "#{Time.now.strftime('%Y%m%d-%H-%M-%S')}"
  @public_dir         = File.join(config[:public_dir], unique_start_id)
  init_public_dir
end
init_public_dir() click to toggle source

Create the Public Dir and copy files from gem root - this public dir

is served by the app is accessible at URL/...
# File lib/neurohmmerapp.rb, line 96
def init_public_dir
  FileUtils.mkdir_p(File.join(@public_dir, 'NeuroHmmer'))
  root_web_files = File.join(NeuroHmmerApp.root, 'public/web_files')
  root_gv        = File.join(NeuroHmmerApp.root, 'public/NeuroHmmer')
  FileUtils.cp_r(root_web_files, @public_dir)
  FileUtils.cp_r(root_gv, @public_dir)
end
open_in_browser(server_url) click to toggle source
# File lib/neurohmmerapp.rb, line 172
def open_in_browser(server_url)
  return if using_ssh? || verbose?
  if RUBY_PLATFORM =~ /linux/ && xdg?
    system "xdg-open #{server_url}"
  elsif RUBY_PLATFORM =~ /darwin/
    system "open #{server_url}"
  end
end
server_url() click to toggle source
# File lib/neurohmmerapp.rb, line 166
def server_url
  host = config[:host]
  host = 'localhost' if host == '127.0.0.1' || host == '0.0.0.0'
  "http://#{host}:#{config[:port]}"
end
using_ssh?() click to toggle source
# File lib/neurohmmerapp.rb, line 181
def using_ssh?
  true if ENV['SSH_CLIENT'] || ENV['SSH_TTY'] || ENV['SSH_CONNECTION']
end
xdg?() click to toggle source
# File lib/neurohmmerapp.rb, line 185
def xdg?
  true if ENV['DISPLAY'] && command?('xdg-open')
end