From: martinahansen Date: Tue, 17 Aug 2010 13:48:11 +0000 (+0000) Subject: replacing read_fasta with ruby version X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=c372ef9ebcf86bde50fea70f9f92bf8ddb9d761a;p=biopieces.git replacing read_fasta with ruby version git-svn-id: http://biopieces.googlecode.com/svn/trunk@1062 74ccb610-7750-0410-82ae-013aeee3265d --- diff --git a/code_ruby/Maasha/lib/biopieces.rb b/code_ruby/Maasha/lib/biopieces.rb index 3bc2cc2..05bc736 100644 --- a/code_ruby/Maasha/lib/biopieces.rb +++ b/code_ruby/Maasha/lib/biopieces.rb @@ -474,24 +474,31 @@ class Status def set time0 = Time.new.strftime("%Y-%m-%d %X") - File.open(path, mode="w") { |file| file.puts [time0, ARGV.join(" ")].join(";") } + File.open(path, mode="w") do |fh| + fh.puts [time0, ARGV.join(" ")].join(";") + end end # Append the a temporary directory path to the status file. - def set_tmpdir(tmp_path) - status = File.open(path, mode="r").read.chomp + def set_tmpdir(tmpdir_path) + File.open(path, mode="r") do |fh| + fh.read.chomp + end File.open(path, mode="w") do |fh| - fh << [status, tmp_path].join(";") + "\n" + fh << [status, tmpdir_path].join(";") + "\n" end end # Extract the temporary directory path from the status file, # and return this or nil if not found. def get_tmpdir - tmp_path = File.open(path, mode="r").read.chomp.split(";").last + File.open(path, mode="r") do |fh| + tmpdir_path = fh.read.chomp.split(";").last + return tmpdir_path if File.directory?(tmpdir_path) + end - File.directory?(tmp_path) ? tmp_path : nil + nil end # Write the Biopiece status to the log file. diff --git a/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000001.html b/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000001.html index 5da85bc..7e9371a 100644 --- a/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000001.html +++ b/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000001.html @@ -7,7 +7,7 @@ -
# File biopieces.rb, line 19
+  
# File biopieces.rb, line 43
   def initialize(test=nil, input=STDIN, output=STDOUT)
     @test   = test
     @input  = input
diff --git a/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000002.html b/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000002.html
index 38f8dca..b69e120 100644
--- a/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000002.html
+++ b/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000002.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 30
+  
# File biopieces.rb, line 54
   def parse(argv, cast_list=[], script_path=$0)
     casts          = Casts.new(cast_list)
     option_handler = OptionHandler.new(argv, casts, script_path, @test)
diff --git a/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000003.html b/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000003.html
index c1a10dd..2675d14 100644
--- a/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000003.html
+++ b/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000003.html
@@ -7,9 +7,10 @@
   
 
 
-  
# File biopieces.rb, line 38
+  
# File biopieces.rb, line 62
   def each_record
     @in = Stream::open(@options, mode="r", @input) unless @in.is_a? IO
+    return if @in.nil?
 
     record = {}
 
diff --git a/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000005.html b/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000005.html
index 441f0ea..cb2274b 100644
--- a/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000005.html
+++ b/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000005.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 63
+  
# File biopieces.rb, line 88
   def puts(record)
     @out = Stream::open(@options, mode="w", @output) unless @out.is_a? IO
 
diff --git a/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000006.html b/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000006.html
index 8c7cf6f..9e4ff5e 100644
--- a/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000006.html
+++ b/code_ruby/Maasha/lib/doc/classes/Biopieces.src/M000006.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 74
+  
# File biopieces.rb, line 99
   def mktmpdir
     time = Time.now.to_i
     user = ENV["USER"]
diff --git a/code_ruby/Maasha/lib/doc/classes/Casts.src/M000007.html b/code_ruby/Maasha/lib/doc/classes/Casts.src/M000007.html
index 163a6aa..8ae5d3f 100644
--- a/code_ruby/Maasha/lib/doc/classes/Casts.src/M000007.html
+++ b/code_ruby/Maasha/lib/doc/classes/Casts.src/M000007.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 100
+  
# File biopieces.rb, line 125
   def initialize(cast_list=[])
     @cast_list = cast_list
     ubiquitous
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000008.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000008.html
index 5a62bc6..5a296d0 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000008.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000008.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 225
+  
# File biopieces.rb, line 250
   def initialize(argv, casts, script_path, test=nil)
     @argv        = argv
     @casts       = casts
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000009.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000009.html
index 07ba638..97be195 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000009.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000009.html
@@ -7,32 +7,33 @@
   
 
 
-  
# File biopieces.rb, line 235
+  
# File biopieces.rb, line 260
   def options_parse
     @options = {}
 
     option_parser = OptionParser.new do |option|
       @casts.each do |cast|
-        if cast[:type] == 'flag'
+        case cast[:type]
+        when 'flag'
           option.on("-#{cast[:short]}", "--#{cast[:long]}") do |o|
             @options[cast[:long]] = o
           end
-        elsif cast[:type] =~ REGEX_LIST
+        when 'float'
+          option.on("-#{cast[:short]}", "--#{cast[:long]} F", Float) do |f|
+            @options[cast[:long]] = f
+          end
+        when REGEX_LIST
           option.on( "-#{cast[:short]}", "--#{cast[:long]} A", Array) do |a|
             @options[cast[:long]] = a
           end
-        elsif cast[:type] =~ REGEX_INT
+        when REGEX_INT
           option.on("-#{cast[:short]}", "--#{cast[:long]} I", Integer) do |i|
             @options[cast[:long]] = i
           end
-        elsif cast[:type] =~ REGEX_STRING
+        when REGEX_STRING
           option.on("-#{cast[:short]}", "--#{cast[:long]} S", String) do |s|
             @options[cast[:long]] = s
           end
