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

More consistently throw EncodeError #319

Merged
merged 1 commit into from
Oct 22, 2024
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
25 changes: 21 additions & 4 deletions src/alphabet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,31 @@ iscomplete(A::Alphabet) = Val(length(symbols(A)) === 1 << bits_per_symbol(A))
## Encoders & Decoders

"""
encode(::Alphabet, x::S)
encode(::Alphabet, s::BioSymbol)


Encode BioSymbol `S` to an internal representation using an `Alphabet`.
Internal function, do not use in user code.
Encode BioSymbol `s` to an internal representation using an [`Alphabet`](@ref).
This decoding is checked to enforce valid data element.
If `s` cannot be encoded to the given alphabet, throw an `EncodeError`

"""
function encode end
encode(A::Alphabet, s::BioSymbol) = throw(EncodeError(A, s))

"""
EncodeError

Exception thrown when a `BioSymbol` cannot be encoded to a given [`Alphabet`](@ref).

# Examples
```
julia> try
BioSequences.encode(DNAAlphabet{2}(), DNA_N)
catch err
println(err isa BioSequences.EncodeError)
end
true
```
"""
struct EncodeError{A<:Alphabet,T} <: Exception
val::T
end
Expand Down
12 changes: 11 additions & 1 deletion test/alphabet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ encode = BioSequences.encode
EncodeError = BioSequences.EncodeError
decode = BioSequences.decode

@testset "EncodeError" begin
@test_throws EncodeError encode(DNAAlphabet{4}(), RNA_U)
@test_throws EncodeError encode(DNAAlphabet{2}(), DNA_M)
@test_throws EncodeError encode(DNAAlphabet{4}(), AA_C)
@test_throws EncodeError encode(AminoAcidAlphabet(), DNA_C)
@test_throws EncodeError encode(AminoAcidAlphabet(), RNA_N)
@test_throws EncodeError encode(RNAAlphabet{2}(), DNA_C)
@test_throws EncodeError encode(RNAAlphabet{2}(), RNA_K)
end

# NOTE: See the docs for the interface of Alphabet
struct ReducedAAAlphabet <: Alphabet end

Expand Down Expand Up @@ -185,4 +195,4 @@ end
@test BioSequences.has_interface(Alphabet, RNAAlphabet{2}())
@test BioSequences.has_interface(Alphabet, RNAAlphabet{4}())
@test BioSequences.has_interface(Alphabet, AminoAcidAlphabet())
end
end
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using Documenter
using Random
using StableRNGs
using LinearAlgebra: normalize
import BioSymbols
using BioSymbols
using BioSequences
using StatsBase
using YAML
Expand Down
Loading