2019-09-07 11:42:06 +00:00
|
|
|
|
@inline function anchor!(stream::BufferedStreams.BufferedInputStream, p, immobilize = true)
|
|
|
|
|
stream.anchor = p
|
|
|
|
|
stream.immobilized = immobilize
|
|
|
|
|
return stream
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@inline function upanchor!(stream::BufferedStreams.BufferedInputStream)
|
|
|
|
|
@assert stream.anchor != 0 "upanchor! called with no anchor set"
|
|
|
|
|
anchor = stream.anchor
|
|
|
|
|
stream.anchor = 0
|
|
|
|
|
stream.immobilized = false
|
|
|
|
|
return anchor
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function ensure_margin!(stream::BufferedStreams.BufferedInputStream)
|
|
|
|
|
if stream.position * 20 > length(stream.buffer) * 19
|
|
|
|
|
BufferedStreams.shiftdata!(stream)
|
|
|
|
|
end
|
|
|
|
|
return nothing
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@inline function resize_and_copy!(dst::Vector{UInt8}, src::Vector{UInt8}, r::UnitRange{Int})
|
|
|
|
|
return resize_and_copy!(dst, 1, src, r)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@inline function resize_and_copy!(dst::Vector{UInt8}, dstart::Int, src::Vector{UInt8}, r::UnitRange{Int})
|
|
|
|
|
rlen = length(r)
|
|
|
|
|
if length(dst) != dstart + rlen - 1
|
|
|
|
|
resize!(dst, dstart + rlen - 1)
|
|
|
|
|
end
|
|
|
|
|
copyto!(dst, dstart, src, first(r), rlen)
|
|
|
|
|
return dst
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function generate_index_function(record_type, machine, init_code, actions; kwargs...)
|
|
|
|
|
kwargs = Dict(kwargs)
|
|
|
|
|
context = Automa.CodeGenContext(
|
|
|
|
|
generator = get(kwargs, :generator, :goto),
|
|
|
|
|
checkbounds = get(kwargs, :checkbounds, false),
|
|
|
|
|
loopunroll = get(kwargs, :loopunroll, 0)
|
|
|
|
|
)
|
|
|
|
|
quote
|
|
|
|
|
function index!(record::$(record_type))
|
|
|
|
|
data = record.data
|
|
|
|
|
p = 1
|
|
|
|
|
p_end = p_eof = sizeof(data)
|
|
|
|
|
initialize!(record)
|
|
|
|
|
$(init_code)
|
|
|
|
|
cs = $(machine.start_state)
|
|
|
|
|
$(Automa.generate_exec_code(context, machine, actions))
|
|
|
|
|
if cs != 0
|
|
|
|
|
throw(ArgumentError(string("failed to index ", $(record_type), " ~>", repr(String(data[p:min(p+7,p_end)])))))
|
|
|
|
|
end
|
|
|
|
|
@assert isfilled(record)
|
|
|
|
|
return record
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function generate_readheader_function(reader_type, metainfo_type, machine, init_code, actions, finish_code=:())
|
|
|
|
|
quote
|
|
|
|
|
function readheader!(reader::$(reader_type))
|
|
|
|
|
_readheader!(reader, reader.state)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function _readheader!(reader::$(reader_type), state::State)
|
|
|
|
|
stream = state.stream
|
|
|
|
|
ensure_margin!(stream)
|
|
|
|
|
cs = state.cs
|
|
|
|
|
linenum = state.linenum
|
|
|
|
|
data = stream.buffer
|
|
|
|
|
p = stream.position
|
|
|
|
|
p_end = stream.available
|
|
|
|
|
p_eof = -1
|
|
|
|
|
finish_header = false
|
|
|
|
|
record = $(metainfo_type)()
|
|
|
|
|
|
|
|
|
|
$(init_code)
|
|
|
|
|
|
|
|
|
|
while true
|
|
|
|
|
$(Automa.generate_exec_code(Automa.CodeGenContext(generator=:table), machine, actions))
|
|
|
|
|
|
|
|
|
|
state.cs = cs
|
|
|
|
|
state.finished = cs == 0
|
|
|
|
|
state.linenum = linenum
|
|
|
|
|
stream.position = p
|
|
|
|
|
|
|
|
|
|
if cs < 0
|
|
|
|
|
error("$($(reader_type)) file format error on line ", linenum)
|
|
|
|
|
elseif finish_header
|
|
|
|
|
$(finish_code)
|
|
|
|
|
break
|
|
|
|
|
elseif p > p_eof ≥ 0
|
|
|
|
|
error("incomplete $($(reader_type)) input on line ", linenum)
|
|
|
|
|
else
|
|
|
|
|
hits_eof = BufferedStreams.fillbuffer!(stream) == 0
|
|
|
|
|
p = stream.position
|
|
|
|
|
p_end = stream.available
|
|
|
|
|
if hits_eof
|
|
|
|
|
p_eof = p_end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function generate_read_function(reader_type, machine, init_code, actions; kwargs...)
|
|
|
|
|
kwargs = Dict(kwargs)
|
|
|
|
|
context = Automa.CodeGenContext(
|
|
|
|
|
generator=get(kwargs, :generator, :goto),
|
|
|
|
|
checkbounds=get(kwargs, :checkbounds, false),
|
|
|
|
|
loopunroll=get(kwargs, :loopunroll, 0)
|
|
|
|
|
)
|
|
|
|
|
quote
|
|
|
|
|
function Base.read!(reader::$(reader_type), record::eltype($(reader_type)))::eltype($(reader_type))
|
|
|
|
|
return _read!(reader, reader.state, record)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function _read!(reader::$(reader_type), state::State, record::eltype($(reader_type)))
|
|
|
|
|
stream = state.stream
|
|
|
|
|
ensure_margin!(stream)
|
|
|
|
|
cs = state.cs
|
|
|
|
|
linenum = state.linenum
|
|
|
|
|
data = stream.buffer
|
|
|
|
|
p = stream.position
|
|
|
|
|
p_end = stream.available
|
|
|
|
|
p_eof = -1
|
|
|
|
|
found_record = false
|
|
|
|
|
initialize!(record)
|
|
|
|
|
|
|
|
|
|
$(init_code)
|
|
|
|
|
|
|
|
|
|
if state.finished
|
|
|
|
|
throw(EOFError())
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
while true
|
|
|
|
|
$(Automa.generate_exec_code(context, machine, actions))
|
|
|
|
|
|
|
|
|
|
state.cs = cs
|
|
|
|
|
state.finished |= cs == 0
|
|
|
|
|
state.linenum = linenum
|
|
|
|
|
stream.position = p
|
|
|
|
|
|
|
|
|
|
if cs < 0
|
|
|
|
|
error($(reader_type), " file format error on line ", linenum, " ~>", repr(String(data[p:min(p+7,p_end)])))
|
|
|
|
|
elseif found_record
|
|
|
|
|
break
|
|
|
|
|
elseif cs == 0
|
|
|
|
|
throw(EOFError())
|
|
|
|
|
elseif p > p_eof ≥ 0
|
|
|
|
|
error("incomplete $($(reader_type)) input on line ", linenum)
|
|
|
|
|
elseif BufferedStreams.available_bytes(stream) < 64
|
|
|
|
|
hits_eof = BufferedStreams.fillbuffer!(stream) == 0
|
|
|
|
|
p = stream.position
|
|
|
|
|
p_end = stream.available
|
|
|
|
|
if hits_eof
|
|
|
|
|
p_eof = p_end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@assert isfilled(record)
|
|
|
|
|
return record
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
# Automa.jl generated readrecord! and readmetainfo! functions
|
|
|
|
|
# ========================================
|
|
|
|
|
|
|
|
|
|
# file = header . body
|
|
|
|
|
# header = metainfo*
|
|
|
|
|
# body = record*
|
|
|
|
|
const sam_metainfo_machine, sam_record_machine, sam_header_machine, sam_body_machine = (function ()
|
2019-09-07 11:47:00 +00:00
|
|
|
|
|
|
|
|
|
isinteractive() && info("compiling SAM")
|
|
|
|
|
|
2019-09-07 11:42:06 +00:00
|
|
|
|
cat = Automa.RegExp.cat
|
|
|
|
|
rep = Automa.RegExp.rep
|
|
|
|
|
alt = Automa.RegExp.alt
|
|
|
|
|
opt = Automa.RegExp.opt
|
|
|
|
|
any = Automa.RegExp.any
|
|
|
|
|
|
|
|
|
|
metainfo = let
|
|
|
|
|
tag = re"[A-Z][A-Z]" \ cat("CO")
|
2019-11-30 10:18:34 +00:00
|
|
|
|
tag.actions[:enter] = [:pos1]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
tag.actions[:exit] = [:metainfo_tag]
|
|
|
|
|
|
|
|
|
|
dict = let
|
|
|
|
|
key = re"[A-Za-z][A-Za-z0-9]"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
key.actions[:enter] = [:pos2]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
key.actions[:exit] = [:metainfo_dict_key]
|
|
|
|
|
val = re"[ -~]+"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
val.actions[:enter] = [:pos2]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
val.actions[:exit] = [:metainfo_dict_val]
|
|
|
|
|
keyval = cat(key, ':', val)
|
|
|
|
|
|
|
|
|
|
cat(keyval, rep(cat('\t', keyval)))
|
|
|
|
|
end
|
2019-11-30 10:18:34 +00:00
|
|
|
|
dict.actions[:enter] = [:pos1]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
dict.actions[:exit] = [:metainfo_val]
|
|
|
|
|
|
|
|
|
|
co = cat("CO")
|
2019-11-30 10:18:34 +00:00
|
|
|
|
co.actions[:enter] = [:pos1]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
co.actions[:exit] = [:metainfo_tag]
|
|
|
|
|
|
|
|
|
|
comment = re"[^\r\n]*"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
comment.actions[:enter] = [:pos1]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
comment.actions[:exit] = [:metainfo_val]
|
|
|
|
|
|
|
|
|
|
cat('@', alt(cat(tag, '\t', dict), cat(co, '\t', comment)))
|
|
|
|
|
end
|
2019-11-30 10:18:34 +00:00
|
|
|
|
metainfo.actions[:enter] = [:mark]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
metainfo.actions[:exit] = [:metainfo]
|
|
|
|
|
|
|
|
|
|
record = let
|
|
|
|
|
qname = re"[!-?A-~]+"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
qname.actions[:enter] = [:pos]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
qname.actions[:exit] = [:record_qname]
|
|
|
|
|
|
|
|
|
|
flag = re"[0-9]+"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
flag.actions[:enter] = [:pos]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
flag.actions[:exit] = [:record_flag]
|
|
|
|
|
|
|
|
|
|
rname = re"\*|[!-()+-<>-~][!-~]*"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
rname.actions[:enter] = [:pos]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
rname.actions[:exit] = [:record_rname]
|
|
|
|
|
|
|
|
|
|
pos = re"[0-9]+"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
pos.actions[:enter] = [:pos]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
pos.actions[:exit] = [:record_pos]
|
|
|
|
|
|
|
|
|
|
mapq = re"[0-9]+"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
mapq.actions[:enter] = [:pos]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
mapq.actions[:exit] = [:record_mapq]
|
|
|
|
|
|
|
|
|
|
cigar = re"\*|([0-9]+[MIDNSHPX=])+"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
cigar.actions[:enter] = [:pos]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
cigar.actions[:exit] = [:record_cigar]
|
|
|
|
|
|
|
|
|
|
rnext = re"\*|=|[!-()+-<>-~][!-~]*"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
rnext.actions[:enter] = [:pos]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
rnext.actions[:exit] = [:record_rnext]
|
|
|
|
|
|
|
|
|
|
pnext = re"[0-9]+"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
pnext.actions[:enter] = [:pos]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
pnext.actions[:exit] = [:record_pnext]
|
|
|
|
|
|
|
|
|
|
tlen = re"[-+]?[0-9]+"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
tlen.actions[:enter] = [:pos]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
tlen.actions[:exit] = [:record_tlen]
|
|
|
|
|
|
|
|
|
|
seq = re"\*|[A-Za-z=.]+"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
seq.actions[:enter] = [:pos]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
seq.actions[:exit] = [:record_seq]
|
|
|
|
|
|
|
|
|
|
qual = re"[!-~]+"
|
2019-11-30 10:18:34 +00:00
|
|
|
|
qual.actions[:enter] = [:pos]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
qual.actions[:exit] = [:record_qual]
|
|
|
|
|
|
|
|
|
|
field = let
|
|
|
|
|
tag = re"[A-Za-z][A-Za-z0-9]"
|
|
|
|
|
val = alt(
|
|
|
|
|
re"A:[!-~]",
|
|
|
|
|
re"i:[-+]?[0-9]+",
|
|
|
|
|
re"f:[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?",
|
|
|
|
|
re"Z:[ !-~]*",
|
|
|
|
|
re"H:([0-9A-F][0-9A-F])*",
|
|
|
|
|
re"B:[cCsSiIf](,[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?)+")
|
|
|
|
|
|
|
|
|
|
cat(tag, ':', val)
|
|
|
|
|
end
|
2019-11-30 10:18:34 +00:00
|
|
|
|
field.actions[:enter] = [:pos]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
field.actions[:exit] = [:record_field]
|
|
|
|
|
|
|
|
|
|
cat(
|
|
|
|
|
qname, '\t',
|
|
|
|
|
flag, '\t',
|
|
|
|
|
rname, '\t',
|
|
|
|
|
pos, '\t',
|
|
|
|
|
mapq, '\t',
|
|
|
|
|
cigar, '\t',
|
|
|
|
|
rnext, '\t',
|
|
|
|
|
pnext, '\t',
|
|
|
|
|
tlen, '\t',
|
|
|
|
|
seq, '\t',
|
|
|
|
|
qual,
|
|
|
|
|
rep(cat('\t', field)))
|
|
|
|
|
end
|
2019-11-30 10:18:34 +00:00
|
|
|
|
record.actions[:enter] = [:mark]
|
2019-09-07 11:42:06 +00:00
|
|
|
|
record.actions[:exit] = [:record]
|
|
|
|
|
|
|
|
|
|
newline = let
|
|
|
|
|
lf = re"\n"
|
|
|
|
|
lf.actions[:enter] = [:countline]
|
|
|
|
|
|
|
|
|
|
cat(re"\r?", lf)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
header′ = rep(cat(metainfo, newline))
|
|
|
|
|
header′.actions[:exit] = [:header]
|
|
|
|
|
header = cat(header′, opt(any() \ cat('@'))) # look ahead
|
|
|
|
|
|
|
|
|
|
body = rep(cat(record, newline))
|
|
|
|
|
|
|
|
|
|
return map(Automa.compile, (metainfo, record, header, body))
|
|
|
|
|
end)()
|
|
|
|
|
|
|
|
|
|
const sam_metainfo_actions = Dict(
|
2019-11-30 10:18:34 +00:00
|
|
|
|
:metainfo_tag => :(record.tag = (pos1:p-1) .- offset),
|
|
|
|
|
:metainfo_val => :(record.val = (pos1:p-1) .- offset),
|
|
|
|
|
:metainfo_dict_key => :(push!(record.dictkey, (pos2:p-1) .- offset)),
|
|
|
|
|
:metainfo_dict_val => :(push!(record.dictval, (pos2:p-1) .- offset)),
|
2019-09-07 11:42:06 +00:00
|
|
|
|
:metainfo => quote
|
|
|
|
|
resize_and_copy!(record.data, data, offset+1:p-1)
|
|
|
|
|
record.filled = (offset+1:p-1) .- offset
|
|
|
|
|
end,
|
2019-11-30 10:18:34 +00:00
|
|
|
|
:mark => :(),
|
|
|
|
|
:pos1 => :(pos1 = p),
|
|
|
|
|
:pos2 => :(pos2 = p)
|
2019-09-07 23:08:12 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
generate_index_function(
|
|
|
|
|
MetaInfo,
|
|
|
|
|
sam_metainfo_machine,
|
2019-11-30 10:18:34 +00:00
|
|
|
|
:(pos1 = pos2 = offset = 0),
|
2019-09-07 23:08:12 +00:00
|
|
|
|
sam_metainfo_actions
|
|
|
|
|
) |> eval
|
|
|
|
|
|
|
|
|
|
generate_readheader_function(
|
|
|
|
|
Reader,
|
|
|
|
|
MetaInfo,
|
|
|
|
|
sam_header_machine,
|
2019-11-30 10:18:34 +00:00
|
|
|
|
:(pos1 = pos2 = offset = 0),
|
2019-09-07 23:08:12 +00:00
|
|
|
|
merge(sam_metainfo_actions, Dict(
|
|
|
|
|
:metainfo => quote
|
|
|
|
|
resize_and_copy!(record.data, data, upanchor!(stream):p-1)
|
|
|
|
|
record.filled = (offset+1:p-1) .- offset
|
|
|
|
|
@assert isfilled(record)
|
|
|
|
|
push!(reader.header.metainfo, record)
|
|
|
|
|
ensure_margin!(stream)
|
|
|
|
|
record = MetaInfo()
|
|
|
|
|
end,
|
|
|
|
|
:header => :(finish_header = true; @escape),
|
|
|
|
|
:countline => :(linenum += 1),
|
2019-11-30 10:18:34 +00:00
|
|
|
|
:mark => :(anchor!(stream, p); offset = p - 1))),
|
2019-09-07 23:08:12 +00:00
|
|
|
|
quote
|
|
|
|
|
if !eof(stream)
|
|
|
|
|
stream.position -= 1 # cancel look-ahead
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
) |> eval
|
2019-09-07 11:42:06 +00:00
|
|
|
|
|
|
|
|
|
const sam_record_actions = Dict(
|
2019-11-30 10:18:34 +00:00
|
|
|
|
:record_qname => :(record.qname = (pos:p-1) .- offset),
|
|
|
|
|
:record_flag => :(record.flag = (pos:p-1) .- offset),
|
|
|
|
|
:record_rname => :(record.rname = (pos:p-1) .- offset),
|
|
|
|
|
:record_pos => :(record.pos = (pos:p-1) .- offset),
|
|
|
|
|
:record_mapq => :(record.mapq = (pos:p-1) .- offset),
|
|
|
|
|
:record_cigar => :(record.cigar = (pos:p-1) .- offset),
|
|
|
|
|
:record_rnext => :(record.rnext = (pos:p-1) .- offset),
|
|
|
|
|
:record_pnext => :(record.pnext = (pos:p-1) .- offset),
|
|
|
|
|
:record_tlen => :(record.tlen = (pos:p-1) .- offset),
|
|
|
|
|
:record_seq => :(record.seq = (pos:p-1) .- offset),
|
|
|
|
|
:record_qual => :(record.qual = (pos:p-1) .- offset),
|
|
|
|
|
:record_field => :(push!(record.fields, (pos:p-1) .- offset)),
|
2019-09-07 11:42:06 +00:00
|
|
|
|
:record => quote
|
|
|
|
|
resize_and_copy!(record.data, data, 1:p-1)
|
|
|
|
|
record.filled = (offset+1:p-1) .- offset
|
|
|
|
|
end,
|
2019-11-30 10:18:34 +00:00
|
|
|
|
:mark => :(),
|
|
|
|
|
:pos => :(pos = p)
|
2019-09-07 23:08:12 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
generate_index_function(
|
|
|
|
|
Record,
|
|
|
|
|
sam_record_machine,
|
2019-11-30 10:18:34 +00:00
|
|
|
|
:(pos = offset = 0),
|
2019-09-07 23:08:12 +00:00
|
|
|
|
sam_record_actions
|
|
|
|
|
) |> eval
|
|
|
|
|
|
|
|
|
|
generate_read_function(
|
|
|
|
|
Reader,
|
|
|
|
|
sam_body_machine,
|
2019-11-30 10:18:34 +00:00
|
|
|
|
:(pos = offset = 0),
|
2019-09-07 23:08:12 +00:00
|
|
|
|
merge(sam_record_actions, Dict(
|
|
|
|
|
:record => quote
|
|
|
|
|
resize_and_copy!(record.data, data, upanchor!(stream):p-1)
|
|
|
|
|
record.filled = (offset+1:p-1) .- offset
|
|
|
|
|
found_record = true
|
|
|
|
|
@escape
|
|
|
|
|
end,
|
|
|
|
|
:countline => :(linenum += 1),
|
2019-11-30 10:18:34 +00:00
|
|
|
|
:mark => :(anchor!(stream, p); offset = p - 1))
|
2019-09-07 23:08:12 +00:00
|
|
|
|
)
|
|
|
|
|
) |> eval
|