| 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.evomodelxml; |
| 27 |
|
| 28 |
import java.io.File; |
| 29 |
import java.io.FileNotFoundException; |
| 30 |
import java.io.FileReader; |
| 31 |
import java.io.Reader; |
| 32 |
|
| 33 |
import jebl.math.Binomial; |
| 34 |
|
| 35 |
import org.apache.commons.math.MathException; |
| 36 |
import org.apache.commons.math.analysis.interpolation.LoessInterpolator; |
| 37 |
|
| 38 |
import dr.evolution.io.Importer.ImportException; |
| 39 |
import dr.evolution.io.TreeTrace; |
| 40 |
import dr.evomodel.coalescent.CoalescentConstantLikelihood; |
| 41 |
import dr.evomodel.coalescent.ExponentialProductLikelihood; |
| 42 |
import dr.evomodel.coalescent.ExponentialProductPosteriorMeansLikelihood; |
| 43 |
import dr.evomodel.coalescent.ExponentialProductSufficientStatisticsLikelihood; |
| 44 |
import dr.evomodel.coalescent.GammaProductLikelihood; |
| 45 |
import dr.evomodel.tree.ConditionalCladeFrequency; |
| 46 |
import dr.evomodel.tree.TreeModel; |
| 47 |
import dr.evomodel.tree.ConditionalCladeProbability; |
| 48 |
import dr.inference.distribution.MultivariateDistributionLikelihood; |
| 49 |
import dr.inference.model.ConstantLikelihood; |
| 50 |
import dr.inference.model.Likelihood; |
| 51 |
import dr.inference.model.Statistic; |
| 52 |
import dr.inference.trace.LogFileTraces; |
| 53 |
import dr.inference.trace.TraceException; |
| 54 |
import dr.math.distributions.MultivariateGammaDistribution; |
| 55 |
import dr.math.functionEval.GammaFunction; |
| 56 |
import dr.util.FileHelpers; |
| 57 |
import dr.xml.AbstractXMLObjectParser; |
| 58 |
import dr.xml.AttributeRule; |
| 59 |
import dr.xml.ElementRule; |
| 60 |
import dr.xml.XMLObject; |
| 61 |
import dr.xml.XMLObjectParser; |
| 62 |
import dr.xml.XMLParseException; |
| 63 |
import dr.xml.XMLSyntaxRule; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public class TreeWorkingPriorParsers { |
| 70 |
|
| 71 |
public final static boolean DEBUG = false; |
| 72 |
|
| 73 |
public static final String CONSTANT_TREE_TOPOLOGY_PRIOR = "constantTreeTopologyPrior"; |
| 74 |
public static final String CONTEMPORANEOUS_COALESCENT_CONSTANT = "contemporaneousCoalescentConstantLikelihood"; |
| 75 |
public static final String COALESCENT_CONSTANT_LIKELIHOOD = "coalescentConstantLikelihood"; |
| 76 |
public static final String CONDITIONAL_CLADE_REFERENCE_PRIOR = "conditionalCladeProbability"; |
| 77 |
public static final String COALESCENT_HEIGHTS_REFERENCE_PRIOR = "coalescentHeightsReferencePrior"; |
| 78 |
public static final String PRODUCT_OF_EXPONENTIALS = "productOfExponentials"; |
| 79 |
public static final String PRODUCT_OF_EXPONENTIALS_POSTERIOR_MEANS = "productOfExponentialsPosteriorMeans"; |
| 80 |
public static final String PRODUCT_OF_EXPONENTIALS_POSTERIOR_MEANS_LOESS = "productOfExponentialsPosteriorMeansLoess"; |
| 81 |
public static final String PRODUCT_OF_EXPONENTIALS_SUFFICIENT_STATISTICS = "productOfExponentialsSufficientStatistics"; |
| 82 |
public static final String PRODUCT_OF_GAMMAS = "productOfGammas"; |
| 83 |
public static final String CONSTANT_DECREASED_VARIANCE_PRIOR = "constantDecreasedVariancePrior"; |
| 84 |
public final static String BURNIN = "burnin"; |
| 85 |
public final static String EPSILON = "epsilon"; |
| 86 |
public static final String PARAMETER_COLUMN = "parameterColumn"; |
| 87 |
|
| 88 |
public static final double epsilon = 0.1; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public static XMLObjectParser COALESCENT_CONSTANT_LIKELIHOOD_PARSER = new AbstractXMLObjectParser () { |
| 94 |
|
| 95 |
public String getParserName() { |
| 96 |
return COALESCENT_CONSTANT_LIKELIHOOD; |
| 97 |
} |
| 98 |
|
| 99 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class); |
| 117 |
|
| 118 |
return new CoalescentConstantLikelihood(tree); |
| 119 |
|
| 120 |
} |
| 121 |
|
| 122 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 123 |
return rules; |
| 124 |
} |
| 125 |
|
| 126 |
private final XMLSyntaxRule[] rules = { |
| 127 |
new ElementRule(TreeModel.class) |
| 128 |
}; |
| 129 |
|
| 130 |
public String getParserDescription() { |
| 131 |
return "Calculates the number of possible combinations of coalescent events."; |
| 132 |
} |
| 133 |
|
| 134 |
public Class getReturnType() { |
| 135 |
return Likelihood.class; |
| 136 |
} |
| 137 |
|
| 138 |
}; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public static XMLObjectParser CONTEMPORANEOUS_COALESCENT_CONSTANT_PARSER = new AbstractXMLObjectParser () { |
| 144 |
|
| 145 |
public String getParserName() { |
| 146 |
return CONTEMPORANEOUS_COALESCENT_CONSTANT; |
| 147 |
} |
| 148 |
|
| 149 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 150 |
|
| 151 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 152 |
final int nTaxa = treeModel.getExternalNodeCount(); |
| 153 |
double logPDF = 0.0; |
| 154 |
for (int i = nTaxa; i > 2; --i) { |
| 155 |
logPDF += Math.log(Binomial.choose2(i)); |
| 156 |
} |
| 157 |
return new ConstantLikelihood(-logPDF); |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 162 |
return rules; |
| 163 |
} |
| 164 |
|
| 165 |
private final XMLSyntaxRule[] rules = { |
| 166 |
new ElementRule(TreeModel.class) |
| 167 |
}; |
| 168 |
|
| 169 |
public String getParserDescription() { |
| 170 |
return "Calculates the number of possible combinations of coalescent events."; |
| 171 |
} |
| 172 |
|
| 173 |
public Class getReturnType() { |
| 174 |
return Likelihood.class; |
| 175 |
} |
| 176 |
|
| 177 |
}; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
public static XMLObjectParser PRODUCT_OF_EXPONENTIALS_PARSER = new AbstractXMLObjectParser() { |
| 183 |
|
| 184 |
public String getParserName() { |
| 185 |
return PRODUCT_OF_EXPONENTIALS; |
| 186 |
} |
| 187 |
|
| 188 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 189 |
|
| 190 |
double logPopSize = xo.getDoubleAttribute("logPopSize"); |
| 191 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 192 |
|
| 193 |
return new ExponentialProductLikelihood(treeModel, logPopSize); |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 198 |
return rules; |
| 199 |
} |
| 200 |
|
| 201 |
private final XMLSyntaxRule[] rules = { |
| 202 |
AttributeRule.newDoubleRule("logPopSize"), |
| 203 |
new ElementRule(TreeModel.class) |
| 204 |
}; |
| 205 |
|
| 206 |
public String getParserDescription() { |
| 207 |
return "Calculates a product of exponentials based on a (set of) fixed log population sizes."; |
| 208 |
} |
| 209 |
|
| 210 |
public Class getReturnType() { |
| 211 |
return Likelihood.class; |
| 212 |
} |
| 213 |
|
| 214 |
}; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
public static XMLObjectParser CONSTANT_DECREASED_VARIANCE_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 220 |
|
| 221 |
public String getParserName() { |
| 222 |
return CONSTANT_DECREASED_VARIANCE_PRIOR; |
| 223 |
} |
| 224 |
|
| 225 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 226 |
|
| 227 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 228 |
String fileName = xo.getStringAttribute(FileHelpers.FILE_NAME); |
| 229 |
|
| 230 |
try { |
| 231 |
|
| 232 |
File file = new File(fileName); |
| 233 |
String parent = file.getParent(); |
| 234 |
|
| 235 |
if (!file.isAbsolute()) { |
| 236 |
parent = System.getProperty("user.dir"); |
| 237 |
} |
| 238 |
file = new File(parent, fileName); |
| 239 |
fileName = file.getAbsolutePath(); |
| 240 |
|
| 241 |
String parameterName = xo.getStringAttribute(PARAMETER_COLUMN); |
| 242 |
int dimension = xo.getIntegerAttribute("dimension"); |
| 243 |
|
| 244 |
LogFileTraces traces = new LogFileTraces(fileName, file); |
| 245 |
traces.loadTraces(); |
| 246 |
long maxState = traces.getMaxState(); |
| 247 |
|
| 248 |
|
| 249 |
long burnin = xo.getAttribute("burnin", maxState / 10); |
| 250 |
if (burnin < 0 || burnin >= maxState) { |
| 251 |
burnin = maxState / 10; |
| 252 |
System.out.println("WARNING: Burn-in larger than total number of states - using 10%"); |
| 253 |
} |
| 254 |
traces.setBurnIn(burnin); |
| 255 |
|
| 256 |
int traceIndexParameter = -1; |
| 257 |
|
| 258 |
if (DEBUG) { |
| 259 |
System.err.println("Looking for the following column:" + parameterName); |
| 260 |
} |
| 261 |
for (int i = 0; i < traces.getTraceCount(); i++) { |
| 262 |
String traceName = traces.getTraceName(i); |
| 263 |
if (traceName.trim().equals(parameterName)) { |
| 264 |
traceIndexParameter = i; |
| 265 |
break; |
| 266 |
} |
| 267 |
} |
| 268 |
if (DEBUG) { |
| 269 |
System.err.println("Overview of traceIndexParameter:"); |
| 270 |
} |
| 271 |
if (traceIndexParameter == -1) { |
| 272 |
throw new XMLParseException("Not all traces could be linked to the required columns."); |
| 273 |
} else if (DEBUG) { |
| 274 |
System.out.println(" traceIndexParameter: " + traceIndexParameter); |
| 275 |
} |
| 276 |
|
| 277 |
Double[] parameterSamples = new Double[traces.getStateCount()]; |
| 278 |
traces.getValues(traceIndexParameter).toArray(parameterSamples); |
| 279 |
|
| 280 |
|
| 281 |
double posteriorMean = 0.0; |
| 282 |
for (int i = 0; i < parameterSamples.length; i++) { |
| 283 |
posteriorMean += parameterSamples[i]; |
| 284 |
} |
| 285 |
posteriorMean /= ((double)parameterSamples.length); |
| 286 |
if (DEBUG) { |
| 287 |
System.err.println("Variable column -> " + posteriorMean); |
| 288 |
} |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
return new GammaProductLikelihood(treeModel, posteriorMean, dimension); |
| 294 |
|
| 295 |
} catch (FileNotFoundException fnfe) { |
| 296 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 297 |
} catch (java.io.IOException ioe) { |
| 298 |
throw new XMLParseException(ioe.getMessage()); |
| 299 |
} catch (TraceException e) { |
| 300 |
throw new XMLParseException(e.getMessage()); |
| 301 |
} |
| 302 |
|
| 303 |
} |
| 304 |
|
| 305 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 306 |
return rules; |
| 307 |
} |
| 308 |
|
| 309 |
private final XMLSyntaxRule[] rules = { |
| 310 |
AttributeRule.newStringRule("fileName"), |
| 311 |
AttributeRule.newStringRule("parameterColumn"), |
| 312 |
AttributeRule.newIntegerRule("dimension"), |
| 313 |
new ElementRule(TreeModel.class) |
| 314 |
}; |
| 315 |
|
| 316 |
public String getParserDescription() { |
| 317 |
return "Calculates a product of exponentials based on a set of posterior sample means."; |
| 318 |
} |
| 319 |
|
| 320 |
public Class getReturnType() { |
| 321 |
return Likelihood.class; |
| 322 |
} |
| 323 |
|
| 324 |
}; |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
public static XMLObjectParser PRODUCT_OF_EXPONENTIALS_POSTERIOR_MEANS_PARSER = new AbstractXMLObjectParser() { |
| 330 |
|
| 331 |
public String getParserName() { |
| 332 |
return PRODUCT_OF_EXPONENTIALS_POSTERIOR_MEANS; |
| 333 |
} |
| 334 |
|
| 335 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 336 |
|
| 337 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 338 |
String fileName = xo.getStringAttribute(FileHelpers.FILE_NAME); |
| 339 |
|
| 340 |
try { |
| 341 |
|
| 342 |
File file = new File(fileName); |
| 343 |
String parent = file.getParent(); |
| 344 |
|
| 345 |
if (!file.isAbsolute()) { |
| 346 |
parent = System.getProperty("user.dir"); |
| 347 |
} |
| 348 |
file = new File(parent, fileName); |
| 349 |
fileName = file.getAbsolutePath(); |
| 350 |
|
| 351 |
String parameterName = xo.getStringAttribute(PARAMETER_COLUMN); |
| 352 |
int dimension = xo.getIntegerAttribute("dimension"); |
| 353 |
|
| 354 |
LogFileTraces traces = new LogFileTraces(fileName, file); |
| 355 |
traces.loadTraces(); |
| 356 |
long maxState = traces.getMaxState(); |
| 357 |
|
| 358 |
|
| 359 |
long burnin = xo.getAttribute("burnin", maxState / 10); |
| 360 |
if (burnin < 0 || burnin >= maxState) { |
| 361 |
burnin = maxState / 10; |
| 362 |
System.out.println("WARNING: Burn-in larger than total number of states - using 10%"); |
| 363 |
} |
| 364 |
traces.setBurnIn(burnin); |
| 365 |
|
| 366 |
int[] traceIndexParameter = new int[dimension]; |
| 367 |
for (int i = 0; i < traceIndexParameter.length; i++) { |
| 368 |
traceIndexParameter[i] = -1; |
| 369 |
} |
| 370 |
|
| 371 |
String[] columnNames = new String[dimension]; |
| 372 |
for (int i = 1; i <= columnNames.length; i++) { |
| 373 |
columnNames[i-1] = parameterName + i; |
| 374 |
} |
| 375 |
if (DEBUG) { |
| 376 |
System.err.println("Looking for the following columns:"); |
| 377 |
for (int i = 0; i < columnNames.length; i++) { |
| 378 |
System.err.println(" " + columnNames[i]); |
| 379 |
} |
| 380 |
} |
| 381 |
for (int i = 0; i < traces.getTraceCount(); i++) { |
| 382 |
String traceName = traces.getTraceName(i); |
| 383 |
for (int j = 0; j < columnNames.length; j++) { |
| 384 |
if (traceName.trim().equals(columnNames[j])) { |
| 385 |
traceIndexParameter[j] = i; |
| 386 |
break; |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
if (DEBUG) { |
| 391 |
System.err.println("Overview of traceIndexParameter:"); |
| 392 |
} |
| 393 |
for (int i = 0; i < traceIndexParameter.length; i++) { |
| 394 |
if (traceIndexParameter[i] == -1) { |
| 395 |
throw new XMLParseException("Not all traces could be linked to the required columns, problem with trace index: " + i + "; traceIndexParameter.length = " + traceIndexParameter.length); |
| 396 |
} else if (DEBUG) { |
| 397 |
System.err.println(" traceIndexParameter[" + i + "]: " + traceIndexParameter[i]); |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
Double[][] parameterSamples = new Double[dimension][traces.getStateCount()]; |
| 402 |
for (int i = 0; i < dimension; i++) { |
| 403 |
traces.getValues(traceIndexParameter[i]).toArray(parameterSamples[i]); |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
double[] posteriorMeans = new double[dimension]; |
| 408 |
|
| 409 |
for (int i = 0; i < posteriorMeans.length; i++) { |
| 410 |
|
| 411 |
double mean = 0.0; |
| 412 |
for (int j = 0; j < parameterSamples[i].length; j++) { |
| 413 |
mean += parameterSamples[i][j]; |
| 414 |
} |
| 415 |
mean /= ((double)parameterSamples[i].length); |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
if (DEBUG) { |
| 420 |
System.err.println("Variable column: " + i + " -> " + mean); |
| 421 |
} |
| 422 |
posteriorMeans[i] = Math.log(mean); |
| 423 |
} |
| 424 |
|
| 425 |
return new ExponentialProductPosteriorMeansLikelihood(treeModel, posteriorMeans); |
| 426 |
|
| 427 |
} catch (FileNotFoundException fnfe) { |
| 428 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 429 |
} catch (java.io.IOException ioe) { |
| 430 |
throw new XMLParseException(ioe.getMessage()); |
| 431 |
} catch (TraceException e) { |
| 432 |
throw new XMLParseException(e.getMessage()); |
| 433 |
} |
| 434 |
|
| 435 |
} |
| 436 |
|
| 437 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 438 |
return rules; |
| 439 |
} |
| 440 |
|
| 441 |
private final XMLSyntaxRule[] rules = { |
| 442 |
AttributeRule.newStringRule("fileName"), |
| 443 |
AttributeRule.newStringRule("parameterColumn"), |
| 444 |
AttributeRule.newIntegerRule("dimension"), |
| 445 |
new ElementRule(TreeModel.class) |
| 446 |
}; |
| 447 |
|
| 448 |
public String getParserDescription() { |
| 449 |
return "Calculates a product of exponentials based on a set of posterior sample means."; |
| 450 |
} |
| 451 |
|
| 452 |
public Class getReturnType() { |
| 453 |
return Likelihood.class; |
| 454 |
} |
| 455 |
|
| 456 |
}; |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
public static XMLObjectParser PRODUCT_OF_EXPONENTIALS_POSTERIOR_MEANS_LOESS_PARSER = new AbstractXMLObjectParser() { |
| 462 |
|
| 463 |
public String getParserName() { |
| 464 |
return PRODUCT_OF_EXPONENTIALS_POSTERIOR_MEANS_LOESS; |
| 465 |
} |
| 466 |
|
| 467 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 468 |
|
| 469 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 470 |
String fileName = xo.getStringAttribute(FileHelpers.FILE_NAME); |
| 471 |
|
| 472 |
try { |
| 473 |
|
| 474 |
File file = new File(fileName); |
| 475 |
String parent = file.getParent(); |
| 476 |
|
| 477 |
if (!file.isAbsolute()) { |
| 478 |
parent = System.getProperty("user.dir"); |
| 479 |
} |
| 480 |
file = new File(parent, fileName); |
| 481 |
fileName = file.getAbsolutePath(); |
| 482 |
|
| 483 |
String parameterName = xo.getStringAttribute(PARAMETER_COLUMN); |
| 484 |
int dimension = xo.getIntegerAttribute("dimension"); |
| 485 |
|
| 486 |
LogFileTraces traces = new LogFileTraces(fileName, file); |
| 487 |
traces.loadTraces(); |
| 488 |
long maxState = traces.getMaxState(); |
| 489 |
|
| 490 |
|
| 491 |
long burnin = xo.getAttribute("burnin", maxState / 10); |
| 492 |
if (burnin < 0 || burnin >= maxState) { |
| 493 |
burnin = maxState / 10; |
| 494 |
System.out.println("WARNING: Burn-in larger than total number of states - using 10%"); |
| 495 |
System.out.println("Burnin: " + burnin); |
| 496 |
} |
| 497 |
traces.setBurnIn(burnin); |
| 498 |
|
| 499 |
int[] traceIndexParameter = new int[dimension]; |
| 500 |
for (int i = 0; i < traceIndexParameter.length; i++) { |
| 501 |
traceIndexParameter[i] = -1; |
| 502 |
} |
| 503 |
|
| 504 |
String[] columnNames = new String[dimension]; |
| 505 |
for (int i = 1; i <= columnNames.length; i++) { |
| 506 |
columnNames[i-1] = parameterName + i; |
| 507 |
} |
| 508 |
if (DEBUG) { |
| 509 |
System.err.println("Looking for the following columns:"); |
| 510 |
for (int i = 0; i < columnNames.length; i++) { |
| 511 |
System.err.println(" " + columnNames[i]); |
| 512 |
} |
| 513 |
} |
| 514 |
for (int i = 0; i < traces.getTraceCount(); i++) { |
| 515 |
String traceName = traces.getTraceName(i); |
| 516 |
for (int j = 0; j < columnNames.length; j++) { |
| 517 |
if (traceName.trim().equals(columnNames[j])) { |
| 518 |
traceIndexParameter[j] = i; |
| 519 |
break; |
| 520 |
} |
| 521 |
} |
| 522 |
} |
| 523 |
if (DEBUG) { |
| 524 |
System.err.println("Overview of traceIndexParameter:"); |
| 525 |
} |
| 526 |
for (int i = 0; i < traceIndexParameter.length; i++) { |
| 527 |
if (traceIndexParameter[i] == -1) { |
| 528 |
throw new XMLParseException("Not all traces could be linked to the required columns, problem with trace index: " + i + "; traceIndexParameter.length = " + traceIndexParameter.length); |
| 529 |
} |
| 530 |
if (DEBUG) { |
| 531 |
System.err.println(" traceIndexParameter[" + i + "]: " + traceIndexParameter[i]); |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
Double[][] parameterSamples = new Double[dimension][traces.getStateCount()]; |
| 536 |
for (int i = 0; i < dimension; i++) { |
| 537 |
traces.getValues(traceIndexParameter[i]).toArray(parameterSamples[i]); |
| 538 |
} |
| 539 |
|
| 540 |
|
| 541 |
double[] posteriorMeans = new double[dimension]; |
| 542 |
|
| 543 |
for (int i = 0; i < posteriorMeans.length; i++) { |
| 544 |
|
| 545 |
double mean = 0.0; |
| 546 |
for (int j = 0; j < parameterSamples[i].length; j++) { |
| 547 |
mean += parameterSamples[i][j]; |
| 548 |
} |
| 549 |
mean /= ((double)parameterSamples[i].length); |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
if (DEBUG) { |
| 554 |
System.err.println("Variable column: " + i + " -> " + mean); |
| 555 |
} |
| 556 |
posteriorMeans[i] = Math.log(mean); |
| 557 |
} |
| 558 |
|
| 559 |
try { |
| 560 |
|
| 561 |
if (DEBUG) { |
| 562 |
System.err.println("Log Posterior Means:"); |
| 563 |
for (int i = 0; i < posteriorMeans.length; i++) { |
| 564 |
System.err.println(posteriorMeans[i]); |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
LoessInterpolator loess = new LoessInterpolator(1.0, 2); |
| 569 |
double[] xvalues = new double[posteriorMeans.length]; |
| 570 |
for (int i = 0; i < posteriorMeans.length; i++) { |
| 571 |
xvalues[i] = i; |
| 572 |
} |
| 573 |
|
| 574 |
double[] loessOutput = loess.smooth(xvalues, posteriorMeans); |
| 575 |
|
| 576 |
if (DEBUG) { |
| 577 |
System.err.println("Loess output:"); |
| 578 |
for (int i = 0; i < loessOutput.length; i++) { |
| 579 |
System.err.println(loessOutput[i]); |
| 580 |
} |
| 581 |
} |
| 582 |
|
| 583 |
posteriorMeans = loessOutput; |
| 584 |
|
| 585 |
} catch (MathException e) { |
| 586 |
|
| 587 |
e.printStackTrace(); |
| 588 |
} |
| 589 |
|
| 590 |
return new ExponentialProductPosteriorMeansLikelihood(treeModel, posteriorMeans); |
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
} catch (FileNotFoundException fnfe) { |
| 598 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 599 |
} catch (java.io.IOException ioe) { |
| 600 |
throw new XMLParseException(ioe.getMessage()); |
| 601 |
} catch (TraceException e) { |
| 602 |
throw new XMLParseException(e.getMessage()); |
| 603 |
} |
| 604 |
|
| 605 |
} |
| 606 |
|
| 607 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 608 |
return rules; |
| 609 |
} |
| 610 |
|
| 611 |
private final XMLSyntaxRule[] rules = { |
| 612 |
AttributeRule.newStringRule("fileName"), |
| 613 |
AttributeRule.newStringRule("parameterColumn"), |
| 614 |
AttributeRule.newIntegerRule("dimension"), |
| 615 |
new ElementRule(TreeModel.class) |
| 616 |
}; |
| 617 |
|
| 618 |
public String getParserDescription() { |
| 619 |
return "Calculates a product of exponentials based on a set of posterior sample means, undergoing loess smoothing."; |
| 620 |
} |
| 621 |
|
| 622 |
public Class getReturnType() { |
| 623 |
return Likelihood.class; |
| 624 |
} |
| 625 |
|
| 626 |
}; |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
public static XMLObjectParser PRODUCT_OF_EXPONENTIALS_SUFFICIENT_STATISTICS_PARSER = new AbstractXMLObjectParser() { |
| 632 |
|
| 633 |
public String getParserName() { |
| 634 |
return PRODUCT_OF_EXPONENTIALS_SUFFICIENT_STATISTICS; |
| 635 |
} |
| 636 |
|
| 637 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 638 |
|
| 639 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 640 |
String fileName = xo.getStringAttribute(FileHelpers.FILE_NAME); |
| 641 |
|
| 642 |
try { |
| 643 |
|
| 644 |
File file = new File(fileName); |
| 645 |
String parent = file.getParent(); |
| 646 |
|
| 647 |
if (!file.isAbsolute()) { |
| 648 |
parent = System.getProperty("user.dir"); |
| 649 |
} |
| 650 |
file = new File(parent, fileName); |
| 651 |
fileName = file.getAbsolutePath(); |
| 652 |
|
| 653 |
String parameterName = xo.getStringAttribute(PARAMETER_COLUMN); |
| 654 |
int dimension = xo.getIntegerAttribute("dimension"); |
| 655 |
|
| 656 |
LogFileTraces traces = new LogFileTraces(fileName, file); |
| 657 |
traces.loadTraces(); |
| 658 |
long maxState = traces.getMaxState(); |
| 659 |
|
| 660 |
|
| 661 |
long burnin = xo.getAttribute("burnin", maxState / 10); |
| 662 |
if (burnin < 0 || burnin >= maxState) { |
| 663 |
burnin = maxState / 10; |
| 664 |
System.out.println("WARNING: Burn-in larger than total number of states - using 10%"); |
| 665 |
} |
| 666 |
traces.setBurnIn(burnin); |
| 667 |
|
| 668 |
int[] traceIndexParameter = new int[dimension]; |
| 669 |
for (int i = 0; i < traceIndexParameter.length; i++) { |
| 670 |
traceIndexParameter[i] = -1; |
| 671 |
} |
| 672 |
|
| 673 |
String[] columnNames = new String[dimension]; |
| 674 |
for (int i = 1; i <= columnNames.length; i++) { |
| 675 |
columnNames[i-1] = parameterName + i; |
| 676 |
} |
| 677 |
if (DEBUG) { |
| 678 |
System.err.println("Looking for the following columns:"); |
| 679 |
for (int i = 0; i < columnNames.length; i++) { |
| 680 |
System.err.println(" " + columnNames[i]); |
| 681 |
} |
| 682 |
} |
| 683 |
for (int i = 0; i < traces.getTraceCount(); i++) { |
| 684 |
String traceName = traces.getTraceName(i); |
| 685 |
for (int j = 0; j < columnNames.length; j++) { |
| 686 |
if (traceName.trim().equals(columnNames[j])) { |
| 687 |
traceIndexParameter[j] = i; |
| 688 |
break; |
| 689 |
} |
| 690 |
} |
| 691 |
} |
| 692 |
if (DEBUG) { |
| 693 |
System.err.println("Overview of traceIndexParameter:"); |
| 694 |
} |
| 695 |
for (int i = 0; i < traceIndexParameter.length; i++) { |
| 696 |
if (traceIndexParameter[i] == -1) { |
| 697 |
throw new XMLParseException("Not all traces could be linked to the required columns."); |
| 698 |
} |
| 699 |
if (DEBUG) { |
| 700 |
System.err.println(" traceIndexParameter[" + i + "]: " + traceIndexParameter[i]); |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
Double[][] parameterSamples = new Double[dimension][traces.getStateCount()]; |
| 705 |
for (int i = 0; i < dimension; i++) { |
| 706 |
traces.getValues(traceIndexParameter[i]).toArray(parameterSamples[i]); |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
double[] posteriorMeans = new double[dimension]; |
| 711 |
|
| 712 |
for (int i = 0; i < posteriorMeans.length; i++) { |
| 713 |
|
| 714 |
double mean = 0.0; |
| 715 |
for (int j = 0; j < parameterSamples[i].length; j++) { |
| 716 |
mean += parameterSamples[i][j]; |
| 717 |
} |
| 718 |
mean /= ((double)parameterSamples[i].length); |
| 719 |
if (DEBUG) { |
| 720 |
System.err.println("Variable column: " + i + " -> " + mean); |
| 721 |
} |
| 722 |
posteriorMeans[i] = Math.log(mean); |
| 723 |
} |
| 724 |
|
| 725 |
return new ExponentialProductSufficientStatisticsLikelihood(treeModel, posteriorMeans); |
| 726 |
|
| 727 |
} catch (FileNotFoundException fnfe) { |
| 728 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 729 |
} catch (java.io.IOException ioe) { |
| 730 |
throw new XMLParseException(ioe.getMessage()); |
| 731 |
} catch (TraceException e) { |
| 732 |
throw new XMLParseException(e.getMessage()); |
| 733 |
} |
| 734 |
|
| 735 |
} |
| 736 |
|
| 737 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 738 |
return rules; |
| 739 |
} |
| 740 |
|
| 741 |
private final XMLSyntaxRule[] rules = { |
| 742 |
AttributeRule.newStringRule("fileName"), |
| 743 |
AttributeRule.newStringRule("parameterColumn"), |
| 744 |
AttributeRule.newIntegerRule("dimension"), |
| 745 |
new ElementRule(TreeModel.class) |
| 746 |
}; |
| 747 |
|
| 748 |
public String getParserDescription() { |
| 749 |
return "Calculates a product of exponentials based on a set of posterior sample means, using sufficient statistics."; |
| 750 |
} |
| 751 |
|
| 752 |
public Class getReturnType() { |
| 753 |
return Likelihood.class; |
| 754 |
} |
| 755 |
|
| 756 |
}; |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
public static XMLObjectParser PRODUCT_OF_GAMMAS_PARSER = new AbstractXMLObjectParser() { |
| 762 |
|
| 763 |
public String getParserName() { |
| 764 |
return PRODUCT_OF_GAMMAS; |
| 765 |
} |
| 766 |
|
| 767 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 768 |
|
| 769 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 770 |
double popSize = xo.getDoubleAttribute("popSize"); |
| 771 |
String fileName = xo.getStringAttribute(FileHelpers.FILE_NAME); |
| 772 |
|
| 773 |
try { |
| 774 |
|
| 775 |
File file = new File(fileName); |
| 776 |
String parent = file.getParent(); |
| 777 |
|
| 778 |
if (!file.isAbsolute()) { |
| 779 |
parent = System.getProperty("user.dir"); |
| 780 |
} |
| 781 |
file = new File(parent, fileName); |
| 782 |
fileName = file.getAbsolutePath(); |
| 783 |
|
| 784 |
String parameterName = xo.getStringAttribute(PARAMETER_COLUMN); |
| 785 |
int dimension = xo.getIntegerAttribute("dimension"); |
| 786 |
|
| 787 |
LogFileTraces traces = new LogFileTraces(fileName, file); |
| 788 |
traces.loadTraces(); |
| 789 |
long maxState = traces.getMaxState(); |
| 790 |
|
| 791 |
|
| 792 |
long burnin = xo.getAttribute("burnin", maxState / 10); |
| 793 |
if (burnin < 0 || burnin >= maxState) { |
| 794 |
burnin = maxState / 10; |
| 795 |
System.out.println("WARNING: Burn-in larger than total number of states - using 10%"); |
| 796 |
} |
| 797 |
traces.setBurnIn(burnin); |
| 798 |
|
| 799 |
int[] traceIndexParameter = new int[dimension]; |
| 800 |
for (int i = 0; i < traceIndexParameter.length; i++) { |
| 801 |
traceIndexParameter[i] = -1; |
| 802 |
} |
| 803 |
|
| 804 |
String[] columnNames = new String[dimension]; |
| 805 |
for (int i = 1; i <= columnNames.length; i++) { |
| 806 |
columnNames[i-1] = parameterName + i; |
| 807 |
} |
| 808 |
if (DEBUG) { |
| 809 |
System.err.println("Looking for the following columns:"); |
| 810 |
for (int i = 0; i < columnNames.length; i++) { |
| 811 |
System.err.println(" " + columnNames[i]); |
| 812 |
} |
| 813 |
} |
| 814 |
for (int i = 0; i < traces.getTraceCount(); i++) { |
| 815 |
String traceName = traces.getTraceName(i); |
| 816 |
for (int j = 0; j < columnNames.length; j++) { |
| 817 |
if (traceName.trim().equals(columnNames[j])) { |
| 818 |
traceIndexParameter[j] = i; |
| 819 |
break; |
| 820 |
} |
| 821 |
} |
| 822 |
} |
| 823 |
if (DEBUG) { |
| 824 |
System.err.println("Overview of traceIndexParameter:"); |
| 825 |
} |
| 826 |
for (int i = 0; i < traceIndexParameter.length; i++) { |
| 827 |
if (traceIndexParameter[i] == -1) { |
| 828 |
throw new XMLParseException("Not all traces could be linked to the required columns."); |
| 829 |
} |
| 830 |
if (DEBUG) { |
| 831 |
System.err.println(" traceIndexParameter[" + i + "]: " + traceIndexParameter[i]); |
| 832 |
} |
| 833 |
} |
| 834 |
|
| 835 |
boolean[] flags = new boolean[dimension]; |
| 836 |
for (int i = 0; i < dimension; i++) { |
| 837 |
flags[i] = true; |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
Double[][] parameterSamples = new Double[dimension][traces.getStateCount()]; |
| 842 |
for (int i = 0; i < dimension; i++) { |
| 843 |
traces.getValues(traceIndexParameter[i]).toArray(parameterSamples[i]); |
| 844 |
double initial = parameterSamples[i][0]; |
| 845 |
boolean tempFlag = false; |
| 846 |
for (int j = 0; j < parameterSamples[i].length; j++) { |
| 847 |
if (parameterSamples[i][j] != initial) { |
| 848 |
tempFlag = true; |
| 849 |
break; |
| 850 |
} |
| 851 |
} |
| 852 |
flags[i] = tempFlag; |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
double[] means = new double[dimension]; |
| 858 |
double[] variances = new double[dimension]; |
| 859 |
|
| 860 |
for (int i = 0; i < flags.length; i++) { |
| 861 |
if (flags[i]) { |
| 862 |
|
| 863 |
double mean = 0.0; |
| 864 |
for (int j = 0; j < parameterSamples[i].length; j++) { |
| 865 |
mean += parameterSamples[i][j]; |
| 866 |
} |
| 867 |
mean /= ((double)parameterSamples[i].length); |
| 868 |
means[i] = mean; |
| 869 |
double variance = 0.0; |
| 870 |
for (int j = 0; j < parameterSamples[i].length; j++) { |
| 871 |
variance += Math.pow(parameterSamples[i][j] - mean, 2); |
| 872 |
} |
| 873 |
variance /= ((double)(parameterSamples[i].length-1)); |
| 874 |
variances[i] = variance; |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
if (DEBUG) { |
| 879 |
System.err.println("Variable column: " + i + " -> " + means[i] + " " + variances[i]); |
| 880 |
} |
| 881 |
} else { |
| 882 |
|
| 883 |
double mean = 0.0; |
| 884 |
for (int j = 0; j < parameterSamples[i].length; j++) { |
| 885 |
mean += parameterSamples[i][j]; |
| 886 |
} |
| 887 |
mean /= ((double)parameterSamples[i].length); |
| 888 |
means[i] = mean; |
| 889 |
|
| 890 |
variances[i] = 0.0; |
| 891 |
variances[i] = epsilon; |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
if (DEBUG) { |
| 896 |
System.err.println("Constant column: " + i + " -> " + means[i] + " " + variances[i]); |
| 897 |
} |
| 898 |
} |
| 899 |
} |
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
return new GammaProductLikelihood(treeModel, popSize, means, variances); |
| 931 |
|
| 932 |
|
| 933 |
} catch (FileNotFoundException fnfe) { |
| 934 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 935 |
} catch (java.io.IOException ioe) { |
| 936 |
throw new XMLParseException(ioe.getMessage()); |
| 937 |
} catch (TraceException e) { |
| 938 |
throw new XMLParseException(e.getMessage()); |
| 939 |
} |
| 940 |
|
| 941 |
} |
| 942 |
|
| 943 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 944 |
return rules; |
| 945 |
} |
| 946 |
|
| 947 |
private final XMLSyntaxRule[] rules = { |
| 948 |
|
| 949 |
AttributeRule.newStringRule("fileName"), |
| 950 |
AttributeRule.newStringRule("parameterColumn"), |
| 951 |
AttributeRule.newIntegerRule("dimension"), |
| 952 |
AttributeRule.newDoubleRule("popSize"), |
| 953 |
|
| 954 |
new ElementRule(TreeModel.class) |
| 955 |
}; |
| 956 |
|
| 957 |
public String getParserDescription() { |
| 958 |
return "Calculates a product of gamma distributions."; |
| 959 |
} |
| 960 |
|
| 961 |
public Class getReturnType() { |
| 962 |
return Likelihood.class; |
| 963 |
} |
| 964 |
|
| 965 |
}; |
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
public static XMLObjectParser COALESCENT_HEIGHTS_REFERENCE_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 971 |
|
| 972 |
public String getParserName() { |
| 973 |
return COALESCENT_HEIGHTS_REFERENCE_PRIOR; |
| 974 |
} |
| 975 |
|
| 976 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 977 |
|
| 978 |
String fileName = xo.getStringAttribute(FileHelpers.FILE_NAME); |
| 979 |
|
| 980 |
try { |
| 981 |
|
| 982 |
File file = new File(fileName); |
| 983 |
String parent = file.getParent(); |
| 984 |
|
| 985 |
if (!file.isAbsolute()) { |
| 986 |
parent = System.getProperty("user.dir"); |
| 987 |
} |
| 988 |
file = new File(parent, fileName); |
| 989 |
fileName = file.getAbsolutePath(); |
| 990 |
|
| 991 |
String parameterName = xo.getStringAttribute(PARAMETER_COLUMN); |
| 992 |
int dimension = xo.getIntegerAttribute("dimension"); |
| 993 |
|
| 994 |
LogFileTraces traces = new LogFileTraces(fileName, file); |
| 995 |
traces.loadTraces(); |
| 996 |
long maxState = traces.getMaxState(); |
| 997 |
|
| 998 |
|
| 999 |
long burnin = xo.getAttribute("burnin", maxState / 10); |
| 1000 |
if (burnin < 0 || burnin >= maxState) { |
| 1001 |
burnin = maxState / 10; |
| 1002 |
System.out.println("WARNING: Burn-in larger than total number of states - using 10%"); |
| 1003 |
} |
| 1004 |
traces.setBurnIn(burnin); |
| 1005 |
|
| 1006 |
int[] traceIndexParameter = new int[dimension]; |
| 1007 |
for (int i = 0; i < traceIndexParameter.length; i++) { |
| 1008 |
traceIndexParameter[i] = -1; |
| 1009 |
} |
| 1010 |
|
| 1011 |
String[] columnNames = new String[dimension]; |
| 1012 |
for (int i = 1; i <= columnNames.length; i++) { |
| 1013 |
columnNames[i-1] = parameterName + i; |
| 1014 |
} |
| 1015 |
if (DEBUG) { |
| 1016 |
System.err.println("Looking for the following columns:"); |
| 1017 |
for (int i = 0; i < columnNames.length; i++) { |
| 1018 |
System.err.println(" " + columnNames[i]); |
| 1019 |
} |
| 1020 |
} |
| 1021 |
for (int i = 0; i < traces.getTraceCount(); i++) { |
| 1022 |
String traceName = traces.getTraceName(i); |
| 1023 |
for (int j = 0; j < columnNames.length; j++) { |
| 1024 |
if (traceName.trim().equals(columnNames[j])) { |
| 1025 |
traceIndexParameter[j] = i; |
| 1026 |
break; |
| 1027 |
} |
| 1028 |
} |
| 1029 |
} |
| 1030 |
if (DEBUG) { |
| 1031 |
System.err.println("Overview of traceIndexParameter:"); |
| 1032 |
} |
| 1033 |
for (int i = 0; i < traceIndexParameter.length; i++) { |
| 1034 |
if (traceIndexParameter[i] == -1) { |
| 1035 |
throw new XMLParseException("Not all traces could be linked to the required columns."); |
| 1036 |
} |
| 1037 |
if (DEBUG) { |
| 1038 |
System.err.println(" traceIndexParameter[" + i + "]: " + traceIndexParameter[i]); |
| 1039 |
} |
| 1040 |
} |
| 1041 |
|
| 1042 |
boolean[] flags = new boolean[dimension]; |
| 1043 |
for (int i = 0; i < dimension; i++) { |
| 1044 |
flags[i] = true; |
| 1045 |
} |
| 1046 |
|
| 1047 |
Double[][] parameterSamples = new Double[dimension][traces.getStateCount()]; |
| 1048 |
for (int i = 0; i < dimension; i++) { |
| 1049 |
traces.getValues(traceIndexParameter[i]).toArray(parameterSamples[i]); |
| 1050 |
double initial = parameterSamples[i][0]; |
| 1051 |
boolean tempFlag = false; |
| 1052 |
for (int j = 0; j < parameterSamples[i].length; j++) { |
| 1053 |
if (parameterSamples[i][j] != initial) { |
| 1054 |
tempFlag = true; |
| 1055 |
break; |
| 1056 |
} |
| 1057 |
} |
| 1058 |
flags[i] = tempFlag; |
| 1059 |
} |
| 1060 |
|
| 1061 |
double[] shapes = new double[dimension]; |
| 1062 |
double[] scales = new double[dimension]; |
| 1063 |
|
| 1064 |
for (int i = 0; i < flags.length; i++) { |
| 1065 |
if (flags[i]) { |
| 1066 |
double mean = 0.0; |
| 1067 |
for (int j = 0; j < parameterSamples[i].length; j++) { |
| 1068 |
mean += parameterSamples[i][j]; |
| 1069 |
} |
| 1070 |
mean /= ((double)parameterSamples[i].length); |
| 1071 |
double variance = 0.0; |
| 1072 |
for (int j = 0; j < parameterSamples[i].length; j++) { |
| 1073 |
variance += Math.pow(parameterSamples[i][j] - mean, 2); |
| 1074 |
} |
| 1075 |
variance /= ((double)(parameterSamples[i].length-1)); |
| 1076 |
scales[i] = variance/mean; |
| 1077 |
shapes[i] = mean/scales[i]; |
| 1078 |
if (DEBUG) { |
| 1079 |
System.err.println("Variable column: " + i + " -> " + shapes[i] + " " + scales[i]); |
| 1080 |
} |
| 1081 |
} |
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
} |
| 1095 |
|
| 1096 |
if (DEBUG) { |
| 1097 |
System.err.println("Columns to be evaluated:"); |
| 1098 |
for (int i = 0; i < flags.length; i++) { |
| 1099 |
if (flags[i]) { |
| 1100 |
System.err.println("Column " + i); |
| 1101 |
} |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
MultivariateGammaDistribution mvgd = new MultivariateGammaDistribution(shapes, scales, flags); |
| 1106 |
|
| 1107 |
MultivariateDistributionLikelihood likelihood = new MultivariateDistributionLikelihood(mvgd); |
| 1108 |
for (int j = 0; j < xo.getChildCount(); j++) { |
| 1109 |
if (xo.getChild(j) instanceof Statistic) { |
| 1110 |
likelihood.addData((Statistic) xo.getChild(j)); |
| 1111 |
} else { |
| 1112 |
throw new XMLParseException("illegal element in " + xo.getName() + " element"); |
| 1113 |
} |
| 1114 |
} |
| 1115 |
|
| 1116 |
return likelihood; |
| 1117 |
|
| 1118 |
} catch (FileNotFoundException fnfe) { |
| 1119 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 1120 |
} catch (java.io.IOException ioe) { |
| 1121 |
throw new XMLParseException(ioe.getMessage()); |
| 1122 |
} catch (TraceException e) { |
| 1123 |
throw new XMLParseException(e.getMessage()); |
| 1124 |
} |
| 1125 |
|
| 1126 |
} |
| 1127 |
|
| 1128 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 1129 |
return rules; |
| 1130 |
} |
| 1131 |
|
| 1132 |
private final XMLSyntaxRule[] rules = { |
| 1133 |
AttributeRule.newStringRule("fileName"), |
| 1134 |
AttributeRule.newStringRule("parameterColumn"), |
| 1135 |
AttributeRule.newIntegerRule("dimension"), |
| 1136 |
|
| 1137 |
new ElementRule(Statistic.class, 1, Integer.MAX_VALUE) |
| 1138 |
}; |
| 1139 |
|
| 1140 |
public String getParserDescription() { |
| 1141 |
return "Calculates the coalescent height probabilities based on a sample of coalescent heights."; |
| 1142 |
} |
| 1143 |
|
| 1144 |
public Class getReturnType() { |
| 1145 |
return MultivariateDistributionLikelihood.class; |
| 1146 |
} |
| 1147 |
|
| 1148 |
}; |
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
|
| 1154 |
public static XMLObjectParser CONSTANT_TREE_TOPOLOGY_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 1155 |
|
| 1156 |
public String getParserName() { |
| 1157 |
return CONSTANT_TREE_TOPOLOGY_PRIOR; |
| 1158 |
} |
| 1159 |
|
| 1160 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 1161 |
|
| 1162 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 1163 |
final int nTaxa = treeModel.getExternalNodeCount(); |
| 1164 |
double combinations = GammaFunction.factorial(2*nTaxa-3)/(GammaFunction.factorial(nTaxa-2)*Math.pow(2.0,nTaxa-2)); |
| 1165 |
double logPDF = Math.log(1.0/combinations); |
| 1166 |
return new ConstantLikelihood(logPDF); |
| 1167 |
|
| 1168 |
} |
| 1169 |
|
| 1170 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 1171 |
return rules; |
| 1172 |
} |
| 1173 |
|
| 1174 |
private final XMLSyntaxRule[] rules = { |
| 1175 |
new ElementRule(TreeModel.class) |
| 1176 |
}; |
| 1177 |
|
| 1178 |
public String getParserDescription() { |
| 1179 |
return "Calculates the constant tree topology prior, i.e. 1 over the total number of rooted bifurcating trees."; |
| 1180 |
} |
| 1181 |
|
| 1182 |
public Class getReturnType() { |
| 1183 |
return Likelihood.class; |
| 1184 |
} |
| 1185 |
|
| 1186 |
}; |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
|
| 1191 |
public static XMLObjectParser CONDITIONAL_CLADE_REFERENCE_PRIOR_PARSER = new AbstractXMLObjectParser() { |
| 1192 |
|
| 1193 |
public String getParserName() { |
| 1194 |
return CONDITIONAL_CLADE_REFERENCE_PRIOR; |
| 1195 |
} |
| 1196 |
|
| 1197 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 1202 |
|
| 1203 |
String fileName = xo.getStringAttribute(FileHelpers.FILE_NAME); |
| 1204 |
|
| 1205 |
try { |
| 1206 |
|
| 1207 |
File file = new File(fileName); |
| 1208 |
String name = file.getName(); |
| 1209 |
String parent = file.getParent(); |
| 1210 |
|
| 1211 |
if (!file.isAbsolute()) { |
| 1212 |
parent = System.getProperty("user.dir"); |
| 1213 |
} |
| 1214 |
file = new File(parent, fileName); |
| 1215 |
fileName = file.getAbsolutePath(); |
| 1216 |
|
| 1217 |
Reader reader = new FileReader(new File(parent, name)); |
| 1218 |
|
| 1219 |
|
| 1220 |
int burnin = -1; |
| 1221 |
if (xo.hasAttribute(BURNIN)) { |
| 1222 |
|
| 1223 |
burnin = xo.getIntegerAttribute(BURNIN); |
| 1224 |
} |
| 1225 |
|
| 1226 |
|
| 1227 |
double e = 1.0; |
| 1228 |
if (xo.hasAttribute(EPSILON)) { |
| 1229 |
|
| 1230 |
e = xo.getDoubleAttribute(EPSILON); |
| 1231 |
} |
| 1232 |
|
| 1233 |
TreeTrace trace = TreeTrace.loadTreeTrace(reader); |
| 1234 |
|
| 1235 |
ConditionalCladeFrequency ccf = new ConditionalCladeFrequency(new TreeTrace[]{trace}, e, burnin, false); |
| 1236 |
|
| 1237 |
ConditionalCladeProbability ccp = new ConditionalCladeProbability(ccf, treeModel); |
| 1238 |
|
| 1239 |
return ccp; |
| 1240 |
|
| 1241 |
} catch (FileNotFoundException fnfe) { |
| 1242 |
throw new XMLParseException("File '" + fileName + "' can not be opened for " + getParserName() + " element."); |
| 1243 |
} catch (java.io.IOException ioe) { |
| 1244 |
throw new XMLParseException(ioe.getMessage()); |
| 1245 |
} catch (ImportException ie) { |
| 1246 |
throw new XMLParseException(ie.getMessage()); |
| 1247 |
} |
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
} |
| 1257 |
|
| 1258 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 1259 |
return rules; |
| 1260 |
} |
| 1261 |
|
| 1262 |
private final XMLSyntaxRule[] rules = { |
| 1263 |
AttributeRule.newStringRule("fileName"), |
| 1264 |
AttributeRule.newIntegerRule(BURNIN), |
| 1265 |
AttributeRule.newDoubleRule(EPSILON), |
| 1266 |
new ElementRule(TreeModel.class) |
| 1267 |
}; |
| 1268 |
|
| 1269 |
public String getParserDescription() { |
| 1270 |
return "Calculates the conditional clade probability of a tree based on a sample of tree space."; |
| 1271 |
} |
| 1272 |
|
| 1273 |
public Class getReturnType() { |
| 1274 |
return Likelihood.class; |
| 1275 |
} |
| 1276 |
}; |
| 1277 |
|
| 1278 |
} |