Skip to content

Instantly share code, notes, and snippets.

@amonshiz
Created February 10, 2015 02:09
Show Gist options
  • Save amonshiz/11a34f1d5d4b51bf8b9a to your computer and use it in GitHub Desktop.
Save amonshiz/11a34f1d5d4b51bf8b9a to your computer and use it in GitHub Desktop.
Finding a Motif in DNA
module Bioinformatics.DNANucleotide (
DNANucleotide (..),
charToDNANucleotide,
nucleotideComplement,
calculateGCContent,
findMotif
) where
import Data.Char
import Data.List
data DNANucleotide = A | C | G | T deriving(Show, Eq, Ord, Read)
charToDNANucleotide :: Char -> DNANucleotide
charToDNANucleotide c = read [toUpper c] :: DNANucleotide
nucleotideComplement :: DNANucleotide -> DNANucleotide
nucleotideComplement A = T
nucleotideComplement T = A
nucleotideComplement C = G
nucleotideComplement G = C
calculateGCContent :: [DNANucleotide] -> Double
calculateGCContent ns =
let gcs = filter (\n -> n == G || n == C) ns
in (fromIntegral $ length gcs) / (fromIntegral $ length ns)
findMotif :: [DNANucleotide] -> [DNANucleotide] -> [Int]
findMotif t = map (+1) . findIndices (isPrefixOf t) . tails
import qualified Bioinformatics.DNANucleotide as D
main = do
theDNAString <- getLine
theMotif <- getLine
putStrLn $ unwords . map show $ D.findMotif (map D.charToDNANucleotide theMotif) (map D.charToDNANucleotide theDNAString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment