| 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.epidemiology.casetocase.operators; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evomodel.epidemiology.casetocase.AbstractCase; |
| 30 |
import dr.evomodel.epidemiology.casetocase.BranchMapModel; |
| 31 |
import dr.evomodel.epidemiology.casetocase.CaseToCaseTreeLikelihood; |
| 32 |
import dr.evomodel.operators.AbstractTreeOperator; |
| 33 |
import dr.evomodel.tree.TreeModel; |
| 34 |
import dr.inference.operators.MCMCOperator; |
| 35 |
import dr.math.MathUtils; |
| 36 |
import dr.xml.*; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class TransmissionWilsonBaldingB extends AbstractTreeOperator { |
| 45 |
|
| 46 |
private final CaseToCaseTreeLikelihood c2cLikelihood; |
| 47 |
public static final String TRANSMISSION_WILSON_BALDING_B = "transmissionWilsonBaldingB"; |
| 48 |
private double logq; |
| 49 |
private static final boolean DEBUG = false; |
| 50 |
private final int tipCount; |
| 51 |
|
| 52 |
private final boolean resampleInfectionTimes; |
| 53 |
|
| 54 |
public TransmissionWilsonBaldingB(CaseToCaseTreeLikelihood c2cLikelihood, double weight, |
| 55 |
boolean resampleInfectionTimes) { |
| 56 |
this.c2cLikelihood = c2cLikelihood; |
| 57 |
setWeight(weight); |
| 58 |
tipCount = c2cLikelihood.getTreeModel().getExternalNodeCount(); |
| 59 |
|
| 60 |
this.resampleInfectionTimes = resampleInfectionTimes; |
| 61 |
} |
| 62 |
|
| 63 |
public double doOperation() { |
| 64 |
|
| 65 |
if(DEBUG){ |
| 66 |
c2cLikelihood.outputTreeToFile("BeforeTWWB.nex", false); |
| 67 |
} |
| 68 |
|
| 69 |
proposeTree(); |
| 70 |
|
| 71 |
if (c2cLikelihood.getTreeModel().getExternalNodeCount() != tipCount) { |
| 72 |
int newCount = c2cLikelihood.getTreeModel().getExternalNodeCount(); |
| 73 |
throw new RuntimeException("Lost some tips in modified SPR! (" + |
| 74 |
tipCount + "-> " + newCount + ")"); |
| 75 |
} |
| 76 |
|
| 77 |
if(DEBUG){ |
| 78 |
c2cLikelihood.outputTreeToFile("AfterTWWB.nex", false); |
| 79 |
} |
| 80 |
|
| 81 |
return logq; |
| 82 |
} |
| 83 |
|
| 84 |
public void proposeTree() { |
| 85 |
TreeModel tree = c2cLikelihood.getTreeModel(); |
| 86 |
BranchMapModel branchMap = c2cLikelihood.getBranchMap(); |
| 87 |
NodeRef i; |
| 88 |
double oldMinAge, newMinAge, newRange, oldRange, newAge, q; |
| 89 |
|
| 90 |
final int nodeCount = tree.getNodeCount(); |
| 91 |
do { |
| 92 |
i = tree.getNode(MathUtils.nextInt(nodeCount)); |
| 93 |
} while (!eligibleForMove(i, tree, branchMap)); |
| 94 |
final NodeRef iP = tree.getParent(i); |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
NodeRef j = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 99 |
NodeRef jP = tree.getParent(j); |
| 100 |
|
| 101 |
while ((jP != null && tree.getNodeHeight(jP) <= tree.getNodeHeight(i)) || (i == j)) { |
| 102 |
j = tree.getNode(MathUtils.nextInt(tree.getNodeCount())); |
| 103 |
jP = tree.getParent(j); |
| 104 |
} |
| 105 |
|
| 106 |
if (iP == tree.getRoot() || j == tree.getRoot()) { |
| 107 |
logq = Double.NEGATIVE_INFINITY; |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
if (jP == iP || j == iP || jP == i){ |
| 112 |
logq = Double.NEGATIVE_INFINITY; |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
final NodeRef CiP = getOtherChild(tree, iP, i); |
| 117 |
NodeRef PiP = tree.getParent(iP); |
| 118 |
|
| 119 |
if(resampleInfectionTimes) { |
| 120 |
|
| 121 |
AbstractCase iCase = branchMap.get(i.getNumber()); |
| 122 |
AbstractCase iPCase = branchMap.get(iP.getNumber()); |
| 123 |
AbstractCase CiPCase = branchMap.get(CiP.getNumber()); |
| 124 |
AbstractCase PiPCase = null; |
| 125 |
if(PiP!=null){ |
| 126 |
PiPCase = branchMap.get(PiP.getNumber()); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
if (iCase != iPCase) { |
| 132 |
iCase.setInfectionBranchPosition(MathUtils.nextDouble()); |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
if (PiPCase == null || CiPCase != PiPCase) { |
| 137 |
CiPCase.setInfectionBranchPosition(MathUtils.nextDouble()); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
AbstractCase jCase = branchMap.get(j.getNumber()); |
| 143 |
|
| 144 |
jCase.setInfectionBranchPosition(MathUtils.nextDouble()); |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
newMinAge = Math.max(tree.getNodeHeight(i), tree.getNodeHeight(j)); |
| 149 |
newRange = tree.getNodeHeight(jP) - newMinAge; |
| 150 |
newAge = newMinAge + (MathUtils.nextDouble() * newRange); |
| 151 |
oldMinAge = Math.max(tree.getNodeHeight(i), tree.getNodeHeight(CiP)); |
| 152 |
oldRange = tree.getNodeHeight(PiP) - oldMinAge; |
| 153 |
q = newRange / Math.abs(oldRange); |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
if(branchMap.get(PiP.getNumber())!=branchMap.get(CiP.getNumber())){ |
| 158 |
q *= 0.5; |
| 159 |
} |
| 160 |
|
| 161 |
if(branchMap.get(jP.getNumber())!=branchMap.get(j.getNumber())){ |
| 162 |
q *= 2; |
| 163 |
} |
| 164 |
|
| 165 |
tree.beginTreeEdit(); |
| 166 |
|
| 167 |
|
| 168 |
if (j == tree.getRoot()) { |
| 169 |
|
| 170 |
|
| 171 |
tree.removeChild(iP, CiP); |
| 172 |
tree.removeChild(PiP, iP); |
| 173 |
|
| 174 |
|
| 175 |
tree.addChild(iP, j); |
| 176 |
tree.addChild(PiP, CiP); |
| 177 |
|
| 178 |
|
| 179 |
tree.setRoot(iP); |
| 180 |
|
| 181 |
} else if (iP == tree.getRoot()) { |
| 182 |
|
| 183 |
|
| 184 |
tree.removeChild(jP, j); |
| 185 |
tree.removeChild(iP, CiP); |
| 186 |
|
| 187 |
|
| 188 |
tree.addChild(iP, j); |
| 189 |
tree.addChild(jP, iP); |
| 190 |
|
| 191 |
|
| 192 |
tree.setRoot(CiP); |
| 193 |
|
| 194 |
} else { |
| 195 |
|
| 196 |
tree.removeChild(jP, j); |
| 197 |
tree.removeChild(iP, CiP); |
| 198 |
tree.removeChild(PiP, iP); |
| 199 |
|
| 200 |
|
| 201 |
tree.addChild(iP, j); |
| 202 |
tree.addChild(jP, iP); |
| 203 |
tree.addChild(PiP, CiP); |
| 204 |
} |
| 205 |
|
| 206 |
tree.setNodeHeight(iP, newAge); |
| 207 |
|
| 208 |
tree.endTreeEdit(); |
| 209 |
|
| 210 |
|
| 211 |
logq = Math.log(q); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
if(MathUtils.nextInt(2)==0){ |
| 216 |
branchMap.set(iP.getNumber(), branchMap.get(jP.getNumber()), true); |
| 217 |
} else { |
| 218 |
branchMap.set(iP.getNumber(), branchMap.get(j.getNumber()), true); |
| 219 |
} |
| 220 |
|
| 221 |
if(DEBUG){ |
| 222 |
c2cLikelihood.getTreeModel().checkPartitions(); |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
public String getPerformanceSuggestion() { |
| 228 |
return "Not implemented"; |
| 229 |
} |
| 230 |
|
| 231 |
private boolean eligibleForMove(NodeRef node, TreeModel tree, BranchMapModel branchMap){ |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
return (!tree.isRoot(node) && branchMap.get(tree.getParent(node).getNumber()) |
| 236 |
!=branchMap.get(node.getNumber())); |
| 237 |
} |
| 238 |
|
| 239 |
public String getOperatorName() { |
| 240 |
return TRANSMISSION_WILSON_BALDING_B + " (" + c2cLikelihood.getTreeModel().getId() +")"; |
| 241 |
} |
| 242 |
|
| 243 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 244 |
|
| 245 |
public static final String RESAMPLE_INFECTION_TIMES = "resampleInfectionTimes"; |
| 246 |
|
| 247 |
public String getParserName() { |
| 248 |
return TRANSMISSION_WILSON_BALDING_B; |
| 249 |
} |
| 250 |
|
| 251 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 252 |
final CaseToCaseTreeLikelihood c2cL |
| 253 |
= (CaseToCaseTreeLikelihood) xo.getChild(CaseToCaseTreeLikelihood.class); |
| 254 |
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 255 |
|
| 256 |
boolean resampleInfectionTimes = false; |
| 257 |
|
| 258 |
if(xo.hasAttribute(RESAMPLE_INFECTION_TIMES)) { |
| 259 |
resampleInfectionTimes = xo.getBooleanAttribute(RESAMPLE_INFECTION_TIMES); |
| 260 |
} |
| 261 |
|
| 262 |
return new TransmissionWilsonBaldingB(c2cL, weight, resampleInfectionTimes); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
public String getParserDescription(){ |
| 270 |
return "This element represents a Wilson-Balding move operator, such that the transplantation of the " + |
| 271 |
"phylogenetic subtree is also transplantation of a transmission subtree."; |
| 272 |
} |
| 273 |
|
| 274 |
public Class getReturnType(){ |
| 275 |
return TransmissionWilsonBaldingB.class; |
| 276 |
} |
| 277 |
|
| 278 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 279 |
return rules; |
| 280 |
} |
| 281 |
|
| 282 |
private final XMLSyntaxRule[] rules;{ |
| 283 |
rules = new XMLSyntaxRule[]{ |
| 284 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 285 |
AttributeRule.newBooleanRule(RESAMPLE_INFECTION_TIMES, true), |
| 286 |
new ElementRule(CaseToCaseTreeLikelihood.class) |
| 287 |
}; |
| 288 |
} |
| 289 |
}; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
} |