Skip to content

Commit

Permalink
Merge 'thunk' tool into 'ouch' tool
Browse files Browse the repository at this point in the history
Expand with CLI options with usage:

  ouch -e C4Cl6               # enumeration
  ouch -f some_compounds.smi  # list compounds in SMILES file

In general `ouch -?`:

  Common flags:
    -f --file=FILE          List compounds from SMILES file
    -e --enumerate=FORMULA  Enumerate molecular formula
    -? --help               Display help message
    -V --version            Print version information
  • Loading branch information
mkrauskopf committed Apr 25, 2013
1 parent 6c872fb commit a50d29c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 37 deletions.
19 changes: 0 additions & 19 deletions Ouch/resources/app/thunk.hs

This file was deleted.

4 changes: 2 additions & 2 deletions ouch.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ executable ouch
main-is: tools/ouch.hs
ghc-options: -Wall -O2
build-depends: base >=3 && <5, binary -any, bytestring -any,
containers -any, deepseq -any, parallel -any, parsec >=3.1.0,
time -any, vector -any
containers -any, deepseq -any, parallel -any, parsec >=3.1.0,
time -any, vector -any, cmdargs

66 changes: 50 additions & 16 deletions tools/ouch.hs
Original file line number Diff line number Diff line change
@@ -1,24 +1,50 @@
{-# LANGUAGE RecordWildCards, DeriveDataTypeable #-}
--
-- So far simple SMILES file CLI printer without options.
-- CLI OUCH tool.
--
-- Usage: ouch <SMILES_file>
-- Common flags:
-- -f --file=FILE List compounds from SMILES file
-- -e --enumerate=FORMULA Enumerate molecular formula
-- -? --help Display help message
-- -V --version Print version information
--

import Control.Monad(when)
import Control.Monad(unless)
import Data.Functor((<$>))
import Ouch.Enumerate.Formula(expand, Formula)
import Ouch.Input.Smiles(readSmi)
import Ouch.Property.Composition(molecularFormula, molecularWeight)
import Ouch.Structure.Molecule
import System.Environment
import System.Exit(exitFailure)
import System.Console.CmdArgs.Implicit
import System.Environment(getArgs, withArgs)
import Text.Printf(printf)
import qualified Ouch.Property.Builder as OPB

showMolecule :: String -- ^ SMILES string
-> String -- ^ description
showMolecule smile = smile ++ "\n --> " ++ showProperty molecularFormula mol ++ ", " ++ showMolWeight mol
where
mol = readSmi smile
ouchVersion, ouchProgram, ouchSummary :: String
ouchVersion = "0.0.1"
ouchProgram = "ouch"
ouchSummary = ouchProgram ++ " v" ++ ouchVersion

data CmdOptions = CmdOptions {
file :: String,
enumerate :: String
} deriving (Show, Data, Typeable)

defOpts :: CmdOptions
defOpts = CmdOptions
{ file = "" &= typ "FILE" &= help "List compounds from SMILES file"
, enumerate = "" &= typ "FORMULA" &= help "Enumerate molecular formula"
} &= summary ouchSummary
&= program ouchProgram
&= help "OUCH CLI tool"


showMolecule :: Molecule -> String
showMolecule mol = showProperty molecularFormula mol ++ ", " ++ showMolWeight mol

showSmile :: String -- ^ SMILES string
-> String -- ^ description
showSmile smile = smile ++ "\n --> " ++ showMolecule (readSmi smile)

showProperty :: OPB.Property -> Molecule -> String
showProperty p m = show $ getPropertyValue p m
Expand All @@ -32,12 +58,20 @@ getPropertyValue prop m = case OPB.value prop of
Left v -> v
Right f -> f m

-- | If no arguments are passed to a program, displays help message and exits,
-- returns parsed arguments otherwise.
cmdArgsOrHelp :: Data a => a -> IO a
cmdArgsOrHelp opts = do
mainArgs <- getArgs
(if null mainArgs then withArgs ["-?"] else id) (cmdArgs opts)

main :: IO ()
main = do
args <- getArgs
when (null args) $ do
putStrLn "ouch: SMILES file operand required"
exitFailure
smiles <- lines <$> readFile (head args)
mapM_ (putStrLn . showMolecule) smiles
CmdOptions{..} <- cmdArgsOrHelp defOpts
unless (null file) $ do
smiles <- lines <$> readFile file
mapM_ (putStrLn . showSmile) smiles
unless (null enumerate) $
mapM_ print (expand (read enumerate::Formula))


0 comments on commit a50d29c

Please sign in to comment.