Debian Codegraph @

src/dr/evolution/colouring/DefaultTreeColouring.java

raw symbols SCIP
1 /*
2 * DefaultTreeColouring.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.colouring;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.Tree;
30
31 /**
32 * @author Alexei Drummond
33 * @author Andrew Rambaut
34 *
35 * @version $Id: TreeColouring.java,v 1.8 2006/08/23 10:46:33 rambaut Exp $
36 */
37 public class DefaultTreeColouring implements TreeColouring {
38
39 public DefaultTreeColouring(int colourCount, Tree tree) {
40 this.colourCount = colourCount;
41 this.tree = tree;
42 branchColourings = new DefaultBranchColouring[tree.getNodeCount()];
43 colourChangeCount = 0;
44 }
45
46 /* Creates a mutable copy of the colouring (without probability assigned) */
47 public DefaultTreeColouring( DefaultTreeColouring treeColouring ) {
48
49 if (!treeColouring.immutable) {
50 throw new RuntimeException("Attempted to create a copy of a mutable colouring");
51 }
52 this.colourCount = treeColouring.colourCount;
53 this.tree = treeColouring.tree; // NOT a copy, to allow tree editing after creation
54 colourChangeCount = treeColouring.colourChangeCount;
55 branchColourings = (DefaultBranchColouring[])treeColouring.branchColourings.clone(); // Shallow copy
56 for (int i=0; i<branchColourings.length; i++) {
57 branchColourings[i] = branchColourings[i].getCopy(); // Turn into a deep copy
58 }
59 }
60
61 public int getColourCount() {
62 return colourCount;
63 }
64
65 public Tree getTree() {
66 return tree;
67 }
68
69 public void setBranchColouring(NodeRef node, DefaultBranchColouring branchColouring) {
70
71 if (immutable) throw new RuntimeException("Attempted to setBranchColouring of an immutable colouring");
72 if (tree.isRoot(node)) throw new IllegalArgumentException("Can't set a BranchColouring for the root node");
73
74 if (branchColourings[node.getNumber()] != null) {
75 colourChangeCount -= branchColourings[node.getNumber()].getColourChanges().size();
76 }
77
78 branchColourings[node.getNumber()] = branchColouring;
79
80 colourChangeCount += branchColouring.getColourChanges().size();
81 }
82
83 /*
84 * Previous version - faster, but more complicated.
85 *
86 public BranchColouring getBranchColouring(NodeRef node) {
87
88 if (tree.isRoot(node)) throw new IllegalArgumentException("Can't get a BranchColouring for the root node");
89
90 if (mutableBranches) {
91 return branchColourings[node.getNumber()];
92 }
93
94 if (branchColourings[node.getNumber()] == null) return null;
95
96 return branchColourings[node.getNumber()].getCopy();
97 }
98 */
99
100 public void setLogProbabilityDensity(double logP) {
101 if (hasProbability) throw new RuntimeException("Attempted to set probability twice");
102 makeImmutable();
103 hasProbability = true;
104 this.logP = logP;
105 }
106
107 public void makeImmutable() {
108 immutable = true;
109 }
110
111 public boolean isImmutable() {
112 return immutable;
113 }
114
115 public boolean hasProbability() {
116 return hasProbability;
117 }
118
119 public void checkColouring() {
120 checkColouringAtNode(tree.getRoot());
121 }
122
123 private void checkColouringAtNode(NodeRef node) {
124 int colour = getNodeColour(node);
125
126 if (!tree.isExternal(node)) {
127 for(int i = 0; i < tree.getChildCount(node); i++) {
128 NodeRef child = tree.getChild(node, i);
129 if (branchColourings[child.getNumber()].getParentColour() != colour) {
130 throw new RuntimeException("Colour mismatch at node " + node.toString());
131 }
132 checkColouringAtNode(child);
133 }
134 }
135 }
136
137 /**
138 * @param node
139 * @return the colour of the given node.
140 */
141 public int getNodeColour(NodeRef node) {
142 if (tree.isRoot(node)) {
143 NodeRef child = tree.getChild(node, 0);
144 return branchColourings[child.getNumber()].getParentColour();
145 } else {
146 return branchColourings[node.getNumber()].getChildColour();
147 }
148 }
149
150 /**
151 * This method will return the colour on the ancestral branch if the given time
152 * is older than the parent of the given node.
153 *
154 * @param node
155 * @return the colour of the branch above the given node at the given (absolute) time.
156 */
157 /* public final int getColour(NodeRef node, double time) {
158
159 double nodeHeight = tree.getNodeHeight(node);
160
161 if (time < nodeHeight) throw new IllegalArgumentException("Specified time is younger than node!");
162 while ((!tree.isRoot(node)) && (tree.getNodeHeight(tree.getParent(node)) < time)) {
163 node = tree.getParent(node);
164 }
165 if (tree.isRoot(node)) throw new IllegalArgumentException("Can't ascertain lineage colour above root!");
166 int colour = getColour(node);
167 List changes = getColourChanges(node);
168 int size = changes.size();
169 int index = 0;
170 while (index < size && ((ColourChange)changes.get(index)).getTime() < time) {
171 index += 1;
172 }
173 if (index == 0) return colour;
174 return ((ColourChange)changes.get(index-1)).getColourAbove();
175 }
176 */
177
178 /**
179 * @param node
180 * @return a branch colouring above this node
181 */
182 public BranchColouring getBranchColouring(NodeRef node) {
183 return branchColourings[node.getNumber()];
184 }
185
186
187 public int getColourChangeCount() {
188 return colourChangeCount;
189 }
190
191 /**
192 * @return the log probability density of this colouring.
193 */
194 public double getLogProbabilityDensity() {
195 if (!hasProbability) {
196 throw new RuntimeException("Tree colouring has no probability density; use colouringModel.getTreeColouringWithProbability()");
197 }
198 return logP;
199 }
200
201 private final Tree tree;
202 private final int colourCount;
203 private final DefaultBranchColouring[] branchColourings;
204 private int colourChangeCount;
205
206 private double logP = Double.NEGATIVE_INFINITY;
207
208 private boolean immutable = false; // true if colouring cannot be changed
209 private boolean hasProbability = false; // true if probability has been assigned (implies immutable)
210
211 }