| 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.*; |
| 30 |
import dr.inference.operators.MCMCOperator; |
| 31 |
import dr.inference.operators.SimpleMCMCOperator; |
| 32 |
import dr.math.MathUtils; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class InfectionBranchMovementOperator extends SimpleMCMCOperator{ |
| 42 |
|
| 43 |
public static final String INFECTION_BRANCH_MOVEMENT_OPERATOR = "infectionBranchMovementOperator"; |
| 44 |
private CaseToCaseTreeLikelihood c2cLikelihood; |
| 45 |
|
| 46 |
private final boolean resampleInfectionTimes; |
| 47 |
|
| 48 |
private static final boolean DEBUG = false; |
| 49 |
|
| 50 |
public InfectionBranchMovementOperator(CaseToCaseTreeLikelihood c2cLikelihood, double weight, |
| 51 |
boolean resampleInfectionTimes){ |
| 52 |
this.c2cLikelihood = c2cLikelihood; |
| 53 |
setWeight(weight); |
| 54 |
|
| 55 |
this.resampleInfectionTimes = resampleInfectionTimes; |
| 56 |
} |
| 57 |
|
| 58 |
public String getOperatorName(){ |
| 59 |
return INFECTION_BRANCH_MOVEMENT_OPERATOR; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public double doOperation() { |
| 66 |
|
| 67 |
PartitionedTreeModel tree = c2cLikelihood.getTreeModel(); |
| 68 |
BranchMapModel branchMap = c2cLikelihood.getBranchMap(); |
| 69 |
|
| 70 |
|
| 71 |
int caseIndexToAdjust = MathUtils.nextInt(c2cLikelihood.getOutbreak().size()); |
| 72 |
|
| 73 |
AbstractCase aCase = c2cLikelihood.getOutbreak().getCase(caseIndexToAdjust); |
| 74 |
|
| 75 |
|
| 76 |
while(branchMap.get(tree.getRoot().getNumber()) == aCase || !aCase.wasEverInfected()){ |
| 77 |
caseIndexToAdjust = MathUtils.nextInt(c2cLikelihood.getOutbreak().size()); |
| 78 |
aCase = c2cLikelihood.getOutbreak().getCase(caseIndexToAdjust); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
NodeRef node = tree.getEarliestNodeInElement(aCase); |
| 83 |
|
| 84 |
double hr = adjustTree(tree, node); |
| 85 |
|
| 86 |
return hr; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
private double adjustTree(PartitionedTreeModel tree, NodeRef node) { |
| 91 |
double out; |
| 92 |
|
| 93 |
BranchMapModel map = tree.getBranchMap(); |
| 94 |
|
| 95 |
AbstractCase infectedCase = map.get(node.getNumber()); |
| 96 |
AbstractCase infectorCase = map.get(tree.getParent(node).getNumber()); |
| 97 |
|
| 98 |
NodeRef infectedMRCA = tree.caseMRCA(infectedCase); |
| 99 |
|
| 100 |
boolean downIsPossible = node != infectedMRCA; |
| 101 |
boolean upIsPossible = !(tree.isRootBlockedBy(infectedCase, infectorCase) |
| 102 |
&& tree.isAncestral(tree.getParent(node))); |
| 103 |
|
| 104 |
if(upIsPossible && downIsPossible){ |
| 105 |
out = MathUtils.nextBoolean() ? moveUp(tree, node) : moveDown(tree, node); |
| 106 |
} else if(upIsPossible){ |
| 107 |
out = moveUp(tree, node); |
| 108 |
} else if(downIsPossible){ |
| 109 |
out = moveDown(tree, node); |
| 110 |
} else { |
| 111 |
|
| 112 |
return Double.NEGATIVE_INFINITY; |
| 113 |
} |
| 114 |
|
| 115 |
if(DEBUG){ |
| 116 |
c2cLikelihood.getTreeModel().checkPartitions(); |
| 117 |
} |
| 118 |
return out; |
| 119 |
} |
| 120 |
|
| 121 |
private double moveUp(PartitionedTreeModel tree, NodeRef node){ |
| 122 |
BranchMapModel map = tree.getBranchMap(); |
| 123 |
|
| 124 |
AbstractCase infectedCase = map.get(node.getNumber()); |
| 125 |
|
| 126 |
AbstractCase[] newMap = map.getArrayCopy(); |
| 127 |
|
| 128 |
NodeRef parent = tree.getParent(node); |
| 129 |
|
| 130 |
double hr = 0; |
| 131 |
|
| 132 |
NodeRef sibling = node; |
| 133 |
for(int i=0; i<tree.getChildCount(parent); i++){ |
| 134 |
if(tree.getChild(parent, i)!=node){ |
| 135 |
sibling = tree.getChild(parent, i); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
AbstractCase infectorCase = map.get(parent.getNumber()); |
| 140 |
|
| 141 |
NodeRef infectedMRCA = tree.caseMRCA(infectedCase); |
| 142 |
NodeRef infectorMRCA = tree.caseMRCA(infectorCase); |
| 143 |
|
| 144 |
if(c2cLikelihood.getTreeModel().isAncestral(parent)){ |
| 145 |
|
| 146 |
if(resampleInfectionTimes){ |
| 147 |
infectorCase.setInfectionBranchPosition(MathUtils.nextDouble()); |
| 148 |
} |
| 149 |
|
| 150 |
NodeRef grandparent = tree.getParent(parent); |
| 151 |
if(grandparent!=null && map.get(grandparent.getNumber())==map.get(parent.getNumber())){ |
| 152 |
for(Integer ancestor: c2cLikelihood.getTreeModel().samePartitionElementUpTree(parent)){ |
| 153 |
newMap[ancestor] = map.get(node.getNumber()); |
| 154 |
} |
| 155 |
newMap[grandparent.getNumber()]=map.get(node.getNumber()); |
| 156 |
} |
| 157 |
|
| 158 |
hr += node == infectedMRCA ? Math.log(0.5) : 0; |
| 159 |
|
| 160 |
} else { |
| 161 |
if(map.get(sibling.getNumber())==map.get(parent.getNumber())){ |
| 162 |
for(Integer descendant: c2cLikelihood.getTreeModel().samePartitionElementDownTree(sibling)){ |
| 163 |
newMap[descendant]=map.get(node.getNumber()); |
| 164 |
} |
| 165 |
newMap[sibling.getNumber()]=map.get(node.getNumber()); |
| 166 |
} |
| 167 |
|
| 168 |
hr += node == infectedMRCA ? Math.log(0.5) : 0; |
| 169 |
} |
| 170 |
newMap[parent.getNumber()]=map.get(node.getNumber()); |
| 171 |
map.setAll(newMap, false); |
| 172 |
|
| 173 |
|
| 174 |
if(c2cLikelihood.getTreeModel().isAncestral(parent)){ |
| 175 |
hr += sibling == infectorMRCA ? Math.log(2) : 0; |
| 176 |
} else { |
| 177 |
NodeRef grandparent = tree.getParent(parent); |
| 178 |
|
| 179 |
hr += tree.isRootBlockedBy(infectedCase, infectorCase) |
| 180 |
&& tree.isAncestral(grandparent) ? Math.log(2) : 0; |
| 181 |
|
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
if(resampleInfectionTimes){ |
| 186 |
infectedCase.setInfectionBranchPosition(MathUtils.nextDouble()); |
| 187 |
} |
| 188 |
|
| 189 |
return hr; |
| 190 |
} |
| 191 |
|
| 192 |
private double moveDown(PartitionedTreeModel tree, NodeRef node){ |
| 193 |
BranchMapModel map = tree.getBranchMap(); |
| 194 |
|
| 195 |
AbstractCase infectedCase = map.get(node.getNumber()); |
| 196 |
AbstractCase infectorCase = map.get(tree.getParent(node).getNumber()); |
| 197 |
|
| 198 |
AbstractCase[] newMap = map.getArrayCopy(); |
| 199 |
|
| 200 |
double out = 0; |
| 201 |
|
| 202 |
NodeRef parent = tree.getParent(node); |
| 203 |
|
| 204 |
NodeRef infectedMRCA = tree.caseMRCA(infectedCase); |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
for(int i=0; i<tree.getChildCount(node); i++){ |
| 209 |
NodeRef child = tree.getChild(node, i); |
| 210 |
if(!c2cLikelihood.getTreeModel().isAncestral(child)){ |
| 211 |
for(Integer descendant: c2cLikelihood.getTreeModel().samePartitionElementDownTree(child)){ |
| 212 |
newMap[descendant]=map.get(parent.getNumber()); |
| 213 |
} |
| 214 |
newMap[child.getNumber()]=map.get(parent.getNumber()); |
| 215 |
} else if(child == infectedMRCA && map.get(child.getNumber())==map.get(node.getNumber())){ |
| 216 |
|
| 217 |
out += Math.log(2); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
out += tree.isRootBlockedBy(infectedCase, infectorCase) |
| 223 |
&& tree.isAncestral(parent) ? Math.log(0.5) : 0; |
| 224 |
|
| 225 |
if(resampleInfectionTimes){ |
| 226 |
infectedCase.setInfectionBranchPosition(MathUtils.nextDouble()); |
| 227 |
} |
| 228 |
|
| 229 |
newMap[node.getNumber()]=map.get(parent.getNumber()); |
| 230 |
map.setAll(newMap, false); |
| 231 |
|
| 232 |
return out; |
| 233 |
} |
| 234 |
|
| 235 |
public String getPerformanceSuggestion(){ |
| 236 |
return "Not implemented"; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser(){ |
| 242 |
|
| 243 |
public static final String RESAMPLE_INFECTION_TIMES = "resampleInfectionTimes"; |
| 244 |
|
| 245 |
public String getParserName(){ |
| 246 |
return INFECTION_BRANCH_MOVEMENT_OPERATOR; |
| 247 |
} |
| 248 |
|
| 249 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 250 |
|
| 251 |
CaseToCaseTreeLikelihood ftLikelihood = |
| 252 |
(CaseToCaseTreeLikelihood) xo.getChild(CaseToCaseTreeLikelihood.class); |
| 253 |
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 254 |
|
| 255 |
boolean resampleInfectionTimes = false; |
| 256 |
|
| 257 |
if(xo.hasAttribute(RESAMPLE_INFECTION_TIMES)) { |
| 258 |
resampleInfectionTimes = xo.getBooleanAttribute(RESAMPLE_INFECTION_TIMES); |
| 259 |
} |
| 260 |
|
| 261 |
return new InfectionBranchMovementOperator(ftLikelihood, weight, resampleInfectionTimes); |
| 262 |
} |
| 263 |
|
| 264 |
public String getParserDescription(){ |
| 265 |
return "This operator switches the painting of a random eligible internal node from the painting of one " + |
| 266 |
"of its children to the painting of the other"; |
| 267 |
} |
| 268 |
|
| 269 |
public Class getReturnType() { |
| 270 |
return InfectionBranchMovementOperator.class; |
| 271 |
} |
| 272 |
|
| 273 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 274 |
return rules; |
| 275 |
} |
| 276 |
|
| 277 |
private final XMLSyntaxRule[] rules = { |
| 278 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 279 |
AttributeRule.newBooleanRule(RESAMPLE_INFECTION_TIMES, true), |
| 280 |
new ElementRule(CaseToCaseTreeLikelihood.class), |
| 281 |
}; |
| 282 |
}; |
| 283 |
} |