Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * TransmissionSubtreeSlideB.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.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 * Implements the Wilson-Balding branch swapping move if it moves an entire subtree of the transmission tree.
40 *
41 * @author Matthew Hall
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 // choose a random eligible node
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 //this one can go anywhere
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 // what happens on i's branch
130
131 if (iCase != iPCase) {
132 iCase.setInfectionBranchPosition(MathUtils.nextDouble());
133 }
134
135 // what happens between PiP and CiP
136 if (PiPCase == null || CiPCase != PiPCase) {
137 CiPCase.setInfectionBranchPosition(MathUtils.nextDouble());
138 }
139
140 // what happens between k and j
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 // need to account for the random reassignment of iP
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 // 1. remove edges <iP, CiP>
171 tree.removeChild(iP, CiP);
172 tree.removeChild(PiP, iP);
173
174 // 2. add edges <k, iP>, <iP, j>, <PiP, CiP>
175 tree.addChild(iP, j);
176 tree.addChild(PiP, CiP);
177
178 // iP is the new root
179 tree.setRoot(iP);
180
181 } else if (iP == tree.getRoot()) {
182
183 // 1. remove edges <k, j>, <iP, CiP>, <PiP, iP>
184 tree.removeChild(jP, j);
185 tree.removeChild(iP, CiP);
186
187 // 2. add edges <k, iP>, <iP, j>, <PiP, CiP>
188 tree.addChild(iP, j);
189 tree.addChild(jP, iP);
190
191 //CiP is the new root
192 tree.setRoot(CiP);
193
194 } else {
195 // 1. remove edges <k, j>, <iP, CiP>, <PiP, iP>
196 tree.removeChild(jP, j);
197 tree.removeChild(iP, CiP);
198 tree.removeChild(PiP, iP);
199
200 // 2. add edges <k, iP>, <iP, j>, <PiP, CiP>
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 // repaint the parent to match either its new parent or its new child (50% chance of each).
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 // to be eligible for this move, the node's parent must exist and be in a different partition to itself. This
233 // forces the parent to be in the same partition as either its grandchild or its other child.
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 // AbstractXMLObjectParser implementation
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 }