From: martinahansen Date: Thu, 6 Oct 2011 07:10:19 +0000 (+0000) Subject: fixed alignment descriptors in sam.rb X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=ec5a950facae93989c8448f0b9b75e4dafce21ef;p=biopieces.git fixed alignment descriptors in sam.rb git-svn-id: http://biopieces.googlecode.com/svn/trunk@1541 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/code_ruby/lib/maasha/sam.rb b/code_ruby/lib/maasha/sam.rb index 7f4712b..3bd71ac 100644 --- a/code_ruby/lib/maasha/sam.rb +++ b/code_ruby/lib/maasha/sam.rb @@ -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| diff --git a/code_ruby/test/maasha/test_sam.rb b/code_ruby/test/maasha/test_sam.rb index d02d710..06fcf25 100755 --- a/code_ruby/test/maasha/test_sam.rb +++ b/code_ruby/test/maasha/test_sam.rb @@ -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