Debian Codegraph @

src/dr/evomodel/coalescent/CoalescentSimulator.java

raw symbols SCIP
1 /*
2 * CoalescentSimulator.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.coalescent;
27
28 import dr.evolution.tree.*;
29 import dr.evolution.util.Date;
30 import dr.evolution.util.Taxon;
31 import dr.evolution.util.TaxonList;
32 import dr.evolution.util.TimeScale;
33 import dr.inference.distribution.ParametricDistributionModel;
34 import dr.math.UnivariateFunction;
35
36 /**
37 * Simulates a set of coalescent intervals given a demographic model.
38 *
39 * @author Alexei Drummond
40 * @version $Id: CoalescentSimulator.java,v 1.43 2005/10/27 10:40:48 rambaut Exp $
41 */
42 public class CoalescentSimulator {
43
44 dr.evolution.coalescent.CoalescentSimulator simulator = new dr.evolution.coalescent.CoalescentSimulator();
45 /**
46 * Simulates a coalescent tree from a set of subtrees.
47 */
48 public CoalescentSimulator() {
49 }
50
51 /**
52 * Simulates a coalescent tree from a set of subtrees.
53 *
54 * @param subtrees an array of tree to be used as subtrees
55 * @param model the demographic model to use
56 * @param rootHeight an optional root height with which to scale the whole tree
57 * @param preserveSubtreesHeights true if heights of subtrees should be preserved
58 * @return a simulated coalescent tree
59 */
60 public SimpleTree simulateTree(Tree[] subtrees, DemographicModel model, double rootHeight,
61 boolean preserveSubtreesHeights) {
62
63 SimpleNode[] roots = new SimpleNode[subtrees.length];
64 SimpleTree tree;
65
66 for (int i = 0; i < roots.length; i++) {
67 roots[i] = new SimpleNode(subtrees[i], subtrees[i].getRoot());
68 }
69
70 // if just one taxonList then finished
71 if (roots.length == 1) {
72 tree = new SimpleTree(roots[0]);
73 } else {
74 tree = new SimpleTree(simulator.simulateCoalescent(roots, model.getDemographicFunction()));
75 }
76
77 if (!Double.isNaN(rootHeight) && rootHeight > 0.0) {
78 if (preserveSubtreesHeights) {
79 limitNodes(tree, rootHeight - 1e-12);
80 tree.setRootHeight(rootHeight);
81 } else {
82 attemptToScaleTree(tree, rootHeight);
83 }
84 }
85
86 return tree;
87 }
88
89
90 /**
91 * Simulates a coalescent tree, given a taxon list.
92 *
93 * @param taxa the set of taxa to simulate a coalescent tree between
94 * @param model the demographic model to use
95 * @return a simulated coalescent tree
96 */
97 public SimpleTree simulateTree(TaxonList taxa, DemographicModel model) {
98 return simulator.simulateTree(taxa, model.getDemographicFunction());
99 }
100
101 public void attemptToScaleTree(MutableTree tree, double rootHeight) {
102 // avoid empty tree
103 if (tree.getRoot() == null) return;
104
105 double scale = rootHeight / tree.getNodeHeight(tree.getRoot());
106 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
107 NodeRef n = tree.getInternalNode(i);
108 tree.setNodeHeight(n, tree.getNodeHeight(n) * scale);
109 }
110 MutableTree.Utils.correctHeightsForTips(tree);
111 }
112
113 public int sizeOfIntersection(TaxonList tl1, TaxonList tl2) {
114 int nIn = 0;
115 for (int j = 0; j < tl1.getTaxonCount(); ++j) {
116 if (tl2.getTaxonIndex(tl1.getTaxon(j)) >= 0) {
117 ++nIn;
118 }
119 }
120 return nIn;
121 }
122
123 public boolean contained(TaxonList taxons, TaxonList taxons1) {
124 return sizeOfIntersection(taxons, taxons1) == taxons.getTaxonCount();
125 }
126
127 /**
128 * Clip nodes height above limit.
129 *
130 * @param tree to clip
131 * @param limit height limit
132 */
133 private void limitNodes(MutableTree tree, double limit) {
134 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
135 final NodeRef n = tree.getInternalNode(i);
136 if (tree.getNodeHeight(n) > limit) {
137 tree.setNodeHeight(n, limit);
138 }
139 }
140 MutableTree.Utils.correctHeightsForTips(tree);
141 }
142
143 public static class TaxaConstraint {
144 public final TaxonList taxons;
145 public final double lower;
146 public final boolean isMonophyletic;
147 public double upper;
148
149 public TaxaConstraint(TaxonList taxons, ParametricDistributionModel p, boolean isMono) {
150 this.taxons = taxons;
151 this.isMonophyletic = isMono;
152
153 if (p != null) {
154 final UnivariateFunction univariateFunction = p.getProbabilityDensityFunction();
155 lower = univariateFunction.getLowerBound();
156 upper = univariateFunction.getUpperBound();
157 } else {
158 lower = 0;
159 upper = Double.POSITIVE_INFINITY;
160 }
161 }
162
163 public TaxaConstraint(TaxonList taxons, double low, double high, boolean isMono) {
164 this.taxons = taxons;
165 this.isMonophyletic = isMono;
166 upper = high;
167 lower = low;
168 }
169
170 public boolean realLimits() {
171 return lower != 0 || upper != Double.POSITIVE_INFINITY;
172 }
173 }
174
175 }