| 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.tools; |
| 27 |
|
| 28 |
import dr.inference.trace.LogFileTraces; |
| 29 |
import dr.inference.trace.TraceDistribution; |
| 30 |
import dr.inference.trace.TraceException; |
| 31 |
import dr.inference.trace.TraceList; |
| 32 |
import dr.stats.Variate; |
| 33 |
import jebl.evolution.coalescent.IntervalList; |
| 34 |
import jebl.evolution.coalescent.Intervals; |
| 35 |
import jebl.evolution.io.ImportException; |
| 36 |
import jebl.evolution.io.NewickImporter; |
| 37 |
import jebl.evolution.io.NexusImporter; |
| 38 |
import jebl.evolution.io.TreeImporter; |
| 39 |
import jebl.evolution.trees.RootedTree; |
| 40 |
|
| 41 |
import java.io.BufferedReader; |
| 42 |
import java.io.File; |
| 43 |
import java.io.FileReader; |
| 44 |
import java.io.IOException; |
| 45 |
import java.util.ArrayList; |
| 46 |
import java.util.List; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public class SkylineReconstructor { |
| 53 |
|
| 54 |
private int binCount; |
| 55 |
private double minTime; |
| 56 |
private double maxTime; |
| 57 |
private double ageOfYoungest; |
| 58 |
|
| 59 |
private Variate xData = new Variate.D(); |
| 60 |
private Variate yDataMean = new Variate.D(); |
| 61 |
private Variate yDataMedian = new Variate.D(); |
| 62 |
private Variate yDataUpper = new Variate.D(); |
| 63 |
private Variate yDataLower = new Variate.D(); |
| 64 |
|
| 65 |
public SkylineReconstructor(File logFile, File treeFile, int burnin, |
| 66 |
int binCount, double minTime, double maxTime, double ageOfYoungest) |
| 67 |
throws IOException, ImportException, TraceException { |
| 68 |
|
| 69 |
this.binCount = binCount; |
| 70 |
this.minTime = minTime; |
| 71 |
this.maxTime = maxTime; |
| 72 |
this.ageOfYoungest = ageOfYoungest; |
| 73 |
|
| 74 |
LogFileTraces traces = new LogFileTraces(logFile.getName(), logFile); |
| 75 |
traces.loadTraces(); |
| 76 |
traces.setBurnIn(burnin); |
| 77 |
|
| 78 |
int stateCount = traces.getStateCount(); |
| 79 |
|
| 80 |
int firstPopSize = findArgument(traces, "popSize"); |
| 81 |
int popSizeCount = getTraceRange(traces, firstPopSize); |
| 82 |
int firstGroupSize = findArgument(traces, "groupSize"); |
| 83 |
int groupSizeCount = getTraceRange(traces, firstGroupSize); |
| 84 |
|
| 85 |
boolean isLinear = (groupSizeCount == popSizeCount - 1); |
| 86 |
if (!isLinear && groupSizeCount != popSizeCount) { |
| 87 |
if (isLinear) { |
| 88 |
if (groupSizeCount != popSizeCount - 1) { |
| 89 |
throw new TraceException("For the stepwise (constant) Bayesian skyline model there should " + |
| 90 |
"be the same number of group size as population size parameters."); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
ArrayList<ArrayList> popSizes = new ArrayList<ArrayList>(); |
| 96 |
ArrayList<ArrayList> groupSizes = new ArrayList<ArrayList>(); |
| 97 |
|
| 98 |
for (int i = 0; i < popSizeCount; i++) { |
| 99 |
popSizes.add(new ArrayList(traces.getValues(firstPopSize + i))); |
| 100 |
} |
| 101 |
for (int i = 0; i < groupSizeCount; i++) { |
| 102 |
groupSizes.add(new ArrayList(traces.getValues(firstGroupSize + i))); |
| 103 |
} |
| 104 |
|
| 105 |
List heights = traces.getValues(traces.getTraceIndex("treeModel.rootHeight")); |
| 106 |
TraceDistribution distribution = new TraceDistribution(heights, |
| 107 |
traces.getTrace(traces.getTraceIndex("treeModel.rootHeight")).getTraceType()); |
| 108 |
|
| 109 |
double timeMean = distribution.getMean(); |
| 110 |
double timeMedian = distribution.getMedian(); |
| 111 |
double timeUpper = distribution.getUpperHPD(); |
| 112 |
double timeLower = distribution.getLowerHPD(); |
| 113 |
|
| 114 |
|
| 115 |
double maxHeight = maxTime; |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
BufferedReader reader = new BufferedReader(new FileReader(treeFile)); |
| 133 |
|
| 134 |
String line = reader.readLine(); |
| 135 |
|
| 136 |
TreeImporter importer; |
| 137 |
if (line.toUpperCase().startsWith("#NEXUS")) { |
| 138 |
importer = new NexusImporter(reader); |
| 139 |
} else { |
| 140 |
importer = new NewickImporter(reader, false); |
| 141 |
} |
| 142 |
|
| 143 |
double delta = maxHeight / (binCount - 1); |
| 144 |
int skip = (int) (burnin / traces.getStepSize()); |
| 145 |
int state = 0; |
| 146 |
|
| 147 |
while (importer.hasTree() && state < skip) { |
| 148 |
importer.importNextTree(); |
| 149 |
state += 1; |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
double[][] groupTimes = new double[stateCount][]; |
| 154 |
|
| 155 |
state = 0; |
| 156 |
|
| 157 |
while (importer.hasTree()) { |
| 158 |
RootedTree tree = (RootedTree) importer.importNextTree(); |
| 159 |
IntervalList intervals = new Intervals(tree); |
| 160 |
int intervalCount = intervals.getIntervalCount(); |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
groupTimes[state] = new double[groupSizeCount]; |
| 165 |
double totalTime = 0.0; |
| 166 |
int groupSize = 1; |
| 167 |
int groupIndex = 0; |
| 168 |
int subIndex = 0; |
| 169 |
if (firstGroupSize > 0) { |
| 170 |
double g = (Double) groupSizes.get(groupIndex).get(state); |
| 171 |
if (g != Math.round(g)) { |
| 172 |
throw new RuntimeException("Group size " + groupIndex + " should be integer but found:" + g); |
| 173 |
} else groupSize = (int) Math.round(g); |
| 174 |
} |
| 175 |
|
| 176 |
for (int j = 0; j < intervalCount; j++) { |
| 177 |
|
| 178 |
totalTime += intervals.getInterval(j); |
| 179 |
|
| 180 |
if (intervals.getIntervalType(j) == IntervalList.IntervalType.COALESCENT) { |
| 181 |
subIndex += 1; |
| 182 |
if (subIndex == groupSize) { |
| 183 |
groupTimes[state][groupIndex] = totalTime; |
| 184 |
subIndex = 0; |
| 185 |
groupIndex += 1; |
| 186 |
if (groupIndex < groupSizeCount) { |
| 187 |
double g = (Double) groupSizes.get(groupIndex).get(state); |
| 188 |
if (g != Math.round(g)) { |
| 189 |
throw new RuntimeException("Group size " + groupIndex + " should be integer but found:" + g); |
| 190 |
} else groupSize = (int) Math.round(g); |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
int diff = intervals.getCoalescentEvents(j) - 1; |
| 197 |
if (diff > 0) |
| 198 |
throw new RuntimeException("Don't handle multifurcations!"); |
| 199 |
} |
| 200 |
|
| 201 |
state += 1; |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
Variate[] bins = new Variate[binCount]; |
| 206 |
double height = 0.0; |
| 207 |
|
| 208 |
for (int k = 0; k < binCount; k++) { |
| 209 |
bins[k] = new Variate.D(); |
| 210 |
|
| 211 |
if (height >= 0.0 && height <= maxHeight) { |
| 212 |
for (state = 0; state < stateCount; state++) { |
| 213 |
|
| 214 |
if (isLinear) { |
| 215 |
double lastGroupTime = 0.0; |
| 216 |
|
| 217 |
int index = 0; |
| 218 |
while (index < groupTimes[state].length && groupTimes[state][index] < height) { |
| 219 |
lastGroupTime = groupTimes[state][index]; |
| 220 |
index += 1; |
| 221 |
} |
| 222 |
|
| 223 |
if (index < groupTimes[state].length - 1) { |
| 224 |
double t = (height - lastGroupTime) / (groupTimes[state][index] - lastGroupTime); |
| 225 |
double p1 = (Double) groupSizes.get(index).get(state); |
| 226 |
double p2 = (Double) groupSizes.get(index + 1).get(state); |
| 227 |
double popsize = p1 + ((p2 - p1) * t); |
| 228 |
bins[k].add(popsize); |
| 229 |
} |
| 230 |
} else { |
| 231 |
int index = 0; |
| 232 |
while (index < groupTimes[state].length && groupTimes[state][index] < height) { |
| 233 |
index += 1; |
| 234 |
} |
| 235 |
|
| 236 |
if (index < groupTimes[state].length) { |
| 237 |
double popSize = (Double) groupSizes.get(index).get(state); |
| 238 |
if (popSize == 0.0) { |
| 239 |
throw new RuntimeException("Zero pop size"); |
| 240 |
} |
| 241 |
|
| 242 |
bins[k].add(popSize); |
| 243 |
} else { |
| 244 |
|
| 245 |
|
| 246 |
} |
| 247 |
} |
| 248 |
} |
| 249 |
} |
| 250 |
height += delta; |
| 251 |
} |
| 252 |
|
| 253 |
double t = 0.0; |
| 254 |
|
| 255 |
for (Variate bin : bins) { |
| 256 |
xData.add(t); |
| 257 |
if (bin.getCount() > 0) { |
| 258 |
yDataMean.add(bin.getMean()); |
| 259 |
yDataMedian.add(bin.getQuantile(0.5)); |
| 260 |
yDataLower.add(bin.getQuantile(0.025)); |
| 261 |
yDataUpper.add(bin.getQuantile(0.975)); |
| 262 |
} else { |
| 263 |
yDataMean.add(Double.NaN); |
| 264 |
yDataMedian.add(Double.NaN); |
| 265 |
yDataLower.add(Double.NaN); |
| 266 |
yDataUpper.add(Double.NaN); |
| 267 |
} |
| 268 |
t += delta; |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
public Variate getXData() { |
| 273 |
return xData; |
| 274 |
} |
| 275 |
|
| 276 |
public Variate getYDataMean() { |
| 277 |
return yDataMean; |
| 278 |
} |
| 279 |
|
| 280 |
public Variate getYDataUpper() { |
| 281 |
return yDataUpper; |
| 282 |
} |
| 283 |
|
| 284 |
public Variate getYDataMedian() { |
| 285 |
return yDataMedian; |
| 286 |
} |
| 287 |
|
| 288 |
public Variate getYDataLower() { |
| 289 |
return yDataLower; |
| 290 |
} |
| 291 |
|
| 292 |
private int findArgument(TraceList traceList, String argument) { |
| 293 |
for (int j = 0; j < traceList.getTraceCount(); j++) { |
| 294 |
String statistic = traceList.getTraceName(j); |
| 295 |
String suffix = getNumericalSuffix(statistic); |
| 296 |
if ((suffix.length() == 0 || suffix.equals("1")) && statistic.substring(0, statistic.length() - 1).contains(argument)) { |
| 297 |
return j; |
| 298 |
} |
| 299 |
} |
| 300 |
return -1; |
| 301 |
} |
| 302 |
|
| 303 |
private String getNumericalSuffix(String argument) { |
| 304 |
int i = argument.length() - 1; |
| 305 |
|
| 306 |
if (i < 0) return ""; |
| 307 |
|
| 308 |
char ch = argument.charAt(i); |
| 309 |
|
| 310 |
if (!Character.isDigit(ch)) return ""; |
| 311 |
|
| 312 |
while (i > 0 && Character.isDigit(ch)) { |
| 313 |
i -= 1; |
| 314 |
ch = argument.charAt(i); |
| 315 |
} |
| 316 |
|
| 317 |
return argument.substring(i + 1, argument.length()); |
| 318 |
} |
| 319 |
|
| 320 |
private int getTraceRange(TraceList traceList, int first) { |
| 321 |
int i = 1; |
| 322 |
int k = first; |
| 323 |
|
| 324 |
String name = traceList.getTraceName(first); |
| 325 |
String root = name.substring(0, name.length() - 1); |
| 326 |
while (k < traceList.getTraceCount() && traceList.getTraceName(k).equals(root + i)) { |
| 327 |
i++; |
| 328 |
k++; |
| 329 |
} |
| 330 |
|
| 331 |
return i - 1; |
| 332 |
} |
| 333 |
|
| 334 |
public static void main(String[] argv) { |
| 335 |
|
| 336 |
Variate x = null; |
| 337 |
List<Variate> plots = new ArrayList<Variate>(); |
| 338 |
|
| 339 |
for (int i = 1; i <= 200; i++) { |
| 340 |
String stem = "sim" + (i < 10 ? "00" : (i < 100 ? "0" : "")) + i; |
| 341 |
|
| 342 |
try { |
| 343 |
SkylineReconstructor skyline = new SkylineReconstructor( |
| 344 |
new File(stem + ".log"), |
| 345 |
new File(stem + ".trees"), |
| 346 |
1000000, |
| 347 |
200, |
| 348 |
0.0, |
| 349 |
150000, |
| 350 |
0.0 |
| 351 |
); |
| 352 |
if (x == null) { |
| 353 |
x = skyline.getXData(); |
| 354 |
} |
| 355 |
plots.add(skyline.getYDataMean()); |
| 356 |
|
| 357 |
} catch (IOException e) { |
| 358 |
e.printStackTrace(); |
| 359 |
} catch (ImportException e) { |
| 360 |
e.printStackTrace(); |
| 361 |
} catch (TraceException e) { |
| 362 |
e.printStackTrace(); |
| 363 |
} |
| 364 |
if (i % 10 == 0) { |
| 365 |
System.err.println("Read " + i); |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
for (int i = 0; i < x.getCount(); i++) { |
| 370 |
System.out.print(x.get(i)); |
| 371 |
|
| 372 |
for (Variate y : plots) { |
| 373 |
System.out.print("\t" + y.get(i)); |
| 374 |
} |
| 375 |
System.out.println(); |
| 376 |
} |
| 377 |
} |
| 378 |
} |