Ejemplo n.º 1
0
        /// <summary>
        /// Constructs a consensus tree.
        /// </summary>
        /// <param name="trees">The collection of trees whose consensus is to be computed.</param>
        /// <param name="rooted">Whether the consensus tree should be rooted or not.</param>
        /// <param name="clockLike">Whether the trees are to be treated as clock-like trees or not. This has an effect on how the branch lengths of the consensus tree are computed.</param>
        /// <param name="threshold">The (inclusive) threshold for splits to be included in the consensus tree. Use <c>0</c> to get all compatible splits, <c>0.5</c> for a majority-rule consensus or <c>1</c> for a strict consensus.</param>
        /// <param name="useMedian">If this is <c>true</c>, the lengths of the branches in the tree will be computed based on the median length/age of the splits used to build the tree. Otherwise, the mean will be used.</param>
        /// <returns>A rooted consensus tree.</returns>
        public static TreeNode GetConsensus(this IEnumerable <TreeNode> trees, bool rooted, bool clockLike, double threshold, bool useMedian)
        {
            Contract.Requires(trees != null);

            Dictionary <string, List <double> > splits = new Dictionary <string, List <double> >();

            int totalTrees = 0;

            Split.LengthTypes lengthType = clockLike ? Split.LengthTypes.Age : Split.LengthTypes.Length;

            foreach (TreeNode tree in trees)
            {
                List <Split> treeSplits = tree.GetSplits(lengthType);

                for (int i = 0; i < treeSplits.Count; i++)
                {
                    if (splits.TryGetValue(treeSplits[i].Name, out List <double> splitLengths))
                    {
                        splits[treeSplits[i].Name].Add(treeSplits[i].Length);
                    }
                    else
                    {
                        splits.Add(treeSplits[i].Name, new List <double>()
                        {
                            treeSplits[i].Length
                        });
                    }
                }

                totalTrees++;
            }

            List <Split> orderedSplits = new List <Split>(from el in splits orderby el.Value.Count descending where ((double)el.Value.Count / (double)totalTrees) >= threshold select new Split(el.Key, (useMedian ? el.Value.Median() : el.Value.Average()), lengthType, ((double)el.Value.Count / (double)totalTrees)));

            List <Split> finalSplits = new List <Split>();

            for (int i = 0; i < orderedSplits.Count; i++)
            {
                if (orderedSplits[i].IsCompatible(finalSplits))
                {
                    finalSplits.Add(orderedSplits[i]);
                }
            }

            return(Split.BuildTree(finalSplits, rooted));
        }