]> git.donarmstrong.com Git - biopieces.git/commitdiff
fixed alignment descriptors in sam.rb
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 6 Oct 2011 07:10:19 +0000 (07:10 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Thu, 6 Oct 2011 07:10:19 +0000 (07:10 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@1541 74ccb610-7750-0410-82ae-013aeee3265d

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

index 7f4712bf23082364139d76b7debd5c86cef98b5f..3bd71ac55e2408643ca10ba6d8f3c3ec2786d8e9 100644 (file)
@@ -88,15 +88,15 @@ class Sam < Filesys
   # Class method to convert a Biopiece record
   # into a SAM entry.
   def self.to_sam(bp)
-    "FISK"
+    "FISK" # FIXME
   end
 
   # Create alignment descriptors according to the KISS
   # format description:
   # http://code.google.com/p/biopieces/wiki/KissFormat
   def self.align_descriptors(sam)
-    offset     = 0
-    align      = []
+    offset = 0
+    align  = []
 
     # Insertions
     sam[:CIGAR].each do |len, op|
@@ -118,13 +118,12 @@ class Sam < Filesys
       if m =~ /\d+/      # Matches
         offset += m.to_i
       elsif m[0] == '^'  # Deletions
-        1.upto(m.size - 1).each do
-          nt = sam[:SEQ].seq[offset]
-
-          align << [offset, "#{nt}>-"]
-
-          deletions += 1
-          offset    += 1
+        m.each_char do |nt|
+          unless nt == '^'
+            align << [offset, "#{nt}>-"]
+            deletions += 1
+            offset    += 1
+          end
         end
       else               # Mismatches
         m.each_char do |nt|
index d02d710942680300290bddd3ac42ef35c249e766..06fcf25975d96ba929d67e2329ac77c5763732e7 100755 (executable)
@@ -355,7 +355,7 @@ class SamTest < Test::Unit::TestCase
       assert_equal("SAM", Sam.to_bp(s)[:REC_TYPE])
       assert_equal("ID00036734", Sam.to_bp(s)[:Q_ID])
       assert_equal("-", Sam.to_bp(s)[:STRAND])
-        assert_equal("gi48994873", Sam.to_bp(s)[:S_ID])
+      assert_equal("gi48994873", Sam.to_bp(s)[:S_ID])
       assert_equal(366089, Sam.to_bp(s)[:S_BEG])
       assert_equal(37, Sam.to_bp(s)[:MAPQ])
       assert_equal("37M1I62M", Sam.to_bp(s)[:CIGAR])
@@ -363,5 +363,45 @@ class SamTest < Test::Unit::TestCase
       assert_equal("37:->T", Sam.to_bp(s)[:ALIGN])
     end
   end
+
+  def test_Sam_to_bp_alignment_descriptor_without_mismatch_or_indel_returns_correctly
+    string = "q_id\t0\ts_id\t1000\t40\t10M\t*\t0\t0\tGTTCCGCTAT\t*\tXT:A:U\tNM:i:0\tX0:i:1\tX1:i:0\tXM:i:0\tXO:i:1\tXG:i:1\tMD:Z:10\n"
+
+    sam = Sam.new(StringIO.new(string))
+
+    sam.each do |s|
+      assert_equal(nil, Sam.to_bp(s)[:ALIGN])
+    end
+  end
+
+  def test_Sam_to_bp_alignment_descriptor_with_mismatches_returns_correctly
+    string = "q_id\t0\ts_id\t1000\t40\t10M\t*\t0\t0\tgTTCCGCTAt\t*\tXT:A:U\tNM:i:2\tX0:i:1\tX1:i:0\tXM:i:0\tXO:i:1\tXG:i:1\tMD:Z:0C8A\n"
+
+    sam = Sam.new(StringIO.new(string))
+
+    sam.each do |s|
+      assert_equal("0:C>g,9:A>t", Sam.to_bp(s)[:ALIGN])
+    end
+  end
+
+  def test_Sam_to_bp_alignment_descriptor_with_insertions_returns_correctly
+    string = "q_id\t0\ts_id\t1000\t40\t1I10M1I\t*\t0\t0\taGTTCCGCTATc\t*\tXT:A:U\tNM:i:2\tX0:i:1\tX1:i:0\tXM:i:0\tXO:i:1\tXG:i:1\tMD:Z:12\n"
+
+    sam = Sam.new(StringIO.new(string))
+
+    sam.each do |s|
+      assert_equal("0:->a,11:->c", Sam.to_bp(s)[:ALIGN])
+    end
+  end
+
+  def test_Sam_to_bp_alignment_descriptor_with_deletions_returns_correctly
+    string = "q_id\t0\ts_id\t1000\t40\t2D10M\t*\t0\t0\tGTTCCGCTAT\t*\tXT:A:U\tNM:i:2\tX0:i:1\tX1:i:0\tXM:i:0\tXO:i:1\tXG:i:1\tMD:Z:^AC10\n"
+
+    sam = Sam.new(StringIO.new(string))
+
+    sam.each do |s|
+      assert_equal("0:A>-,1:C>-", Sam.to_bp(s)[:ALIGN])
+    end
+  end
 end