From: martinahansen Date: Sun, 12 Jul 2009 20:52:48 +0000 (+0000) Subject: more ruby stuff X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=c7646eddde8b81fe50450d7765568706dad23407;p=biopieces.git more ruby stuff git-svn-id: http://biopieces.googlecode.com/svn/trunk@566 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/code_ruby/Maasha/lib/match.rb b/code_ruby/Maasha/lib/match.rb new file mode 100644 index 0000000..2467b93 --- /dev/null +++ b/code_ruby/Maasha/lib/match.rb @@ -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 diff --git a/code_ruby/Maasha/lib/seq.rb b/code_ruby/Maasha/lib/seq.rb index 26bfee3..99f2124 100644 --- a/code_ruby/Maasha/lib/seq.rb +++ b/code_ruby/Maasha/lib/seq.rb @@ -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 index 0000000..bec9e4f --- /dev/null +++ b/code_ruby/Maasha/test/test_match.rb @@ -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 diff --git a/code_ruby/Maasha/test/test_seq.rb b/code_ruby/Maasha/test/test_seq.rb index ce6f466..39f4714 100755 --- a/code_ruby/Maasha/test/test_seq.rb +++ b/code_ruby/Maasha/test/test_seq.rb @@ -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