Skip to content

Rehaul BAM and SAM accessor functions #28

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

Open
wants to merge 9 commits 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
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## Rehaul

* [TODO: ADD STUFF]
4 changes: 1 addition & 3 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@ version = "0.2.6"

[deps]
Automa = "67c07d97-cdcb-5c2c-af73-a7f9c32a568b"
BGZFStreams = "28d598bf-9b8f-59f1-b38c-5a06b4a0f5e6"
BioAlignments = "00701ae9-d1dc-5365-b64a-a3a3ebf5695e"
BioGenerics = "47718e42-2ac5-11e9-14af-e5595289c2ea"
BioSequences = "7e6ae17a-c86d-528c-b3b9-7f778a29fe59"
CodecBGZF = "d9d91ef6-315d-495b-8131-db2ca24339d6"
GenomicFeatures = "899a7d2d-5c61-547b-bef9-6698a8d05446"
Indexes = "4ffb77ac-cb80-11e8-1b35-4b78cc642f6d"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
TranscodingStreams = "3bb67fe8-82b1-5028-8e26-92a6c54297fa"

[compat]
Automa = "0.7, 0.8"
BGZFStreams = "0.3"
BioAlignments = "2"
BioGenerics = "0.1"
BioSequences = "2.0.4"
GenomicFeatures = "2"
Indexes = "0.1"
TranscodingStreams = "0.6, 0.7, 0.8, 0.9"
julia = "1"

Expand Down
5 changes: 5 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## SAM
* quality_iter
* no-copy auxdata
* Display similar to BAM
* convert SAM/BAM and vice versa
1 change: 1 addition & 0 deletions src/XAM.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export
SAM,
BAM

include("common.jl")
include("sam/sam.jl")
include("bam/bam.jl")

Expand Down
20 changes: 10 additions & 10 deletions src/bam/auxdata.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# BAM Auxiliary Data
# ==================

struct AuxData <: AbstractDict{String,Any}
data::Vector{UInt8}
struct AuxData{T <: AbstractVector{UInt8}} <: AbstractDict{String,Any}
data::T
end

function Base.getindex(aux::AuxData, tag::AbstractString)
Expand Down Expand Up @@ -48,7 +48,7 @@ function checkauxtag(tag::AbstractString)
end
end

