| 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.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 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 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 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 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 |
|
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return new SimpleTree(simulateCoalescent(nodes, demoFunctions, colourChangeMatrix)); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 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 |
} |