module Biobase.Infernal.CM.Export where
import qualified Data.Array.IArray as A
import Text.Printf
import Biobase.Infernal.CM
import Biobase.Infernal.CM.Import
toString :: CM n s -> String
toString cm@CM{..} = unlines $ hdr ++ ["MODEL:"] ++ nds ++ ["//"]
where
hdr = map (\(k,v) -> k ++ " " ++ v) header
nds = concatMap (nodeToString cm) $ A.elems nodes
nodeToString :: CM n s -> Node n -> [String]
nodeToString cm Node{..} = printf "\t\t\t\t[ %s %d ]" (show ntype) nid : map (stateToString cm . (states cm A.!)) nstates
stateToString :: CM n s -> State s -> String
stateToString cm State{..} = printf " %2s %6d %6d %2d %6d %6d %-55s %s" (show stype) sid maxP numP minC numC tscores escores
where
maxP
| null sparents = 1
| otherwise = maximum sparents
numP = length sparents
minC
| null schildren = 1
| otherwise = minimum $ map tchild schildren
numC
| stype == B = maximum $ map tchild schildren
| otherwise = length schildren
tscores :: String
tscores
| stype == B = ""
| otherwise = concatMap (printf "%s " . stringS . tscore) schildren
escores :: String
escores
| null semission = ""
| otherwise = concatMap (printf "%.3f " . escore) semission
stringS x
| x == (1/0) = " *"
| otherwise = printf "%.3f" x