{-# Language OverloadedStrings #-}
{-# Language DeriveDataTypeable #-}

module Main where

import Clustal
import Control.Monad (when) 
import qualified Data.ByteString.Lazy.Char8 as B
import System.Console.CmdArgs

data Opt = Opt { inputs :: FilePath
               , txtout :: Maybe FilePath
               , columns :: Maybe FilePath
               } deriving (Typeable, Data, Show, Eq)

myopts :: Opt
myopts = Opt
        { inputs  = "-" &= args &= typFile
        , txtout  = Nothing &= help "Output file for text format"
        , columns = Nothing &= help "Output file for column histogram"
        }

main :: IO ()
main = do
  o <- cmdArgs myopts
  (h:ls) <- B.lines `fmap` if inputs o == "-" then B.getContents else B.readFile (inputs o)
  when ((head $ B.words h) /= "CLUSTAL") (error "Not clustal ALN input?")
  let m = merge $ chunks $ drop 2 ls

  case txtout o of 
    Just t -> writeF t (B.unlines m)
    Nothing -> if Nothing == columns o then B.putStrLn (B.unlines m) 
               else return ()
  
  case columns o of
    Just c -> writeF c (B.unlines $ map format $ col_freq m)
    Nothing -> return ()

format :: (Int,[(Char,Int)]) -> B.ByteString
format (i,col) = B.pack . ((show i++"\t")++) . unwords . map format1 $ col
  where 
    format1 (c,l) = c:':':show l

writeF :: FilePath -> B.ByteString -> IO ()
writeF "-"  = B.putStrLn
writeF f  = B.writeFile f