-        elsif cast[:type] == 'float'
-          option.on("-#{cast[:short]}", "--#{cast[:long]} F", Float) do |f|
-            @options[cast[:long]] = f
-          end
         else
           raise ArgumentError, "Unknown option type: '#{cast[:type]}'"
         end
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000010.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000010.html
index 92efe29..f8f6991 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000010.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000010.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 282
+  
# File biopieces.rb, line 308
   def wiki_path
     path = ENV["BP_DIR"] + "/bp_usage/" + File.basename(@script_path) + ".wiki"
     raise "No such wiki file: #{path}" unless File.file? path
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000011.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000011.html
index 9ce7ab3..fc4110d 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000011.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000011.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 289
+  
# File biopieces.rb, line 315
   def print_usage_full?
     @options["help"]
   end
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000012.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000012.html index f8d892f..454bef7 100644 --- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000012.html +++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000012.html @@ -7,7 +7,7 @@ -
# File biopieces.rb, line 294
+  
# File biopieces.rb, line 320
   def print_usage_short?
     if not $stdin.tty?
       return false
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000013.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000013.html
index 9da1eed..859c8a1 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000013.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000013.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 311
+  
# File biopieces.rb, line 337
   def print_usage_and_exit(full=nil)
     if @test
       return
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000014.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000014.html
index e1c0dfb..6666325 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000014.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000014.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 328
+  
# File biopieces.rb, line 354
   def options_default
     @casts.each do |cast|
       if cast[:default]
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000015.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000015.html
index d7730e8..44dfb09 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000015.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000015.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 338
+  
# File biopieces.rb, line 364
   def options_glob
     @casts.each do |cast|
       if cast[:type] == 'files' or cast[:type] == 'files!'
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000016.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000016.html
index b8bcc88..59771a2 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000016.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000016.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 361
+  
# File biopieces.rb, line 387
   def options_check
     @casts.each do |cast|
       options_check_mandatory(cast)
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000017.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000017.html
index 10fb109..f6c78dd 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000017.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000017.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 375
+  
# File biopieces.rb, line 401
   def options_check_mandatory(cast)
     if cast[:mandatory]
       raise ArgumentError, "Mandatory argument: --#{cast[:long]}" unless @options.has_key? cast[:long]
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000018.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000018.html
index dfd342b..91480ab 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000018.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000018.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 382
+  
# File biopieces.rb, line 408
   def options_check_int(cast)
     if cast[:type] == 'int' and @options.has_key? cast[:long]
       unless @options[cast[:long]].is_a? Integer
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000019.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000019.html
index 601ea2c..2bae214 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000019.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000019.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 391
+  
# File biopieces.rb, line 417
   def options_check_uint(cast)
     if cast[:type] == 'uint' and @options.has_key? cast[:long]
       unless @options[cast[:long]].is_a? Integer and @options[cast[:long]] >= 0
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000020.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000020.html
index 76b9b83..c4ad6f1 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000020.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000020.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 400
+  
# File biopieces.rb, line 426
   def options_check_file(cast)
     if cast[:type] == 'file!' and @options.has_key? cast[:long]
       raise ArgumentError, "No such file: '#{@options[cast[:long]]}'" unless File.file? @options[cast[:long]]
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000021.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000021.html
index 500f639..12c6e7b 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000021.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000021.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 407
+  
# File biopieces.rb, line 433
   def options_check_files(cast)
     if cast[:type] == 'files!' and @options.has_key? cast[:long]
       @options[cast[:long]].each do |path|
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000022.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000022.html
index 7ffff36..b7399d7 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000022.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000022.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 416
+  
# File biopieces.rb, line 442
   def options_check_dir(cast)
     if cast[:type] == 'dir!' and @options.has_key? cast[:long]
       raise ArgumentError, "No such directory: '#{@options[cast[:long]]}'" unless File.directory? @options[cast[:long]]
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000023.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000023.html
index 0ffe185..de6439e 100644
--- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000023.html
+++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000023.html
@@ -7,13 +7,13 @@
   
 
 
-  
# File biopieces.rb, line 423
+  
# File biopieces.rb, line 449
   def options_check_allowed(cast)
     if cast[:allowed] and @options.has_key? cast[:long]
       allowed_hash = {}
-      cast[:allowed].split(',').each { |a| allowed_hash[a] = 1 }
+      cast[:allowed].split(',').each { |a| allowed_hash[a.to_s] = 1 }
   
-      raise ArgumentError, "Argument '#{@options[cast[:long]]}' to --#{cast[:long]} not allowed" unless allowed_hash.has_key? @options[cast[:long]]
+      raise ArgumentError, "Argument '#{@options[cast[:long]]}' to --#{cast[:long]} not allowed" unless allowed_hash.has_key? @options[cast[:long]].to_s
     end
   end
diff --git a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000024.html b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000024.html index db4ee6b..5bbf893 100644 --- a/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000024.html +++ b/code_ruby/Maasha/lib/doc/classes/OptionHandler.src/M000024.html @@ -7,11 +7,11 @@ -
# File biopieces.rb, line 433
+  
# File biopieces.rb, line 459
   def options_check_disallowed(cast)
     if cast[:disallowed] and @options.has_key? cast[:long]
       cast[:disallowed].split(',').each do |val|
-        raise ArgumentError, "Argument '#{@options[cast[:long]]}' to --#{cast[:long]} is disallowed" if val == @options[cast[:long]]
+        raise ArgumentError, "Argument '#{@options[cast[:long]]}' to --#{cast[:long]} is disallowed" if val.to_s == @options[cast[:long]].to_s
       end
     end
   end
diff --git a/code_ruby/Maasha/lib/doc/classes/Seq.html b/code_ruby/Maasha/lib/doc/classes/Seq.html index 3f9c2ae..2617168 100644 --- a/code_ruby/Maasha/lib/doc/classes/Seq.html +++ b/code_ruby/Maasha/lib/doc/classes/Seq.html @@ -70,7 +70,7 @@ Parent: - String + Object @@ -83,14 +83,6 @@
-
-

-Class containing generic sequence methods and nucleic acid and amino acid -subclasses. -

- -
-
@@ -99,15 +91,25 @@ subclasses.
- generate   + complement   + + generate   + + is_dna   + + is_protein   + + is_rna   - generate!   + len   - guess_type   + length   - wrap   + to_bp   - wrap!   + to_dna   + + to_rna  
@@ -118,15 +120,49 @@ subclasses.
-
-

Classes and Modules

- Class Seq::AA
-Class Seq::NA
-
+
+

Attributes

+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
qual [RW] 
seq [RW] 
seq_name [RW] 
type [RW] 
+
+
@@ -136,15 +172,39 @@ Class Seq::NA

Public Instance methods

-
- +
+ + + + +
+ +

+Method that complements sequence including ambiguity codes. +

+ +
+
+ + +
+
- + - generate( length ) + generate(length,type) @@ -160,6 +220,89 @@ Method that generates a random sequence of a given length.
+
+ + + + +
+ +

+Method that returns true is a given sequence type is DNA. +

+ +
+
+ + +
+ + + + +
+ +
+
+ + +
+ + + + +
+ +
+
+ + +
+ + +
+ + len() + +
+ +
+ +

+Alias for length +

+ +
+
+ +
@@ -168,7 +311,7 @@ Method that generates a random sequence of a given length. - generate!( length ) + length() @@ -176,24 +319,19 @@ Method that generates a random sequence of a given length.
-

-Method that replaces sequence with a randomly generated sequence of a given -length. -

-
-
- +
+
- + - guess_type() + to_bp() @@ -202,23 +340,23 @@ length.

-Guess the sequence type by analyzing the first 100 residues allowing for -ambiguity codes. +Method that given a Seq entry returns a Biopieces record (a hash).

-
- +
+
- + - wrap( width = 80, delimit = "\n" ) + to_dna() @@ -227,22 +365,22 @@ ambiguity codes.

-Method to wrap a sequence to a given width using a given delimiter. +Method to reverse-transcribe RNA to DNA.

-
- +
+
- + - wrap!( width = 80, delimit = "\n" ) + to_rna() @@ -251,8 +389,7 @@ Method to wrap a sequence to a given width using a given delimiter.

