Debian Codegraph @

src/dr/app/coalgen/CoalGenApp.java

raw symbols SCIP
1 /*
2 * CoalGenApp.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.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 * @author Andrew Rambaut
50 * @author Alexei Drummond
51 * @version $Id$
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 // BufferedReader reader = new BufferedReader(new FileReader(treesFileName));
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); // const stepwise bsp
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 // the dates are distances from root so make them heights
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 // Main entry point
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 }