module Biobase.SubstMatrix where
import Control.DeepSeq (NFData(..))
import Data.Aeson (FromJSON,ToJSON)
import Data.Binary (Binary)
import Data.Serialize (Serialize)
import Data.Vector.Unboxed.Deriving
import GHC.Generics (Generic)
import qualified Data.Map.Strict as M
import qualified Data.Vector.Unboxed as VU
import Biobase.Primary.AA (AA,aaRange)
import Biobase.Primary.Letter
import Biobase.Primary.Nuc.DNA (DNA)
import Biobase.Primary.Trans (dnaAAmap)
import Biobase.Types.Odds
import Data.PrimitiveArray
import qualified Biobase.Primary.AA as AA
import qualified Biobase.Primary.Nuc.DNA as D
data Similarity
data Distance
newtype AASubstMat t = AASubstMat { aaSubstMat :: Unboxed (Z:.Letter AA:.Letter AA) DLO }
deriving (Generic,Eq,Read,Show)
instance Binary (AASubstMat t)
instance Serialize (AASubstMat t)
instance NFData (AASubstMat t)
type SubstPAM = AASubstMat Similarity
type SubstBLOSUM = AASubstMat Distance
newtype ANuc3SubstMat t = ANuc3SubstMat { anuc3SubstMat :: Unboxed (Z:.Letter AA:.Letter DNA:.Letter DNA:.Letter DNA) DLO }
deriving (Generic,Eq,Read,Show)
instance Binary (ANuc3SubstMat t)
instance Serialize (ANuc3SubstMat t)
instance NFData (ANuc3SubstMat t)
newtype ANuc2SubstMat t = ANuc2SubstMat { anuc2SubstMat :: Unboxed (Z:.Letter AA:.Letter DNA:.Letter DNA) DLO }
deriving (Generic,Eq,Read,Show)
instance Binary (ANuc2SubstMat t)
instance Serialize (ANuc2SubstMat t)
instance NFData (ANuc2SubstMat t)
newtype ANuc1SubstMat t = ANuc1SubstMat { anuc1SubstMat :: Unboxed (Z:.Letter AA:.Letter DNA) DLO }
deriving (Generic,Eq,Read,Show)
instance Binary (ANuc1SubstMat t)
instance Serialize (ANuc1SubstMat t)
instance NFData (ANuc1SubstMat t)
mkANuc3SubstMat :: AASubstMat t -> ANuc3SubstMat t
mkANuc3SubstMat (AASubstMat m) = ANuc3SubstMat $ fromAssocs (Z:. AA.Stop :. D.A:.D.A:.D.A) (Z:. AA.Z :. D.N:.D.N:.D.N) (DLO $ 999)
[ ( (Z:.a:.u:.v:.w) , maybe (DLO $ 999) (\b -> m!(Z:.a:.b)) $ M.lookup uvw dnaAAmap)
| a <- aaRange
, u <- [D.A .. D.N], v <- [D.A .. D.N], w <- [D.A .. D.N]
, let uvw = VU.fromList [u,v,w]
]
mkANuc2SubstMat :: (DLO -> DLO -> DLO) -> AASubstMat t -> ANuc2SubstMat t
mkANuc2SubstMat f (AASubstMat m) = ANuc2SubstMat $ fromAssocs (Z:. AA.Stop :. D.A:.D.A) (Z:. AA.Z :. D.N:.D.N) (DLO $ 999)
. M.assocs
. M.fromListWith f
$ [ ((Z:.a:.x:.y), maybe (DLO $ 999) (\k -> m!(Z:.a:.k)) $ M.lookup uvw dnaAAmap)
| a <- aaRange
, u <- [D.A .. D.N], v <- [D.A .. D.N], w <- [D.A .. D.N]
, (x,y) <- [ (u,v), (u,w), (v,w) ]
, let uvw = VU.fromList [u,v,w]
]
mkANuc1SubstMat :: (DLO -> DLO -> DLO) -> AASubstMat t -> ANuc1SubstMat t
mkANuc1SubstMat f (AASubstMat m) = ANuc1SubstMat $ fromAssocs (Z:. AA.Stop :. D.A) (Z:. AA.Z :. D.N) (DLO $ 999)
. M.assocs
. M.fromListWith f
$ [ ((Z:.a:.x), maybe (DLO $ 999) (\k -> m!(Z:.a:.k)) $ M.lookup uvw dnaAAmap)
| a <- aaRange
, u <- [D.A .. D.N], v <- [D.A .. D.N], w <- [D.A .. D.N]
, x <- [u,v,w]
, let uvw = VU.fromList [u,v,w]
]