From: martinahansen Date: Fri, 3 Jul 2009 14:40:18 +0000 (+0000) Subject: clearning ruby dir X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=b5a27ee6cfcd801325482ff0d1f4ccd8fbfb81db;p=biopieces.git clearning ruby dir git-svn-id: http://biopieces.googlecode.com/svn/trunk@552 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/code_ruby/Maasha/biopieces.rb b/code_ruby/Maasha/biopieces.rb deleted file mode 100755 index 61d9cd7..0000000 --- a/code_ruby/Maasha/biopieces.rb +++ /dev/null @@ -1,62 +0,0 @@ -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__ diff --git a/code_ruby/Maasha/lib/biopieces.rb b/code_ruby/Maasha/lib/biopieces.rb new file mode 100755 index 0000000..61d9cd7 --- /dev/null +++ b/code_ruby/Maasha/lib/biopieces.rb @@ -0,0 +1,62 @@ +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__ diff --git a/code_ruby/Maasha/lib/seq.rb b/code_ruby/Maasha/lib/seq.rb index ba79c85..f80d397 100644 --- a/code_ruby/Maasha/lib/seq.rb +++ b/code_ruby/Maasha/lib/seq.rb @@ -8,6 +8,11 @@ class Seq < String @seq_type = seq_type end + # Method for outputting sequence as a String. + def to_s + @seq + end + # Guess the sequence type by analyzing the first 100 residues allowing for ambiguity codes. def seq_type_guess seq_beg = @seq[ 0, 100 ].upcase diff --git a/code_ruby/Maasha/test/test_seq.rb b/code_ruby/Maasha/test/test_seq.rb index 7e8c89d..4297720 100755 --- a/code_ruby/Maasha/test/test_seq.rb +++ b/code_ruby/Maasha/test/test_seq.rb @@ -28,6 +28,12 @@ class TestSeq < Test::Unit::TestCase assert_equal( :DNA, s.seq_type ) end + # Testing Seq#to_s + def test_to_s + s = Seq.new( "ATCG" ) + assert_equal( "ATCG", s.to_s ) + end + # Testing Seq#seq_type_guess def test_seq_type_guess_DNA_uppercase @@ -150,19 +156,19 @@ class TestSeq < Test::Unit::TestCase def test_wrap_with_0_args s = Seq.new( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACGACTACGACT" ) - assert_equal( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACG\nACTACGACT", s.wrap ) + assert_equal( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACG\nACTACGACT", s.wrap.to_s ) end def test_wrap_with_1_args s = Seq.new( "ATCG" ) - assert_equal( "AT\nCG", s.wrap( 2 ) ) + assert_equal( "AT\nCG", s.wrap( 2 ).to_s ) end def test_wrap_with_2_args s = Seq.new( "ATCG" ) - assert_equal( "AT\rCG", s.wrap( 2, "\r" ) ) + assert_equal( "AT\rCG", s.wrap( 2, "\r" ).to_s ) end def test_wrap_dont_change_instance_var @@ -170,7 +176,7 @@ class TestSeq < Test::Unit::TestCase s.wrap( 2 ) - assert_equal( "ATCG", s.seq ) + assert_equal( "ATCG", s.to_s ) end # Testing Seq#wrap! @@ -180,7 +186,7 @@ class TestSeq < Test::Unit::TestCase s.wrap! - assert_equal( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACG\nACTACGACT", s.seq ) + assert_equal( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACG\nACTACGACT", s.to_s ) end def test_wrap_with_1_args! @@ -188,7 +194,7 @@ class TestSeq < Test::Unit::TestCase s.wrap!( 2 ) - assert_equal( "AT\nCG", s.seq ) + assert_equal( "AT\nCG", s.to_s ) end def test_wrap_with_2_args! @@ -196,7 +202,7 @@ class TestSeq < Test::Unit::TestCase s.wrap!( 2, "\r" ) - assert_equal( "AT\rCG", s.seq ) + assert_equal( "AT\rCG", s.to_s ) end # Testing Seq#generate @@ -227,7 +233,7 @@ class TestSeq < Test::Unit::TestCase seq = s.generate( 40 ) - assert_equal( "", s.seq ) + assert_equal( "", s.to_s ) end # Testing Seq#generate! @@ -235,11 +241,11 @@ class TestSeq < Test::Unit::TestCase def test_generate! s = Seq.new( "", :AA ) - gen_seq = s.generate!( 40 ) + gen_seq = s.generate!( 40 ).to_s assert_equal( 40, gen_seq.length ) assert_equal( 40, s.seq.length ) - assert_equal( gen_seq, s.seq ) + assert_equal( gen_seq, s.to_s ) end # Testing Seq::AA#initialize @@ -247,14 +253,14 @@ class TestSeq < Test::Unit::TestCase # test marked for deletion - too simple and not informative def test_Seq_AA_initialize_with_0_args s = Seq::AA.new - assert_equal( "", s.seq ) + assert_equal( "", s.to_s ) assert_equal( :AA, s.seq_type ) end # test marked for deletion - too simple and not informative def test_Seq_AA_initialize_with_1_args s = Seq::AA.new( "SEQ" ) - assert_equal( "SEQ", s.seq ) + assert_equal( "SEQ", s.to_s ) assert_equal( :AA, s.seq_type ) end end diff --git a/code_ruby/Maasha/test_module.rb b/code_ruby/Maasha/test_module.rb deleted file mode 100755 index a94e234..0000000 --- a/code_ruby/Maasha/test_module.rb +++ /dev/null @@ -1,7 +0,0 @@ -class Test - puts "Modules Test1 loaded" - - def greet_world - puts "Hello" - end -end