]> git.donarmstrong.com Git - biopieces.git/commitdiff
refactoring of ruby code converting sequences types to symbols
authormartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Fri, 8 Mar 2013 10:43:05 +0000 (10:43 +0000)
committermartinahansen <martinahansen@74ccb610-7750-0410-82ae-013aeee3265d>
Fri, 8 Mar 2013 10:43:05 +0000 (10:43 +0000)
git-svn-id: http://biopieces.googlecode.com/svn/trunk@2114 74ccb610-7750-0410-82ae-013aeee3265d

13 files changed:
bp_bin/blast_seq_pair
bp_bin/find_adaptor
bp_bin/patscan_seq
bp_bin/pcr_seq
bp_bin/shred_seq
code_ruby/lib/maasha/align.rb
code_ruby/lib/maasha/embl.rb
code_ruby/lib/maasha/genbank.rb
code_ruby/lib/maasha/locator.rb
code_ruby/lib/maasha/seq.rb
code_ruby/lib/maasha/seq/patscan.rb
code_ruby/test/maasha/test_locator.rb
code_ruby/test/maasha/test_seq.rb

index 1963eafc4b128d94579d6d02e86cddf14964962c..d612a6cdc06b7c3ead49bdae36945b5ae3f01e66 100755 (executable)
@@ -121,14 +121,14 @@ class Blast
   def program_choose(type1, type2)
     program = ""
 
-    if type1 == 'protein'
-      if type2 == 'protein'
+    if type1 == :protein
+      if type2 == :protein
         program = 'blastp'
       else
         program = 'tblastn'
       end
     else
-      if type2 == 'protein'
+      if type2 == :protein
         program = 'blastx'
       else
         program = 'blastn'
