Debian Codegraph @

src/dr/evomodel/branchratemodel/MixtureModelBranchRates.java

raw symbols SCIP
1 /*
2 * MixtureModelBranchRates.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.branchratemodel;
27
28 import dr.evolution.tree.NodeRef;
29 import dr.evolution.tree.SimpleTree;
30 import dr.evolution.tree.Tree;
31 import dr.evomodel.tree.TreeModel;
32 import dr.evomodel.tree.TreeParameterModel;
33 import dr.evomodelxml.branchratemodel.MixtureModelBranchRatesParser;
34 import dr.inference.distribution.ParametricDistributionModel;
35 import dr.inference.model.*;
36
37 /**
38 * @author Wai Lok Sibon Li
39 * @version $Id: DiscretizedBranchRates.java,v 1.11 2009/12/01 17:44:30 rambaut Exp $
40 */
41 public class MixtureModelBranchRates extends AbstractBranchRateModel {
42
43 private final ParametricDistributionModel[] distributionModels;
44
45 // The rate categories of each branch
46 final TreeParameterModel rateCategoryQuantiles;
47
48 private Parameter distributionIndexParameter;
49
50 private final double[] rates;
51 private boolean useQuantilesForRates = true;
52 private boolean normalize = false;
53 private double normalizeBranchRateTo = Double.NaN;
54 private double scaleFactor = 1.0;
55 private TreeModel treeModel;
56 private Tree tree;
57
58 public MixtureModelBranchRates(
59 TreeModel tree,
60 Parameter rateCategoryQuantilesParameter,
61 ParametricDistributionModel[] models,
62 Parameter distributionIndexParameter) {
63 this(tree, rateCategoryQuantilesParameter, models, distributionIndexParameter, true, false, Double.NaN);
64 }
65
66 public MixtureModelBranchRates(
67 TreeModel tree,
68 Parameter rateCategoryQuantilesParameter,
69 ParametricDistributionModel[] models,
70 Parameter distributionIndexParameter,
71 boolean normalize,
72 double normalizeBranchRateTo) {
73 this(tree, rateCategoryQuantilesParameter, models, distributionIndexParameter, true, normalize, normalizeBranchRateTo);
74 }
75
76 public MixtureModelBranchRates(
77 TreeModel tree,
78 Parameter rateCategoryQuantilesParameter,
79 ParametricDistributionModel[] models,
80 Parameter distributionIndexParameter,
81 boolean useQuantilesForRates) {
82 this(tree, rateCategoryQuantilesParameter, models, distributionIndexParameter, useQuantilesForRates, false, Double.NaN);
83 }
84
85 public MixtureModelBranchRates(
86 TreeModel tree,
87 Parameter rateCategoryQuantilesParameter,
88 ParametricDistributionModel[] models,
89 Parameter distributionIndexParameter,
90 boolean useQuantilesForRates,
91 boolean normalize,
92 double normalizeBranchRateTo) {
93 super(MixtureModelBranchRatesParser.MIXTURE_MODEL_BRANCH_RATES);
94
95 this.useQuantilesForRates = useQuantilesForRates;
96
97 this.rateCategoryQuantiles = new TreeParameterModel(tree, rateCategoryQuantilesParameter, false);
98
99 rates = new double[tree.getNodeCount()];
100
101 this.normalize = normalize;
102
103 this.treeModel = tree;
104 this.distributionModels = models;
105 this.normalizeBranchRateTo = normalizeBranchRateTo;
106
107 this.tree = new SimpleTree(tree);
108
109 this.distributionIndexParameter = distributionIndexParameter;
110 addVariable(this.distributionIndexParameter);
111
112 //Force the boundaries of rateCategoryParameter to match the category count
113 //d Parameter.DefaultBounds bound = new Parameter.DefaultBounds(categoryCount - 1, 0, rateCategoryParameter.getDimension());
114 //d rateCategoryParameter.addBounds(bound);
115 //rateCategoryQuantilesParameter.;
116
117
118
119 Parameter.DefaultBounds bound = new Parameter.DefaultBounds(1.0, 0.0, rateCategoryQuantilesParameter.getDimension());
120 rateCategoryQuantilesParameter.addBounds(bound);
121
122
123 Parameter.DefaultBounds bound2 = new Parameter.DefaultBounds(models.length - 1, 0.0, 1);
124 distributionIndexParameter.addBounds(bound2);
125 distributionIndexParameter.setParameterValue(0, 0);
126
127 //Parameter distributionIndexParameter;
128
129
130 for (ParametricDistributionModel distributionModel : distributionModels) {
131 addModel(distributionModel);
132 }
133 // AR - commented out: changes to the tree are handled by model changed events fired by rateCategories
134 // addModel(tree);
135 //d addModel(rateCategories);
136
137 addModel(rateCategoryQuantiles);
138
139 //addModel(treeModel); // Maybe
140 // AR - commented out: changes to rateCategoryParameter are handled by model changed events fired by rateCategories
141 // addVariable(rateCategoryParameter);
142
143 if (normalize) {
144 tree.addModelListener(new ModelListener() {
145
146 public void modelChangedEvent(Model model, Object object, int index) {
147 computeFactor();
148 }
149
150 public void modelRestored(Model model) {
151 computeFactor();
152 }
153 });
154 }
155
156 setupRates();
157 }
158
159 // compute scale factor
160 private void computeFactor() {
161
162 //scale mean rate to 1.0 or separate parameter
163
164 double treeRate = 0.0;
165 double treeTime = 0.0;
166
167 //normalizeBranchRateTo = 1.0;
168 for (int i = 0; i < treeModel.getNodeCount(); i++) {
169 NodeRef node = treeModel.getNode(i);
170 if (!treeModel.isRoot(node)) {
171 //d int rateCategory = (int) Math.round(rateCategories.getNodeValue(treeModel, node));
172 //d treeRate += rates[rateCategory] * treeModel.getBranchLength(node);
173 treeTime += treeModel.getBranchLength(node);
174
175 // d System.out.println("rates and time\t" + rates[rateCategory] + "\t" + treeModel.getBranchLength(node));
176 }
177 }
178 //treeRate /= treeTime;
179
180 scaleFactor = normalizeBranchRateTo / (treeRate / treeTime);
181 System.out.println("scaleFactor\t\t\t\t\t" + scaleFactor);
182 }
183
184 public void handleModelChangedEvent(Model model, Object object, int index) {
185 //System.out.println("if you dont know: " + model.getClass().getName());
186 for (ParametricDistributionModel distributionModel : distributionModels) {
187 if (model == distributionModel) {
188 setupRates();
189 fireModelChanged();
190 } //else if (model == rateCategories) {
191 // AR - commented out: if just the rate categories have changed the rates will be the same
192 // setupRates();
193 // fireModelChanged(null, index);
194 //}
195 }
196 if (model == rateCategoryQuantiles) {
197 setupRates(); // Maybe
198 //rateCategories.fireModelChanged();
199 fireModelChanged(null, index);
200 }
201 /*else if(model == distributionIndexParameter) { // Not a model
202 setupRates();
203 }*/
204 /*else if (model == treeModel) {
205 setupRates(); // Maybe
206 }*/
207 }
208
209 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
210 if(variable==distributionIndexParameter) {
211 //System.out.print(Math.round(distributionIndexParameter.getValue(0)));
212 setupRates();
213 fireModelChanged();
214 }
215 // AR - commented out: changes to rateCategoryParameter are handled by model changed events
216 //setupRates(); // Maybe
217 }
218
219 protected void storeState() {
220 //setupRates(); // Maybe
221 }
222
223 protected void restoreState() {
224 setupRates();
225 }
226
227 protected void acceptState() {
228 //setupRates(); // Maybe
229 }
230
231 public double getBranchRate(final Tree tree, final NodeRef node) {
232 assert !tree.isRoot(node) : "root node doesn't have a rate!";
233 //int rateCategory = (int) Math.round(rateCategories.getNodeValue(tree, node));
234 return rates[node.getNumber()] * scaleFactor;
235 }
236
237 /**
238 * Calculates the actual rates corresponding to the category indices.
239 */
240 protected void setupRates() {
241
242
243 //System.out.println("BRRRTTZZZ " + distributionIndexParameter.getValue(0));
244 for (int i = 0; i < tree.getNodeCount(); i++) {
245 //rates[i] = distributionModel.quantile(rateCategoryQuantiles.getNodeValue(
246 // rateCategoryQuantiles.getTreeModel(), rateCategoryQuantiles.getTreeModel().getNode(i) ));
247 if (!tree.isRoot(tree.getNode(i))) {
248
249 if(useQuantilesForRates) { /* Using quantiles to represent rates */
250 rates[tree.getNode(i).getNumber()] = distributionModels[(int) Math.round(distributionIndexParameter.getValue(0))]
251 .quantile(rateCategoryQuantiles.getNodeValue(tree, tree.getNode(i)));
252 }
253 else { /* Not using quantiles to represent rates. This is practically useless for anything else other than simulation */
254 rates[tree.getNode(i).getNumber()] = rateCategoryQuantiles.getNodeValue(tree, tree.getNode(i));
255 }
256 }
257 }
258 /*System.out.print(distributionModels[(int) Math.round(distributionIndexParameter.getValue(0))].getClass().getName() + "\t" + (int) Math.round(distributionIndexParameter.getValue(0)) + "\t" + rates[1] + "\t" + rateCategoryQuantiles.getNodeValue(tree, tree.getNode(1)));// + "\t" + distributionModels[(int) Math.round(distributionIndexParameter.getValue(0))].);
259 if(distributionModels[(int) Math.round(distributionIndexParameter.getValue(0))].getClass().getName().equals("dr.inference.distribution.LogNormalDistributionModel")) {
260 LogNormalDistributionModel lndm = (LogNormalDistributionModel) distributionModels[(int) Math.round(distributionIndexParameter.getValue(0))];
261 System.out.println("\t" + lndm.getS());
262 }
263 else if (distributionModels[(int) Math.round(distributionIndexParameter.getValue(0))].getClass().getName().equals("dr.inference.distribution.InverseGaussianDistributionModel")) {
264 InverseGaussianDistributionModel lndm = (InverseGaussianDistributionModel) distributionModels[(int) Math.round(distributionIndexParameter.getValue(0))];
265 System.out.println("\t" + lndm.getS());
266 }*/
267 if (normalize) computeFactor();
268 }
269
270 }