]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/lib/maasha/prodigal.rb
changed layout of ruby source
[biopieces.git] / code_ruby / lib / maasha / prodigal.rb
1 # Copyright (C) 2011 Martin A. Hansen.
2
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
17 # http://www.gnu.org/copyleft/gpl.html
18
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
20
21 # This software is part of the Biopieces framework (www.biopieces.org).
22
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24
25 require 'maasha/genbank'
26
27 # Class for running the Prodigal gene finder.
28 # http://prodigal.ornl.gov/
29 class Prodigal
30   include Enumerable
31
32   # Method to initialize a Prodigal object
33   # given a temporary infile, outfile and options hash.
34   def initialize(infile, outfile, options)
35     @infile  = infile
36     @outfile = outfile
37     @options = options
38   end
39
40   # Method to run Prodigal.
41   def run
42     commands = []
43     commands << "prodigal"
44     commands << "-c" if @options[:full]
45     commands << "-p #{@options[:procedure]}"
46     commands << "-i #{@infile}"
47     commands << "-o #{@outfile}"
48
49     execute(commands, @options[:verbose])
50   end
51
52   # Method for iterating over the Prodigal results,
53   # which are in GenBank format.
54   def each
55     Genbank.open(@outfile, mode='r') do |gb|
56       gb.each do |entry|
57         yield entry
58       end
59     end
60   end
61
62   # Method that returns the sum of the predicted gene lengths.
63   def coverage
64     coverage = 0
65
66     self.each do |gb|
67       coverage += gb[:S_END] - gb[:S_BEG]
68     end
69
70     coverage
71   end
72
73   private
74
75   # Method to execute commands on the command line.
76   # TODO this wants to be in a module on its own.
77   def execute(commands, verbose = false)
78     commands.unshift "nice -n 19"
79     commands.push "> /dev/null 2>&1" unless verbose
80
81     command = commands.join(" ")
82
83     begin
84       system(command)
85       raise "Command failed: #{command}" unless $?.success?
86     rescue
87       $stderr.puts "Command failed: #{command}"
88     end
89   end
90 end
91
92
93 __END__