Skip to content

Instantly share code, notes, and snippets.

@amonshiz
Created August 16, 2015 21:34
Show Gist options
  • Save amonshiz/9f309c93eb1b02e24f00 to your computer and use it in GitHub Desktop.
Save amonshiz/9f309c93eb1b02e24f00 to your computer and use it in GitHub Desktop.
Project Rosalind - Locating RestrictionSites
module Bioinformatics.Utilities (
getLines,
dnaToRNA,
reverseComplement
) where
import qualified Bioinformatics.DNANucleotide as D
import qualified Bioinformatics.RNANucleotide as R
getLines :: String -> IO String
getLines contents = do
line <- getLine
if null line
then return contents
else getLines $ contents ++ line ++ "\n"
dnaToRNA :: D.DNANucleotide -> R.RNANucleotide
dnaToRNA d
| d == D.A = R.A
| d == D.C = R.C
| d == D.G = R.G
| otherwise = R.U
reverseComplement :: [D.DNANucleotide] -> [D.DNANucleotide]
reverseComplement = map (D.nucleotideComplement) . reverse
import Bioinformatics.DNANucleotide (DNANucleotide (..))
import Bioinformatics.FASTA (FASTAFormatLine (..),
stringToFASTAFormat)
import Bioinformatics.Utilities (getLines, reverseComplement)
import Data.List (inits, tails)
data PotentialLocation a = PotentialLocation {
getIndex::Int,
getPotentials::[[a]]
} deriving (Show)
boundingFilter :: [[a]] -> [[a]]
boundingFilter =
filter (\as ->
length as >= 4
&& length as <= 12)
rawPotentialSites :: [a] -> [[[a]]]
rawPotentialSites = map inits . tails
rawPotentialLocations :: [[[a]]] -> [PotentialLocation a]
rawPotentialLocations =
zipWith PotentialLocation [1..]
removeTooLongShortEmpty :: [PotentialLocation a] -> [PotentialLocation a]
removeTooLongShortEmpty =
map (\pl -> PotentialLocation (getIndex pl) (boundingFilter $ getPotentials pl))
-- Take a dna string and tails it, then reduce to only lengths of 4-12 elements
buildPotentialSites :: [a] -> [PotentialLocation a]
buildPotentialSites =
filter (not . null . getPotentials) . removeTooLongShortEmpty . rawPotentialLocations . rawPotentialSites
checkIndividualPotential :: [DNANucleotide] -> Bool
checkIndividualPotential as = as == reverseComplement as
filterPotentialLocationSites :: PotentialLocation DNANucleotide -> PotentialLocation DNANucleotide
filterPotentialLocationSites pl =
PotentialLocation (getIndex pl) (filter checkIndividualPotential $ getPotentials pl)
filterPotentialLocations :: [PotentialLocation DNANucleotide] -> [PotentialLocation DNANucleotide]
filterPotentialLocations =
filter (not . null . getPotentials) . map filterPotentialLocationSites
buildPotentialLocationResult :: PotentialLocation a -> [String]
buildPotentialLocationResult pl =
let lengths = map length $ getPotentials pl
in map (\l -> unwords [show $ getIndex pl, show l]) lengths
buildResults :: [PotentialLocation a] -> String
buildResults =
unlines . concatMap buildPotentialLocationResult
main :: IO()
main = do
dnaString <- getLines ""
let forwardString = content $ stringToFASTAFormat dnaString
potentialSites = buildPotentialSites forwardString
putStr . buildResults $ filterPotentialLocations potentialSites
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment