| 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.evomodel.coalescent; |
| 27 |
|
| 28 |
import dr.evolution.io.Importer; |
| 29 |
import dr.evolution.io.NexusImporter; |
| 30 |
import dr.evolution.io.TreeImporter; |
| 31 |
import dr.evolution.tree.Tree; |
| 32 |
import dr.inference.trace.LogFileTraces; |
| 33 |
import dr.inference.trace.TraceException; |
| 34 |
import dr.stats.DiscreteStatistics; |
| 35 |
import dr.util.HeapSort; |
| 36 |
import dr.util.TabularData; |
| 37 |
|
| 38 |
import java.io.File; |
| 39 |
import java.io.FileReader; |
| 40 |
import java.io.IOException; |
| 41 |
import java.io.PrintWriter; |
| 42 |
import java.util.Arrays; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public class EBSPAnalysis extends TabularData { |
| 48 |
|
| 49 |
private final double[] xPoints; |
| 50 |
private final double[] means; |
| 51 |
private final double[] medians; |
| 52 |
private final double[][] hpdLower; |
| 53 |
private final double[][] hpdHigh; |
| 54 |
private final double[] HPDLevels; |
| 55 |
|
| 56 |
private int[] coalBins; |
| 57 |
|
| 58 |
private final boolean quantiles; |
| 59 |
|
| 60 |
public EBSPAnalysis(File log, File[] treeFiles, VariableDemographicModel.Type modelType, |
| 61 |
String firstColumnName, String firstIndicatorColumnName, |
| 62 |
String rootHeightColumnName, int coalPointBins, double burnIn, |
| 63 |
double[] inHPDLevels, boolean quantiles, boolean logSpace, boolean mid, |
| 64 |
int restrictToNchanges, PrintWriter allDemoWriter) |
| 65 |
throws IOException, Importer.ImportException, TraceException { |
| 66 |
|
| 67 |
LogFileTraces ltraces = new LogFileTraces(log.getCanonicalPath(), log); |
| 68 |
ltraces.loadTraces(); |
| 69 |
ltraces.setBurnIn(0); |
| 70 |
final int runLengthIncludingBurnin = ltraces.getStateCount(); |
| 71 |
|
| 72 |
int intBurnIn = (int) Math.floor(burnIn < 1 ? runLengthIncludingBurnin * burnIn : burnIn); |
| 73 |
final int nStates = runLengthIncludingBurnin - intBurnIn; |
| 74 |
|
| 75 |
ltraces.setBurnIn(intBurnIn * ltraces.getStepSize()); |
| 76 |
|
| 77 |
assert ltraces.getStateCount() == nStates; |
| 78 |
|
| 79 |
this.quantiles = quantiles; |
| 80 |
HPDLevels = (inHPDLevels != null) ? inHPDLevels : new double[]{0.95}; |
| 81 |
|
| 82 |
int populationFirstColumn = -1; |
| 83 |
int indicatorsFirstColumn = -1; |
| 84 |
int rootHeightColumn = -1; |
| 85 |
|
| 86 |
for (int n = 0; n < ltraces.getTraceCount(); ++n) { |
| 87 |
final String traceName = ltraces.getTraceName(n); |
| 88 |
if (traceName.equals(firstColumnName)) { |
| 89 |
populationFirstColumn = n; |
| 90 |
} else if (traceName.equals(firstIndicatorColumnName)) { |
| 91 |
indicatorsFirstColumn = n; |
| 92 |
} else if (rootHeightColumnName != null && traceName.equals(rootHeightColumnName)) { |
| 93 |
rootHeightColumn = n; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
if (populationFirstColumn < 0 || indicatorsFirstColumn < 0) { |
| 98 |
throw new TraceException("incorrect trace column names: unable to find populations/indicators"); |
| 99 |
} |
| 100 |
|
| 101 |
double binSize = 0; |
| 102 |
if (coalPointBins > 0) { |
| 103 |
if (rootHeightColumn < 0) { |
| 104 |
throw new TraceException("incorrect tree height column"); |
| 105 |
} |
| 106 |
double hSum = -0; |
| 107 |
double[] h = new double[1]; |
| 108 |
for (int ns = 0; ns < nStates; ++ns) { |
| 109 |
ltraces.getStateValues(ns, h, rootHeightColumn); |
| 110 |
hSum += h[0]; |
| 111 |
} |
| 112 |
binSize = hSum / (nStates * coalPointBins); |
| 113 |
coalBins = new int[coalPointBins]; |
| 114 |
Arrays.fill(coalBins, 0); |
| 115 |
} |
| 116 |
|
| 117 |
TreeImporter[] treeImporters = new TreeImporter[treeFiles.length]; |
| 118 |
final boolean isStepWise = modelType == VariableDemographicModel.Type.STEPWISE; |
| 119 |
|
| 120 |
int nIndicators = 0; |
| 121 |
|
| 122 |
for (int k = 0; k < treeFiles.length; ++k) { |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
treeImporters[k] = new NexusImporter(new FileReader(treeFiles[k])); |
| 127 |
assert intBurnIn > 0; |
| 128 |
for (int z = 0; z < intBurnIn - 1; ++z) { |
| 129 |
treeImporters[k].importNextTree(); |
| 130 |
} |
| 131 |
nIndicators += treeImporters[k].importNextTree().getExternalNodeCount() - 1; |
| 132 |
} |
| 133 |
|
| 134 |
if (isStepWise) { |
| 135 |
nIndicators -= 1; |
| 136 |
} |
| 137 |
|
| 138 |
final int nXaxisPoints = nIndicators + (isStepWise ? 1 : 0) + 1; |
| 139 |
xPoints = new double[nXaxisPoints]; |
| 140 |
Arrays.fill(xPoints, 0.0); |
| 141 |
|
| 142 |
int nDataPoints = 0; |
| 143 |
VDdemographicFunction[] allDemog = new VDdemographicFunction[nStates]; |
| 144 |
{ |
| 145 |
double[] indicators = new double[nIndicators]; |
| 146 |
double[] pop = new double[nIndicators + 1]; |
| 147 |
Tree[] tt = new Tree[treeFiles.length]; |
| 148 |
|
| 149 |
boolean match = true; |
| 150 |
for (int ns = 0; ns < nStates; ++ns) { |
| 151 |
|
| 152 |
ltraces.getStateValues(ns, indicators, indicatorsFirstColumn); |
| 153 |
ltraces.getStateValues(ns, pop, populationFirstColumn); |
| 154 |
|
| 155 |
if (match) { |
| 156 |
for (int nt = 0; nt < tt.length; ++nt) { |
| 157 |
tt[nt] = treeImporters[nt].importNextTree(); |
| 158 |
if( tt[nt] == null ) { |
| 159 |
throw new TraceException("All NEXUS tree files should contain the same number of states"); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
final String name1 = tt[0].getId(); |
| 165 |
final int state1 = Integer.parseInt(name1.substring(name1.indexOf('_') + 1, name1.length())); |
| 166 |
|
| 167 |
for (int j = 1; j < tt.length; ++j) { |
| 168 |
final String name2 = tt[j].getId(); |
| 169 |
int state2 = Integer.parseInt(name1.substring(name2.indexOf('_') + 1, name2.length())); |
| 170 |
if (state1 != state2) { |
| 171 |
throw new TraceException("NEXUS tree files have different rates or corrupted!!!!"); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
if ((ns + intBurnIn) * ltraces.getStepSize() == state1) { |
| 176 |
match = true; |
| 177 |
final VDdemographicFunction demoFunction = |
| 178 |
new VDdemographicFunction(tt, modelType, indicators, pop, logSpace, mid); |
| 179 |
|
| 180 |
if (restrictToNchanges >= 0 && demoFunction.numberOfChanges() != restrictToNchanges) { |
| 181 |
continue; |
| 182 |
} |
| 183 |
|
| 184 |
double[] xs = demoFunction.allTimePoints(); |
| 185 |
for (int k = 0; k < xs.length; ++k) { |
| 186 |
xPoints[k + 1] += xs[k]; |
| 187 |
} |
| 188 |
if (coalPointBins > 0) { |
| 189 |
for (double x : xs) { |
| 190 |
coalBins[Math.min((int) (x / binSize), coalBins.length - 1)]++; |
| 191 |
} |
| 192 |
} |
| 193 |
allDemog[nDataPoints] = demoFunction; |
| 194 |
++nDataPoints; |
| 195 |
|
| 196 |
demoFunction.freeze(); |
| 197 |
} else { |
| 198 |
match = false; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
for (int k = 0; k < xPoints.length; ++k) { |
| 203 |
xPoints[k] /= nStates; |
| 204 |
} |
| 205 |
|
| 206 |
if (nStates != nDataPoints) { |
| 207 |
|
| 208 |
System.err.println("Different Rates is \"main\" and \"tree\" log files"); |
| 209 |
|
| 210 |
} |
| 211 |
if (nDataPoints < 10) { |
| 212 |
|
| 213 |
System.err.println("Warning!!! Not Sufficient number of data points"); |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
double[] popValues = new double[nDataPoints]; |
| 218 |
means = new double[nXaxisPoints]; |
| 219 |
medians = new double[nXaxisPoints]; |
| 220 |
hpdLower = new double[HPDLevels.length][]; |
| 221 |
hpdHigh = new double[HPDLevels.length][]; |
| 222 |
|
| 223 |
for (int i = 0; i < HPDLevels.length; ++i) { |
| 224 |
hpdLower[i] = new double[nXaxisPoints]; |
| 225 |
hpdHigh[i] = new double[nXaxisPoints]; |
| 226 |
} |
| 227 |
|
| 228 |
for (int nx = 0; nx < xPoints.length; ++nx) { |
| 229 |
final double x = xPoints[nx]; |
| 230 |
|
| 231 |
for (int ns = 0; ns < nDataPoints; ++ns) { |
| 232 |
popValues[ns] = allDemog[ns].getDemographic(x); |
| 233 |
} |
| 234 |
int[] indices = new int[popValues.length]; |
| 235 |
HeapSort.sort(popValues, indices); |
| 236 |
|
| 237 |
means[nx] = DiscreteStatistics.mean(popValues); |
| 238 |
for (int i = 0; i < HPDLevels.length; ++i) { |
| 239 |
if (quantiles) { |
| 240 |
hpdLower[i][nx] = DiscreteStatistics.quantile((1 - HPDLevels[i]) / 2, popValues, indices); |
| 241 |
hpdHigh[i][nx] = DiscreteStatistics.quantile((1 + HPDLevels[i]) / 2, popValues, indices); |
| 242 |
} else { |
| 243 |
final double[] hpd = DiscreteStatistics.HPDInterval(HPDLevels[i], popValues, indices); |
| 244 |
hpdLower[i][nx] = hpd[0]; |
| 245 |
hpdHigh[i][nx] = hpd[1]; |
| 246 |
} |
| 247 |
} |
| 248 |
medians[nx] = DiscreteStatistics.median(popValues, indices); |
| 249 |
} |
| 250 |
|
| 251 |
if( allDemoWriter != null ) { |
| 252 |
for(double xPoint : xPoints) { |
| 253 |
allDemoWriter.print(xPoint); |
| 254 |
allDemoWriter.append(' '); |
| 255 |
} |
| 256 |
|
| 257 |
for (int ns = 0; ns < nDataPoints; ++ns) { |
| 258 |
allDemoWriter.println(); |
| 259 |
for(double xPoint : xPoints) { |
| 260 |
allDemoWriter.print(allDemog[ns].getDemographic(xPoint)); |
| 261 |
allDemoWriter.append(' '); |
| 262 |
} |
| 263 |
} |
| 264 |
allDemoWriter.close(); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
private final String[] columnNames = {"time", "mean", "median"}; |
| 269 |
|
| 270 |
public int nColumns() { |
| 271 |
return columnNames.length + 2 * HPDLevels.length + (coalBins != null ? 1 : 0); |
| 272 |
} |
| 273 |
|
| 274 |
public String columnName(int nColumn) { |
| 275 |
final int fixed = columnNames.length; |
| 276 |
if (nColumn < fixed) { |
| 277 |
return columnNames[nColumn]; |
| 278 |
} |
| 279 |
nColumn -= fixed; |
| 280 |
if (nColumn < 2 * HPDLevels.length) { |
| 281 |
final double p = HPDLevels[nColumn / 2]; |
| 282 |
final String s = (nColumn % 2 == 0) ? "lower" : "upper"; |
| 283 |
return (quantiles ? "cpd " : "hpd ") + s + " " + Math.round(p * 100); |
| 284 |
} |
| 285 |
assert (nColumn - 2 * HPDLevels.length) == 0; |
| 286 |
return "bins"; |
| 287 |
} |
| 288 |
|
| 289 |
public int nRows() { |
| 290 |
return Math.max(xPoints.length, (coalBins != null ? coalBins.length : 0)); |
| 291 |
} |
| 292 |
|
| 293 |
public Object data(int nRow, int nColumn) { |
| 294 |
switch (nColumn) { |
| 295 |
case 0: { |
| 296 |
if (nRow < xPoints.length) { |
| 297 |
return xPoints[nRow]; |
| 298 |
} |
| 299 |
break; |
| 300 |
} |
| 301 |
case 1: { |
| 302 |
if (nRow < means.length) { |
| 303 |
return means[nRow]; |
| 304 |
} |
| 305 |
break; |
| 306 |
} |
| 307 |
case 2: { |
| 308 |
if (nRow < medians.length) { |
| 309 |
return medians[nRow]; |
| 310 |
} |
| 311 |
break; |
| 312 |
} |
| 313 |
default: { |
| 314 |
final int j = nColumn - columnNames.length; |
| 315 |
if (j < 2 * HPDLevels.length) { |
| 316 |
if (nRow < xPoints.length) { |
| 317 |
final int k = j / 2; |
| 318 |
if (0 <= k && k < HPDLevels.length) { |
| 319 |
if (j % 2 == 0) { |
| 320 |
return hpdLower[k][nRow]; |
| 321 |
} else { |
| 322 |
return hpdHigh[k][nRow]; |
| 323 |
} |
| 324 |
} |
| 325 |
} |
| 326 |
} else { |
| 327 |
if (nRow < coalBins.length) { |
| 328 |
return coalBins[nRow]; |
| 329 |
} |
| 330 |
} |
| 331 |
break; |
| 332 |
} |
| 333 |
} |
| 334 |
return ""; |
| 335 |
} |
| 336 |
|
| 337 |
} |