]> git.donarmstrong.com Git - biopieces.git/commitdiff
worked on unit tests for sam.rb
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Wed, 24 Aug 2011 21:22:48 +0000 (21:22 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Wed, 24 Aug 2011 21:22:48 +0000 (21:22 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1500 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/lib/maasha/sam.rb
code_ruby/test/maasha/test_sam.rb

index 0161cd7fee757a98194fdc53158811fe9e96ac7b..b82c6b1b2742f6f3757d1b90c8a598502ec9844b 100644 (file)
@@ -309,31 +309,41 @@ class Sam < Filesys
     raise SamError, "Bad mapq: #{mapq}" unless (0 .. 2**8 - 1).include? mapq
   end
 
-  # Method to check cigar.
+  # Method to check cigar string.
   def check_cigar(cigar, seq)
     raise SamError, "Bad cigar: #{cigar}" unless cigar =~ /^(\*|([0-9]+[MIDNSHPX=])+)$/
 
-    # Check cigar hard clipping only at ends.
+    unless cigar == '*'
+      check_cigar_hard_clip(cigar)
+      check_cigar_soft_clip(cigar)
+      check_cigar_seq_len(cigar, seq) unless seq == '*'
+    end
+  end
+
+  # Method to check cigar hard clipping only at ends.
+  def check_cigar_hard_clip(cigar)
     if cigar.gsub(/^[0-9]+H|[0-9]+H$/, "").match('H')
       raise SamError, "Bad cigar with internal H: #{cigar}"
     end
+  end
 
-    # Check cigar soft clipping only at ends or H.
+  # Method to check cigar soft clipping only at ends or H.
+  def check_cigar_soft_clip(cigar)
     if cigar.gsub(/^[0-9]+H|[0-9]+H$/, "").gsub(/^[0-9]+S|[0-9]+S$/, "").match('S')
       raise SamError, "Bad cigar with internal S: #{cigar}"
     end
+  end
 
-    # Check cigar length matches sequence length.
-    unless cigar == '*' or seq == '*'
-      cigar_len = 0
-      
-      cigar.scan(/([0-9]+)([MIDNSHPX=])/).each do |len, op|
-        cigar_len += len.to_i if op =~ /[MISX=]/
-      end
+  # Method to check cigar length matches sequence length.
+  def check_cigar_seq_len(cigar, seq)
+    cigar_len = 0
 
-      if cigar_len != seq.length
-        raise SamError, "cigar and sequence length mismatch: #{cigar_len} != #{seq.length}"
-      end
+    cigar.scan(/([0-9]+)([MIDNSHPX=])/).each do |len, op|
+      cigar_len += len.to_i if op =~ /[MISX=]/
+    end
+
+    if cigar_len != seq.length
+      raise SamError, "cigar and sequence length mismatch: #{cigar_len} != #{seq.length}"
     end
   end
 
index 3671a8dca2b72423016e6c8bb55d38b47eb4d5ce..31cb59f167b28f05f39d3c8ecdd9f9b4305df88d 100755 (executable)
@@ -381,6 +381,19 @@ class SamTest < Test::Unit::TestCase
     assert_nothing_raised { sam.each }
   end
 
+  def test_Sam_each_with_bad_cigar_soft_clip_raises
+    sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t1M1S1M\t*\t*\t\*\tA\t*\n"))
+    assert_raise(SamError) { sam.each }
+  end
+
+  def test_Sam_each_with_ok_cigar_soft_clip_dont_raise
+    sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t1S1M\t*\t*\t\*\tAA\t*\n"))
+    assert_nothing_raised { sam.each }
+
+    sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t1H1S1M\t*\t*\t\*\tAA\t*\n"))
+    assert_nothing_raised { sam.each }
+  end
+
   def test_Sam_each_with_bad_cigar_length_raise
     sam = Sam.new(StringIO.new("*\t*\t*\t*\t*\t6M\t*\t*\t\*\tAAAAA\t*\n"))
     assert_raise(SamError) { sam.each }