Permalink
Browse files

Split out FastaRecord

  • Loading branch information...
1 parent cb204b7 commit 49430ef745fcb4402ffe951feb9a85374c979b80 @pjotrp committed Apr 26, 2013
Showing with 15 additions and 5 deletions.
  1. +13 −3 lib/bigbio/db/fasta/fastareader.rb
  2. +2 −2 spec/emitter_spec.rb
@@ -135,14 +135,17 @@ def indexed?
class FastaReader
- def FastaReader::emit_seq func
- buf = func.call
+ # func passes in a FASTA buffer. Every time a record is parsed it is
+ # yielded.
+ #
+ def FastaReader::emit getbuf_func
+ buf = getbuf_func.call
seq = ""
id = nil
descr = nil
buf.split(/\n/).each do | line |
if line =~ /^>/
- yield FastaRecord.new(id, descr, seq) if id and seq and seq.size > 0
+ yield id, descr, seq if descr
descr = line[1..-1].strip
matched = /^(\S+)/.match(descr)
id = matched[0]
@@ -151,6 +154,13 @@ def FastaReader::emit_seq func
seq += line.strip
end
end
+ yield id, descr, seq if descr
+ end
+
+ def FastaReader::emit_fastarecord getbuf_func
+ emit(getbuf_func) do | id, descr, seq |
+ yield FastaRecord.new(id, descr, seq)
+ end
end
end
View
@@ -22,7 +22,7 @@
it "should emit functional style" do
count = 0
- FastaReader::emit_seq(lambda { File.open("test/data/fasta/nt.fa").read() }) do |rec|
+ FastaReader::emit_fastarecord(-> { File.open("test/data/fasta/nt.fa").read }) { |rec|
case count
when 0
rec.id.should == "PUT-157a-Arabidopsis_thaliana-1"
@@ -34,7 +34,7 @@
break
end
count += 1
- end
+ }
end
it "should emit large parts" do

0 comments on commit 49430ef

Please sign in to comment.