| 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.operators.ExchangeOperator; |
| 34 |
import dr.evomodel.operators.SubtreeSlideOperator; |
| 35 |
import dr.evomodel.operators.WilsonBalding; |
| 36 |
import dr.evomodel.tree.TreeModel; |
| 37 |
import dr.inference.operators.AbstractAdaptableOperator; |
| 38 |
import dr.inference.operators.AdaptableMCMCOperator; |
| 39 |
import dr.inference.operators.AdaptationMode; |
| 40 |
import dr.xml.*; |
| 41 |
|
| 42 |
import java.util.ArrayList; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public class TransmissionTreeOperator extends AbstractAdaptableOperator { |
| 53 |
|
| 54 |
private final CaseToCaseTreeLikelihood c2cLikelihood; |
| 55 |
private final AbstractTreeOperator innerOperator; |
| 56 |
public static final String TRANSMISSION_TREE_OPERATOR = "transmissionTreeOperator"; |
| 57 |
|
| 58 |
public TransmissionTreeOperator(CaseToCaseTreeLikelihood c2cLikelihood, AbstractTreeOperator operator, |
| 59 |
AdaptationMode mode) { |
| 60 |
super(mode); |
| 61 |
this.c2cLikelihood = c2cLikelihood; |
| 62 |
this.innerOperator = operator; |
| 63 |
this.setWeight(innerOperator.getWeight()); |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
throw new RuntimeException("TransmissionTreeOperator only works on non-extended tree paintings"); |
| 68 |
} |
| 69 |
|
| 70 |
public TransmissionTreeOperator(CaseToCaseTreeLikelihood c2cLikelihood, AbstractTreeOperator operator) { |
| 71 |
this(c2cLikelihood,operator, AdaptationMode.ADAPTATION_OFF); |
| 72 |
} |
| 73 |
|
| 74 |
public double doOperation() { |
| 75 |
TreeModel tree = c2cLikelihood.getTreeModel(); |
| 76 |
BranchMapModel branchMap = c2cLikelihood.getBranchMap(); |
| 77 |
AbstractCase[] newBranchMap = branchMap.getArrayCopy(); |
| 78 |
int[] oldParents = getParentsArray(tree); |
| 79 |
double[] oldHeights = getHeightsArray(tree); |
| 80 |
double hr = innerOperator.doOperation(); |
| 81 |
int[] newParents = getParentsArray(tree); |
| 82 |
ArrayList<Integer> changedNodes = new ArrayList<Integer>(); |
| 83 |
for(int i=0; i<tree.getNodeCount(); i++){ |
| 84 |
if(oldParents[i]!=newParents[i]){ |
| 85 |
changedNodes.add(i); |
| 86 |
} |
| 87 |
} |
| 88 |
if(changedNodes.size()!=0){ |
| 89 |
if(innerOperator instanceof ExchangeOperator){ |
| 90 |
|
| 91 |
AbstractCase[] nodePaintings = new AbstractCase[2]; |
| 92 |
AbstractCase[] parentPaintings = new AbstractCase[2]; |
| 93 |
for(int i=0; i<2; i++){ |
| 94 |
nodePaintings[i] = branchMap.get(changedNodes.get(i)); |
| 95 |
parentPaintings[i] = branchMap.get(oldParents[changedNodes.get(i)]); |
| 96 |
} |
| 97 |
if(nodePaintings[0]==parentPaintings[0] || nodePaintings[1]==parentPaintings[1]){ |
| 98 |
|
| 99 |
for(int i=0; i<2; i++){ |
| 100 |
paintUp(tree, nodePaintings[1-i], nodePaintings[i], branchMap, newBranchMap, |
| 101 |
tree.getNode(changedNodes.get(i)).getNumber(),newParents); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
} else if(innerOperator instanceof SubtreeSlideOperator || innerOperator instanceof WilsonBalding){ |
| 106 |
|
| 107 |
int movedNode = -1; |
| 108 |
int oldChild = -1; |
| 109 |
int newChild = -1; |
| 110 |
for(int i=0; i<tree.getNodeCount(); i++){ |
| 111 |
|
| 112 |
|
| 113 |
if(tree.getNodeHeight(tree.getNode(i))!=oldHeights[i]){ |
| 114 |
movedNode = i; |
| 115 |
break; |
| 116 |
} |
| 117 |
} |
| 118 |
for(int j:changedNodes){ |
| 119 |
if(j!=movedNode){ |
| 120 |
if(tree.getParent(tree.getNode(j))==tree.getNode(movedNode)){ |
| 121 |
newChild = j; |
| 122 |
} else { |
| 123 |
oldChild = j; |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
if(movedNode == -1 || oldChild == -1 || newChild == -1) { |
| 128 |
|
| 129 |
throw new RuntimeException("Failed to establish relationship between relocated node and others"); |
| 130 |
} |
| 131 |
NodeRef movedNodeObject = tree.getNode(movedNode); |
| 132 |
NodeRef oldChildObject = tree.getNode(oldChild); |
| 133 |
NodeRef newChildObject = tree.getNode(newChild); |
| 134 |
int otherChild = -1; |
| 135 |
NodeRef otherChildObject; |
| 136 |
|
| 137 |
for(int i=0; i<tree.getChildCount(movedNodeObject);i++){ |
| 138 |
if(tree.getChild(movedNodeObject,i)!=newChildObject){ |
| 139 |
otherChildObject = tree.getChild(movedNodeObject,i); |
| 140 |
otherChild = otherChildObject.getNumber(); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
if(branchMap.get(otherChild)!=branchMap.get(movedNode)){ |
| 146 |
newBranchMap[movedNode]=branchMap.get(newChild); |
| 147 |
} else { |
| 148 |
|
| 149 |
|
| 150 |
paintUp(tree, branchMap.get(movedNode),branchMap.get(oldChild),branchMap,newBranchMap,oldChild, |
| 151 |
oldParents); |
| 152 |
|
| 153 |
newBranchMap[movedNode]=branchMap.get(movedNode); |
| 154 |
branchMap.setAll(newBranchMap, true); |
| 155 |
|
| 156 |
|
| 157 |
paintUp(tree, branchMap.get(newChild), branchMap.get(movedNode), branchMap, newBranchMap, movedNode, |
| 158 |
newParents); |
| 159 |
} |
| 160 |
} else { |
| 161 |
|
| 162 |
throw new UnsupportedOperationException("Operator class "+innerOperator.getOperatorName()+" not yet " + |
| 163 |
"supported"); |
| 164 |
} |
| 165 |
} |
| 166 |
branchMap.setAll(newBranchMap, true); |
| 167 |
c2cLikelihood.makeDirty(); |
| 168 |
return hr; |
| 169 |
} |
| 170 |
|
| 171 |
private void paintUp(TreeModel tree, AbstractCase oldCase, AbstractCase newCase, BranchMapModel oldbranchMap, |
| 172 |
AbstractCase[] newBranchMap, int nodeNo, int[] parents){ |
| 173 |
if(parents[nodeNo]==-1){ |
| 174 |
return; |
| 175 |
} |
| 176 |
NodeRef newParent = tree.getNode(parents[nodeNo]); |
| 177 |
while(newParent!=null && oldbranchMap.get(newParent.getNumber())==oldCase){ |
| 178 |
newBranchMap[newParent.getNumber()]=newCase; |
| 179 |
newParent = parents[newParent.getNumber()]== -1 ? null : tree.getNode(parents[newParent.getNumber()]); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
private int[] getParentsArray(TreeModel tree){ |
| 184 |
int[] out = new int[tree.getNodeCount()]; |
| 185 |
for(int i=0; i<tree.getNodeCount(); i++){ |
| 186 |
if(tree.getNode(i)==tree.getRoot()){ |
| 187 |
out[i]=-1; |
| 188 |
} else { |
| 189 |
out[i]=tree.getParent(tree.getNode(i)).getNumber(); |
| 190 |
} |
| 191 |
} |
| 192 |
return out; |
| 193 |
} |
| 194 |
|
| 195 |
private double[] getHeightsArray(TreeModel tree){ |
| 196 |
double[] out = new double[tree.getNodeCount()]; |
| 197 |
for(int i=0; i<tree.getNodeCount(); i++){ |
| 198 |
out[i]=tree.getNodeHeight(tree.getNode(i)); |
| 199 |
} |
| 200 |
return out; |
| 201 |
} |
| 202 |
|
| 203 |
public double getAdaptableParameter() { |
| 204 |
if(innerOperator instanceof AdaptableMCMCOperator){ |
| 205 |
return ((AdaptableMCMCOperator) innerOperator).getAdaptableParameter(); |
| 206 |
} |
| 207 |
throw new IllegalArgumentException(); |
| 208 |
} |
| 209 |
|
| 210 |
public void setAdaptableParameter(double value) { |
| 211 |
if(innerOperator instanceof AdaptableMCMCOperator){ |
| 212 |
((AdaptableMCMCOperator) innerOperator).setAdaptableParameter(value); |
| 213 |
return; |
| 214 |
} |
| 215 |
throw new IllegalArgumentException(); |
| 216 |
} |
| 217 |
|
| 218 |
public double getRawParameter() { |
| 219 |
if(innerOperator instanceof AdaptableMCMCOperator){ |
| 220 |
return ((AdaptableMCMCOperator) innerOperator).getRawParameter(); |
| 221 |
} |
| 222 |
throw new IllegalArgumentException(); |
| 223 |
} |
| 224 |
|
| 225 |
public String getAdaptableParameterName() { |
| 226 |
if(innerOperator instanceof AdaptableMCMCOperator) { |
| 227 |
return ((AdaptableMCMCOperator) innerOperator).getAdaptableParameterName(); |
| 228 |
} |
| 229 |
return ""; |
| 230 |
} |
| 231 |
|
| 232 |
public String getOperatorName() { |
| 233 |
return TRANSMISSION_TREE_OPERATOR; |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 238 |
|
| 239 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 240 |
CaseToCaseTreeLikelihood c2cL |
| 241 |
= (CaseToCaseTreeLikelihood)xo.getChild(CaseToCaseTreeLikelihood.class); |
| 242 |
AbstractTreeOperator treeOp = (AbstractTreeOperator)xo.getChild(AbstractTreeOperator.class); |
| 243 |
|
| 244 |
AdaptationMode mode = AdaptationMode.ADAPTATION_OFF; |
| 245 |
|
| 246 |
if(treeOp instanceof AdaptableMCMCOperator){ |
| 247 |
mode = ((AdaptableMCMCOperator) treeOp).getMode(); |
| 248 |
} |
| 249 |
|
| 250 |
return new TransmissionTreeOperator(c2cL,treeOp,mode); |
| 251 |
|
| 252 |
} |
| 253 |
|
| 254 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 255 |
return rules; |
| 256 |
} |
| 257 |
|
| 258 |
public String getParserDescription() { |
| 259 |
return "Performs a tree move then readjusts the transmission network in order to maintain its integrity."; |
| 260 |
} |
| 261 |
|
| 262 |
public Class getReturnType() { |
| 263 |
return TransmissionTreeOperator.class; |
| 264 |
} |
| 265 |
|
| 266 |
public String getParserName() { |
| 267 |
return TRANSMISSION_TREE_OPERATOR; |
| 268 |
} |
| 269 |
|
| 270 |
private final XMLSyntaxRule[] rules = { |
| 271 |
new ElementRule(CaseToCaseTreeLikelihood.class, "The transmission network likelihood element"), |
| 272 |
new ElementRule(AbstractTreeOperator.class, "A phylogenetic tree operator.") |
| 273 |
}; |
| 274 |
}; |
| 275 |
} |