{-# LANGUAGE OverloadedStrings #-}

-- |
-- Module      :  SLynx.Tools
-- Description :  Common tools for sequence lynx
-- Copyright   :  2021 Dominik Schrempf
-- License     :  GPL-3.0-or-later
--
-- Maintainer  :  dominik.schrempf@gmail.com
-- Stability   :  unstable
-- Portability :  portable
--
-- Creation date: Sat Sep  7 06:24:22 2019.
module SLynx.Tools
  ( -- * SLynx.Tools
    readSeqs,

    -- * Options
    alphabetOpt,
  )
where

import Control.Monad.IO.Class
import ELynx.Alphabet.Alphabet
import ELynx.Sequence.Import.Fasta
import ELynx.Sequence.Sequence
import ELynx.Tools.InputOutput
import ELynx.Tools.Logger
import Options.Applicative

-- | Read sequences of given alphabet from file or standard input.
readSeqs ::
  (HasLock e, HasLogHandles e, HasVerbosity e) =>
  Alphabet ->
  FilePath ->
  Logger e [Sequence]
readSeqs :: forall e.
(HasLock e, HasLogHandles e, HasVerbosity e) =>
Alphabet -> FilePath -> Logger e [Sequence]
readSeqs Alphabet
a FilePath
fp = do
  forall e.
(HasLock e, HasLogHandles e, HasVerbosity e) =>
FilePath -> Logger e ()
logInfoS forall a b. (a -> b) -> a -> b
$
    FilePath
"Read sequences from file "
      forall a. Semigroup a => a -> a -> a
<> FilePath
fp
      forall a. Semigroup a => a -> a -> a
<> FilePath
"; alphabet "
      forall a. Semigroup a => a -> a -> a
<> forall a. Show a => a -> FilePath
show Alphabet
a
      forall a. Semigroup a => a -> a -> a
<> FilePath
"."
  forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO forall a b. (a -> b) -> a -> b
$ forall a. Parser a -> FilePath -> IO a
parseFileWith (Alphabet -> Parser [Sequence]
fasta Alphabet
a) FilePath
fp

-- | Command line option to specify the alphabet. Used by various commands.
alphabetOpt :: Parser Alphabet
alphabetOpt :: Parser Alphabet
alphabetOpt =
  forall a. ReadM a -> Mod OptionFields a -> Parser a
option forall a. Read a => ReadM a
auto forall a b. (a -> b) -> a -> b
$
    forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"alphabet"
      forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'a'
      forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"NAME"
      forall a. Semigroup a => a -> a -> a
<> forall (f :: * -> *) a. FilePath -> Mod f a
help
        FilePath
"Specify alphabet type NAME"