Module: SequenceServer::Helpers::SystemHelpers

Defined in:
lib/helpers.rb

Instance Method Summary collapse

Instance Method Details

#scan_blast_db(db_root, blastdbcmd = 'blastdbcmd') ⇒ Object

Scan the given directory (including subdirectory) for blast databases.


Arguments:

  • db_root(String) - absolute path to the blast databases


Returns:

  • a hash of sorted blast databases grouped by database type:

protein, or nucleotide


Raises:

  • IOError - if no database can be found

    > scan_blast_db('/home/yeban/blast_db')

    > { "protein" => [], "nucleotide" => [] }

Raises:

  • (IOError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/helpers.rb', line 59

def scan_blast_db(db_root, blastdbcmd = 'blastdbcmd')
  raise IOError, "Database directory doesn't exist: #{db_root}" unless File.directory?( db_root )

  find_dbs_command = %|#{blastdbcmd} -recursive -list #{db_root} -list_outfmt "%p %f %t" 2>&1|

  begin
    db_list = %x|#{find_dbs_command}|
    if db_list.empty?
      raise IOError, "No formatted blast databases found in '#{ db_root }'."
    end
  rescue => e
    puts '', e.to_s

    print "Do you want to format your blast databases now? [Y/n]: "
    choice = gets.chomp[0,1].downcase

    unless choice == 'n'
      database_formatter = File.join(settings.root, 'database_formatter.rb')
      system("#{database_formatter} #{db_root}")
      retry
    else
      raise # let the caller decide what to do if database discovery fails
    end
  end

  if db_list.match(/BLAST Database error/)
    raise IOError, "Error parsing blast databases.\n" + "Tried: '#{find_dbs_command}'\n"+
      "It crashed with the following error: '#{db_list}'\n" +
      "Try reformatting databases using makeblastdb.\n"
  end

  db = {}

  db_list.each_line do |line|
    type, name, *title =  line.split(' ') 
    type = type.downcase
    name = name.freeze
    title = title.join(' ').freeze
    #LOG.info("Found #{type} database: #{title} at #{name}")
    (db[type] ||= []) << Database.new(name, title)
  end


  # the erb would fail as calling nil.each_with_index if a dbtype was undefined. 
  db['protein']    = [] unless db.keys.include?('protein')
  db['nucleotide'] = [] unless db.keys.include?('nucleotide')

  # sort the list of dbs
  db['protein'].sort!
  db['nucleotide'].sort!

  db 
end

#scan_blast_executables(bin) ⇒ Object

Scan the given directory for blast executables. Passing `nil` scans the system `PATH`.


Arguments:

  • bin(String) - absolute path to the directory containing blast binaries


Returns:

  • a hash of blast methods, and their corresponding absolute path


Raises:

  • IOError - if the executables can't be found

    > scan_blast_executables('/home/yeban/bin')

    > { "blastx"=>"/home/yeban/bin/blastx",

      "blastn"=>"/home/yeban/bin/blastn",
      ...
    }


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/helpers.rb', line 23

def scan_blast_executables(bin)
  if bin and not File.directory?(bin)
    raise IOError, "Could not find '#{bin}' defined in config.yml."
  end

  binaries = {}
  %w|blastn blastp blastx tblastn tblastx blastdbcmd makeblastdb|.each do |method|
    path = File.join(bin, method) rescue method
    if command?(path)
      binaries[method] = path
    else
      blasturl = 'http://www.ncbi.nlm.nih.gov/blast/Blast.cgi?CMD=Web&PAGE_TYPE=BlastDocs&DOC_TYPE=Download'
      raise IOError, "Could not find blast binaries. You may need to
      install BLAST+ from #{blasturl}. And/or point config.yml to blast's
      bin directory."
    end
  end

  #LOG.info("Config bin dir:          #{bin}")
  binaries
end