From c59cc231c38eb62f8acd0bd604cadca057583802 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Wed, 9 Jan 2013 10:19:07 +0000 Subject: [PATCH] rewrote assemble_seq_velvet git-svn-id: http://biopieces.googlecode.com/svn/trunk@2066 74ccb610-7750-0410-82ae-013aeee3265d --- bp_bin/assemble_seq_velvet | 107 +++++++++---------------------------- 1 file changed, 25 insertions(+), 82 deletions(-) diff --git a/bp_bin/assemble_seq_velvet b/bp_bin/assemble_seq_velvet index d613967..3b023b2 100755 --- a/bp_bin/assemble_seq_velvet +++ b/bp_bin/assemble_seq_velvet @@ -38,67 +38,33 @@ class Velvet @verbose = verbose end - def run_velveth(kmer_min, kmer_max, type) - @kmer_min = kmer_min - @kmer_max = kmer_max - - kmer = @kmer_min - - while kmer <= @kmer_max - dir_velveth = File.join(@directory, "Kmer_#{kmer}") - - Dir.mkdir(dir_velveth) - - commands = [] - commands << "velveth" - commands << dir_velveth - commands << kmer - commands << "-#{type}" - commands << "-fasta" - commands << @sequence_file - - execute(commands) - - kmer += 2 - end + def run_velveth(kmer_min, kmer_max, kmer_step, type) + commands = [] + commands << "velveth" + commands << @directory + commands << "#{kmer_min},#{kmer_max},#{kmer_step}" + commands << "-#{type}" + commands << "-fasta" + commands << @sequence_file + + execute(commands) end def run_velvetg(cov_cutoff, exp_cov, paired) - Dir.glob("#{@directory}/Kmer_*").each do |dir_velveth| - files_velveth = Dir.glob("#{dir_velveth}/*") - - dir_velvetg = File.join(dir_velveth, "Cov_cutoff_#{cov_cutoff}") - - Dir.mkdir(dir_velvetg) - FileUtils.cp_r files_velveth, dir_velvetg - - commands = [] - commands << "velvetg" - commands << dir_velvetg - commands << "-cov_cutoff #{cov_cutoff}" - commands << "-exp_cov #{exp_cov}" - commands << "-ins_length #{ins_length}" if paired - commands << "-clean yes" - - execute(commands) - end - end - - def pick_best_assembly - list = [] - - Dir.glob("#{@directory}/Kmer_*/Cov_cutoff_*/contigs.fa").each do |file| - n50 = fasta_n50(file) - list << [file, n50] - end - - list.sort_by { |e| e.last }.last.first + commands = [] + commands << "velvetg" + commands << @directory + commands << "-cov_cutoff #{cov_cutoff}" + commands << "-exp_cov #{exp_cov}" + commands << "-ins_length #{ins_length}" if paired + commands << "-clean yes" + + execute(commands) end private def execute(commands) - commands.unshift "nice -n 19" commands.push "> /dev/null 2>&1" unless @verbose command = commands.join(" ") @@ -110,49 +76,26 @@ class Velvet $stderr.puts "Command failed: #{command}" end end - - def fasta_n50(file) - total = 0 - lengths = [] - count = 0 - n50 = 0 - - Fasta.open(file, mode="r") do |fasta_io| - fasta_io.each do |entry| - total += entry.length - lengths << entry.length - end - end - - lengths.sort.reverse.each do |length| - count += length - - if count >= total * 0.50 - n50 = length - break - end - end - - n50 - end end -types = 'short,shortPaired,long,longPaired' +types = 'short,shortPaired,long,longPaired' casts = [] casts << {:long=>'directory', :short=>'d', :type=>'dir', :mandatory=>true, :default=>nil, :allowed=>nil, :disallowed=>nil} casts << {:long=>'type', :short=>'t', :type=>'string', :mandatory=>true, :default=>'short', :allowed=>types, :disallowed=>nil} casts << {:long=>'kmer_min', :short=>'k', :type=>'uint', :mandatory=>true, :default=>19, :allowed=>nil, :disallowed=>nil} casts << {:long=>'kmer_max', :short=>'K', :type=>'uint', :mandatory=>true, :default=>31, :allowed=>nil, :disallowed=>nil} +casts << {:long=>'kmer_step', :short=>'s', :type=>'uint', :mandatory=>true, :default=>2, :allowed=>nil, :disallowed=>nil} casts << {:long=>'cov_cutoff', :short=>'c', :type=>'float', :mandatory=>true, :default=>'auto', :allowed=>nil, :disallowed=>nil} casts << {:long=>'exp_cov', :short=>'e', :type=>'float', :mandatory=>true, :default=>'auto', :allowed=>nil, :disallowed=>nil} -casts << {:long=>'ins_length', :short=>'i', :type=>'uint', :mandatory=>false, :default=>700, :allowed=>nil, :disallowed=>nil} +casts << {:long=>'ins_length', :short=>'i', :type=>'uint', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil} casts << {:long=>'clean', :short=>'X', :type=>'flag', :mandatory=>false, :default=>nil, :allowed=>nil, :disallowed=>nil} options = Biopieces.options_parse(ARGV, casts) raise ArgumentError, "kmer_min #{options[:kmer_min]} must be uneven." if options[:kmer_min].even? raise ArgumentError, "kmer_max #{options[:kmer_max]} must be uneven." if options[:kmer_max].even? +raise ArgumentError, "kmer_step #{options[:kmer_step]} must be even." unless options[:kmer_step].even? raise ArgumentError, "kmer_min >= kmer_max: #{options[:kmer_min]} >= #{options[:kmer_max]}" unless options[:kmer_max] > options[:kmer_min] Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory]) @@ -171,9 +114,9 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output| unless File.size(file_fasta) == 0 velvet = Velvet.new(options[:directory], file_fasta, options[:verbose]) - velvet.run_velveth(options[:kmer_min], options[:kmer_max], options[:type]) + velvet.run_velveth(options[:kmer_min], options[:kmer_max], options[:kmer_step], options[:type]) velvet.run_velvetg(options[:cov_cutoff], options[:exp_cov], options[:type].match("Paired")) - file_contigs = velvet.pick_best_assembly + file_contigs = File.join(options[:directory], "contigs.fa") Fasta.open(file_contigs, mode="r") do |fasta_io| fasta_io.each do |entry| -- 2.39.2