Skip to content

Instantly share code, notes, and snippets.

@amonshiz
Created June 2, 2015 04:28
Show Gist options
  • Save amonshiz/a0cb14ab61ac1b9880c0 to your computer and use it in GitHub Desktop.
Save amonshiz/a0cb14ab61ac1b9880c0 to your computer and use it in GitHub Desktop.
import qualified Bioinformatics.DNANucleotide as D
import qualified Bioinformatics.FASTA as F
import qualified Data.List as L
getLines contents = do
line <- getLine
if null line
then return contents
else getLines $ contents ++ line ++ "\n"
convertToProfile :: [[D.DNANucleotide]] -> String
convertToProfile dnaStrings =
unlines $ map buildString [D.A, D.C, D.G, D.T]
where columns = L.transpose dnaStrings
lengths nucleotide = map (length . filter (== nucleotide))
buildString n = show n ++ ": " ++ (unwords . map show $ lengths n columns)
determineConcensus :: [[D.DNANucleotide]] -> String
determineConcensus =
concat . map show . mostCommon . L.transpose
where mostCommon = map (head . L.maximumBy (\a1 a2 -> length a1 `compare` length a2) . L.group . L.sort)
main = do
lines <- getLines ""
let fastas = F.buildFASTALines lines
let dnaStrings = tail $ map F.content fastas
putStrLn $ determineConcensus dnaStrings
putStrLn $ convertToProfile dnaStrings
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment