Jump to content

Bioinformatics/Sequence mutation: Difference between revisions

m
(added Haskell)
Line 902:
</pre>
=={{header|Haskell}}==
<lang haskell>import DataControl.List Monad (group, sort(>=>))
import Data.List (group, sort)
import Data.List.Split (chunksOf)
import System.Random (Random, randomR, random, newStdGen, randoms, getStdRandom)
Line 930 ⟶ 931:
toChar = head . show
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 = zip [50,100..] . chunksOf 50
 
baseCounts :: DNASequence -> [(DNABase, Int)]
Line 950 ⟶ 942:
 
mutateSequence :: DNASequence -> IO ((Mutation, Int), DNASequence)
mutateSequence xsds = randomMutationmutate ds >>=<< (`mutate` xs)randomMutation
where
 
randomMutation = head . randoms <$> newStdGen
mutate :: Mutation -> DNASequence -> IO ((Mutation, Int), DNASequence)
mutate m mutate xs m = do
i <- randomIndex (length xs)
case m of
Swap -> randomDNA >>= \d -> pure ((Swap, i), swapIndex i d xs)
Insert -> randomDNA >>= \d -> pure ((Insert, i), insertIndex i d 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
 
randomMutation :: IO Mutation
randomMutation = head . randoms <$> newStdGen
 
mutations :: Int -> DNASequence -> IO DNASequence
Line 1,028 ⟶ 1,017:
Total: 201
</pre>
 
=={{header|J}}==
<lang J>ACGT=: 'ACGT'
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.