-Method to wrap and replace a sequence to a given width using a given -delimiter. +Method to transcribe DNA to RNA.

diff --git a/code_ruby/Maasha/lib/doc/classes/Seq.src/M000035.html b/code_ruby/Maasha/lib/doc/classes/Seq.src/M000035.html index 8340c9a..9817e7f 100644 --- a/code_ruby/Maasha/lib/doc/classes/Seq.src/M000035.html +++ b/code_ruby/Maasha/lib/doc/classes/Seq.src/M000035.html @@ -2,14 +2,14 @@ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - generate! (Seq) + length (Seq) -
# File seq.rb, line 40
-        def generate!( length )
-                self.replace( self.generate( length ) )
-        end
+
# File seq.rb, line 11
+  def length
+    self.seq.nil? ? 0 : self.seq.length
+  end
diff --git a/code_ruby/Maasha/lib/doc/classes/Status.html b/code_ruby/Maasha/lib/doc/classes/Status.html index 2ca780f..d6681d4 100644 --- a/code_ruby/Maasha/lib/doc/classes/Status.html +++ b/code_ruby/Maasha/lib/doc/classes/Status.html @@ -236,7 +236,7 @@ Write the status to a status file. - set_tmpdir(tmp_path) + set_tmpdir(tmpdir_path) diff --git a/code_ruby/Maasha/lib/doc/classes/Status.src/M000025.html b/code_ruby/Maasha/lib/doc/classes/Status.src/M000025.html index 5b625be..84e53d7 100644 --- a/code_ruby/Maasha/lib/doc/classes/Status.src/M000025.html +++ b/code_ruby/Maasha/lib/doc/classes/Status.src/M000025.html @@ -7,11 +7,13 @@ -
# File biopieces.rb, line 448
+  
# File biopieces.rb, line 474
   def set
     time0  = Time.new.strftime("%Y-%m-%d %X")
 
-    File.open(path, mode="w") { |file| file.puts [time0, ARGV.join(" ")].join(";") }
+    File.open(path, mode="w") do |fh|
+      fh.puts [time0, ARGV.join(" ")].join(";")
+    end
   end
diff --git a/code_ruby/Maasha/lib/doc/classes/Status.src/M000026.html b/code_ruby/Maasha/lib/doc/classes/Status.src/M000026.html index 2951cbb..3a81609 100644 --- a/code_ruby/Maasha/lib/doc/classes/Status.src/M000026.html +++ b/code_ruby/Maasha/lib/doc/classes/Status.src/M000026.html @@ -7,12 +7,14 @@ -
# File biopieces.rb, line 455
-  def set_tmpdir(tmp_path)
-    status = File.open(path, mode="r").read.chomp
+  
# File biopieces.rb, line 483
+  def set_tmpdir(tmpdir_path)
+    File.open(path, mode="r") do |fh|
+      fh.read.chomp
+    end
 
     File.open(path, mode="w") do |fh|
-      fh << [status, tmp_path].join(";") + "\n"
+      fh << [status, tmpdir_path].join(";") + "\n"
     end
   end
diff --git a/code_ruby/Maasha/lib/doc/classes/Status.src/M000027.html b/code_ruby/Maasha/lib/doc/classes/Status.src/M000027.html index cf79dfc..a78ea57 100644 --- a/code_ruby/Maasha/lib/doc/classes/Status.src/M000027.html +++ b/code_ruby/Maasha/lib/doc/classes/Status.src/M000027.html @@ -7,11 +7,14 @@ -
# File biopieces.rb, line 465
+  
# File biopieces.rb, line 495
   def get_tmpdir
-    tmp_path = File.open(path, mode="r").read.chomp.split(";").last
+    File.open(path, mode="r") do |fh|
+      tmpdir_path = fh.read.chomp.split(";").last
+      return tmpdir_path if File.directory?(tmpdir_path)
+    end
 
-    File.directory?(tmp_path) ? tmp_path : nil
+    nil
   end
diff --git a/code_ruby/Maasha/lib/doc/classes/Status.src/M000028.html b/code_ruby/Maasha/lib/doc/classes/Status.src/M000028.html index fea2bcb..4f73b4e 100644 --- a/code_ruby/Maasha/lib/doc/classes/Status.src/M000028.html +++ b/code_ruby/Maasha/lib/doc/classes/Status.src/M000028.html @@ -7,7 +7,7 @@ -
# File biopieces.rb, line 472
+  
# File biopieces.rb, line 505
   def log(exit_status)
     time1   = Time.new.strftime("%Y-%m-%d %X")
     user    = ENV["USER"]
diff --git a/code_ruby/Maasha/lib/doc/classes/Status.src/M000029.html b/code_ruby/Maasha/lib/doc/classes/Status.src/M000029.html
index 397c4bd..2e74133 100644
--- a/code_ruby/Maasha/lib/doc/classes/Status.src/M000029.html
+++ b/code_ruby/Maasha/lib/doc/classes/Status.src/M000029.html
@@ -7,7 +7,7 @@
   
 
 
-  
# File biopieces.rb, line 489
+  
# File biopieces.rb, line 522
   def delete
     File.delete(path)
   end
diff --git a/code_ruby/Maasha/lib/doc/classes/Stream.src/M000030.html b/code_ruby/Maasha/lib/doc/classes/Stream.src/M000030.html index 55b2a87..9a8cbd8 100644 --- a/code_ruby/Maasha/lib/doc/classes/Stream.src/M000030.html +++ b/code_ruby/Maasha/lib/doc/classes/Stream.src/M000030.html @@ -7,7 +7,7 @@ -
# File biopieces.rb, line 514
+  
# File biopieces.rb, line 547
   def self.open(options, mode, stdio)
     if mode == "r"
       $stdin.tty? ? read(options["stream_in"]) : stdio
diff --git a/code_ruby/Maasha/lib/doc/created.rid b/code_ruby/Maasha/lib/doc/created.rid
index b6664bf..90dc268 100644
--- a/code_ruby/Maasha/lib/doc/created.rid
+++ b/code_ruby/Maasha/lib/doc/created.rid
@@ -1 +1 @@
-Fri, 06 Aug 2010 15:47:42 +0200
+Tue, 17 Aug 2010 15:43:49 +0200
diff --git a/code_ruby/Maasha/lib/doc/files/biopieces_rb.html b/code_ruby/Maasha/lib/doc/files/biopieces_rb.html
index e396f11..0efeadc 100644
--- a/code_ruby/Maasha/lib/doc/files/biopieces_rb.html
+++ b/code_ruby/Maasha/lib/doc/files/biopieces_rb.html
@@ -53,7 +53,7 @@
     
     
       Last Update:
-      2010-08-06 13:58:09 +0200
+      2010-08-17 14:23:28 +0200
     
     
   
@@ -63,6 +63,13 @@
+
+

+Copyright (C) 2007-2010 Martin A. Hansen. +

+ +
+

Required files

