+++ /dev/null
-class BioPieces
- RECORD_DELIMITER = "\n---\n"
-
- class Record
- attr_reader :__hash__
-
- def initialize( data )
- @__hash__ = data
- end
-
- def method_missing( name, *args, &block )
- if args.empty? && @__hash__.has_key?( name ) then
- @__hash__[ name ]
- else
- super # get the original exception
- end
- end
-
- def to_s
- @__hash__.map { | key, value | "#{ key }: #{ value }" }.join( "\n" ) + RECORD_DELIMITER
- end
-
- def to_hash
- @__hash__.dup
- end
- end
-
- def self.file( path )
- bp = new( File.open( path ) )
- yield bp if block_given?
- bp
- ensure
- # if no block was given, the user wants the BioPieces instance
- # with a still open File so he can read it, so only close if a block
- # had been given.
- bp.close if block_given?
- end
-
- def initialize( stream )
- @stream = stream
- end
-
- def close
- @stream.close if @stream.respond_to? :close
- end
-
- def record_get
- return unless block = @stream.gets( RECORD_DELIMITER )
-
- block.chomp!( RECORD_DELIMITER )
- data = Hash[ *block.split( /: |\n/, 2 ) ]
- Record.new( data )
- end
-
- def each
- yield record_get until @stream.eof?
- self # not to have a messy return value
- end
-end
-
-
-__END__
+++ /dev/null
-require 'Maasha/lib/seq'
-require 'pp'
-
-class Match
- attr_accessor :q_beg, :s_beg, :len
-
- # Method to initialize a Match with query begin (q_beg) and subject begin (s_beg)
- # positions and the match length (len).
- def initialize( q_beg, s_beg, len )
- raise ArgumentError, "q_beg must be a positive integer." if q_beg < 0
- raise ArgumentError, "s_beg must be a positive integer." if s_beg < 0
- raise ArgumentError, "len must be a positive integer." if len <= 0
- @q_beg = q_beg
- @s_beg = s_beg
- @len = len
- end
-
- # Method to convert a match to a string.
- def to_s
- "q_beg: #{ @q_beg } s_beg: #{ @s_beg } len: #{ len }"
- end
-
- # Method to determine if a match is already included in an array of matches.
- def redundant?( matches )
- matches.each do |match|
- if self.q_beg >= match.q_beg and self.q_beg + self.len <= match.q_beg + match.len and
- self.s_beg >= match.s_beg and self.s_beg + self.len <= match.s_beg + match.len and
- return true
- end
- end
-
- return false
- 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 Seq
- # Method to create a word index from a sequence. The positions of all sequence words of
- # a given size are saved in a hash of arrays that is returned. Words containing N or n
- # are disregarded.
- def word_index( size )
- raise ArgumentError, "Size must be a positive integer." if size <= 0
-
- index = {}
-
- 0.upto( self.length - size ) do |i|
- word = self[ i .. i + size - 1 ].upcase
- if word.count( "N" ) == 0
- word = word.to_sym
-
- if index.include? word
- index[ word ] << i
- else
- index[ word ] = [ i ]
- end
- end
- end
-
- index
- end
-end
-
-
-class Matches
- attr_accessor :q_seq, :s_seq, :word_size
-
- # Method to initialize an object with two sequences and a word size as arguments.
- def initialize( q_seq, s_seq, word_size )
- raise ArgumentError, "q_seq is empty." if q_seq.empty?
- raise ArgumentError, "s_seq is empty." if s_seq.empty?
- raise ArgumentError, "word_size must be a positive integer." if word_size <= 0
- @q_seq = q_seq
- @s_seq = s_seq
- @word_size = word_size
- end
-
- # Locate all maximum expanded matches between two sequences and return
- # these in an array.
- def locate
- matches = []
-
- index = @q_seq.word_index( @word_size )
-
- 0.upto( @s_seq.length - @word_size ) do |s_beg|
- word = @s_seq[ s_beg .. s_beg + @word_size - 1 ].upcase
- if word.count( "N" ) == 0
- word = word.to_sym
-
- if index.include? word
- index[ word ].each do |q_beg|
- match = Match.new( q_beg, s_beg, word_size )
-
- unless match.redundant?( matches )
- match.expand( @q_seq, @s_seq )
-
- matches << match
- end
- end
- end
- end
- end
-
- matches
- end
-end
-
+++ /dev/null
-#!/usr/bin/env ruby
-
-require 'Maasha/lib/bp_optparse'
-require 'test/unit'
-require 'pp'
-
-class Test_bp_optparse < Test::Unit::TestCase
- def test_add_default_options
- new = BP_optparse.new()
- assert_equal( new.add_default_options.last[ :long ], "verbose" )
- end
-
- def test_check_duplicate_long
- new = BP_optparse.new( [ { :long => "long_option" }, { :long => "long_option" } ] )
-
- assert_raise( ArgumentError ) { new.check_duplicate_long }
- end
-
- def test_check_duplicate_short
- new = BP_optparse.new( [ { :short => "s" }, { :short => "s" } ] )
-
- assert_raise( ArgumentError ) { new.check_duplicate_short }
- end
-
- def test_compile_option_list_has_4_elements
- new = BP_optparse.new()
- new.add_default_options
- option_list = new.compile_option_list
- assert_equal( option_list.count, 4 )
- end
-
- def test_compile_option_list_last_is_verbose
- new = BP_optparse.new()
- new.add_default_options
- option_list = new.compile_option_list
- assert_equal( option_list.last.first, "--verbose" )
- end
-
- def test_parse_options
- n = BP_optparse.new()
- n.add_default_options
- option_list = n.compile_option_list
-
- options = n.parse_options( option_list )
-
- pp options
- end
-end
+++ /dev/null
-#!/usr/bin/env ruby
-
-require 'Maasha/lib/match'
-require 'test/unit'
-require 'pp'
-
-class TestMatch < Test::Unit::TestCase
- def test_to_s
- match = Match.new( 0, 1, 2 )
- assert_equal( "q_beg: 0 s_beg: 1 len: 2", match.to_s )
- end
-
- def test_redundant?
- match1 = Match.new( 0, 0, 2 )
- match2 = Match.new( 0, 0, 2 )
-
- matches = []
- matches << match1
-
- assert_equal( true, match2.redundant?( matches ) )
- end
-
- def test_not_redundant_1?
- match1 = Match.new( 0, 0, 2 )
- match2 = Match.new( 1, 1, 2 )
-
- matches = []
- matches << match1
-
- assert_equal( false, match2.redundant?( matches ) )
- end
-
- def test_not_redundant_2?
- match1 = Match.new( 1, 1, 2 )
- match2 = Match.new( 0, 0, 2 )
-
- matches = []
- matches << match1
-
- assert_equal( false, match2.redundant?( matches ) )
- end
-
- def test_expand_forward
- match = Match.new( 1, 1, 1 )
-
- class << match
- public :expand_forward
- end
-
- match.expand_forward( "ATCG", "ATCG" )
-
- assert_equal( 1, match.q_beg )
- assert_equal( 1, match.s_beg )
- assert_equal( 3, match.len )
- end
-
- def test_expand_backward
- match = Match.new( 1, 1, 1 )
-
- class << match
- public :expand_backward
- end
-
- match.expand_backward( "ATCG", "ATCG" )
-
- assert_equal( 0, match.q_beg )
- assert_equal( 0, match.s_beg )
- assert_equal( 2, match.len )
- end
-
- def test_expand
- match = Match.new( 1, 1, 2 )
-
- match.expand( "ATCG", "ATCG" )
-
- assert_equal( 0, match.q_beg )
- assert_equal( 0, match.s_beg )
- assert_equal( 4, match.len )
- end
-
- # Testing Seq#word_index
-
- def test_word_index_bad_arg
- seq = Seq.new( "ATCG" )
-
- assert_raise( ArgumentError ) { seq.word_index( 0 ) }
- assert_raise( ArgumentError ) { seq.word_index( -1 ) }
- end
-
- def test_word_index_simple
- seq = Seq.new( "ATCG" )
-
- index = seq.word_index( 2 )
-
- assert_equal( 0, index[ "AT".to_sym ].first )
- end
-
- def test_word_index_non_simple
- seq = Seq.new( "ATCGATCG" )
-
- index = seq.word_index( 2 )
-
- assert_equal( 4, index[ "AT".to_sym ].last )
- end
-
- def test_word_index_skip_Ns
- seq = Seq.new( "ATNG" )
-
- index = seq.word_index( 2 )
-
- assert_equal( nil, index[ "NG".to_sym ] )
- end
-
- # Testing Matches#find_matches
-
- def test_find_matches_1_match
- q_seq = Seq.new( "ATCG" )
- s_seq = Seq.new( "ATCG" )
-
- m = Matches.new( q_seq, s_seq, 2 )
- matches = m.locate
-
- assert_equal( 0, matches.first.q_beg )
- assert_equal( 0, matches.first.s_beg )
- assert_equal( 4, matches.first.len )
- end
-
- def test_find_matches_2_matches
- q_seq = Seq.new( "ATCG_GCTA" )
- s_seq = Seq.new( "GCTA.ATCG" )
-
- m = Matches.new( q_seq, s_seq, 2 )
- matches = m.locate
-
- assert_equal( 2, matches.count )
- assert_equal( 0, matches.first.s_beg )
- assert_equal( 5, matches.first.q_beg )
- assert_equal( 4, matches.first.len )
- assert_equal( 5, matches.last.s_beg )
- assert_equal( 0, matches.last.q_beg )
- assert_equal( 4, matches.last.len )
- end
-end
-