Debian Codegraph @

src/dr/evomodel/branchratemodel/CountableModelMixtureBranchRates.java

raw symbols SCIP
1 /*
2 * CountableModelMixtureBranchRates.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.Tree;
30 import dr.evomodel.tree.TreeModel;
31 import dr.evomodelxml.branchratemodel.CountableMixtureBranchRatesParser;
32 import dr.inference.loggers.LogColumn;
33 import dr.inference.loggers.Loggable;
34 import dr.inference.loggers.NumberColumn;
35 import dr.inference.model.Model;
36 import dr.inference.model.Parameter;
37 import dr.inference.model.Variable;
38
39 import java.util.List;
40
41 /**
42 * A modification of CountableMixtureBranchRates so the fixed effects come from a list of branchRateModels
43 * @author Marc Suchard
44 * @author Philippe Lemey
45 * @author Andrew Rambaut
46 */
47 public class CountableModelMixtureBranchRates extends AbstractBranchRateModel implements Loggable {
48
49 private final List<AbstractBranchRateModel> fixedEffectsModels;
50 private final TreeModel treeModel;
51 private final List<AbstractBranchRateModel> randomEffectsModels;
52 private final int categoryCount;
53
54 public CountableModelMixtureBranchRates(CountableBranchCategoryProvider rateCategories,
55 TreeModel treeModel,
56 List<AbstractBranchRateModel> fixedEffects,
57 List<AbstractBranchRateModel> randomEffects, boolean inLogSpace) {
58 super(CountableMixtureBranchRatesParser.COUNTABLE_CLOCK_BRANCH_RATES);
59
60 this.treeModel = treeModel;
61 categoryCount = fixedEffects.size();
62 this.rateCategories = rateCategories;
63 rateCategories.setCategoryCount(categoryCount);
64
65 if (rateCategories instanceof Model) {
66 addModel((Model)rateCategories);
67 }
68 this.fixedEffectsModels = fixedEffects;
69 for (AbstractBranchRateModel model : fixedEffectsModels) {
70 addModel(model);
71 }
72
73 // Handle random effects
74 this.randomEffectsModels = randomEffects;
75 if (randomEffectsModels != null) {
76 for (AbstractBranchRateModel model : randomEffectsModels) {
77 addModel(model);
78 }
79 }
80 // TODO Check that randomEffectsModel means are zero
81
82 modelInLogSpace = inLogSpace;
83 }
84
85 public double getLogLikelihood() {
86 double logLike = 0.0;
87 for (AbstractBranchRateModel model : fixedEffectsModels) {
88 logLike += model.getLogLikelihood();
89 }
90 if (randomEffectsModels != null) {
91 for (AbstractBranchRateModel model : randomEffectsModels) {
92 logLike += model.getLogLikelihood();
93 }
94 }
95 return logLike;
96 }
97
98 public LogColumn[] getColumns() {
99 LogColumn[] columns = new LogColumn[categoryCount];
100 for (int i = 0; i < categoryCount; ++i) {
101 columns[i] = new OccupancyColumn(i);
102 }
103
104 return columns;
105 }
106
107 private class OccupancyColumn extends NumberColumn {
108 private final int index;
109
110 public OccupancyColumn(int index) {
111 super("Occupancy");
112 this.index = index;
113 }
114
115 public double getDoubleValue() {
116 int occupancy = 0;
117 for (NodeRef node : treeModel.getNodes()) {
118 if (node != treeModel.getRoot()) {
119 if (rateCategories.getBranchCategory(treeModel, node) == index) {
120 occupancy++;
121 }
122 }
123 }
124 return occupancy;
125 }
126 }
127
128 public void handleModelChangedEvent(Model model, Object object, int index) {
129 if (model == rateCategories) {
130 fireModelChanged();
131 } else {
132 AbstractBranchRateModel foundModel = findModel(model);
133 if (foundModel != null) {
134 if (object == model) {
135 fireModelChanged();
136 } else if (object == null) {
137 fireModelChanged(null, index);
138 } else {
139 // throw new IllegalArgumentException("Unknown object component!");
140 fireModelChanged();
141 }
142 } else {
143 throw new IllegalArgumentException("Unknown model component!");
144 }
145 }
146 }
147
148 private AbstractBranchRateModel findModel(Model model) {
149 if (randomEffectsModels != null) {
150 int index = randomEffectsModels.indexOf(model);
151 if (index != -1) {
152 return randomEffectsModels.get(index);
153 }
154 }
155 int index = fixedEffectsModels.indexOf(model);
156 if (index != -1) {
157 return fixedEffectsModels.get(index);
158 }
159 return null;
160 }
161
162 protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
163 fireModelChanged();
164 }
165
166 protected void storeState() {
167 // nothing to do
168 }
169
170 protected void restoreState() {
171 // nothing to do
172 }
173
174 protected void acceptState() {
175 // nothing to do
176 }
177
178 public double getBranchRate(final Tree tree, final NodeRef node) {
179
180 assert !tree.isRoot(node) : "root node doesn't have a rate!";
181
182 int rateCategory = rateCategories.getBranchCategory(tree, node);
183
184 AbstractBranchRateModel fixedModel = fixedEffectsModels.get(rateCategory);
185 double effect = fixedModel.getBranchRate(tree, node);
186
187 if (randomEffectsModels != null) {
188 for (AbstractBranchRateModel model : randomEffectsModels) {
189 if (modelInLogSpace) {
190 effect += model.getBranchRate(tree, node);
191 } else {
192 effect *= model.getBranchRate(tree, node);
193 }
194 }
195 }
196 if (modelInLogSpace) {
197 effect = Math.exp(effect);
198 }
199 return effect;
200 }
201
202 private final CountableBranchCategoryProvider rateCategories;
203 private final boolean modelInLogSpace;
204 }