]> git.donarmstrong.com Git - biopieces.git/blob - bp_bin/assemble_seq_velvet
polished asseble_seq_velvet
[biopieces.git] / bp_bin / assemble_seq_velvet
1 #!/usr/bin/env ruby
2
3 # Copyright (C) 2007-2011 Martin A. Hansen.
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 # http://www.gnu.org/copyleft/gpl.html
20
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
22
23 # This program is part of the Biopieces framework (www.biopieces.org).
24
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26
27 # Assemble sequences in the stream using Velvet.
28
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
30
31 require 'biopieces'
32 require 'fasta'
33
34 class Velvet
35   def initialize(directory, sequence_file, verbose)
36     @directory     = directory
37     @sequence_file = sequence_file
38     @verbose       = verbose
39   end
40
41   def run_velveth(kmer_min, kmer_max, type)
42     @kmer_min = kmer_min
43     @kmer_max = kmer_max
44
45     kmer = @kmer_min
46
47     while kmer <= @kmer_max
48       dir_velveth = [@directory, "Kmer_#{kmer}"].join(File::SEPARATOR)
49
50       Dir.mkdir(dir_velveth)
51
52       commands = []
53       commands << "velveth"
54       commands << dir_velveth
55       commands << kmer
56       commands << "-#{type}"
57       commands << "-fasta"
58       commands << @sequence_file
59
60       execute(commands)
61
62       kmer += 2
63     end
64   end
65
66   def run_velvetg(cov_cutoffs, exp_cov)
67     Dir.glob("#{@directory}/Kmer_*").each do |dir_velveth|
68       files_velveth = Dir.glob("#{dir_velveth}/*")
69
70       cov_cutoffs.each do |cov_cutoff|
71         dir_velvetg = [dir_velveth, "Cov_cutoff_#{cov_cutoff}"].join(File::SEPARATOR)
72
73         Dir.mkdir(dir_velvetg)
74         FileUtils.cp_r files_velveth, dir_velvetg
75         
76         commands = []
77         commands << "velvetg"
78         commands << dir_velvetg
79         commands << "-cov_cutoff #{cov_cutoff}"
80         commands << "-exp_cov #{exp_cov}"
81         commands << "-clean yes"
82
83         execute(commands)
84       end
85     end
86   end
87
88   def pick_best_assembly
89     list = []
90
91     Dir.glob("#{@directory}/Kmer_*/Cov_cutoff_*/contigs.fa").each do |file|
92       n50 = fasta_n50(file)
93       list << [file, n50]
94     end
95
96     list.sort_by { |e| e.last }.last.first
97   end
98
99   private
100
101   def execute(commands)
102     commands.unshift "nice -n 19"
103     commands.push "> /dev/null 2>&1" unless @verbose
104
105     command = commands.join(" ")
106
107     begin
108       system(command)
109       raise "Command failed: #{command}" unless $?.success?
110     rescue
111       $stderr.puts "Command failed: #{command}"
112     end
113   end
114
115   def fasta_n50(file)
116     total   = 0
117     lengths = []
118     count   = 0
119     n50     = 0
120
121     Fasta.open(file, mode="r") do |fasta_io|
122       fasta_io.each do |entry|
123         total   += entry.length
124         lengths << entry.length
125       end
126     end
127
128     lengths.sort.reverse.each do |length|
129       count += length
130
131       if count >= total * 0.50
132         n50 = length
133         break
134       end
135     end
136
137     n50
138   end
139 end
140
141 types       = 'short,shortPaired,long,longPaired'
142 cov_cutoffs = "2,4,8,16"
143
144 casts = []
145 casts << {:long=>'directory',  :short=>'d', :type=>'dir',    :mandatory=>true,  :default=>nil,         :allowed=>nil,   :disallowed=>nil}
146 casts << {:long=>'type',       :short=>'t', :type=>'string', :mandatory=>true,  :default=>'short',     :allowed=>types, :disallowed=>nil}
147 casts << {:long=>'kmer_min',   :short=>'k', :type=>'uint',   :mandatory=>true,  :default=>19,          :allowed=>nil,   :disallowed=>nil}
148 casts << {:long=>'kmer_max',   :short=>'K', :type=>'uint',   :mandatory=>true,  :default=>31,          :allowed=>nil,   :disallowed=>nil}
149 casts << {:long=>'cov_cutoff', :short=>'c', :type=>'list',   :mandatory=>true,  :default=>cov_cutoffs, :allowed=>nil,   :disallowed=>nil}
150 casts << {:long=>'exp_cov',    :short=>'e', :type=>'float',  :mandatory=>true,  :default=>'auto',      :allowed=>nil,   :disallowed=>nil}
151 casts << {:long=>'clean',      :short=>'x', :type=>'flag',   :mandatory=>false, :default=>nil,         :allowed=>nil,   :disallowed=>nil}
152
153 bp = Biopieces.new
154
155 options = bp.parse(ARGV, casts)
156
157 raise ArgumentError, "kmer_min #{options[:kmer_min]} must be uneven." if options[:kmer_min].even?
158 raise ArgumentError, "kmer_max #{options[:kmer_max]} must be uneven." if options[:kmer_max].even?
159 raise ArgumentError, "kmer_min >= kmer_max: #{options[:kmer_min]} >= #{options[:kmer_max]}" unless options[:kmer_max] > options[:kmer_min]
160
161 Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
162
163 file_fasta = [options[:directory], "sequence_in.fna"].join(File::SEPARATOR)
164
165 Fasta.open(file_fasta, mode="w") do |fasta_io|
166   bp.each_record do |record|
167     fasta_io.puts record
168   end
169 end
170
171 unless File.size(file_fasta) == 0
172   velvet = Velvet.new(options[:directory], file_fasta, options[:verbose])
173   velvet.run_velveth(options[:kmer_min], options[:kmer_max], options[:type])
174   velvet.run_velvetg(options[:cov_cutoff], options[:exp_cov])
175   file_contigs = velvet.pick_best_assembly
176
177   Fasta.open(file_contigs, mode="r") do |fasta_io|
178     fasta_io.each do |entry|
179       bp.puts entry.to_bp
180     end
181   end
182 end
183
184 FileUtils.remove_entry_secure options[:directory] if options[:clean]
185
186
187 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
188
189
190 __END__