@@ -116,7 +116,8 @@ export
116116 compatbits,
117117 alphabet,
118118 encoded_data,
119- encode
119+ encode,
120+ stringbyte
120121
121122
122123"""
@@ -146,6 +147,42 @@ Base.broadcastable(x::BioSymbol) = (x,)
146147include (" nucleicacid.jl" )
147148include (" aminoacid.jl" )
148149
150+ # Less efficient fallback. Should only be called for symbols of AsciiAlphabet
151+ """
152+ stringbyte(::BioSymbol)::UInt8
153+
154+ For biosymbol types that can be represented as ASCII characters, `stringbyte(x)`
155+ returns the printable ASCII byte that represents the character in a string.
156+
157+ # Examples
158+ ```julia
159+ julia> stringbyte(DNA_A) == UInt8('A')
160+ true
161+
162+ julia> stringbyte(AA_Gap) == UInt8('-')
163+ true
164+ ```
165+ """
166+ function stringbyte end
167+
168+ # Create a lookup table from biosymbol to the UInt8 for the character that would
169+ # represent it in a string, e.g. DNA_G -> UInt8('G')
170+ for alphabettype in (" DNA" , " RNA" , " AminoAcid" )
171+ tablename = Symbol (uppercase (alphabettype), " _TO_BYTE" )
172+ typ = Symbol (alphabettype)
173+ @eval begin
174+ const $ (tablename) = let
175+ alph = alphabet ($ (typ))
176+ bytes = zeros (UInt8, length (alph))
177+ @inbounds for letter in alph
178+ bytes[reinterpret (UInt8, letter) + 1 ] = UInt8 (Char (letter))
179+ end
180+ Tuple (bytes)
181+ end
182+ stringbyte (x:: $ (typ)) = @inbounds $ (tablename)[reinterpret (UInt8, x) + 1 ]
183+ end
184+ end
185+
149186"""
150187 isgap(symbol::BioSymbol)
151188
0 commit comments