Skip to content

Commit

Permalink
Add fast each_record method
Browse files Browse the repository at this point in the history
  • Loading branch information
mooreryan committed Apr 16, 2016
1 parent 27a72be commit 0c51343
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
35 changes: 35 additions & 0 deletions lib/parse_fasta/fastq_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,39 @@ def each_record
f.close if f.instance_of?(Zlib::GzipReader)
return f
end

def each_record_fast
count = 0
header = ''
sequence = ''
description = ''
quality = ''

begin
f = Zlib::GzipReader.open(self)
rescue Zlib::GzipFile::Error => e
f = self
end

f.each_line do |line|
line.chomp!

case count % 4
when 0
header = line[1..-1]
when 1
sequence = line
when 2
description = line[1..-1]
when 3
quality = line
yield(header, sequence, description, quality)
end

count += 1
end

f.close if f.instance_of?(Zlib::GzipReader)
return f
end
end
30 changes: 29 additions & 1 deletion spec/lib/fastq_file_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
describe FastqFile do
let(:records) {
[["seq1", "AACCTTGG", "", ")#3gTqN8"],
["seq2 apples", "ACTG", "seq2 apples", "*ujM"]] }
["seq2 apples", "ACTG", "seq2 apples", "*ujM"]]
}

let(:records_fast) {
[["seq1", "AA CC TT GG", "", ")# 3g Tq N8"],
["seq2 apples", "ACTG", "seq2 apples", "*ujM"]]
}

let(:f_handle) { FastqFile.open(@fname).each_record { |s| } }


Expand All @@ -45,6 +52,27 @@
end
end

describe "#each_record_fast" do
before(:each) do
@fname = "#{File.dirname(__FILE__)}/../../test_files/test.fq.gz"
end

it "yields proper header, sequence, description, and quality" do
expect { |b|
FastqFile.open(@fname).each_record_fast(&b)
}.to yield_successive_args(records_fast[0], records_fast[1])
end

it "yields all params as String" do
FastqFile.open(@fname).each_record_fast do |h, s, d, q|
expect(h).to be_an_instance_of String
expect(s).to be_an_instance_of String
expect(d).to be_an_instance_of String
expect(q).to be_an_instance_of String
end
end
end

describe "#to_hash" do
let(:records) {
{ "seq1" => { head: "seq1",
Expand Down

0 comments on commit 0c51343

Please sign in to comment.