Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * TransmissionExchangeOperatorA.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 import java.util.ArrayList;
39
40 /**
41 * Implements branch exchange operations that leave the transmission tree unchanged. As this already severely
42 * restricts the set of eligible pairs of nodes, this is set up as special case of Wide Exchange.
43 *
44 * todo For both exchange operators, and the Type B STS and WB moves, you can actually keep the infection times.
45 * This likely helps both speed and convergence.
46 *
47 *
48 * @author Matthew Hall
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 // find a node
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 // throw new OperatorFailedException("No valid exchanges for this node");
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 // get a set of candidates for an exchange of a given node. Does not include the node itself or its sibling, so
140 // the check for a failed move will be if this set is of size 0
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 // AbstractXMLObjectParser implementation
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 }