]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/Maasha/lib/seq.rb
added digest_seq Biopiece
[biopieces.git] / code_ruby / Maasha / lib / seq.rb
index 68d6bad7e727be263e81ab148cd29f515b49284f..8b98acee5d13bd1a4e344d37752f8b3569794033 100644 (file)
@@ -141,6 +141,8 @@ end
 class DigestError < StandardError; end
 
 class Digest
+  include Enumerable
+
   # Initialize a digest object with the following arguments:
   # - seq: A sequence object.
   # - pattern: A restriction enzyme recognition pattern.
@@ -149,42 +151,31 @@ class Digest
     @seq     = seq
     @pattern = disambiguate(pattern)
     @cut_pos = cut_pos
+    @offset  = 0
   end
 
-  # Method that returns a list of positions where
-  # a specified restriction enzyme will cut the sequence.
-  def positions
-    positions = []
-
-    @seq.seq.scan @pattern do |match|
+  # Method to get the next digestion product from a sequence.
+  def each
+    @seq.seq.scan @pattern do
       pos = $`.length + @cut_pos - 1
-
+     
       if pos >= 0 and pos < @seq.seq.length - 2
-        positions << pos
-      end
-    end
+        seq = Seq.new("#{@seq.seq_name}[#{@offset + 1}-#{pos + 1}]", @seq.seq[@offset .. pos], @seq.type)
 
-    positions
-  end
-
-  # Method that returns a list of strings with digestion produts
-  # from the digestion of a specified sequence with a specified 
-  # restriction enzyme.
-  def products
-    products = []
-
-    beg = 0
+        yield seq
+      end
 
-    self.positions.each do |pos|
-      products << @seq.seq[beg .. pos]
-      beg = pos + 1
+      @offset = pos + 1
     end
 
-    if beg < @seq.seq.length
-      products << @seq.seq[beg .. @seq.seq.length]
+    # Handle remaining sequence after last cut.
+    if @offset > 0 and @offset < @seq.seq.length
+      seq = Seq.new("#{@seq.seq_name}[#{@offset + 1}-#{@seq.seq.length}]", @seq.seq[@offset .. @seq.seq.length], @seq.type)
+
+      yield seq
     end
 
-    products
+    self # conventionally
   end
 
   private