| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class CoalescentSimulator { |
| 43 |
|
| 44 |
dr.evolution.coalescent.CoalescentSimulator simulator = new dr.evolution.coalescent.CoalescentSimulator(); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public CoalescentSimulator() { |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|
| 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 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 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 |
|
| 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 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 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 |
} |