module Biobase.Secondary.Convert where
import Biobase.Types.BioSequence
import Biobase.Primary.Letter
import Biobase.Primary.Nuc.RNA
import Biobase.Secondary.Basepair
import Biobase.Secondary.Vienna (ViennaPair(..))
import qualified Biobase.Secondary.Vienna as SV
import qualified Biobase.Secondary.Basepair as SB
class BasepairConvert a b where
basepairConvert :: a -> b
instance BasepairConvert (Letter RNA n,Letter RNA n) Basepair where
basepairConvert (l,r)
| l >= A && l <= U && r >= A && r <= U
= BP $ 4 * getLetter l + getLetter r
| otherwise = NoBP
{-# Inline basepairConvert #-}
instance BasepairConvert Basepair (Letter RNA n, Letter RNA n) where
basepairConvert k
| k == NoBP || k == NS = (N,N)
| otherwise = let (l,r) = getBP k `divMod` 4 in (Letter l, Letter r)
{-# Inline basepairConvert #-}
instance BasepairConvert (Letter RNA n, Letter RNA n) ViennaPair where
basepairConvert = \case
(C,G) -> SV.CG
(G,C) -> SV.GC
(G,U) -> SV.GU
(U,G) -> SV.UG
(A,U) -> SV.AU
(U,A) -> SV.UA
_ -> SV.NS
{-# Inline basepairConvert #-}
instance BasepairConvert ViennaPair (Letter RNA n, Letter RNA n) where
basepairConvert = \case
SV.CG -> (C,G)
SV.GC -> (G,C)
SV.GU -> (G,U)
SV.UG -> (U,G)
SV.AU -> (A,U)
SV.UA -> (U,A)
SV.NS -> (N,N)
{-# Inline basepairConvert #-}
instance BasepairConvert Basepair ViennaPair where
basepairConvert = \case
SB.AU -> SV.AU
SB.CG -> SV.CG
SB.GC -> SV.GC
SB.GU -> SV.GU
SB.UA -> SV.UA
SB.UG -> SV.UG
_ -> SV.NS
{-# Inline basepairConvert #-}
instance BasepairConvert ViennaPair Basepair where
basepairConvert = \case
SV.AU -> SB.AU
SV.CG -> SB.CG
SV.GC -> SB.GC
SV.GU -> SB.GU
SV.UA -> SB.UA
SV.UG -> SB.UG
_ -> SB.NS
{-# Inline basepairConvert #-}