Debian Codegraph @

src/dr/evomodel/tree/WanderingTaxonLogger.java

raw symbols SCIP
1 /*
2 * WanderingTaxonLogger.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.evomodel.tree;
27
28 import dr.evolution.tree.Tree;
29 import dr.evolution.tree.NodeRef;
30 import dr.evolution.tree.TreeTrait;
31 import dr.evolution.tree.TreeTraitProvider;
32 import dr.evolution.util.Taxon;
33
34 /**
35 * @author Marc A. Suchard
36 * @author Philippe Lemey
37 */
38 public class WanderingTaxonLogger implements TreeTraitProvider {
39
40 public static final String WANDERER = "wanderingTaxonLogger";
41 public static final String RELATIVE = "relative";
42
43 public WanderingTaxonLogger(String name, Taxon taxon, Relative relative) {
44 if (name == null) {
45 this.name = RELATIVE;
46 } else {
47 this.name = name;
48 }
49 this.taxon = taxon;
50 this.relative = relative;
51 }
52
53 TreeTrait relativeTrait = new TreeTrait.I() {
54 public String getTraitName() {
55 return name;
56 }
57
58 public Intent getIntent() {
59 return Intent.NODE;
60 }
61
62 public Integer getTrait(Tree tree, NodeRef node) {
63 int rtnValue = 0;
64 if (relative == Relative.PARENT) {
65 if (isAnyChildEqualToTaxon(tree, node, taxon, null)) {
66 rtnValue = 1;
67 }
68 } else if (relative == Relative.SISTER && !tree.isRoot(node)) {
69 if (isAnyChildEqualToTaxon(tree, tree.getParent(node), taxon, node)) {
70 rtnValue = 1;
71 }
72 }
73
74 return rtnValue;
75 }
76 };
77
78 public TreeTrait[] getTreeTraits() {
79 return new TreeTrait[] { relativeTrait };
80 }
81
82 public TreeTrait getTreeTrait(String key) {
83 // ignore the key - it must be the one they wanted, no?
84 return relativeTrait;
85 }
86
87 private boolean isAnyChildEqualToTaxon(Tree tree, NodeRef node, Taxon taxon, NodeRef exclude) {
88 for (int i = 0; i < tree.getChildCount(node); i++) {
89 NodeRef child = tree.getChild(node, i);
90 if (child != exclude && tree.isExternal(child)) {
91 String taxonString = tree.getNodeTaxon(child).getId();
92 if (taxonString.equals(taxon.getId())) {
93 return true;
94 }
95 }
96 }
97 return false;
98 }
99
100 public enum Relative {
101 PARENT,
102 SISTER
103 }
104
105 private String name;
106 private Taxon taxon;
107 private Relative relative;
108
109 }