Debian Codegraph @

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

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