3 # Copyright (C) 2007-2011 Martin A. Hansen.
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.
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.
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.
19 # http://www.gnu.org/copyleft/gpl.html
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
23 # This program is part of the Biopieces framework (www.biopieces.org).
25 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> DESCRIPTION <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
27 # Assemble sequences in the stream using Velvet.
29 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
36 def initialize(directory, sequence_file, verbose)
37 @directory = directory
38 @sequence_file = sequence_file
42 def run_velveth(kmer_min, kmer_max, type)
48 while kmer <= @kmer_max
49 dir_velveth = [@directory, "Velvet_#{kmer}"].join(File::SEPARATOR)
51 Dir.mkdir(dir_velveth)
55 commands << dir_velveth
57 commands << "-#{type}"
59 commands << @sequence_file
67 def run_velvetg(cov_cutoffs, exp_cov)
68 Dir.glob("#{@directory}/Velvet_*").each do |dir_velveth|
69 files_velveth = Dir.glob("#{dir_velveth}/*")
71 cov_cutoffs.each do |cov_cutoff|
72 dir_velvetg = [dir_velveth, "Cov_cutoff_#{cov_cutoff}"].join(File::SEPARATOR)
74 Dir.mkdir(dir_velvetg)
75 FileUtils.cp_r files_velveth, dir_velvetg
79 commands << dir_velvetg
80 commands << "-cov_cutoff #{cov_cutoff}"
81 commands << "-exp_cov #{exp_cov}"
88 def pick_best_assembly
91 Dir.glob("#{@directory}/Velvet_*/Cov_cutoff_*/contigs.fa").each do |file|
96 list.sort_by { |e| e.last }.last.first
101 def execute(commands)
102 commands.unshift "nice -n 19"
103 commands.push "> /dev/null 2>&1" unless @verbose
105 command = commands.join(" ")
109 raise "Command failed: #{command}" unless $?.success?
111 $stderr.puts "Command failed: #{command}"
119 Fasta.open(file, mode="r") do |fasta_io|
120 fasta_io.each do |entry|
121 total += entry.length
122 lengths << entry.length
129 def n50(total, lengths)
133 lengths.sort.reverse.each do |length|
136 if count >= total * 0.50
146 types = 'short,shortPaired,long,longPaired'
147 cov_cutoffs = "2,4,8,16"
150 casts << {:long=>'directory', :short=>'d', :type=>'dir', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
151 casts << {:long=>'type', :short=>'t', :type=>'string', :mandatory=>true, :default=>'short', :allowed=>types, :disallowed=>nil}
152 casts << {:long=>'kmer_min', :short=>'k', :type=>'uint', :mandatory=>true, :default=>19, :allowed=>nil, :disallowed=>nil}
153 casts << {:long=>'kmer_max', :short=>'K', :type=>'uint', :mandatory=>true, :default=>31, :allowed=>nil, :disallowed=>nil}
154 casts << {:long=>'cov_cutoff', :short=>'c', :type=>'list', :mandatory=>true, :default=>cov_cutoffs, :allowed=>nil, :disallowed=>nil}
155 casts << {:long=>'exp_cov', :short=>'e', :type=>'float', :mandatory=>true, :default=>'auto', :allowed=>nil, :disallowed=>nil}
156 casts << {:long=>'clean', :short=>'x', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
160 options = bp.parse(ARGV, casts)
162 raise ArgumentError, "kmer_min #{options[:kmer_min]} must be uneven." if options[:kmer_min].even?
163 raise ArgumentError, "kmer_max #{options[:kmer_max]} must be uneven." if options[:kmer_max].even?
164 raise ArgumentError, "kmer_min >= kmer_max: #{options[:kmer_min]} >= #{options[:kmer_max]}" unless options[:kmer_max] > options[:kmer_min]
166 Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
168 file_fasta = [options[:directory], "sequence_in.fna"].join(File::SEPARATOR)
170 Fasta.open(file_fasta, mode="w") do |fasta_io|
171 bp.each_record do |record|
176 unless File.size(file_fasta) == 0
177 velvet = Velvet.new(options[:directory], file_fasta, options[:verbose])
178 velvet.run_velveth(options[:kmer_min], options[:kmer_max], options[:type])
179 velvet.run_velvetg(options[:cov_cutoff], options[:exp_cov])
180 file_contigs = velvet.pick_best_assembly
182 Fasta.open(file_contigs, mode="r") do |fasta_io|
183 fasta_io.each do |entry|
189 FileUtils.remove_entry_secure options[:directory] if options[:clean]
192 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<