Bioinformatics/Sequence mutation: Difference between revisions

Content added Content deleted
Line 946: Line 946:
i <- randomIndex (length xs)
i <- randomIndex (length xs)
case m of
case m of
Swap -> randomDNA >>= \d -> pure ((Swap, i), swapIndex i d xs)
Swap -> randomDNA >>= \d -> pure ((Swap, i), swapElement i d xs)
Insert -> randomDNA >>= \d -> pure ((Insert, i), insertIndex i d xs)
Insert -> randomDNA >>= \d -> pure ((Insert, i), insertElement i d xs)
Delete -> pure ((Delete, i), dropIndex i xs)
Delete -> pure ((Delete, i), dropElement i xs)
where
where
dropIndex i xs = take (pred i) xs <> drop i xs
dropElement i xs = take (pred i) xs <> drop i xs
insertIndex i e xs = take i xs <> [e] <> drop i xs
insertElement i e xs = take i xs <> [e] <> drop i xs
swapIndex i a xs = take (pred i) xs <> [a] <> drop i xs
swapElement i a xs = take (pred i) xs <> [a] <> drop i xs
randomIndex max = getStdRandom (randomR (1, max))
randomIndex max = getStdRandom (randomR (1, max))
randomDNA = head . randoms <$> newStdGen
randomDNA = head . randoms <$> newStdGen


mutations :: Int -> DNASequence -> IO DNASequence
mutate :: Int -> DNASequence -> IO DNASequence
mutations 0 s = pure s
mutate 0 s = pure s
mutations n s = do
mutate n s = do
(m, ms) <- mutateSequence s
(m, ms) <- mutateSequence s
uncurry (printf "%6s @ %d\n") m
uncurry (printf "%6s @ %d\n") m
mutations (pred n) ms
mutate (pred n) ms


main :: IO ()
main :: IO ()
main = do
main = do
dnaseq <- newSequence 200
ds <- newSequence 200
putStrLn "\nInitial Sequence:" >> showSequence dnaseq
putStrLn "\nInitial Sequence:" >> showSequence ds
putStrLn "\nBase Counts:" >> showBaseCounts dnaseq
putStrLn "\nBase Counts:" >> showBaseCounts ds
printf "Total: %d\n\n" $ length dnaseq
printf "Total: %d\n\n" $ length ds
ms <- mutations 10 dnaseq
ms <- mutate 10 ds
putStrLn "\nMutated Sequence:" >> showSequence ms
putStrLn "\nMutated Sequence:" >> showSequence ms
putStrLn "\nBase Counts:" >> showBaseCounts ms
putStrLn "\nBase Counts:" >> showBaseCounts ms