]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/assemble_seq_velvet
set cutoff to auto in velvet
[biopieces.git] / bp_bin / assemble_seq_velvet
index cf6160f05ede5477b57c6f366d88cfad05686ae6..90cf759b8c82c6fd947c6356a85961c2b20e2c19 100755 (executable)
@@ -28,9 +28,8 @@
 
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
-require 'biopieces'
-require 'fasta'
-require 'pp'
+require 'maasha/biopieces'
+require 'maasha/fasta'
 
 class Velvet
   def initialize(directory, sequence_file, verbose)
@@ -46,7 +45,7 @@ class Velvet
     kmer = @kmer_min
 
     while kmer <= @kmer_max
-      dir_velveth = [@directory, "Velvet_#{kmer}"].join(File::SEPARATOR)
+      dir_velveth = File.join(@directory, "Kmer_#{kmer}")
 
       Dir.mkdir(dir_velveth)
 
@@ -64,31 +63,30 @@ class Velvet
     end
   end
 
-  def run_velvetg(cov_cutoffs, exp_cov)
-    Dir.glob("#{@directory}/Velvet_*").each do |dir_velveth|
+  def run_velvetg(cov_cutoff, exp_cov)
+    Dir.glob("#{@directory}/Kmer_*").each do |dir_velveth|
       files_velveth = Dir.glob("#{dir_velveth}/*")
 
-      cov_cutoffs.each do |cov_cutoff|
-        dir_velvetg = [dir_velveth, "Cov_cutoff_#{cov_cutoff}"].join(File::SEPARATOR)
+      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}"
+      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 << "-clean yes"
 
-        execute(commands)
-      end
+      execute(commands)
     end
   end
 
   def pick_best_assembly
     list = []
 
-    Dir.glob("#{@directory}/Velvet_*/Cov_cutoff_*/contigs.fa").each do |file|
+    Dir.glob("#{@directory}/Kmer_*/Cov_cutoff_*/contigs.fa").each do |file|
       n50 = fasta_n50(file)
       list << [file, n50]
     end
@@ -115,6 +113,8 @@ class Velvet
   def fasta_n50(file)
     total   = 0
     lengths = []
+    count   = 0
+    n50     = 0
 
     Fasta.open(file, mode="r") do |fasta_io|
       fasta_io.each do |entry|
@@ -123,13 +123,6 @@ class Velvet
       end
     end
 
-    n50(total, lengths)
-  end
-
-  def n50(total, lengths)
-    count = 0
-    n50   = 0
-
     lengths.sort.reverse.each do |length|
       count += length
 
@@ -144,20 +137,17 @@ class Velvet
 end
 
 types       = 'short,shortPaired,long,longPaired'
-cov_cutoffs = "2,4,8,16"
 
 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=>'cov_cutoff', :short=>'c', :type=>'list',   :mandatory=>true,  :default=>cov_cutoffs, :allowed=>nil,   :disallowed=>nil}
-casts << {:long=>'exp_cov',    :short=>'e', :type=>'float',  :mandatory=>true,  :default=>'auto',      :allowed=>nil,   :disallowed=>nil}
-casts << {:long=>'clean',      :short=>'x', :type=>'flag',   :mandatory=>false, :default=>nil,         :allowed=>nil,   :disallowed=>nil}
-
-bp = Biopieces.new
+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=>'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=>'clean',      :short=>'X', :type=>'flag',   :mandatory=>false, :default=>nil,     :allowed=>nil,   :disallowed=>nil}
 
-options = bp.parse(ARGV, casts)
+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?
@@ -165,27 +155,33 @@ raise ArgumentError, "kmer_min >= kmer_max: #{options[:kmer_min]} >= #{options[:
 
 Dir.mkdir(options[:directory]) unless Dir.exists?(options[:directory])
 
-file_fasta = [options[:directory], "sequence_in.fna"].join(File::SEPARATOR)
+file_fasta = File.join(options[:directory], "sequence_in.fna")
 
-Fasta.open(file_fasta, mode="w") do |fasta_io|
-  bp.each_record do |record|
-    fasta_io.puts record
-  end
-end
-
-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_velvetg(options[:cov_cutoff], options[:exp_cov])
-  file_contigs = velvet.pick_best_assembly
-
-  Fasta.open(file_contigs, mode="r") do |fasta_io|
-    fasta_io.each do |entry|
-      bp.puts entry.to_bp
+Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
+  Fasta.open(file_fasta, mode="w") do |fasta_io|
+    input.each_record do |record|
+      if record[:SEQ_NAME] and record[:SEQ]
+        seq = Seq.new_bp(record)
+        fasta_io.puts seq.to_fasta
+      end
     end
-  end
+       end
+
+       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_velvetg(options[:cov_cutoff], options[:exp_cov])
+               file_contigs = velvet.pick_best_assembly
+
+               Fasta.open(file_contigs, mode="r") do |fasta_io|
+                       fasta_io.each do |entry|
+                               output.puts entry.to_bp
+                       end
+               end
+       end
 end
 
+FileUtils.remove_entry_secure file_fasta
 FileUtils.remove_entry_secure options[:directory] if options[:clean]