Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * InfectionBranchMovementOperator.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.*;
30 import dr.inference.operators.MCMCOperator;
31 import dr.inference.operators.SimpleMCMCOperator;
32 import dr.math.MathUtils;
33 import dr.xml.*;
34
35 /**
36 * This operator finds a branch that corresponds to a transmission event, and moves that event up one branch or down
37 * one branch
38 *
39 * @author Matthew Hall
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 /* Switch the partition of a randomly selected internal node from the painting of one of its children to the
63 * painting of the other, and adjust the rest of the tree to ensure the result still obeys partition rules.*/
64
65 public double doOperation() {
66
67 PartitionedTreeModel tree = c2cLikelihood.getTreeModel();
68 BranchMapModel branchMap = c2cLikelihood.getBranchMap();
69
70 // find a case whose infection event we are going to move about
71 int caseIndexToAdjust = MathUtils.nextInt(c2cLikelihood.getOutbreak().size());
72
73 AbstractCase aCase = c2cLikelihood.getOutbreak().getCase(caseIndexToAdjust);
74
75 // if the infection event is the seed of the epidemic, we need to try again
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 // find the child node of the transmission branch
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 // throw new OperatorFailedException("Chosen infection event cannot be adjusted in this tree");
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 //HR adjustments for reverse moves
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 // check if either child is not ancestral (at most one is not, and if so it must have been in the same
207 // partition as both the other child and 'node')
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 // we're moving a transmission event as far down as it can go and need to adjust the HR accordingly
217 out += Math.log(2);
218 }
219 }
220
221 //if you couldn't move it any further up
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 /* Parser */
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 }