Debian Codegraph @

src/dr/oldevomodel/treelikelihood/MicrosatelliteSamplerTreeLikelihood.java

raw symbols SCIP
1 /*
2 * MicrosatelliteSamplerTreeLikelihood.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.oldevomodel.treelikelihood;
27
28 import dr.evomodel.tree.MicrosatelliteSamplerTreeModel;
29 import dr.evomodel.tree.TreeChangedEvent;
30 import dr.evomodel.tree.TreeModel;
31 import dr.oldevomodel.substmodel.MicrosatelliteModel;
32 import dr.evomodel.branchratemodel.BranchRateModel;
33 import dr.evolution.tree.NodeRef;
34 import dr.inference.model.Model;
35
36 import java.util.ArrayList;
37
38 /**
39 * @author Chieh-Hsi Wu
40 *
41 * Treelikelihood that allows ancestral state sampling for microsatellite data.
42 *
43 */
44 @Deprecated // Switching to BEAGLE
45 public class MicrosatelliteSamplerTreeLikelihood extends AbstractTreeLikelihood{
46
47 MicrosatelliteSamplerTreeModel treeMicrosatSamplerModel;
48 MicrosatelliteModel microsatelliteModel;
49 BranchRateModel branchRateModel;
50 double logL = 0.0;
51 double storedLogL = 0.0;
52 boolean modelChanged = false;
53 private ArrayList<Integer> updateAllList;
54 private ArrayList<Integer> updatedNodeList;
55 public MicrosatelliteSamplerTreeLikelihood(MicrosatelliteSamplerTreeModel treeMicrosatSamplerModel,
56 MicrosatelliteModel microsatelliteModel,
57 BranchRateModel branchRate){
58 super("MicrosatelliteSamplerTreeLikelihood",
59 treeMicrosatSamplerModel.getMicrosatPattern(),
60 treeMicrosatSamplerModel.getTreeModel());
61 this.treeMicrosatSamplerModel = treeMicrosatSamplerModel;
62 this.microsatelliteModel = microsatelliteModel;
63 this.branchRateModel = branchRate;
64
65 updateAllList = new ArrayList<Integer>();
66
67 for(int i = 0; i < nodeCount; i++){
68 updateAllList.add(i);
69 }
70 updatedNodeList = updateAllList;
71
72 addModel(this.branchRateModel);
73 addModel(this.treeMicrosatSamplerModel);
74 addModel(this.microsatelliteModel);
75
76
77 }
78
79
80 protected void handleModelChangedEvent(Model model, Object object, int index) {
81
82 if(updatedNodeList == updateAllList){
83
84 }else if (model == treeModel) {
85
86 if (object instanceof TreeModel.TreeChangedEvent) {
87
88 if(((TreeModel.TreeChangedEvent) object).areAllInternalHeightsChanged()){
89 updateAllNodes();
90 } else if (((TreeChangedEvent) object).isNodeChanged()) {
91 // If a node event occurs the node and its two child nodes
92 // are flagged for updating (this will result in everything
93 // above being updated as well. Node events occur when a node
94 // is added to a branch, removed from a branch or its height or
95 // rate changes.
96 updateNodeAndChildren(((TreeChangedEvent) object).getNode());
97 } else if (((TreeChangedEvent) object).isTreeChanged()) {
98 // Full tree events result in a complete updating of the tree likelihood
99 // Currently this event type is not used.
100 System.err.println("Full tree update event - these events currently aren't used\n" +
101 "so either this is in error or a new feature is using them so remove this message.");
102 updateAllNodes();
103 } else {
104 // Other event types are ignored (probably trait changes).
105 System.err.println("Another tree event has occured (possibly a trait change).");
106 }
107 }
108
109 } else if (model == treeMicrosatSamplerModel){
110
111 if(treeMicrosatSamplerModel.areInternalNodesChanged()) {
112
113 updateNodeAndChildren((treeMicrosatSamplerModel.getTreeModel()).getNode(index));
114 treeMicrosatSamplerModel.setInternalNodesChanged(false);
115 }
116
117 } else if (model == microsatelliteModel) {
118 if(microsatelliteModel.isModelUpdated()){
119 updateAllNodes();
120 }
121 } else if (model == branchRateModel) {
122 if (index == -1) {
123 updateAllNodes();
124 } else {
125 updateNode(treeModel.getNode(index));
126 }
127 } else {
128 throw new RuntimeException("Unknown componentChangedEvent");
129 }
130
131 modelChanged = true;
132 super.handleModelChangedEvent(model, object, index);
133 }
134
135
136
137 public boolean hasModelChanged(){
138 return modelChanged;
139 }
140
141 /**
142 * Nodes are added to upadteNodeList if it is to be updated.
143 */
144 protected void updateNodeAndChildren(NodeRef node) {
145 int nodeNum = node.getNumber();
146
147 if(!updateNode[nodeNum]){
148 updateNode[nodeNum] = true;
149 updatedNodeList.add(nodeNum);
150 }
151
152 for (int i = 0; i < treeModel.getChildCount(node); i++) {
153 int childNodeNum = (treeModel.getChild(node, i)).getNumber();
154 if(!updateNode[childNodeNum]){
155 updateNode[childNodeNum] = true;
156 updatedNodeList.add(childNodeNum);
157 }
158 }
159 likelihoodKnown = false;
160 }
161
162 /**
163 *
164 * If an event requires all the nodes to be updated
165 * then the updateNodeList will be referenced to
166 * updateAllList, which is a list that contains all the node
167 * in the tree.
168 *
169 */
170 protected void updateAllNodes() {
171 updatedNodeList = updateAllList;
172 likelihoodKnown = false;
173 }
174
175
176 public double calculateLogLikelihood(){
177
178 traverse();
179
180 //used for likelihood calculation.
181 double temp = 0.0;
182 double temp1 = 0.0;
183 double temp2 = 0.0;
184
185 //get the number of nodes that needs to be updated.
186 int updateNum = updatedNodeList.size();
187
188 //iterate through the nodes to be updated
189 for(int i = 0; i < updateNum; i++){
190
191 //get the node number of the node to be updated
192 int nodeNum = updatedNodeList.get(i);
193
194 //calculate the sum of the previous log probabilities of the nodes flagged for update
195 //only calculate this if the number of nodes to be updated is less than the total number of nodes in the tree and
196 //when there is a change in the tree.
197 if(modelChanged && updatedNodeList != updateAllList)
198 temp1 += treeMicrosatSamplerModel.getStoredLogBranchLikelihood(nodeNum);
199
200 //the sum of the probabilities of the nodes that have been updated.
201 temp2 += treeMicrosatSamplerModel.getLogBranchLikelihood(nodeNum);
202
203 //indicate that the update is complete.
204 updateNode[nodeNum] = false;
205 }
206
207 //subtract the sum of the previous log probabilities of the updated nodes from the previous likelihood.
208 if(modelChanged && updatedNodeList != updateAllList)
209 temp = logL - temp1;
210
211
212 //add the new likelihoods of the updated node to get the current likelihood.
213 logL = temp + temp2;
214
215 //indicate that the completion of calculating the likelihood after a modelChange
216 modelChanged = false;
217
218 //clear the list of the nodes to be updated
219 updatedNodeList = new ArrayList<Integer>();
220
221
222
223 return logL;
224
225 }
226
227 protected void storeState() {
228 storedLogL = logL;
229
230 super.storeState();
231
232 }
233
234 /**
235 * Restore the additional stored state
236 */
237 protected void restoreState() {
238 logL = storedLogL;
239 super.restoreState();
240
241 }
242
243
244 private void traverse(){
245 TreeModel tree = treeMicrosatSamplerModel.getTreeModel();
246
247 int updateNum = updatedNodeList.size();
248 for(int i = 0; i < updateNum; i++){
249 NodeRef node = tree.getNode(updatedNodeList.get(i));
250 int nodeState = treeMicrosatSamplerModel.getNodeValue(node);
251 if(!tree.isRoot(node)){
252
253 NodeRef parent = tree.getParent(node);
254 int parentState = treeMicrosatSamplerModel.getNodeValue(parent);
255
256 double branchLength = tree.getBranchLength(node)*branchRateModel.getBranchRate(tree,node);
257 double nodePr = microsatelliteModel.getLogOneTransitionProbabilityEntry(branchLength, parentState, nodeState);
258
259 treeMicrosatSamplerModel.setLogBranchLikelihood(node, nodePr);
260 }else{
261 double logEqFreq = Math.log(microsatelliteModel.getStationaryDistribution()[nodeState]);
262 treeMicrosatSamplerModel.setLogBranchLikelihood(node, logEqFreq);
263
264 }
265
266
267 }
268
269 }
270
271
272
273
274 }