Beispiel #1
0
 /// <summary>
 /// Create a new node from the initial node, recursively changing things.
 /// </summary>
 /// <param name="initialNode">The node found from the newick parser</param>
 public SelectomeTreeNode(Node initialNode)
 {
     if (initialNode == null)
     {
         throw new NullReferenceException("initialNode");
     }
     this.MetaData = initialNode.MetaData;
     this.children = new Dictionary<SelectomeTreeNode, Edge>();
     this.Name = initialNode.Name;
     //recursively update child nodes
     foreach (var child in initialNode.Children)
     {
         this.children.Add(new SelectomeTreeNode(child.Key), child.Value);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Gets the Branch node
        /// </summary>
        /// <param name="reader">TextReader we are working with</param>
        /// <param name="isRoot"></param>
        /// <returns>Branch object</returns>
        private Node GetBranch(TextReader reader, bool isRoot)
        {
            Node branch = new Node { IsRoot = isRoot };

            bool firstBranch = true;
            while (true)
            {
                char peek = Peek(reader);
                if (!firstBranch && peek == ')')
                {
                    break;
                }

                char c = ReadChar(reader);
                if (firstBranch)
                {
                    if (c != '(')
                    {
                        throw new FormatException(string.Format
                            (CultureInfo.CurrentCulture, Resource.IOFormatErrorMessage, Name,
                            "Missing [(] as a first branch"));
                    }
                    firstBranch = false;
                }
                else
                {
                    if (c != ',')
                    {
                        throw new FormatException(string.Format
                            (CultureInfo.CurrentCulture, Resource.IOFormatErrorMessage, Name,
                            "Missing [,] in between branches"));
                    }
                }

                var tmp = GetNode(reader, false);
                branch.Children.Add(tmp.Key, tmp.Value);
            }

            //move to next char of ")"
            char nextCloseChar = ReadChar(reader);
            if (nextCloseChar != ')')
            {
                throw new FormatException(string.Format
                    (CultureInfo.CurrentCulture, Resource.IOFormatErrorMessage, Name,
                    "Missing [)] branch is not closed"));
            }

            return branch;
        }
        /// <summary>
        /// Gets the formatted object which is used for formatting
        /// </summary>
        /// <returns>Formatter tree.</returns>
        private static Tree GetFormattedObject()
        {
            // Tree with three nodes are created
            Node nd1 = new Node();
            nd1.Name = "a";
            Edge ed1 = new Edge();
            ed1.Distance = 0.1;

            Node nd2 = new Node();
            nd2.Name = "b";
            Edge ed2 = new Edge();
            ed2.Distance = 0.2;

            Node nd3 = new Node();
            nd3.Name = "c";
            Edge ed3 = new Edge();
            ed3.Distance = 0.3;

            Node nd4 = new Node();
            nd4.Children.Add(nd1, ed1);
            Edge ed4 = new Edge();
            ed4.Distance = 0.4;

            Node nd5 = new Node();
            nd5.Children.Add(nd2, ed2);
            nd5.Children.Add(nd3, ed3);
            Edge ed5 = new Edge();
            ed5.Distance = 0.5;

            Node ndRoot = new Node();
            ndRoot.Children.Add(nd4, ed4);
            ndRoot.Children.Add(nd5, ed5);

            // All the Node and Edges are combined to form a tree
            Tree baseTree = new Tree();
            baseTree.Root = ndRoot;

            return baseTree;
        }
Beispiel #4
0
        /// <summary>
        /// Recursive method to get each node into string
        /// </summary>
        /// <param name="node">tree node</param>
        /// <param name="edge">edge</param>
        /// <param name="stringBuilder">output newick string</param>
        void Write(Node node, Edge edge, ref StringBuilder stringBuilder)
        {
            if (node.IsLeaf)
            {
                stringBuilder.Append(string.Format("{0}:{1}", node.Name, edge.Distance));
            }
            else
            {
                
                stringBuilder.Append("(");
                bool firstNode = true;
                foreach (KeyValuePair<Node, Edge> child in node.Children)
                {
                    Edge localEdge = child.Value;
                    Node localNode = child.Key;

                    if (localNode == node)
                    {
                        throw new ArgumentException(string.Format
                            (CultureInfo.CurrentCulture, Resource.IOFormatErrorMessage, Name,
                            "Tree object has more than one root"));
                    }
                    if (!firstNode)
                    {
                        stringBuilder.Append(",");
                    }
                    this.Write(localNode, localEdge, ref stringBuilder);
                    firstNode = false;
                }

                //last node
                stringBuilder.Append(null == edge ? ")" : string.Format("):{0}", edge.Distance));
            }
        }
        /// <summary>
        /// Gets the leaves for the entire phylogenetic tree.
        /// </summary>
        /// <param name="nodeList">Phylogenetic tree node list.</param>
        /// <param name="nodeName">Node names</param>
        /// <param name="distance">edge distances</param>
        void GetAllNodesAndEdges(Node rootNode,
              ref List<string> nodeName, ref List<double> distance)
        {
            IList<Edge> edges = rootNode.Edges;

            if (null != edges)
            {
                // Get all the edges distances
                foreach (Edge ed in edges)
                {
                    distance.Add(ed.Distance);
                }
            }

            // Gets all the nodes
            IList<Node> nodes = rootNode.Nodes;

            if (null != nodes)
            {
                foreach (Node nd in nodes)
                {
                    if (nd.IsLeaf)
                    {
                        nodeName.Add(nd.Name);
                    }
                    else
                    {
                        // Get the nodes and edges recursively until 
                        // all the nodes and edges are retrieved.
                        GetAllNodesAndEdges(nd, ref nodeName, ref distance);
                    }
                }
            }
        }