Debian Codegraph @

src/dr/evomodel/branchratemodel/CountableBranchCategoryProvider.java

raw symbols SCIP
1 /*
2 * CountableBranchCategoryProvider.java
3 *
4 * Copyright (c) 2002-2016 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.TreeUtils;
29 import dr.evomodel.treelikelihood.MarkovJumpsTraitProvider;
30 import dr.evolution.tree.NodeRef;
31 import dr.evolution.tree.Tree;
32 import dr.evolution.tree.TreeTrait;
33 import dr.evolution.util.TaxonList;
34 import dr.evomodel.tree.TreeModel;
35 import dr.evomodel.tree.TreeParameterModel;
36 import dr.inference.model.Model;
37 import dr.inference.model.Parameter;
38 import dr.math.MathUtils;
39
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Set;
43
44 /**
45 * @author Marc A. Suchard
46 * @author Philippe Lemey
47 * @author Andrew Rambaut
48 */
49 public interface CountableBranchCategoryProvider extends TreeTrait<Double> {
50
51 public int getBranchCategory(final Tree tree, final NodeRef node);
52
53 public void setCategoryCount(final int count);
54
55 public int getCategoryCount();
56
57 public class SingleBranchCategoryModel implements CountableBranchCategoryProvider {
58 @Override
59 public int getBranchCategory(final Tree tree, final NodeRef node) {
60 return 0;
61 }
62 @Override
63 public void setCategoryCount(final int count) {
64 // Do nothing
65 }
66 @Override
67 public int getCategoryCount() {
68 return 1;
69 }
70
71 @Override
72 public String getTraitName() {
73 return "categories";
74 }
75
76 @Override
77 public Intent getIntent() {
78 return Intent.BRANCH;
79 }
80
81 @Override
82 public Class getTraitClass() {
83 return Integer.class;
84 }
85
86 @Override
87 public Double getTrait(Tree tree, NodeRef node) {
88 return 1.0;
89 }
90
91 @Override
92 public String getTraitString(Tree tree, NodeRef node) {
93 return "1";
94 }
95
96 @Override
97 public boolean getLoggable() {
98 return true;
99 }
100 }
101
102 public abstract class BranchCategoryModel extends TreeParameterModel implements CountableBranchCategoryProvider {
103
104 public BranchCategoryModel(TreeModel tree, Parameter parameter) {
105 super(tree, parameter, false, Intent.BRANCH);
106
107 this.categoryParameter = parameter;
108 // for (int i = 0; i < parameter.getDimension(); ++i) {
109 // categoryParameter.setParameterValue(i, 0.0);
110 // }
111 this.treeModel = tree;
112
113 categoryCount = 1;
114 }
115
116 // public BranchCategoryModel(TreeModel tree, Parameter parameter, boolean resetCategories) {
117 // super(tree, parameter, false, Intent.BRANCH);
118 //
119 // this.categoryParameter = parameter;
120 // this.treeModel = tree;
121 // }
122
123 public void setCategoryCount(final int count) {
124
125 categoryCount = count;
126 Parameter.DefaultBounds bound = new Parameter.DefaultBounds(categoryCount - 1, 0, categoryParameter.getDimension());
127 categoryParameter.addBounds(bound);
128
129 for (int i = 0; i < categoryParameter.getDimension(); ++i) {
130 if (categoryParameter.getParameterValue(i) >= categoryCount) {
131 categoryParameter.setParameterValue(i, categoryCount - 1);
132 }
133 }
134 }
135
136 @Override
137 public int getBranchCategory(final Tree tree, final NodeRef node) {
138 return (int) Math.round(getNodeValue(tree, node));
139 }
140
141 @Override
142 public int getCategoryCount() {
143 return categoryCount;
144 }
145
146 protected int categoryCount;
147 protected final Parameter categoryParameter;
148 protected final TreeModel treeModel;
149 }
150
151 public class IndependentBranchCategoryModel extends BranchCategoryModel {
152
153 public IndependentBranchCategoryModel(TreeModel tree, Parameter parameter) {
154 super(tree, parameter);
155 }
156
157 public void randomize() {
158 for (NodeRef node : treeModel.getNodes()) {
159 if (node != treeModel.getRoot()) {
160 int index = MathUtils.nextInt(categoryCount);
161 setNodeValue(treeModel, node, index);
162 }
163 }
164 }
165 }
166
167 public class MarkovJumpBranchCategoryModel extends BranchCategoryModel {
168
169 public MarkovJumpBranchCategoryModel(MarkovJumpsTraitProvider markovJumpTrait, Parameter parameter) {
170 super(markovJumpTrait.getTreeModel(), parameter);
171 }
172
173 @Override
174 public int getBranchCategory(final Tree tree, final NodeRef node) {
175 synchronized (this) {
176 if (traitsChanged) {
177 updateTraitRateCategories();
178 traitsChanged = false;
179 }
180 }
181 return super.getBranchCategory(tree, node);
182 }
183
184 private void updateTraitRateCategories() {
185
186 }
187
188 public void handleModelChangedEvent(Model model, Object object, int index) {
189 if (model == treeModel) {
190 traitsChanged = true;
191 fireModelChanged();
192 } else {
193 throw new IllegalArgumentException("Unknown model component!");
194 }
195 }
196
197 private boolean traitsChanged = true;
198 }
199
200 public class CladeBranchCategoryModel extends BranchCategoryModel {
201
202 public CladeBranchCategoryModel(TreeModel tree, Parameter parameter) {
203 super(tree, parameter);
204 }
205
206 public void handleModelChangedEvent(Model model, Object object, int index) {
207 if (model == treeModel) {
208 cladesChanged = true;
209 fireModelChanged();
210 } else {
211 throw new IllegalArgumentException("Unknown model component!");
212 }
213 }
214
215 private void recurseDownClade(final NodeRef node, final TreeModel treeModel, final CladeContainer clade, boolean include) {
216
217 if (include && !treeModel.isRoot(node)) {
218 setNodeValue(treeModel, node, clade.getRateCategory());
219 }
220
221 if (!treeModel.isExternal(node)) {
222 for (int i = 0; i < tree.getChildCount(node); i++) {
223 NodeRef child = tree.getChild(node, i);
224 recurseDownClade(child, treeModel, clade, true);
225 }
226 }
227 }
228
229 private void updateCladeRateCategories() {
230 if (leafSetList != null) {
231 // Set all to zero
232 for (NodeRef node : treeModel.getNodes()) {
233 if (node != treeModel.getRoot()) {
234 setNodeValue(treeModel, node, 0.0);
235 }
236 }
237 // Handle clades
238 for (CladeContainer clade : leafSetList) {
239 NodeRef node = TreeUtils.getCommonAncestorNode(treeModel, clade.getLeafSet());
240 if (node != treeModel.getRoot()) {
241 if (clade.getIncludeStem()) {
242 setNodeValue(treeModel, node, clade.getRateCategory());
243 }
244 }
245 // Include the clade below
246 if (!clade.getExcludeClade()) {
247 recurseDownClade(node, treeModel, clade, clade.getIncludeStem());
248 }
249 }
250 }
251
252 if (trunkSetList != null) {
253 //we keep the default rates assignments by clade definitions if they exist (leafSetList != null), if they do not exist, set default to 0.0
254 if (leafSetList == null) {
255 for (NodeRef node : treeModel.getNodes()) {
256 if (node != treeModel.getRoot()) {
257 setNodeValue(treeModel, node, 0.0);
258 }
259 }
260 }
261 // currently, specific backbone rates will overwrite branch assignments by clade definitions
262 //TODO: think about turning this around. One can imagine setting backbone rates and then additional rates based on clade definitions
263 for (CladeContainer trunk : trunkSetList) {
264 for (NodeRef node : treeModel.getNodes()) {
265 //TODO: add xml argument for tip exclusion
266 if (onAncestralPath(treeModel, node, trunk.getLeafSet(), trunk.getExcludeClade(), false)) {
267 if (node != treeModel.getRoot()) {
268 setNodeValue(treeModel, node, trunk.getRateCategory());
269 }
270 }
271 }
272 }
273 }
274 }
275
276 private boolean onAncestralPath(Tree tree, NodeRef node, Set targetSet, boolean excludeClade, boolean excludeTips) {
277
278 if (excludeTips){
279 if (tree.isExternal(node)) return false;
280 }
281
282 Set leafSet = TreeUtils.getDescendantLeaves(tree, node);
283 int size = leafSet.size();
284
285 leafSet.retainAll(targetSet);
286
287 if (leafSet.size() > 0) {
288
289 if (excludeClade) {
290
291 if (leafSet.size() == size) {
292
293 Set superLeafSet = TreeUtils.getDescendantLeaves(tree, tree.getParent(node));
294 superLeafSet.removeAll(targetSet);
295
296 // the branch is on ancestral path if the super tree has some non-targets in it
297 return (superLeafSet.size() > 0);
298
299 } else return true;
300
301 } else {
302 if (leafSet.size() > 0) {
303 return true;
304 }
305 else return false;
306 }
307 } else return false;
308
309 }
310
311 public void setClade(TaxonList taxonList, int rateCategory, boolean includeStem, boolean excludeClade, boolean trunk) throws TreeUtils.MissingTaxonException {
312
313 Set<String> leafSet = TreeUtils.getLeavesForTaxa(treeModel, taxonList);
314 if (!trunk) {
315 if (leafSetList == null) {
316 leafSetList = new ArrayList<CladeContainer>();
317 }
318 leafSetList.add(new CladeContainer(leafSet, rateCategory, includeStem, excludeClade));
319 cladesChanged = true;
320 } else {
321 if (trunkSetList == null) {
322 trunkSetList = new ArrayList<CladeContainer>();
323 }
324 trunkSetList.add(new CladeContainer(leafSet, rateCategory, includeStem, excludeClade));
325 cladesChanged = true;
326 }
327 if (rateCategory >= categoryCount) {
328 categoryCount = rateCategory + 1;
329 }
330 }
331
332
333 @Override
334 public int getBranchCategory(final Tree tree, final NodeRef node) {
335 synchronized (this) {
336 if (cladesChanged) {
337 updateCladeRateCategories();
338 cladesChanged = false;
339 }
340 }
341 return super.getBranchCategory(tree, node);
342 }
343
344 private class CladeContainer {
345 private Set<String> leafSet;
346 private int rateCategory;
347 boolean includeStem;
348 boolean excludeClade;
349
350 public CladeContainer(Set<String> leafSet, int rateCategory, boolean includeStem, boolean excludeClade) {
351 this.leafSet = leafSet;
352 this.rateCategory = rateCategory;
353 this.includeStem = includeStem;
354 this.excludeClade = excludeClade;
355 }
356
357 public Set<String> getLeafSet() {
358 return leafSet;
359 }
360
361 public int getRateCategory() {
362 return rateCategory;
363 }
364
365 public boolean getIncludeStem() {
366 return includeStem;
367 }
368
369 public boolean getExcludeClade() {
370 return excludeClade;
371 }
372 }
373
374 private boolean cladesChanged = false;
375 private List<CladeContainer> leafSetList = null;
376 private List<CladeContainer> trunkSetList = null;
377 }
378 }