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 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 # Error class for all exceptions to do with FASTA.
27 class FastaError < StandardError; end
32 # Class method allowing open to be used on (zipped) files.
35 ios = self.zopen(*args)
48 def initialize(io, type=nil)
53 # Method to close ios.
58 # Iterator method for parsing FASTA enries.
60 while entry = get_entry do
65 # Method to get the next FASTA entry form an ios and return this
66 # as a Seq object. If no entry is found or eof then nil is returned.
68 block = @io.gets($/ + '>')
69 return nil if block.nil?
71 block.chomp!($/ + '>')
73 (seq_name, seq) = block.split($/, 2)
75 raise FastaError, "Bad FASTA format" if seq_name.nil? or seq.nil?
78 entry.type = @type.nil? ? nil : @type.downcase
79 entry.seq = seq.gsub(/\s/, '')
80 entry.seq_name = seq_name.sub(/^>/, '').rstrip
82 raise FastaError, "Bad FASTA format" if entry.seq_name.empty?
83 raise FastaError, "Bad FASTA format" if entry.seq.empty?
88 # TODO - this should be some custom to_s method instead.
90 if record.has_key? :SEQ_NAME and record.has_key? :SEQ
91 @io.print ">#{record[:SEQ_NAME]}\n"
92 @io.print "#{record[:SEQ]}\n"
98 # Helper method to return an ios to a file that may be zipped in which case
99 # the ios is unzipped on the fly. See File.open.
100 def self.zopen(*args)
101 ios = File.open(*args)
104 ios = Zlib::GzipReader.new(ios)