1 # Copyright (C) 2007-2011 Martin A. Hansen.
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.
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.
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.
17 # http://www.gnu.org/copyleft/gpl.html
19 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
21 # This software is part of the Biopieces framework (www.biopieces.org).
23 # >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>><<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
26 require 'maasha/filesys'
28 # Error class for all exceptions to do with FASTQ.
29 class FastqError < StandardError; end
31 # Class for parsing FASTQ entries from an ios and return as Seq objects.
33 # Method to get the next FASTQ entry from an ios and return this
34 # as a Seq object. If no entry is found or eof then nil is returned.
37 seq_name = @io.gets.chomp!
40 qual = @io.gets.chomp!
44 entry.seq_name = seq_name[1 .. seq_name.length]
55 # Class for indesing FASTQ entries. The index will be
56 # a hash with the FASTQ sequence name as key and a
57 # FastqElem as value. The latter contains info on
58 # byte offset and length for each entry.
65 # Method to initialize a FastqIndex object. For reading
66 # entries from file an _ios_ object must be supplied.
67 def initialize(ios = nil)
73 # Method to add a Fastq entry to a FastqIndex.
75 offset_seq = @offset + HEADCHAR + entry.seq_name.length + NEWLINE
76 offset_qual = @offset + HEADCHAR + entry.seq_name.length + NEWLINE + entry.length + NEWLINE + HEADCHAR + NEWLINE
78 @index[entry.seq_name] = FastqElem.new(offset_seq, offset_qual, entry.length)
80 @offset += HEADCHAR + entry.seq_name.length + NEWLINE + entry.length + NEWLINE + HEADCHAR + NEWLINE + entry.length + NEWLINE
83 # Method to read from file a Fastq entry from an indexed position,
84 # and return the entry as a Seq object.
86 raise FastqError, "Sequence name: #{seq_name} not found in index." unless @index.has_key? seq_name
88 elem = @index[seq_name]
89 @ios.sysseek(elem.offset_seq)
90 seq = @ios.sysread(elem.length)
91 @ios.sysseek(elem.offset_qual)
92 qual = @ios.sysread(elem.length)
94 Seq.new(seq_name, seq, nil, qual)
99 # Class for storing index information to be used
100 # with disk based index.
102 attr_reader :offset_seq, :offset_qual, :length
104 def initialize(offset_seq, offset_qual, length)
105 @offset_seq = offset_seq
106 @offset_qual = offset_qual