<commit_before>{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TemplateHaskell #-}
module Bio.Parse.Sequence.Phd where

import Bio.Core.Sequence
import Bio.Parse.Sequence.SequenceParser
import Control.Applicative
import Control.Lens
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.Monoid (mempty)
import Data.Typeable
import Text.Trifecta as T

data Nucleotide = Nucleotide {
    _nucleotide :: Char
  , _quality    :: Integer
  , _traceIdx   :: Integer
  } deriving (Eq, Ord, Show, Typeable)

makeLenses ''Nucleotide

data PhdSequence = PhdSequence {
    _identifier :: BL.ByteString
  , _sequence   :: [Nucleotide]
  } deriving (Eq, Ord, Show, Typeable)

makeLenses ''PhdSequence

instance BioSeq PhdSequence where
  seqid     = SeqLabel . _identifier
  seqheader = SeqLabel . _identifier
  seqdata   = SeqData . BL.pack . map _nucleotide . _sequence
  seqlength = fromIntegral . length . _sequence

-- | Parse a comment from a PHD file.
parseComment :: Parser BL.ByteString
parseComment = do
  _ <- string "BEGIN_COMMENT"
  BL.pack <$> manyTill anyChar (try (string "END_COMMENT"))

-- | Parse a nucleotide line from a PHD file.
parseNucleotide :: Parser Nucleotide
parseNucleotide = do
  nucleotide' <- oneOf "acgt"   -- TODO: Is uppercase valid too?
  spaces
  quality' <- integer
  spaces
  traceIdx' <- integer
  _ <- newline
  return $ Nucleotide nucleotide' quality' traceIdx'

-- | Parse a section of nucleotides from a PHD file.
parseDnaSection :: Parser [Nucleotide]
parseDnaSection = do
  _ <- string "BEGIN_DNA"
  _ <- newline
  nucleotides <- many parseNucleotide
  _ <- string "END_DNA"
  return nucleotides

-- | Parse a sequence from a PHD file.
parseSequence :: Parser PhdSequence
parseSequence = do
  _ <- string "BEGIN_SEQUENCE"
  identifier' <- BL.pack <$> manyTill anyChar (try newline)
  _ <- some newline
  sequence' <- parseDnaSection
  _ <- some newline
  _ <- string "END_SEQUENCE"
  _ <- some newline
  return $ PhdSequence identifier' sequence'

-- | Parses many sequences.
parseSequences :: Parser [PhdSequence]
parseSequences = manyTill parseSequence eof

-- | Parses sequences from a 'String'.
--<commit_msg>This is all sorts of broken.<commit_after>{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE TemplateHaskell #-}
module Bio.Parse.Sequence.Phd where

import Bio.Core.Sequence
import Bio.Parse.Sequence.SequenceParser
import Control.Applicative
import Control.Lens
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy.Char8 as BL
import Data.Monoid (mempty)
import Data.Typeable
import Text.Trifecta as T

data Nucleotide = Nucleotide {
    _nucleotide :: Char
  , _quality    :: Integer
  , _traceIdx   :: Integer
  } deriving (Eq, Ord, Show, Typeable)

makeLenses ''Nucleotide

data PhdSequence = PhdSequence {
    _identifier :: BL.ByteString
  , _sequence   :: [Nucleotide]
  } deriving (Eq, Ord, Show, Typeable)

makeLenses ''PhdSequence

instance BioSeq PhdSequence where
  seqid     = SeqLabel . _identifier
  seqheader = SeqLabel . _identifier
  seqdata   = SeqData . BL.pack . map _nucleotide . _sequence
  seqlength = fromIntegral . length . _sequence

-- | Parse a comment from a PHD file.
parseComment :: Parser BL.ByteString
parseComment = do
  _ <- string "BEGIN_COMMENT"
  BL.pack <$> manyTill anyChar (try (string "END_COMMENT"))

-- | Parse a nucleotide line from a PHD file.
parseNucleotide :: Parser Nucleotide
parseNucleotide = do
  nucleotide' <- letter
  spaces
  quality' <- integer
  spaces
  traceIdx' <- integer
  _ <- some newline
  return $ Nucleotide nucleotide' quality' traceIdx'

-- | Parse a section of nucleotides from a PHD file.
parseDnaSection :: Parser [Nucleotide]
parseDnaSection = do
  _ <- string "BEGIN_DNA"
  _ <- some newline
  nucleotides <- many parseNucleotide
  _ <- string "END_DNA"
  _ <- many newline
  return nucleotides

-- | Parse a sequence from a PHD file.
parseSequence :: Parser PhdSequence
parseSequence = do
  _ <- string "BEGIN_SEQUENCE "
  identifier' <- BL.pack <$> manyTill anyChar (try newline)
  _ <- some newline
  sequence' <- parseDnaSection
  _ <- string "END_SEQUENCE"
  _ <- many newline
  return $ PhdSequence identifier' sequence'

-- | Parses many sequences.
parseSequences :: Parser [PhdSequence]
parseSequences = manyTill parseSequence eof

-- | Parses sequences from a 'String'.
--
{'fraction_non_alphanumeric': 0.09327251995438997, 'fraction_numerical': 0.0009122006841505131, 'mean_word_length': 3.762214983713355, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2336388', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1366, 'n_words': 423}