use of dr.inference.loggers.TabDelimitedFormatter in project beast-mcmc by beast-dev.
the class TreeLoggerParser method parseXMLParameters.
protected void parseXMLParameters(XMLObject xo) throws XMLParseException {
// reset this every time...
branchRates = null;
tree = (Tree) xo.getChild(Tree.class);
title = xo.getAttribute(TITLE, "");
nexusFormat = xo.getAttribute(NEXUS_FORMAT, false);
sortTranslationTable = xo.getAttribute(SORT_TRANSLATION_TABLE, true);
boolean substitutions = xo.getAttribute(BRANCH_LENGTHS, "").equals(SUBSTITUTIONS);
List<TreeAttributeProvider> taps = new ArrayList<TreeAttributeProvider>();
List<TreeTraitProvider> ttps = new ArrayList<TreeTraitProvider>();
// ttps2 are for TTPs that are not specified within a Trait element. These are only
// included if not already added through a trait element to avoid duplication of
// (in particular) the BranchRates which is required for substitution trees.
List<TreeTraitProvider> ttps2 = new ArrayList<TreeTraitProvider>();
for (int i = 0; i < xo.getChildCount(); i++) {
Object cxo = xo.getChild(i);
if (cxo instanceof Likelihood) {
final Likelihood likelihood = (Likelihood) cxo;
taps.add(new TreeAttributeProvider() {
public String[] getTreeAttributeLabel() {
return new String[] { "lnP" };
}
public String[] getAttributeForTree(Tree tree) {
return new String[] { Double.toString(likelihood.getLogLikelihood()) };
}
});
}
if (cxo instanceof TreeAttributeProvider) {
taps.add((TreeAttributeProvider) cxo);
}
if (cxo instanceof TreeTraitProvider) {
if (xo.hasAttribute(FILTER_TRAITS)) {
String[] matches = ((String) xo.getAttribute(FILTER_TRAITS)).split("[\\s,]+");
TreeTraitProvider ttp = (TreeTraitProvider) cxo;
TreeTrait[] traits = ttp.getTreeTraits();
List<TreeTrait> filteredTraits = new ArrayList<TreeTrait>();
for (String match : matches) {
for (TreeTrait trait : traits) {
if (trait.getTraitName().startsWith(match)) {
filteredTraits.add(trait);
}
}
}
if (filteredTraits.size() > 0) {
ttps2.add(new TreeTraitProvider.Helper(filteredTraits));
}
} else {
// Add all of them
ttps2.add((TreeTraitProvider) cxo);
}
}
if (cxo instanceof XMLObject) {
XMLObject xco = (XMLObject) cxo;
if (xco.getName().equals(TREE_TRAIT)) {
TreeTraitProvider ttp = (TreeTraitProvider) xco.getChild(TreeTraitProvider.class);
if (xco.hasAttribute(NAME)) {
// a specific named trait is required (optionally with a tag to name it in the tree file)
String name = xco.getStringAttribute(NAME);
final TreeTrait trait = ttp.getTreeTrait(name);
if (trait == null) {
String childName = "TreeTraitProvider";
if (ttp instanceof Likelihood) {
childName = ((Likelihood) ttp).prettyName();
} else if (ttp instanceof Model) {
childName = ((Model) ttp).getModelName();
}
throw new XMLParseException("Trait named, " + name + ", not found for " + childName);
}
final String tag;
if (xco.hasAttribute(TAG)) {
tag = xco.getStringAttribute(TAG);
} else {
tag = name;
}
ttps.add(new TreeTraitProvider.Helper(tag, new TreeTrait() {
public String getTraitName() {
return tag;
}
public Intent getIntent() {
return trait.getIntent();
}
public Class getTraitClass() {
return trait.getTraitClass();
}
public Object getTrait(Tree tree, NodeRef node) {
return trait.getTrait(tree, node);
}
public String getTraitString(Tree tree, NodeRef node) {
return trait.getTraitString(tree, node);
}
public boolean getLoggable() {
return trait.getLoggable();
}
}));
} else if (xo.hasAttribute(FILTER_TRAITS)) {
// else a filter attribute is given to ask for all traits that starts with a specific
// string
String[] matches = ((String) xo.getAttribute(FILTER_TRAITS)).split("[\\s,]+");
TreeTrait[] traits = ttp.getTreeTraits();
List<TreeTrait> filteredTraits = new ArrayList<TreeTrait>();
for (String match : matches) {
for (TreeTrait trait : traits) {
if (trait.getTraitName().startsWith(match)) {
filteredTraits.add(trait);
}
}
}
if (filteredTraits.size() > 0) {
ttps.add(new TreeTraitProvider.Helper(filteredTraits));
}
} else {
// neither named or filtered traits so just add them all
ttps.add(ttp);
}
}
}
// be able to put arbitrary statistics in as tree attributes
if (cxo instanceof Loggable) {
final Loggable loggable = (Loggable) cxo;
taps.add(new TreeAttributeProvider() {
public String[] getTreeAttributeLabel() {
String[] labels = new String[loggable.getColumns().length];
for (int i = 0; i < loggable.getColumns().length; i++) {
labels[i] = loggable.getColumns()[i].getLabel();
}
return labels;
}
public String[] getAttributeForTree(Tree tree) {
String[] values = new String[loggable.getColumns().length];
for (int i = 0; i < loggable.getColumns().length; i++) {
values[i] = loggable.getColumns()[i].getFormatted();
}
return values;
}
});
}
}
// inclusion of the codon partitioned robust counting TTP...
if (ttps2.size() > 0) {
ttps.addAll(ttps2);
}
if (substitutions) {
branchRates = (BranchRates) xo.getChild(BranchRates.class);
}
if (substitutions && branchRates == null) {
throw new XMLParseException("To log trees in units of substitutions a BranchRateModel must be provided");
}
// logEvery of zero only displays at the end
logEvery = xo.getAttribute(LOG_EVERY, 0);
// double normaliseMeanRateTo = xo.getAttribute(NORMALISE_MEAN_RATE_TO, Double.NaN);
// decimal places
final int dp = xo.getAttribute(DECIMAL_PLACES, -1);
if (dp != -1) {
format = NumberFormat.getNumberInstance(Locale.ENGLISH);
format.setMaximumFractionDigits(dp);
}
final PrintWriter pw = getLogFile(xo, getParserName());
formatter = new TabDelimitedFormatter(pw);
treeAttributeProviders = new TreeAttributeProvider[taps.size()];
taps.toArray(treeAttributeProviders);
treeTraitProviders = new TreeTraitProvider[ttps.size()];
ttps.toArray(treeTraitProviders);
// I think the default should be to have names rather than numbers, thus the false default - AJD
// I think the default should be numbers - using names results in larger files and end user never
// sees the numbers anyway as any software loading the nexus files does the translation - JH
mapNames = xo.getAttribute(MAP_NAMES, true);
condition = logEvery == 0 ? (TreeLogger.LogUpon) xo.getChild(TreeLogger.LogUpon.class) : null;
}
use of dr.inference.loggers.TabDelimitedFormatter in project beast-mcmc by beast-dev.
the class RLYModelTest method randomLocalYuleTester.
private void randomLocalYuleTester(TreeModel treeModel, Parameter I, Parameter b, OperatorSchedule schedule) {
MCMC mcmc = new MCMC("mcmc1");
MCMCOptions options = new MCMCOptions(1000000);
TreeLengthStatistic tls = new TreeLengthStatistic(TL, treeModel);
TreeHeightStatistic rootHeight = new TreeHeightStatistic(TREE_HEIGHT, treeModel);
Parameter m = new Parameter.Default("m", 1.0, 0.0, Double.MAX_VALUE);
SpeciationModel speciationModel = new RandomLocalYuleModel(b, I, m, false, Units.Type.YEARS, 4);
Likelihood likelihood = new SpeciationLikelihood(treeModel, speciationModel, "randomYule.like");
ArrayLogFormatter formatter = new ArrayLogFormatter(false);
MCLogger[] loggers = new MCLogger[2];
loggers[0] = new MCLogger(formatter, 100, false);
loggers[0].add(likelihood);
loggers[0].add(rootHeight);
loggers[0].add(tls);
loggers[0].add(I);
loggers[1] = new MCLogger(new TabDelimitedFormatter(System.out), 100000, false);
loggers[1].add(likelihood);
loggers[1].add(rootHeight);
loggers[1].add(tls);
loggers[1].add(I);
mcmc.setShowOperatorAnalysis(true);
mcmc.init(options, likelihood, schedule, loggers);
mcmc.run();
List<Trace> traces = formatter.getTraces();
ArrayTraceList traceList = new ArrayTraceList("yuleModelTest", traces, 0);
for (int i = 1; i < traces.size(); i++) {
traceList.analyseTrace(i);
}
TraceCorrelation tlStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("root." + birthRateIndicator));
System.out.println("mean = " + tlStats.getMean());
System.out.println("expected mean = 0.5");
assertExpectation("root." + birthRateIndicator, tlStats, 0.5);
}
use of dr.inference.loggers.TabDelimitedFormatter in project beast-mcmc by beast-dev.
the class YuleModelTest method yuleTester.
// public void testYuleWithWideExchange() {
//
// TreeModel treeModel = new TreeModel("treeModel", tree);
// Doesn't compile...
// yuleTester(treeModel, ExchangeOperatorTest.getWideExchangeSchedule(treeModel));
// }
private void yuleTester(TreeModel treeModel, OperatorSchedule schedule) {
MCMC mcmc = new MCMC("mcmc1");
MCMCOptions options = new MCMCOptions(1000000);
TreeLengthStatistic tls = new TreeLengthStatistic(TL, treeModel);
TreeHeightStatistic rootHeight = new TreeHeightStatistic(TREE_HEIGHT, treeModel);
Parameter b = new Parameter.Default("b", 2.0, 0.0, Double.MAX_VALUE);
Parameter d = new Parameter.Default("d", 0.0, 0.0, Double.MAX_VALUE);
SpeciationModel speciationModel = new BirthDeathGernhard08Model(b, d, null, BirthDeathGernhard08Model.TreeType.TIMESONLY, Units.Type.YEARS);
Likelihood likelihood = new SpeciationLikelihood(treeModel, speciationModel, "yule.like");
ArrayLogFormatter formatter = new ArrayLogFormatter(false);
MCLogger[] loggers = new MCLogger[2];
loggers[0] = new MCLogger(formatter, 100, false);
loggers[0].add(likelihood);
loggers[0].add(rootHeight);
loggers[0].add(tls);
loggers[1] = new MCLogger(new TabDelimitedFormatter(System.out), 100000, false);
loggers[1].add(likelihood);
loggers[1].add(rootHeight);
loggers[1].add(tls);
mcmc.setShowOperatorAnalysis(true);
mcmc.init(options, likelihood, schedule, loggers);
mcmc.run();
List<Trace> traces = formatter.getTraces();
ArrayTraceList traceList = new ArrayTraceList("yuleModelTest", traces, 0);
for (int i = 1; i < traces.size(); i++) {
traceList.analyseTrace(i);
}
// expectation of root height for 4 tips and lambda = 2
// rootHeight = 0.541666
// TL = 1.5
TraceCorrelation tlStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TL));
assertExpectation(TL, tlStats, 1.5);
TraceCorrelation treeHeightStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TREE_HEIGHT));
assertExpectation(TREE_HEIGHT, treeHeightStats, 0.5416666);
}
use of dr.inference.loggers.TabDelimitedFormatter in project beast-mcmc by beast-dev.
the class StrictClockTest method testStrictClock.
public void testStrictClock() throws Exception {
Parameter popSize = new Parameter.Default(ConstantPopulationModelParser.POPULATION_SIZE, 380.0, 0, 38000.0);
ConstantPopulationModel constantModel = createRandomInitialTree(popSize);
TreeIntervals intervalList = new TreeIntervals(treeModel, null, null);
CoalescentLikelihood coalescent = new CoalescentLikelihood(intervalList, constantModel);
coalescent.setId("coalescent");
// clock model
Parameter rateParameter = new Parameter.Default(StrictClockBranchRates.RATE, 2.3E-5, 0, 100.0);
StrictClockBranchRates branchRateModel = new StrictClockBranchRates(rateParameter);
// Sub model
Parameter freqs = new Parameter.Default(alignment.getStateFrequencies());
Parameter kappa = new Parameter.Default(HKYParser.KAPPA, 1.0, 0, 100.0);
FrequencyModel f = new FrequencyModel(Nucleotides.INSTANCE, freqs);
HKY hky = new HKY(kappa, f);
// siteModel
GammaSiteModel siteModel = new GammaSiteModel(hky);
Parameter mu = new Parameter.Default(GammaSiteModelParser.MUTATION_RATE, 1.0, 0, Double.POSITIVE_INFINITY);
siteModel.setMutationRateParameter(mu);
// treeLikelihood
SitePatterns patterns = new SitePatterns(alignment, null, 0, -1, 1, true);
TreeLikelihood treeLikelihood = new TreeLikelihood(patterns, treeModel, siteModel, branchRateModel, null, false, false, true, false, false);
treeLikelihood.setId(TreeLikelihoodParser.TREE_LIKELIHOOD);
// Operators
OperatorSchedule schedule = new SimpleOperatorSchedule();
MCMCOperator operator = new ScaleOperator(kappa, 0.75);
operator.setWeight(1.0);
schedule.addOperator(operator);
operator = new ScaleOperator(rateParameter, 0.75);
operator.setWeight(3.0);
schedule.addOperator(operator);
Parameter allInternalHeights = ((DefaultTreeModel) treeModel).createNodeHeightsParameter(true, true, false);
operator = new UpDownOperator(new Scalable[] { new Scalable.Default(rateParameter) }, new Scalable[] { new Scalable.Default(allInternalHeights) }, 0.75, 3.0, AdaptationMode.ADAPTATION_ON);
schedule.addOperator(operator);
operator = new ScaleOperator(popSize, 0.75);
operator.setWeight(3.0);
schedule.addOperator(operator);
Parameter rootHeight = ((DefaultTreeModel) treeModel).getRootHeightParameter();
rootHeight.setId(TREE_HEIGHT);
operator = new ScaleOperator(rootHeight, 0.75);
operator.setWeight(3.0);
schedule.addOperator(operator);
Parameter internalHeights = ((DefaultTreeModel) treeModel).createNodeHeightsParameter(false, true, false);
operator = new UniformOperator(internalHeights, 30.0);
schedule.addOperator(operator);
operator = new SubtreeSlideOperator(((DefaultTreeModel) treeModel), 15.0, 1.0, true, false, false, false, AdaptationMode.ADAPTATION_ON, AdaptableMCMCOperator.DEFAULT_ADAPTATION_TARGET);
schedule.addOperator(operator);
operator = new ExchangeOperator(ExchangeOperator.NARROW, treeModel, 15.0);
// operator.doOperation();
schedule.addOperator(operator);
operator = new ExchangeOperator(ExchangeOperator.WIDE, treeModel, 3.0);
// operator.doOperation();
schedule.addOperator(operator);
operator = new WilsonBalding(treeModel, 3.0);
// operator.doOperation();
schedule.addOperator(operator);
// CompoundLikelihood
List<Likelihood> likelihoods = new ArrayList<Likelihood>();
likelihoods.add(coalescent);
Likelihood prior = new CompoundLikelihood(0, likelihoods);
prior.setId(CompoundLikelihoodParser.PRIOR);
likelihoods.clear();
likelihoods.add(treeLikelihood);
Likelihood likelihood = new CompoundLikelihood(-1, likelihoods);
likelihoods.clear();
likelihoods.add(prior);
likelihoods.add(likelihood);
Likelihood posterior = new CompoundLikelihood(0, likelihoods);
posterior.setId(CompoundLikelihoodParser.POSTERIOR);
// Log
ArrayLogFormatter formatter = new ArrayLogFormatter(false);
MCLogger[] loggers = new MCLogger[2];
loggers[0] = new MCLogger(formatter, 500, false);
loggers[0].add(posterior);
loggers[0].add(treeLikelihood);
loggers[0].add(rootHeight);
loggers[0].add(rateParameter);
loggers[0].add(popSize);
loggers[0].add(kappa);
loggers[0].add(coalescent);
loggers[1] = new MCLogger(new TabDelimitedFormatter(System.out), 10000, false);
loggers[1].add(posterior);
loggers[1].add(treeLikelihood);
loggers[1].add(rootHeight);
loggers[1].add(rateParameter);
loggers[1].add(coalescent);
// MCMC
MCMC mcmc = new MCMC("mcmc1");
MCMCOptions options = new MCMCOptions(1000000);
mcmc.setShowOperatorAnalysis(true);
mcmc.init(options, posterior, schedule, loggers);
mcmc.run();
// time
System.out.println(mcmc.getTimer().toString());
// Tracer
List<Trace> traces = formatter.getTraces();
ArrayTraceList traceList = new ArrayTraceList("RandomLocalClockTest", traces, 0);
for (int i = 1; i < traces.size(); i++) {
traceList.analyseTrace(i);
}
// <expectation name="posterior" value="-3928.71"/>
// <expectation name="clock.rate" value="8.04835E-4"/>
// <expectation name="constant.popSize" value="37.3762"/>
// <expectation name="hky.kappa" value="18.2782"/>
// <expectation name="treeModel.rootHeight" value="69.0580"/>
// <expectation name="treeLikelihood" value="-3856.59"/>
// <expectation name="coalescent" value="-72.1285"/>
TraceCorrelation likelihoodStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(CompoundLikelihoodParser.POSTERIOR));
assertExpectation(CompoundLikelihoodParser.POSTERIOR, likelihoodStats, -3928.71);
likelihoodStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TreeLikelihoodParser.TREE_LIKELIHOOD));
assertExpectation(TreeLikelihoodParser.TREE_LIKELIHOOD, likelihoodStats, -3856.59);
TraceCorrelation treeHeightStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TREE_HEIGHT));
assertExpectation(TREE_HEIGHT, treeHeightStats, 69.0580);
TraceCorrelation kappaStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(HKYParser.KAPPA));
assertExpectation(HKYParser.KAPPA, kappaStats, 18.2782);
TraceCorrelation rateStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(StrictClockBranchRates.RATE));
assertExpectation(StrictClockBranchRates.RATE, rateStats, 8.04835E-4);
TraceCorrelation popStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(ConstantPopulationModelParser.POPULATION_SIZE));
assertExpectation(ConstantPopulationModelParser.POPULATION_SIZE, popStats, 37.3762);
TraceCorrelation coalescentStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("coalescent"));
assertExpectation("coalescent", coalescentStats, -72.1285);
}
use of dr.inference.loggers.TabDelimitedFormatter in project beast-mcmc by beast-dev.
the class GeneralSubstitutionModelTest method testGeneralSubstitutionModel.
public void testGeneralSubstitutionModel() {
// Sub model
FrequencyModel freqModel = new FrequencyModel(dataType, alignment.getStateFrequencies());
// dimension="5" value="1.0"
Parameter ratesPara = new Parameter.Default(GeneralSubstitutionModelParser.RATES, 5, 1.0);
// relativeTo="5"
GeneralSubstitutionModel generalSubstitutionModel = new GeneralSubstitutionModel(dataType, freqModel, ratesPara, 4);
// siteModel
GammaSiteModel siteModel = new GammaSiteModel(generalSubstitutionModel);
Parameter mu = new Parameter.Default(GammaSiteModelParser.MUTATION_RATE, 1.0, 0, Double.POSITIVE_INFINITY);
siteModel.setMutationRateParameter(mu);
// treeLikelihood
SitePatterns patterns = new SitePatterns(alignment, null, 0, -1, 1, true);
TreeLikelihood treeLikelihood = new TreeLikelihood(patterns, treeModel, siteModel, null, null, false, false, true, false, false);
treeLikelihood.setId(TreeLikelihoodParser.TREE_LIKELIHOOD);
// Operators
OperatorSchedule schedule = new SimpleOperatorSchedule();
MCMCOperator operator = new ScaleOperator(ratesPara, 0.5);
operator.setWeight(1.0);
schedule.addOperator(operator);
Parameter rootHeight = ((DefaultTreeModel) treeModel).getRootHeightParameter();
rootHeight.setId(TREE_HEIGHT);
operator = new ScaleOperator(rootHeight, 0.5);
operator.setWeight(1.0);
schedule.addOperator(operator);
Parameter internalHeights = ((DefaultTreeModel) treeModel).createNodeHeightsParameter(false, true, false);
operator = new UniformOperator(internalHeights, 10.0);
schedule.addOperator(operator);
operator = new SubtreeSlideOperator(((DefaultTreeModel) treeModel), 1, 1, true, false, false, false, AdaptationMode.ADAPTATION_ON, AdaptableMCMCOperator.DEFAULT_ADAPTATION_TARGET);
schedule.addOperator(operator);
operator = new ExchangeOperator(ExchangeOperator.NARROW, treeModel, 1.0);
// operator.doOperation();
schedule.addOperator(operator);
operator = new ExchangeOperator(ExchangeOperator.WIDE, treeModel, 1.0);
// operator.doOperation();
schedule.addOperator(operator);
operator = new WilsonBalding(treeModel, 1.0);
// operator.doOperation();
schedule.addOperator(operator);
// Log
ArrayLogFormatter formatter = new ArrayLogFormatter(false);
MCLogger[] loggers = new MCLogger[2];
loggers[0] = new MCLogger(formatter, 1000, false);
loggers[0].add(treeLikelihood);
loggers[0].add(rootHeight);
loggers[0].add(ratesPara);
loggers[1] = new MCLogger(new TabDelimitedFormatter(System.out), 100000, false);
loggers[1].add(treeLikelihood);
loggers[1].add(rootHeight);
loggers[1].add(ratesPara);
// MCMC
MCMC mcmc = new MCMC("mcmc1");
MCMCOptions options = new MCMCOptions(10000000);
mcmc.setShowOperatorAnalysis(true);
mcmc.init(options, treeLikelihood, schedule, loggers);
mcmc.run();
// time
System.out.println(mcmc.getTimer().toString());
// Tracer
List<Trace> traces = formatter.getTraces();
ArrayTraceList traceList = new ArrayTraceList("GeneralSubstitutionModelTest", traces, 0);
for (int i = 1; i < traces.size(); i++) {
traceList.analyseTrace(i);
}
// <expectation name="likelihood" value="-1815.75"/>
// <expectation name="treeModel.rootHeight" value="6.42048E-2"/>
// <expectation name="rateAC" value="6.08986E-2"/>
TraceCorrelation likelihoodStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TreeLikelihoodParser.TREE_LIKELIHOOD));
assertExpectation(TreeLikelihoodParser.TREE_LIKELIHOOD, likelihoodStats, -1815.75);
TraceCorrelation treeHeightStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TREE_HEIGHT));
assertExpectation(TREE_HEIGHT, treeHeightStats, 0.0640787258170083);
TraceCorrelation rateACStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(GeneralSubstitutionModelParser.RATES + "1"));
assertExpectation(GeneralSubstitutionModelParser.RATES + "1", rateACStats, 0.061071756742081366);
}
Aggregations