diff --git a/src/bam/reader.jl b/src/bam/reader.jl index 953f5ca..43e228e 100644 --- a/src/bam/reader.jl +++ b/src/bam/reader.jl @@ -72,12 +72,11 @@ function Base.seekstart(reader::Reader) seek(reader.stream, reader.start_offset) end -function Base.iterate(reader::Reader, rec=Record()) - if eof(reader) +function Base.iterate(reader::Reader, nextone = Record()) + if BioGenerics.IO.tryread!(reader, nextone) === nothing return nothing end - read!(reader, rec) - return copy(rec), rec + return copy(nextone), empty!(nextone) end # Initialize a BAM reader by reading the header section. diff --git a/src/bam/record.jl b/src/bam/record.jl index bf65078..7f322b5 100644 --- a/src/bam/record.jl +++ b/src/bam/record.jl @@ -76,6 +76,22 @@ function Base.copy(record::Record) return copy end +function Base.empty!(record::Record) + record.block_size = 0 + record.refid = 0 + record.pos = 0 + record.bin_mq_nl = 0 + record.flag_nc = 0 + record.l_seq = 0 + record.next_refid = 0 + record.next_pos = 0 + record.tlen = 0 + + #Note: data will be overwritten and indexed using data_size. + + return record +end + function Base.show(io::IO, record::Record) print(io, summary(record), ':') if isfilled(record) diff --git a/src/sam/reader.jl b/src/sam/reader.jl index 24152c4..a20fd4c 100644 --- a/src/sam/reader.jl +++ b/src/sam/reader.jl @@ -81,9 +81,20 @@ function index!(record::Record) return record end -function Base.read!(rdr::Reader, rec::Record) +function Base.iterate(reader::Reader, nextone::Record = Record()) + if BioGenerics.IO.tryread!(reader, nextone) === nothing + return nothing + end + return copy(nextone), empty!(nextone) +end - empty!(rec.fields) #Note: data is pushed to the fields field, and other field data is overwritten. #TODO: distinguish for inplace reading pattern. +""" + read!(rdr::Reader, rec::Record) + +Read a `Record` into `rec`; overwriting or adding to existing field values. +It is assumed that `rec` is already initialized or empty. +""" +function Base.read!(rdr::Reader, rec::Record) cs, ln, f = readrecord!(rdr.state.stream, rec, (rdr.state.state, rdr.state.linenum)) diff --git a/src/sam/record.jl b/src/sam/record.jl index f7f381c..9bb33bd 100644 --- a/src/sam/record.jl +++ b/src/sam/record.jl @@ -571,7 +571,7 @@ end # Helper Functions # ---------------- -function initialize!(record::Record) +function Base.empty!(record::Record) record.filled = 1:0 record.qname = 1:0 record.flag = 1:0 @@ -588,6 +588,10 @@ function initialize!(record::Record) return record end +function initialize!(record::Record) #TODO: deprecate. + return empty!(record) +end + function checkfilled(record::Record) if !isfilled(record) throw(ArgumentError("unfilled SAM record"))