2 Module : Gargantext.Viz.Phylo.Tools
3 Description : Phylomemy Tools to build/manage it
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 {-# LANGUAGE FlexibleContexts #-}
14 {-# LANGUAGE NoImplicitPrelude #-}
15 {-# LANGUAGE OverloadedStrings #-}
16 {-# LANGUAGE RankNTypes #-}
17 {-# LANGUAGE ViewPatterns #-}
19 module Gargantext.Viz.Phylo.Tools
22 import Control.Lens hiding (both, Level, Empty)
23 import Data.List (filter, intersect, (++), sort, null, tail, last, tails, delete, nub, sortOn, nubBy)
24 import Data.Maybe (mapMaybe,fromMaybe)
25 import Data.Map (Map, mapKeys, member, (!))
27 import Data.Text (Text,toLower,unwords)
28 import Data.Tuple.Extra
29 import Data.Vector (Vector,elemIndex)
30 import Gargantext.Prelude
31 import Gargantext.Viz.Phylo
32 import qualified Data.Map as Map
33 import qualified Data.Set as Set
34 import qualified Data.Vector as Vector
42 -- | Define a default value
43 def :: a -> Maybe a -> a
47 -- | Does a List of Sets contains at least one Set of an other List
48 doesAnySetContains :: Eq a => Set a -> [Set a] -> [Set a] -> Bool
49 doesAnySetContains h l l' = any (\c -> doesContains (Set.toList c) (Set.toList h)) (l' ++ l)
52 -- | Does a list of A contains an other list of A
53 doesContains :: Eq a => [a] -> [a] -> Bool
56 | length l' > length l = False
57 | elem (head' "doesContains" l') l = doesContains l (tail l')
61 -- | Does a list of ordered A contains an other list of ordered A
62 doesContainsOrd :: Eq a => Ord a => [a] -> [a] -> Bool
65 | last l < (head' "doesContainsOrd" l') = False
66 | (head' "doesContainsOrd" l') `elem` l = True
67 | otherwise = doesContainsOrd l (tail l')
70 -- | To filter nested Sets of a
71 filterNestedSets :: Eq a => Set a -> [Set a] -> [Set a] -> [Set a]
72 filterNestedSets h l l'
73 | null l = if doesAnySetContains h l l'
76 | doesAnySetContains h l l' = filterNestedSets (head' "filterNestedSets1" l) (tail l) l'
77 | otherwise = filterNestedSets (head' "filterNestedSets2" l) (tail l) (h : l')
81 -- | To get the good pair of keys (x,y) or (y,x) in a given Map (a,b) c
82 getKeyPair :: (Int,Int) -> Map (Int,Int) a -> (Int,Int)
83 getKeyPair (x,y) m = case findPair (x,y) m of
84 Nothing -> panic "[ERR][Viz.Phylo.Example.getKeyPair] Nothing"
87 --------------------------------------
88 findPair :: (Int,Int) -> Map (Int,Int) a -> Maybe (Int,Int)
90 | member (x',y') m' = Just (x',y')
91 | member (y',x') m' = Just (y',x')
93 --------------------------------------
96 -- | To filter Fis with small Support but by keeping non empty Periods
97 keepFilled :: (Int -> [a] -> [a]) -> Int -> [a] -> [a]
98 keepFilled f thr l = if (null $ f thr l) && (not $ null l)
99 then keepFilled f (thr - 1) l
103 -- | To get all combinations of a list
104 listToFullCombi :: Eq a => [a] -> [(a,a)]
105 listToFullCombi l = [(x,y) | x <- l, y <- l]
108 -- | To get all combinations of a list
109 listToDirectedCombi :: Eq a => [a] -> [(a,a)]
110 listToDirectedCombi l = [(x,y) | x <- l, y <- l, x /= y]
113 -- | To get all combinations of a list and apply a function to the resulting list of pairs
114 listToDirectedCombiWith :: Eq a => forall b. (a -> b) -> [a] -> [(b,b)]
115 listToDirectedCombiWith f l = [(f x,f y) | x <- l, y <- l, x /= y]
118 -- | To get the sequential combinations of an order list
119 listToSequentialCombi :: Eq a => [a] -> [(a,a)]
120 listToSequentialCombi l = nubBy (\x y -> fst x == fst y) $ listToUnDirectedCombi l
123 -- | To get all combinations of a list with no repetition
124 listToUnDirectedCombi :: [a] -> [(a,a)]
125 listToUnDirectedCombi l = [ (x,y) | (x:rest) <- tails l, y <- rest ]
128 -- | To get all combinations of a list with no repetition and apply a function to the resulting list of pairs
129 listToUnDirectedCombiWith :: forall a b. (a -> b) -> [a] -> [(b,b)]
130 listToUnDirectedCombiWith f l = [ (f x, f y) | (x:rest) <- tails l, y <- rest ]
133 -- | To transform a list of Ngrams Indexes into a Label
134 ngramsToLabel :: Vector Ngrams -> [Int] -> Text
135 ngramsToLabel ngrams l = unwords $ ngramsToText ngrams l
138 -- | To transform a list of Ngrams Indexes into a list of Text
139 ngramsToText :: Vector Ngrams -> [Int] -> [Text]
140 ngramsToText ngrams l = map (\idx -> ngrams Vector.! idx) l
143 -- | To unify the keys (x,y) that Map 1 share with Map 2 such as: (x,y) <=> (y,x)
144 unifySharedKeys :: Eq a => Ord a => Map (a,a) b -> Map (a,a) b -> Map (a,a) b
145 unifySharedKeys m1 m2 = mapKeys (\(x,y) -> if member (y,x) m2
154 -- | An analyzer ingests a Ngrams and generates a modified version of it
155 phyloAnalyzer :: Ngrams -> Ngrams
156 phyloAnalyzer n = toLower n
158 -- | To init the foundation roots of the Phylo as a Vector of Ngrams
159 initFoundationsRoots :: [Ngrams] -> Vector Ngrams
160 initFoundationsRoots l = Vector.fromList $ map phyloAnalyzer l
162 -- | To init the base of a Phylo from a List of Periods and Foundations
163 initPhyloBase :: [(Date, Date)] -> PhyloFoundations -> PhyloParam -> Phylo
164 initPhyloBase pds fds prm = Phylo ((fst . (head' "initPhyloBase")) pds, (snd . last) pds) fds (map (\pd -> initPhyloPeriod pd []) pds) prm
166 -- | To init the param of a Phylo
167 initPhyloParam :: Maybe Text -> Maybe Software -> Maybe PhyloQueryBuild -> PhyloParam
168 initPhyloParam (def defaultPhyloVersion -> v) (def defaultSoftware -> s) (def defaultQueryBuild -> q) = PhyloParam v s q
170 -- | To get the last computed Level in a Phylo
171 getLastLevel :: Phylo -> Level
172 getLastLevel p = (last . sort)
173 $ map (snd . getPhyloLevelId)
174 $ view ( phylo_periods
176 . phylo_periodLevels ) p
183 -- | To get the foundations of a Phylo
184 getFoundations :: Phylo -> PhyloFoundations
185 getFoundations = _phylo_foundations
187 -- | To get the foundations roots of a Phylo
188 getFoundationsRoots :: Phylo -> Vector Ngrams
189 getFoundationsRoots p = (getFoundations p) ^. phylo_foundationsRoots
191 -- | To get the Index of a Ngrams in the foundationsRoots of a Phylo
192 getIdxInRoots :: Ngrams -> Phylo -> Int
193 getIdxInRoots n p = case (elemIndex n (getFoundationsRoots p)) of
194 Nothing -> panic "[ERR][Viz.Phylo.Tools.getIdxInRoots] Ngrams not in foundationsRoots"
202 -- | To alter a PhyloGroup matching a given Level
203 alterGroupWithLevel :: (PhyloGroup -> PhyloGroup) -> Level -> Phylo -> Phylo
204 alterGroupWithLevel f lvl p = over ( phylo_periods
210 ) (\g -> if getGroupLevel g == lvl
215 -- | To alter each list of PhyloGroups following a given function
216 alterPhyloGroups :: ([PhyloGroup] -> [PhyloGroup]) -> Phylo -> Phylo
217 alterPhyloGroups f p = over ( phylo_periods
225 -- | To filter the PhyloGroup of a Phylo according to a function and a value
226 filterGroups :: Eq a => (PhyloGroup -> a) -> a -> [PhyloGroup] -> [PhyloGroup]
227 filterGroups f x l = filter (\g -> (f g) == x) l
230 -- | To maybe get the PhyloBranchId of a PhyloGroup
231 getGroupBranchId :: PhyloGroup -> Maybe PhyloBranchId
232 getGroupBranchId = _phylo_groupBranchId
235 -- | To get the PhyloGroups Childs of a PhyloGroup
236 getGroupChilds :: PhyloGroup -> Phylo -> [PhyloGroup]
237 getGroupChilds g p = getGroupsFromIds (getGroupPeriodChildsId g) p
240 -- | To get the id of a PhyloGroup
241 getGroupId :: PhyloGroup -> PhyloGroupId
242 getGroupId = _phylo_groupId
245 -- | To get the level out of the id of a PhyloGroup
246 getGroupLevel :: PhyloGroup -> Int
247 getGroupLevel = snd . fst . getGroupId
250 -- | To get the level child pointers of a PhyloGroup
251 getGroupLevelChilds :: PhyloGroup -> [Pointer]
252 getGroupLevelChilds = _phylo_groupLevelChilds
255 -- | To get the PhyloGroups Level Childs Ids of a PhyloGroup
256 getGroupLevelChildsId :: PhyloGroup -> [PhyloGroupId]
257 getGroupLevelChildsId g = map fst $ getGroupLevelChilds g
260 -- | To get the level parent pointers of a PhyloGroup
261 getGroupLevelParents :: PhyloGroup -> [Pointer]
262 getGroupLevelParents = _phylo_groupLevelParents
265 -- | To get the PhyloGroups Level Parents Ids of a PhyloGroup
266 getGroupLevelParentsId :: PhyloGroup -> [PhyloGroupId]
267 getGroupLevelParentsId g = map fst $ getGroupLevelParents g
270 -- | To get the PhyloGroups Level Parents Ids of a PhyloGroup
271 getGroupLevelParentId :: PhyloGroup -> PhyloGroupId
272 getGroupLevelParentId g = (head' "getGroupLevelParentId") $ getGroupLevelParentsId g
274 -- | To get the Meta value of a PhyloGroup
275 getGroupMeta :: Text -> PhyloGroup -> Double
276 getGroupMeta k g = (g ^. phylo_groupMeta) ! k
279 -- | To get the Ngrams of a PhyloGroup
280 getGroupNgrams :: PhyloGroup -> [Int]
281 getGroupNgrams = _phylo_groupNgrams
284 -- | To get the list of pairs (Childs & Parents) of a PhyloGroup
285 getGroupPairs :: PhyloGroup -> Phylo -> [PhyloGroup]
286 getGroupPairs g p = (getGroupChilds g p) ++ (getGroupParents g p)
289 -- | To get the PhyloGroups Parents of a PhyloGroup
290 getGroupParents :: PhyloGroup -> Phylo -> [PhyloGroup]
291 getGroupParents g p = getGroupsFromIds (getGroupPeriodParentsId g) p
294 -- | To get the period out of the id of a PhyloGroup
295 getGroupPeriod :: PhyloGroup -> (Date,Date)
296 getGroupPeriod = fst . fst . getGroupId
299 -- | To get the period child pointers of a PhyloGroup
300 getGroupPeriodChilds :: PhyloGroup -> [Pointer]
301 getGroupPeriodChilds = _phylo_groupPeriodChilds
304 -- | To get the PhyloGroups Period Parents Ids of a PhyloGroup
305 getGroupPeriodChildsId :: PhyloGroup -> [PhyloGroupId]
306 getGroupPeriodChildsId g = map fst $ getGroupPeriodChilds g
309 -- | To get the period parent pointers of a PhyloGroup
310 getGroupPeriodParents :: PhyloGroup -> [Pointer]
311 getGroupPeriodParents = _phylo_groupPeriodParents
314 -- | To get the PhyloGroups Period Parents Ids of a PhyloGroup
315 getGroupPeriodParentsId :: PhyloGroup -> [PhyloGroupId]
316 getGroupPeriodParentsId g = map fst $ getGroupPeriodParents g
319 -- | To get the pointers of a given Phylogroup
320 getGroupPointers :: EdgeType -> Filiation -> PhyloGroup -> [Pointer]
321 getGroupPointers t f g = case t of
322 PeriodEdge -> case f of
323 Ascendant -> getGroupPeriodParents g
324 Descendant -> getGroupPeriodChilds g
325 _ -> panic "[ERR][Viz.Phylo.Tools.getGroupPointers] wrong filiation"
326 LevelEdge -> case f of
327 Ascendant -> getGroupLevelParents g
328 Descendant -> getGroupLevelChilds g
329 _ -> panic "[ERR][Viz.Phylo.Tools.getGroupPointers] wrong filiation"
332 -- | To get the roots labels of a list of group ngrams
333 getGroupText :: PhyloGroup -> Phylo -> [Text]
334 getGroupText g p = ngramsToText (getFoundationsRoots p) (getGroupNgrams g)
337 -- | To get all the PhyloGroup of a Phylo
338 getGroups :: Phylo -> [PhyloGroup]
339 getGroups = view ( phylo_periods
347 -- | To get all PhyloGroups matching a list of PhyloGroupIds in a Phylo
348 getGroupsFromIds :: [PhyloGroupId] -> Phylo -> [PhyloGroup]
349 getGroupsFromIds ids p = filter (\g -> elem (getGroupId g) ids) $ getGroups p
351 -- | To get a PhyloGroup matching a PhyloGroupId in a Phylo
352 getGroupFromId :: PhyloGroupId -> Phylo -> PhyloGroup
353 getGroupFromId id p = (head' "getGroupFromId") $ getGroupsFromIds [id] p
356 -- | To get the corresponding list of PhyloGroups from a list of PhyloNodes
357 getGroupsFromNodes :: [PhyloNode] -> Phylo -> [PhyloGroup]
358 getGroupsFromNodes ns p = getGroupsFromIds (map getNodeId ns) p
361 -- | To get all the PhyloGroup of a Phylo with a given level and period
362 getGroupsWithFilters :: Int -> (Date,Date) -> Phylo -> [PhyloGroup]
363 getGroupsWithFilters lvl prd p = (getGroupsWithLevel lvl p)
365 (getGroupsWithPeriod prd p)
368 -- | To get all the PhyloGroup of a Phylo with a given Level
369 getGroupsWithLevel :: Int -> Phylo -> [PhyloGroup]
370 getGroupsWithLevel lvl p = filterGroups getGroupLevel lvl (getGroups p)
373 -- | To get all the PhyloGroup of a Phylo with a given Period
374 getGroupsWithPeriod :: (Date,Date) -> Phylo -> [PhyloGroup]
375 getGroupsWithPeriod prd p = filterGroups getGroupPeriod prd (getGroups p)
378 -- | To create a PhyloGroup in a Phylo out of a list of Ngrams and a set of parameters
379 initGroup :: [Ngrams] -> Text -> Int -> Int -> Int -> Int -> Phylo -> PhyloGroup
380 initGroup ngrams lbl idx lvl from' to' p = PhyloGroup
381 (((from', to'), lvl), idx)
383 (sort $ map (\x -> getIdxInRoots x p) ngrams)
389 ---------------------
390 -- | PhyloPeriod | --
391 ---------------------
394 -- | To alter each PhyloPeriod of a Phylo following a given function
395 alterPhyloPeriods :: (PhyloPeriod -> PhyloPeriod) -> Phylo -> Phylo
396 alterPhyloPeriods f p = over ( phylo_periods
400 -- | To append a list of PhyloPeriod to a Phylo
401 appendToPhyloPeriods :: [PhyloPeriod] -> Phylo -> Phylo
402 appendToPhyloPeriods l p = over (phylo_periods) (++ l) p
405 -- | To get all the PhyloPeriodIds of a Phylo
406 getPhyloPeriods :: Phylo -> [PhyloPeriodId]
407 getPhyloPeriods p = map _phylo_periodId
408 $ view (phylo_periods) p
411 -- | To get the id of a given PhyloPeriod
412 getPhyloPeriodId :: PhyloPeriod -> PhyloPeriodId
413 getPhyloPeriodId prd = _phylo_periodId prd
416 -- | To create a PhyloPeriod
417 initPhyloPeriod :: PhyloPeriodId -> [PhyloLevel] -> PhyloPeriod
418 initPhyloPeriod id l = PhyloPeriod id l
426 -- | To alter a list of PhyloLevels following a given function
427 alterPhyloLevels :: ([PhyloLevel] -> [PhyloLevel]) -> Phylo -> Phylo
428 alterPhyloLevels f p = over ( phylo_periods
430 . phylo_periodLevels) f p
433 -- | To get the PhylolevelId of a given PhyloLevel
434 getPhyloLevelId :: PhyloLevel -> PhyloLevelId
435 getPhyloLevelId = _phylo_levelId
438 -- | To get all the Phylolevels of a given PhyloPeriod
439 getPhyloLevels :: PhyloPeriod -> [PhyloLevel]
440 getPhyloLevels = view (phylo_periodLevels)
443 -- | To create a PhyloLevel
444 initPhyloLevel :: PhyloLevelId -> [PhyloGroup] -> PhyloLevel
445 initPhyloLevel id groups = PhyloLevel id groups
448 -- | To set the LevelId of a PhyloLevel and of all its PhyloGroups
449 setPhyloLevelId :: Int -> PhyloLevel -> PhyloLevel
450 setPhyloLevelId lvl' (PhyloLevel (id, _lvl) groups)
451 = PhyloLevel (id, lvl') groups'
453 groups' = over (traverse . phylo_groupId)
454 (\((period, _lvl), idx) -> ((period, lvl'), idx))
463 -- | To get the clique of a PhyloFis
464 getClique :: PhyloFis -> Clique
465 getClique = _phyloFis_clique
467 -- | To get the metrics of a PhyloFis
468 getFisMetrics :: PhyloFis -> Map (Int,Int) (Map Text [Double])
469 getFisMetrics = _phyloFis_metrics
471 -- | To get the support of a PhyloFis
472 getSupport :: PhyloFis -> Support
473 getSupport = _phyloFis_support
476 ----------------------------
477 -- | PhyloNodes & Edges | --
478 ----------------------------
481 -- | To filter some GroupEdges with a given threshold
482 filterGroupEdges :: Double -> [GroupEdge] -> [GroupEdge]
483 filterGroupEdges thr edges = filter (\((_s,_t),w) -> w > thr) edges
486 -- | To get the neighbours (directed/undirected) of a PhyloGroup from a list of GroupEdges
487 getNeighbours :: Bool -> PhyloGroup -> [GroupEdge] -> [PhyloGroup]
488 getNeighbours directed g e = case directed of
489 True -> map (\((_s,t),_w) -> t)
490 $ filter (\((s,_t),_w) -> s == g) e
491 False -> map (\((s,t),_w) -> (head' "getNeighbours") $ delete g $ nub [s,t,g])
492 $ filter (\((s,t),_w) -> s == g || t == g) e
495 -- | To get the PhyloBranchId of PhyloNode if it exists
496 getNodeBranchId :: PhyloNode -> PhyloBranchId
497 getNodeBranchId n = case n ^. pn_bid of
498 Nothing -> panic "[ERR][Viz.Phylo.Tools.getNodeBranchId] branchId not found"
502 -- | To get the PhyloGroupId of a PhyloNode
503 getNodeId :: PhyloNode -> PhyloGroupId
504 getNodeId n = n ^. pn_id
507 -- | To get the Level of a PhyloNode
508 getNodeLevel :: PhyloNode -> Level
509 getNodeLevel n = (snd . fst) $ getNodeId n
512 -- | To get the Parent Node of a PhyloNode in a PhyloView
513 getNodeParent :: PhyloNode -> PhyloView -> [PhyloNode]
514 getNodeParent n v = filter (\n' -> elem (getNodeId n') (getNodeParentsId n))
518 -- | To get the Parent Node id of a PhyloNode if it exists
519 getNodeParentsId :: PhyloNode -> [PhyloGroupId]
520 getNodeParentsId n = case n ^. pn_parents of
521 Nothing -> panic "[ERR][Viz.Phylo.Tools.getNodeParentsId] node parent not found"
525 -- | To get a list of PhyloNodes grouped by PhyloBranch in a PhyloView
526 getNodesByBranches :: PhyloView -> [(PhyloBranchId,[PhyloNode])]
527 getNodesByBranches v = zip bIds $ map (\id -> filter (\n -> (getNodeBranchId n) == id)
528 $ getNodesInBranches v ) bIds
530 --------------------------------------
531 bIds :: [PhyloBranchId]
532 bIds = getViewBranchIds v
533 --------------------------------------
536 -- | To get a list of PhyloNodes owned by any PhyloBranches in a PhyloView
537 getNodesInBranches :: PhyloView -> [PhyloNode]
538 getNodesInBranches v = filter (\n -> isJust $ n ^. pn_bid)
542 -- | To get the PhyloGroupId of the Source of a PhyloEdge
543 getSourceId :: PhyloEdge -> PhyloGroupId
544 getSourceId e = e ^. pe_source
547 -- | To get the PhyloGroupId of the Target of a PhyloEdge
548 getTargetId :: PhyloEdge -> PhyloGroupId
549 getTargetId e = e ^. pe_target
552 ---------------------
553 -- | PhyloBranch | --
554 ---------------------
557 -- | To get the PhyloBranchId of a PhyloBranch
558 getBranchId :: PhyloBranch -> PhyloBranchId
559 getBranchId b = b ^. pb_id
561 -- | To get a list of PhyloBranchIds
562 getBranchIds :: Phylo -> [PhyloBranchId]
563 getBranchIds p = sortOn snd
565 $ mapMaybe getGroupBranchId
569 -- | To get a list of PhyloBranchIds given a Level in a Phylo
570 getBranchIdsWith :: Level -> Phylo -> [PhyloBranchId]
571 getBranchIdsWith lvl p = sortOn snd
572 $ mapMaybe getGroupBranchId
573 $ getGroupsWithLevel lvl p
576 -- | To get the Meta value of a PhyloBranch
577 getBranchMeta :: Text -> PhyloBranch -> [Double]
578 getBranchMeta k b = (b ^. pb_metrics) ! k
581 -- | To get all the PhyloBranchIds of a PhyloView
582 getViewBranchIds :: PhyloView -> [PhyloBranchId]
583 getViewBranchIds v = map getBranchId $ v ^. pv_branches
586 -- | To get a list of PhyloGroup sharing the same PhyloBranchId
587 getGroupsByBranches :: Phylo -> [(PhyloBranchId,[PhyloGroup])]
588 getGroupsByBranches p = zip (getBranchIds p)
589 $ map (\id -> filter (\g -> (fromJust $ getGroupBranchId g) == id)
590 $ getGroupsInBranches p)
594 -- | To get the sublist of all the PhyloGroups linked to a branch
595 getGroupsInBranches :: Phylo -> [PhyloGroup]
596 getGroupsInBranches p = filter (\g -> isJust $ g ^. phylo_groupBranchId)
600 --------------------------------
601 -- | PhyloQuery & QueryView | --
602 --------------------------------
605 -- | To filter PhyloView's Branches by level
606 filterBranchesByLevel :: Level -> PhyloView -> [PhyloBranch]
607 filterBranchesByLevel lvl pv = filter (\pb -> lvl == (fst $ pb ^. pb_id))
611 -- | To filter PhyloView's Edges by level
612 filterEdgesByLevel :: Level -> [PhyloEdge] -> [PhyloEdge]
613 filterEdgesByLevel lvl pes = filter (\pe -> (lvl == ((snd . fst) $ pe ^. pe_source))
614 && (lvl == ((snd . fst) $ pe ^. pe_target))) pes
617 -- | To filter PhyloView's Edges by type
618 filterEdgesByType :: EdgeType -> [PhyloEdge] -> [PhyloEdge]
619 filterEdgesByType t pes = filter (\pe -> t == (pe ^. pe_type)) pes
622 -- | To filter PhyloView's Nodes by the oldest Period
623 filterNodesByFirstPeriod :: [PhyloNode] -> [PhyloNode]
624 filterNodesByFirstPeriod pns = filter (\pn -> fstPrd == ((fst . fst) $ pn ^. pn_id)) pns
626 --------------------------------------
627 fstPrd :: (Date,Date)
628 fstPrd = (head' "filterNodesByFirstPeriod")
630 $ map (\pn -> (fst . fst) $ pn ^. pn_id) pns
631 --------------------------------------
634 -- | To filter PhyloView's Nodes by Branch
635 filterNodesByBranch :: PhyloBranchId -> [PhyloNode] -> [PhyloNode]
636 filterNodesByBranch bId pns = filter (\pn -> if isJust $ pn ^. pn_bid
637 then if bId == (fromJust $ pn ^. pn_bid)
643 -- | To filter PhyloView's Nodes by level
644 filterNodesByLevel :: Level -> [PhyloNode] -> [PhyloNode]
645 filterNodesByLevel lvl pns = filter (\pn -> lvl == ((snd . fst) $ pn ^. pn_id)) pns
648 -- | To filter PhyloView's Nodes by Period
649 filterNodesByPeriod :: PhyloPeriodId -> [PhyloNode] -> [PhyloNode]
650 filterNodesByPeriod prd pns = filter (\pn -> prd == ((fst . fst) $ pn ^. pn_id)) pns
653 -- | To get the first clustering method to apply to get the contextual units of a Phylo
654 getContextualUnit :: PhyloQueryBuild -> Cluster
655 getContextualUnit q = q ^. q_contextualUnit
658 -- | To get the metrics to apply to contextual units
659 getContextualUnitMetrics :: PhyloQueryBuild -> [Metric]
660 getContextualUnitMetrics q = q ^. q_contextualUnitMetrics
663 -- | To get the filters to apply to contextual units
664 getContextualUnitFilters :: PhyloQueryBuild -> [Filter]
665 getContextualUnitFilters q = q ^. q_contextualUnitFilters
668 -- | To get the cluster methods to apply to the Nths levels of a Phylo
669 getNthCluster :: PhyloQueryBuild -> Cluster
670 getNthCluster q = q ^. q_nthCluster
673 -- | To get the Sup Level of a reconstruction of a Phylo from a PhyloQuery
674 getNthLevel :: PhyloQueryBuild -> Level
675 getNthLevel q = q ^. q_nthLevel
678 -- | To get the Grain of the PhyloPeriods from a PhyloQuery
679 getPeriodGrain :: PhyloQueryBuild -> Int
680 getPeriodGrain q = q ^. q_periodGrain
683 -- | To get the intertemporal matching strategy to apply to a Phylo from a PhyloQuery
684 getInterTemporalMatching :: PhyloQueryBuild -> Proximity
685 getInterTemporalMatching q = q ^. q_interTemporalMatching
688 -- | To get the Steps of the PhyloPeriods from a PhyloQuery
689 getPeriodSteps :: PhyloQueryBuild -> Int
690 getPeriodSteps q = q ^. q_periodSteps
693 --------------------------------------------------
694 -- | PhyloQueryBuild & PhyloQueryView Constructors | --
695 --------------------------------------------------
697 -- | To get the threshold of a Proximity
698 getThreshold :: Proximity -> Double
699 getThreshold prox = case prox of
700 WeightedLogJaccard (WLJParams thr _) -> thr
701 Hamming (HammingParams thr) -> thr
702 Filiation -> panic "[ERR][Viz.Phylo.Tools.getThreshold] Filiation"
705 -- | To get the Proximity associated to a given Clustering method
706 getProximity :: Cluster -> Proximity
707 getProximity cluster = case cluster of
708 Louvain (LouvainParams proxi) -> proxi
709 RelatedComponents (RCParams proxi) -> proxi
710 _ -> panic "[ERR][Viz.Phylo.Tools.getProximity] this cluster has no associated Proximity"
713 -- | To initialize all the Cluster / Proximity with their default parameters
714 initFis :: Maybe Bool -> Maybe Support -> Maybe Int -> FisParams
715 initFis (def True -> kmf) (def 1 -> min') (def 1 -> thr) = FisParams kmf min' thr
717 initHamming :: Maybe Double -> HammingParams
718 initHamming (def 0.01 -> sens) = HammingParams sens
720 initLonelyBranch :: Maybe Int -> Maybe Int -> Maybe Int -> LBParams
721 initLonelyBranch (def 2 -> periodsInf) (def 2 -> periodsSup) (def 1 -> minNodes) = LBParams periodsInf periodsSup minNodes
723 initSizeBranch :: Maybe Int -> SBParams
724 initSizeBranch (def 1 -> minSize) = SBParams minSize
726 initLonelyBranch' :: Maybe Int -> Maybe Int -> Maybe Int -> LBParams
727 initLonelyBranch' (def 0 -> periodsInf) (def 0 -> periodsSup) (def 1 -> minNodes) = LBParams periodsInf periodsSup minNodes
729 initLouvain :: Maybe Proximity -> LouvainParams
730 initLouvain (def defaultWeightedLogJaccard -> proxi) = LouvainParams proxi
732 initRelatedComponents :: Maybe Proximity -> RCParams
733 initRelatedComponents (def Filiation -> proxi) = RCParams proxi
735 initWeightedLogJaccard :: Maybe Double -> Maybe Double -> WLJParams
736 initWeightedLogJaccard (def 0 -> thr) (def 0.01 -> sens) = WLJParams thr sens
739 -- | To initialize a PhyloQueryBuild from given and default parameters
740 initPhyloQueryBuild :: Text -> Text -> Maybe Int -> Maybe Int -> Maybe Cluster -> Maybe [Metric] -> Maybe [Filter] -> Maybe Proximity -> Maybe Level -> Maybe Cluster -> PhyloQueryBuild
741 initPhyloQueryBuild name desc (def 5 -> grain) (def 3 -> steps) (def defaultFis -> cluster) (def [] -> metrics) (def [] -> filters)
742 (def defaultWeightedLogJaccard -> matching') (def 2 -> nthLevel) (def defaultRelatedComponents -> nthCluster) =
743 PhyloQueryBuild name desc grain steps cluster metrics filters matching' nthLevel nthCluster
747 -- | To initialize a PhyloQueryView default parameters
748 initPhyloQueryView :: Maybe Level -> Maybe Filiation -> Maybe Bool -> Maybe Level -> Maybe [Metric] -> Maybe [Filter] -> Maybe [Tagger] -> Maybe (Sort, Order) -> Maybe ExportMode -> Maybe DisplayMode -> Maybe Bool -> PhyloQueryView
749 initPhyloQueryView (def 2 -> lvl) (def Descendant -> f) (def False -> c) (def 1 -> d) (def [] -> ms) (def [] -> fs) (def [] -> ts) s (def Json -> em) (def Flat -> dm) (def True -> v) =
750 PhyloQueryView lvl f c d ms fs ts s em dm v
753 -- | To define some obvious boolean getters
754 shouldKeepMinorFis :: FisParams -> Bool
755 shouldKeepMinorFis = _fis_keepMinorFis
757 ----------------------------
758 -- | Default ressources | --
759 ----------------------------
763 defaultFis :: Cluster
764 defaultFis = Fis (initFis Nothing Nothing Nothing)
766 defaultLouvain :: Cluster
767 defaultLouvain = Louvain (initLouvain Nothing)
769 defaultRelatedComponents :: Cluster
770 defaultRelatedComponents = RelatedComponents (initRelatedComponents Nothing)
774 defaultLonelyBranch :: Filter
775 defaultLonelyBranch = LonelyBranch (initLonelyBranch Nothing Nothing Nothing)
777 defaultSizeBranch :: Filter
778 defaultSizeBranch = SizeBranch (initSizeBranch Nothing)
782 defaultPhyloParam :: PhyloParam
783 defaultPhyloParam = initPhyloParam Nothing Nothing Nothing
787 defaultHamming :: Proximity
788 defaultHamming = Hamming (initHamming Nothing)
790 defaultWeightedLogJaccard :: Proximity
791 defaultWeightedLogJaccard = WeightedLogJaccard (initWeightedLogJaccard Nothing Nothing)
795 defaultQueryBuild :: PhyloQueryBuild
796 defaultQueryBuild = initPhyloQueryBuild "Cesar et Cleôpatre" "An example of Phylomemy (french without accent)"
797 Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
799 defaultQueryView :: PhyloQueryView
800 defaultQueryView = initPhyloQueryView Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing Nothing
804 defaultSoftware :: Software
805 defaultSoftware = Software "Gargantext" "v4"
809 defaultPhyloVersion :: Text
810 defaultPhyloVersion = "v1"