JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / jalview / io / NewickFile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 // NewickFile.java
22 // Tree I/O
23 // http://evolution.genetics.washington.edu/phylip/newick_doc.html
24 // TODO: Implement Basic NHX tag parsing and preservation
25 // TODO: http://evolution.genetics.wustl.edu/eddy/forester/NHX.html
26 // TODO: Extended SequenceNodeI to hold parsed NHX strings
27 package jalview.io;
28
29 import jalview.datamodel.SequenceNode;
30 import jalview.jsdev.RegExp;
31 import jalview.jsdev.api.RegExpInterface;
32 import jalview.util.MessageManager;
33
34 import java.io.BufferedReader;
35 import java.io.File;
36 import java.io.FileReader;
37 import java.io.IOException;
38 import java.util.StringTokenizer;
39
40 import javajs.J2SIgnoreImport;
41
42 //import com.stevesoft.pat.Regex;
43
44 /**
45  * Parse a new hanpshire style tree Caveats: NHX files are NOT supported and the
46  * tree distances and topology are unreliable when they are parsed. TODO: on
47  * this: NHX codes are appended in comments beginning with &&NHX. The codes are
48  * given below (from http://www.phylosoft.org/forester/NHX.html): Element Type
49  * Description Corresponding phyloXML element (parent element in parentheses) no
50  * tag string name of this node/clade (MUST BE FIRST, IF ASSIGNED)
51  * <name>(<clade>) : decimal branch length to parent node (MUST BE SECOND, IF
52  * ASSIGNED) <branch_length>(<clade>) :GN= string gene name <name>(<sequence>)
53  * :AC= string sequence accession <accession>(<sequence>) :ND= string node
54  * identifier - if this is being used, it has to be unique within each phylogeny
55  * <node_id>(<clade>) :B= decimal confidence value for parent branch
56  * <confidence>(<clade>) :D= 'T', 'F', or '?' 'T' if this node represents a
57  * duplication event - 'F' if this node represents a speciation event, '?' if
58  * this node represents an unknown event (D= tag should be replaced by Ev= tag)
59  * n/a :Ev=duplications>speciations>gene losses>event type>duplication type int
60  * int int string string event (replaces the =D tag), number of duplication,
61  * speciation, and gene loss events, type of event (transfer, fusion, root,
62  * unknown, other, speciation_duplication_loss, unassigned) <events>(<clade>)
63  * :E= string EC number at this node <annotation>(<sequence>) :Fu= string
64  * function at this node <annotation>(<sequence>)
65  * :DS=protein-length>from>to>support>name>from>... int int int double string
66  * int ... domain structure at this node <domain_architecture>(<sequence>) :S=
67  * string species name of the species/phylum at this node <taxonomy>(<clade>)
68  * :T= integer taxonomy ID of the species/phylum at this node <id>(<taxonomy>)
69  * :W= integer width of parent branch <width>(<clade>) :C=rrr.ggg.bbb
70  * integer.integer.integer color of parent branch <color>(<clade>) :Co= 'Y' or
71  * 'N' collapse this node when drawing the tree (default is not to collapse) n/a
72  * :XB= string custom data associated with a branch <property>(<clade>) :XN=
73  * string custom data associated with a node <property>(<clade>) :O= integer
74  * orthologous to this external node n/a :SN= integer subtree neighbors n/a :SO=
75  * integer super orthologous (no duplications on paths) to this external node
76  * n/a
77  * 
78  * @author Jim Procter
79  * @version $Revision$
80  */
81 @J2SIgnoreImport({java.io.File.class, java.io.FileReader.class})
82 public class NewickFile extends FileParse
83 {
84   SequenceNode root;
85
86   private boolean HasBootstrap = false;
87
88   private boolean HasDistances = false;
89
90   private boolean RootHasDistance = false;
91
92   // File IO Flags
93   boolean ReplaceUnderscores = false;
94
95   boolean printRootInfo = true;
96
97   private RegExpInterface[] NodeSafeName = new RegExpInterface[]
98   { RegExp.perlCode("m/[\\[,:'()]/"), // test for
99       // requiring
100       // quotes
101       RegExp.perlCode("s/'/''/"), // escaping quote
102       // characters
103       RegExp.perlCode("s/\\/w/_/") // unqoted whitespace
104   // transformation
105   };
106
107   char QuoteChar = '\'';
108
109   /**
110    * Creates a new NewickFile object.
111    * 
112    * @param inStr
113    *          DOCUMENT ME!
114    * 
115    * @throws IOException
116    *           DOCUMENT ME!
117    */
118   public NewickFile(String inStr) throws IOException
119   {
120     super(inStr, "Paste");
121   }
122
123   /**
124    * Creates a new NewickFile object.
125    * 
126    * @param inFile
127    *          DOCUMENT ME!
128    * @param type
129    *          DOCUMENT ME!
130    * 
131    * @throws IOException
132    *           DOCUMENT ME!
133    */
134   public NewickFile(String inFile, String type) throws IOException
135   {
136     super(inFile, type);
137   }
138
139   public NewickFile(FileParse source) throws IOException
140   {
141     super(source);
142   }
143
144   /**
145    * Creates a new NewickFile object.
146    * 
147    * @param newtree
148    *          DOCUMENT ME!
149    */
150   public NewickFile(SequenceNode newtree)
151   {
152     root = newtree;
153   }
154
155   /**
156    * Creates a new NewickFile object.
157    * 
158    * @param newtree
159    *          DOCUMENT ME!
160    * @param bootstrap
161    *          DOCUMENT ME!
162    */
163   public NewickFile(SequenceNode newtree, boolean bootstrap)
164   {
165     HasBootstrap = bootstrap;
166     root = newtree;
167   }
168
169   /**
170    * Creates a new NewickFile object.
171    * 
172    * @param newtree
173    *          DOCUMENT ME!
174    * @param bootstrap
175    *          DOCUMENT ME!
176    * @param distances
177    *          DOCUMENT ME!
178    */
179   public NewickFile(SequenceNode newtree, boolean bootstrap,
180           boolean distances)
181   {
182     root = newtree;
183     HasBootstrap = bootstrap;
184     HasDistances = distances;
185   }
186
187   /**
188    * Creates a new NewickFile object.
189    * 
190    * @param newtree
191    *          DOCUMENT ME!
192    * @param bootstrap
193    *          DOCUMENT ME!
194    * @param distances
195    *          DOCUMENT ME!
196    * @param rootdistance
197    *          DOCUMENT ME!
198    */
199   public NewickFile(SequenceNode newtree, boolean bootstrap,
200           boolean distances, boolean rootdistance)
201   {
202     root = newtree;
203     HasBootstrap = bootstrap;
204     HasDistances = distances;
205     RootHasDistance = rootdistance;
206   }
207
208   /**
209    * DOCUMENT ME!
210    * 
211    * @param Error
212    *          DOCUMENT ME!
213    * @param Er
214    *          DOCUMENT ME!
215    * @param r
216    *          DOCUMENT ME!
217    * @param p
218    *          DOCUMENT ME!
219    * @param s
220    *          DOCUMENT ME!
221    * 
222    * @return DOCUMENT ME!
223    */
224   private String ErrorStringrange(String Error, String Er, int r, int p,
225           String s)
226   {
227     return ((Error == null) ? "" : Error)
228             + Er
229             + " at position "
230             + p
231             + " ( "
232             + s.substring(((p - r) < 0) ? 0 : (p - r),
233                     ((p + r) > s.length()) ? s.length() : (p + r)) + " )\n";
234   }
235
236   // @tree annotations
237   // These are set automatically by the reader
238   public boolean HasBootstrap()
239   {
240     return HasBootstrap;
241   }
242
243   /**
244    * DOCUMENT ME!
245    * 
246    * @return DOCUMENT ME!
247    */
248   public boolean HasDistances()
249   {
250     return HasDistances;
251   }
252
253   public boolean HasRootDistance()
254   {
255     return RootHasDistance;
256   }
257
258         /**
259          * parse the filesource as a newick file (new hampshire and/or extended)
260          * 
261          * @throws IOException
262          *           with a line number and character position for badly formatted NH
263          *           strings
264          */
265         public void parse() throws IOException {
266                 String nf;
267
268                 { // fill nf with complete tree file
269
270                         StringBuffer file = new StringBuffer();
271
272                         while ((nf = nextLine()) != null) {
273                                 file.append(nf);
274                         }
275
276                         nf = file.toString();
277                 }
278
279                 root = new SequenceNode();
280
281                 SequenceNode realroot = null;
282                 SequenceNode c = root;
283
284                 int d = -1;
285                 int cp = 0;
286                 // int flen = nf.length();
287
288                 String Error = null;
289                 String nodename = null;
290                 String commentString2 = null; // comments after simple node props
291
292                 float DefDistance = (float) 0.001; // @param Default distance for a node -
293                 // very very small
294                 int DefBootstrap = -1; // @param Default bootstrap for a node
295
296                 float distance = DefDistance;
297                 int bootstrap = DefBootstrap;
298
299                 boolean ascending = false; // flag indicating that we are leaving the
300                 // current node
301
302                 RegExpInterface majorsyms = RegExp.newRegex("[(\\['),;]");
303
304                 int nextcp = 0;
305                 int ncp = cp;
306                 boolean parsednodename = false;
307                 while (majorsyms.searchFrom(nf, cp) && (Error == null)) {
308                         int fcp = majorsyms.matchedFrom();
309                         char schar;
310                         switch (schar = nf.charAt(fcp)) {
311                         case '(':
312
313                                 // ascending should not be set
314                                 // New Internal node
315                                 if (ascending) {
316                                         Error = ErrorStringrange(Error, "Unexpected '('", 7, fcp, nf);
317
318                                         continue;
319                                 }
320
321                                 ;
322                                 d++;
323
324                                 if (c.right() == null) {
325                                         c.setRight(new SequenceNode(null, c, null, DefDistance, DefBootstrap,
326                                                         false));
327                                         c = (SequenceNode) c.right();
328                                 } else {
329                                         if (c.left() != null) {
330                                                 // Dummy node for polytomy - keeps c.left free for new node
331                                                 SequenceNode tmpn = new SequenceNode(null, c, null, 0, 0, true);
332                                                 tmpn.SetChildren(c.left(), c.right());
333                                                 c.setRight(tmpn);
334                                         }
335
336                                         c.setLeft(new SequenceNode(null, c, null, DefDistance, DefBootstrap,
337                                                         false));
338                                         c = (SequenceNode) c.left();
339                                 }
340
341                                 if (realroot == null) {
342                                         realroot = c;
343                                 }
344
345                                 nodename = null;
346                                 distance = DefDistance;
347                                 bootstrap = DefBootstrap;
348                                 cp = fcp + 1;
349
350                                 break;
351
352                         // Deal with quoted fields
353                         case '\'':
354
355                                 RegExpInterface qnodename = RegExp.newRegex("'([^']|'')+'");
356
357                                 if (qnodename.searchFrom(nf, fcp)) {
358                                         int nl = qnodename.stringMatched().length();
359                                         nodename = new String(qnodename.stringMatched().substring(1, nl - 1));
360                                         // unpack any escaped colons
361                                         RegExpInterface xpandquotes = RegExp.perlCode("s/''/'/");
362                                         String widernodename = xpandquotes.replaceAll(nodename);
363                                         nodename = widernodename;
364                                         // jump to after end of quoted nodename
365                                         nextcp = fcp + nl + 1;
366                                         parsednodename = true;
367                                 } else {
368                                         Error = ErrorStringrange(Error, "Unterminated quotes for nodename",
369                                                         7, fcp, nf);
370                                 }
371
372                                 break;
373
374                         default:
375                                 if (schar == ';') {
376                                         if (d != -1) {
377                                                 Error = ErrorStringrange(Error, "Wayward semicolon (depth=" + d
378                                                                 + ")", 7, fcp, nf);
379                                         }
380                                         // cp advanced at the end of default
381                                 }
382                                 if (schar == '[') {
383                                         // node string contains Comment or structured/extended NH format info
384                                         /*
385                                          * if ((fcp-cp>1 && nf.substring(cp,fcp).trim().length()>1)) { // will
386                                          * process in remains System.err.println("skipped text:
387                                          * '"+nf.substring(cp,fcp)+"'"); }
388                                          */
389                                         // verify termination.
390                                         RegExpInterface comment = RegExp.newRegex("]");
391                                         if (comment.searchFrom(nf, fcp)) {
392                                                 // Skip the comment field
393                                                 nextcp = comment.matchedFrom() + 1;
394                                                 warningMessage = "Tree file contained comments which may confuse input algorithm.";
395                                                 break;
396
397                                                 // cp advanced at the end of default to nextcp, ncp is unchanged so
398                                                 // any node info can be read.
399                                         } else {
400                                                 Error = ErrorStringrange(Error, "Unterminated comment", 3, fcp, nf);
401                                         }
402
403                                         ;
404                                 }
405                                 // Parse simpler field strings
406                                 String fstring = nf.substring(ncp, fcp);
407                                 // remove any comments before we parse the node info
408                                 // TODO: test newick file with quoted square brackets in node name (is
409                                 // this allowed?)
410                                 while (fstring.indexOf(']') > -1) {
411                                         int cstart = fstring.indexOf('[');
412                                         int cend = fstring.indexOf(']');
413                                         commentString2 = fstring.substring(cstart + 1, cend);
414                                         fstring = fstring.substring(0, cstart) + fstring.substring(cend + 1);
415
416                                 }
417                                 RegExpInterface uqnodename = RegExp.newRegex("\\b([^' :;\\](),]+)");
418                                 RegExpInterface nbootstrap = RegExp.newRegex("\\s*([0-9+]+)\\s*:");
419                                 RegExpInterface ndist = RegExp.newRegex(":([-0-9Ee.+]+)");
420
421                                 if (!parsednodename
422                                                 && uqnodename.search(fstring)
423                                                 && ((uqnodename.matchedFromI(1) == 0) || (fstring.charAt(uqnodename
424                                                                 .matchedFromI(1) - 1) != ':'))) // JBPNote
425                                 // HACK!
426                                 {
427                                         if (nodename == null) {
428                                                 if (ReplaceUnderscores) {
429                                                         nodename = uqnodename.stringMatchedI(1).replace('_', ' ');
430                                                 } else {
431                                                         nodename = uqnodename.stringMatchedI(1);
432                                                 }
433                                         } else {
434                                                 Error = ErrorStringrange(Error,
435                                                                 "File has broken algorithm - overwritten nodename", 10, fcp, nf);
436                                         }
437                                 }
438                                 // get comment bootstraps
439
440                                 if (nbootstrap.search(fstring)) {
441                                         if (nbootstrap.stringMatchedI(1).equals(uqnodename.stringMatchedI(1))) {
442                                                 nodename = null; // no nodename here.
443                                         }
444                                         if (nodename == null
445                                                         || nodename.length() == 0
446                                                         || nbootstrap.matchedFromI(1) > (uqnodename.matchedFromI(1) + uqnodename
447                                                                         .stringMatched().length())) {
448                                                 try {
449                                                         bootstrap = (new Integer(nbootstrap.stringMatchedI(1)))
450                                                                         .intValue();
451                                                         HasBootstrap = true;
452                                                 } catch (Exception e) {
453                                                         Error = ErrorStringrange(Error, "Can't parse bootstrap value", 4,
454                                                                         ncp + nbootstrap.matchedFrom(), nf);
455                                                 }
456                                         }
457                                 }
458
459                                 boolean nodehasdistance = false;
460
461                                 if (ndist.search(fstring)) {
462                                         try {
463                                                 distance = (new Float(ndist.stringMatchedI(1))).floatValue();
464                                                 HasDistances = true;
465                                                 nodehasdistance = true;
466                                         } catch (Exception e) {
467                                                 Error = ErrorStringrange(Error, "Can't parse node distance value",
468                                                                 7, ncp + ndist.matchedFrom(), nf);
469                                         }
470                                 }
471
472                                 if (ascending) {
473                                         // Write node info here
474                                         c.setName(nodename);
475                                         // Trees without distances still need a render distance
476                                         c.dist = (HasDistances) ? distance : DefDistance;
477                                         // be consistent for internal bootstrap defaults too
478                                         c.setBootstrap((HasBootstrap) ? bootstrap : DefBootstrap);
479                                         if (c == realroot) {
480                                                 RootHasDistance = nodehasdistance; // JBPNote This is really
481                                                 // UGLY!!! Ensure root node gets
482                                                 // its given distance
483                                         }
484                                         parseNHXNodeProps(c, commentString2);
485                                         commentString2 = null;
486                                 } else {
487                                         // Find a place to put the leaf
488                                         SequenceNode newnode = new SequenceNode(null, c, nodename,
489                                                         (HasDistances) ? distance : DefDistance,
490                                                         (HasBootstrap) ? bootstrap : DefBootstrap, false);
491                                         parseNHXNodeProps(c, commentString2);
492                                         commentString2 = null;
493
494                                         if (c.right() == null) {
495                                                 c.setRight(newnode);
496                                         } else {
497                                                 if (c.left() == null) {
498                                                         c.setLeft(newnode);
499                                                 } else {
500                                                         // Insert a dummy node for polytomy
501                                                         // dummy nodes have distances
502                                                         SequenceNode newdummy = new SequenceNode(null, c, null,
503                                                                         (HasDistances ? 0 : DefDistance), 0, true);
504                                                         newdummy.SetChildren(c.left(), newnode);
505                                                         c.setLeft(newdummy);
506                                                 }
507                                         }
508                                 }
509
510                                 if (ascending) {
511                                         // move back up the tree from preceding closure
512                                         c = c.AscendTree();
513
514                                         if ((d > -1) && (c == null)) {
515                                                 Error = ErrorStringrange(
516                                                                 Error,
517                                                                 "File broke algorithm: Lost place in tree (is there an extra ')' ?)",
518                                                                 7, fcp, nf);
519                                         }
520                                 }
521
522                                 if (nf.charAt(fcp) == ')') {
523                                         d--;
524                                         ascending = true;
525                                 } else {
526                                         if (nf.charAt(fcp) == ',') {
527                                                 if (ascending) {
528                                                         ascending = false;
529                                                 } else {
530                                                         // Just advance focus, if we need to
531                                                         if ((c.left() != null) && (!c.left().isLeaf())) {
532                                                                 c = (SequenceNode) c.left();
533                                                         }
534                                                 }
535                                         }
536                                 }
537
538                                 // Reset new node properties to obvious fakes
539                                 nodename = null;
540                                 distance = DefDistance;
541                                 bootstrap = DefBootstrap;
542                                 commentString2 = null;
543                                 parsednodename = false;
544                         }
545                         if (nextcp == 0) {
546                                 ncp = cp = fcp + 1;
547                         } else {
548                                 cp = nextcp;
549                                 nextcp = 0;
550                         }
551                 }
552
553                 if (Error != null) {
554                         throw (new IOException(MessageManager.formatMessage("exception.newfile",
555                                         new String[] { Error.toString() })));
556                 }
557                 if (root == null) {
558                         throw (new IOException(MessageManager.formatMessage("exception.newfile",
559                                         new String[] { MessageManager.getString("label.no_tree_read_in") })));
560                 }
561                 // THe next line is failing for topali trees - not sure why yet. if
562                 // (root.right()!=null && root.isDummy())
563                 root = (SequenceNode) root.right().detach(); // remove the imaginary root.
564
565                 if (!RootHasDistance) {
566                         root.dist = (HasDistances) ? 0 : DefDistance;
567                 }
568         }
569
570   /**
571    * parse NHX codes in comment strings and update NewickFile state flags for
572    * distances and bootstraps, and add any additional properties onto the node.
573    * 
574    * @param c
575    * @param commentString
576    * @param commentString2
577    */
578   private void parseNHXNodeProps(SequenceNode c, String commentString)
579   {
580     // TODO: store raw comment on the sequenceNode so it can be recovered when
581     // tree is output
582     if (commentString != null && commentString.startsWith("&&NHX"))
583     {
584       StringTokenizer st = new StringTokenizer(commentString.substring(5),
585               ":");
586       while (st.hasMoreTokens())
587       {
588         String tok = st.nextToken();
589         int colpos = tok.indexOf("=");
590
591         if (colpos > -1)
592         {
593           String code = tok.substring(0, colpos);
594           String value = tok.substring(colpos + 1);
595           try
596           {
597             // parse out code/value pairs
598             if (code.toLowerCase().equals("b"))
599             {
600               int v = -1;
601               Float iv = new Float(value);
602               v = iv.intValue(); // jalview only does integer bootstraps
603               // currently
604               c.setBootstrap(v);
605               HasBootstrap = true;
606             }
607             // more codes here.
608           } catch (Exception e)
609           {
610             System.err.println("Couldn't parse code '" + code + "' = '"
611                     + value + "'");
612             e.printStackTrace(System.err);
613           }
614         }
615       }
616     }
617
618   }
619
620   /**
621    * DOCUMENT ME!
622    * 
623    * @return DOCUMENT ME!
624    */
625   public SequenceNode getTree()
626   {
627     return root;
628   }
629
630   /**
631    * Generate a newick format tree according to internal flags for bootstraps,
632    * distances and root distances.
633    * 
634    * @return new hampshire tree in a single line
635    */
636   public String print()
637   {
638     synchronized (this)
639     {
640       StringBuffer tf = new StringBuffer();
641       print(tf, root);
642
643       return (tf.append(";").toString());
644     }
645   }
646
647   /**
648    * 
649    * 
650    * Generate a newick format tree according to internal flags for distances and
651    * root distances and user specificied writing of bootstraps.
652    * 
653    * @param withbootstraps
654    *          controls if bootstrap values are explicitly written.
655    * 
656    * @return new hampshire tree in a single line
657    */
658   public String print(boolean withbootstraps)
659   {
660     synchronized (this)
661     {
662       boolean boots = this.HasBootstrap;
663       this.HasBootstrap = withbootstraps;
664
665       String rv = print();
666       this.HasBootstrap = boots;
667
668       return rv;
669     }
670   }
671
672   /**
673    * 
674    * Generate newick format tree according to internal flags for writing root
675    * node distances.
676    * 
677    * @param withbootstraps
678    *          explicitly write bootstrap values
679    * @param withdists
680    *          explicitly write distances
681    * 
682    * @return new hampshire tree in a single line
683    */
684   public String print(boolean withbootstraps, boolean withdists)
685   {
686     synchronized (this)
687     {
688       boolean dists = this.HasDistances;
689       this.HasDistances = withdists;
690
691       String rv = print(withbootstraps);
692       this.HasDistances = dists;
693
694       return rv;
695     }
696   }
697
698   /**
699    * Generate newick format tree according to user specified flags
700    * 
701    * @param withbootstraps
702    *          explicitly write bootstrap values
703    * @param withdists
704    *          explicitly write distances
705    * @param printRootInfo
706    *          explicitly write root distance
707    * 
708    * @return new hampshire tree in a single line
709    */
710   public String print(boolean withbootstraps, boolean withdists,
711           boolean printRootInfo)
712   {
713     synchronized (this)
714     {
715       boolean rootinfo = printRootInfo;
716       this.printRootInfo = printRootInfo;
717
718       String rv = print(withbootstraps, withdists);
719       this.printRootInfo = rootinfo;
720
721       return rv;
722     }
723   }
724
725   /**
726    * DOCUMENT ME!
727    * 
728    * @return DOCUMENT ME!
729    */
730   char getQuoteChar()
731   {
732     return QuoteChar;
733   }
734
735   /**
736    * DOCUMENT ME!
737    * 
738    * @param c
739    *          DOCUMENT ME!
740    * 
741    * @return DOCUMENT ME!
742    */
743   char setQuoteChar(char c)
744   {
745     char old = QuoteChar;
746     QuoteChar = c;
747
748     return old;
749   }
750
751   /**
752    * DOCUMENT ME!
753    * 
754    * @param name
755    *          DOCUMENT ME!
756    * 
757    * @return DOCUMENT ME!
758    */
759   private String nodeName(String name)
760   {
761     if (NodeSafeName[0].search(name))
762     {
763       return QuoteChar + NodeSafeName[1].replaceAll(name) + QuoteChar;
764     }
765     else
766     {
767       return NodeSafeName[2].replaceAll(name);
768     }
769   }
770
771   /**
772    * DOCUMENT ME!
773    * 
774    * @param c
775    *          DOCUMENT ME!
776    * 
777    * @return DOCUMENT ME!
778    */
779   private String printNodeField(SequenceNode c)
780   {
781     return ((c.getName() == null) ? "" : nodeName(c.getName()))
782             + ((HasBootstrap) ? ((c.getBootstrap() > -1) ? ((c.getName() != null ? " "
783                     : "") + c.getBootstrap())
784                     : "")
785                     : "") + ((HasDistances) ? (":" + c.dist) : "");
786   }
787
788   /**
789    * DOCUMENT ME!
790    * 
791    * @param root
792    *          DOCUMENT ME!
793    * 
794    * @return DOCUMENT ME!
795    */
796   private String printRootField(SequenceNode root)
797   {
798     return (printRootInfo) ? (((root.getName() == null) ? ""
799             : nodeName(root.getName()))
800             + ((HasBootstrap) ? ((root.getBootstrap() > -1) ? ((root
801                     .getName() != null ? " " : "") + +root.getBootstrap())
802                     : "") : "") + ((RootHasDistance) ? (":" + root.dist)
803             : "")) : "";
804   }
805
806   // Non recursive call deals with root node properties
807   public void print(StringBuffer tf, SequenceNode root)
808   {
809     if (root != null)
810     {
811       if (root.isLeaf() && printRootInfo)
812       {
813         tf.append(printRootField(root));
814       }
815       else
816       {
817         if (root.isDummy())
818         {
819           _print(tf, (SequenceNode) root.right());
820           _print(tf, (SequenceNode) root.left());
821         }
822         else
823         {
824           tf.append("(");
825           _print(tf, (SequenceNode) root.right());
826
827           if (root.left() != null)
828           {
829             tf.append(",");
830           }
831
832           _print(tf, (SequenceNode) root.left());
833           tf.append(")" + printRootField(root));
834         }
835       }
836     }
837   }
838
839   // Recursive call for non-root nodes
840   public void _print(StringBuffer tf, SequenceNode c)
841   {
842     if (c != null)
843     {
844       if (c.isLeaf())
845       {
846         tf.append(printNodeField(c));
847       }
848       else
849       {
850         if (c.isDummy())
851         {
852           _print(tf, (SequenceNode) c.left());
853           if (c.left() != null)
854           {
855             tf.append(",");
856           }
857           _print(tf, (SequenceNode) c.right());
858         }
859         else
860         {
861           tf.append("(");
862           _print(tf, (SequenceNode) c.right());
863
864           if (c.left() != null)
865           {
866             tf.append(",");
867           }
868
869           _print(tf, (SequenceNode) c.left());
870           tf.append(")" + printNodeField(c));
871         }
872       }
873     }
874   }
875
876   // Test
877   /**
878    * @J2SIgnore
879    * @param args
880    */
881   public static void main(String[] args)
882   {
883     try
884     {
885       if (args == null || args.length != 1)
886       {
887         System.err
888                 .println("Takes one argument - file name of a newick tree file.");
889         System.exit(0);
890       }
891
892       File fn = new File(args[0]);
893
894       StringBuffer newickfile = new StringBuffer();
895       BufferedReader treefile = new BufferedReader(new FileReader(fn));
896       String l;
897
898       while ((l = treefile.readLine()) != null)
899       {
900         newickfile.append(l);
901       }
902
903       treefile.close();
904       System.out.println("Read file :\n");
905
906       NewickFile trf = new NewickFile(args[0], "File");
907       trf.parse();
908       System.out.println("Original file :\n");
909
910       RegExpInterface nonl = RegExp.newRegex("\n+", "");
911       System.out.println(nonl.replaceAll(newickfile.toString()) + "\n");
912
913       System.out.println("Parsed file.\n");
914       System.out.println("Default output type for original input.\n");
915       System.out.println(trf.print());
916       System.out.println("Without bootstraps.\n");
917       System.out.println(trf.print(false));
918       System.out.println("Without distances.\n");
919       System.out.println(trf.print(true, false));
920       System.out.println("Without bootstraps but with distanecs.\n");
921       System.out.println(trf.print(false, true));
922       System.out.println("Without bootstraps or distanecs.\n");
923       System.out.println(trf.print(false, false));
924       System.out.println("With bootstraps and with distances.\n");
925       System.out.println(trf.print(true, true));
926     } catch (java.io.IOException e)
927     {
928       System.err.println("Exception\n" + e);
929       e.printStackTrace();
930     }
931   }
932 }