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

Move some indexing logic from LongSequence to BioSequence #133

Merged
merged 3 commits into from
Mar 10, 2021
Merged
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
30 changes: 30 additions & 0 deletions src/biosequence/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,36 @@ function Base.setindex!(seq::BioSequence, x, locs::AbstractVector{<:Integer})
return unsafe_setindex!(seq, x, locs)
end

function Base.setindex!(seq::BioSequence, x::BioSequence, locs::AbstractVector{<:Integer})
@boundscheck checkbounds(seq, locs)
checkdimension(x, locs)
@inbounds for i in eachindex(locs)
unsafe_setindex!(seq, x[i], locs[i])
end
seq
end

function Base.setindex!(seq::BioSequence, x::BioSequence, locs::AbstractVector{Bool})
@boundscheck checkbounds(seq, locs)
checkdimension(x, locs)
j = 0
@inbounds for i in eachindex(locs)
if locs[i]
j += 1
unsafe_setindex!(seq, x[j], i)
end
end
seq
end

function Base.setindex!(seq::BioSequence, other::BioSequence, ::Colon)
return setindex!(seq, other, 1:lastindex(seq))
end

function Base.setindex!(seq::BioSequence, x, ::Colon)
return setindex!(seq, x, 1:lastindex(seq))
end

@inline function Base.iterate(seq::BioSequence, i::Int = firstindex(seq))
if i > lastindex(seq)
return nothing
Expand Down
80 changes: 34 additions & 46 deletions src/longsequences/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,45 +10,22 @@
return bitindex(BitsPerSymbol(seq), encoded_data_eltype(seq), i + first(seq.part) - 1)
end

# More efficient due to copyto!
function Base.getindex(seq::LongSequence, part::UnitRange{<:Integer})
@boundscheck checkbounds(seq, part)
newseq = typeof(seq)(length(part))
return copyto!(newseq, 1, seq, first(part), length(part))
end

# More efficient due to copyto!
function Base.setindex!(seq::SeqOrView{A},
other::SeqOrView{A},
locs::AbstractVector{<:Integer}) where {A}
@boundscheck checkbounds(seq, locs)
checkdimension(other, locs)
return unsafe_setindex!(seq, other, locs)
end

function Base.setindex!(seq::SeqOrView{A},
other::SeqOrView{A},
locs::AbstractVector{Bool}) where {A}
@boundscheck checkbounds(seq, locs)
checkdimension(other, locs)
return unsafe_setindex!(seq, other, locs)
end

function Base.setindex!(seq::SeqOrView{A},
other::SeqOrView{A},
locs::UnitRange{<:Integer}) where {A}
locs::UnitRange{<:Integer}) where {A <: Alphabet}
@boundscheck checkbounds(seq, locs)
checkdimension(other, locs)
return copyto!(seq, locs.start, other, 1, length(locs))
end

function Base.setindex!(seq::SeqOrView{A},
other::SeqOrView{A}, ::Colon) where {A}
return setindex!(seq, other, 1:lastindex(seq))
end

function Base.setindex!(seq::SeqOrView{A}, x, ::Colon) where {A}
return setindex!(seq, x, 1:lastindex(seq))
end

@inline function encoded_setindex!(seq::SeqOrView{A},
bin::Unsigned, i::Integer) where {A <: Alphabet}
return encoded_setindex!(seq, bin, bitindex(seq, i))
Expand All @@ -63,6 +40,12 @@ end
return s
end

function Base.setindex!(seq::SeqOrView, x, locs::AbstractVector{<:Integer})
@boundscheck checkbounds(seq, locs)
unsafe_setindex!(seq, x, locs)
end


@inline function unsafe_setindex!(seq::SeqOrView, x, locs::AbstractVector{<:Integer})
bin = encode(Alphabet(seq), convert(eltype(seq), x))
for i in locs
Expand All @@ -71,6 +54,21 @@ end
return seq
end

# Only to avoid ambiguity
function Base.setindex!(seq::SeqOrView, x::SeqOrView, locs::AbstractVector{Bool})
@boundscheck checkbounds(seq, locs)
checkdimension(x, locs)
j = 0
@inbounds for i in eachindex(locs)
if locs[i]
j += 1
seq[i] = x[j]
end
end
return seq
end

# To save encoding.
function unsafe_setindex!(seq::SeqOrView, x, locs::AbstractVector{Bool})
bin = encode(Alphabet(seq), convert(eltype(seq), x))
i = j = 0
Expand All @@ -84,24 +82,14 @@ function unsafe_setindex!(seq::SeqOrView, x, locs::AbstractVector{Bool})
return seq
end

function unsafe_setindex!(seq::SeqOrView{A}, other::SeqOrView{A},
locs::AbstractVector{<:Integer}) where {A <: Alphabet}
for (i, x) in zip(locs, other)
unsafe_setindex!(seq, x, i)
end
return seq
end

function unsafe_setindex!(seq::SeqOrView{A},
other::SeqOrView{A},
locs::AbstractVector{Bool}) where {A <: Alphabet}
i = j = 0
while true
i = findnext(locs, i + 1)
if i === nothing
break
end
unsafe_setindex!(seq, other[j += 1], i)
end
return seq
# To avoid ambiguity errors
function Base.setindex!(seq::SeqOrView{A},
other::SeqOrView{A},
locs::AbstractVector{<:Integer}) where {A <: Alphabet}
@boundscheck checkbounds(seq, locs)
checkdimension(other, locs)
@inbounds for (i, n) in zip(locs, other)
seq[i] = n
end
return seq
end
4 changes: 2 additions & 2 deletions src/longsequences/seqview.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ end

# These unions are significant because LongSubSeq and LongSequence have the same
# encoding underneath, so many methods can be shared.
const SeqOrView{A} = Union{LongSequence{A}, LongSubSeq{A}}
const SeqOrView{A} = Union{LongSequence{A}, LongSubSeq{A}} where {A <: Alphabet}
const NucleicSeqOrView = SeqOrView{<:NucleicAcidAlphabet}

Base.length(v::LongSubSeq) = last(v.part) - first(v.part) + 1
Expand All @@ -27,7 +27,7 @@ function LongSubSeq(seq::SeqOrView{A}) where {A <: Alphabet}
return LongSubSeq{A}(seq)
end

function LongSubSeq{A}(seq::LongSequence{A}, part::UnitRange{Int}) where {A <: Alphabet}
function LongSubSeq{A}(seq::LongSequence{A}, part::UnitRange{<:Integer}) where {A <: Alphabet}
@boundscheck checkbounds(seq, part)
return LongSubSeq{A}(seq.data, part)
end
Expand Down