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, "Kmer_#{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}/Kmer_*").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}"
82 commands << "-clean yes"
89 def pick_best_assembly
92 Dir.glob("#{@directory}/Kmer_*/Cov_cutoff_*/contigs.fa").each do |file|
97 list.sort_by { |e| e.last }.last.first
102 def execute(commands)
103 commands.unshift "nice -n 19"
104 commands.push "> /dev/null 2>&1" unless @verbose
106 command = commands.join(" ")
110 raise "Command failed: #{command}" unless $?.success?
112 $stderr.puts "Command failed: #{command}"
120 Fasta.open(file, mode="r") do |fasta_io|
121 fasta_io.each do |entry|
122 total += entry.length
123 lengths << entry.length
130 def n50(total, lengths)
134 lengths.sort.reverse.each do |length|
137 if count >= total * 0.50
147 types = 'short,shortPaired,long,longPaired'
148 cov_cutoffs = "2,4,8,16"
151 casts << {:long=>'directory', :short=>'d', :type=>'dir', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil}
152 casts << {:long=>'type', :short=>'t', :type=>'string', :mandatory=>true, :default=>'short', :allowed=>types, :disallowed=>nil}
153 casts << {:long=>'kmer_min', :short=>'k', :type=>'uint', :mandatory=>true, :default=>19, :allowed=>nil, :disallowed=>nil}
154 casts << {:long=>'kmer_max', :short=>'K', :type=>'uint', :mandatory=>true, :default=>31, :allowed=>nil, :disallowed=>nil}
155 casts << {:long=>'cov_cutoff', :short=>'c', :type=>'list', :mandatory=>true, :default=>cov_cutoffs, :allowed=>nil, :disallowed=>nil}
156 casts << {:long=>'exp_cov', :short=>'e', :type=>'float', :mandatory=>true, :default=>'auto', :allowed=>nil, :disallowed=>nil}
157 casts << {:long=>'clean', :short=>'x', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil}
161 options = bp.parse(ARGV, casts)
163 raise ArgumentError, "kmer_min #{options[:kmer_min]} must be uneven." if options[:kmer_min].even?
164 raise ArgumentError, "kmer_max #{options[:kmer_max]} must be uneven." if options[:kmer_max].even?
165 raise ArgumentError, "kmer_min >= kmer_max: #{options[:kmer_min]} >= #{options[:kmer_max]}" unless options[:kmer_max] > options[:kmer_min]
167 Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
169 file_fasta = [options[:directory], "sequence_in.fna"].join(File::SEPARATOR)
171 Fasta.open(file_fasta, mode="w") do |fasta_io|
172 bp.each_record do |record|
177 unless File.size(file_fasta) == 0
178 velvet = Velvet.new(options[:directory], file_fasta, options[:verbose])
179 velvet.run_velveth(options[:kmer_min], options[:kmer_max], options[:type])
180 velvet.run_velvetg(options[:cov_cutoff], options[:exp_cov])
181 file_contigs = velvet.pick_best_assembly
183 Fasta.open(file_contigs, mode="r") do |fasta_io|
184 fasta_io.each do |entry|
190 FileUtils.remove_entry_secure options[:directory] if options[:clean]
193 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<