module Data.Fasta.Text.Parse ( parsecFasta
, parsecCLIPFasta
, attoFasta
, attoCLIPFasta
, pipesFasta
, pipesCLIPFasta
, removeNs
, removeN
, removeCLIPNs ) where
import Data.Char
import Text.Parsec
import Text.Parsec.Text
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import qualified Control.Applicative as CA
import Control.Monad (void)
import qualified Data.Attoparsec.Text as A
import Pipes
import qualified Pipes.Prelude as P
import qualified Pipes.Text as PT
import qualified Pipes.Group as PG
import qualified Pipes.Attoparsec as PA
import Control.Lens (view)
import qualified Control.Foldl as FL
import Data.Fasta.Text.Types
eol :: Parsec T.Text u String
eol = choice . map (try . string) $ ["\n\r", "\r\n", "\n", "\r"]
eoe :: Parsec T.Text u ()
eoe = lookAhead (void $ char '>') <|> eof
fasta :: Parsec T.Text u FastaSequence
fasta = do
spaces
char '>'
header <- manyTill (satisfy (/= '>')) eol
fseq <- manyTill anyChar eoe
return (FastaSequence { fastaHeader = T.pack header
, fastaSeq = T.pack . removeWhitespace $ fseq } )
where
removeWhitespace = filter (`notElem` ("\n\r " :: String))
fastaFile :: Parsec T.Text u [FastaSequence]
fastaFile = do
spaces
many fasta
fastaCLIP :: Parsec T.Text u (FastaSequence, [FastaSequence])
fastaCLIP = do
spaces
char '>'
germline <- fasta
clones <- many $ try fasta
return (germline, clones)
fastaCLIPFile :: Parsec T.Text u [(FastaSequence, [FastaSequence])]
fastaCLIPFile = do
spaces
many fastaCLIP
parsecFasta :: T.Text -> [FastaSequence]
parsecFasta = eToV . parse fastaFile "error"
where
eToV (Right x) = x
eToV (Left x) = error ("Unable to parse fasta file\n" ++ show x)
parsecCLIPFasta :: T.Text -> CloneMap
parsecCLIPFasta = Map.fromList
. map (\(!x, (!y, !z)) -> ((x, y), z))
. zip [0..]
. eToV
. parse fastaCLIPFile "error"
where
eToV (Right x) = x
eToV (Left x) = error ("Unable to parse fasta file\n" ++ show x)
anyButSpace :: A.Parser Char
anyButSpace = do
A.skipSpace
x <- A.anyChar
A.skipSpace
return x
fasta' :: A.Parser FastaSequence
fasta' = do
header <- A.takeWhile (not . A.isEndOfLine)
A.endOfLine
fseq <- A.manyTill anyButSpace (void (A.char '>') CA.<|> A.endOfInput)
return FastaSequence { fastaHeader = header
, fastaSeq = T.pack fseq }
fastaFile' :: A.Parser [FastaSequence]
fastaFile' = do
A.skipSpace
A.char '>'
A.many' fasta'
fastaCLIP' :: A.Parser FastaSequence
fastaCLIP' = do
header <- A.takeWhile (not . A.isEndOfLine)
A.endOfLine
fseq <- A.manyTill anyButSpace (void (A.char '>') CA.<|> A.endOfInput)
return FastaSequence { fastaHeader = header
, fastaSeq = T.pack fseq }
clone' :: A.Parser (Germline, [FastaSequence])
clone' = do
A.skipSpace
germline <- fastaCLIP'
fseqs <- A.manyTill fasta' (void (A.char '>') CA.<|> A.endOfInput)
return (germline, fseqs)
fastaCLIPFile' :: A.Parser [(Germline, [FastaSequence])]
fastaCLIPFile' = do
A.skipSpace
A.string ">>"
A.many' clone'
attoFasta :: T.Text -> [FastaSequence]
attoFasta = eToV . A.parseOnly fastaFile'
where
eToV (Right x) = x
eToV (Left x) = error ("Unable to parse fasta file\n" ++ show x)
attoCLIPFasta :: T.Text -> [(Germline, [FastaSequence])]
attoCLIPFasta = eToV . A.parseOnly fastaCLIPFile'
where
eToV (Right x) = x
eToV (Left x) = error ("Unable to parse fasta file\n" ++ show x)
pipesFasta :: (MonadIO m) => Producer T.Text m () -> Producer FastaSequence m ()
pipesFasta p = FL.purely PG.folds FL.mconcat ( view (PT.splits '>')
. PT.drop (1 :: Int)
$ p )
>-> P.map toFasta
where
toFasta x = FastaSequence { fastaHeader = head . T.lines $ x
, fastaSeq = T.concat . tail . T.lines $ x }
pipesCLIPFasta :: (MonadIO m)
=> Producer T.Text m ()
-> Producer (Germline, [FastaSequence]) m (Either (PA.ParsingError, Producer T.Text m ()) ())
pipesCLIPFasta = PA.parsed clone' . PT.drop 2 . (>-> PT.stripStart)
removeNs :: [FastaSequence] -> [FastaSequence]
removeNs = map (\x -> x { fastaSeq = noN . fastaSeq $ x })
where
noN = T.map (\y -> if y /= 'N' && y /= 'n' then y else '-')
removeN :: FastaSequence -> FastaSequence
removeN x = x { fastaSeq = noN . fastaSeq $ x }
where
noN = T.map (\y -> if y /= 'N' && y /= 'n' then y else '-')
removeCLIPNs :: CloneMap -> CloneMap
removeCLIPNs = Map.fromList . map remove . Map.toList
where
remove ((!x, !y), !z) = ((x, newSeq y), map newSeq z)
newSeq !x = x { fastaSeq = noN . fastaSeq $ x }
noN = T.map (\y -> if y /= 'N' && y /= 'n' then y else '-')