/// <summary> /// Writes a PhylogeneticTree to the writer. /// </summary> /// <param name="stream">The Stream used to write the formatted Phylogenetic tree text, it will remain open.</param> /// <param name="tree">PhylogeneticTree to format.</param> public void Format(Stream stream, Tree tree) { if (tree == null) { throw new ArgumentNullException(string.Format (CultureInfo.CurrentCulture, Resource.IOFormatErrorMessage, Name, "Tree object is null")); } if (stream == null) { throw new ArgumentNullException("stream"); } var stringBuilder = new StringBuilder(); this.Write(tree.Root, null, ref stringBuilder); // Append end char ";" stringBuilder.Append(";"); using (var writer = stream.OpenWrite()) { writer.Write(stringBuilder.ToString()); writer.Flush(); } }
/// <summary> /// Writes a single data element to the formatter. /// </summary> /// <param name="formatter">Formatter</param> /// <param name="data">Tree Data</param> public static void Format(this IPhylogeneticTreeFormatter formatter, Tree data) { var fs = ParserFormatterExtensions<IPhylogeneticTreeFormatter>.GetOpenStream(formatter, true); if (fs != null) formatter.Format(fs, data); else throw new Exception("You must open a formatter before calling Write."); }
/// <summary> /// Writes a single sequence to the formatter. /// </summary> /// <param name="formatter">Formatter</param> /// <param name="data">Tree data</param> /// <param name="filename">Filename</param> public static void Format(this IPhylogeneticTreeFormatter formatter, Tree data, string filename) { if (formatter == null) { throw new ArgumentNullException("formatter"); } if (data == null) { throw new ArgumentNullException("data"); } if (string.IsNullOrWhiteSpace(filename)) { throw new ArgumentNullException("filename"); } using (var fs = File.Create(filename)) formatter.Format(fs, data); }
/// <summary> /// Internal parse method used to parse out the data from a low-level TextReader. /// </summary> /// <param name="reader">Reader</param> /// <returns>Parsed Tree</returns> protected Tree InternalParse(TextReader reader) { string treeName = TreeName; if (string.IsNullOrWhiteSpace(treeName)) treeName = DefaultTreeName; Tree tree = new Tree { Name = treeName }; Node rootNode = GetNode(reader, true).Key; if (Peek(reader) != ';') { if (Peek(reader) == ':') { ReadChar(reader); } } // move to next char after ";" char semicolon = ReadChar(reader); if (semicolon != ';') { throw new FormatException(string.Format (CultureInfo.CurrentCulture, Resource.IOFormatErrorMessage, Name, "Missing [;] at the end of tree text")); } tree.Root = rootNode; return tree; }
/// <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; }