Debian Codegraph @

src/dr/evomodel/epidemiology/casetocase/operators/TransmissionWilsonBaldingA.java

raw symbols SCIP
1 /*
2 * TransmissionWilsonBaldingA.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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.epidemiology.casetocase.PartitionedTreeModel;
33 import dr.evomodel.operators.AbstractTreeOperator;
34 import dr.evomodel.tree.TreeModel;
35 import dr.inference.operators.MCMCOperator;
36 import dr.math.MathUtils;
37 import dr.xml.*;
38
39 import java.util.ArrayList;
40 import java.util.HashSet;
41
42 /**
43 * Implements the Wilson-Balding branch swapping move if it does not change the transmission tree.
44 *
45 * @author Matthew Hall
46 */
47
48 public class TransmissionWilsonBaldingA extends AbstractTreeOperator {
49
50 private final CaseToCaseTreeLikelihood c2cLikelihood;
51 public static final String TRANSMISSION_WILSON_BALDING_A = "transmissionWilsonBaldingA";
52 private double logq;
53 private static final boolean DEBUG = false;
54 private final int tipCount;
55
56 private final boolean resampleInfectionTimes;
57
58 public TransmissionWilsonBaldingA(CaseToCaseTreeLikelihood c2cLikelihood, double weight,
59 boolean resampleInfectionTimes) {
60 this.c2cLikelihood = c2cLikelihood;
61 setWeight(weight);
62 tipCount = c2cLikelihood.getTreeModel().getExternalNodeCount();
63
64 this.resampleInfectionTimes = resampleInfectionTimes;
65 }
66
67 public double doOperation() {
68 proposeTree();
69 if (c2cLikelihood.getTreeModel().getExternalNodeCount() != tipCount) {
70 int newCount = c2cLikelihood.getTreeModel().getExternalNodeCount();
71 throw new RuntimeException("Lost some tips in modified SPR! (" +
72 tipCount + "-> " + newCount + ")");
73 }
74
75 return logq;
76 }
77
78 public void proposeTree() {
79 PartitionedTreeModel tree = c2cLikelihood.getTreeModel();
80 BranchMapModel branchMap = c2cLikelihood.getBranchMap();
81 NodeRef i;
82 double oldMinAge, newMinAge, newRange, oldRange, newAge, q;
83 // choose a random node avoiding root, and nodes that are ineligible for this move because they have nowhere to
84 // go
85
86 ArrayList<NodeRef> eligibleNodes = getEligibleNodes(tree, branchMap);
87
88 i = eligibleNodes.get(MathUtils.nextInt(eligibleNodes.size()));
89
90 double eligibleNodeCount = eligibleNodes.size();
91
92 final NodeRef iP = tree.getParent(i);
93
94 Integer[] sameElements = tree.samePartitionElement(iP);
95
96 HashSet<Integer> possibleDestinations = new HashSet<Integer>();
97 // we can insert the node above OR BELOW any node in the same partition
98 for (Integer sameElement : sameElements) {
99 possibleDestinations.add(sameElement);
100 if (!tree.isExternal(tree.getNode(sameElement))) {
101 possibleDestinations.add(tree.getChild(tree.getNode(sameElement), 0).getNumber());
102 possibleDestinations.add(tree.getChild(tree.getNode(sameElement), 1).getNumber());
103 }
104 }
105 Integer[] pd = possibleDestinations.toArray(new Integer[possibleDestinations.size()]);
106
107 NodeRef j = tree.getNode(pd[MathUtils.nextInt(pd.length)]);
108 NodeRef jP = tree.getParent(j);
109
110 while ((jP != null && (tree.getNodeHeight(jP) <= tree.getNodeHeight(i))) || (i == j)) {
111 j = tree.getNode(pd[MathUtils.nextInt(pd.length)]);
112 jP = tree.getParent(j);
113 }
114
115 if (iP == tree.getRoot() || j == tree.getRoot()) {
116 logq = Double.NEGATIVE_INFINITY;
117 return;
118 }
119
120 if (jP == iP || j == iP || jP == i){
121 logq = Double.NEGATIVE_INFINITY;
122 return;
123 }
124
125 final NodeRef CiP = getOtherChild(tree, iP, i);
126 NodeRef PiP = tree.getParent(iP);
127
128 if(resampleInfectionTimes) {
129
130 AbstractCase iCase = branchMap.get(i.getNumber());
131 AbstractCase iPCase = branchMap.get(iP.getNumber());
132 AbstractCase CiPCase = branchMap.get(CiP.getNumber());
133 AbstractCase PiPCase = null;
134 if(PiP!=null){
135 PiPCase = branchMap.get(PiP.getNumber());
136 }
137
138 // what happens on i's branch
139
140 if (iCase != iPCase) {
141 iCase.setInfectionBranchPosition(MathUtils.nextDouble());
142 }
143
144 // what happens between PiP and CiP
145 if (PiPCase == null || CiPCase != PiPCase) {
146 CiPCase.setInfectionBranchPosition(MathUtils.nextDouble());
147 }
148
149 // what happens between k and j
150
151
152 AbstractCase jCase = branchMap.get(j.getNumber());
153 AbstractCase kCase = branchMap.get(jP.getNumber());
154
155 if(iPCase != jCase && iPCase != kCase){
156 throw new RuntimeException("TWBA misbehaving.");
157 }
158
159 jCase.setInfectionBranchPosition(MathUtils.nextDouble());
160
161 }
162
163 newMinAge = Math.max(tree.getNodeHeight(i), tree.getNodeHeight(j));
164 newRange = tree.getNodeHeight(jP) - newMinAge;
165 newAge = newMinAge + (MathUtils.nextDouble() * newRange);
166 oldMinAge = Math.max(tree.getNodeHeight(i), tree.getNodeHeight(CiP));
167 oldRange = tree.getNodeHeight(PiP) - oldMinAge;
168 q = newRange / Math.abs(oldRange);
169
170 tree.beginTreeEdit();
171
172 if (j == tree.getRoot()) {
173
174 // 1. remove edges <iP, CiP>
175 tree.removeChild(iP, CiP);
176 tree.removeChild(PiP, iP);
177
178 // 2. add edges <k, iP>, <iP, j>, <PiP, CiP>
179 tree.addChild(iP, j);
180 tree.addChild(PiP, CiP);
181
182 // iP is the new root
183 tree.setRoot(iP);
184
185 } else if (iP == tree.getRoot()) {
186
187 // 1. remove edges <k, j>, <iP, CiP>, <PiP, iP>
188 tree.removeChild(jP, j);
189 tree.removeChild(iP, CiP);
190
191 // 2. add edges <k, iP>, <iP, j>, <PiP, CiP>
192 tree.addChild(iP, j);
193 tree.addChild(jP, iP);
194
195 //CiP is the new root
196 tree.setRoot(CiP);
197
198 } else {
199 // 1. remove edges <k, j>, <iP, CiP>, <PiP, iP>
200 tree.removeChild(jP, j);
201 tree.removeChild(iP, CiP);
202 tree.removeChild(PiP, iP);
203
204 // 2. add edges <k, iP>, <iP, j>, <PiP, CiP>
205 tree.addChild(iP, j);
206 tree.addChild(jP, iP);
207 tree.addChild(PiP, CiP);
208 }
209
210 tree.setNodeHeight(iP, newAge);
211
212 tree.endTreeEdit();
213
214 if(DEBUG){
215 c2cLikelihood.getTreeModel().checkPartitions();
216 }
217 logq = Math.log(q);
218
219 double reverseEligibleNodeCount = getEligibleNodes(tree, branchMap).size();
220
221 logq += Math.log(eligibleNodeCount/reverseEligibleNodeCount);
222
223 }
224
225 public String getPerformanceSuggestion() {
226 return "Not implemented";
227 }
228
229 private boolean eligibleForMove(NodeRef node, TreeModel tree, BranchMapModel branchMap){
230 // to be eligible for this move, the node's parent and grandparent, or parent and other child, must be in the
231 // same partition (so removing the parent has no effect on the transmission tree)
232 return (!tree.isRoot(node)
233 && ((tree.getParent(tree.getParent(node))!=null
234 && branchMap.get(tree.getParent(node).getNumber())
235 ==branchMap.get(tree.getParent(tree.getParent(node)).getNumber()))
236 || branchMap.get(tree.getParent(node).getNumber())==branchMap.get(getOtherChild(tree,
237 tree.getParent(node), node).getNumber())));
238 }
239
240 private ArrayList<NodeRef> getEligibleNodes(TreeModel tree, BranchMapModel branchMap){
241 ArrayList<NodeRef> out = new ArrayList<NodeRef>();
242 for(NodeRef node : tree.getNodes()){
243 if(eligibleForMove(node, tree, branchMap)){
244 out.add(node);
245 }
246 }
247 return out;
248 }
249
250 public String getOperatorName() {
251 return TRANSMISSION_WILSON_BALDING_A + " (" + c2cLikelihood.getTreeModel().getId() +")";
252 }
253
254 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
255
256 public static final String RESAMPLE_INFECTION_TIMES = "resampleInfectionTimes";
257
258 public String getParserName() {
259 return TRANSMISSION_WILSON_BALDING_A;
260 }
261
262 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
263 final CaseToCaseTreeLikelihood c2cL
264 = (CaseToCaseTreeLikelihood) xo.getChild(CaseToCaseTreeLikelihood.class);
265 final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
266
267 boolean resampleInfectionTimes = false;
268
269 if(xo.hasAttribute(RESAMPLE_INFECTION_TIMES)) {
270 resampleInfectionTimes = xo.getBooleanAttribute(RESAMPLE_INFECTION_TIMES);
271 }
272
273 return new TransmissionWilsonBaldingA(c2cL, weight, resampleInfectionTimes);
274 }
275
276 // ************************************************************************
277 // AbstractXMLObjectParser implementation
278 // ************************************************************************
279
280 public String getParserDescription(){
281 return "This element represents a Wilson-Balding move operator, such that the transplantation of the " +
282 "subtree does not affect the topology of the transmission tree.";
283 }
284
285 public Class getReturnType(){
286 return TransmissionWilsonBaldingA.class;
287 }
288
289 public XMLSyntaxRule[] getSyntaxRules() {
290 return rules;
291 }
292
293 private final XMLSyntaxRule[] rules;{
294 rules = new XMLSyntaxRule[]{
295 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
296 AttributeRule.newBooleanRule(RESAMPLE_INFECTION_TIMES, true),
297 new ElementRule(CaseToCaseTreeLikelihood.class)
298 };
299 }
300 };
301 }