diff --git a/code_ruby/Maasha/lib/doc/files/seq_rb.html b/code_ruby/Maasha/lib/doc/files/seq_rb.html index 2d6aa87..e080b99 100644 --- a/code_ruby/Maasha/lib/doc/files/seq_rb.html +++ b/code_ruby/Maasha/lib/doc/files/seq_rb.html @@ -53,7 +53,7 @@ Last Update: - 2009-08-10 09:00:55 +0200 + 2010-08-17 08:33:38 +0200
@@ -63,14 +63,6 @@
-
-

-Class containing generic sequence methods and nucleic acid and amino acid -subclasses. -

- -
-
@@ -80,6 +72,37 @@ subclasses.
+
+

Constants

+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
DNA=%w[a t c g]
RNA=%w[a u c g]
PROTEIN=%w[f l s y c w p h q r i m t n k v a d e g]
+
+
+ diff --git a/code_ruby/Maasha/lib/doc/fr_class_index.html b/code_ruby/Maasha/lib/doc/fr_class_index.html index 6cdfd54..7fc7d71 100644 --- a/code_ruby/Maasha/lib/doc/fr_class_index.html +++ b/code_ruby/Maasha/lib/doc/fr_class_index.html @@ -23,17 +23,15 @@ Casts
- OptionHandler
- - Seq
+ Fasta
- Seq::AA
+ FastaError
- Seq::NA
+ OptionHandler
- Seq::NA::DNA
+ Seq
- Seq::NA::RNA
+ SeqError
Status
diff --git a/code_ruby/Maasha/lib/doc/fr_file_index.html b/code_ruby/Maasha/lib/doc/fr_file_index.html index e78e9be..fd47302 100644 --- a/code_ruby/Maasha/lib/doc/fr_file_index.html +++ b/code_ruby/Maasha/lib/doc/fr_file_index.html @@ -19,6 +19,8 @@ biopieces.rb
+ fasta.rb
+ seq.rb
diff --git a/code_ruby/Maasha/lib/doc/fr_method_index.html b/code_ruby/Maasha/lib/doc/fr_method_index.html index 3834175..5a0e1e9 100644 --- a/code_ruby/Maasha/lib/doc/fr_method_index.html +++ b/code_ruby/Maasha/lib/doc/fr_method_index.html @@ -17,38 +17,48 @@

Methods

