Skip to content
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "CIGARStrings"
uuid = "e5b51f81-6ffd-4e20-bbec-f118e37ea906"
version = "0.1.4"
version = "0.1.5"
authors = ["Jakob Nybo Nissen <jakobnybonissen@gmail.com>"]

[deps]
Expand Down
1 change: 1 addition & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ However, in order to make zero-copy CIGARs possible, the `BAMCIGAR` type is back

```@docs
CIGARStrings.BAMCIGAR
CIGARStrings.BAMCIGAR(::MutableMemoryView{UInt8}, ::CIGAR)
```

A `BAMCIGAR` can be constructed from its binary representation, using any type which implements `MemoryViews.MemoryView`:
Expand Down
1 change: 1 addition & 0 deletions docs/src/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ end

```@docs; canonical = false
CIGAR
BAMCIGAR
CIGARElement
CIGAROp
Translation
Expand Down
2 changes: 1 addition & 1 deletion src/CIGARStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export CIGAR,
public CIGARError, CIGARErrorType, Errors, try_parse, outside, pos, gap,
TranslationKind, PositionMapper

using MemoryViews: MemoryViews, ImmutableMemoryView, MemoryView
using MemoryViews: MemoryViews, ImmutableMemoryView, MemoryView, MutableMemoryView

struct Unsafe end
const unsafe = Unsafe()
Expand Down
38 changes: 35 additions & 3 deletions src/bamcigar.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ stored compactly in 32-bit integers.
Semantically, a BAMCIGAR behaves much similar to a CIGAR.

Construct a BAMCIGAR either from a CIGAR, taking an optional `Vector{UInt8}`
to use as backing storage, or using [`CIGARStrings.try_parse`](@ref).
to use as backing storage, or using [`CIGARStrings.try_parse`](@ref),
or [`BAMCIGAR(::MutableMemoryView{UInt8}, ::CIGAR)`](@ref)

# Examples
```jldoctest
Expand Down Expand Up @@ -135,20 +136,51 @@ function cigar_view!(v::Vector{UInt8}, x::BAMCIGAR)
end

BAMCIGAR(x::CIGAR) = BAMCIGAR(x, UInt8[])

function BAMCIGAR(x::CIGAR, v::Vector{UInt8})
resize!(v, 4 * length(x))
return @inbounds BAMCIGAR(MemoryView(v), x)
end

"""
BAMCIGAR(mem::MutableMemoryView{UInt8}, x::CIGAR)::BAMCIGAR

Construct a `BAMCIGAR` equal to `x`, using the memory `mem`.
After calling this, `mem` may not be mutated, and is considered
owned by the resulting `BAMCIGAR`.

Throw a `BoundsError` if `length(mem) < 4 * length(x)`.

# Examples
```jldoctest
julia> x = CIGAR("150M3D9S");

julia> mem = MemoryView(zeros(UInt8, 15));

julia> cigar = BAMCIGAR(mem, x)
BAMCIGAR(CIGAR("150M3D9S"))

julia> parent(MemoryView(cigar)) === parent(mem)
true
```
"""
function BAMCIGAR(mem::MutableMemoryView{UInt8}, x::CIGAR)
@boundscheck if length(mem) < 4 * length(x)
throw(BoundsError(mem, 4 * length(x)))
end
mem = @inbounds mem[1:(4 * length(x))]
i = 1
for element in x
u = getfield(element, :x)
for _ in 1:4
@inbounds v[i] = u % UInt8
@inbounds mem[i] = u % UInt8
i += 1
u >>= 8
end
end
return BAMCIGAR(
unsafe,
ImmutableMemoryView(v),
ImmutableMemoryView(mem),
x.aln_len,
x.ref_len,
x.query_len
Expand Down
Loading