From 9cbef4726264d5f04d5803a7551510e5304f13f8 Mon Sep 17 00:00:00 2001 From: martinahansen Date: Wed, 24 Aug 2011 21:22:48 +0000 Subject: [PATCH] worked on unit tests for sam.rb git-svn-id: http://biopieces.googlecode.com/svn/trunk@1500 74ccb610-7750-0410-82ae-013aeee3265d --- code_ruby/lib/maasha/sam.rb | 36 ++++++++++++++++++++----------- code_ruby/test/maasha/test_sam.rb | 13 +++++++++++ 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/code_ruby/lib/maasha/sam.rb b/code_ruby/lib/maasha/sam.rb index 0161cd7..b82c6b1 100644 --- a/code_ruby/lib/maasha/sam.rb +++ b/code_ruby/lib/maasha/sam.rb @@ -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 diff --git a/code_ruby/test/maasha/test_sam.rb b/code_ruby/test/maasha/test_sam.rb index 3671a8d..31cb59f 100755 --- a/code_ruby/test/maasha/test_sam.rb +++ b/code_ruby/test/maasha/test_sam.rb @@ -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 } -- 2.39.2