-- ace2fa
-- Read an ACE file, output in Fasta format
-- (for importing into seaview)

module Main where

import Bio.Alignment.AlignData (Gaps,insertGaps)
import Bio.Alignment.ACE
import Bio.Sequence
import qualified Data.ByteString.Lazy.Char8 as B
import System.SimpleArgs (getArgs)

main :: IO ()
main = do 
  a <- readACE =<< getArgs
  mapM_ (\ss -> writeFasta (B.unpack $ seqlabel $ head ss) ss) (map pad_all $ concat a) 

pad_all :: Assembly -> [Sequence Nuc]
pad_all asm = pad 21 (contig asm) : map padRead (fragments asm)
    where padRead (off,_dir,sq,gs) = pad (20+off) (sq,gs) -- note: they are already complemented

-- generate a sequence with '-' for gaps
pad :: Offset -> (Sequence Nuc,Gaps) -> Sequence Nuc
pad off (Seq n s _,gs) = let s' = (if off < 0 then B.drop (negate off) else B.append (B.replicate off '-')) (insertGaps '-' (s,gs))
                         in (Seq n s' Nothing)




