File: TreeDrawing.java

package info (click to toggle)
libpj-java 0.0~20150107%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 13,396 kB
  • sloc: java: 99,543; ansic: 987; sh: 153; xml: 26; makefile: 10; sed: 4
file content (600 lines) | stat: -rw-r--r-- 15,039 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//******************************************************************************
//
// File:    TreeDrawing.java
// Package: edu.rit.compbio.phyl
// Unit:    Class edu.rit.compbio.phyl.TreeDrawing
//
// This Java source file is copyright (C) 2008 by Alan Kaminsky. All rights
// reserved. For further information, contact the author, Alan Kaminsky, at
// ark@cs.rit.edu.
//
// This Java source file is part of the Parallel Java Library ("PJ"). PJ is free
// software; you can redistribute it and/or modify it under the terms of the GNU
// General Public License as published by the Free Software Foundation; either
// version 3 of the License, or (at your option) any later version.
//
// PJ is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
// A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// Linking this library statically or dynamically with other modules is making a
// combined work based on this library. Thus, the terms and conditions of the
// GNU General Public License cover the whole combination.
//
// As a special exception, the copyright holders of this library give you
// permission to link this library with independent modules to produce an
// executable, regardless of the license terms of these independent modules, and
// to copy and distribute the resulting executable under terms of your choice,
// provided that you also meet, for each linked independent module, the terms
// and conditions of the license of that module. An independent module is a
// module which is not derived from or based on this library. If you modify this
// library, you may extend this exception to your version of the library, but
// you are not obligated to do so. If you do not wish to do so, delete this
// exception statement from your version.
//
// A copy of the GNU General Public License is provided in the file gpl.txt. You
// may also obtain a copy of the GNU General Public License on the World Wide
// Web at http://www.gnu.org/licenses/gpl.html.
//
//******************************************************************************

package edu.rit.compbio.phyl;

import edu.rit.draw.Drawing;

import edu.rit.draw.item.Group;
import edu.rit.draw.item.Line;
import edu.rit.draw.item.Point;
import edu.rit.draw.item.Text;

import java.io.PrintStream;

import java.text.DecimalFormat;

import java.util.LinkedList;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Class TreeDrawing provides an object that draws a picture of a phylogenetic
 * tree. The tree is specified either as a {@linkplain DnaSequenceTree} object
 * or as a string in a subset of Newick Standard format.
 * <P>
 * <I>Note:</I> Class TreeDrawing is not multiple thread safe.
 * <P>
 * <B>Newick Standard format.</B> The subset of Newick Standard format class
 * TreeDrawing supports is described by this BNF syntax. Nonterminal symbols are
 * in <I>italic</I> font, terminal symbols are in <TT>typewriter</TT> font, |
 * designates alternatives.
 * <P>
 * <I>tree</I> ::= <I>node</I> <TT>;</TT>
 * <BR><I>node</I> ::= <TT>(</TT> <I>childList</I> <TT>)</TT> <I>nameLength</I>
 * <BR><I>childList</I> ::= <I>child</I> | <I>child</I> <TT>,</TT> <I>childList</I>
 * <BR><I>child</I> ::= <I>tip</I> | <I>node</I>
 * <BR><I>tip</I> ::= <I>nameLength</I>
 * <BR><I>nameLength</I> ::= <I>empty</I> | <I>name</I> | <TT>:</TT> <I>length</I> | <I>name</I> <TT>:</TT> <I>length</I>
 * <BR><I>name</I> ::= Any sequence of non-whitespace characters except <TT>(</TT> <TT>)</TT> <TT>,</TT> <TT>:</TT> <TT>;</TT>
 * <BR><I>length</I> ::= Floating point number, as in the <TT>Double.valueOf()</TT> method
 * <P>
 * This subset only supports non-quoted names with no whitespace; however, any
 * underscore character <TT>_</TT> in a name is replaced with a space character.
 * The full Newick Standard format supports quoted names including whitespace.
 * For further information about Newick Standard format, see:
 * <UL>
 * <LI>
 * <A HREF="http://evolution.gs.washington.edu/phylip/newicktree.html" TARGET="_top">http://evolution.gs.washington.edu/phylip/newicktree.html</A>
 * </UL>
 * <P>
 * Here is an example tree:
 * <P>
 * <TT>"(Gibbon:10,(Orangutan:8,(Gorilla:6,(Chimp:4,Human:4)))apes)primates;"</TT>
 * <P>
 * Here is the resulting drawing:
 * <P>
 * <IMG SRC="doc-files/tree.png">
 *
 * @author  Alan Kaminsky
 * @version 25-Jul-2008
 */
