module Biobase.Infernal.VerboseHit.Export where
import Control.Monad.Trans.Class (lift)
import Data.ByteString.Char8 as BS
import Data.Iteratee as I
import Data.Maybe
import Prelude as P
import Text.Printf
import Biobase.Infernal.Types
import Biobase.Infernal.VerboseHit
import Biobase.Infernal.VerboseHit.Internal
eneeByteString :: Monad m => Enumeratee [VerboseHit] ByteString m a
eneeByteString = eneeByteStrings ><> mapChunks BS.concat
eneeByteStrings :: Monad m => Enumeratee [VerboseHit] [ByteString] m a
eneeByteStrings = unfoldConvStream f (AliGo BS.empty BS.empty '?' []) where
f acc = do
h <- I.head
let na = newAcc acc h
p <- I.peek
return ( fst na
, return . BS.unlines $ snd na ++ P.map (append "##") (vhAnnotation h) ++ [showVerboseHit h] ++ maybe ["//"] (const []) p
)
newAcc a@(AliGo{..}) h@VerboseHit{..}
| otherwise = ( AliGo (unModelIdentification vhModel) (unScaffold vhTarget) vhStrand [], ls )
where ls = [ "//" | aliCM /= BS.empty && bCM ] ++
[ "CM: " `BS.append` unModelIdentification vhModel | bCM ] ++
[ ">" `BS.append` unScaffold vhTarget `BS.append` "\n" | bCM || bSc] ++
[ str `BS.append` " strand results:\n" | bCM || bSc || bSt ]
bCM = aliCM /= unModelIdentification vhModel
bSc = aliScaffold /= unScaffold vhTarget
bSt = aliStrand /= vhStrand
str
| vhStrand == '+' = "Plus"
| vhStrand == '-' = "Minus"
| otherwise = "Unknown"
showVerboseHit :: VerboseHit -> BS.ByteString
showVerboseHit VerboseHit{..} = BS.unlines
[ BS.pack $ printf " Query = %d - %d, Target = %d - %d"
vhModelStart vhModelStop vhTargetStart vhTargetStop
, BS.pack $ printf " Score = %.2f, E = %f, P = %.4e, GC = %d"
(unBitScore vhBitScore) vhEvalue vhPvalue vhGCpercent
, ""
, ws11 `BS.append` vhWuss
, (BS.pack $ printf "%10d " vhModelStart)
`BS.append` vhConsensus
`BS.append` (BS.pack $ printf " %d" vhModelStop)
, ws11 `BS.append` vhScoring
, (BS.pack $ printf "%10d " vhTargetStart)
`BS.append` vhSequence
`BS.append` (BS.pack $ printf " %d" vhTargetStop)
] where
ws11 = BS.pack $ P.replicate 11 ' '