module Biobase.GeneticCodes.Translation where
import Control.Lens
import qualified Data.Map.Strict as M
import qualified Data.ByteString.Char8 as BS
import Biobase.Types.BioSequence
import Biobase.Types.Codon
import Biobase.GeneticCodes.Types
class Translation t where
type TargetType t ∷ *
type CodonType t ∷ *
type AAType t ∷ *
translate ∷ TranslationTable (CodonType t) (AAType t) → t → TargetType t
instance Translation (Codon Char) where
type TargetType (Codon Char) = Char
type CodonType (Codon Char) = Char
type AAType (Codon Char) = Char
translate tbl t = maybe 'X' _aminoAcid $ M.lookup t (tbl^.codonToAminoAcid)
{-# Inline translate #-}
instance Translation String where
type TargetType String = String
type CodonType String = Char
type AAType String = Char
translate tbl =
let go xs | [x,y,z] ← hd = translate tbl (Codon x y z) : go tl
| otherwise = []
where (hd,tl) = splitAt 3 xs
in go
instance Translation (BioSequence DNA) where
type TargetType (BioSequence DNA) = BioSequence AA
type CodonType (BioSequence DNA) = Char
type AAType (BioSequence DNA) = Char
translate tbl (BioSequence xs) =
let go k = Just (translate tbl $ Codon (BS.index xs k) (BS.index xs (k+1)) (BS.index xs (k+2)) ,k+3)
in BioSequence . fst $ BS.unfoldrN (BS.length xs `div` 3) go 0