Debian Codegraph @

src/dr/evolution/colouring/DefaultBranchColouring.java

raw symbols SCIP
1 /*
2 * DefaultBranchColouring.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.ColourChange;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33
34 /**
35 * BranchColouring.java
36 *
37 * Represents a colouring on a single branch, both backward and forward in time.
38 *
39 * @author Gerton Lunter
40 * @author Andrew Rambaut
41 *
42 * @version $Id: BranchColouring.java,v 1.6 2006/08/12 12:55:44 gerton Exp $
43 */
44
45 public class DefaultBranchColouring implements BranchColouring {
46
47 public final static boolean checkSanity = true;
48
49 public DefaultBranchColouring() {
50
51 this.parentColour = -1;
52
53 }
54
55 public DefaultBranchColouring( int parentColour, int childColour ) {
56
57 this.parentColour = parentColour;
58 this.childColour = childColour;
59
60 }
61
62 public DefaultBranchColouring( DefaultBranchColouring history ) {
63
64 parentColour = history.parentColour;
65 childColour = history.childColour;
66 colourChanges.addAll( history.colourChanges );
67 changeHeights.addAll( history.changeHeights );
68 finalSanityCheck();
69
70 }
71
72 public DefaultBranchColouring getCopy() {
73
74 return new DefaultBranchColouring( this );
75
76 }
77
78 public void clear() {
79
80 colourChanges.clear();
81 changeHeights.clear();
82
83 }
84
85 public void reset( int parentColour, int childColour ) {
86
87 this.parentColour = parentColour;
88 this.childColour = childColour;
89 colourChanges.clear();
90 changeHeights.clear();
91
92 }
93
94
95 public int getParentColour() {
96 return parentColour;
97 }
98
99
100 public int getChildColour() {
101 return childColour;
102 }
103
104
105 /**
106 * Add a colour change event, going in the parent->child direction.
107 *
108 * @param colour
109 * @param time
110 */
111 public void addEvent( int colour, double time ) {
112
113 colourChanges.add(colour);
114 changeHeights.add(time);
115
116 sanityCheck();
117
118 }
119
120 /**
121 * Add a number of change events below the current child
122 */
123 public void addHistory( DefaultBranchColouring history ) {
124
125 // require that my child colour is the new history's parent colour
126 if (childColour != history.parentColour) {
127 throw new Error("My child colour and the added parent colour don't match");
128 }
129 colourChanges.addAll( history.colourChanges );
130 changeHeights.addAll( history.changeHeights );
131 childColour = history.childColour;
132
133 fullSanityCheck();
134
135 }
136
137 /**
138 *
139 * @return number of events
140 */
141 public int getNumEvents() {
142 return colourChanges.size();
143 }
144
145 /**
146 *
147 * @param i event, 0..getNumEvents inclusive.
148 * @return Colour below event. If i==0, returns parent colour
149 */
150 public int getForwardColourBelow( int i ) {
151 finalSanityCheck();
152 if (i==0) return parentColour;
153 return colourChanges.get(i - 1);
154 }
155
156 /**
157 *
158 * @param i event, 1..getNumEvents inclusive
159 * @return time of event
160 */
161 public double getForwardTime( int i ) {
162 finalSanityCheck();
163 return changeHeights.get(i - 1);
164 }
165
166 /**
167 *
168 * @param i event, 0..getNumEvents inclusive
169 * @return Colour above event. If i==0, returns child colour
170 */
171 public int getBackwardColourAbove( int i ) {
172 finalSanityCheck();
173 int total = colourChanges.size();
174 if (i == total) return parentColour;
175 return colourChanges.get(total - 1 - i);
176 }
177
178 /**
179 *
180 * @param i event, 1..getNumEvents inclusive
181 * @return time of event
182 */
183 public double getBackwardTime( int i ) {
184 finalSanityCheck();
185 int total = colourChanges.size();
186 return changeHeights.get(total - i);
187 }
188
189 /**
190 * Returns event index corresponding to first event after time, going forward in time
191 * @param height
192 * @return 1 if first event is after time; getNumEvents+1 if time is after last event; 1 if no events exist
193 */
194 public int getNextForwardEvent( double height ) {
195
196 int i;
197 for (i=0; i < changeHeights.size() && changeHeights.get(i) > height; i += 1 ) {}
198 return i+1;
199
200 }
201
202 public double getTimeInColour(int colour, double parentHeight, double childHeight) {
203
204 double totalTime = 0.0;
205 int currentColour = getForwardColourBelow(0); // parent colour
206 double previousTime = parentHeight;
207 for (int i = 1; i <= getNumEvents(); i++) {
208 double currentTime = getForwardTime( i );
209 if (currentColour == colour) {
210 totalTime += previousTime - currentTime;
211 }
212 currentColour = getForwardColourBelow( i );
213 previousTime = currentTime;
214 }
215 if (currentColour == colour) {
216 totalTime += previousTime - childHeight;
217 }
218 return totalTime;
219
220 }
221 /**
222 * for backward compatibility (evolution.tree.ColouredTreePainter)
223 *
224 * @return list of colour changes
225 */
226 public List<ColourChange> getColourChanges() {
227
228 List<ColourChange> cc = new ArrayList<ColourChange>(0);
229 for (int i=1; i<=getNumEvents(); i++) {
230
231 cc.add( new ColourChange( getBackwardTime(i), getBackwardColourAbove(i) ) );
232
233 }
234
235 return cc;
236
237 }
238
239 private void sanityCheck() {
240
241 if (checkSanity) {
242 int total = colourChanges.size();
243 if (total > 0) {
244 if ( colourChanges.get(0) == parentColour) {
245 throw new Error("First event does not change colour");
246 }
247 }
248 if (total > 1) {
249 if ((colourChanges.get(total-1)).intValue() == (colourChanges.get(total-2)).intValue()) {
250 throw new Error("Last event does not change colour");
251 }
252
253 if ( changeHeights.get(total - 1) > changeHeights.get(total - 2) ) {
254 throw new Error("Child event occurs before parent event");
255 }
256 }
257 }
258 }
259
260 private void fullSanityCheck() {
261
262 if (checkSanity) {
263 sanityCheck();
264 int total = colourChanges.size();
265 for (int i=1; i < total; i++) {
266 if ((colourChanges.get(i)).intValue() == (colourChanges.get(i-1)).intValue()) {
267 throw new Error("Event "+i+" does not change colour");
268 }
269 if ( changeHeights.get(i) > changeHeights.get(i - 1) ) {
270 throw new Error("Event "+i+" jumps back in time");
271 }
272 }
273 }
274 }
275
276
277 private void finalSanityCheck() {
278
279 if (checkSanity) {
280
281 if (parentColour == -1) {
282 throw new Error("Parent colour has not been set");
283 }
284 int total = colourChanges.size();
285 if (total > 0) {
286 if ( colourChanges.get(total - 1) != childColour) {
287 throw new Error("Last event does not change colour into child's");
288 }
289 }
290 }
291 }
292
293 private final List<Integer> colourChanges = new ArrayList<Integer>(0); // new colours, forward in time
294 private final List<Double> changeHeights = new ArrayList<Double>(0); // change heights, forward in time
295 private int parentColour;
296 private int childColour;
297
298 }