diff --git a/code_ruby/Maasha/lib/fasta.rb b/code_ruby/Maasha/lib/fasta.rb new file mode 100644 index 0000000..23bad26 --- /dev/null +++ b/code_ruby/Maasha/lib/fasta.rb @@ -0,0 +1,87 @@ +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +# http://www.gnu.org/copyleft/gpl.html + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# This software is part of the Biopieces framework (www.biopieces.org). + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +require 'seq' + +# Error class for all exceptions to do with FASTA. +class FastaError < StandardError; end + +class Fasta + include Enumerable + + # Class method allowing open to be used on files. + # See File.open. + def self.open(*args) + ios = File.open(*args) + fasta = self.new(ios) + + if block_given? + begin + yield fasta + ensure + ios.close + end + + return true + else + return fasta + end + end + + def initialize(io, type=nil) + @io = io + @type = type + end + + # Iterator method for parsing FASTA enries. + def each + while entry = get_entry do + yield entry + end + end + + # Method to get the next FASTA entry form an ios and return this + # as a Seq object. If no entry is found or eof then nil is returned. + def get_entry + block = @io.gets($/ + '>') + return nil if block.nil? + + block.chomp!($/ + '>') + + (seq_name, seq) = block.split($/, 2) + + raise FastaError, "Bad FASTA format" if seq_name.nil? or seq.nil? + + entry = Seq.new + entry.type = @type.nil? ? nil : @type.downcase + entry.seq = seq.gsub(/\s/, '') + entry.seq_name = seq_name.sub(/^>/, '').rstrip + + raise FastaError, "Bad FASTA format" if entry.seq_name.empty? + raise FastaError, "Bad FASTA format" if entry.seq.empty? + + entry + end +end + + +__END__ diff --git a/code_ruby/Maasha/lib/seq.rb b/code_ruby/Maasha/lib/seq.rb index 4ce1e01..e55a383 100644 --- a/code_ruby/Maasha/lib/seq.rb +++ b/code_ruby/Maasha/lib/seq.rb @@ -1,127 +1,184 @@ +DNA = %w[a t c g] +RNA = %w[a u c g] +PROTEIN = %w[f l s y c w p h q r i m t n k v a d e g] + +# Error class for all exceptions to do with Seq. +class SeqError < StandardError; end + +class Seq + attr_accessor :seq_name, :seq, :type, :qual + + def length + self.seq.nil? ? 0 : self.seq.length + end + + alias len length + + # Method that returns true is a given sequence type is DNA. + def is_dna + self.type == 'dna' + end + + def is_rna + self.type == 'rna' + end + + def is_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.seq.tr!('Tt','Uu') + end + + # Method to reverse-transcribe RNA to DNA. + 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.seq.tr!('Uu','Tt') + end + + # Method that given a Seq entry returns a Biopieces record (a hash). + def to_bp + raise SeqError, "Missing seq_name" if self.seq_name.nil? + raise SeqError, "Missing seq" if self.seq.nil? + record = {} + record['SEQ_NAME'] = self.seq_name + record['SEQ'] = self.seq + record['SEQ_LEN'] = self.length + record + end + + # Method that complements sequence including ambiguity codes. + def complement + raise SeqError, "Cannot complement 0 length sequence" if self.length == 0 + + if self.is_dna + self.seq.tr!( 'AGCUTRYWSMKHDVBNagcutrywsmkhdvbn', 'TCGAAYRWSKMDHBVNtcgaayrwskmdhbvn' ) + elsif self.is_rna + self.seq.tr!( 'AGCUTRYWSMKHDVBNagcutrywsmkhdvbn', 'UCGAAYRWSKMDHBVNucgaayrwskmdhbvn' ) + else + raise SeqError, "Cannot complement sequence type: #{self.type}" + end + end + + # Method that generates a random sequence of a given length. + def generate(length,type) + raise SeqError, "Cannot generate negative sequence length: #{length}" if length <= 0 + + case type.downcase + when "dna" + alph = DNA + when "rna" + alph = RNA + when "protein" + 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 + seq_new + end +end + +__END__ + + + + + # Class containing generic sequence methods and nucleic acid and amino acid subclasses. class Seq < String - # Guess the sequence type by analyzing the first 100 residues allowing for ambiguity codes. - def guess_type - raise ArgumentError, "No sequence." if self.empty? - - seq_beg = self[0, 100].upcase - - if seq_beg.count( "FLPQIE" ) > 0 - Seq::AA.new(self) - elsif seq_beg.count("U") > 0 - Seq::NA::RNA.new(self) - else - Seq::NA::DNA.new(self) - end - end - - # Method to wrap a sequence to a given width using a given delimiter. - def wrap(width = 80, delimit = $/) - raise ArgumentError, "Cannot wrap sequence to negative width: #{width}." if width <= 0 - - self.delete!(" \t\n\r") - self.gsub(/.{#{width}}(?!$)/, "\\0#{delimit}") - end - - # Method to wrap and replace a sequence to a given width using a given delimiter. - def wrap!(width = 80, delimit = $/) - self.replace(self.wrap(width, delimit)) - end - - # Method that generates a random sequence of a given length. - def generate(length) - raise ArgumentError, "Cannot generate negative sequence length: #{length}." if length <= 0 - - alph = self.residues - Array.new(length) { alph[rand(alph.size)] }.join("") - end - - # Method that replaces sequence with a randomly generated sequence of a given length. - def generate!(length) - self.replace(self.generate(length)) - end - - # Class containing methods specific for amino acid (AA) sequences. - class AA < Seq - # Method that returns an array of amino acid residues. - def residues - %w{ F L S Y C W P H Q R I M T N K V A D E G } - end - - # Calculate the molecular weight of an amino acid seuqunce. - # The caluculation is only approximate since there is no correction - # for amino bond formation and the MW used are somewhat imprecise: - # http://www.expasy.ch/tools/pscale/Molecularweight.html - def mol_weight - raise ArgumentError, "invalid residues found: #{self.delete("#{residues.join( "" )}")}" if self.upcase =~ /[^#{residues.join( "" )}]/ - - mol_weight_aa = { - "A" => 89.000, # Ala - "R" => 174.000, # Arg - "N" => 132.000, # Asn - "D" => 133.000, # Asp - "C" => 121.000, # Cys - "Q" => 146.000, # Gln - "E" => 147.000, # Glu - "G" => 75.000, # Gly - "H" => 155.000, # His - "I" => 131.000, # Ile - "L" => 131.000, # Leu - "K" => 146.000, # Lys - "M" => 149.000, # Met - "F" => 165.000, # Phe - "P" => 115.000, # Pro - "S" => 105.000, # Ser - "T" => 119.000, # Thr - "W" => 204.000, # Trp - "Y" => 181.000, # Tyr - "V" => 117.000, # Val - } - - mw = 0.0 - - self.upcase.each_char { |c| mw += mol_weight_aa[ c ] } - - mw - end - end - - # Class containing methods specific for nucleic acid (NA) sequences. - class NA < Seq - # Class containing methods specific for DNA sequences. - class DNA < NA - # Method that returns an array of DNA residues. - def residues - %w{ A T C G } - end - - # Method to transcribe DNA to RNA. - def to_RNA - Seq::NA::RNA.new( self.tr( 'Tt', 'Uu' ) ) - end - - # Method that complements DNA sequence including ambiguity codes. - def complement - self.tr!( 'AGCUTRYWSMKHDVBNagcutrywsmkhdvbn', 'TCGAAYRWSKMDHBVNtcgaayrwskmdhbvn' ) - end - end - - # Class containing methods specific for RNA sequences. - class RNA < NA - # Method that returns an array of RNA residues. - def residues - %w{ A U C G } - end - - # Method to reverse transcribe RNA to DNA. - def to_DNA - Seq::NA::DNA.new( self.tr( 'Uu', 'Tt' ) ) - end - - # Method that complements RNA sequence including ambiguity codes. - def complement - self.tr!( 'AGCUTRYWSMKHDVBNagcutrywsmkhdvbn', 'UCGAAYRWSKMDHBVNucgaayrwskmdhbvn' ) - end - end - end -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? + + seq_beg = self[0, 100].upcase + + if seq_beg.count( "FLPQIE" ) > 0 + Seq::AA.new(self) + elsif seq_beg.count("U") > 0 + Seq::NA::RNA.new(self) + else + Seq::NA::DNA.new(self) + end + end + + # Method to wrap a sequence to a given width using a given delimiter. + def wrap(width = 80, delimit = $/) + raise ArgumentError, "Cannot wrap sequence to negative width: #{width}." if width <= 0 + + self.delete!(" \t\n\r") + self.gsub(/.{#{width}}(?!$)/, "\\0#{delimit}") + end + + # Method to wrap and replace a sequence to a given width using a given delimiter. + def wrap!(width = 80, delimit = $/) + self.replace(self.wrap(width, delimit)) + end + + # Method that generates a random sequence of a given length. + def generate(length) + raise ArgumentError, "Cannot generate negative sequence length: #{length}." if length <= 0 + + alph = self.residues + Array.new(length) { alph[rand(alph.size)] }.join("") + end + + # Method that replaces sequence with a randomly generated sequence of a given length. + def generate!(length) + self.replace(self.generate(length)) + end + + # Class containing methods specific for amino acid (AA) sequences. + class AA < Seq + # Method that returns an array of amino acid residues. + def residues + %w{ F L S Y C W P H Q R I M T N K V A D E G } + end + + # Calculate the molecular weight of an amino acid seuqunce. + # The caluculation is only approximate since there is no correction + # for amino bond formation and the MW used are somewhat imprecise: + # http://www.expasy.ch/tools/pscale/Molecularweight.html + def mol_weight + raise ArgumentError, "invalid residues found: #{self.delete("#{residues.join( "" )}")}" if self.upcase =~ /[^#{residues.join( "" )}]/ + + mol_weight_aa = { + "A" => 89.000, # Ala + "R" => 174.000, # Arg + "N" => 132.000, # Asn + "D" => 133.000, # Asp + "C" => 121.000, # Cys + "Q" => 146.000, # Gln + "E" => 147.000, # Glu + "G" => 75.000, # Gly + "H" => 155.000, # His + "I" => 131.000, # Ile + "L" => 131.000, # Leu + "K" => 146.000, # Lys + "M" => 149.000, # Met + "F" => 165.000, # Phe + "P" => 115.000, # Pro + "S" => 105.000, # Ser + "T" => 119.000, # Thr + "W" => 204.000, # Trp + "Y" => 181.000, # Tyr + "V" => 117.000, # Val + } + + mw = 0.0 + + self.upcase.each_char { |c| mw += mol_weight_aa[ c ] } + + mw + end + end diff --git a/code_ruby/Maasha/test/test_biopieces.rb b/code_ruby/Maasha/test/test_biopieces.rb index 9770bc3..123b866 100755 --- a/code_ruby/Maasha/test/test_biopieces.rb +++ b/code_ruby/Maasha/test/test_biopieces.rb @@ -1,5 +1,5 @@ #!/usr/bin/env ruby -# $:.unshift File.join(File.dirname(__FILE__),'..','lib') +$:.unshift File.join(File.dirname(__FILE__),'..','lib') # Copyright (C) 2007-2010 Martin A. Hansen. @@ -34,7 +34,7 @@ TYPES = %w[flag string list int uint float file file! files files! dir dir DUMMY_FILE = __FILE__ SCRIPT_PATH = "write_fasta" -class OptionTest < Test::Unit::TestCase +class BiopiecesTest < Test::Unit::TestCase def setup @input = StringIO.new diff --git a/code_ruby/Maasha/test/test_fasta.rb b/code_ruby/Maasha/test/test_fasta.rb new file mode 100755 index 0000000..dc59f58 --- /dev/null +++ b/code_ruby/Maasha/test/test_fasta.rb @@ -0,0 +1,90 @@ +#!/usr/bin/env ruby +$:.unshift File.join(File.dirname(__FILE__),'..','lib') + +# Copyright (C) 2007-2010 Martin A. Hansen. + +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +# http://www.gnu.org/copyleft/gpl.html + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +# This software is part of the Biopieces framework (www.biopieces.org). + +# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + +require 'test/unit' +require 'fasta' +require 'stringio' +require 'pp' + +class FastaTest < Test::Unit::TestCase + test "Fasta#get_entry obtains the correct seq_name" do + fasta = Fasta.new(StringIO.new(">test\nATCG\n")) + assert_equal(fasta.get_entry.seq_name, "test") + end + + test "Fasta#get_entry obtains the correct seq without trailing newlines" do + fasta = Fasta.new(StringIO.new(">test\nATCG")) + assert_equal(fasta.get_entry.seq, "ATCG") + end + + test "Fasta#get_entry obtains the correct seq with trailing newlines" do + fasta = Fasta.new(StringIO.new(">test\nATCG\n\n\n")) + assert_equal(fasta.get_entry.seq, "ATCG") + end + + test "Fasta#get_entry obtains the correct type" do + fasta = Fasta.new(StringIO.new(">test\nATCG\n"), 'DNA') + assert_equal(fasta.get_entry.type, "dna") + end + + test "Fasta#get_entry rstrips whitespace from seq_name" do + fasta = Fasta.new(StringIO.new(">test\n\r\t ATCG\n")) + assert_equal(fasta.get_entry.seq_name, "test") + end + + test "Fasta#get_entry strips whitespace from seq" do + fasta = Fasta.new(StringIO.new(">test\n\r\t AT\n\r\t CG\n\r\t ")) + assert_equal(fasta.get_entry.seq, "ATCG") + end + + test "Fasta#get_entry with two entries obtain correct" do + fasta = Fasta.new(StringIO.new(">test1\n\r\t AT\n\r\t CG\n\r\t \n>test2\n\r\t atcg\n")) + assert_equal(fasta.get_entry.seq, "ATCG") + assert_equal(fasta.get_entry.seq, "atcg") + end + + test "Fasta#get_entry without seq_name raises" do + fasta = Fasta.new(StringIO.new("ATCG\n")) + assert_raise( FastaError ) { fasta.get_entry } + end + + test "Fasta#get_entry without seq raises" do + fasta = Fasta.new(StringIO.new(">test\n\n")) + assert_raise( FastaError ) { fasta.get_entry } + end + + test "Fasta#get_entry with leading newline raises" do + fasta = Fasta.new(StringIO.new("\n>test\nATCG\n")) + assert_raise( FastaError ) { fasta.get_entry } + end + +# FIXME +# test "Fasta#get_entry raises on missing > in seq_name" do +# fasta = Fasta.new(StringIO.new("test\nATCG\n")) +# assert_raise( FastaError ) { fasta.get_entry } +# end +end diff --git a/code_ruby/Maasha/test/test_seq.rb b/code_ruby/Maasha/test/test_seq.rb index c080665..b8841ac 100755 --- a/code_ruby/Maasha/test/test_seq.rb +++ b/code_ruby/Maasha/test/test_seq.rb @@ -5,260 +5,389 @@ require 'test/unit' require 'pp' class TestSeq < Test::Unit::TestCase - # Testing Seq#initialize + def setup + @entry = Seq.new + end + + # test "Seq# autoremoves whitespace, newlines, and carriage returns" do + # dna = Seq.new + # dna.seq = "A\tT\r\tC\nG " + # assert_equal(dna.seq, "ATCG") + # end + + test "Seq#is_dna with no sequence type returns false" do + assert_false(@entry.is_dna) + end + + test "Seq#is_dna with dna sequence type returns true" do + @entry.type = 'dna' + assert_true(@entry.is_dna) + end + + test "Seq#is_rna with no sequence type returns false" do + assert_false(@entry.is_rna) + end + + test "Seq#is_rna with rna sequence type returns true" do + @entry.type = 'rna' + assert_true(@entry.is_rna) + end + + test "Seq#is_protein with no sequence type returns false" do + assert_false(@entry.is_protein) + end + + test "Seq#is_protein with protein sequence type returns true" do + @entry.type = 'protein' + assert_true(@entry.is_protein) + end + + test "Sequence length is correct" do + @entry.seq = 'ATCG' + assert_equal(@entry.length, 4) + end + + test "Seq#to_rna raises if no sequence" do + @entry.type = 'dna' + assert_raise(SeqError) { @entry.to_rna } + end + + test "Seq#to_rna raises on bad type" do + @entry.seq = 'ATCG' + @entry.type = 'rna' + assert_raise(SeqError) { @entry.to_rna } + end + + test "Seq#to_rna transcribes correctly" do + @entry.seq = 'ATCGatcg' + @entry.type = 'dna' + assert_equal(@entry.to_rna, "AUCGaucg") + end + + test "Seq#to_rna changes entry type to rna" do + @entry.seq = 'ATCGatcg' + @entry.type = 'dna' + @entry.to_rna + assert_equal(@entry.type, "rna") + end + + test "Seq#to_dna raises if no sequence" do + @entry.type = 'rna' + assert_raise(SeqError) { @entry.to_dna } + end + + test "Seq#to_dna raises on bad type" do + @entry.seq = 'AUCG' + @entry.type = 'dna' + assert_raise(SeqError) { @entry.to_dna } + end + + test "Seq#to_dna transcribes correctly" do + @entry.seq = 'AUCGaucg' + @entry.type = 'rna' + assert_equal(@entry.to_dna, "ATCGatcg") + end + + test "Seq#to_dna changes entry type to dna" do + @entry.seq = 'AUCGaucg' + @entry.type = 'rna' + @entry.to_dna + assert_equal(@entry.type, "dna") + end + + test "Seq#to_bp returns correct record" do + @entry.seq_name = 'test' + @entry.seq = 'ATCG' + assert_equal(@entry.to_bp, {"SEQ_NAME"=>"test", "SEQ"=>"ATCG", "SEQ_LEN"=>4}) + end + + test "Seq#to_bp raises on missing seq_name" do + @entry.seq = 'ATCG' + assert_raise(SeqError) { @entry.to_bp } + end + + test "Seq#to_bp raises on missing sequence" do + @entry.seq_name = 'test' + assert_raise(SeqError) { @entry.to_bp } + end + + test "Seq#complement raises if no sequence" do + @entry.type = 'dna' + assert_raise(SeqError) { @entry.complement } + end + + test "Seq#complement raises on bad type" do + @entry.seq = 'ATCG' + @entry.type = 'protein' + assert_raise(SeqError) { @entry.complement } + end + + test "Seq#complement for DNA is correct" do + @entry.seq = 'ATCGatcg' + @entry.type = 'dna' + assert_equal(@entry.complement, "TAGCtagc") + end + + test "Seq#complement for RNA is correct" do + @entry.seq = 'AUCGaucg' + @entry.type = 'rna' + assert_equal(@entry.complement, "UAGCuagc") + end + + test "Seq#generate raises if length <= 0" do + assert_raise(SeqError) { @entry.generate(-10, "dna") } + assert_raise(SeqError) { @entry.generate(0, "dna") } + end + + test "Seq#generate raises on bad type" do + assert_raise(SeqError) { @entry.generate(10, "foo") } + end + + test "Seq#generate don't raise on ok type" do + %w[ dna DNA rna RNA protein Protein ].each do |type| + assert_nothing_raised { @entry.generate(10, type) } + end + end +end + - # test marked for deletion - too simple and not informative - def test_Seq_initialize_with_0_args - s = Seq.new - assert_equal( "", s.to_s ) - end +__END__ + +class TestSeq < Test::Unit::TestCase + # Testing Seq#guess_type - # test marked for deletion - too simple and not informative - def test_Seq_initialize_with_1_args - s = Seq.new( "ATCG" ) - assert_equal( "ATCG", s.to_s ) - end + def test_guess_type_raise_if_no_sequence + s = Seq.new - # Testing Seq#to_s - def test_to_s - s = Seq.new( "ATCG" ) - assert_equal( "ATCG", s.to_s ) - end + assert_raise( ArgumentError ) { s.guess_type } + end - # Testing Seq#guess_type + def test_guess_type_AA_uppercase + s1 = Seq.new( "SEQ" ) + s2 = Seq::AA.new( "SEQ" ) + assert_equal( s1.guess_type.class, s2.class ) + end - def test_guess_type_raise_if_no_sequence - s = Seq.new + def test_guess_type_AA_lowercase + s1 = Seq.new( "seq" ) + s2 = Seq::AA.new( "seq" ) - assert_raise( ArgumentError ) { s.guess_type } - end + assert_equal( s1.guess_type.class, s2.class ) + end - def test_guess_type_AA_uppercase - s1 = Seq.new( "SEQ" ) - s2 = Seq::AA.new( "SEQ" ) - assert_equal( s1.guess_type.class, s2.class ) - end + def test_guess_type_DNA_uppercase + s1 = Seq.new( "ATCG" ) + s2 = Seq::NA::DNA.new( "ATCG" ) - def test_guess_type_AA_lowercase - s1 = Seq.new( "seq" ) - s2 = Seq::AA.new( "seq" ) + assert_equal( s1.guess_type.class, s2.class ) + end - assert_equal( s1.guess_type.class, s2.class ) - end + def test_guess_type_DNA_lowercase + s1 = Seq.new( "atcg" ) + s2 = Seq::NA::DNA.new( "atcg" ) - def test_guess_type_DNA_uppercase - s1 = Seq.new( "ATCG" ) - s2 = Seq::NA::DNA.new( "ATCG" ) + assert_equal( s1.guess_type.class, s2.class ) + end - assert_equal( s1.guess_type.class, s2.class ) - end + def test_guess_type_RNA_uppercase + s1 = Seq.new( "AUCG" ) + s2 = Seq::NA::RNA.new( "AUCG" ) - def test_guess_type_DNA_lowercase - s1 = Seq.new( "atcg" ) - s2 = Seq::NA::DNA.new( "atcg" ) + assert_equal( s1.guess_type.class, s2.class ) + end - assert_equal( s1.guess_type.class, s2.class ) - end + def test_guess_type_RNA_lowercase + s1 = Seq.new( "aucg" ) + s2 = Seq::NA::RNA.new( "aucg" ) - def test_guess_type_RNA_uppercase - s1 = Seq.new( "AUCG" ) - s2 = Seq::NA::RNA.new( "AUCG" ) + assert_equal( s1.guess_type.class, s2.class ) + end - assert_equal( s1.guess_type.class, s2.class ) - end + # Testing Seq#wrap - def test_guess_type_RNA_lowercase - s1 = Seq.new( "aucg" ) - s2 = Seq::NA::RNA.new( "aucg" ) + def test_wrap_arg_is_a_positive_number + s = Seq.new - assert_equal( s1.guess_type.class, s2.class ) - end + assert_raise( ArgumentError ) { s.wrap( 0 ) } + assert_raise( ArgumentError ) { s.wrap( -10 ) } + end - # Testing Seq#wrap + def test_wrap_with_0_args + s = Seq.new( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACGACTACGACT" ) - def test_wrap_arg_is_a_positive_number - s = Seq.new + assert_equal( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACG\nACTACGACT", s.wrap.to_s ) + end - assert_raise( ArgumentError ) { s.wrap( 0 ) } - assert_raise( ArgumentError ) { s.wrap( -10 ) } - end + def test_wrap_with_1_args + s = Seq.new( "ATCG" ) - def test_wrap_with_0_args - s = Seq.new( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACGACTACGACT" ) + assert_equal( "AT\nCG", s.wrap( 2 ).to_s ) + end - assert_equal( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACG\nACTACGACT", s.wrap.to_s ) - end + def test_wrap_with_2_args + s = Seq.new( "ATCG" ) - def test_wrap_with_1_args - s = Seq.new( "ATCG" ) + assert_equal( "AT\rCG", s.wrap( 2, "\r" ).to_s ) + end - assert_equal( "AT\nCG", s.wrap( 2 ).to_s ) - end + def test_wrap_dont_change_instance_var + s = Seq.new( "ATCG" ) - def test_wrap_with_2_args - s = Seq.new( "ATCG" ) + s.wrap( 2 ) - assert_equal( "AT\rCG", s.wrap( 2, "\r" ).to_s ) - end + assert_equal( "ATCG", s.to_s ) + end - def test_wrap_dont_change_instance_var - s = Seq.new( "ATCG" ) + # Testing Seq#wrap! - s.wrap( 2 ) + def test_wrap_with_0_args! + s = Seq.new( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACGACTACGACT" ) - assert_equal( "ATCG", s.to_s ) - end + s.wrap! - # Testing Seq#wrap! + assert_equal( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACG\nACTACGACT", s.to_s ) + end - def test_wrap_with_0_args! - s = Seq.new( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACGACTACGACT" ) + def test_wrap_with_1_args! + s = Seq.new( "ATCG" ) - s.wrap! + s.wrap!( 2 ) - assert_equal( "ACTGACTAGCATCGACTACGACTGACACGACGACGACGACCGAACGATCGATCGCAGACGACGCAGCATGACGACGTACG\nACTACGACT", s.to_s ) - end + assert_equal( "AT\nCG", s.to_s ) + end - def test_wrap_with_1_args! - s = Seq.new( "ATCG" ) + def test_wrap_with_2_args! + s = Seq.new( "ATCG" ) - s.wrap!( 2 ) + s.wrap!( 2, "\r" ) - assert_equal( "AT\nCG", s.to_s ) - end + assert_equal( "AT\rCG", s.to_s ) + end - def test_wrap_with_2_args! - s = Seq.new( "ATCG" ) + # Testing Seq#generate - s.wrap!( 2, "\r" ) + def test_generate_arg_is_a_positive_number + s = Seq.new - assert_equal( "AT\rCG", s.to_s ) - end + assert_raise( ArgumentError ) { s.generate( 0 ) } + assert_raise( ArgumentError ) { s.generate( -10 ) } + end - # Testing Seq#generate + def test_generate + s = Seq::AA.new - def test_generate_arg_is_a_positive_number - s = Seq.new + seq = s.generate( 40 ) - assert_raise( ArgumentError ) { s.generate( 0 ) } - assert_raise( ArgumentError ) { s.generate( -10 ) } - end + assert_equal( 40, seq.length ) + end - def test_generate - s = Seq::AA.new + def test_generate_dont_change_instance_var + s = Seq::AA.new - seq = s.generate( 40 ) + seq = s.generate( 40 ) - assert_equal( 40, seq.length ) - end + assert_equal( "", s.to_s ) + end - def test_generate_dont_change_instance_var - s = Seq::AA.new + # Testing Seq#generate! - seq = s.generate( 40 ) + def test_generate! + s = Seq::AA.new - assert_equal( "", s.to_s ) - end + s.generate!( 40 ) - # Testing Seq#generate! + assert_equal( 40, s.length ) + end - def test_generate! - s = Seq::AA.new + # Testing Seq::AA#residues - s.generate!( 40 ) - - assert_equal( 40, s.length ) - end + def test_Seq_AA_residues + s = Seq::AA.new - # Testing Seq::AA#residues + assert_equal( %w{ F L S Y C W P H Q R I M T N K V A D E G }, s.residues ) + end - def test_Seq_AA_residues - s = Seq::AA.new + # Testing Seq::AA#mol_weight - assert_equal( %w{ F L S Y C W P H Q R I M T N K V A D E G }, s.residues ) - end + def test_Seq_aa_mol_weight_bad_residue + s = Seq::AA.new( "7" ) + assert_raise( ArgumentError ) { s.mol_weight } + end - # Testing Seq::AA#mol_weight + def test_Seq_aa_mol_wight_return_correct_uppercase + s = Seq::AA.new( "SEQ" ) + assert_equal( 398.0, s.mol_weight ) + end - def test_Seq_aa_mol_weight_bad_residue - s = Seq::AA.new( "7" ) - assert_raise( ArgumentError ) { s.mol_weight } - end + def test_Seq_aa_mol_wight_return_correct_lowercase + s = Seq::AA.new( "seq" ) + assert_equal( 398.0, s.mol_weight ) + end - def test_Seq_aa_mol_wight_return_correct_uppercase - s = Seq::AA.new( "SEQ" ) - assert_equal( 398.0, s.mol_weight ) - end + # Testing Seq::NA::DNA#residues - def test_Seq_aa_mol_wight_return_correct_lowercase - s = Seq::AA.new( "seq" ) - assert_equal( 398.0, s.mol_weight ) - end + def test_Seq_NA_DNA_residues + s = Seq::NA::DNA.new - # Testing Seq::NA::DNA#residues + assert_equal( %w{ A T C G }, s.residues ) + end - def test_Seq_NA_DNA_residues - s = Seq::NA::DNA.new + # Testing Seq::NA::DNA#complement - assert_equal( %w{ A T C G }, s.residues ) - end + def test_Seq_NA_DNA_complement_correct + s = Seq::NA::DNA.new( "ATCGatcg" ) + assert_equal( "TAGCtagc", s.complement.to_s ) + end - # Testing Seq::NA::DNA#complement + # Testing Seq::NA::DNA#to_RNA - def test_Seq_NA_DNA_complement_correct - s = Seq::NA::DNA.new( "ATCGatcg" ) - assert_equal( "TAGCtagc", s.complement.to_s ) - end + def test_Seq_NA_DNA_to_RNA_returns_RNA_object + dna = Seq::NA::DNA.new( "ATCGatcg" ) + rna = Seq::NA::RNA.new - # Testing Seq::NA::DNA#to_RNA + new_rna = dna.to_RNA - def test_Seq_NA_DNA_to_RNA_returns_RNA_object - dna = Seq::NA::DNA.new( "ATCGatcg" ) - rna = Seq::NA::RNA.new - - new_rna = dna.to_RNA + assert_equal( rna.class, new_rna.class ) + end - assert_equal( rna.class, new_rna.class ) - end + def test_Seq_NA_DNA_to_RNA_is_correct + dna = Seq::NA::DNA.new( "ATCGatcg" ) + rna = dna.to_RNA - def test_Seq_NA_DNA_to_RNA_is_correct - dna = Seq::NA::DNA.new( "ATCGatcg" ) - rna = dna.to_RNA + assert_equal( "AUCGaucg", rna.to_s ) + end - assert_equal( "AUCGaucg", rna.to_s ) - end + # Testing Seq::NA::RNA#residues - # Testing Seq::NA::RNA#residues + def test_Seq_NA_RNA_residues + s = Seq::NA::RNA.new - def test_Seq_NA_RNA_residues - s = Seq::NA::RNA.new + assert_equal( %w{ A U C G }, s.residues ) + end - assert_equal( %w{ A U C G }, s.residues ) - end + # Testing Seq::NA::RNA#complement - # Testing Seq::NA::RNA#complement + def test_Seq_NA_RNA_complement_correct + s = Seq::NA::RNA.new( "AUCGaucg" ) + assert_equal( "UAGCuagc", s.complement.to_s ) + end - def test_Seq_NA_RNA_complement_correct - s = Seq::NA::RNA.new( "AUCGaucg" ) - assert_equal( "UAGCuagc", s.complement.to_s ) - end + # Testing Seq::NA::RNA#to_DNA - # Testing Seq::NA::RNA#to_DNA + def test_Seq_NA_RNA_to_DNA_returns_DNA_object + rna = Seq::NA::RNA.new( "AUCGaucg" ) + dna = Seq::NA::DNA.new - def test_Seq_NA_RNA_to_DNA_returns_DNA_object - rna = Seq::NA::RNA.new( "AUCGaucg" ) - dna = Seq::NA::DNA.new - - new_dna = rna.to_DNA + new_dna = rna.to_DNA - assert_equal( dna.class, new_dna.class ) - end + assert_equal( dna.class, new_dna.class ) + end - def test_Seq_NA_RNA_to_DNA_is_correct - rna = Seq::NA::RNA.new( "AUCGaucg" ) - dna = rna.to_DNA + def test_Seq_NA_RNA_to_DNA_is_correct + rna = Seq::NA::RNA.new( "AUCGaucg" ) + dna = rna.to_DNA - assert_equal( "ATCGatcg", dna.to_s ) - end + assert_equal( "ATCGatcg", dna.to_s ) + end end