Exemple #1
0
        /// <summary>
        /// Transform the tree into a collection of (undirected) splits.
        /// </summary>
        /// <param name="lengthType">Determines whether the <see cref="Split.Length"/> should represent ages or branch lenghts.</param>
        /// <returns>A list of splits induced by the tree. Each split corresponds to a branch in the tree.</returns>
        internal List <Split> GetSplits(Split.LengthTypes lengthType)
        {
            List <Split> tbr = new List <Split>();

            List <TreeNode> nodes = this.GetChildrenRecursive();

            if (this.Children.Count == 2)
            {
                for (int i = 0; i < nodes.Count; i++)
                {
                    List <string> nodeLeaves = nodes[i].GetLeafNames();
                    nodeLeaves.Sort();

                    tbr.Add(new Split(nodeLeaves.Aggregate((a, b) => a + "," + b), lengthType == Split.LengthTypes.Length ? nodes[i].Length : nodes[i].LongestDownstreamLength(), lengthType, 1));
                }
            }
            else
            {
                List <string> allLeaves = this.GetLeafNames();

                for (int i = 0; i < nodes.Count; i++)
                {
                    List <string> nodeLeaves = nodes[i].GetLeafNames();

                    List <string> diffLeaves = (from el in allLeaves where !nodeLeaves.Contains(el) select el).ToList();

                    nodeLeaves.Sort();
                    diffLeaves.Sort();

                    if (diffLeaves.Count > 0)
                    {
                        List <string> splitTerminals = new List <string>()
                        {
                            nodeLeaves.Aggregate((a, b) => a + "," + b), diffLeaves.Aggregate((a, b) => a + "," + b)
                        };
                        splitTerminals.Sort();

                        tbr.Add(new Split(splitTerminals.Aggregate((a, b) => a + "|" + b), lengthType == Split.LengthTypes.Length ? nodes[i].Length : (nodes[i].LongestDownstreamLength() + nodes[i].Length), lengthType, 1));
                    }
                    else
                    {
                        tbr.Add(new Split(nodeLeaves.Aggregate((a, b) => a + "," + b), lengthType == Split.LengthTypes.Length ? nodes[i].Length : nodes[i].LongestDownstreamLength(), lengthType, 1));
                    }
                }
            }
            return(tbr);
        }
Exemple #2
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));
        }