{-# LANGUAGE DeriveDataTypeable          #-}
{-# LANGUAGE DeriveGeneric               #-}
{-# LANGUAGE FlexibleContexts            #-}
{-# LANGUAGE FlexibleInstances           #-}
{-# LANGUAGE MultiParamTypeClasses       #-}
{-# LANGUAGE OverloadedLists             #-}
{-# LANGUAGE OverloadedStrings           #-}
{-# LANGUAGE MultiWayIf                  #-}
{-# LANGUAGE PackageImports              #-}
{-# LANGUAGE RecordWildCards             #-}
{-# LANGUAGE ScopedTypeVariables         #-}
{-# LANGUAGE TemplateHaskell             #-}
{-# LANGUAGE TypeFamilies                #-}
{-# LANGUAGE QuasiQuotes                 #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing #-}

-- |
-- Module      :  Data.SAM.Version1_6.Read.Parser.Header.RG.Base
-- Copyright   :  (c) Matthew Mosior 2023
-- License     :  BSD-style
-- Maintainer  :  mattm.github@gmail.com
-- Portability :  portable
--
-- = WARNING
--
-- This module is considered __internal__.
--
-- The Package Versioning Policy __does not apply__.
--
-- The contents of this module may change __in any way whatsoever__
-- and __without any warning__ between minor versions of this package.
--
-- Authors importing this library are expected to track development
-- closely.
--
-- All credit goes to the author(s)/maintainer(s) of the
-- [containers](https://hackage.haskell.org/package/containers) library
-- for the above warning text.
--
-- = Description
--
-- This library enables the decoding/encoding of SAM, BAM and CRAM file formats.

module Data.SAM.Version1_6.Read.Parser.Header.RG.Base ( -- * SAM_V1_6 parser - header section (Read group)
                                                        parse_SAM_V1_6_Read_Group
                                                      ) where

import Data.SAM.Version1_6.Header
import Data.SAM.Version1_6.Read.Error
import Data.SAM.Version1_6.Read.Parser.Header.RG.ID
import Data.SAM.Version1_6.Read.Parser.Header.RG.BC
import Data.SAM.Version1_6.Read.Parser.Header.RG.CN
import Data.SAM.Version1_6.Read.Parser.Header.RG.DS
import Data.SAM.Version1_6.Read.Parser.Header.RG.DT
import Data.SAM.Version1_6.Read.Parser.Header.RG.FO
import Data.SAM.Version1_6.Read.Parser.Header.RG.KS
import Data.SAM.Version1_6.Read.Parser.Header.RG.LB
import Data.SAM.Version1_6.Read.Parser.Header.RG.PG
import Data.SAM.Version1_6.Read.Parser.Header.RG.PI
import Data.SAM.Version1_6.Read.Parser.Header.RG.PL
import Data.SAM.Version1_6.Read.Parser.Header.RG.PM
import Data.SAM.Version1_6.Read.Parser.Header.RG.PU
import Data.SAM.Version1_6.Read.Parser.Header.RG.SM

import Data.Attoparsec.ByteString.Lazy   as DABL
import Text.Regex.PCRE.Heavy

-- | Make a parser optional, return Nothing if there is no match.
maybeOption :: Parser a
            -> Parser (Maybe a)
maybeOption :: forall a. Parser a -> Parser (Maybe a)
maybeOption Parser a
p = Maybe a
-> Parser ByteString (Maybe a) -> Parser ByteString (Maybe a)
forall (f :: * -> *) a. Alternative f => a -> f a -> f a
option Maybe a
forall a. Maybe a
Nothing (a -> Maybe a
forall a. a -> Maybe a
Just (a -> Maybe a) -> Parser a -> Parser ByteString (Maybe a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Parser a
p)

-- | @"SAM_V1_6_Read_Group"@ parser.
-- Defines a parser for @RG tag section of the SAM v1.6 file format.
-- See the [SAM v1.6](http://samtools.github.io/hts-specs/SAMv1.pdf) specification documentation.
parse_SAM_V1_6_Read_Group :: Parser SAM_V1_6_Read_Group
parse_SAM_V1_6_Read_Group :: Parser SAM_V1_6_Read_Group
parse_SAM_V1_6_Read_Group = do
  ByteString
_         <- do ByteString
rgheaderp <- (Word8 -> Bool) -> Parser ByteString ByteString
DABL.takeTill (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
09)
                  -- Parse @RG tag of the header section.
                  case (ByteString
rgheaderp ByteString -> Regex -> Bool
forall a. ConvertibleStrings a ByteString => a -> Regex -> Bool
=~ [re|[@][R][G]|]) of
                    Bool
False -> String -> Parser ByteString ByteString
forall a. String -> Parser ByteString a
forall (m :: * -> *) a. MonadFail m => String -> m a
fail (String -> Parser ByteString ByteString)
-> String -> Parser ByteString ByteString
forall a b. (a -> b) -> a -> b
$ SAM_V1_6_Error -> String
forall a. Show a => a -> String
show SAM_V1_6_Error
SAM_V1_6_Error_Read_Group_Tag_Incorrect_Format
                    Bool
True  -> -- @RG tag is in the accepted format.
                             ByteString -> Parser ByteString ByteString
forall a. a -> Parser ByteString a
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
rgheaderp
  Word8
_         <- Word8 -> Parser Word8
word8 Word8
09
  -- This parser assumes that the ID tag always appears first, followed by
  -- the BC, CN, DS, DT, FO, KS, LB, PG, PI, PL,
  -- PM, PU and SM tags if they exist, in that order.
  SAM_V1_6_Read_Group_Identifier
id <- Parser SAM_V1_6_Read_Group_Identifier
parse_SAM_V1_6_SAM_V1_6_Read_Group_ID
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Barcode_Sequence
bc <- Parser SAM_V1_6_Read_Group_Barcode_Sequence
-> Parser (Maybe SAM_V1_6_Read_Group_Barcode_Sequence)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Barcode_Sequence
parse_SAM_V1_6_SAM_V1_6_Read_Group_BC
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Sequencing_Center
cn <- Parser SAM_V1_6_Read_Group_Sequencing_Center
-> Parser (Maybe SAM_V1_6_Read_Group_Sequencing_Center)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Sequencing_Center
parse_SAM_V1_6_SAM_V1_6_Read_Group_CN
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Description
ds <- Parser SAM_V1_6_Read_Group_Description
-> Parser (Maybe SAM_V1_6_Read_Group_Description)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Description
parse_SAM_V1_6_SAM_V1_6_Read_Group_DS
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Run_Date
dt <- Parser SAM_V1_6_Read_Group_Run_Date
-> Parser (Maybe SAM_V1_6_Read_Group_Run_Date)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Run_Date
parse_SAM_V1_6_SAM_V1_6_Read_Group_DT
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Flow_Order
fo <- Parser SAM_V1_6_Read_Group_Flow_Order
-> Parser (Maybe SAM_V1_6_Read_Group_Flow_Order)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Flow_Order
parse_SAM_V1_6_SAM_V1_6_Read_Group_FO
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Key_Sequence
ks <- Parser SAM_V1_6_Read_Group_Key_Sequence
-> Parser (Maybe SAM_V1_6_Read_Group_Key_Sequence)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Key_Sequence
parse_SAM_V1_6_SAM_V1_6_Read_Group_KS
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Library
lb <- Parser SAM_V1_6_Read_Group_Library
-> Parser (Maybe SAM_V1_6_Read_Group_Library)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Library
parse_SAM_V1_6_SAM_V1_6_Read_Group_LB
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Programs
pg <- Parser SAM_V1_6_Read_Group_Programs
-> Parser (Maybe SAM_V1_6_Read_Group_Programs)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Programs
parse_SAM_V1_6_SAM_V1_6_Read_Group_PG
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Predicted_Median_Insert_Size
pi <- Parser SAM_V1_6_Read_Group_Predicted_Median_Insert_Size
-> Parser (Maybe SAM_V1_6_Read_Group_Predicted_Median_Insert_Size)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Predicted_Median_Insert_Size
parse_SAM_V1_6_SAM_V1_6_Read_Group_PI
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Platform
pl <- Parser SAM_V1_6_Read_Group_Platform
-> Parser (Maybe SAM_V1_6_Read_Group_Platform)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Platform
parse_SAM_V1_6_SAM_V1_6_Read_Group_PL
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Platform_Model
pm <- Parser SAM_V1_6_Read_Group_Platform_Model
-> Parser (Maybe SAM_V1_6_Read_Group_Platform_Model)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Platform_Model
parse_SAM_V1_6_SAM_V1_6_Read_Group_PM
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Platform_Unit
pu <- Parser SAM_V1_6_Read_Group_Platform_Unit
-> Parser (Maybe SAM_V1_6_Read_Group_Platform_Unit)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Platform_Unit
parse_SAM_V1_6_SAM_V1_6_Read_Group_PU
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Read_Group_Sample
sm <- Parser SAM_V1_6_Read_Group_Sample
-> Parser (Maybe SAM_V1_6_Read_Group_Sample)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Read_Group_Sample
parse_SAM_V1_6_SAM_V1_6_Read_Group_SM
  SAM_V1_6_Read_Group -> Parser SAM_V1_6_Read_Group
forall a. a -> Parser ByteString a
forall (m :: * -> *) a. Monad m => a -> m a
return SAM_V1_6_Read_Group { sam_v1_6_read_group_identifer :: SAM_V1_6_Read_Group_Identifier
sam_v1_6_read_group_identifer                    = SAM_V1_6_Read_Group_Identifier
id
                             , sam_v1_6_read_group_barcode_sequence :: Maybe SAM_V1_6_Read_Group_Barcode_Sequence
sam_v1_6_read_group_barcode_sequence             = Maybe SAM_V1_6_Read_Group_Barcode_Sequence
bc
                             , sam_v1_6_read_group_sequencing_center :: Maybe SAM_V1_6_Read_Group_Sequencing_Center
sam_v1_6_read_group_sequencing_center            = Maybe SAM_V1_6_Read_Group_Sequencing_Center
cn
                             , sam_v1_6_read_group_description :: Maybe SAM_V1_6_Read_Group_Description
sam_v1_6_read_group_description                  = Maybe SAM_V1_6_Read_Group_Description
ds
                             , sam_v1_6_read_group_run_date :: Maybe SAM_V1_6_Read_Group_Run_Date
sam_v1_6_read_group_run_date                     = Maybe SAM_V1_6_Read_Group_Run_Date
dt
                             , sam_v1_6_read_group_flow_order :: Maybe SAM_V1_6_Read_Group_Flow_Order
sam_v1_6_read_group_flow_order                   = Maybe SAM_V1_6_Read_Group_Flow_Order
fo
                             , sam_v1_6_read_group_key_sequence :: Maybe SAM_V1_6_Read_Group_Key_Sequence
sam_v1_6_read_group_key_sequence                 = Maybe SAM_V1_6_Read_Group_Key_Sequence
ks
                             , sam_v1_6_read_group_library :: Maybe SAM_V1_6_Read_Group_Library
sam_v1_6_read_group_library                      = Maybe SAM_V1_6_Read_Group_Library
lb
                             , sam_v1_6_read_group_programs :: Maybe SAM_V1_6_Read_Group_Programs
sam_v1_6_read_group_programs                     = Maybe SAM_V1_6_Read_Group_Programs
pg
                             , sam_v1_6_read_group_predicted_median_insert_size :: Maybe SAM_V1_6_Read_Group_Predicted_Median_Insert_Size
sam_v1_6_read_group_predicted_median_insert_size = Maybe SAM_V1_6_Read_Group_Predicted_Median_Insert_Size
pi
                             , sam_v1_6_read_group_platform :: Maybe SAM_V1_6_Read_Group_Platform
sam_v1_6_read_group_platform                     = Maybe SAM_V1_6_Read_Group_Platform
pl
                             , sam_v1_6_read_group_platform_model :: Maybe SAM_V1_6_Read_Group_Platform_Model
sam_v1_6_read_group_platform_model               = Maybe SAM_V1_6_Read_Group_Platform_Model
pm
                             , sam_v1_6_read_group_platform_unit :: Maybe SAM_V1_6_Read_Group_Platform_Unit
sam_v1_6_read_group_platform_unit                = Maybe SAM_V1_6_Read_Group_Platform_Unit
pu
                             , sam_v1_6_read_group_sample :: Maybe SAM_V1_6_Read_Group_Sample
sam_v1_6_read_group_sample                       = Maybe SAM_V1_6_Read_Group_Sample
sm
                             }