Debian Codegraph @

src/dr/evolution/coalescent/PLCoalescentSimulator.java

raw symbols SCIP
1 /*
2 * PLCoalescentSimulator.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;
27
28 import dr.evolution.tree.Tree;
29 import dr.evolution.tree.TreeUtils;
30 import dr.evolution.util.Date;
31 import dr.evolution.util.Taxa;
32 import dr.evolution.util.Taxon;
33 import dr.evolution.util.Units;
34
35 import java.io.*;
36 import java.util.ArrayList;
37 import java.util.List;
38
39 /**
40 * @author Alexei Drummond
41 */
42 public class PLCoalescentSimulator {
43 private static PrintStream debug = System.err;
44
45 public static void main(String[] arg) throws IOException {
46
47 // READ DEMOGRAPHIC FUNCTION
48
49 String filename = arg[0];
50 BufferedReader reader = new BufferedReader(new FileReader(filename));
51
52 double popSizeScale = 1.0;
53 double generationTime = 1.0;
54 if (arg.length > 2) {
55 popSizeScale = Double.parseDouble(arg[2]);
56 }
57 if (arg.length > 3) {
58 generationTime = Double.parseDouble(arg[3]);
59 }
60
61 PrintWriter populationFuncLogger = null;
62 if (arg.length > 5) {
63 String logFileName = arg[5];
64 if (logFileName.equals("-")) {
65 populationFuncLogger = new PrintWriter(System.out);
66 } else {
67 populationFuncLogger = new PrintWriter(new FileWriter(logFileName));
68 }
69 }
70
71 List<Double> times = new ArrayList<Double>();
72
73 String line = reader.readLine();
74 String[] tokens = line.trim().split("[\t ]+");
75 if (tokens.length < 2) throw new RuntimeException();
76
77 ArrayList<ArrayList> popSizes = new ArrayList<ArrayList>();
78 while (line != null) {
79 double time = Double.parseDouble(tokens[0]) / generationTime;
80 times.add(time);
81
82 for (int i = 1; i < tokens.length; i++) {
83 popSizes.add(new ArrayList<Double>());
84 popSizes.get(i - 1).add(Double.parseDouble(tokens[i]));
85 }
86 line = reader.readLine();
87 if (line != null) {
88 tokens = line.trim().split("[\t ]+");
89 if (tokens.length != popSizes.size() + 1) throw new RuntimeException();
90 }
91 }
92
93 reader.close();
94
95 // READ SAMPLE TIMES
96
97 String samplesFilename = arg[1];
98
99 reader = new BufferedReader(new FileReader(samplesFilename));
100
101 line = reader.readLine();
102 Taxa taxa = new Taxa();
103 int id = 0;
104 while (line != null) {
105
106 if (!line.startsWith("#")) {
107
108 tokens = line.split("[\t ]+");
109
110 if (tokens.length == 4) {
111
112 double t0 = Double.parseDouble(tokens[0]);
113 double t1 = Double.parseDouble(tokens[1]);
114 double dt = Double.parseDouble(tokens[2]);
115 int k = Integer.parseInt(tokens[3]);
116 for (double time = t0; time <= t1; time += dt) {
117
118 double sampleTime = time / generationTime;
119 for (int i = 0; i < k; i++) {
120 Taxon taxon = new Taxon("t" + id);
121 taxon.setAttribute(dr.evolution.util.Date.DATE, new Date(sampleTime, Units.Type.GENERATIONS, true));
122 taxa.addTaxon(taxon);
123 id += 1;
124 }
125 }
126
127 } else {
128
129 // sample times are in the same units as simulation
130 double sampleTime = Double.parseDouble(tokens[0]) / generationTime;
131 int count = Integer.parseInt(tokens[1]);
132
133 for (int i = 0; i < count; i++) {
134 Taxon taxon = new Taxon(id + "");
135 taxon.setAttribute(dr.evolution.util.Date.DATE, new Date(sampleTime, Units.Type.GENERATIONS, true));
136 taxa.addTaxon(taxon);
137 id += 1;
138 }
139 }
140 }
141 line = reader.readLine();
142 }
143
144 double minTheta = Double.MAX_VALUE;
145 double maxTheta = 0.0;
146
147 PrintWriter out;
148 if (arg.length > 4) {
149 out = new PrintWriter(new FileWriter(arg[4]));
150 } else {
151 out = new PrintWriter(System.out);
152 }
153
154 int pp = 0;
155 for (List<Double> popSize : popSizes) {
156 double[] thetas = new double[popSize.size()];
157 double[] intervals = new double[times.size() - 1];
158
159 if (populationFuncLogger != null) {
160 populationFuncLogger.println("# " + pp);
161 ++pp;
162 }
163
164 // must reverse the direction of the model
165 for (int j = intervals.length; j > 0; j--) {
166 intervals[intervals.length - j] = times.get(j) - times.get(j - 1);
167
168 final double theta = popSize.get(j) * popSizeScale;
169 thetas[intervals.length - j] = theta;
170 if (theta < minTheta) {
171 minTheta = theta;
172 }
173 if (theta > maxTheta) {
174 maxTheta = theta;
175 }
176
177 final double t = times.get(intervals.length) - times.get(j);
178 if (populationFuncLogger != null) {
179 populationFuncLogger.println(t + "\t" + theta);
180 }
181 }
182
183 if (debug != null) {
184 debug.println("min theta = " + minTheta);
185 debug.println("max theta = " + maxTheta);
186 }
187
188 PiecewiseLinearPopulation demo = new PiecewiseLinearPopulation(intervals, thetas, Units.Type.GENERATIONS);
189
190 CoalescentSimulator simulator = new CoalescentSimulator();
191 Tree tree = simulator.simulateTree(taxa, demo);
192
193 out.println(TreeUtils.newick(tree));
194 if (debug != null) {
195 debug.println(TreeUtils.newick(tree));
196 }
197 }
198
199 if (populationFuncLogger != null) {
200 populationFuncLogger.flush();
201 populationFuncLogger.close();
202 }
203
204 out.flush();
205 out.close();
206 }
207 }