| 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.inference.model.Parameter; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
import dr.inference.trace.LogFileTraces; |
| 37 |
import dr.inference.trace.TraceException; |
| 38 |
import dr.evomodel.coalescent.operators.GaussianProcessSkytrackBlockUpdateOperator; |
| 39 |
|
| 40 |
import dr.stats.DiscreteStatistics; |
| 41 |
|
| 42 |
|
| 43 |
import dr.util.TabularData; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
import java.io.*; |
| 49 |
|
| 50 |
import java.util.Arrays; |
| 51 |
import java.util.StringTokenizer; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public class GPSkytrackAnalysis extends TabularData { |
| 57 |
|
| 58 |
|
| 59 |
private final double[] xPoints; |
| 60 |
private final double[] means; |
| 61 |
private final double[] medians; |
| 62 |
private final double[] hpdLower; |
| 63 |
private final double[] hpdHigh; |
| 64 |
private final double [][] gValues; |
| 65 |
private final double [][] tValues; |
| 66 |
private final double [][] newGvalues; |
| 67 |
private final double [][] popValues; |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
private Parameter numGridPoints; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public GPSkytrackAnalysis(File log, double burnIn, Parameter numGridPoints) throws IOException, Importer.ImportException, TraceException { |
| 89 |
GaussianProcessSkytrackBlockUpdateOperator GPOperator=new GaussianProcessSkytrackBlockUpdateOperator(); |
| 90 |
this.numGridPoints=numGridPoints; |
| 91 |
LogFileTraces ltraces = new LogFileTraces(log.getCanonicalPath(), log); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
ltraces.loadTraces(); |
| 96 |
|
| 97 |
|
| 98 |
ltraces.setBurnIn(0); |
| 99 |
final int runLengthIncludingBurnin = ltraces.getStateCount(); |
| 100 |
|
| 101 |
int intBurnIn = (int) Math.floor(burnIn < 1 ? runLengthIncludingBurnin * burnIn : burnIn); |
| 102 |
final int nStates = runLengthIncludingBurnin - intBurnIn; |
| 103 |
ltraces.setBurnIn(intBurnIn * ltraces.getStepSize()); |
| 104 |
assert ltraces.getStateCount() == nStates; |
| 105 |
|
| 106 |
|
| 107 |
xPoints = new double[(int) numGridPoints.getParameterValue(0)+1]; |
| 108 |
means = new double[(int) numGridPoints.getParameterValue(0)+1]; |
| 109 |
medians = new double[(int) numGridPoints.getParameterValue(0)+1]; |
| 110 |
hpdHigh = new double[(int) numGridPoints.getParameterValue(0)+1]; |
| 111 |
hpdLower = new double[(int) numGridPoints.getParameterValue(0)+1]; |
| 112 |
|
| 113 |
int numbPointsColumn = -1; |
| 114 |
int gvaluesColumn=-1; |
| 115 |
int xvaluesColumn=-1; |
| 116 |
int lambdaColumn = -1; |
| 117 |
int precColumn = -1; |
| 118 |
int tmrcaColumn=-1; |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
for (int n = 0; n < ltraces.getTraceCount(); ++n) { |
| 123 |
final String traceName = ltraces.getTraceName(n); |
| 124 |
System.err.println(traceName); |
| 125 |
if (traceName.equals("skyride.points")) { |
| 126 |
numbPointsColumn = n; |
| 127 |
} else if (traceName.equals("skyride.lambda_bound")) { |
| 128 |
lambdaColumn = n; |
| 129 |
} else if (traceName.equals("skyride.precision")) { |
| 130 |
precColumn = n; |
| 131 |
} else if (traceName.equals("skyride.tmrca")) { |
| 132 |
tmrcaColumn = n; |
| 133 |
} else if (traceName.equals("changePoints")){ |
| 134 |
xvaluesColumn=n; |
| 135 |
} else if (traceName.equals("Gvalues")){ |
| 136 |
gvaluesColumn=n; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
if (numbPointsColumn < 0 || lambdaColumn < 0 || precColumn<0 || tmrcaColumn<0 || xvaluesColumn<0 || gvaluesColumn<0) { |
| 143 |
throw new TraceException("incorrect trace column names: unable to find correct columns for summary"); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
double binSize = 0; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
int [] numPoints = new int[nStates]; |
| 152 |
double[] lambda = new double[nStates]; |
| 153 |
double[] kappa= new double[nStates]; |
| 154 |
double tmrca=0; |
| 155 |
|
| 156 |
double tempTmrca=0.0; |
| 157 |
int maxpts=0; |
| 158 |
for (int ns = 0; ns < nStates; ++ns) { |
| 159 |
lambda[ns]= (Double) ltraces.getTrace(lambdaColumn).getValue(ns); |
| 160 |
numPoints[ns]=(int)Math.round((Double) ltraces.getTrace(numbPointsColumn).getValue(ns)); |
| 161 |
kappa[ns]=(Double) ltraces.getTrace(precColumn).getValue(ns); |
| 162 |
tempTmrca=(Double) ltraces.getTrace(tmrcaColumn).getValue(ns); |
| 163 |
|
| 164 |
System.exit(-1); |
| 165 |
if (tempTmrca>tmrca){tmrca=tempTmrca;} |
| 166 |
if (numPoints[ns]>maxpts) {maxpts=numPoints[ns];} |
| 167 |
} |
| 168 |
|
| 169 |
binSize = tmrca / numGridPoints.getParameterValue(0); |
| 170 |
xPoints[0]=0.0; |
| 171 |
for (int np=1;np<xPoints.length;np++){ |
| 172 |
xPoints[np]=xPoints[np-1]+binSize; |
| 173 |
} |
| 174 |
|
| 175 |
gValues=new double[nStates][]; |
| 176 |
tValues=new double[nStates][]; |
| 177 |
newGvalues=new double[nStates][]; |
| 178 |
popValues=new double[(int) numGridPoints.getParameterValue(0)+1][]; |
| 179 |
readChain(gValues,"gvalues.txt"); |
| 180 |
readChain(tValues,"locations.txt"); |
| 181 |
for (int i=0;i<=numGridPoints.getParameterValue(0);i++){ |
| 182 |
popValues[i]=new double[nStates-1] ; |
| 183 |
} |
| 184 |
|
| 185 |
for (int j=0;j<nStates-1;j++){ |
| 186 |
|
| 187 |
|
| 188 |
newGvalues[j]=GPOperator.getGPvaluesS(tValues[j], gValues[j], xPoints, kappa[j]); |
| 189 |
|
| 190 |
for (int i=0;i<=numGridPoints.getParameterValue(0);i++){ |
| 191 |
popValues[i][j]=(1+Math.exp(-newGvalues[j][i]))/lambda[j]; |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
for (int nx = 0; nx < xPoints.length; ++nx) { |
| 201 |
means[nx] = DiscreteStatistics.mean(popValues[nx]); |
| 202 |
medians[nx]=DiscreteStatistics.median(popValues[nx]); |
| 203 |
hpdLower[nx]=DiscreteStatistics.quantile(0.025,popValues[nx]); |
| 204 |
hpdHigh[nx]=DiscreteStatistics.quantile(0.975,popValues[nx]); |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
public void readChain(double [][] current,String fileName){ |
| 242 |
try { |
| 243 |
BufferedReader br = new BufferedReader(new FileReader(fileName)); |
| 244 |
|
| 245 |
String line=null; |
| 246 |
int i=0; |
| 247 |
|
| 248 |
while ((line=br.readLine())!=null){ |
| 249 |
String[] parts=line.split(" "); |
| 250 |
|
| 251 |
current[i]=new double[parts.length]; |
| 252 |
for (int j=0;j<parts.length;j++){ |
| 253 |
current[i][j]=Double.parseDouble(parts[j]); |
| 254 |
} |
| 255 |
i++; |
| 256 |
} br.close(); |
| 257 |
} |
| 258 |
catch(java.io.IOException ioe){ |
| 259 |
System.err.println("IOException:"+ ioe.getMessage()); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
private final String[] columnNames = {"time", "mean", "median","lower","upper"}; |
| 266 |
|
| 267 |
public int nColumns() { |
| 268 |
return 5; |
| 269 |
} |
| 270 |
|
| 271 |
public String columnName(int nColumn) { |
| 272 |
|
| 273 |
|
| 274 |
return columnNames[nColumn]; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public int nRows() { |
| 287 |
return (int) numGridPoints.getParameterValue(0)+1; |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
public Object data(int nRow, int nColumn) { |
| 292 |
switch (nColumn) { |
| 293 |
case 0: { |
| 294 |
if (nRow < xPoints.length) { |
| 295 |
return xPoints[nRow]; |
| 296 |
} |
| 297 |
break; |
| 298 |
} |
| 299 |
case 1: { |
| 300 |
if (nRow < means.length) { |
| 301 |
return means[nRow]; |
| 302 |
} |
| 303 |
break; |
| 304 |
} |
| 305 |
case 2: { |
| 306 |
if (nRow < medians.length) { |
| 307 |
return medians[nRow]; |
| 308 |
} |
| 309 |
break; |
| 310 |
} |
| 311 |
case 3: { |
| 312 |
if (nRow < hpdLower.length) { |
| 313 |
return hpdLower[nRow]; |
| 314 |
} |
| 315 |
break; |
| 316 |
} |
| 317 |
case 4: { |
| 318 |
if (nRow < hpdHigh.length) { |
| 319 |
return hpdHigh[nRow]; |
| 320 |
} |
| 321 |
break; |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
} |
| 344 |
return ""; |
| 345 |
} |
| 346 |
|
| 347 |
|
| 348 |
} |