Skip to content

Commit

Permalink
Invert parsing and construct from string
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobnissen committed Jun 16, 2022
1 parent 61557ad commit 85cbc31
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
3 changes: 3 additions & 0 deletions src/BioSequences.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ using Random

BioSymbols.gap(::Type{Char}) = '-'

const SeqLike = Union{AbstractVector, AbstractString}
const ASCIILike = Union{String, SubString{String}}

include("alphabet.jl")

# Load the bit-twiddling internals that optimised BioSequences methods depend on.
Expand Down
33 changes: 18 additions & 15 deletions src/longsequences/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,20 +60,8 @@ function (::Type{T})(seq::LongSequence{<:NucleicAcidAlphabet{N}}) where
end

# Constructors from strings
function LongSequence{A}(s::Union{String, SubString{String}}) where {A<:Alphabet}
return LongSequence{A}(s, codetype(A()))
end

# Generic method for String/Substring.
function LongSequence{A}(s::Union{String, SubString{String}}, ::AlphabetCode) where {A<:Alphabet}
len = length(s)
seq = LongSequence{A}(undef, len)
return copyto!(seq, 1, s, 1, len)
end

function LongSequence{A}(s::Union{String, SubString{String}}, ::AsciiAlphabet) where {A<:Alphabet}
seq = LongSequence{A}(undef, ncodeunits(s))
return encode_chunks!(seq, 1, codeunits(s), 1, ncodeunits(s))
function LongSequence{A}(s::AbstractString) where {A <: Alphabet}
return parse(LongSequence{A}, s)
end

function LongSequence{A}(
Expand All @@ -85,4 +73,19 @@ function LongSequence{A}(
return copyto!(seq, 1, src, first(part), len)
end

Base.parse(::Type{LongSequence{A}}, seq::AbstractString) where A = LongSequence{A}(seq)
Base.parse(::Type{T}, s::AbstractString) where {T <: LongSequence} = parse(T, String(s))

function Base.parse(::Type{LongSequence{A}}, seq::ASCIILike) where {A<:Alphabet}
_parse(LongSequence{A}, seq, codetype(A()))
end

function _parse(::Type{LongSequence{A}}, s::ASCIILike, ::AlphabetCode) where {A<:Alphabet}
len = length(s)
seq = LongSequence{A}(undef, len)
return copyto!(seq, 1, s, 1, len)
end

function _parse(::Type{LongSequence{A}}, s::ASCIILike, ::AsciiAlphabet) where {A<:Alphabet}
seq = LongSequence{A}(undef, ncodeunits(s))
return encode_chunks!(seq, 1, codeunits(s), 1, ncodeunits(s))
end
2 changes: 0 additions & 2 deletions src/longsequences/copying.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,6 @@ function _copyto!(dst::SeqOrView{A}, doff::Integer,
end

#########
const SeqLike = Union{AbstractVector, AbstractString}
const ASCIILike = Union{String, SubString{String}}

"""
copy!(dst::LongSequence, src)
Expand Down

0 comments on commit 85cbc31

Please sign in to comment.