module Biobase.Primary where
import Data.Char (toUpper)
import Data.Ix (Ix(..))
import Data.Primitive.Types
import Data.Tuple (swap)
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
import qualified Data.Text as T
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Vector.Unboxed as VU
import Biobase.Primary.Bounds
class MkPrimary a where
mkPrimary :: a -> Primary
type Primary = VU.Vector Nuc
newtype Nuc = Nuc {unNuc :: Int}
deriving (Eq,Ord,Ix)
(nN : nA : nC : nG : nT : nIMI : _) = map Nuc [0 .. ]
nU = nT
acgt = [nA..nT]
acgu = acgt
nacgt = [nN..nT]
nacgu = nacgt
mkNuc :: Char -> Nuc
mkNuc = f . toUpper where
f k
| Just v <- k `lookup` charNucList = v
| otherwise = nN
fromNuc :: Nuc -> Char
fromNuc = f where
f k
| Just v <- k `lookup` nucCharList = v
| otherwise = 'N'
charNucList =
[ ('N',nN)
, ('A',nA)
, ('C',nC)
, ('G',nG)
, ('T',nT)
, ('U',nU)
]
nucCharList = map swap charNucList
instance Show Nuc where
show n = [fromNuc n]
instance Read Nuc where
readsPrec p [] = []
readsPrec p (x:xs)
| x ==' ' = readsPrec p xs
| Just n <- x `lookup` charNucList = [(n,xs)]
| otherwise = []
deriving instance Prim Nuc
deriving instance VGM.MVector VU.MVector Nuc
deriving instance VG.Vector VU.Vector Nuc
deriving instance VU.Unbox Nuc
instance Bounded Nuc where
minBound = nN
maxBound = nT
instance Bounds Nuc where
minNormal = nA
maxNormal = nT
minExtended = nN
maxExtended = nT
instance Enum Nuc where
toEnum = Nuc
fromEnum = unNuc
instance MkPrimary String where
mkPrimary = VU.fromList . map mkNuc
instance MkPrimary BS.ByteString where
mkPrimary = mkPrimary . BS.unpack
instance MkPrimary BSL.ByteString where
mkPrimary = mkPrimary . BSL.unpack
instance MkPrimary T.Text where
mkPrimary = mkPrimary . T.unpack
instance MkPrimary [Nuc] where
mkPrimary = VU.fromList