]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/Maasha/lib/prodigal.rb
1a1fd552af5311caf9f8b3ce1bd0187aa6b81007
[biopieces.git] / code_ruby / Maasha / lib / 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 '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 << "-i #{@infile}"
46     commands << "-o #{@outfile}"
47
48     execute(commands, @options[:verbose])
49   end
50
51   # Method for iterating over the Prodigal results,
52   # which are in GenBank format.
53   def each
54     Genbank.open(@outfile, mode='r') do |gb|
55       gb.each do |entry|
56         yield entry
57       end
58     end
59   end
60
61   # Method that returns the sum of the predicted gene lengths.
62   def coverage
63     coverage = 0
64
65     self.each do |gb|
66       coverage += gb[:S_END] - gb[:S_BEG]
67     end
68
69     coverage
70   end
71
72   private
73
74   # Method to execute commands on the command line.
75   # TODO this wants to be in a module on its own.
76   def execute(commands, verbose = false)
77     commands.unshift "nice -n 19"
78     commands.push "> /dev/null 2>&1" unless verbose
79
80     command = commands.join(" ")
81
82     begin
83       system(command)
84       raise "Command failed: #{command}" unless $?.success?
85     rescue
86       $stderr.puts "Command failed: #{command}"
87     end
88   end
89 end
90
91
92 __END__