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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
31 require 'maasha/biopieces'
32 require 'maasha/fasta'
35 def initialize(directory, sequence_file, verbose)
36 @directory = directory
37 @sequence_file = sequence_file
41 def run_velveth(kmer_min, kmer_max, type)
47 while kmer <= @kmer_max
48 dir_velveth = File.join(@directory, "Kmer_#{kmer}")
50 Dir.mkdir(dir_velveth)
54 commands << dir_velveth
56 commands << "-#{type}"
58 commands << @sequence_file
66 def run_velvetg(cov_cutoffs, exp_cov)
67 Dir.glob("#{@directory}/Kmer_*").each do |dir_velveth|
68 files_velveth = Dir.glob("#{dir_velveth}/*")
70 cov_cutoffs.each do |cov_cutoff|
71 dir_velvetg = File.join(dir_velveth, "Cov_cutoff_#{cov_cutoff}")
73 Dir.mkdir(dir_velvetg)
74 FileUtils.cp_r files_velveth, dir_velvetg
78 commands << dir_velvetg
79 commands << "-cov_cutoff #{cov_cutoff}"
80 commands << "-exp_cov #{exp_cov}"
81 commands << "-clean yes"
88 def pick_best_assembly
91 Dir.glob("#{@directory}/Kmer_*/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}"
121 Fasta.open(file, mode="r") do |fasta_io|
122 fasta_io.each do |entry|
123 total += entry.length
124 lengths << entry.length
128 lengths.sort.reverse.each do |length|
131 if count >= total * 0.50
141 types = 'short,shortPaired,long,longPaired'
142 cov_cutoffs = "2,4,8,16"
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}
153 options = Biopieces.options_parse(ARGV, casts)
155 raise ArgumentError, "kmer_min #{options[:kmer_min]} must be uneven." if options[:kmer_min].even?
156 raise ArgumentError, "kmer_max #{options[:kmer_max]} must be uneven." if options[:kmer_max].even?
157 raise ArgumentError, "kmer_min >= kmer_max: #{options[:kmer_min]} >= #{options[:kmer_max]}" unless options[:kmer_max] > options[:kmer_min]
159 Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
161 file_fasta = File.join(options[:directory], "sequence_in.fna")
163 Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
164 Fasta.open(file_fasta, mode="w") do |fasta_io|
165 input.each_record do |record|
170 unless File.size(file_fasta) == 0
171 velvet = Velvet.new(options[:directory], file_fasta, options[:verbose])
172 velvet.run_velveth(options[:kmer_min], options[:kmer_max], options[:type])
173 velvet.run_velvetg(options[:cov_cutoff], options[:exp_cov])
174 file_contigs = velvet.pick_best_assembly
176 Fasta.open(file_contigs, mode="r") do |fasta_io|
177 fasta_io.each do |entry|
178 output.puts entry.to_bp
184 FileUtils.remove_entry_secure file_fasta
185 FileUtils.remove_entry_secure options[:directory] if options[:clean]
188 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<