Skip to content

Commit

Permalink
More consistently throw EncodeError
Browse files Browse the repository at this point in the history
Add a fallback method to `encode`, such that encoding into an incompatible
alphabet will more consistently throw EncodeError.
This will help users catch these errors.
  • Loading branch information
jakobnissen committed Oct 22, 2024
1 parent c8e348e commit 2d309fa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
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

0 comments on commit 2d309fa

Please sign in to comment.