]> git.donarmstrong.com Git - biopieces.git/blobdiff - code_ruby/lib/maasha/fasta.rb
changed fasta parse to allow leading empty lines
[biopieces.git] / code_ruby / lib / maasha / fasta.rb
index 05655f67c86d609e8e4ab666347df228ad246c50..971ad545582c8e82d63b3e2c3846069c87acd3b5 100644 (file)
@@ -32,7 +32,11 @@ class Fasta < Filesys
   # 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($/ + '>')
+    block = nil 
+
+    while block = @io.gets($/ + '>') and block.chomp($/ + '>').empty?
+    end
+
     return nil if block.nil?
 
     block.chomp!($/ + '>')
@@ -42,15 +46,40 @@ class Fasta < Filesys
     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
+    entry.type     = nil
 
     raise FastaError, "Bad FASTA format" if entry.seq_name.empty?
     raise FastaError, "Bad FASTA format" if entry.seq.empty?
 
     entry
   end
+
+  # Method to get the next pseudo FASTA entry consisting of a sequence name and
+  # space seperated quality scores in decimals instead of sequence. This is
+  # the quality format used by Sanger and 454.
+  def get_decimal_qual
+    block = @io.gets($/ + '>')
+    return nil if block.nil?
+
+    block.chomp!($/ + '>')
+
+    (seq_name, qual) = block.split($/, 2)
+
+    raise FastaError, "Bad FASTA qual format" if seq_name.nil? or qual.nil?
+
+    entry          = Seq.new
+    entry.seq_name = seq_name.sub(/^>/, '').rstrip
+    entry.seq      = nil
+    entry.type     = nil
+    entry.qual     = qual.tr("\n", " ").strip.split(" ").collect { |q| (q.to_i + SCORE_BASE).chr }.join("")
+
+    raise FastaError, "Bad FASTA format" if entry.seq_name.empty?
+    raise FastaError, "Bad FASTA format" if entry.qual.empty?
+
+    entry
+  end
 end