| 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 |
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 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 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 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 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 |
|
| 122 |
|
| 123 |
public void addHistory( DefaultBranchColouring history ) { |
| 124 |
|
| 125 |
|
| 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 |
|
| 140 |
|
| 141 |
public int getNumEvents() { |
| 142 |
return colourChanges.size(); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 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 |
|
| 159 |
|
| 160 |
|
| 161 |
public double getForwardTime( int i ) { |
| 162 |
finalSanityCheck(); |
| 163 |
return changeHeights.get(i - 1); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 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 |
|
| 181 |
|
| 182 |
|
| 183 |
public double getBackwardTime( int i ) { |
| 184 |
finalSanityCheck(); |
| 185 |
int total = colourChanges.size(); |
| 186 |
return changeHeights.get(total - i); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 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); |
| 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 |
|
| 223 |
|
| 224 |
|
| 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); |
| 294 |
private final List<Double> changeHeights = new ArrayList<Double>(0); |
| 295 |
private int parentColour; |
| 296 |
private int childColour; |
| 297 |
|
| 298 |
} |