{-# 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           #-}

-- |
-- Module      :  Data.SAM.Version1_6.Read.Parser.Header.SQ.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.SQ.Base ( -- * SAM_V1_6 parser - header section (Reference sequence dictionary)
                                                        parse_SAM_V1_6_Reference_Sequence_Dictionary
                                                      ) where

import Data.SAM.Version1_6.Header
import Data.SAM.Version1_6.Read.Error
import Data.SAM.Version1_6.Read.Parser.Header.SQ.SN
import Data.SAM.Version1_6.Read.Parser.Header.SQ.LN
import Data.SAM.Version1_6.Read.Parser.Header.SQ.AH
import Data.SAM.Version1_6.Read.Parser.Header.SQ.AN
import Data.SAM.Version1_6.Read.Parser.Header.SQ.AS
import Data.SAM.Version1_6.Read.Parser.Header.SQ.DS
import Data.SAM.Version1_6.Read.Parser.Header.SQ.M5
import Data.SAM.Version1_6.Read.Parser.Header.SQ.SP
import Data.SAM.Version1_6.Read.Parser.Header.SQ.TP
import Data.SAM.Version1_6.Read.Parser.Header.SQ.UR

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_Reference_Sequence_Dictionary"@ parser.
-- Defines a parser for @SQ 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_Reference_Sequence_Dictionary :: Parser SAM_V1_6_Reference_Sequence_Dictionary
parse_SAM_V1_6_Reference_Sequence_Dictionary :: Parser SAM_V1_6_Reference_Sequence_Dictionary
parse_SAM_V1_6_Reference_Sequence_Dictionary = do
  ByteString
_         <- do ByteString
sqheaderp <- (Word8 -> Bool) -> Parser ByteString ByteString
DABL.takeTill (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
09)
                  -- Parse @SQ tag of the header section.
                  case (ByteString
sqheaderp ByteString -> Regex -> Bool
forall a. ConvertibleStrings a ByteString => a -> Regex -> Bool
=~ [re|[@][S][Q]|]) 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_File_Level_Metadata_Tag_Incorrect_Format
                    Bool
True  -> -- @SQ 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
sqheaderp
  Word8
_         <- Word8 -> Parser Word8
word8 Word8
09
  -- This parser assumes that the SN tag always appears first, followed by
  -- the LN tag, followed by the AH, AN, AS, DS, M5,
  -- SP, TP and UR tags if they exist, in that order.
  SAM_V1_6_Reference_Sequence_Dictionary_Reference_Sequence_Name
sn <- Parser
  SAM_V1_6_Reference_Sequence_Dictionary_Reference_Sequence_Name
parse_SAM_V1_6_SAM_V1_6_Reference_Sequence_Dictionary_SN
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  SAM_V1_6_Reference_Sequence_Dictionary_Reference_Sequence_Length
ln <- Parser
  SAM_V1_6_Reference_Sequence_Dictionary_Reference_Sequence_Length
parse_SAM_V1_6_SAM_V1_6_Reference_Sequence_Dictionary_LN
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Reference_Sequence_Dictionary_Alternative_Locus
ah <- Parser SAM_V1_6_Reference_Sequence_Dictionary_Alternative_Locus
-> Parser
     (Maybe SAM_V1_6_Reference_Sequence_Dictionary_Alternative_Locus)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Reference_Sequence_Dictionary_Alternative_Locus
parse_SAM_V1_6_SAM_V1_6_Reference_Sequence_Dictionary_AH
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe
  SAM_V1_6_Reference_Sequence_Dictionary_Alternative_Reference_Sequence_Names
an <- Parser
  SAM_V1_6_Reference_Sequence_Dictionary_Alternative_Reference_Sequence_Names
-> Parser
     (Maybe
        SAM_V1_6_Reference_Sequence_Dictionary_Alternative_Reference_Sequence_Names)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser
  SAM_V1_6_Reference_Sequence_Dictionary_Alternative_Reference_Sequence_Names
parse_SAM_V1_6_SAM_V1_6_Reference_Sequence_Dictionary_AN
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe
  SAM_V1_6_Reference_Sequence_Dictionary_Genome_Assembly_Identifier
as <- Parser
  SAM_V1_6_Reference_Sequence_Dictionary_Genome_Assembly_Identifier
-> Parser
     (Maybe
        SAM_V1_6_Reference_Sequence_Dictionary_Genome_Assembly_Identifier)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser
  SAM_V1_6_Reference_Sequence_Dictionary_Genome_Assembly_Identifier
parse_SAM_V1_6_SAM_V1_6_Reference_Sequence_Dictionary_AS
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Reference_Sequence_Dictionary_Description
ds <- Parser SAM_V1_6_Reference_Sequence_Dictionary_Description
-> Parser
     (Maybe SAM_V1_6_Reference_Sequence_Dictionary_Description)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Reference_Sequence_Dictionary_Description
parse_SAM_V1_6_SAM_V1_6_Reference_Sequence_Dictionary_DS
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Reference_Sequence_Dictionary_MD5_Checksum
m5 <- Parser SAM_V1_6_Reference_Sequence_Dictionary_MD5_Checksum
-> Parser
     (Maybe SAM_V1_6_Reference_Sequence_Dictionary_MD5_Checksum)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Reference_Sequence_Dictionary_MD5_Checksum
parse_SAM_V1_6_SAM_V1_6_Reference_Sequence_Dictionary_M5
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Reference_Sequence_Dictionary_Species
sp <- Parser SAM_V1_6_Reference_Sequence_Dictionary_Species
-> Parser (Maybe SAM_V1_6_Reference_Sequence_Dictionary_Species)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Reference_Sequence_Dictionary_Species
parse_SAM_V1_6_SAM_V1_6_Reference_Sequence_Dictionary_SP
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Reference_Sequence_Dictionary_Molecule_Topology
tp <- Parser SAM_V1_6_Reference_Sequence_Dictionary_Molecule_Topology
-> Parser
     (Maybe SAM_V1_6_Reference_Sequence_Dictionary_Molecule_Topology)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Reference_Sequence_Dictionary_Molecule_Topology
parse_SAM_V1_6_SAM_V1_6_Reference_Sequence_Dictionary_TP
  Word8
_  <- Word8 -> Parser Word8
word8 Word8
09
  Maybe SAM_V1_6_Reference_Sequence_Dictionary_URI
ur <- Parser SAM_V1_6_Reference_Sequence_Dictionary_URI
-> Parser (Maybe SAM_V1_6_Reference_Sequence_Dictionary_URI)
forall a. Parser a -> Parser (Maybe a)
maybeOption Parser SAM_V1_6_Reference_Sequence_Dictionary_URI
parse_SAM_V1_6_SAM_V1_6_Reference_Sequence_Dictionary_UR 
  SAM_V1_6_Reference_Sequence_Dictionary
-> Parser SAM_V1_6_Reference_Sequence_Dictionary
forall a. a -> Parser ByteString a
forall (m :: * -> *) a. Monad m => a -> m a
return SAM_V1_6_Reference_Sequence_Dictionary { sam_v1_6_reference_sequence_dictionary_reference_sequence_name :: SAM_V1_6_Reference_Sequence_Dictionary_Reference_Sequence_Name
sam_v1_6_reference_sequence_dictionary_reference_sequence_name                        = SAM_V1_6_Reference_Sequence_Dictionary_Reference_Sequence_Name
sn  
                                                , sam_v1_6_reference_sequence_dictionary_reference_sequence_length :: SAM_V1_6_Reference_Sequence_Dictionary_Reference_Sequence_Length
sam_v1_6_reference_sequence_dictionary_reference_sequence_length                      = SAM_V1_6_Reference_Sequence_Dictionary_Reference_Sequence_Length
ln
                                                , sam_v1_6_reference_sequence_dictionary_reference_alternative_locus :: Maybe SAM_V1_6_Reference_Sequence_Dictionary_Alternative_Locus
sam_v1_6_reference_sequence_dictionary_reference_alternative_locus                    = Maybe SAM_V1_6_Reference_Sequence_Dictionary_Alternative_Locus
ah
                                                , sam_v1_6_reference_sequence_dictionary_reference_alternative_reference_sequence_names :: Maybe
  SAM_V1_6_Reference_Sequence_Dictionary_Alternative_Reference_Sequence_Names
sam_v1_6_reference_sequence_dictionary_reference_alternative_reference_sequence_names = Maybe
  SAM_V1_6_Reference_Sequence_Dictionary_Alternative_Reference_Sequence_Names
an
                                                , sam_v1_6_reference_sequence_dictionary_genome_assembly_identifier :: Maybe
  SAM_V1_6_Reference_Sequence_Dictionary_Genome_Assembly_Identifier
sam_v1_6_reference_sequence_dictionary_genome_assembly_identifier                     = Maybe
  SAM_V1_6_Reference_Sequence_Dictionary_Genome_Assembly_Identifier
as
                                                , sam_v1_6_reference_sequence_dictionary_description :: Maybe SAM_V1_6_Reference_Sequence_Dictionary_Description
sam_v1_6_reference_sequence_dictionary_description                                    = Maybe SAM_V1_6_Reference_Sequence_Dictionary_Description
ds
                                                , sam_v1_6_reference_sequence_dictionary_md5_checksum :: Maybe SAM_V1_6_Reference_Sequence_Dictionary_MD5_Checksum
sam_v1_6_reference_sequence_dictionary_md5_checksum                                   = Maybe SAM_V1_6_Reference_Sequence_Dictionary_MD5_Checksum
m5
                                                , sam_v1_6_reference_sequence_dictionary_species :: Maybe SAM_V1_6_Reference_Sequence_Dictionary_Species
sam_v1_6_reference_sequence_dictionary_species                                        = Maybe SAM_V1_6_Reference_Sequence_Dictionary_Species
sp
                                                , sam_v1_6_reference_sequence_dictionary_molecule_topology :: Maybe SAM_V1_6_Reference_Sequence_Dictionary_Molecule_Topology
sam_v1_6_reference_sequence_dictionary_molecule_topology                              = Maybe SAM_V1_6_Reference_Sequence_Dictionary_Molecule_Topology
tp
                                                , sam_v1_6_reference_sequence_dictionary_uri :: Maybe SAM_V1_6_Reference_Sequence_Dictionary_URI
sam_v1_6_reference_sequence_dictionary_uri                                            = Maybe SAM_V1_6_Reference_Sequence_Dictionary_URI
ur
                                                }