public class TreeDrawing
	{

// Hidden constants.

	// Vertical distance between tip strings.
	private static final double V = 27.0;

	// Length of lines from a node to its descendents.
	private static final double H = 72.0;

	// Gap from line to tip text.
	private static final double GAP = 6.0;

	// Gap from line to branch length text.
	private static final double VGAP = 3.0;

	// Regular expression for parsing tokens in Newick Standard format.
	private static final Pattern pattern =
		Pattern.compile ("\\(|\\)|,|:|;|[^(),:; \\t\\n\\x0B\\f\\r]+");

// Hidden data members.

	// Tree string in Newick Standard format.
	private String treestring;

	// For formatting branch lengths.
	private DecimalFormat format = new DecimalFormat ("0.00");

	// Matcher for parsing tokens.
	private Matcher matcher;

	// Current token and its starting index in <treestring>.
	private String token;
	private int index;

	// Number of tips.
	private int tipCount;

	// Group in which to store the tree drawing.
	private Group group;

// Exported constructors.

	/**
	 * Construct a new tree drawing object.
	 */
	public TreeDrawing()
		{
		}

// Main program.

	/**
	 * Specify the format with which to draw branch lengths. The format string
	 * is used to construct a java.text.DecimalFormat object. If not specified,
	 * the default format string is <TT>"0.00"</TT>.
	 *
	 * @param  format  Format string.
	 */
	public void setBranchLengthFormat
		(String format)
		{
		this.format = new DecimalFormat (format);
		}

	/**
	 * Draw a picture of the given tree. The picture is added to the default
	 * {@linkplain edu.rit.draw.Drawing} object.
	 *
	 * @param  tree  DNA sequence tree.
	 */
	public void draw
		(DnaSequenceTree tree)
		{
		draw (tree, Drawing.defaultDrawing());
		}

	/**
	 * Draw a picture of the given tree. The picture is added to the given
	 * {@linkplain edu.rit.draw.Drawing} object.
	 *
	 * @param  tree  DNA sequence tree.
	 * @param  dwg   Drawing object.
	 */
	public void draw
		(DnaSequenceTree tree,
		 Drawing dwg)
		{
		draw (tree, new Group());
		dwg.add (group);
		}

	/**
	 * Draw a picture of the given tree. The picture is added to the given
	 * {@linkplain edu.rit.draw.item.Group} object.
	 *
	 * @param  tree   DNA sequence tree.
	 * @param  group  Drawing group object.
	 */
	public void draw
		(DnaSequenceTree tree,
		 Group group)
		{
		// Initialize.
		this.tipCount = 0;
		this.group = group;

		// Traverse the tree, building up the drawing as it goes.
		Point root = drawNode (tree, tree.root());

		// Add final line to root.
		group.append (new Line().to(root).hby(-H));
		}

	/**
	 * Draw a picture of the given tree string. The picture is added to the
	 * default {@linkplain edu.rit.draw.Drawing} object.
	 *
	 * @param  tree  Tree as a string in Newick Standard format.
	 *
	 * @exception  SyntaxException
	 *     Thrown if there was a syntax error in <TT>tree</TT>.
	 */
	public void draw
		(String tree)
		throws SyntaxException
		{
		draw (tree, Drawing.defaultDrawing());
		}

	/**
	 * Draw a picture of the given tree string. The picture is added to the
	 * given {@linkplain edu.rit.draw.Drawing} object.
	 *
	 * @param  tree  Tree as a string in Newick Standard format.
	 * @param  dwg   Drawing object.
	 *
	 * @exception  SyntaxException
	 *     Thrown if there was a syntax error in <TT>tree</TT>.
	 */
	public void draw
		(String tree,
		 Drawing dwg)
		throws SyntaxException
		{
		draw (tree, new Group());
		dwg.add (group);
		}

	/**
	 * Draw a picture of the given tree string. The picture is added to the
	 * given {@linkplain edu.rit.draw.item.Group} object.
	 *
	 * @param  tree   Tree as a string in Newick Standard format.
	 * @param  group  Drawing group object.
	 *
	 * @exception  SyntaxException
	 *     Thrown if there was a syntax error in <TT>tree</TT>.
	 */
	public void draw
		(String tree,
		 Group group)
		throws SyntaxException
		{
		// Initialize.
		this.treestring = tree;
		this.tipCount = 0;
		this.group = group;

		// Set up matcher.
		matcher = pattern.matcher (treestring);
		nextToken();

		// Parse the tree, building up the drawing as it goes.
		Point root = parseNode();
		if (! ";".equals (token)) syntaxError ("; expected");
		nextToken();
		if (token != null) syntaxError ("Extra characters");

		// Add final line to root.
		group.append (new Line().to(root).hby(-H));
		}

// Hidden operations.

	/**
	 * Draw a node.
	 *
	 * @param  tree   DNA sequence tree.
	 * @param  index  Node index.
	 *
	 * @return  Point to which to attach line from parent.
	 */
	private Point drawNode
		(DnaSequenceTree tree,
		 int index)
		{
		Point p;

		// Draw a tip node.
		boolean isTip = tree.child1 (index) == -1;
		if (isTip)
			{
			p = new Point (0, tipCount * V);
			++ tipCount;
			}

		// Draw an interior node.
		else
			{
			Point p1 = drawNode (tree, tree.child1 (index));
			Point p2 = drawNode (tree, tree.child2 (index));
			double x = Math.min (p1.x(), p2.x()) - H;
			double y1 = p1.y();
			double y2 = p2.y();
			p = new Point (x, (y1 + y2)/2);
			group.append (new Line().to(p1).hto(x).vto(p2).hto(p2));
			}

		// Draw node name.
		DnaSequence seq = tree.seq (index);
		if (seq != null) drawName (seq.name(), p);

		// Draw branch length.
		Double brlen = tree.branchLength (index);
		if (brlen != null) drawBranchLength (brlen, p, isTip);

		return p;
		}

	/**
	 * Draw a node name.
	 *
	 * @param  name  Node name, or null if none.
	 * @param  p     Node's anchor point.
	 */
	private void drawName
		(String name,
		 Point p)
		{
		if (name != null)
			{
			group.append (new Text().text(name).w(p.e(GAP)));
			}
		}

	/**
	 * Draw a branch length.
	 *
	 * @param  brlen  Branch length.
	 * @param  p      Node's anchor point.
	 * @param  isTip  True if node is a tip node, false otherwise.
	 */
	private void drawBranchLength
		(double brlen,
		 Point p,
		 boolean isTip)
		{
		if (isTip)
			{
			group.append
				(new Text().text(format.format(brlen)).se(p.n(VGAP).w(GAP)));
			}
		else
			{
			group.append
				(new Text().text(format.format(brlen)).s(p.n(VGAP).w(H/2)));
			}
		}

	/**
	 * Parse a node.
	 *
	 * @return  Point to which to attach line from parent.
	 *
	 * @exception  SyntaxException
	 *     Thrown if a syntax error occurred.
	 */
	private Point parseNode()
		throws SyntaxException
		{
		if (! "(".equals (token)) syntaxError ("( expected");
		nextToken();
		Point p = parseChildList();
		if (! ")".equals (token)) syntaxError (") expected");
		nextToken();
		parseNameLength (p, false);
		return p;
		}

	/**
	 * Parse a name and/or length.
	 *
	 * @param  p      Point at which to anchor name and branch length.
	 * @param  isTip  True if node is a tip node, false otherwise.
	 *
	 * @exception  SyntaxException
	 *     Thrown if a syntax error occurred.
	 */
	private void parseNameLength
		(Point p,
		 boolean isTip)
		throws SyntaxException
		{
		if (isName (token))
			{
			drawName (token.replaceAll ("_", " "), p);
			nextToken();
			}
		if (":".equals (token))
			{
			nextToken();
			double brlen = 0.0;
			try
				{
				brlen = new Double (token);
				}
			catch (NullPointerException exc)
				{
				syntaxError ("Branch length expected");
				}
			catch (NumberFormatException exc)
				{
				syntaxError ("Branch length expected");
				}
			nextToken();
			drawBranchLength (brlen, p, isTip);
			}
		}

	/**
	 * Determine if the given token is a name.
	 *
	 * @param  token  Token.
	 *
	 * @return  True if <TT>token</TT> is a name, false otherwise.
	 */
	private boolean isName
		(String token)
		{
		return
			token != null &&
			! "(".equals (token) &&
			! ")".equals (token) &&
			! ",".equals (token) &&
			! ":".equals (token) &&
			! ";".equals (token);
		}

	/**
	 * Parse a child list.
	 *
	 * @return  Point to which to attach line from parent.
	 *
	 * @exception  SyntaxException
	 *     Thrown if a syntax error occurred.
	 */
	private Point parseChildList()
		throws SyntaxException
		{
		LinkedList<Point> childList = new LinkedList<Point>();
		double x = 0.0;
		double ymin = 0.0;
		double ymax = 0.0;
		for (;;)
			{
			Point p = parseChild();
			childList.add (p);
			x = Math.min (x, p.x());
			if (! ",".equals (token)) break;
			nextToken();
			}
		x -= H;
		for (Point p : childList)
			{
			group.append (new Line().to(p).hto(x));
			}
		ymin = childList.get(0).y();
		ymax = childList.get(childList.size()-1).y();
		group.append (new Line().to(x,ymin).to(x,ymax));
		return new Point (x, (ymin+ymax)/2);
		}

	/**
	 * Parse a child.
	 *
	 * @return  Point to which to attach line from parent.
	 *
	 * @exception  SyntaxException
	 *     Thrown if a syntax error occurred.
	 */
	private Point parseChild()
		throws SyntaxException
		{
		if ("(".equals (token))
			{
			return parseNode();
			}
		else
			{
			return parseTip();
			}
		}

	/**
	 * Parse a tip.
	 *
	 * @return  Point to which to attach line from parent.
	 *
	 * @exception  SyntaxException
	 *     Thrown if a syntax error occurred.
	 */
	private Point parseTip()
		throws SyntaxException
		{
		Point p = new Point (0, tipCount * V);
		++ tipCount;
		parseNameLength (p, true);
		return p;
		}

	/**
	 * Get the next token being parsed.
	 */
	private void nextToken()
		{
		if (matcher.find())
			{
			token = treestring.substring (matcher.start(), matcher.end());
			index = matcher.start();
			}
		else
			{
			token = null;
			index = treestring.length();
			}
		}

	/**
	 * Report a syntax error.
	 *
	 * @param  msg  Error message.
	 *
	 * @exception  SyntaxException
	 *     Thrown if a syntax error occurred.
	 */
	private void syntaxError
		(String msg)
		throws SyntaxException
		{
		throw new SyntaxException (treestring, index, msg);
		}

// Helper classes.

	/**
	 * Class TreeDrawing.SyntaxException is an exception thrown if there was a
	 * syntax error in a tree string.
	 *
	 * @author  Alan Kaminsky
	 * @version 15-Jul-2008
	 */
	public static class SyntaxException
		extends Exception
		{
		private String treestring;
		private int index;

		/**
		 * Construct a new syntax exception.
		 */
		private SyntaxException
			(String treestring,
			 int index,
			 String msg)
			{
			super (msg);
			this.treestring = treestring;
			this.index = index;
			}

		/**
		 * Print a syntax error message on the given print stream.
		 *
		 * @param  out  Print stream.
		 */
		public void printSyntaxError
			(PrintStream out)
			{
			synchronized (out)
				{
				out.println ("Syntax error: " + getMessage());
				out.println (treestring);
				for (int i = 0; i < index; ++ i) out.print (' ');
				out.println ('^');
				}
			}
		}

	}