| 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.evolution.coalescent; |
| 27 |
|
| 28 |
import dr.app.tools.NexusExporter; |
| 29 |
import dr.evolution.tree.*; |
| 30 |
import dr.evolution.util.*; |
| 31 |
import dr.evolution.util.Date; |
| 32 |
import dr.math.MathUtils; |
| 33 |
import dr.util.HeapSort; |
| 34 |
|
| 35 |
import java.io.PrintStream; |
| 36 |
import java.util.*; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class CoalescentSimulator { |
| 49 |
|
| 50 |
public CoalescentSimulator() {} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public SimpleTree simulateTree(TaxonList taxa, DemographicFunction demoFunction) { |
| 59 |
|
| 60 |
if( taxa.getTaxonCount() == 0 ) return new SimpleTree(); |
| 61 |
|
| 62 |
SimpleNode[] nodes = new SimpleNode[taxa.getTaxonCount()]; |
| 63 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 64 |
nodes[i] = new SimpleNode(); |
| 65 |
nodes[i].setTaxon(taxa.getTaxon(i)); |
| 66 |
} |
| 67 |
|
| 68 |
boolean usingDates = Taxon.getMostRecentDate() != null; |
| 69 |
|
| 70 |
for (int i = 0; i < taxa.getTaxonCount(); i++) { |
| 71 |
Taxon taxon = taxa.getTaxon(i); |
| 72 |
if (usingDates) { |
| 73 |
nodes[i].setHeight(taxon.getHeight()); |
| 74 |
} else { |
| 75 |
|
| 76 |
nodes[i].setHeight(0.0); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return new SimpleTree(simulateCoalescent(nodes, demoFunction)); |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public SimpleNode simulateCoalescent(SimpleNode[] nodes, DemographicFunction demographic) { |
| 87 |
|
| 88 |
|
| 89 |
if( ! TreeUtils.allDisjoint(nodes) ) { |
| 90 |
throw new RuntimeException("subtrees' taxa overlap"); |
| 91 |
} |
| 92 |
|
| 93 |
if( nodes.length == 0 ) { |
| 94 |
throw new IllegalArgumentException("empty nodes set") ; |
| 95 |
} |
| 96 |
|
| 97 |
for(int attempts = 0; attempts < 1000; ++attempts) { |
| 98 |
SimpleNode[] rootNode = simulateCoalescent(nodes, demographic, 0.0, Double.POSITIVE_INFINITY); |
| 99 |
if( rootNode.length == 1 ) { |
| 100 |
return rootNode[0]; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
throw new RuntimeException("failed to merge trees after 1000 tries."); |
| 105 |
} |
| 106 |
|
| 107 |
public SimpleNode[] simulateCoalescent(SimpleNode[] nodes, DemographicFunction demographic, double currentHeight, |
| 108 |
double maxHeight){ |
| 109 |
return simulateCoalescent(nodes, demographic, currentHeight, maxHeight, false); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
public SimpleNode[] simulateCoalescent(SimpleNode[] nodes, DemographicFunction demographic, double currentHeight, |
| 116 |
double maxHeight, boolean enforceMaxHeight) { |
| 117 |
|
| 118 |
|
| 119 |
if( nodes.length == 1 ) return nodes; |
| 120 |
|
| 121 |
double[] heights = new double[nodes.length]; |
| 122 |
for (int i = 0; i < nodes.length; i++) { |
| 123 |
heights[i] = nodes[i].getHeight(); |
| 124 |
} |
| 125 |
int[] indices = new int[nodes.length]; |
| 126 |
HeapSort.sort(heights, indices); |
| 127 |
|
| 128 |
|
| 129 |
nodeList.clear(); |
| 130 |
activeNodeCount = 0; |
| 131 |
for (int i = 0; i < nodes.length; i++) { |
| 132 |
nodeList.add(nodes[indices[i]]); |
| 133 |
} |
| 134 |
setCurrentHeight(currentHeight); |
| 135 |
|
| 136 |
|
| 137 |
while (getActiveNodeCount() < 2) { |
| 138 |
currentHeight = getMinimumInactiveHeight(); |
| 139 |
setCurrentHeight(currentHeight); |
| 140 |
} |
| 141 |
|
| 142 |
double nextCoalescentHeight; |
| 143 |
|
| 144 |
|
| 145 |
if(!enforceMaxHeight){ |
| 146 |
nextCoalescentHeight = currentHeight + DemographicFunction.Utils.getSimulatedInterval(demographic, |
| 147 |
getActiveNodeCount(), currentHeight); |
| 148 |
} else { |
| 149 |
nextCoalescentHeight = currentHeight + DemographicFunction.Utils.getSimulatedInterval(demographic, |
| 150 |
getActiveNodeCount(), currentHeight, maxHeight); |
| 151 |
} |
| 152 |
|
| 153 |
while (nextCoalescentHeight < maxHeight && (getNodeCount() > 1)) { |
| 154 |
|
| 155 |
if (nextCoalescentHeight >= getMinimumInactiveHeight()) { |
| 156 |
currentHeight = getMinimumInactiveHeight(); |
| 157 |
setCurrentHeight(currentHeight); |
| 158 |
} else { |
| 159 |
currentHeight = nextCoalescentHeight; |
| 160 |
coalesceTwoActiveNodes(currentHeight); |
| 161 |
} |
| 162 |
|
| 163 |
if (getNodeCount() > 1) { |
| 164 |
|
| 165 |
while (getActiveNodeCount() < 2) { |
| 166 |
currentHeight = getMinimumInactiveHeight(); |
| 167 |
setCurrentHeight(currentHeight); |
| 168 |
} |
| 169 |
|
| 170 |
if(!enforceMaxHeight){ |
| 171 |
|
| 172 |
nextCoalescentHeight = currentHeight + DemographicFunction.Utils.getSimulatedInterval(demographic, |
| 173 |
getActiveNodeCount(), currentHeight); |
| 174 |
} else { |
| 175 |
nextCoalescentHeight = currentHeight + DemographicFunction.Utils.getSimulatedInterval(demographic, |
| 176 |
getActiveNodeCount(), currentHeight, maxHeight); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
SimpleNode[] nodesLeft = new SimpleNode[nodeList.size()]; |
| 182 |
for (int i = 0; i < nodesLeft.length; i++) { |
| 183 |
nodesLeft[i] = nodeList.get(i); |
| 184 |
} |
| 185 |
|
| 186 |
return nodesLeft; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
private double getMinimumInactiveHeight() { |
| 193 |
if (activeNodeCount < nodeList.size()) { |
| 194 |
return (nodeList.get(activeNodeCount)).getHeight(); |
| 195 |
} else return Double.POSITIVE_INFINITY; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
private void setCurrentHeight(double height) { |
| 202 |
while (getMinimumInactiveHeight() <= height) { |
| 203 |
activeNodeCount += 1; |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
private int getActiveNodeCount() { |
| 211 |
return activeNodeCount; |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
private int getNodeCount() { |
| 218 |
return nodeList.size(); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
private void coalesceTwoActiveNodes(double height) { |
| 226 |
int node1 = MathUtils.nextInt(activeNodeCount); |
| 227 |
int node2 = node1; |
| 228 |
while (node2 == node1) { |
| 229 |
node2 = MathUtils.nextInt(activeNodeCount); |
| 230 |
} |
| 231 |
|
| 232 |
SimpleNode left = nodeList.get(node1); |
| 233 |
SimpleNode right = nodeList.get(node2); |
| 234 |
|
| 235 |
SimpleNode newNode = new SimpleNode(); |
| 236 |
newNode.setHeight(height); |
| 237 |
newNode.addChild(left); |
| 238 |
newNode.addChild(right); |
| 239 |
|
| 240 |
nodeList.remove(left); |
| 241 |
nodeList.remove(right); |
| 242 |
|
| 243 |
activeNodeCount -= 2; |
| 244 |
|
| 245 |
nodeList.add(activeNodeCount, newNode); |
| 246 |
|
| 247 |
activeNodeCount += 1; |
| 248 |
|
| 249 |
if (getMinimumInactiveHeight() < height) { |
| 250 |
throw new RuntimeException("This should never happen! Somehow the current active node is older than the next inactive node!"); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
private final ArrayList<SimpleNode> nodeList = new ArrayList<SimpleNode>(); |
| 255 |
private int activeNodeCount = 0; |
| 256 |
|
| 257 |
public static void main1(String[] args) { |
| 258 |
|
| 259 |
double[] samplingTimes = { |
| 260 |
0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 |
| 261 |
}; |
| 262 |
|
| 263 |
ExponentialGrowth exponentialGrowth = new ExponentialGrowth(Units.Type.YEARS); |
| 264 |
exponentialGrowth.setN0(10); |
| 265 |
exponentialGrowth.setGrowthRate(0.5); |
| 266 |
|
| 267 |
ConstantPopulation constantPopulation = new ConstantPopulation(Units.Type.YEARS); |
| 268 |
constantPopulation.setN0(10); |
| 269 |
|
| 270 |
Taxa taxa = new Taxa(); |
| 271 |
int i = 1; |
| 272 |
for (double time : samplingTimes) { |
| 273 |
Taxon taxon = new Taxon("tip" + i); |
| 274 |
taxon.setAttribute("date", new Date(time, Units.Type.YEARS, true)); |
| 275 |
i++; |
| 276 |
taxa.addTaxon(taxon); |
| 277 |
} |
| 278 |
CoalescentSimulator simulator = new CoalescentSimulator(); |
| 279 |
Tree tree = simulator.simulateTree(taxa, exponentialGrowth); |
| 280 |
|
| 281 |
List<Double> heights = new ArrayList<Double>(); |
| 282 |
for (int j = 0; j < tree.getInternalNodeCount(); j++) { |
| 283 |
heights.add(tree.getNodeHeight(tree.getInternalNode(j))); |
| 284 |
} |
| 285 |
|
| 286 |
Collections.sort(heights); |
| 287 |
|
| 288 |
for (int j = 0; j < heights.size(); j++) { |
| 289 |
System.out.println(j + "\t" + heights.get(j)); |
| 290 |
} |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
public static void main(String[] args) { |
| 295 |
|
| 296 |
int N = 100000000; |
| 297 |
|
| 298 |
double[] samplingTimes = { |
| 299 |
0.0, 0.0, 0.0, 0.0, 0.0 |
| 300 |
}; |
| 301 |
|
| 302 |
ConstantPopulation constantPopulation = new ConstantPopulation(Units.Type.YEARS); |
| 303 |
constantPopulation.setN0(1); |
| 304 |
|
| 305 |
Taxa taxa = new Taxa(); |
| 306 |
int i = 1; |
| 307 |
for (double time : samplingTimes) { |
| 308 |
Taxon taxon = new Taxon("tip" + i); |
| 309 |
taxon.setAttribute("date", new Date(time, Units.Type.YEARS, true)); |
| 310 |
i++; |
| 311 |
taxa.addTaxon(taxon); |
| 312 |
} |
| 313 |
CoalescentSimulator simulator = new CoalescentSimulator(); |
| 314 |
|
| 315 |
PrintStream out = System.out; |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
NexusExporter exporter = new NexusExporter(out); |
| 322 |
Tree tree = simulator.simulateTree(taxa, constantPopulation); |
| 323 |
Map<String, Integer> idMap = exporter.writeNexusHeader(tree); |
| 324 |
out.println("\t\t;"); |
| 325 |
exporter.writeNexusTree(tree, "TREE_" + 0, true, idMap); |
| 326 |
|
| 327 |
for (i = 1; i < N; i++) { |
| 328 |
tree = simulator.simulateTree(taxa, constantPopulation); |
| 329 |
exporter.writeNexusTree(tree, "TREE_" + i, true, idMap); |
| 330 |
} |
| 331 |
out.println("End;"); |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
} |