Debian Codegraph @

src/dr/evolution/coalescent/structure/StructuredCoalescentSimulator.java

raw symbols SCIP
1 /*
2 * StructuredCoalescentSimulator.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.evolution.coalescent.structure;
27
28 import dr.evolution.coalescent.DemographicFunction;
29 import dr.evolution.colouring.ColourChangeMatrix;
30 import dr.evolution.tree.SimpleNode;
31 import dr.evolution.tree.SimpleTree;
32 import dr.evolution.tree.Tree;
33 import dr.evolution.util.TaxonList;
34 import dr.evolution.util.TimeScale;
35
36 /**
37 * This class provides the basic engine for coalescent simulation of a given demographic model over a given time period.
38 *
39 * @author Andrew Rambaut
40 * <p/>
41 * $Id: StructuredCoalescentSimulator.java,v 1.7 2006/01/12 11:12:42 rambaut Exp $
42 */
43 public class StructuredCoalescentSimulator {
44
45 public static final String COALESCENT_TREE = "structuredCoalescentTree";
46 public static final String COALESCENT_SIMULATOR = "structuredCoalescentSimulator";
47 public static final String ROOT_HEIGHT = "rootHeight";
48
49 public StructuredCoalescentSimulator() {
50 }
51
52
53 /**
54 * Simulates a coalescent tree, given a taxon list.
55 *
56 * @param taxa the set of taxa to simulate a coalescent tree between
57 * @param demoFunctions the demographic function to use
58 */
59 public Tree simulateTree(TaxonList[] taxa, DemographicFunction[] demoFunctions, ColourChangeMatrix colourChangeMatrix) {
60
61 SimpleNode[][] nodes = new SimpleNode[taxa.length][];
62 for (int i = 0; i < taxa.length; i++) {
63 nodes[i] = new SimpleNode[taxa[i].getTaxonCount()];
64 for (int j = 0; j < taxa[i].getTaxonCount(); j++) {
65 nodes[i][j] = new SimpleNode();
66 nodes[i][j].setTaxon(taxa[i].getTaxon(j));
67 }
68 }
69
70 dr.evolution.util.Date mostRecent = null;
71 boolean usingDates = false;
72
73 for (int i = 0; i < taxa.length; i++) {
74 for (int j = 0; j < taxa[i].getTaxonCount(); j++) {
75 if (TaxonList.Utils.hasAttribute(taxa[i], j, dr.evolution.util.Date.DATE)) {
76 usingDates = true;
77 dr.evolution.util.Date date = (dr.evolution.util.Date) taxa[i].getTaxonAttribute(j, dr.evolution.util.Date.DATE);
78 if ((date != null) && (mostRecent == null || date.after(mostRecent))) {
79 mostRecent = date;
80 }
81 } else {
82 // assume contemporaneous tips
83 nodes[i][j].setHeight(0.0);
84 }
85 }
86 }
87
88 if (usingDates) {
89 assert mostRecent != null;
90 TimeScale timeScale = new TimeScale(mostRecent.getUnits(), true, mostRecent.getAbsoluteTimeValue());
91
92 for (int i = 0; i < taxa.length; i++) {
93 for (int j = 0; j < taxa[i].getTaxonCount(); j++) {
94 dr.evolution.util.Date date = (dr.evolution.util.Date) taxa[i].getTaxonAttribute(j, dr.evolution.util.Date.DATE);
95
96 if (date == null) {
97 throw new IllegalArgumentException("Taxon, " + taxa[i].getTaxonId(j) + ", is missing its date");
98 }
99
100 nodes[i][j].setHeight(timeScale.convertTime(date.getTimeValue(), date));
101 }
102 if (demoFunctions[0].getUnits() != mostRecent.getUnits()) {
103 //throw new IllegalArgumentException("The units of the demographic model and the most recent date must match!");
104 }
105 }
106 }
107
108 return new SimpleTree(simulateCoalescent(nodes, demoFunctions, colourChangeMatrix));
109 }
110
111 /**
112 * @return the root node of the given array of nodes after simulation of the coalescent under the given demographic model.
113 */
114 public SimpleNode simulateCoalescent(SimpleNode[][] nodes, DemographicFunction[] demographic, ColourChangeMatrix colourChangeMatrix) {
115
116
117 SimpleNode[] rootNode = simulateCoalescent(nodes, demographic, colourChangeMatrix, 0.0, Double.POSITIVE_INFINITY);
118
119 int attempts = 0;
120 while (rootNode.length > 1 && attempts < 1000) {
121 rootNode = simulateCoalescent(nodes, demographic, colourChangeMatrix, 0.0, Double.POSITIVE_INFINITY);
122 attempts += 1;
123 }
124
125 if (rootNode.length > 1) {
126 throw new RuntimeException(rootNode.length + " nodes found where there should have been 1, after 1000 tries!");
127 }
128
129 return rootNode[0];
130 }
131
132 public SimpleNode[] simulateCoalescent(SimpleNode[][] nodes, DemographicFunction[] demographic, ColourChangeMatrix colourChangeMatrix, double currentHeight, double maxHeight) {
133
134 return null;
135 }
136
137 }