| 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.evomodel.branchratemodel; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.tree.TreeTrait; |
| 31 |
import dr.evolution.tree.TreeUtils; |
| 32 |
import dr.evolution.util.TaxonList; |
| 33 |
import dr.evomodel.tree.TreeModel; |
| 34 |
import dr.evomodelxml.branchratemodel.LocalClockModelParser; |
| 35 |
import dr.inference.model.Model; |
| 36 |
import dr.inference.model.Parameter; |
| 37 |
import dr.inference.model.Variable; |
| 38 |
import dr.util.Author; |
| 39 |
import dr.util.Citable; |
| 40 |
import dr.util.Citation; |
| 41 |
|
| 42 |
import java.util.*; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class LocalClockModel extends AbstractBranchRateModel implements Citable { |
| 49 |
|
| 50 |
private TreeModel treeModel; |
| 51 |
protected Map<Integer, LocalClock> localTipClocks = new HashMap<Integer, LocalClock>(); |
| 52 |
protected Map<BitSet, LocalClock> localCladeClocks = new HashMap<BitSet, LocalClock>(); |
| 53 |
protected LocalClock trunkClock = null; |
| 54 |
|
| 55 |
private boolean updateNodeClocks = true; |
| 56 |
private Map<NodeRef, LocalClock> nodeClockMap = new HashMap<NodeRef, LocalClock>(); |
| 57 |
private final Parameter globalRateParameter; |
| 58 |
|
| 59 |
public LocalClockModel(TreeModel treeModel, Parameter globalRateParameter) { |
| 60 |
|
| 61 |
super(LocalClockModelParser.LOCAL_CLOCK_MODEL); |
| 62 |
this.treeModel = treeModel; |
| 63 |
|
| 64 |
addModel(treeModel); |
| 65 |
|
| 66 |
this.globalRateParameter = globalRateParameter; |
| 67 |
addVariable(globalRateParameter); |
| 68 |
|
| 69 |
|
| 70 |
helper.addTrait(this); |
| 71 |
|
| 72 |
updateNodeClocks = true; |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
public void addExternalBranchClock(TaxonList taxonList, Parameter rateParameter, boolean isRelativeRate) throws TreeUtils.MissingTaxonException { |
| 77 |
Set<Integer> tips = TreeUtils.getTipsForTaxa(treeModel, taxonList); |
| 78 |
LocalClock clock = new LocalClock(rateParameter, isRelativeRate, tips, ClockType.EXTERNAL); |
| 79 |
for (int i : tips) { |
| 80 |
localTipClocks.put(i, clock); |
| 81 |
} |
| 82 |
addVariable(rateParameter); |
| 83 |
} |
| 84 |
|
| 85 |
public void addCladeClock(TaxonList taxonList, Parameter rateParameter, boolean isRelativeRate, double stemProportion, boolean excludeClade) throws TreeUtils.MissingTaxonException { |
| 86 |
Set<Integer> tips = TreeUtils.getTipsForTaxa(treeModel, taxonList); |
| 87 |
BitSet tipBitSet = TreeUtils.getTipsBitSetForTaxa(treeModel, taxonList); |
| 88 |
LocalClock clock = new LocalClock(rateParameter, isRelativeRate, tips, stemProportion, excludeClade); |
| 89 |
localCladeClocks.put(tipBitSet, clock); |
| 90 |
addVariable(rateParameter); |
| 91 |
} |
| 92 |
|
| 93 |
public void addTrunkClock(TaxonList taxonList, Parameter rateParameter, Parameter indexParameter, boolean isRelativeRate) throws TreeUtils.MissingTaxonException { |
| 94 |
if (trunkClock != null) { |
| 95 |
throw new RuntimeException("Trunk already defined for this LocalClockModel"); |
| 96 |
} |
| 97 |
|
| 98 |
List<Integer> tipList = new ArrayList<Integer>(TreeUtils.getTipsForTaxa(treeModel, taxonList)); |
| 99 |
trunkClock = new LocalClock(rateParameter, indexParameter, isRelativeRate, tipList, ClockType.TRUNK); |
| 100 |
addVariable(rateParameter); |
| 101 |
if (indexParameter != null) { |
| 102 |
addVariable(indexParameter); |
| 103 |
} |
| 104 |
|
| 105 |
helper.addTrait("trunk", new TreeTrait.S() { |
| 106 |
|
| 107 |
public String getTraitName() { |
| 108 |
return "trunk"; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
public Intent getIntent() { |
| 113 |
return Intent.BRANCH; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
public String getTrait(Tree tree, NodeRef node) { |
| 118 |
setupNodeClocks(tree); |
| 119 |
if (nodeClockMap.get(node) == trunkClock) { |
| 120 |
return "T"; |
| 121 |
} |
| 122 |
return "B"; |
| 123 |
} |
| 124 |
}); |
| 125 |
} |
| 126 |
|
| 127 |
public void handleModelChangedEvent(Model model, Object object, int index) { |
| 128 |
updateNodeClocks = true; |
| 129 |
fireModelChanged(); |
| 130 |
} |
| 131 |
|
| 132 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 133 |
if (trunkClock != null && variable == trunkClock.indexParameter) { |
| 134 |
updateNodeClocks = true; |
| 135 |
} |
| 136 |
fireModelChanged(); |
| 137 |
} |
| 138 |
|
| 139 |
protected void storeState() { |
| 140 |
} |
| 141 |
|
| 142 |
protected void restoreState() { |
| 143 |
updateNodeClocks = true; |
| 144 |
} |
| 145 |
|
| 146 |
protected void acceptState() { |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public TreeTrait[] getTreeTraits() { |
| 152 |
return helper.getTreeTraits(); |
| 153 |
} |
| 154 |
|
| 155 |
public TreeTrait getTreeTrait(String key) { |
| 156 |
return helper.getTreeTrait(key); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public double getBranchRate(final Tree tree, final NodeRef node) { |
| 162 |
|
| 163 |
if (tree.isRoot(node)) { |
| 164 |
throw new IllegalArgumentException("root node doesn't have a rate!"); |
| 165 |
} |
| 166 |
|
| 167 |
setupNodeClocks(tree); |
| 168 |
|
| 169 |
double rate = globalRateParameter.getParameterValue(0); |
| 170 |
|
| 171 |
LocalClock parentClock = nodeClockMap.get(tree.getParent(node)); |
| 172 |
LocalClock localClock = nodeClockMap.get(node); |
| 173 |
if (localClock != null) { |
| 174 |
double parentRate = rate; |
| 175 |
double stemProportion = 1.0; |
| 176 |
|
| 177 |
if (localClock != parentClock) { |
| 178 |
|
| 179 |
if (parentClock != null) { |
| 180 |
if (parentClock.isRelativeRate()) { |
| 181 |
parentRate *= localClock.getRateParameter().getParameterValue(0); |
| 182 |
} else { |
| 183 |
parentRate = localClock.getRateParameter().getParameterValue(0); |
| 184 |
} |
| 185 |
} |
| 186 |
stemProportion = localClock.getStemProportion(); |
| 187 |
} |
| 188 |
|
| 189 |
if (localClock.isRelativeRate()) { |
| 190 |
rate *= localClock.getRateParameter().getParameterValue(0); |
| 191 |
} else { |
| 192 |
rate = localClock.getRateParameter().getParameterValue(0); |
| 193 |
} |
| 194 |
|
| 195 |
rate = (rate * stemProportion) + (parentRate * (1.0 - stemProportion)); |
| 196 |
} |
| 197 |
|
| 198 |
return rate; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private void setupNodeClocks(final Tree tree) { |
| 206 |
if (updateNodeClocks) { |
| 207 |
nodeClockMap.clear(); |
| 208 |
setupRateParameters(tree, tree.getRoot(), new BitSet()); |
| 209 |
|
| 210 |
if (trunkClock != null) { |
| 211 |
|
| 212 |
setupTrunkRates(tree, tree.getRoot()); |
| 213 |
} |
| 214 |
|
| 215 |
updateNodeClocks = false; |
| 216 |
} |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
private void setupRateParameters(Tree tree, NodeRef node, BitSet tips) { |
| 229 |
LocalClock clock; |
| 230 |
|
| 231 |
if (tree.isExternal(node)) { |
| 232 |
tips.set(node.getNumber()); |
| 233 |
clock = localTipClocks.get(node.getNumber()); |
| 234 |
} else { |
| 235 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 236 |
NodeRef child = tree.getChild(node, i); |
| 237 |
BitSet childTips = new BitSet(); |
| 238 |
setupRateParameters(tree, child, childTips); |
| 239 |
|
| 240 |
tips.or(childTips); |
| 241 |
} |
| 242 |
clock = localCladeClocks.get(tips); |
| 243 |
} |
| 244 |
|
| 245 |
if (clock != null) { |
| 246 |
setNodeClock(tree, node, clock, clock.excludeClade()); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
private boolean setupTrunkRates(Tree tree, NodeRef node) { |
| 251 |
LocalClock clock = null; |
| 252 |
|
| 253 |
if (tree.isExternal(node)) { |
| 254 |
if (trunkClock.indexParameter != null) { |
| 255 |
if (trunkClock.tipList.get((int) trunkClock.indexParameter.getParameterValue(0)) == node.getNumber()) { |
| 256 |
clock = trunkClock; |
| 257 |
} |
| 258 |
} else if (trunkClock.tipList.contains(node.getNumber())) { |
| 259 |
clock = trunkClock; |
| 260 |
} |
| 261 |
} else { |
| 262 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 263 |
NodeRef child = tree.getChild(node, i); |
| 264 |
if (setupTrunkRates(tree, child)) { |
| 265 |
|
| 266 |
clock = trunkClock; |
| 267 |
} |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
if (clock != null) { |
| 272 |
setNodeClock(tree, node, clock, clock.excludeClade()); |
| 273 |
return true; |
| 274 |
} |
| 275 |
|
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
private void setNodeClock(Tree tree, NodeRef node, LocalClock localClock, boolean excludeClade) { |
| 287 |
|
| 288 |
if (!tree.isExternal(node) && !excludeClade) { |
| 289 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 290 |
NodeRef child = tree.getChild(node, i); |
| 291 |
setNodeClock(tree, child, localClock, false); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
if (!nodeClockMap.containsKey(node)) { |
| 296 |
nodeClockMap.put(node, localClock); |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
enum ClockType { |
| 301 |
CLADE, |
| 302 |
TRUNK, |
| 303 |
EXTERNAL |
| 304 |
} |
| 305 |
|
| 306 |
private class LocalClock { |
| 307 |
|
| 308 |
LocalClock(Parameter rateParameter, boolean isRelativeRate, Set<Integer> tipSet, ClockType type) { |
| 309 |
this.rateParameter = rateParameter; |
| 310 |
this.indexParameter = null; |
| 311 |
this.isRelativeRate = isRelativeRate; |
| 312 |
this.tips = tipSet; |
| 313 |
this.tipList = null; |
| 314 |
this.type = type; |
| 315 |
this.stemProportion = 1.0; |
| 316 |
this.excludeClade = true; |
| 317 |
} |
| 318 |
|
| 319 |
LocalClock(Parameter rateParameter, Parameter indexParameter, boolean isRelativeRate, List<Integer> tipList, ClockType type) { |
| 320 |
this.rateParameter = rateParameter; |
| 321 |
this.indexParameter = indexParameter; |
| 322 |
this.isRelativeRate = isRelativeRate; |
| 323 |
this.tips = null; |
| 324 |
this.tipList = tipList; |
| 325 |
this.type = type; |
| 326 |
this.stemProportion = 1.0; |
| 327 |
this.excludeClade = true; |
| 328 |
} |
| 329 |
|
| 330 |
LocalClock(Parameter rateParameter, boolean isRelativeRate, Set<Integer> tips, double stemProportion, boolean excludeClade) { |
| 331 |
this.rateParameter = rateParameter; |
| 332 |
this.indexParameter = null; |
| 333 |
this.isRelativeRate = isRelativeRate; |
| 334 |
this.tips = tips; |
| 335 |
this.tipList = null; |
| 336 |
this.type = ClockType.CLADE; |
| 337 |
this.stemProportion = stemProportion; |
| 338 |
this.excludeClade = excludeClade; |
| 339 |
} |
| 340 |
|
| 341 |
double getStemProportion() { |
| 342 |
return this.stemProportion; |
| 343 |
} |
| 344 |
|
| 345 |
boolean excludeClade() { |
| 346 |
return excludeClade; |
| 347 |
} |
| 348 |
|
| 349 |
ClockType getType() { |
| 350 |
return this.type; |
| 351 |
} |
| 352 |
|
| 353 |
boolean isRelativeRate() { |
| 354 |
return isRelativeRate; |
| 355 |
} |
| 356 |
|
| 357 |
Parameter getRateParameter() { |
| 358 |
return this.rateParameter; |
| 359 |
} |
| 360 |
|
| 361 |
private final Parameter rateParameter; |
| 362 |
private final Parameter indexParameter; |
| 363 |
private final boolean isRelativeRate; |
| 364 |
private final Set<Integer> tips; |
| 365 |
private final List<Integer> tipList; |
| 366 |
private final ClockType type; |
| 367 |
private final double stemProportion; |
| 368 |
private final boolean excludeClade; |
| 369 |
} |
| 370 |
|
| 371 |
private final Helper helper = new Helper(); |
| 372 |
|
| 373 |
@Override |
| 374 |
public Citation.Category getCategory() { |
| 375 |
return Citation.Category.MOLECULAR_CLOCK; |
| 376 |
} |
| 377 |
|
| 378 |
@Override |
| 379 |
public String getDescription() { |
| 380 |
return "Local clock model"; |
| 381 |
} |
| 382 |
|
| 383 |
@Override |
| 384 |
public List<Citation> getCitations() { |
| 385 |
return Collections.singletonList(CITATION); |
| 386 |
} |
| 387 |
|
| 388 |
public static Citation CITATION = new Citation( |
| 389 |
new Author[]{ |
| 390 |
new Author("AD", "Yoder"), |
| 391 |
new Author("Z", "Yang") |
| 392 |
}, |
| 393 |
"Estimation of Primate Speciation Dates Using Local Molecular Clocks", |
| 394 |
2000, |
| 395 |
"Mol Biol Evol", |
| 396 |
17, 1081, 1090 |
| 397 |
); |
| 398 |
} |