Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * TransmissionTreeOperator.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.operators.ExchangeOperator;
34 import dr.evomodel.operators.SubtreeSlideOperator;
35 import dr.evomodel.operators.WilsonBalding;
36 import dr.evomodel.tree.TreeModel;
37 import dr.inference.operators.AbstractAdaptableOperator;
38 import dr.inference.operators.AdaptableMCMCOperator;
39 import dr.inference.operators.AdaptationMode;
40 import dr.xml.*;
41
42 import java.util.ArrayList;
43
44 /**
45 * An operator that wraps a (phylogenetic) tree operator and adjusts the transmission tree accordingly. Only works
46 * for the simpler (TT=TMRCA) version of the transmission tree (suitable for birth-death models and their derivatives).
47 * TT moves are deterministic, so the Hastings ratios reported by the inner operators are not affected.
48 *
49 * @author Matthew Hall
50 */
51
52 public class TransmissionTreeOperator extends AbstractAdaptableOperator {
53
54 private final CaseToCaseTreeLikelihood c2cLikelihood;
55 private final AbstractTreeOperator innerOperator;
56 public static final String TRANSMISSION_TREE_OPERATOR = "transmissionTreeOperator";
57
58 public TransmissionTreeOperator(CaseToCaseTreeLikelihood c2cLikelihood, AbstractTreeOperator operator,
59 AdaptationMode mode) {
60 super(mode);
61 this.c2cLikelihood = c2cLikelihood;
62 this.innerOperator = operator;
63 this.setWeight(innerOperator.getWeight());
64
65 // this doesn't work at all right now - it awaits the restoration of the TT=TMRCA version
66
67 throw new RuntimeException("TransmissionTreeOperator only works on non-extended tree paintings");
68 }
69
70 public TransmissionTreeOperator(CaseToCaseTreeLikelihood c2cLikelihood, AbstractTreeOperator operator) {
71 this(c2cLikelihood,operator, AdaptationMode.ADAPTATION_OFF);
72 }
73
74 public double doOperation() {
75 TreeModel tree = c2cLikelihood.getTreeModel();
76 BranchMapModel branchMap = c2cLikelihood.getBranchMap();
77 AbstractCase[] newBranchMap = branchMap.getArrayCopy();
78 int[] oldParents = getParentsArray(tree);
79 double[] oldHeights = getHeightsArray(tree);
80 double hr = innerOperator.doOperation();
81 int[] newParents = getParentsArray(tree);
82 ArrayList<Integer> changedNodes = new ArrayList<Integer>();
83 for(int i=0; i<tree.getNodeCount(); i++){
84 if(oldParents[i]!=newParents[i]){
85 changedNodes.add(i);
86 }
87 }
88 if(changedNodes.size()!=0){
89 if(innerOperator instanceof ExchangeOperator){
90 //this is a node swap operator
91 AbstractCase[] nodePaintings = new AbstractCase[2];
92 AbstractCase[] parentPaintings = new AbstractCase[2];
93 for(int i=0; i<2; i++){
94 nodePaintings[i] = branchMap.get(changedNodes.get(i));
95 parentPaintings[i] = branchMap.get(oldParents[changedNodes.get(i)]);
96 }
97 if(nodePaintings[0]==parentPaintings[0] || nodePaintings[1]==parentPaintings[1]){
98 //If this is not true there is nothing to do - the result is already a valid painting
99 for(int i=0; i<2; i++){
100 paintUp(tree, nodePaintings[1-i], nodePaintings[i], branchMap, newBranchMap,
101 tree.getNode(changedNodes.get(i)).getNumber(),newParents);
102 }
103 }
104
105 } else if(innerOperator instanceof SubtreeSlideOperator || innerOperator instanceof WilsonBalding){
106 //this is a node transplantation operator
107 int movedNode = -1;
108 int oldChild = -1;
109 int newChild = -1;
110 for(int i=0; i<tree.getNodeCount(); i++){
111 //the chance of it having the same height as before is minuscule. If so, it will still throw an
112 // exception later, though.
113 if(tree.getNodeHeight(tree.getNode(i))!=oldHeights[i]){
114 movedNode = i;
115 break;
116 }
117 }
118 for(int j:changedNodes){
119 if(j!=movedNode){
120 if(tree.getParent(tree.getNode(j))==tree.getNode(movedNode)){
121 newChild = j;
122 } else {
123 oldChild = j;
124 }
125 }
126 }
127 if(movedNode == -1 || oldChild == -1 || newChild == -1) {
128 // is this a bug or should the move be rejected (i.e., return -Inf HR)?
129 throw new RuntimeException("Failed to establish relationship between relocated node and others");
130 }
131 NodeRef movedNodeObject = tree.getNode(movedNode);
132 NodeRef oldChildObject = tree.getNode(oldChild);
133 NodeRef newChildObject = tree.getNode(newChild);
134 int otherChild = -1;
135 NodeRef otherChildObject;
136 //Find the other child of the moved node (the root of the transplanted subtree)
137 for(int i=0; i<tree.getChildCount(movedNodeObject);i++){
138 if(tree.getChild(movedNodeObject,i)!=newChildObject){
139 otherChildObject = tree.getChild(movedNodeObject,i);
140 otherChild = otherChildObject.getNumber();
141 }
142 }
143
144 //If the child of the moved node is the earliest node with its painting:
145 if(branchMap.get(otherChild)!=branchMap.get(movedNode)){
146 newBranchMap[movedNode]=branchMap.get(newChild);
147 } else {
148 //Change all paintings up the tree from the old child that used to match the moved node to match
149 //the old child
150 paintUp(tree, branchMap.get(movedNode),branchMap.get(oldChild),branchMap,newBranchMap,oldChild,
151 oldParents);
152 //This may have resulted in the moved node being recoloured wrong.
153 newBranchMap[movedNode]=branchMap.get(movedNode);
154 branchMap.setAll(newBranchMap, true);
155 //Change all paintings up the tree from the moved node that used to match the new child to match
156 //the moved node
157 paintUp(tree, branchMap.get(newChild), branchMap.get(movedNode), branchMap, newBranchMap, movedNode,
158 newParents);
159 }
160 } else {
161 //I don't know what this is
162 throw new UnsupportedOperationException("Operator class "+innerOperator.getOperatorName()+" not yet " +
163 "supported");
164 }
165 }
166 branchMap.setAll(newBranchMap, true);
167 c2cLikelihood.makeDirty();
168 return hr;
169 }
170
171 private void paintUp(TreeModel tree, AbstractCase oldCase, AbstractCase newCase, BranchMapModel oldbranchMap,
172 AbstractCase[] newBranchMap, int nodeNo, int[] parents){
173 if(parents[nodeNo]==-1){
174 return;
175 }
176 NodeRef newParent = tree.getNode(parents[nodeNo]);
177 while(newParent!=null && oldbranchMap.get(newParent.getNumber())==oldCase){
178 newBranchMap[newParent.getNumber()]=newCase;
179 newParent = parents[newParent.getNumber()]== -1 ? null : tree.getNode(parents[newParent.getNumber()]);
180 }
181 }
182
183 private int[] getParentsArray(TreeModel tree){
184 int[] out = new int[tree.getNodeCount()];
185 for(int i=0; i<tree.getNodeCount(); i++){
186 if(tree.getNode(i)==tree.getRoot()){
187 out[i]=-1;
188 } else {
189 out[i]=tree.getParent(tree.getNode(i)).getNumber();
190 }
191 }
192 return out;
193 }
194
195 private double[] getHeightsArray(TreeModel tree){
196 double[] out = new double[tree.getNodeCount()];
197 for(int i=0; i<tree.getNodeCount(); i++){
198 out[i]=tree.getNodeHeight(tree.getNode(i));
199 }
200 return out;
201 }
202
203 public double getAdaptableParameter() {
204 if(innerOperator instanceof AdaptableMCMCOperator){
205 return ((AdaptableMCMCOperator) innerOperator).getAdaptableParameter();
206 }
207 throw new IllegalArgumentException();
208 }
209
210 public void setAdaptableParameter(double value) {
211 if(innerOperator instanceof AdaptableMCMCOperator){
212 ((AdaptableMCMCOperator) innerOperator).setAdaptableParameter(value);
213 return;
214 }
215 throw new IllegalArgumentException();
216 }
217
218 public double getRawParameter() {
219 if(innerOperator instanceof AdaptableMCMCOperator){
220 return ((AdaptableMCMCOperator) innerOperator).getRawParameter();
221 }
222 throw new IllegalArgumentException();
223 }
224
225 public String getAdaptableParameterName() {
226 if(innerOperator instanceof AdaptableMCMCOperator) {
227 return ((AdaptableMCMCOperator) innerOperator).getAdaptableParameterName();
228 }
229 return "";
230 }
231
232 public String getOperatorName() {
233 return TRANSMISSION_TREE_OPERATOR;
234 }
235
236
237 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
238
239 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
240 CaseToCaseTreeLikelihood c2cL
241 = (CaseToCaseTreeLikelihood)xo.getChild(CaseToCaseTreeLikelihood.class);
242 AbstractTreeOperator treeOp = (AbstractTreeOperator)xo.getChild(AbstractTreeOperator.class);
243
244 AdaptationMode mode = AdaptationMode.ADAPTATION_OFF;
245
246 if(treeOp instanceof AdaptableMCMCOperator){
247 mode = ((AdaptableMCMCOperator) treeOp).getMode();
248 }
249
250 return new TransmissionTreeOperator(c2cL,treeOp,mode);
251
252 }
253
254 public XMLSyntaxRule[] getSyntaxRules() {
255 return rules;
256 }
257
258 public String getParserDescription() {
259 return "Performs a tree move then readjusts the transmission network in order to maintain its integrity.";
260 }
261
262 public Class getReturnType() {
263 return TransmissionTreeOperator.class;
264 }
265
266 public String getParserName() {
267 return TRANSMISSION_TREE_OPERATOR;
268 }
269
270 private final XMLSyntaxRule[] rules = {
271 new ElementRule(CaseToCaseTreeLikelihood.class, "The transmission network likelihood element"),
272 new ElementRule(AbstractTreeOperator.class, "A phylogenetic tree operator.")
273 };
274 };
275 }