]> git.donarmstrong.com Git - biopieces.git/blobdiff - bp_bin/shred_seq
refactoring of assemble_pairs
[biopieces.git] / bp_bin / shred_seq
index 6c1a8d0623adfefa5c288178297b73c2b79f7622..322916426e606dd11259eb6f34757486296e039d 100755 (executable)
@@ -29,8 +29,8 @@
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 
 
-require 'biopieces'
-require 'seq'
+require 'maasha/biopieces'
+require 'maasha/seq'
 require 'pp'
 
 class Seq
@@ -49,8 +49,9 @@ class Seq
     sum     = 0
     strand  = '+'
 
-    while (sum / self.seq.length) < max_cov
-      entry = get_random_seq(size, strand)
+    while coverage(sum) < max_cov
+      entry = self.subseq_rand(size)
+      entry.reverse!.complement! if strand == '-'
 
       if block_given?
         yield entry
@@ -68,25 +69,9 @@ class Seq
 
   private
 
-  # Method that picks a random subsequence of a given size from a sequence.
-  # The position of the subsequence is appended to the sequence name along
-  # with the strand. The sequence is reverse complemented in case of minus
-  # strand.
-  def get_random_seq(size, strand)
-    if self.seq.size - size == 0
-      start = 0
-    else
-      start = rand(self.seq.size - size)
-    end
-
-    stop     = start + size - 1
-    seq_name = self.seq_name + "[#{start + 1}-#{stop + 1}:#{strand}]"
-    seq      = self.seq[start .. stop]
-    entry    = Seq.new(seq_name, seq, 'dna')
-
-    entry.revcomp if strand == '-'
-
-    entry
+  # Method that returns the coverage of subsequences.
+  def coverage(sum)
+    sum / self.seq.length
   end
 end
 
@@ -94,20 +79,22 @@ casts = []
 casts << {:long=>'size',     :short=>'s', :type=>'uint', :mandatory=>true, :default=>500, :allowed=>nil, :disallowed=>'0'}
 casts << {:long=>'coverage', :short=>'c', :type=>'uint', :mandatory=>true, :default=>100, :allowed=>nil, :disallowed=>'0'}
 
-bp = Biopieces.new
+options = Biopieces.options_parse(ARGV, casts)
 
-options = bp.parse(ARGV, casts)
+Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
+  input.each_record do |record|
+    if record[:SEQ] and record[:SEQ].length >= options[:size]
+      entry      = Seq.new_bp(record)
+      entry.type = :dna
 
-bp.each_record do |record|
-  if record.has_key? :SEQ and record[:SEQ].length >= options[:size]
-    entry = Seq.new(record[:SEQ_NAME], record[:SEQ], record[:SCORES])
-
-    entry.shred(options[:size], options[:coverage]) do |subentry|
-      bp.puts subentry.to_bp
+      entry.shred(options[:size], options[:coverage]) do |subentry|
+        output.puts subentry.to_bp
+      end
     end
   end
 end
 
+
 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<