1 # This program is free software; you can redistribute it and/or
2 # modify it under the terms of the GNU General Public License
3 # as published by the Free Software Foundation; either version 2
4 # of the License, or (at your option) any later version.
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
11 # You should have received a copy of the GNU General Public License
12 # along with this program; if not, write to the Free Software
13 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15 # http://www.gnu.org/copyleft/gpl.html
17 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
19 # This software is part of the Biopieces framework (www.biopieces.org).
21 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
25 # Error class for all exceptions to do with FASTA.
26 class FastaError < StandardError; end
31 # Class method allowing open to be used on files.
34 ios = File.open(*args)
50 def initialize(io, type=nil)
55 # Iterator method for parsing FASTA enries.
57 while entry = get_entry do
62 # Method to get the next FASTA entry form an ios and return this
63 # as a Seq object. If no entry is found or eof then nil is returned.
65 block = @io.gets($/ + '>')
66 return nil if block.nil?
68 block.chomp!($/ + '>')
70 (seq_name, seq) = block.split($/, 2)
72 raise FastaError, "Bad FASTA format" if seq_name.nil? or seq.nil?
75 entry.type = @type.nil? ? nil : @type.downcase
76 entry.seq = seq.gsub(/\s/, '')
77 entry.seq_name = seq_name.sub(/^>/, '').rstrip
79 raise FastaError, "Bad FASTA format" if entry.seq_name.empty?
80 raise FastaError, "Bad FASTA format" if entry.seq.empty?