module ELynx.Data.Tree.Supported
( BranchSupport,
Supported (..),
normalizeBranchSupport,
collapse,
)
where
import Data.Bifoldable
import Data.Bifunctor
import Data.List
import ELynx.Data.Tree.Rooted
type BranchSupport = Double
class Supported e where
getSup :: e -> BranchSupport
setSup :: BranchSupport -> e -> e
apply :: Supported e => (BranchSupport -> BranchSupport) -> e -> e
apply f l = setSup (f s) l where s = getSup l
normalizeBranchSupport :: Supported e => Tree e a -> Tree e a
normalizeBranchSupport t = first (apply (/ m)) t
where
m = bimaximum $ bimap getSup (const 0) t
collapse :: (Eq e, Eq a, Supported e) => BranchSupport -> Tree e a -> Tree e a
collapse th tr =
let tr' = collapse' th tr
in if tr == tr' then tr else collapse th tr'
collapse' :: Supported e => BranchSupport -> Tree e a -> Tree e a
collapse' _ t@(Node _ _ []) = t
collapse' th (Node br lb ts) = Node br lb $ map (collapse' th) (highSupport ++ lowSupportForest)
where
(highSupport, lowSupport) = partition ((>= th) . getSup . branch) ts
lowSupportForest = concatMap forest lowSupport