Skip to content

Commit 50cb02e

Browse files
committed
Invert parsing and construct from string
1 parent 31936ce commit 50cb02e

File tree

3 files changed

+21
-17
lines changed

3 files changed

+21
-17
lines changed

src/BioSequences.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ using Random
204204

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

207+
const SeqLike = Union{AbstractVector, AbstractString}
208+
const ASCIILike = Union{String, SubString{String}}
209+
207210
include("alphabet.jl")
208211

209212
# Load the bit-twiddling internals that optimised BioSequences methods depend on.

src/longsequences/constructors.jl

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,8 @@ function (::Type{T})(seq::LongSequence{<:NucleicAcidAlphabet{N}}) where
6060
end
6161

6262
# Constructors from strings
63-
function LongSequence{A}(s::Union{String, SubString{String}}) where {A<:Alphabet}
64-
return LongSequence{A}(s, codetype(A()))
65-
end
66-
67-
# Generic method for String/Substring.
68-
function LongSequence{A}(s::Union{String, SubString{String}}, ::AlphabetCode) where {A<:Alphabet}
69-
len = length(s)
70-
seq = LongSequence{A}(undef, len)
71-
return copyto!(seq, 1, s, 1, len)
72-
end
73-
74-
function LongSequence{A}(s::Union{String, SubString{String}}, ::AsciiAlphabet) where {A<:Alphabet}
75-
seq = LongSequence{A}(undef, ncodeunits(s))
76-
return encode_chunks!(seq, 1, codeunits(s), 1, ncodeunits(s))
63+
function LongSequence{A}(s::AbstractString) where {A <: Alphabet}
64+
return parse(LongSequence{A}, s)
7765
end
7866

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

88-
Base.parse(::Type{LongSequence{A}}, seq::AbstractString) where A = LongSequence{A}(seq)
76+
Base.parse(::Type{T}, s::AbstractString) where {T <: LongSequence} = parse(T, String(s))
77+
78+
function Base.parse(::Type{LongSequence{A}}, seq::ASCIILike) where {A<:Alphabet}
79+
_parse(LongSequence{A}, seq, codetype(A()))
80+
end
81+
82+
function _parse(::Type{LongSequence{A}}, s::ASCIILike, ::AlphabetCode) where {A<:Alphabet}
83+
len = length(s)
84+
seq = LongSequence{A}(undef, len)
85+
return copyto!(seq, 1, s, 1, len)
86+
end
87+
88+
function _parse(::Type{LongSequence{A}}, s::ASCIILike, ::AsciiAlphabet) where {A<:Alphabet}
89+
seq = LongSequence{A}(undef, ncodeunits(s))
90+
return encode_chunks!(seq, 1, codeunits(s), 1, ncodeunits(s))
91+
end

src/longsequences/copying.jl

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ function _copyto!(dst::SeqOrView{A}, doff::Integer,
107107
end
108108

109109
#########
110-
const SeqLike = Union{AbstractVector, AbstractString}
111-
const ASCIILike = Union{String, SubString{String}}
112110

113111
"""
114112
copy!(dst::LongSequence, src)

0 commit comments

Comments
 (0)