--- /dev/null
+class Match
+ attr_accessor :q_beg, :s_beg, :len
+
+ def initialize( q_beg, s_beg, len )
+ @q_beg = q_beg
+ @s_beg = s_beg
+ @len = len
+ end
+
+ # Method that expands a match forwards and backwards given two strings.
+ def expand( q_seq, s_seq )
+ self.expand_forward( q_seq, s_seq )
+ self.expand_backward( q_seq, s_seq )
+ end
+
+ # Protected method that expands a match forwards given two strings.
+ def expand_forward( q_seq, s_seq )
+ while @q_beg + @len < q_seq.length and @s_beg + @len < s_seq.length and q_seq[ @q_beg + @len + 1 ] == s_seq[ @s_beg + @len + 1 ]
+ @len += 1
+ end
+ end
+
+ # Protected method that expands a match backwards given two strings.
+ def expand_backward( q_seq, s_seq )
+ while @q_beg > 0 and @s_beg > 0 and q_seq[ @q_beg - 1 ] == s_seq[ @s_beg - 1 ]
+ @q_beg -= 1
+ @s_beg -= 1
+ @len += 1
+ end
+ end
+
+ protected :expand_forward, :expand_backward
+end
# Class containing generic sequence methods and nucleic acid and amino acid subclasses.
class Seq < String
+ attr_accessor :seq, :offset
+
+ def initialize ( seq = "", offset = 0 )
+ super( seq )
+ @offset = offset
+ end
+
# Guess the sequence type by analyzing the first 100 residues allowing for ambiguity codes.
def guess_type
raise ArgumentError, "No sequence." if self.empty?
self.replace( self.generate( length ) )
end
+ # Method that returns the next word from a given offset and size.
+ def next_word( size, step = 1 )
+ return nil if @offset + size > self.length
+ word = self[ @offset .. @offset + size - 1 ]
+ @offset += step
+
+ word
+ end
+
+ # Method that creates a list of words from a string.
+ def to_words( size, step = 1 )
+ words = []
+
+ while word = self.next_word( size, step )
+ words << word
+ end
+
+ words
+ end
+
# Class containing methods specific for amino acid (AA) sequences.
class AA < Seq
# Method that returns an array of amino acid residues.
--- /dev/null
+#!/usr/bin/env ruby
+
+require 'Maasha/lib/match'
+require 'test/unit'
+
+class TestMatch < Test::Unit::TestCase
+
+ def test_expand_forward
+ match = Match.new( 1, 1, 2 )
+
+ class << match
+ public :expand_forward
+ end
+
+ match.expand_forward( "ATCG", "ATCG" )
+
+ assert_equal( 3, match.len )
+ end
+
+ def test_expand_backward
+ match = Match.new( 1, 1, 2 )
+
+ class << match
+ public :expand_backward
+ end
+
+ match.expand_backward( "ATCG", "ATCG" )
+
+ assert_equal( 3, match.len )
+ end
+
+ def test_expand
+ match = Match.new( 1, 1, 2 )
+
+ match.expand( "ATCG", "ATCG" )
+
+ assert_equal( 4, match.len )
+ end
+end
require 'Maasha/lib/seq'
require 'test/unit'
+require 'pp'
class TestSeq < Test::Unit::TestCase
assert_equal( 40, s.length )
end
+ # Testing Seq#next_word
+
+ def test_next_word
+ s = Seq::NA::DNA.new( "ATCG" )
+
+ assert_equal( "AT", s.next_word( 2 ) )
+ end
+
+ # Testing Seq#to_words
+ def test_to_words
+ s = Seq::NA::DNA.new( "ATCG" )
+
+ assert_equal( "AT", s.to_words( 2 ).first )
+ end
+
# Testing Seq::AA#residues
def test_Seq_AA_residues