]> git.donarmstrong.com Git - biopieces.git/blob - code_ruby/lib/maasha/fasta.rb
changed fasta parse to allow leading empty lines
[biopieces.git] / code_ruby / lib / maasha / fasta.rb
1 # Copyright (C) 2007-2011 Martin A. Hansen.
2
3 # This program is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU General Public License
5 # as published by the Free Software Foundation; either version 2
6 # of the License, or (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16
17 # http://www.gnu.org/copyleft/gpl.html
18
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
20
21 # This software is part of the Biopieces framework (www.biopieces.org).
22
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24
25 require 'maasha/seq'
26 require 'maasha/filesys'
27
28 # Error class for all exceptions to do with FASTA.
29 class FastaError < StandardError; end
30
31 class Fasta < Filesys
32   # Method to get the next FASTA entry form an ios and return this
33   # as a Seq object. If no entry is found or eof then nil is returned.
34   def get_entry
35     block = nil 
36
37     while block = @io.gets($/ + '>') and block.chomp($/ + '>').empty?
38     end
39
40     return nil if block.nil?
41
42     block.chomp!($/ + '>')
43
44     (seq_name, seq) = block.split($/, 2)
45
46     raise FastaError, "Bad FASTA format" if seq_name.nil? or seq.nil?
47
48     entry          = Seq.new
49     entry.seq      = seq.gsub(/\s/, '')
50     entry.seq_name = seq_name.sub(/^>/, '').rstrip
51     entry.type     = nil
52
53     raise FastaError, "Bad FASTA format" if entry.seq_name.empty?
54     raise FastaError, "Bad FASTA format" if entry.seq.empty?
55
56     entry
57   end
58
59   # Method to get the next pseudo FASTA entry consisting of a sequence name and
60   # space seperated quality scores in decimals instead of sequence. This is
61   # the quality format used by Sanger and 454.
62   def get_decimal_qual
63     block = @io.gets($/ + '>')
64     return nil if block.nil?
65
66     block.chomp!($/ + '>')
67
68     (seq_name, qual) = block.split($/, 2)
69
70     raise FastaError, "Bad FASTA qual format" if seq_name.nil? or qual.nil?
71
72     entry          = Seq.new
73     entry.seq_name = seq_name.sub(/^>/, '').rstrip
74     entry.seq      = nil
75     entry.type     = nil
76     entry.qual     = qual.tr("\n", " ").strip.split(" ").collect { |q| (q.to_i + SCORE_BASE).chr }.join("")
77
78     raise FastaError, "Bad FASTA format" if entry.seq_name.empty?
79     raise FastaError, "Bad FASTA format" if entry.qual.empty?
80
81     entry
82   end
83 end
84
85
86 __END__