]> git.donarmstrong.com Git - biopieces.git/commitdiff
more ruby stuff
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Sun, 12 Jul 2009 20:52:48 +0000 (20:52 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Sun, 12 Jul 2009 20:52:48 +0000 (20:52 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@566 74ccb610-7750-0410-82ae-013aeee3265d

code_ruby/Maasha/lib/match.rb [new file with mode: 0644]
code_ruby/Maasha/lib/seq.rb
code_ruby/Maasha/test/test_match.rb [new file with mode: 0755]
code_ruby/Maasha/test/test_seq.rb

diff --git a/code_ruby/Maasha/lib/match.rb b/code_ruby/Maasha/lib/match.rb
new file mode 100644 (file)
index 0000000..2467b93
--- /dev/null
@@ -0,0 +1,33 @@
+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
index 26bfee3f7c9e04faeb301b3d211ebbd8d8db35a9..99f21244c5b42f524b21dbdf85849756e2104ce2 100644 (file)
@@ -1,5 +1,12 @@
 # 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?
@@ -43,6 +50,26 @@ class Seq < String
                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.
diff --git a/code_ruby/Maasha/test/test_match.rb b/code_ruby/Maasha/test/test_match.rb
new file mode 100755 (executable)
index 0000000..bec9e4f
--- /dev/null
@@ -0,0 +1,39 @@
+#!/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
index ce6f4664753bb16012eb972b2ab12739d466d008..39f4714523d7d879a573176f50e952c46be1fa74 100755 (executable)
@@ -2,6 +2,7 @@
 
 require 'Maasha/lib/seq'
 require 'test/unit'
+require 'pp'
 
 class TestSeq < Test::Unit::TestCase 
 
@@ -183,6 +184,21 @@ 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