Debian Codegraph @

src/dr/evolution/tree/TreeTrait.java

raw symbols SCIP
1 /*
2 * TreeTrait.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evolution.tree;
27
28 /**
29 * @author Andrew Rambaut
30 * @author Marc Suchard
31 * @author Alexei Drummond
32 * @version $Id$
33 */
34 public interface TreeTrait<T> {
35 public enum Intent {
36 NODE,
37 BRANCH,
38 WHOLE_TREE
39 }
40
41 /**
42 * The human readable name of this trait
43 *
44 * @return the name
45 */
46 String getTraitName();
47
48 /**
49 * Specifies whether this is a trait of the tree, the nodes or the branch
50 *
51 * @return Intent
52 */
53 Intent getIntent();
54
55 /**
56 * Return a class object for the trait
57 *
58 * @return the class
59 */
60 Class getTraitClass();
61
62 /**
63 * Returns the trait values for the given node. If this is a branch trait then
64 * it will be for the branch above the specified node (and may not be valid for
65 * the root). The array will be the length returned by getDimension().
66 *
67 * @param tree a reference to a tree
68 * @param node a reference to a node
69 * @return the trait value
70 */
71 T getTrait(final Tree tree, final NodeRef node);
72
73 /**
74 * Get a string representations of the trait value.
75 *
76 * @param tree a reference to a tree
77 * @param node a reference to a node
78 * @return the trait string representation
79 */
80 String getTraitString(final Tree tree, final NodeRef node);
81
82 /**
83 * Specifies whether this trait is loggable
84 *
85 * @return Intent
86 */
87 boolean getLoggable();
88
89 /**
90 * Default behavior
91 */
92 class DefaultBehavior {
93 public boolean getLoggable() {
94 return true;
95 }
96
97 public boolean getFormatAsArray() {
98 return false;
99 }
100 }
101
102 /**
103 * An abstract base class for Double implementations
104 */
105 public abstract class D extends DefaultBehavior implements TreeTrait<Double> {
106
107 public Class getTraitClass() {
108 return Double.class;
109 }
110
111 public String getTraitString(Tree tree, NodeRef node) {
112 return formatTrait(getTrait(tree, node));
113 }
114
115 public static String formatTrait(Double value) {
116 if (value == null) {
117 return null;
118 }
119 return value.toString();
120 }
121 }
122
123 /**
124 * An abstract base class for Double implementations
125 */
126 public abstract class I extends DefaultBehavior implements TreeTrait<Integer> {
127
128 public Class getTraitClass() {
129 return Integer.class;
130 }
131
132 public String getTraitString(Tree tree, NodeRef node) {
133 return formatTrait(getTrait(tree, node));
134 }
135
136 public static String formatTrait(Integer value) {
137 if (value == null) {
138 return null;
139 }
140 return value.toString();
141 }
142 }
143
144 /**
145 * An abstract base class for String implementations
146 */
147 public abstract class S extends DefaultBehavior implements TreeTrait<String> {
148
149 public Class getTraitClass() {
150 return String.class;
151 }
152
153 public String getTraitString(Tree tree, NodeRef node) {
154 return getTrait(tree, node);
155 }
156 }
157
158 /**
159 * An abstract base class for double array implementations
160 */
161 public abstract class DA extends DefaultBehavior implements TreeTrait<double[]> {
162
163 public Class getTraitClass() {
164 return double[].class;
165 }
166
167 public String getTraitString(Tree tree, NodeRef node) {
168 return formatTrait(getTrait(tree, node));
169 }
170
171 public static String formatTrait(double[] values) {
172 if (values == null || values.length == 0) return null;
173 if (values.length > 1) {
174 StringBuilder sb = new StringBuilder("{");
175 sb.append(values[0]);
176 for (int i = 1; i < values.length; i++) {
177 sb.append(",");
178 sb.append(values[i]);
179 }
180 sb.append("}");
181
182 return sb.toString();
183 } else {
184 return Double.toString(values[0]);
185 }
186 }
187 }
188
189 /**
190 * An abstract base class for String array implementations
191 */
192 public abstract class SA extends DefaultBehavior implements TreeTrait<String[]> {
193
194 public Class getTraitClass() {
195 return String[].class;
196 }
197
198 public String getTraitString(Tree tree, NodeRef node) {
199 return formatTrait(getTrait(tree, node), getFormatAsArray());
200 }
201
202 public static String formatTrait(String[] values, boolean asArray) {
203 if (values == null || values.length == 0) return null;
204 if (values.length > 1 || asArray) {
205 StringBuilder sb = new StringBuilder("{");
206 sb.append(values[0]);
207 for (int i = 1; i < values.length; i++) {
208 sb.append(",");
209 sb.append(values[i]);
210 }
211 sb.append("}");
212
213 return sb.toString();
214 } else {
215 return values[0];
216 }
217 }
218 }
219
220 /**
221 * An abstract base class for int array implementations
222 */
223 public abstract class IA extends DefaultBehavior implements TreeTrait<int[]> {
224
225 public Class getTraitClass() {
226 return int[].class;
227 }
228
229 public String getTraitString(Tree tree, NodeRef node) {
230 return formatTrait(getTrait(tree, node));
231 }
232
233 public static String formatTrait(int[] values) {
234 if (values == null || values.length == 0) return null;
235 if (values.length > 1) {
236 StringBuilder sb = new StringBuilder("{");
237 sb.append(values[0]);
238 for (int i = 1; i < values.length; i++) {
239 sb.append(",");
240 sb.append(values[i]);
241 }
242 sb.append("}");
243
244 return sb.toString();
245 } else {
246 return Integer.toString(values[0]);
247 }
248 }
249 }
250
251 /**
252 * An abstract wrapper class that sums a TreeTrait<T> over the entire tree
253 */
254 public abstract class SumOverTree<T> extends DefaultBehavior implements TreeTrait<T> {
255
256 private static final String NAME_PREFIX = "sumOverTree_";
257 private final TreeTrait<T> base;
258 private final String name;
259 private final boolean includeExternalNodes;
260 private final boolean includeInternalNodes;
261
262 public SumOverTree(TreeTrait<T> base) {
263 this(NAME_PREFIX + base.getTraitName(), base);
264 }
265
266 public SumOverTree(String name, TreeTrait<T> base) {
267 this(name, base, true, true);
268 }
269
270 public SumOverTree(String name, TreeTrait<T> base, boolean includeExternalNodes, boolean includeInternalNodes) {
271 this.base = base;
272 this.name = name;
273 this.includeExternalNodes = includeExternalNodes;
274 this.includeInternalNodes = includeInternalNodes;
275 }
276
277 public String getTraitName() {
278 return name;
279 }
280
281 public Intent getIntent() {
282 return Intent.WHOLE_TREE;
283 }
284
285 public T getTrait(Tree tree, NodeRef node) {
286 T count = null;
287 if (includeExternalNodes) {
288 for (int i = 0; i < tree.getExternalNodeCount(); i++) {
289 count = addToMatrix(count, base.getTrait(tree, tree.getExternalNode(i)));
290 }
291 }
292 if (includeInternalNodes) {
293 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
294 count = addToMatrix(count, base.getTrait(tree, tree.getInternalNode(i)));
295 }
296 }
297 return count;
298 }
299
300 public boolean getLoggable() {
301 return base.getLoggable();
302 }
303
304 protected abstract T addToMatrix(T total, T summant);
305 }
306
307 /**
308 * A wrapper class that sums a TreeTrait.DA over the entire tree
309 */
310 public class SumOverTreeDA extends SumOverTree<double[]> {
311
312 public SumOverTreeDA(String name, TreeTrait<double[]> base,
313 boolean includeExternalNodes, boolean includeInternalNodes) {
314 super(name, base, includeExternalNodes, includeInternalNodes);
315 }
316
317 public SumOverTreeDA(String name, TreeTrait<double[]> base) {
318 super(name, base);
319 }
320
321 public SumOverTreeDA(TreeTrait<double[]> base) {
322 super(base);
323 }
324
325 public String getTraitString(Tree tree, NodeRef node) {
326 return DA.formatTrait(getTrait(tree, node));
327 }
328
329 public Class getTraitClass() {
330 return double[].class;
331 }
332
333 protected double[] addToMatrix(double[] total, double[] summant) {
334 return addToMatrixStatic(total, summant);
335 }
336
337 protected static double[] addToMatrixStatic(double[] total, double[] summant) {
338 if (summant == null) {
339 return total;
340 }
341 final int length = summant.length;
342 if (total == null) {
343 total = new double[length];
344 }
345 for (int i = 0; i < length; i++) {
346 total[i] += summant[i];
347 }
348 return total;
349 }
350 }
351
352 /**
353 * A wrapper class that sums a TreeTrait.D over the entire tree
354 */
355 public class SumOverTreeD extends SumOverTree<Double> {
356
357 public SumOverTreeD(String name, TreeTrait<Double> base,
358 boolean includeExternalNodes, boolean includeInternalNodes) {
359 super(name, base, includeExternalNodes, includeInternalNodes);
360 }
361
362 public SumOverTreeD(String name, TreeTrait<Double> base) {
363 super(name, base);
364 }
365
366 public SumOverTreeD(TreeTrait<Double> base) {
367 super(base);
368 }
369
370 public String getTraitString(Tree tree, NodeRef node) {
371 return D.formatTrait(getTrait(tree, node));
372 }
373
374 public Class getTraitClass() {
375 return double[].class;
376 }
377
378 protected Double addToMatrix(Double total, Double summant) {
379 return addToMatrixStatic(total, summant);
380 }
381
382 protected static Double addToMatrixStatic(Double total, Double summant) {
383 if (summant == null) {
384 return total;
385 }
386 if (total == null) {
387 total = 0.0;
388 }
389 total += summant;
390 return total;
391 }
392 }
393
394 /**
395 * An abstract wrapper class that sums a TreeTrait.Array into a TreeTrait
396 */
397 public abstract class SumAcrossArray<T, TA> extends DefaultBehavior implements TreeTrait<T> {
398
399 private TreeTrait<TA> base;
400 private String name;
401 public static final String NAME_PREFIX = "sumAcrossArray_";
402
403 public SumAcrossArray(TreeTrait<TA> base) {
404 this(NAME_PREFIX + base.getTraitName(), base);
405 }
406
407 public SumAcrossArray(String name, TreeTrait<TA> base) {
408 this.name = name;
409 this.base = base;
410 }
411
412 public String getTraitName() {
413 return name;
414 }
415
416 public Intent getIntent() {
417 return base.getIntent();
418 }
419
420 public T getTrait(Tree tree, NodeRef node) {
421 TA values = base.getTrait(tree, node);
422 if (values == null) {
423 return null;
424 }
425 return reduce(values);
426
427 }
428
429 public boolean getLoggable() {
430 return base.getLoggable();
431 }
432
433 protected abstract T reduce(TA values);
434 }
435
436 /**
437 * A wrapper class that sums a TreeTrait.DA into a TreeTrait.D
438 */
439 public class SumAcrossArrayD extends SumAcrossArray<Double, double[]> {
440
441 public SumAcrossArrayD(String name, TreeTrait<double[]> base) {
442 super(name, base);
443 }
444
445 public SumAcrossArrayD(TreeTrait<double[]> base) {
446 super(base);
447 }
448
449 public Class getTraitClass() {
450 return Double.class;
451 }
452
453 protected Double reduce(double[] values) {
454 double total = 0.0;
455 for (double value : values) {
456 total += value;
457 }
458 return total;
459 }
460
461 public String getTraitString(Tree tree, NodeRef node) {
462 return D.formatTrait(getTrait(tree, node));
463 }
464 }
465
466
467 /**
468 * An abstract wrapper class that picks one entry out of TreeTrait<T> where T is an array
469 */
470 public abstract class PickEntry<T, TA> extends DefaultBehavior implements TreeTrait<T> {
471
472 protected TreeTrait<TA> base;
473 private String name;
474 protected int index;
475
476 public PickEntry(TreeTrait<TA> base, int index) {
477 this(base.getTraitName() + "_" + (index + 1), base, index);
478 }
479
480 public PickEntry(String name, TreeTrait<TA> base, int index) {
481 this.name = name;
482 this.base = base;
483 // if (base.getTraitClass() != int[].class || base.getTraitClass() != double[].class) {
484 // throw new RuntimeException("Only supported for arrays");
485 // }
486 this.index = index;
487 }
488
489 public String getTraitName() {
490 return name;
491 }
492
493 public Intent getIntent() {
494 return base.getIntent();
495 }
496 }
497
498 public class PickEntryD extends PickEntry<Double, double[]> {
499
500 public PickEntryD(TreeTrait<double[]> base, int index) {
501 super(base, index);
502 }
503
504 public PickEntryD(String name, TreeTrait<double[]> base, int index) {
505 super(name, base, index);
506 }
507
508 public Class getTraitClass() {
509 return Double.class;
510 }
511
512 public Double getTrait(Tree tree, NodeRef node) {
513 return base.getTrait(tree, node)[index];
514 }
515
516 public String getTraitString(Tree tree, NodeRef node) {
517 return D.formatTrait(getTrait(tree, node));
518 }
519 }
520
521 public class PickEntryDAndScale extends PickEntryD {
522
523 public PickEntryDAndScale(TreeTrait<double[]> base, int index) {
524 super(base, index);
525 }
526
527 public PickEntryDAndScale(String name, TreeTrait<double[]> base, int index) {
528 super(name, base, index);
529 }
530
531 public Double getTrait(Tree tree, NodeRef node) {
532 return (base.getTrait(tree, node)[index]) / tree.getBranchLength(node);
533 }
534 }
535
536 public class PickEntryI extends PickEntry<Integer, int[]> {
537
538 public PickEntryI(TreeTrait<int[]> base, int index) {
539 super(base, index);
540 }
541
542 public PickEntryI(String name, TreeTrait<int[]> base, int index) {
543 super(name, base, index);
544 }
545
546 public Class getTraitClass() {
547 return Double.class;
548 }
549
550 public Integer getTrait(Tree tree, NodeRef node) {
551 return base.getTrait(tree, node)[index];
552 }
553
554 public String getTraitString(Tree tree, NodeRef node) {
555 return I.formatTrait(getTrait(tree, node));
556 }
557 }
558
559 /**
560 * An abstract wrapper class that filters a TreeTrait<T>
561 */
562 public abstract class Filtered<T> extends DefaultBehavior implements TreeTrait<T> {
563
564 private static final String NAME_PREFIX = "filtered_";
565 private final TreeTrait<T> base;
566 private final String name;
567 private final TreeNodeFilter treeNodeFilter;
568
569 public Filtered(TreeTrait<T> base, TreeNodeFilter treeNodeFilter) {
570 this(NAME_PREFIX + base.getTraitName(), base, treeNodeFilter);
571 }
572
573 public Filtered(String name, TreeTrait<T> base, TreeNodeFilter treeNodeFilter) {
574 this.base = base;
575 this.name = name;
576 this.treeNodeFilter = treeNodeFilter;
577 }
578
579 public String getTraitName() {
580 return name;
581 }
582
583 public Intent getIntent() {
584 return base.getIntent();
585 }
586
587 public T getTrait(Tree tree, NodeRef node) {
588
589 T count = null;
590
591 if (getIntent() == Intent.WHOLE_TREE) {
592 for (int i = 0; i < tree.getNodeCount(); i++) {
593 NodeRef tmpNode = tree.getNode(i);
594 if (treeNodeFilter.includeNode(tree, tmpNode)) {
595 count = addToMatrix(count, base.getTrait(tree, tmpNode));
596 }
597 }
598 } else {
599 count = base.getTrait(tree, node);
600 if (!treeNodeFilter.includeNode(tree, node)) {
601 count = zero(count);
602 }
603 }
604 return count;
605 }
606
607 public boolean getLoggable() {
608 return base.getLoggable();
609 }
610
611 protected abstract T addToMatrix(T total, T summant);
612
613 protected abstract T zero(T copy);
614 }
615
616 public class FilteredD extends Filtered<Double> {
617
618 public FilteredD(TreeTrait<Double> base, TreeNodeFilter treeNodeFilter) {
619 super(base, treeNodeFilter);
620 }
621
622 public FilteredD(String name, TreeTrait<Double> base, TreeNodeFilter treeNodeFilter) {
623 super(name, base, treeNodeFilter);
624 }
625
626 @Override
627 protected Double addToMatrix(Double total, Double summant) {
628 return SumOverTreeD.addToMatrixStatic(total, summant);
629 }
630
631 protected Double zero(Double copy) {
632 return 0.0;
633 }
634
635 public Class getTraitClass() {
636 return Double.class;
637 }
638
639 public String getTraitString(Tree tree, NodeRef node) {
640 return D.formatTrait(getTrait(tree, node));
641 }
642 }
643
644 public class FilteredDA extends Filtered<double[]> {
645
646 public FilteredDA(TreeTrait<double[]> base, TreeNodeFilter treeNodeFilter) {
647 super(base, treeNodeFilter);
648 }
649
650 public FilteredDA(String name, TreeTrait<double[]> base, TreeNodeFilter treeNodeFilter) {
651 super(name, base, treeNodeFilter);
652 }
653
654 @Override
655 protected double[] addToMatrix(double[] total, double[] summant) {
656 return SumOverTreeDA.addToMatrixStatic(total, summant);
657 }
658
659 protected double[] zero(double[] copy) {
660 return new double[copy.length];
661 }
662
663 public Class getTraitClass() {
664 return double[].class;
665 }
666
667 public String getTraitString(Tree tree, NodeRef node) {
668 return DA.formatTrait(getTrait(tree, node));
669 }
670 }
671 }
672