| 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 |
import java.util.ArrayList; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class TransmissionExchangeOperatorA extends AbstractTreeOperator { |
| 52 |
|
| 53 |
private final CaseToCaseTreeLikelihood c2cLikelihood; |
| 54 |
public static final String TRANSMISSION_EXCHANGE_OPERATOR_A = "transmissionExchangeOperatorA"; |
| 55 |
|
| 56 |
private final boolean resampleInfectionTimes; |
| 57 |
|
| 58 |
public TransmissionExchangeOperatorA(CaseToCaseTreeLikelihood c2cLikelihood, double weight, |
| 59 |
boolean resampleInfectionTimes) { |
| 60 |
this.c2cLikelihood = c2cLikelihood; |
| 61 |
setWeight(weight); |
| 62 |
|
| 63 |
this.resampleInfectionTimes = resampleInfectionTimes; |
| 64 |
} |
| 65 |
|
| 66 |
public double doOperation() { |
| 67 |
TreeModel tree = c2cLikelihood.getTreeModel(); |
| 68 |
|
| 69 |
double hr = exchange(); |
| 70 |
|
| 71 |
final int tipCount = tree.getExternalNodeCount(); |
| 72 |
|
| 73 |
assert tree.getExternalNodeCount() == tipCount : "Lost some tips"; |
| 74 |
|
| 75 |
return hr; |
| 76 |
} |
| 77 |
|
| 78 |
public double exchange() { |
| 79 |
|
| 80 |
TreeModel tree = c2cLikelihood.getTreeModel(); |
| 81 |
|
| 82 |
final int nodeCount = tree.getNodeCount(); |
| 83 |
final NodeRef root = tree.getRoot(); |
| 84 |
|
| 85 |
NodeRef i = root; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
while(root == i){ |
| 90 |
i = tree.getNode(MathUtils.nextInt(nodeCount)); |
| 91 |
} |
| 92 |
|
| 93 |
ArrayList<NodeRef> candidates = getPossibleExchanges(tree, i); |
| 94 |
|
| 95 |
int candidateCount = candidates.size(); |
| 96 |
|
| 97 |
if(candidateCount==0){ |
| 98 |
|
| 99 |
return Double.NEGATIVE_INFINITY; |
| 100 |
} |
| 101 |
|
| 102 |
NodeRef j = candidates.get(MathUtils.nextInt(candidates.size())); |
| 103 |
|
| 104 |
int jFirstCandidateCount = getPossibleExchanges(tree, j).size(); |
| 105 |
|
| 106 |
double HRDenom = (1/((double)candidateCount)) + (1/((double)jFirstCandidateCount)); |
| 107 |
|
| 108 |
NodeRef iP = tree.getParent(i); |
| 109 |
NodeRef jP = tree.getParent(j); |
| 110 |
|
| 111 |
if(resampleInfectionTimes){ |
| 112 |
BranchMapModel branchMap = c2cLikelihood.getBranchMap(); |
| 113 |
|
| 114 |
AbstractCase iCase = branchMap.get(i.getNumber()); |
| 115 |
AbstractCase jCase = branchMap.get(j.getNumber()); |
| 116 |
AbstractCase parentCase = branchMap.get(iP.getNumber()); |
| 117 |
|
| 118 |
if(iCase!=parentCase){ |
| 119 |
iCase.setInfectionBranchPosition(MathUtils.nextDouble()); |
| 120 |
} |
| 121 |
|
| 122 |
if(jCase!=parentCase){ |
| 123 |
jCase.setInfectionBranchPosition(MathUtils.nextDouble()); |
| 124 |
} |
| 125 |
|
| 126 |
} |
| 127 |
|
| 128 |
exchangeNodes(tree, i, j, iP, jP); |
| 129 |
|
| 130 |
ArrayList<NodeRef> reverseCandidatesIfirst = getPossibleExchanges(tree, i); |
| 131 |
ArrayList<NodeRef> reverseCandidatesJfirst = getPossibleExchanges(tree, j); |
| 132 |
|
| 133 |
double HRNum = (1/(double)reverseCandidatesIfirst.size()) + (1/(double)reverseCandidatesJfirst.size()); |
| 134 |
|
| 135 |
return Math.log(HRNum/HRDenom); |
| 136 |
|
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public ArrayList<NodeRef> getPossibleExchanges(TreeModel tree, NodeRef node){ |
| 143 |
ArrayList<NodeRef> out = new ArrayList<NodeRef>(); |
| 144 |
NodeRef parent = tree.getParent(node); |
| 145 |
if(parent==null){ |
| 146 |
throw new RuntimeException("Can't exchange the root node"); |
| 147 |
} |
| 148 |
Integer[] possibleParentSwaps = c2cLikelihood.getTreeModel().samePartitionElement(parent); |
| 149 |
for(Integer index: possibleParentSwaps){ |
| 150 |
NodeRef newParent = tree.getNode(index); |
| 151 |
if(!tree.isExternal(newParent) && newParent!=parent){ |
| 152 |
for(int i=0; i<2; i++){ |
| 153 |
NodeRef candidate = tree.getChild(newParent, i); |
| 154 |
if(candidate != parent |
| 155 |
&& node != newParent |
| 156 |
&& tree.getNodeHeight(candidate) < tree.getNodeHeight(parent) |
| 157 |
&& tree.getNodeHeight(node) < tree.getNodeHeight(newParent)){ |
| 158 |
if(out.contains(candidate) || candidate==node){ |
| 159 |
throw new RuntimeException("Adding a candidate that already exists in the list or" + |
| 160 |
" the node itself"); |
| 161 |
} |
| 162 |
out.add(candidate); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
return out; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
public String getPerformanceSuggestion() { |
| 173 |
return "Not implemented"; |
| 174 |
} |
| 175 |
|
| 176 |
public String getOperatorName() { |
| 177 |
return "Transmission tree exchange operator type A (" + c2cLikelihood.getTreeModel().getId() +")"; |
| 178 |
} |
| 179 |
|
| 180 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 181 |
|
| 182 |
public static final String RESAMPLE_INFECTION_TIMES = "resampleInfectionTimes"; |
| 183 |
|
| 184 |
public String getParserName() { |
| 185 |
return TRANSMISSION_EXCHANGE_OPERATOR_A; |
| 186 |
} |
| 187 |
|
| 188 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 189 |
final CaseToCaseTreeLikelihood c2cL |
| 190 |
= (CaseToCaseTreeLikelihood) xo.getChild(CaseToCaseTreeLikelihood.class); |
| 191 |
if (c2cL.getTreeModel().getExternalNodeCount() <= 2) { |
| 192 |
throw new XMLParseException("Tree with fewer than 3 taxa"); |
| 193 |
} |
| 194 |
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 195 |
|
| 196 |
boolean resampleInfectionTimes = false; |
| 197 |
|
| 198 |
if(xo.hasAttribute(RESAMPLE_INFECTION_TIMES)) { |
| 199 |
resampleInfectionTimes = xo.getBooleanAttribute(RESAMPLE_INFECTION_TIMES); |
| 200 |
} |
| 201 |
|
| 202 |
return new TransmissionExchangeOperatorA(c2cL, weight, resampleInfectionTimes); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public String getParserDescription(){ |
| 210 |
return "This element represents a exchange operator, swapping two random subtrees in such a way that the" + |
| 211 |
"transmission tree is unaffected."; |
| 212 |
} |
| 213 |
|
| 214 |
public Class getReturnType(){ |
| 215 |
return TransmissionExchangeOperatorA.class; |
| 216 |
} |
| 217 |
|
| 218 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 219 |
return rules; |
| 220 |
} |
| 221 |
|
| 222 |
private final XMLSyntaxRule[] rules;{ |
| 223 |
rules = new XMLSyntaxRule[]{ |
| 224 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 225 |
AttributeRule.newBooleanRule(RESAMPLE_INFECTION_TIMES, true), |
| 226 |
new ElementRule(CaseToCaseTreeLikelihood.class) |
| 227 |
}; |
| 228 |
} |
| 229 |
}; |
| 230 |
|
| 231 |
} |