Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. module DNAEncoder where
  2. import qualified Data.Algorithms.KMP as KMP
  3. import qualified Data.List as DL
  4. import qualified Control.Monad as CM
  5.  
  6. data PLoc a = PLoc {
  7. pattern :: [a],
  8. start :: Int
  9. } deriving (Show)
  10.  
  11. data Valuable a = Valuable {
  12. value :: Int,
  13. item :: PLoc a
  14. } deriving (Show)
  15.  
  16. findAll :: Eq a => [a] -> [a] -> [Int]
  17. findAll haystack needle =
  18. kmpTable `KMP.match` haystack
  19. where kmpTable = KMP.build needle
  20.  
  21. find :: Eq a => [a] -> [a] -> Int
  22. find haystack needle = head result
  23. where result = findAll haystack needle
  24.  
  25. pStop :: PLoc a -> Int
  26. pStop (PLoc p s) = s + length p
  27.  
  28. pStart :: PLoc a -> Int
  29. pStart (PLoc _ s) = s
  30.  
  31. vSorter :: Valuable a -> Valuable a -> Ordering
  32. vSorter (Valuable _ (PLoc _ s1)) (Valuable _ (PLoc _ s2)) = compare s1 s2
  33.  
  34. expand2nd :: (a, [b]) -> [(a, b)]
  35. expand2nd (_, []) = []
  36. expand2nd (a, b:bs) = (a, b) : expand2nd (a, bs)
  37.  
  38. unpack2 :: (a -> b -> c) -> (a, b) -> c
  39. unpack2 f (a, b) = f a b
  40.  
  41. getFirstSatisfy :: (a -> Bool) -> [a] -> Maybe a
  42. getFirstSatisfy _ [] = Nothing
  43. getFirstSatisfy f (x:xs)
  44. | f x = Just x
  45. | otherwise = getFirstSatisfy f xs
  46.  
  47. toDefault :: a -> Maybe a -> a
  48. toDefault defaultValue Nothing = defaultValue
  49. toDefault _ (Just x) = x
  50.  
  51. vIs0 :: Eq a => Valuable a -> Bool
  52. vIs0 (Valuable v _) = v == 0
  53.  
  54. svEnumerate :: Eq a => [Valuable a] -> (Maybe (Valuable a), Maybe (Valuable a))
  55. svEnumerate xs =
  56. let lefts = reverse $ takeWhile vIs0 xs
  57. rights = tail $ dropWhile vIs0 xs
  58. firstLeft = getFirstSatisfy vIs0 lefts
  59. firstRight = getFirstSatisfy vIs0 rights
  60. in (firstLeft, firstRight)
  61.  
  62. main :: IO()
  63. main = do
  64. let
  65. dna = "TCTAACTTGCATCAGATCTGCTCGATCAGATCAA"
  66. includes = ["CTGC"]
  67. excludes = ["TGCAT", "TCAG"]
  68. iLocs = findAll dna `map` includes
  69. eLocs = findAll dna `map` excludes
  70. piLocs = includes `zip` iLocs
  71. peLocs = excludes `zip` eLocs
  72. epiLocs = CM.join $ map expand2nd piLocs
  73. epeLocs = CM.join $ map expand2nd peLocs
  74. pepiLocs = unpack2 PLoc `map` epiLocs
  75. pepeLocs = unpack2 PLoc `map` epeLocs
  76. vpepiLocs = Valuable 1 `map` pepiLocs
  77. vpepeLocs = Valuable 0 `map` pepeLocs
  78. vpepLocs = vpepiLocs ++ vpepeLocs
  79. svpepLocs = DL.sortBy vSorter vpepLocs
  80. (mleft, mright) = svEnumerate svpepLocs
  81. lDefault = toDefault $ Valuable 0 $ PLoc "" (-1)
  82. rDefault = toDefault $ Valuable 0 $ PLoc "" (length dna)
  83. left = lDefault mleft
  84. right = rDefault mright
  85. ldrop = start (item left) + 1
  86. rtake = pStop (item right) - (ldrop + 1)
  87. finalResult = take rtake $ drop ldrop dna
  88. print finalResult
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement