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.
36 seq_name = @io.gets.chomp!
39 qual = @io.gets.chomp!
43 entry.seq_name = seq_name[1 .. seq_name.length]
53 # Class for indesing FASTQ entries. The index will be
54 # a hash with the FASTQ sequence name as key and a
55 # FastqElem as value. The latter contains info on
56 # byte offset and length for each entry.
63 # Method to initialize a FastqIndex object. For reading
64 # entries from file an _ios_ object must be supplied.
65 def initialize(ios = nil)
71 # Method to add a Fastq entry to a FastqIndex.
73 offset_seq = @offset + HEADCHAR + entry.seq_name.length + NEWLINE
74 offset_qual = @offset + HEADCHAR + entry.seq_name.length + NEWLINE + entry.length + NEWLINE + HEADCHAR + NEWLINE
76 @index[entry.seq_name] = FastqElem.new(offset_seq, offset_qual, entry.length)
78 @offset += HEADCHAR + entry.seq_name.length + NEWLINE + entry.length + NEWLINE + HEADCHAR + NEWLINE + entry.length + NEWLINE
81 # Method to read from file a Fastq entry from an indexed position,
82 # and return the entry as a Seq object.
84 raise FastqError, "Sequence name: #{seq_name} not found in index." unless @index[seq_name]
86 elem = @index[seq_name]
87 @ios.sysseek(elem.offset_seq)
88 seq = @ios.sysread(elem.length)
89 @ios.sysseek(elem.offset_qual)
90 qual = @ios.sysread(elem.length)
92 Seq.new(seq_name, seq, nil, qual)
97 # Class for storing index information to be used
98 # with disk based index.
100 attr_reader :offset_seq, :offset_qual, :length
102 def initialize(offset_seq, offset_qual, length)
103 @offset_seq = offset_seq
104 @offset_qual = offset_qual