]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/lib/maasha/prodigal.rb
temporary fix of bzip2 read problem
[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 # Class for running the Prodigal gene finder.
26 # http://prodigal.ornl.gov/
27 class Prodigal
28   include Enumerable
29
30   # Method to initialize a Prodigal object
31   # given a temporary infile, outfile and options hash.
32   def initialize(infile, outfile, options)
33     @infile  = infile    # FASTA
34     @outfile = outfile   # GFF
35     @options = options   # Options hash
36   end
37
38   # Method to run Prodigal.
39   def run
40     commands = []
41     commands << "prodigal"
42     commands << "-c" if @options[:full]
43     commands << "-p #{@options[:procedure]}"
44     commands << "-f gff"
45     commands << "-i #{@infile}"
46     commands << "-a #{@outfile}"
47     commands << "-m"
48     commands << "-q" unless @options[:verbose]
49
50     execute(commands, @options[:verbose])
51   end
52
53   # Method for iterating over the Prodigal results,
54   # which are in GFF format.
55   def each
56     Fasta.open(@outfile, 'r') do |ios|
57       ios.each do |entry|
58         record = {}
59
60         fields = entry.seq_name.split(" # ")
61
62         record[:REC_TYPE] = "GENE"
63         record[:S_ID]     = fields[0]
64         record[:S_BEG]    = fields[1].to_i - 1
65         record[:S_END]    = fields[2].to_i - 1
66         record[:S_LEN]    = record[:S_END] - record[:S_BEG] + 1
67         record[:STRAND]   = fields[3] == '1' ? '+' : '-'
68         record[:SEQ]      = entry.seq
69         record[:SEQ_LEN]  = entry.length
70
71         yield record
72       end
73     end
74   end
75
76   # Method that returns the sum of the predicted gene lengths.
77   def coverage
78     coverage = 0
79
80     self.each do |gb|
81       coverage += gb[:S_END] - gb[:S_BEG]
82     end
83
84     coverage
85   end
86
87   private
88
89   # Method to execute commands on the command line.
90   # TODO this wants to be in a module on its own.
91   def execute(commands, verbose = false)
92     commands.unshift "nice -n 19"
93     commands << " > /dev/null"
94
95     command = commands.join(" ")
96
97     system(command)
98     raise "Command failed: #{command}" unless $?.success?
99   end
100 end
101
102 __END__