Debian Codegraph @

src/dr/evomodel/tree/MicrosatelliteSamplerTreeModel.java

raw symbols SCIP
1 /*
2 * MicrosatelliteSamplerTreeModel.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.tree;
27
28 import dr.inference.model.*;
29 import dr.evolution.tree.NodeRef;
30 import dr.evolution.util.Taxon;
31 import dr.evolution.alignment.Patterns;
32 import dr.math.MathUtils;
33
34 import java.util.Map;
35
36 /**
37 * @author Chieh-Hsi Wu
38 *
39 * Tree model that stores the probabilites of each branch when the ancestral states are sampled.
40 *
41 */
42
43
44 public class MicrosatelliteSamplerTreeModel extends AbstractModel {
45 protected final TreeModel tree;
46
47 // The tree parameter;
48 protected Parameter parameter;
49
50 // the index of the root node.
51 protected int rootNodeNumber;
52 protected int storedRootNodeNumber;
53 private int[] externalValues;
54 private double[] logBranchLikelihoods;
55 private double[] storedLogBranchLikelihoods;
56 private Patterns microsatPat;
57 Map<String, Integer> taxaMap;
58
59 /**
60 * This class constructs a tree parameter, and will set the dimension of the parameter
61 * to match the appropriate number of nodes if necessary.
62 *
63 * @param id model id
64 * @param tree tree that this parameter corresponds to
65 * @param internalValues values to keep in sync with tree topology moves.
66 * @param microsatPat microsatellite patten
67 * @param externalValues values at the tips of the tree stored in an array.
68 * @param taxaMap maps the taxa to the index of array storing the external values
69 * @param internalStateProvided indicates whether internal states has being specified
70 *
71 */
72 public MicrosatelliteSamplerTreeModel(
73 String id,
74 TreeModel tree,
75 Parameter internalValues,
76 Patterns microsatPat,
77 int[] externalValues,
78 Map<String, Integer> taxaMap,
79 boolean internalStateProvided) {
80 super(id);
81
82 this.tree = tree;
83 this.parameter = internalValues;
84 this.microsatPat = microsatPat;
85 this.externalValues = externalValues;
86 this.taxaMap = taxaMap;
87 this.logBranchLikelihoods = new double[tree.getNodeCount()];
88 this.storedLogBranchLikelihoods = new double[tree.getNodeCount()];
89
90 rootNodeNumber = tree.getRoot().getNumber();
91
92 storedRootNodeNumber = rootNodeNumber;
93 if(!internalStateProvided){
94 initialiseInternalStates();
95 }
96 int dim = parameter.getDimension();
97 int treeSize = tree.getInternalNodeCount();
98
99 if (dim != treeSize) {
100 System.err.println("WARNING: setting dimension of parameter to match tree branch count");
101 parameter.setDimension(treeSize);
102 Bounds<Double> bounds = parameter.getBounds();
103 int upper = (int)(double)bounds.getUpperLimit(0);
104 int lower = (int)(double)bounds.getLowerLimit(0);
105 parameter.addBounds(new Parameter.DefaultBounds(upper, lower, treeSize));
106 }
107
108
109 addModel(tree);
110 addVariable(parameter);
111 }
112
113
114 public void handleModelChangedEvent(Model model, Object object, int index) {}
115
116 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
117 int nodeNumber = getNodeNumberFromParameterIndex(index);
118
119 NodeRef node = tree.getNode(nodeNumber);
120 assert (node.getNumber() == nodeNumber) :
121 "node.getNumber()=" + node.getNumber() + ", whereas nodeNumber=" + nodeNumber;
122
123 internalNodesChanged = true;
124 fireModelChanged(this, nodeNumber);
125 }
126
127 boolean internalNodesChanged = false;
128 public boolean areInternalNodesChanged(){
129 return internalNodesChanged;
130 }
131
132 public void setInternalNodesChanged(boolean changed){
133 internalNodesChanged = changed;
134 }
135
136 protected void storeState() {
137 storedRootNodeNumber = rootNodeNumber;
138 System.arraycopy(logBranchLikelihoods, 0, storedLogBranchLikelihoods, 0, logBranchLikelihoods.length);
139 }
140
141 protected void restoreState() {
142 rootNodeNumber = storedRootNodeNumber;
143 double[] temp1 = logBranchLikelihoods;
144 logBranchLikelihoods = storedLogBranchLikelihoods;
145 storedLogBranchLikelihoods = temp1;
146 }
147
148 protected void acceptState() {}
149
150 /**
151 *
152 * @param node Node reference number
153 *
154 * @return the state of the node
155 *
156 */
157 public int getNodeValue(NodeRef node) {
158 if(tree.isExternal(node)){
159 Taxon nodeTaxon = tree.getNodeTaxon(node);
160 int externalValIndex = taxaMap.get(nodeTaxon.getId());
161 return externalValues[externalValIndex];
162 }
163
164 int nodeNumber = node.getNumber();
165 int index = getParameterIndexFromNodeNumber(nodeNumber);
166 return (int)parameter.getParameterValue(index);
167
168 }
169
170 public String getBranchAttributeLabel() {
171 return parameter.getId();
172 }
173
174 public String getAttributeForBranch(NodeRef node) {
175 return Double.toString(getNodeValue(node));
176 }
177
178 public int getNodeNumberFromParameterIndex(int parameterIndex) {
179 return parameterIndex + tree.getExternalNodeCount();
180 }
181
182 public int getParameterIndexFromNodeNumber(int nodeNumber) {
183 return nodeNumber - tree.getExternalNodeCount();
184 }
185
186 public TreeModel getTreeModel() {
187 return tree;
188 }
189
190 public String[] getNodeAttributeLabel() {
191 return new String[]{parameter.getId()};
192 }
193
194 public String[] getAttributeForNode(NodeRef node) {
195 return new String[]{getAttributeForBranch(node)};
196 }
197
198 public double getLogBranchLikelihood(NodeRef node){
199 return getLogBranchLikelihood(node.getNumber());
200 }
201
202 public double getLogBranchLikelihood(int nodeNum){
203 return logBranchLikelihoods[nodeNum];
204 }
205
206 public void setLogBranchLikelihood(NodeRef node, double value){
207 logBranchLikelihoods[node.getNumber()] = value;
208 }
209
210 public double getStoredLogBranchLikelihood(int nodeNum){
211 return storedLogBranchLikelihoods[nodeNum];
212 }
213
214 public Patterns getMicrosatPattern(){
215 return microsatPat;
216 }
217
218
219 /**
220 * initialise the internal states using th mid point of the states of two children
221 */
222 public void initialiseInternalStates(){
223 int nodeCount = tree.getNodeCount();
224 int maxState = microsatPat.getDataType().getStateCount() - 1;
225 int internalNodeCount = tree.getInternalNodeCount();
226 Parameter.DefaultBounds bounds = new Parameter.DefaultBounds(maxState, 0, internalNodeCount);
227 for(int nodeNum = tree.getExternalNodeCount(); nodeNum < nodeCount; nodeNum++){
228 NodeRef node = tree.getNode(nodeNum);
229 NodeRef leftChild = tree.getChild(node, 0);
230 NodeRef rightChild = tree.getChild(node, 1);
231 int nodeValue;
232
233 int leftChildState = getNodeValue(leftChild);
234 int rightChildState = getNodeValue(rightChild);
235 if(leftChildState < maxState && rightChildState < maxState ){
236 nodeValue = (leftChildState+rightChildState)/2;
237 }else if(leftChildState < maxState){
238 nodeValue = leftChildState;
239
240 }else if(rightChildState < maxState){
241 nodeValue = rightChildState;
242
243 }else{
244 nodeValue = MathUtils.nextInt(maxState+1);
245 }
246 parameter.setParameterValueQuietly(nodeNum - tree.getExternalNodeCount(), nodeValue);
247 }
248 parameter.addBounds(bounds);
249
250
251 }
252
253 }
254
255
256