module NewickConvert
( newickToTree
) where
import Data.Maybe
import Data.Tree
import qualified Data.Sequence as Seq
import qualified Data.List.Safe as Safe
import qualified Data.Text as T
import Biobase.Newick
import Types
newickToTree :: Separator -> Field -> NewickTree -> Tree NodeLabel
newickToTree sep field = go . getNewickTree
where
go (Node { rootLabel = x, subForest = ls }) =
Node { rootLabel = NodeLabel { nodeID = label x
, nodeLabels = getLabels sep field
. label
$ x
}
, subForest = map go ls
}
getLabels :: Separator -> Field -> T.Text -> Labels
getLabels (Separator sep) (Field field) =
Seq.singleton . fromMaybe "" . (Safe.!! (field 1)) . T.splitOn sep