{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TemplateHaskell #-}

-- |
-- Module      :  SLynx.Tools
-- Description :  Common tools for sequence lynx
-- Copyright   :  (c) Dominik Schrempf 2021
-- 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.Data.Alphabet.Alphabet
import ELynx.Data.Sequence.Sequence
import ELynx.Import.Sequence.Fasta
import ELynx.Tools
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 :: Alphabet -> FilePath -> Logger e [Sequence]
readSeqs Alphabet
a FilePath
fp = do
  FilePath -> Logger e ()
forall e.
(HasLock e, HasLogHandles e, HasVerbosity e) =>
FilePath -> Logger e ()
logInfoS (FilePath -> Logger e ()) -> FilePath -> Logger e ()
forall a b. (a -> b) -> a -> b
$
    FilePath
"Read sequences from file "
      FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> FilePath
fp
      FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> FilePath
"; alphabet "
      FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> Alphabet -> FilePath
forall a. Show a => a -> FilePath
show Alphabet
a
      FilePath -> FilePath -> FilePath
forall a. Semigroup a => a -> a -> a
<> FilePath
"."
  IO [Sequence] -> Logger e [Sequence]
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO [Sequence] -> Logger e [Sequence])
-> IO [Sequence] -> Logger e [Sequence]
forall a b. (a -> b) -> a -> b
$ Parser [Sequence] -> FilePath -> IO [Sequence]
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 =
  ReadM Alphabet -> Mod OptionFields Alphabet -> Parser Alphabet
forall a. ReadM a -> Mod OptionFields a -> Parser a
option ReadM Alphabet
forall a. Read a => ReadM a
auto (Mod OptionFields Alphabet -> Parser Alphabet)
-> Mod OptionFields Alphabet -> Parser Alphabet
forall a b. (a -> b) -> a -> b
$
    FilePath -> Mod OptionFields Alphabet
forall (f :: * -> *) a. HasName f => FilePath -> Mod f a
long FilePath
"alphabet" Mod OptionFields Alphabet
-> Mod OptionFields Alphabet -> Mod OptionFields Alphabet
forall a. Semigroup a => a -> a -> a
<> Char -> Mod OptionFields Alphabet
forall (f :: * -> *) a. HasName f => Char -> Mod f a
short Char
'a' Mod OptionFields Alphabet
-> Mod OptionFields Alphabet -> Mod OptionFields Alphabet
forall a. Semigroup a => a -> a -> a
<> FilePath -> Mod OptionFields Alphabet
forall (f :: * -> *) a. HasMetavar f => FilePath -> Mod f a
metavar FilePath
"NAME"
      Mod OptionFields Alphabet
-> Mod OptionFields Alphabet -> Mod OptionFields Alphabet
forall a. Semigroup a => a -> a -> a
<> FilePath -> Mod OptionFields Alphabet
forall (f :: * -> *) a. FilePath -> Mod f a
help
        FilePath
"Specify alphabet type NAME"