Exemplo n.º 1
0
        public void NewickStringTest()
        {
            StringBuilder input = new StringBuilder("(A:0.1,B:0.2,(C:0.3,D:0.4):0.5);");

            // parse
            Perf.Start("Parsing...");
            using (NewickParser parser = new NewickParser())
            {
                Tree phylogeneticTree = parser.Parse(input);

                Perf.End();

                Perf.Start("Formatting...");
                NewickFormatter formatter = new NewickFormatter();

                string output = formatter.FormatString(phylogeneticTree);
                Perf.End();
                Assert.AreEqual(input.ToString(), output);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Phylogenetic Tree Formatter General Tests
        /// </summary>
        /// <param name="nodeName">Xml node Name.</param>
        /// <param name="formatParam">Additional parameter</param>
        void PhylogeneticTreeFormatterGeneralTests(string nodeName,
                                                   FormatterParameters formatParam)
        {
            // Gets the expected sequence from the Xml
            string filePath = string.Empty;

            using (NewickParser parser = new NewickParser())
            {
                Tree rootTree = null;

                switch (formatParam)
                {
                case FormatterParameters.Object:
                    rootTree = GetFormattedObject(false);
                    break;

                case FormatterParameters.ObjectSingleNode:
                    rootTree = GetFormattedObject(true);
                    break;

                case FormatterParameters.ParseTextReader:
                    filePath = _utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                Constants.FilePathNode);
                    Assert.IsTrue(File.Exists(filePath));

                    // Logs information to the log file
                    ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                           "Phylogenetic Tree Formatter P1: File Exists in the Path '{0}'.",
                                                           filePath));
                    using (StreamReader reader = File.OpenText(filePath))
                    {
                        rootTree = parser.Parse(reader);
                    }
                    break;

                case FormatterParameters.ParseStringBuilder:
                    filePath = _utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                Constants.FilePathNode);
                    Assert.IsTrue(File.Exists(filePath));

                    // Logs information to the log file
                    ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                           "Phylogenetic Tree Formatter P1: File Exists in the Path '{0}'.",
                                                           filePath));

                    using (StreamReader reader = File.OpenText(filePath))
                    {
                        StringBuilder strBuilderObj =
                            new StringBuilder(reader.ReadToEnd());
                        rootTree = parser.Parse(strBuilderObj);
                    }
                    break;

                default:
                    filePath = _utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                Constants.FilePathNode);
                    Assert.IsTrue(File.Exists(filePath));

                    // Logs information to the log file
                    ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                           "Phylogenetic Tree Formatter P1: File Exists in the Path '{0}'.",
                                                           filePath));

                    rootTree = parser.Parse(filePath);
                    break;
                }

                string outputFilepath = _utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                         Constants.OutputFilePathNode);

                NewickFormatter format = new NewickFormatter();
                switch (formatParam)
                {
                case FormatterParameters.TextWriter:
                    using (TextWriter writer = new StreamWriter(outputFilepath))
                    {
                        format.Format(rootTree, writer);
                    }
                    break;

                case FormatterParameters.FormatString:
                    // Validate format String
                    string formatString = format.FormatString(rootTree);

                    string expectedFormatString = _utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                   Constants.FormatStringNode);

                    Assert.AreEqual(expectedFormatString, formatString);

                    ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                           "Phylogenetic Tree Formatter P1: Format string '{0}' is as expected.",
                                                           formatString));
                    Console.WriteLine(string.Format((IFormatProvider)null,
                                                    "Phylogenetic Tree Formatter P1: Format string '{0}' is as expected.",
                                                    formatString));
                    break;

                default:
                    format.Format(rootTree, outputFilepath);
                    break;
                }

                // Validate only if not a Format String
                if (FormatterParameters.FormatString != formatParam)
                {
                    // Re-parse the created file and validate the tree.
                    using (NewickParser newparserObj = new NewickParser())
                    {
                        Tree newrootTreeObj = null;

                        Assert.IsTrue(File.Exists(outputFilepath));

                        // Logs information to the log file
                        ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                               "Phylogenetic Tree Formatter P1: New File Exists in the Path '{0}'.",
                                                               outputFilepath));

                        newrootTreeObj = newparserObj.Parse(outputFilepath);

                        Node rootNode = newrootTreeObj.Root;

                        string rootBranchCount = _utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                  Constants.RootBranchCountNode);

                        // Validate the root branch count
                        Assert.AreEqual(rootBranchCount,
                                        rootNode.Children.Count.ToString((IFormatProvider)null));

                        ApplicationLog.WriteLine(string.Format((IFormatProvider)null,
                                                               "Phylogenetic Tree Formatter P1: Number of Root Branches found are '{0}'.",
                                                               rootNode.Children.Count.ToString((IFormatProvider)null)));

                        List <string> leavesName     = new List <string>();
                        List <double> leavesDistance = new List <double>();

                        // Gets all the leaves in the root node list
                        GetAllNodesAndEdges(rootNode, ref leavesName,
                                            ref leavesDistance);

                        string[] expectedLeavesName = _utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                       Constants.NodeNamesNode).Split(',');

                        string[] expectedLeavesDistance = _utilityObj.xmlUtil.GetTextValue(nodeName,
                                                                                           Constants.EdgeDistancesNode).Split(',');

                        for (int i = 0; i < expectedLeavesName.Length; i++)
                        {
                            Assert.AreEqual(expectedLeavesName[i], leavesName[i]);
                        }

                        for (int i = 0; i < expectedLeavesDistance.Length; i++)
                        {
                            Assert.AreEqual(expectedLeavesDistance[i], leavesDistance[i].ToString((IFormatProvider)null));
                        }

                        ApplicationLog.WriteLine(
                            "Phylogenetic Tree Parser P1: The Node Names and Distance are as expected.");
                        // Logs to the NUnit GUI (Console.Out) window
                        Console.WriteLine(
                            "Phylogenetic Tree Parser P1: The Node Names and Distance are as expected.");
                    }

                    if (File.Exists(outputFilepath))
                    {
                        File.Delete(outputFilepath);
                    }
                }
            }
        }