function getauxvalue(data::Vector{UInt8}, start::Int, stop::Int, t1::UInt8, t2::UInt8)
function getauxvalue(data::AbstractVector{UInt8}, start::Int, stop::Int, t1::UInt8, t2::UInt8)
pos = findauxtag(data, start, stop, t1, t2)
if pos == 0
throw(KeyError(String([t1, t2])))
Expand All @@ -58,7 +58,7 @@ function getauxvalue(data::Vector{UInt8}, start::Int, stop::Int, t1::UInt8, t2::
return val
end

function loadauxtype(data::Vector{UInt8}, p::Int)
function loadauxtype(data::AbstractVector{UInt8}, p::Int)
function auxtype(b)
return (
b == UInt8('A') ? Char :
Expand All @@ -83,30 +83,30 @@ function loadauxtype(data::Vector{UInt8}, p::Int)

end

function loadauxvalue(data::Vector{UInt8}, p::Int, ::Type{T}) where T
function loadauxvalue(data::AbstractVector{UInt8}, p::Int, ::Type{T}) where T
return p + sizeof(T), unsafe_load(Ptr{T}(pointer(data, p)))
end

function loadauxvalue(data::Vector{UInt8}, p::Int, ::Type{Char})
function loadauxvalue(data::AbstractVector{UInt8}, p::Int, ::Type{Char})
return p + 1, Char(unsafe_load(pointer(data, p)))
end

function loadauxvalue(data::Vector{UInt8}, p::Int, ::Type{Vector{T}}) where T
function loadauxvalue(data::AbstractVector{UInt8}, p::Int, ::Type{Vector{T}}) where T
n = unsafe_load(Ptr{Int32}(pointer(data, p)))
p += 4
xs = Vector{T}(undef, n)
unsafe_copyto!(pointer(xs), Ptr{T}(pointer(data, p)), n)
return p + n * sizeof(T), xs
end

function loadauxvalue(data::Vector{UInt8}, p::Int, ::Type{String})
function loadauxvalue(data::AbstractVector{UInt8}, p::Int, ::Type{String})
dataptr = pointer(data, p)
endptr = ccall(:memchr, Ptr{Cvoid}, (Ptr{Cvoid}, Cint, Csize_t), dataptr, '\0', length(data) - p + 1)
q::Int = p + (endptr - dataptr) - 1
return q + 2, String(data[p:q])
end

function findauxtag(data::Vector{UInt8}, start::Int, stop::Int, t1::UInt8, t2::UInt8)
function findauxtag(data::AbstractVector{UInt8}, start::Int, stop::Int, t1::UInt8, t2::UInt8)
pos = start

while pos ≤ stop && !(data[pos] == t1 && data[pos+1] == t2)
Expand All @@ -123,7 +123,7 @@ end

# Find the starting position of a next tag in `data` after `p`.
# `(data[p], data[p+1])` is supposed to be a current tag.
function next_tag_position(data::Vector{UInt8}, p::Int)
function next_tag_position(data::AbstractVector{UInt8}, p::Int)
typ = Char(data[p+2])
p += 3

Expand Down
4 changes: 2 additions & 2 deletions src/bam/bam.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ module BAM
using BioGenerics
using GenomicFeatures
using XAM.SAM
using CodecBGZF

import BGZFStreams
import BioAlignments
import Indexes
import BioSequences
import BioGenerics: isfilled, header

import GenomicFeatures: eachoverlap


include("../common.jl")
include("bai.jl")
include("auxdata.jl")
include("reader.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/bam/overlap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ end
function Base.iterate(iter::OverlapIterator, state)
while state.chunkid ≤ lastindex(state.chunks)
chunk = state.chunks[state.chunkid]
while BGZFStreams.virtualoffset(iter.reader.stream) < chunk.stop
while VirtualOffset(iter.reader.stream) < chunk.stop
read!(iter.reader, state.record)
c = compare_intervals(state.record, (state.refindex, iter.interval))
if c == 0
Expand Down
14 changes: 6 additions & 8 deletions src/bam/reader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ Create a data reader of the BAM file format.
* `index=nothing`: filepath to a random access index (currently *bai* is supported)
"""
mutable struct Reader{T} <: BioGenerics.IO.AbstractReader
stream::BGZFStreams.BGZFStream{T}
stream::BGZFDecompressorStream{T}
header::SAM.Header
start_offset::BGZFStreams.VirtualOffset
start_offset::VirtualOffset
refseqnames::Vector{String}
refseqlens::Vector{Int}
index::Union{Nothing, BAI}
Expand Down Expand Up @@ -64,7 +64,7 @@ function header(reader::Reader; fillSQ::Bool=false)::SAM.Header
return header
end

function Base.seek(reader::Reader, voffset::BGZFStreams.VirtualOffset)
function Base.seek(reader::Reader, voffset::CodecBGZF.VirtualOffset)
seek(reader.stream, voffset)
end

Expand All @@ -80,7 +80,7 @@ function Base.iterate(reader::Reader, nextone = Record())
end

# Initialize a BAM reader by reading the header section.
function init_bam_reader(input::BGZFStreams.BGZFStream)
function init_bam_reader(input::BGZFDecompressorStream)
# magic bytes
B = read(input, UInt8)
A = read(input, UInt8)
Expand Down Expand Up @@ -108,9 +108,7 @@ function init_bam_reader(input::BGZFStreams.BGZFStream)
refseqlens[i] = seqlen
end

voffset = isa(input.io, Base.AbstractPipe) ?
BGZFStreams.VirtualOffset(0, 0) :
BGZFStreams.virtualoffset(input)
voffset = VirtualOffset(input)

return Reader(
input,
Expand All @@ -122,7 +120,7 @@ function init_bam_reader(input::BGZFStreams.BGZFStream)
end

function init_bam_reader(input::IO)
return init_bam_reader(BGZFStreams.BGZFStream(input))
return init_bam_reader(BGZFDecompressorStream(input))
end

function _read!(reader::Reader, record)
Expand Down
Loading