Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allow unused trailing bytes in BAM records #71

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/bam/reader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,8 @@ function _read!(reader::Reader, record)
reader.stream,
pointer_from_objref(record),
FIXED_FIELDS_BYTES)
dsize = data_size(record)
if length(record.data) < dsize
resize!(record.data, dsize)
end
dsize = record.block_size - FIXED_FIELDS_BYTES + sizeof(record.block_size)
resize!(record.data, dsize)
unsafe_read(reader.stream, pointer(record.data), dsize)
record.reader = reader
return record
Expand Down
27 changes: 10 additions & 17 deletions src/bam/record.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mutable struct Record <: XAMRecord
next_refid::Int32
next_pos::Int32
tlen::Int32
# variable length data
# This vector never has unused bytes at the end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment may be confusing outside the context of the previous design, maybe

Suggested change
# This vector never has unused bytes at the end
# This vector used to have unused bytes at the end, but no longer does

or something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original comment intended to distinguish the fields that could vary in length from those that do not. From that standpoint, it does not need changing. If you'd like to add a comment specific to a field, perhaps it should go on the same line as the field.

data::Vector{UInt8}
reader::Union{Reader, Nothing}

Expand All @@ -41,7 +41,7 @@ function Base.convert(::Type{Record}, data::Vector{UInt8})
record = Record()
dst_pointer = Ptr{UInt8}(pointer_from_objref(record))
unsafe_copyto!(dst_pointer, pointer(data), FIXED_FIELDS_BYTES)
dsize = data_size(record)
dsize = record.block_size - FIXED_FIELDS_BYTES + sizeof(record.block_size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This calculation occurs more than once. It deserves its own function, which may be separate from data_size.

resize!(record.data, dsize)
length(data) < dsize + FIXED_FIELDS_BYTES && throw(ArgumentError("data too short"))
unsafe_copyto!(record.data, 1, data, FIXED_FIELDS_BYTES + 1, dsize)
Expand All @@ -61,19 +61,19 @@ function Base.:(==)(a::Record, b::Record)
a.next_refid == b.next_refid &&
a.next_pos == b.next_pos &&
a.tlen == b.tlen &&
a.data[1:data_size(a)] == b.data[1:data_size(b)]
a.data == b.data
end

function Base.copy(record::Record)
copy = Record()
GC.@preserve copy record begin
dst_pointer = Ptr{UInt8}(pointer_from_objref(copy))
cp = Record()
GC.@preserve cp record begin
dst_pointer = Ptr{UInt8}(pointer_from_objref(cp))
src_pointer = Ptr{UInt8}(pointer_from_objref(record))
unsafe_copyto!(dst_pointer, src_pointer, FIXED_FIELDS_BYTES)
end
copy.data = record.data[1:data_size(record)]
copy.reader = record.reader
return copy
cp.data = copy(record.data)
cp.reader = record.reader
return cp
end

function Base.empty!(record::Record)
Expand Down Expand Up @@ -584,14 +584,7 @@ end
# ----------------

# Return the size of the `.data` field.
function data_size(record::Record)
if isfilled(record)
return record.block_size - FIXED_FIELDS_BYTES + sizeof(record.block_size)
end

return 0

end
data_size(record::Record) = isfilled(record) ? length(record.data) : 0

function checkfilled(record::Record)
if !isfilled(record)
Expand Down
Loading