| 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.app.coalgen; |
| 27 |
|
| 28 |
import dr.evolution.io.Importer; |
| 29 |
import dr.inference.trace.LogFileTraces; |
| 30 |
import dr.inference.trace.TraceException; |
| 31 |
import jam.framework.SingleDocApplication; |
| 32 |
import jebl.evolution.coalescent.EmpiricalDemographicFunction; |
| 33 |
import jebl.evolution.graphs.Node; |
| 34 |
import jebl.evolution.io.ImportException; |
| 35 |
import jebl.evolution.io.NewickExporter; |
| 36 |
import jebl.evolution.io.NexusImporter; |
| 37 |
import jebl.evolution.io.TreeImporter; |
| 38 |
import jebl.evolution.taxa.Taxon; |
| 39 |
import jebl.evolution.trees.RootedTree; |
| 40 |
import jebl.evolution.treesimulation.CoalescentIntervalGenerator; |
| 41 |
import jebl.evolution.treesimulation.IntervalGenerator; |
| 42 |
import jebl.evolution.treesimulation.TreeSimulator; |
| 43 |
|
| 44 |
import javax.swing.*; |
| 45 |
import java.io.*; |
| 46 |
import java.util.Arrays; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class CoalGenApp { |
| 54 |
public CoalGenApp() { |
| 55 |
|
| 56 |
try { |
| 57 |
|
| 58 |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); |
| 59 |
|
| 60 |
java.net.URL url = CoalGenApp.class.getResource("/images/CoalGen.png"); |
| 61 |
Icon icon = null; |
| 62 |
|
| 63 |
if (url != null) { |
| 64 |
icon = new ImageIcon(url); |
| 65 |
} |
| 66 |
|
| 67 |
String nameString = "CoalGen"; |
| 68 |
String aboutString = "Coalescent Tree Simulator\nVersion 2.0\n \nCopyright 2004-2009 Andrew Rambaut and Alexei Drummond\nAll Rights Reserved."; |
| 69 |
|
| 70 |
|
| 71 |
SingleDocApplication app = new SingleDocApplication(new CoalGenMenuFactory(), nameString, aboutString, icon); |
| 72 |
|
| 73 |
|
| 74 |
CoalGenFrame frame = new CoalGenFrame(nameString); |
| 75 |
app.setDocumentFrame(frame); |
| 76 |
|
| 77 |
} catch (Exception e) { |
| 78 |
e.printStackTrace(); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
public static void simulate(String inputFileName, String treesFileName, String outputFileName) throws IOException, TraceException, Importer.ImportException { |
| 83 |
File logFile = new File(inputFileName); |
| 84 |
|
| 85 |
System.out.println("Loading trace file: " + inputFileName); |
| 86 |
LogFileTraces traces = new LogFileTraces(inputFileName, logFile); |
| 87 |
traces.loadTraces(); |
| 88 |
traces.setBurnIn(0); |
| 89 |
System.out.println(traces.getStateCount() + " states loaded"); |
| 90 |
|
| 91 |
System.out.println(); |
| 92 |
System.out.println("Opening trees file: " + treesFileName); |
| 93 |
|
| 94 |
|
| 95 |
System.out.println("Simulating..."); |
| 96 |
System.out.println("0 25 50 75 100"); |
| 97 |
System.out.println("|--------------|--------------|--------------|--------------|"); |
| 98 |
|
| 99 |
int stepSize = traces.getStateCount() / 60; |
| 100 |
if (stepSize < 1) stepSize = 1; |
| 101 |
|
| 102 |
PrintWriter writer = new PrintWriter(new FileWriter(outputFileName)); |
| 103 |
|
| 104 |
FileReader fileReader = new FileReader(treesFileName); |
| 105 |
TreeImporter importer = new NexusImporter(fileReader); |
| 106 |
|
| 107 |
EmpiricalDemographicFunction demo = null; |
| 108 |
IntervalGenerator intervals = null; |
| 109 |
TreeSimulator sim = null; |
| 110 |
|
| 111 |
CoalGenData data = new CoalGenData(); |
| 112 |
data.traces = traces; |
| 113 |
data.setDemographicModel(7); |
| 114 |
data.setupSkyline(); |
| 115 |
|
| 116 |
double[] popSizes = new double[data.popSizeCount]; |
| 117 |
double[] groupSizes = new double[data.groupSizeCount]; |
| 118 |
|
| 119 |
|
| 120 |
NewickExporter exporter = new NewickExporter(writer); |
| 121 |
int count = 0; |
| 122 |
try { |
| 123 |
while (importer.hasTree()) { |
| 124 |
RootedTree inTree = (RootedTree)importer.importNextTree(); |
| 125 |
|
| 126 |
if (sim == null) { |
| 127 |
setSamplingTimes(inTree); |
| 128 |
sim = new TreeSimulator(inTree.getTaxa(), "date"); |
| 129 |
} |
| 130 |
|
| 131 |
data.getNextSkyline(popSizes, groupSizes); |
| 132 |
|
| 133 |
double[] times = getTimes(inTree, groupSizes); |
| 134 |
demo = new EmpiricalDemographicFunction(popSizes, times, true); |
| 135 |
intervals = new CoalescentIntervalGenerator(demo); |
| 136 |
|
| 137 |
RootedTree outTree = sim.simulate(intervals); |
| 138 |
|
| 139 |
exporter.exportTree(outTree); |
| 140 |
writer.println(); |
| 141 |
writer.flush(); |
| 142 |
|
| 143 |
if (count > 0 && count % stepSize == 0) { |
| 144 |
System.out.print("*"); |
| 145 |
System.out.flush(); |
| 146 |
} |
| 147 |
count++; |
| 148 |
} |
| 149 |
} catch (ImportException e) { |
| 150 |
e.printStackTrace(); |
| 151 |
} |
| 152 |
|
| 153 |
fileReader.close(); |
| 154 |
|
| 155 |
|
| 156 |
writer.close(); |
| 157 |
|
| 158 |
} |
| 159 |
|
| 160 |
private static void setSamplingTimes(final RootedTree tree) { |
| 161 |
setSamplingTimes(tree, tree.getRootNode(), 0.0); |
| 162 |
|
| 163 |
|
| 164 |
double maxLength = 0.0; |
| 165 |
for (Taxon taxon : tree.getTaxa()) { |
| 166 |
double length = (Double)taxon.getAttribute("length"); |
| 167 |
if (length > maxLength) { |
| 168 |
maxLength = length; |
| 169 |
} |
| 170 |
} |
| 171 |
for (Taxon taxon : tree.getTaxa()) { |
| 172 |
double length = (Double)taxon.getAttribute("length"); |
| 173 |
taxon.setAttribute("date", maxLength - length); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
private static void setSamplingTimes(final RootedTree tree, final Node node, double length) { |
| 178 |
if (tree.isExternal(node)) { |
| 179 |
tree.getTaxon(node).setAttribute("length", length); |
| 180 |
} else { |
| 181 |
for (Node child : tree.getChildren(node)) { |
| 182 |
double l = tree.getLength(child); |
| 183 |
setSamplingTimes(tree, child, length + l); |
| 184 |
} |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
private static double[] getTimes(final RootedTree tree, final double[] groupSizes) { |
| 189 |
double[] heights = new double[tree.getInternalNodes().size()]; |
| 190 |
|
| 191 |
int i = 0; |
| 192 |
for (Node node : tree.getInternalNodes()) { |
| 193 |
heights[i] = tree.getHeight(node); |
| 194 |
i++; |
| 195 |
} |
| 196 |
Arrays.sort(heights); |
| 197 |
|
| 198 |
if (groupSizes != null) { |
| 199 |
double[] allHeights = heights; |
| 200 |
heights = new double[groupSizes.length]; |
| 201 |
int k = 0; |
| 202 |
for (int j = 0; j < groupSizes.length; j++) { |
| 203 |
k += groupSizes[j]; |
| 204 |
heights[j] = allHeights[k - 1]; |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
return heights; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
static public void main(String[] args) { |
| 214 |
if (args.length > 1) { |
| 215 |
|
| 216 |
if (args.length != 3) { |
| 217 |
System.err.println("Usage: coalgen <input_file> <tree_file> <output_file>"); |
| 218 |
return; |
| 219 |
} |
| 220 |
|
| 221 |
String inputFileName = args[0]; |
| 222 |
String treeFileName = args[1]; |
| 223 |
String outputFileName = args[2]; |
| 224 |
|
| 225 |
try { |
| 226 |
CoalGenApp.simulate(inputFileName, treeFileName, outputFileName); |
| 227 |
} catch (IOException e) { |
| 228 |
e.printStackTrace(); |
| 229 |
} catch (TraceException e) { |
| 230 |
e.printStackTrace(); |
| 231 |
} catch (Importer.ImportException e) { |
| 232 |
e.printStackTrace(); |
| 233 |
} |
| 234 |
|
| 235 |
} else { |
| 236 |
System.setProperty("com.apple.macos.useScreenMenuBar", "true"); |
| 237 |
System.setProperty("apple.laf.useScreenMenuBar", "true"); |
| 238 |
|
| 239 |
new CoalGenApp(); |
| 240 |
} |
| 241 |
} |
| 242 |
} |