-- |
-- Module      :  ELynx.Export.Sequence.Fasta
-- Description :  Export Fasta sequences
-- Copyright   :  (c) Dominik Schrempf 2021
-- License     :  GPL-3.0-or-later
--
--
-- Maintainer  :  dominik.schrempf@gmail.com
-- Stability   :  unstable
-- Portability :  portable
--
-- Write FASTA files.
--
-- [NCBI file specifications](https://blast.ncbi.nlm.nih.gov/Blast.cgi?CMD=Web&PAGE_TYPE=BlastDocs&DOC_TYPE=BlastHelp).
module ELynx.Export.Sequence.Fasta
  ( sequenceToFasta,
    sequencesToFasta,
  )
where

import qualified Data.ByteString.Lazy.Char8 as BL
import ELynx.Data.Sequence.Sequence

fastaHeader :: BL.ByteString -> BL.ByteString -> BL.ByteString
fastaHeader :: ByteString -> ByteString -> ByteString
fastaHeader ByteString
n ByteString
d =
  Char -> ByteString
BL.singleton Char
'>' ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
n ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> if ByteString -> Bool
BL.null ByteString
d then ByteString
BL.empty else [Char] -> ByteString
BL.pack [Char]
" " ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> ByteString
d

-- | Convert a 'Sequence' to Fasta format.
sequenceToFasta :: Sequence -> BL.ByteString
sequenceToFasta :: Sequence -> ByteString
sequenceToFasta Sequence
s =
  [ByteString] -> ByteString
BL.unlines [ByteString -> ByteString -> ByteString
fastaHeader (Sequence -> ByteString
name Sequence
s) (Sequence -> ByteString
description Sequence
s), Characters -> ByteString
toByteString (Characters -> ByteString) -> Characters -> ByteString
forall a b. (a -> b) -> a -> b
$ Sequence -> Characters
characters Sequence
s]

-- | Convert a list 'Sequence's to Fasta format. A newline is added between any
-- two 'Sequence's.
sequencesToFasta :: [Sequence] -> BL.ByteString
sequencesToFasta :: [Sequence] -> ByteString
sequencesToFasta [Sequence]
ss = [ByteString] -> ByteString
BL.concat ([ByteString] -> ByteString) -> [ByteString] -> ByteString
forall a b. (a -> b) -> a -> b
$ (Sequence -> ByteString) -> [Sequence] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map Sequence -> ByteString
sequenceToFasta [Sequence]
ss