Bioinformatics/Sequence mutation: Difference between revisions

Content added Content deleted
(added Haskell)
Line 902: Line 902:
</pre>
</pre>
=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (group, sort)
<lang haskell>import Control.Monad ((>=>))
import Data.List (group, sort)
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)
import System.Random (Random, randomR, random, newStdGen, randoms, getStdRandom)
import System.Random (Random, randomR, random, newStdGen, randoms, getStdRandom)
Line 930: Line 931:
toChar = head . show
toChar = head . show
fromChar c = read [c]
fromChar c = read [c]

dropIndex :: Int -> [a] -> [a]
dropIndex i xs = take (pred i) xs <> drop i xs

insertIndex :: Int -> a -> [a] -> [a]
insertIndex i e xs = take i xs <> [e] <> drop i xs

swapIndex :: Int -> a -> [a] -> [a]
swapIndex i a xs = take (pred i) xs <> [a] <> drop i xs


chunkedDNASequence :: DNASequence -> [(Int, [DNABase])]
chunkedDNASequence :: DNASequence -> [(Int, [DNABase])]
chunkedDNASequence = zip [50,100..] . chunksOf 50
chunkedDNASequence = zip [50,100..] . chunksOf 50


baseCounts :: DNASequence -> [(DNABase, Int)]
baseCounts :: DNASequence -> [(DNABase, Int)]
Line 950: Line 942:


mutateSequence :: DNASequence -> IO ((Mutation, Int), DNASequence)
mutateSequence :: DNASequence -> IO ((Mutation, Int), DNASequence)
mutateSequence xs = randomMutation >>= (`mutate` xs)
mutateSequence ds = mutate ds =<< randomMutation
where

randomMutation = head . randoms <$> newStdGen
mutate :: Mutation -> DNASequence -> IO ((Mutation, Int), DNASequence)
mutate m xs = do
mutate xs m = do
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), swapIndex i d xs)
Insert -> randomDNA >>= \d -> pure ((Insert, i), insertIndex i d xs)
Insert -> randomDNA >>= \d -> pure ((Insert, i), insertIndex i d xs)
Delete -> pure ((Delete, i), dropIndex i xs)
Delete -> pure ((Delete, i), dropIndex i xs)
where

dropIndex i xs = take (pred i) xs <> drop i xs
randomIndex :: Int -> IO Int
insertIndex i e xs = take i xs <> [e] <> drop i xs
randomIndex max = getStdRandom (randomR (1, max))
swapIndex i a xs = take (pred i) xs <> [a] <> drop i xs

randomIndex max = getStdRandom (randomR (1, max))
randomDNA :: IO DNABase
randomDNA = head . randoms <$> newStdGen
randomDNA = head . randoms <$> newStdGen

randomMutation :: IO Mutation
randomMutation = head . randoms <$> newStdGen


mutations :: Int -> DNASequence -> IO DNASequence
mutations :: Int -> DNASequence -> IO DNASequence
Line 1,028: Line 1,017:
Total: 201
Total: 201
</pre>
</pre>

=={{header|J}}==
=={{header|J}}==
<lang J>ACGT=: 'ACGT'
<lang J>ACGT=: 'ACGT'