index ac3681a090c8e80c6139a4d1640ce4dd97664430..f2f593d1ce90f11087b04ca9d2abc8a58a40b264 100755 (executable)
@@ -53,11 +53,11 @@ casts << {:long=>'deletions',   :short=>'d', :type=>'uint',   :mandatory=>false,
 options = Biopieces.options_parse(ARGV, casts)
 
 if options[:forward_rc]
-  options[:forward] = Seq.new("test", options[:forward_rc], 'dna').reverse.complement.seq
+  options[:forward] = Seq.new("test", options[:forward_rc], :dna).reverse.complement.seq
 end
 
 if options[:reverse_rc]
-  options[:reverse] = Seq.new("test", options[:reverse_rc], 'dna').reverse.complement.seq
+  options[:reverse] = Seq.new("test", options[:reverse_rc], :dna).reverse.complement.seq
 end
 
 raise ArgumentError, "no adaptor specified" unless options[:forward] or options[:reverse]
index bc07fcaf792f832875079832132695ef7f3cd1b9..089954ddb6de2893d15e604fbf12ef073cc3099b 100755 (executable)
@@ -50,7 +50,7 @@ class Patscan
     args = []
     args << "scan_for_matches"
     args << "-c"            if comp
-    args << "-p"            if type == 'protein'
+    args << "-p"            if type == :protein
     args << "-n #{max_mis}" if max_mis
     args << "-m #{max_hit}" if max_hit
     args << @pat_file
index f6c874e7e9555e021c83f5213f6277e2f8f591ff..7e487f8df302c2bcfdb75b10216ef828a51d4f4f 100755 (executable)
@@ -145,7 +145,7 @@ class Pattern
 
     seq      = Seq.new
     seq.seq  = primer
-    seq.type = 'dna'
+    seq.type = :dna
     seq.reverse!.complement!
 
     descriptor ? seq.seq + descriptor : seq.seq
@@ -157,18 +157,18 @@ casts << {:long=>'forward',    :short=>'f', :type=>'string', :mandatory=>false,
 casts << {:long=>'forward_rc', :short=>'F', :type=>'string', :mandatory=>false, :default=>nil,  :allowed=>nil, :disallowed=>nil}
 casts << {:long=>'reverse',    :short=>'r', :type=>'string', :mandatory=>false, :default=>nil,  :allowed=>nil, :disallowed=>nil}
 casts << {:long=>'reverse_rc', :short=>'R', :type=>'string', :mandatory=>false, :default=>nil,  :allowed=>nil, :disallowed=>nil}
-casts << {:long=>'max_dist',   :short=>'m', :type=>'uint',   :mandatory=>true, :default=>5000, :allowed=>nil, :disallowed=>"0"}
+casts << {:long=>'max_dist',   :short=>'m', :type=>'uint',   :mandatory=>true,  :default=>5000, :allowed=>nil, :disallowed=>"0"}
 
 options = Biopieces.options_parse(ARGV, casts)
 tmpdir  = Biopieces.mktmpdir
 infile  = File.join(tmpdir, "in.fna")
 
 if options[:forward_rc]
-  options[:forward] = Seq.new("test", options[:forward_rc], 'dna').reverse.complement.seq
+  options[:forward] = Seq.new("test", options[:forward_rc], :dna).reverse.complement.seq
 end
 
 if options[:reverse_rc]
-  options[:reverse] = Seq.new("test", options[:reverse_rc], 'dna').reverse.complement.seq
+  options[:reverse] = Seq.new("test", options[:reverse_rc], :dna).reverse.complement.seq
 end
 
 raise ArgumentError, "no adaptor specified" unless options[:forward] or options[:reverse]
index ffd79cb9252bef5dbf2f95258779c1960ef41cf7..94d5835e53837f84b0e81aa8bffa9200feb557cf 100755 (executable)
@@ -85,7 +85,7 @@ Biopieces.open(options[:stream_in], options[:stream_out]) do |input, output|
   input.each_record do |record|
     if record[:SEQ] and record[:SEQ].length >= options[:size]
       entry      = Seq.new(record[:SEQ_NAME], record[:SEQ], record[:SCORES])
-      entry.type = 'dna'
+      entry.type = :dna
 
       entry.shred(options[:size], options[:coverage]) do |subentry|
         output.puts subentry.to_bp
index c139fe8d89c30a773df458f23e730d2a73e992c4..7beb765935456b8df7f544a807eb92d2b76065c5 100755 (executable)
@@ -251,7 +251,7 @@ class Align
       new_seq      = Seq.new
       new_seq.seq  = consensus_seq
       new_seq.qual = consensus_qual if @has_qual
-      new_seq.type = "dna"
+      new_seq.type = :dna
 
       new_seq
     end
index d08dce56e97d5e02392491bd49f18d4150d234ac..29c3eb66503fc805d2fb0fa108d3939bcb002761 100644 (file)
@@ -87,7 +87,7 @@ class EMBL < Filesys
 
     seq = @entry[j + 1 .. i].join.delete(" 0123456789")
 
-    Seq.new(nil, seq, "dna") if seq
+    Seq.new(nil, seq, :dna) if seq
   end
 
   # Method to get the base keys from EMBL entry and return these
index c3515ecbe326c03e65abb834b1ff84b5413dc0e2..1cf2038ff45d37c24aa788da47c790865590a7d3 100644 (file)
@@ -87,7 +87,7 @@ class Genbank < Filesys
 
     seq = @entry[j + 1 .. i].join.delete(" 0123456789")
 
-    Seq.new(nil, seq, "dna") if seq
+    Seq.new(nil, seq, :dna) if seq
   end
 
   # Method to get the base keys from Genbank entry and return these
index 5996663b0a4cf1c6137bbfd89e1a0d362572f0c5..638f473d7fdcebe23f13eb9d7d7da9d8dd9970c1 100644 (file)
@@ -35,7 +35,7 @@ class Locator
   def initialize(locator, seq)
     @locator = locator
     @seq     = seq
-    @subseq  = Seq.new(nil, "", "dna")
+    @subseq  = Seq.new(nil, "", :dna)
     parse_locator(locator)
   end
 
@@ -107,7 +107,7 @@ class Locator
           int_beg = $1.to_i - 1
           int_end = $2.to_i - 1
 
-          newseq = Seq.new(nil, @seq.seq[int_beg..int_end], "dna")
+          newseq = Seq.new(nil, @seq.seq[int_beg..int_end], :dna)
 
                                        unless newseq.seq.nil?
                                                newseq.reverse!.complement! if comp
@@ -117,7 +117,7 @@ class Locator
         when /^(\d+)$/
           pos = $1.to_i - 1
 
-          newseq = Seq.new(nil, @seq.seq[pos], "dna")
+          newseq = Seq.new(nil, @seq.seq[pos], :dna)
 
                                        unless newseq.seq.nil?
                newseq.reverse!.complement! if comp 
index cf4b6e6802883d5447c7bbad7157ae65bc0c95b1..92ce19edaee4a9176487272a8f1a89c32c7ee806 100644 (file)
@@ -87,7 +87,7 @@ class Seq
   def self.new_bp(record)
     seq_name = record[:SEQ_NAME]
     seq      = record[:SEQ]
-    type     = record[:SEQ_TYPE]
+    type     = record[:SEQ_TYPE].to_sym if record[:SEQ_TYPE]
     qual     = record[:SCORES]
 
     self.new(seq_name, seq, type, qual)
@@ -98,9 +98,9 @@ class Seq
     raise SeqError, "Cannot generate negative oligo length: #{length}" if length <= 0
 
     case type.downcase
-    when /dna/     then alph = DNA
-    when /rna/     then alph = RNA
-    when /protein/ then alph = PROTEIN
+    when :dna     then alph = DNA
+    when :rna     then alph = RNA
+    when :protein then alph = PROTEIN
     else
       raise SeqError, "Unknown sequence type: #{type}"
     end
@@ -140,9 +140,9 @@ class Seq
     raise SeqError, "Guess failed: sequence is nil" if self.seq.nil?
 
     case self.seq[0 ... 100].downcase
-    when /[flpqie]/ then return "protein"
-    when /[u]/      then return "rna"
-    else                 return "dna"
+    when /[flpqie]/ then return :protein
+    when /[u]/      then return :rna
+    else                 return :dna
     end
   end
 
@@ -190,24 +190,24 @@ class Seq
 
   # Method that returns true is a given sequence type is DNA.
   def is_dna?
-    self.type == 'dna'
+    self.type == :dna
   end
 
   # Method that returns true is a given sequence type is RNA.
   def is_rna?
-    self.type == 'rna'
+    self.type == :rna
   end
 
   # Method that returns true is a given sequence type is protein.
   def is_protein?
-    self.type == 'protein'
+    self.type == :protein
   end
 
   # Method to transcribe DNA to RNA.
   def to_rna
     raise SeqError, "Cannot transcribe 0 length sequence" if self.length == 0
     raise SeqError, "Cannot transcribe sequence type: #{self.type}" unless self.is_dna?
-    self.type = 'rna'
+    self.type = :rna
     self.seq.tr!('Tt','Uu')
   end
 
@@ -215,14 +215,13 @@ class Seq
   def to_dna
     raise SeqError, "Cannot reverse-transcribe 0 length sequence" if self.length == 0
     raise SeqError, "Cannot reverse-transcribe sequence type: #{self.type}" unless self.is_rna?
-
-    self.type = 'dna'
+    self.type = :dna
     self.seq.tr!('Uu','Tt')
   end
 
   # Method to translate a DNA sequence to protein.
   def translate!(trans_tab = 11)
-    raise SeqError, "Sequence type must be 'dna' - not #{self.type}" unless self.type == 'dna'
+    raise SeqError, "Sequence type must be 'dna' - not #{self.type}" unless self.type == :dna
     raise SeqError, "Sequence length must be a multiplum of 3 - was: #{self.length}" unless (self.length % 3) == 0
 
     case trans_tab
@@ -257,7 +256,7 @@ class Seq
 
     self.seq  = protein
     self.qual = nil
-    self.type = "protein"
+    self.type = :protein
 
     self
   end
@@ -392,20 +391,17 @@ class Seq
   def generate(length, type)
     raise SeqError, "Cannot generate sequence length < 1: #{length}" if length <= 0
 
-    case type.downcase
-    when "dna"
-      alph = DNA
-    when "rna"
-      alph = RNA
-    when "protein"
-      alph = PROTEIN
+    case type
+    when :dna     then alph = DNA
+    when :rna     then alph = RNA
+    when :protein then alph = PROTEIN
     else
       raise SeqError, "Unknown sequence type: #{type}"
     end
 
     seq_new   = Array.new(length) { alph[rand(alph.size)] }.join("")
     self.seq  = seq_new
-    self.type = type.downcase
+    self.type = type
     seq_new
   end
 
index 4e313980f0e195f3490646368b03b5d6d2f951e4..8cdfa8ec0cc54c6046940b6c1093189f16730d55 100644 (file)
@@ -153,13 +153,13 @@ module Patscan
 
   def pattern_disambiguate
     case self.type
-    when 'protein'
+    when :protein
       @pattern.gsub!('J', '[IFVLWMAGCY]')
       @pattern.gsub!('O', '[TSHEDQNKR]')
       @pattern.gsub!('B', '[DN]')
       @pattern.gsub!('Z', '[EQ]')
       @pattern.gsub!('X', '.')
-    when 'dna'
+    when :dna
       @pattern.gsub!('R', '[AG]')
       @pattern.gsub!('Y', '[CT]')
       @pattern.gsub!('S', '[GC]')
@@ -171,7 +171,7 @@ module Patscan
       @pattern.gsub!('D', '[AGT]')
       @pattern.gsub!('B', '[CGT]')
       @pattern.gsub!('N', '.')
-    when 'rna'
+    when :rna
     else
       raise SeqError "unknown sequence type: #{self.type}"
     end
index 052f779de88ed00582e71317199683bb40e9033b..dc0fe14ba20d7d7cf2b93d27c89f61710da00569 100755 (executable)
@@ -32,7 +32,7 @@ require 'test/helper'
 
 class TestLocator < Test::Unit::TestCase 
   def setup
-    @seq = Seq.new(nil, "tcatgatcaagatctaacagcagaagtacacttctattta", "dna")
+    @seq = Seq.new(nil, "tcatgatcaagatctaacagcagaagtacacttctattta", :dna)
   end
 
   test "#new with single position returns correctly" do
index 4a68d67b080b0ed220ae8fb18d7dfed83c1750fa..eab90cb758f1a95a4d1f71a968f5681f63728da7 100755 (executable)
@@ -35,11 +35,11 @@ class TestSeq < Test::Unit::TestCase
   end
 
   test "Seq.new_bp returns correctly" do
-    record = {:SEQ_NAME => "test", :SEQ => "ATCG", :SEQ_TYPE => "dna", :SCORES => "hhhh"}
+    record = {:SEQ_NAME => "test", :SEQ => "ATCG", :SEQ_TYPE => :dna, :SCORES => "hhhh"}
     seq    = Seq.new_bp(record)
     assert_equal("test", seq.seq_name)
     assert_equal("ATCG", seq.seq)
-    assert_equal("dna",  seq.type)
+    assert_equal(:dna,  seq.type)
     assert_equal("hhhh", seq.qual)
   end
 
@@ -48,7 +48,7 @@ class TestSeq < Test::Unit::TestCase
   end
 
   test "#is_dna? with dna sequence type returns true" do
-    @entry.type = 'dna'
+    @entry.type = :dna
     assert(@entry.is_dna? == true)
   end
 
@@ -57,7 +57,7 @@ class TestSeq < Test::Unit::TestCase
   end
 
   test "#is_rna? with rna sequence type returns true" do
-    @entry.type = 'rna'
+    @entry.type = :rna
     assert(@entry.is_rna? == true)
   end
 
@@ -66,7 +66,7 @@ class TestSeq < Test::Unit::TestCase
   end
 
   test "#is_protein? with protein sequence type returns true" do
-    @entry.type = 'protein'
+    @entry.type = :protein
     assert_equal(true, @entry.is_protein?)
   end
 
@@ -76,17 +76,17 @@ class TestSeq < Test::Unit::TestCase
 
   test "#type_guess with protein returns protein" do
     @entry.seq = 'atcatcrFgatcg'
-    assert_equal('protein', @entry.type_guess)
+    assert_equal(:protein, @entry.type_guess)
   end
 
   test "#type_guess with rna returns rna" do
     @entry.seq = 'atcatcrUgatcg'
-    assert_equal('rna', @entry.type_guess)
+    assert_equal(:rna, @entry.type_guess)
   end
 
   test "#type_guess with dna returns dna" do
     @entry.seq = 'atcatcgatcg'
-    assert_equal('dna', @entry.type_guess)
+    assert_equal(:dna, @entry.type_guess)
   end
 
   test "#type_guess! without sequence raises" do
@@ -96,19 +96,19 @@ class TestSeq < Test::Unit::TestCase
   test "#type_guess! with protein returns protein" do
     @entry.seq = 'atcatcrFgatcg'
     @entry.type_guess!
-    assert_equal('protein', @entry.type)
+    assert_equal(:protein, @entry.type)
   end
 
   test "#type_guess! with rna returns rna" do
     @entry.seq = 'atcatcrUgatcg'
     @entry.type_guess!
-    assert_equal('rna', @entry.type)
+    assert_equal(:rna, @entry.type)
   end
 
   test "#type_guess! with dna returns dna" do
     @entry.seq = 'atcatcgatcg'
     @entry.type_guess!
-    assert_equal('dna', @entry.type)
+    assert_equal(:dna, @entry.type)
   end
 
   test "#length returns corretly" do
@@ -122,51 +122,51 @@ class TestSeq < Test::Unit::TestCase
   end
 
   test "#to_rna with no sequence raises" do
-    @entry.type = 'dna'
+    @entry.type = :dna
     assert_raise(SeqError) { @entry.to_rna }
   end
 
   test "#to_rna with bad type raises" do
     @entry.seq  = 'ATCG'
-    @entry.type = 'rna'
+    @entry.type = :rna
     assert_raise(SeqError) { @entry.to_rna }
   end
 
   test "#to_rna transcribes correctly" do
     @entry.seq  = 'ATCGatcg'
-    @entry.type = 'dna'
+    @entry.type = :dna
     assert_equal("AUCGaucg", @entry.to_rna)
   end
 
   test "#to_rna changes entry type to rna" do
     @entry.seq  = 'ATCGatcg'
-    @entry.type = 'dna'
+    @entry.type = :dna
     @entry.to_rna
-    assert_equal("rna", @entry.type)
+    assert_equal(:rna, @entry.type)
   end
 
   test "#to_dna with no sequence raises" do
-    @entry.type = 'rna'
+    @entry.type = :rna
     assert_raise(SeqError) { @entry.to_dna }
   end
 
   test "#to_dna with bad type raises" do
     @entry.seq  = 'AUCG'
-    @entry.type = 'dna'
+    @entry.type = :dna
     assert_raise(SeqError) { @entry.to_dna }
   end
 
   test "#to_dna transcribes correctly" do
     @entry.seq  = 'AUCGaucg'
-    @entry.type = 'rna'
+    @entry.type = :rna
     assert_equal("ATCGatcg", @entry.to_dna)
   end
 
   test "#to_dna changes entry type to dna" do
     @entry.seq  = 'AUCGaucg'
-    @entry.type = 'rna'
+    @entry.type = :rna
     @entry.to_dna
-    assert_equal("dna", @entry.type)
+    assert_equal(:dna, @entry.type)
   end
 
   test "#to_bp returns correct record" do
@@ -249,19 +249,19 @@ class TestSeq < Test::Unit::TestCase
   end
 
   test "#complement with no sequence raises" do
-    @entry.type = 'dna'
+    @entry.type = :dna
     assert_raise(SeqError) { @entry.complement }
   end
 
   test "#complement with bad type raises" do
     @entry.seq  = 'ATCG'
-    @entry.type = 'protein'
+    @entry.type = :protein
     assert_raise(SeqError) { @entry.complement }
   end
 
   test "#complement for DNA is correct" do
     @entry.seq  = 'ATCGatcg'
-    @entry.type = 'dna'
+    @entry.type = :dna
     comp        = @entry.complement
     assert_equal("TAGCtagc", comp.seq)
     assert_equal("ATCGatcg", @entry.seq)
@@ -269,32 +269,32 @@ class TestSeq < Test::Unit::TestCase
 
   test "#complement for RNA is correct" do
     @entry.seq  = 'AUCGaucg'
-    @entry.type = 'rna'
+    @entry.type = :rna
     comp        = @entry.complement
     assert_equal("UAGCuagc", comp.seq)
     assert_equal("AUCGaucg", @entry.seq)
   end
 
   test "#complement! with no sequence raises" do
-    @entry.type = 'dna'
+    @entry.type = :dna
     assert_raise(SeqError) { @entry.complement! }
   end
 
   test "#complement! with bad type raises" do
     @entry.seq  = 'ATCG'
-    @entry.type = 'protein'
+    @entry.type = :protein
     assert_raise(SeqError) { @entry.complement! }
   end
 
   test "#complement! for DNA is correct" do
     @entry.seq  = 'ATCGatcg'
-    @entry.type = 'dna'
+    @entry.type = :dna
     assert_equal("TAGCtagc", @entry.complement!.seq)
   end
 
   test "#complement! for RNA is correct" do
     @entry.seq  = 'AUCGaucg'
-    @entry.type = 'rna'
+    @entry.type = :rna
     assert_equal("UAGCuagc", @entry.complement!.seq)
   end
 
@@ -306,8 +306,8 @@ class TestSeq < Test::Unit::TestCase
   end
 
   test "#generate with length < 1 raises" do
-    assert_raise(SeqError) { @entry.generate(-10, "dna") }
-    assert_raise(SeqError) { @entry.generate(0, "dna") }
+    assert_raise(SeqError) { @entry.generate(-10, :dna) }
+    assert_raise(SeqError) { @entry.generate(0, :dna) }
   end
 
   test "#generate with bad type raises" do
@@ -315,8 +315,8 @@ class TestSeq < Test::Unit::TestCase
   end
 
   test "#generate with ok type dont raise" do
-    %w[dna DNA rna RNA protein Protein].each do |type|
-      assert_nothing_raised { @entry.generate(10, type) }
+    %w[dna rna protein].each do |type|
+      assert_nothing_raised { @entry.generate(10, type.to_sym) }
     end
   end
 
@@ -335,15 +335,15 @@ class TestSeq < Test::Unit::TestCase
 
   test "#<< with different types raises" do
     @entry.seq = "atcg"
-    assert_raise(SeqError) { @entry << Seq.new("test", "atcg", "dna") }
+    assert_raise(SeqError) { @entry << Seq.new("test", "atcg", :dna) }
   end
 
   test "#<< with missing qual in one entry raises" do
     @entry.seq  = "atcg"
-    @entry.type = "dna"
-    assert_raise(SeqError) { @entry << Seq.new("test", "atcg", "dna", "IIII") }
+    @entry.type = :dna
+    assert_raise(SeqError) { @entry << Seq.new("test", "atcg", :dna, "IIII") }
     @entry.qual = "IIII"
-    assert_raise(SeqError) { @entry << Seq.new("test", "atcg", "dna") }
+    assert_raise(SeqError) { @entry << Seq.new("test", "atcg", :dna) }
   end
 
   test "#<< with nil qual in both entries dont raise" do
@@ -353,9 +353,9 @@ class TestSeq < Test::Unit::TestCase
 
   test "#<< with qual in both entries dont raise" do
     @entry.seq  = "atcg"
-    @entry.type = "dna"
+    @entry.type = :dna
     @entry.qual = "IIII"
-    assert_nothing_raised { @entry << Seq.new("test", "atcg", "dna", "IIII") }
+    assert_nothing_raised { @entry << Seq.new("test", "atcg", :dna, "IIII") }
   end
 
   test "#<< without qual returns correctly" do
@@ -366,9 +366,9 @@ class TestSeq < Test::Unit::TestCase
 
   test "#<< with qual returns correctly" do
     @entry.seq  = "atcg"
-    @entry.type = "dna"
+    @entry.type = :dna
     @entry.qual = "HHHH"
-    @entry <<  Seq.new("test", "ATCG", "dna", "IIII")
+    @entry <<  Seq.new("test", "ATCG", :dna, "IIII")
     assert_equal("atcgATCG", @entry.seq)
     assert_equal("HHHHIIII", @entry.qual)
   end