1 Clazz.declarePackage ("jalview.io");
2 Clazz.load (["jalview.io.FileParse", "jalview.jsdev.RegExp"], "jalview.io.NewickFile", ["jalview.datamodel.SequenceNode", "jalview.util.MessageManager", "java.io.BufferedReader", "$.IOException", "java.lang.Float", "$.StringBuffer", "java.util.StringTokenizer"], function () {
3 c$ = Clazz.decorateAsClass (function () {
5 this.$HasBootstrap = false;
6 this.$HasDistances = false;
7 this.RootHasDistance = false;
8 this.ReplaceUnderscores = false;
9 this.printRootInfo = true;
10 this.NodeSafeName = null;
11 this.QuoteChar = '\'';
12 Clazz.instantialize (this, arguments);
13 }, jalview.io, "NewickFile", jalview.io.FileParse);
14 Clazz.prepareFields (c$, function () {
15 this.NodeSafeName = Clazz.newArray (-1, [jalview.jsdev.RegExp.perlCode ("m/[\\[,:'()]/"), jalview.jsdev.RegExp.perlCode ("s/'/''/"), jalview.jsdev.RegExp.perlCode ("s/\\/w/_/")]);
17 Clazz.makeConstructor (c$,
19 Clazz.superConstructor (this, jalview.io.NewickFile, [inStr, "Paste"]);
21 Clazz.makeConstructor (c$,
23 Clazz.superConstructor (this, jalview.io.NewickFile, []);
25 }, "jalview.datamodel.SequenceNode");
26 Clazz.makeConstructor (c$,
27 function (newtree, bootstrap) {
28 Clazz.superConstructor (this, jalview.io.NewickFile, []);
29 this.$HasBootstrap = bootstrap;
31 }, "jalview.datamodel.SequenceNode,~B");
32 Clazz.makeConstructor (c$,
33 function (newtree, bootstrap, distances) {
34 Clazz.superConstructor (this, jalview.io.NewickFile, []);
36 this.$HasBootstrap = bootstrap;
37 this.$HasDistances = distances;
38 }, "jalview.datamodel.SequenceNode,~B,~B");
39 Clazz.makeConstructor (c$,
40 function (newtree, bootstrap, distances, rootdistance) {
41 Clazz.superConstructor (this, jalview.io.NewickFile, []);
43 this.$HasBootstrap = bootstrap;
44 this.$HasDistances = distances;
45 this.RootHasDistance = rootdistance;
46 }, "jalview.datamodel.SequenceNode,~B,~B,~B");
47 Clazz.defineMethod (c$, "ErrorStringrange",
48 ($fz = function (Error, Er, r, p, s) {
49 return ((Error == null) ? "" : Error) + Er + " at position " + p + " ( " + s.substring (((p - r) < 0) ? 0 : (p - r), ((p + r) > s.length) ? s.length : (p + r)) + " )\n";
50 }, $fz.isPrivate = true, $fz), "~S,~S,~N,~N,~S");
51 Clazz.defineMethod (c$, "HasBootstrap",
53 return this.$HasBootstrap;
55 Clazz.defineMethod (c$, "HasDistances",
57 return this.$HasDistances;
59 Clazz.defineMethod (c$, "HasRootDistance",
61 return this.RootHasDistance;
63 Clazz.defineMethod (c$, "parse",
67 var file = new StringBuffer ();
68 while ((nf = this.nextLine ()) != null) {
71 nf = file.toString ();
72 }this.root = new jalview.datamodel.SequenceNode ();
79 var commentString2 = null;
80 var DefDistance = 0.001;
81 var DefBootstrap = -1;
82 var distance = DefDistance;
83 var bootstrap = DefBootstrap;
84 var ascending = false;
85 var majorsyms = jalview.jsdev.RegExp.newRegex (["[(\\['),;]"]);
88 var parsednodename = false;
89 while (majorsyms.searchFrom (nf, cp) && (Error == null)) {
90 var fcp = majorsyms.matchedFrom ();
92 switch (schar = nf.charAt (fcp)) {
95 Error = this.ErrorStringrange (Error, "Unexpected '('", 7, fcp, nf);
98 if (c.right () == null) {
99 c.setRight ( new jalview.datamodel.SequenceNode (null, c, null, DefDistance, DefBootstrap, false));
102 if (c.left () != null) {
103 var tmpn = new jalview.datamodel.SequenceNode (null, c, null, 0, 0, true);
104 tmpn.SetChildren (c.left (), c.right ());
106 }c.setLeft ( new jalview.datamodel.SequenceNode (null, c, null, DefDistance, DefBootstrap, false));
108 }if (realroot == null) {
111 distance = DefDistance;
112 bootstrap = DefBootstrap;
116 var qnodename = jalview.jsdev.RegExp.newRegex (["'([^']|'')+'"]);
117 if (qnodename.searchFrom (nf, fcp)) {
118 var nl = qnodename.stringMatched ().length;
119 nodename = String.instantialize (qnodename.stringMatched ().substring (1, nl - 1));
120 var xpandquotes = jalview.jsdev.RegExp.perlCode ("s/''/'/");
121 var widernodename = xpandquotes.replaceAll (nodename);
122 nodename = widernodename;
123 nextcp = fcp + nl + 1;
124 parsednodename = true;
126 Error = this.ErrorStringrange (Error, "Unterminated quotes for nodename", 7, fcp, nf);
131 Error = this.ErrorStringrange (Error, "Wayward semicolon (depth=" + d + ")", 7, fcp, nf);
132 }}if (schar == '[') {
133 var comment = jalview.jsdev.RegExp.newRegex (["]"]);
134 if (comment.searchFrom (nf, fcp)) {
135 nextcp = comment.matchedFrom () + 1;
136 this.warningMessage = "Tree file contained comments which may confuse input algorithm.";
139 Error = this.ErrorStringrange (Error, "Unterminated comment", 3, fcp, nf);
140 };}var fstring = nf.substring (ncp, fcp);
141 while (fstring.indexOf (']') > -1) {
142 var cstart = fstring.indexOf ('[');
143 var cend = fstring.indexOf (']');
144 commentString2 = fstring.substring (cstart + 1, cend);
145 fstring = fstring.substring (0, cstart) + fstring.substring (cend + 1);
147 var uqnodename = jalview.jsdev.RegExp.newRegex (["\\b([^' :;\\](),]+)"]);
148 var nbootstrap = jalview.jsdev.RegExp.newRegex (["\\s*([0-9+]+)\\s*:"]);
149 var ndist = jalview.jsdev.RegExp.newRegex ([":([-0-9Ee.+]+)"]);
150 if (!parsednodename && uqnodename.search (fstring) && ((uqnodename.matchedFromI (1) == 0) || (fstring.charAt (uqnodename.matchedFromI (1) - 1) != ':'))) {
151 if (nodename == null) {
152 if (this.ReplaceUnderscores) {
153 nodename = uqnodename.stringMatchedI (1).$replace ('_', ' ');
155 nodename = uqnodename.stringMatchedI (1);
157 Error = this.ErrorStringrange (Error, "File has broken algorithm - overwritten nodename", 10, fcp, nf);
158 }}if (nbootstrap.search (fstring)) {
159 if (nbootstrap.stringMatchedI (1).equals (uqnodename.stringMatchedI (1))) {
161 }if (nodename == null || nodename.length == 0 || nbootstrap.matchedFromI (1) > (uqnodename.matchedFromI (1) + uqnodename.stringMatched ().length)) {
163 bootstrap = ( new Integer (nbootstrap.stringMatchedI (1))).intValue ();
164 this.$HasBootstrap = true;
166 if (Clazz.exceptionOf (e, Exception)) {
167 Error = this.ErrorStringrange (Error, "Can't parse bootstrap value", 4, ncp + nbootstrap.matchedFrom (), nf);
172 }}var nodehasdistance = false;
173 if (ndist.search (fstring)) {
175 distance = ( new Float (ndist.stringMatchedI (1))).floatValue ();
176 this.$HasDistances = true;
177 nodehasdistance = true;
179 if (Clazz.exceptionOf (e, Exception)) {
180 Error = this.ErrorStringrange (Error, "Can't parse node distance value", 7, ncp + ndist.matchedFrom (), nf);
186 c.setName (nodename);
187 c.dist = (this.$HasDistances) ? distance : DefDistance;
188 c.setBootstrap ((this.$HasBootstrap) ? bootstrap : DefBootstrap);
189 if (c === realroot) {
190 this.RootHasDistance = nodehasdistance;
191 }this.parseNHXNodeProps (c, commentString2);
192 commentString2 = null;
194 var newnode = new jalview.datamodel.SequenceNode (null, c, nodename, (this.$HasDistances) ? distance : DefDistance, (this.$HasBootstrap) ? bootstrap : DefBootstrap, false);
195 this.parseNHXNodeProps (c, commentString2);
196 commentString2 = null;
197 if (c.right () == null) {
198 c.setRight (newnode);
200 if (c.left () == null) {
203 var newdummy = new jalview.datamodel.SequenceNode (null, c, null, (this.$HasDistances ? 0 : DefDistance), 0, true);
204 newdummy.SetChildren (c.left (), newnode);
205 c.setLeft (newdummy);
208 if ((d > -1) && (c == null)) {
209 Error = this.ErrorStringrange (Error, "File broke algorithm: Lost place in tree (is there an extra ')' ?)", 7, fcp, nf);
210 }}if (nf.charAt (fcp) == ')') {
214 if (nf.charAt (fcp) == ',') {
218 if ((c.left () != null) && (!c.left ().isLeaf ())) {
221 distance = DefDistance;
222 bootstrap = DefBootstrap;
223 commentString2 = null;
224 parsednodename = false;
233 throw ( new java.io.IOException (jalview.util.MessageManager.formatMessage ("exception.newfile", Clazz.newArray (-1, [Error.toString ()]))));
234 }if (this.root == null) {
235 throw ( new java.io.IOException (jalview.util.MessageManager.formatMessage ("exception.newfile", Clazz.newArray (-1, [jalview.util.MessageManager.getString ("label.no_tree_read_in")]))));
236 }this.root = this.root.right ().detach ();
237 if (!this.RootHasDistance) {
238 this.root.dist = (this.$HasDistances) ? 0 : DefDistance;
240 Clazz.defineMethod (c$, "parseNHXNodeProps",
241 ($fz = function (c, commentString) {
242 if (commentString != null && commentString.startsWith ("&&NHX")) {
243 var st = new java.util.StringTokenizer (commentString.substring (5), ":");
244 while (st.hasMoreTokens ()) {
245 var tok = st.nextToken ();
246 var colpos = tok.indexOf ("=");
248 var code = tok.substring (0, colpos);
249 var value = tok.substring (colpos + 1);
251 if (code.toLowerCase ().equals ("b")) {
253 var iv = new Float (value);
256 this.$HasBootstrap = true;
258 if (Clazz.exceptionOf (e, Exception)) {
259 System.err.println ("Couldn't parse code '" + code + "' = '" + value + "'");
260 e.printStackTrace (System.err);
266 }}, $fz.isPrivate = true, $fz), "jalview.datamodel.SequenceNode,~S");
267 Clazz.defineMethod (c$, "getTree",
271 Clazz.defineMethod (c$, "print",
274 var tf = new StringBuffer ();
275 this.print (tf, this.root);
276 return (tf.append (";").toString ());
278 Clazz.defineMethod (c$, "print",
279 function (withbootstraps) {
281 var boots = this.$HasBootstrap;
282 this.$HasBootstrap = withbootstraps;
283 var rv = this.print ();
284 this.$HasBootstrap = boots;
287 Clazz.defineMethod (c$, "print",
288 function (withbootstraps, withdists) {
290 var dists = this.$HasDistances;
291 this.$HasDistances = withdists;
292 var rv = this.print (withbootstraps);
293 this.$HasDistances = dists;
296 Clazz.defineMethod (c$, "print",
297 function (withbootstraps, withdists, printRootInfo) {
299 var rootinfo = printRootInfo;
300 this.printRootInfo = printRootInfo;
301 var rv = this.print (withbootstraps, withdists);
302 this.printRootInfo = rootinfo;
305 Clazz.defineMethod (c$, "getQuoteChar",
307 return this.QuoteChar;
309 Clazz.defineMethod (c$, "setQuoteChar",
311 var old = this.QuoteChar;
315 Clazz.defineMethod (c$, "nodeName",
316 ($fz = function (name) {
317 if (this.NodeSafeName[0].search (name)) {
318 return this.QuoteChar + this.NodeSafeName[1].replaceAll (name) + this.QuoteChar;
320 return this.NodeSafeName[2].replaceAll (name);
321 }}, $fz.isPrivate = true, $fz), "~S");
322 Clazz.defineMethod (c$, "printNodeField",
323 ($fz = function (c) {
324 return ((c.getName () == null) ? "" : this.nodeName (c.getName ())) + ((this.$HasBootstrap) ? ((c.getBootstrap () > -1) ? ((c.getName () != null ? " " : "") + c.getBootstrap ()) : "") : "") + ((this.$HasDistances) ? (":" + c.dist) : "");
325 }, $fz.isPrivate = true, $fz), "jalview.datamodel.SequenceNode");
326 Clazz.defineMethod (c$, "printRootField",
327 ($fz = function (root) {
328 return (this.printRootInfo) ? (((root.getName () == null) ? "" : this.nodeName (root.getName ())) + ((this.$HasBootstrap) ? ((root.getBootstrap () > -1) ? ((root.getName () != null ? " " : "") + +root.getBootstrap ()) : "") : "") + ((this.RootHasDistance) ? (":" + root.dist) : "")) : "";
329 }, $fz.isPrivate = true, $fz), "jalview.datamodel.SequenceNode");
330 Clazz.defineMethod (c$, "print",
331 function (tf, root) {
333 if (root.isLeaf () && this.printRootInfo) {
334 tf.append (this.printRootField (root));
336 if (root.isDummy ()) {
337 this._print (tf, root.right ());
338 this._print (tf, root.left ());
341 this._print (tf, root.right ());
342 if (root.left () != null) {
344 }this._print (tf, root.left ());
345 tf.append (")" + this.printRootField (root));
346 }}}}, "StringBuffer,jalview.datamodel.SequenceNode");
347 Clazz.defineMethod (c$, "_print",
351 tf.append (this.printNodeField (c));
354 this._print (tf, c.left ());
355 if (c.left () != null) {
357 }this._print (tf, c.right ());
360 this._print (tf, c.right ());
361 if (c.left () != null) {
363 }this._print (tf, c.left ());
364 tf.append (")" + this.printNodeField (c));
365 }}}}, "StringBuffer,jalview.datamodel.SequenceNode");
366 c$.main = Clazz.defineMethod (c$, "main",
369 if (args == null || args.length != 1) {
370 System.err.println ("Takes one argument - file name of a newick tree file.");
372 }var fn = new java.io.File (args[0]);
373 var newickfile = new StringBuffer ();
374 var treefile = new java.io.BufferedReader ( new java.io.FileReader (fn));
376 while ((l = treefile.readLine ()) != null) {
377 newickfile.append (l);
380 System.out.println ("Read file :\n");
381 var trf = new jalview.io.NewickFile (args[0], "File");
383 System.out.println ("Original file :\n");
384 var nonl = jalview.jsdev.RegExp.newRegex (["\n+", ""]);
385 System.out.println (nonl.replaceAll (newickfile.toString ()) + "\n");
386 System.out.println ("Parsed file.\n");
387 System.out.println ("Default output type for original input.\n");
388 System.out.println (trf.print ());
389 System.out.println ("Without bootstraps.\n");
390 System.out.println (trf.print (false));
391 System.out.println ("Without distances.\n");
392 System.out.println (trf.print (true, false));
393 System.out.println ("Without bootstraps but with distanecs.\n");
394 System.out.println (trf.print (false, true));
395 System.out.println ("Without bootstraps or distanecs.\n");
396 System.out.println (trf.print (false, false));
397 System.out.println ("With bootstraps and with distances.\n");
398 System.out.println (trf.print (true, true));
400 if (Clazz.exceptionOf (e, java.io.IOException)) {
401 System.err.println ("Exception\n" + e);
402 e.printStackTrace ();