use of dr.util.Attribute in project beast-mcmc by beast-dev.
the class TreePriorGenerator method writePriorLikelihood.
/**
* Write the prior on node heights (coalescent or speciational models)
*
* @param model PartitionTreeModel
* @param writer the writer
*/
void writePriorLikelihood(PartitionTreeModel model, XMLWriter writer) {
//tree model prefix
// only has prefix, if (options.getPartitionTreePriors().size() > 1)
setModelPrefix(model.getPrefix());
// String priorPrefix = prior.getPrefix();
PartitionTreePrior prior = model.getPartitionTreePrior();
TreePriorType treePrior = prior.getNodeHeightPrior();
switch(treePrior) {
case YULE:
case BIRTH_DEATH:
case BIRTH_DEATH_INCOMPLETE_SAMPLING:
case BIRTH_DEATH_SERIAL_SAMPLING:
case BIRTH_DEATH_BASIC_REPRODUCTIVE_NUMBER:
case YULE_CALIBRATION:
// generate a speciational process
writer.writeComment("Generate a speciation likelihood for Yule or Birth Death");
writer.writeOpenTag(SpeciationLikelihoodParser.SPECIATION_LIKELIHOOD, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, modelPrefix + "speciation") });
// write pop size socket
writer.writeOpenTag(SpeciationLikelihoodParser.MODEL);
writeNodeHeightPriorModelRef(prior, writer);
writer.writeCloseTag(SpeciationLikelihoodParser.MODEL);
writer.writeOpenTag(SpeciationLikelihoodParser.TREE);
writer.writeIDref(TreeModel.TREE_MODEL, modelPrefix + TreeModel.TREE_MODEL);
writer.writeCloseTag(SpeciationLikelihoodParser.TREE);
if (treePrior == TreePriorType.YULE_CALIBRATION) {
if (options.treeModelOptions.isNodeCalibrated(model) == 0) {
writer.writeOpenTag(SpeciationLikelihoodParser.CALIBRATION, new Attribute[] { new Attribute.Default<String>(SpeciationLikelihoodParser.CORRECTION, prior.getCalibCorrectionType().toString()) });
writer.writeOpenTag(SpeciationLikelihoodParser.POINT);
String taxaId;
if (options.hasIdenticalTaxa()) {
taxaId = TaxaParser.TAXA;
} else {
taxaId = options.getDataPartitions(model).get(0).getPrefix() + TaxaParser.TAXA;
}
writer.writeIDref(TaxaParser.TAXA, taxaId);
writeDistribution(model.getParameter("treeModel.rootHeight"), true, writer);
writer.writeCloseTag(SpeciationLikelihoodParser.POINT);
writer.writeCloseTag(SpeciationLikelihoodParser.CALIBRATION);
} else if (options.treeModelOptions.isNodeCalibrated(model) == 1) {
// should be only 1 calibrated internal node with monophyletic for each tree at moment
Taxa t = (Taxa) options.getKeysFromValue(options.taxonSetsTreeModel, model).get(0);
Parameter nodeCalib = options.getStatistic(t);
writer.writeOpenTag(SpeciationLikelihoodParser.CALIBRATION, new Attribute[] { new Attribute.Default<String>(SpeciationLikelihoodParser.CORRECTION, prior.getCalibCorrectionType().toString()) });
writer.writeOpenTag(SpeciationLikelihoodParser.POINT);
writer.writeIDref(TaxaParser.TAXA, t.getId());
writeDistribution(nodeCalib, true, writer);
writer.writeCloseTag(SpeciationLikelihoodParser.POINT);
writer.writeCloseTag(SpeciationLikelihoodParser.CALIBRATION);
if (!options.treeModelOptions.isNodeCalibrated(nodeCalib)) {
throw new IllegalArgumentException("Calibrated Yule model requires a calibration to be specified for node, " + nodeCalib.getName() + ".");
}
}
}
writer.writeCloseTag(SpeciationLikelihoodParser.SPECIATION_LIKELIHOOD);
break;
case SKYLINE:
// generate a Bayesian skyline plot
writer.writeComment("Generate a generalizedSkyLineLikelihood for Bayesian Skyline");
writer.writeOpenTag(BayesianSkylineLikelihoodParser.SKYLINE_LIKELIHOOD, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, modelPrefix + "skyline"), new Attribute.Default<String>("linear", prior.getSkylineModel() == TreePriorParameterizationType.LINEAR_SKYLINE ? "true" : "false") });
// write pop size socket
writer.writeOpenTag(BayesianSkylineLikelihoodParser.POPULATION_SIZES);
if (prior.getSkylineModel() == TreePriorParameterizationType.LINEAR_SKYLINE) {
writeParameter(prior.getParameter("skyline.popSize"), prior.getSkylineGroupCount() + 1, writer);
} else {
writeParameter(prior.getParameter("skyline.popSize"), prior.getSkylineGroupCount(), writer);
}
writer.writeCloseTag(BayesianSkylineLikelihoodParser.POPULATION_SIZES);
// write group size socket
writer.writeOpenTag(BayesianSkylineLikelihoodParser.GROUP_SIZES);
writeParameter(prior.getParameter("skyline.groupSize"), prior.getSkylineGroupCount(), writer);
writer.writeCloseTag(BayesianSkylineLikelihoodParser.GROUP_SIZES);
writer.writeOpenTag(CoalescentLikelihoodParser.POPULATION_TREE);
writer.writeIDref(TreeModel.TREE_MODEL, modelPrefix + TreeModel.TREE_MODEL);
writer.writeCloseTag(CoalescentLikelihoodParser.POPULATION_TREE);
writer.writeCloseTag(BayesianSkylineLikelihoodParser.SKYLINE_LIKELIHOOD);
writer.writeText("");
writeExponentialMarkovLikelihood(prior, writer);
break;
case EXTENDED_SKYLINE:
// different format
break;
case GMRF_SKYRIDE:
writer.writeComment("Generate a gmrfSkyrideLikelihood for GMRF Bayesian Skyride process");
writer.writeOpenTag(GMRFSkyrideLikelihoodParser.SKYLINE_LIKELIHOOD, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, modelPrefix + "skyride"), new Attribute.Default<String>(GMRFSkyrideLikelihoodParser.TIME_AWARE_SMOOTHING, prior.getSkyrideSmoothing() == TreePriorParameterizationType.TIME_AWARE_SKYRIDE ? "true" : "false"), new Attribute.Default<String>(GMRFSkyrideLikelihoodParser.RANDOMIZE_TREE, //TODO For GMRF, tree model/tree prior combination not implemented by BEAST yet. The validation is in BeastGenerator.checkOptions()
options.getPartitionTreeModels(prior).get(0).getStartingTreeType() == StartingTreeType.UPGMA ? "true" : "false") });
int skyrideIntervalCount = options.taxonList.getTaxonCount() - 1;
writer.writeOpenTag(GMRFSkyrideLikelihoodParser.POPULATION_PARAMETER);
writer.writeComment("skyride.logPopSize is in log units unlike other popSize");
writeParameter(prior.getParameter("skyride.logPopSize"), skyrideIntervalCount, writer);
writer.writeCloseTag(GMRFSkyrideLikelihoodParser.POPULATION_PARAMETER);
writer.writeOpenTag(GMRFSkyrideLikelihoodParser.GROUP_SIZES);
writeParameter(prior.getParameter("skyride.groupSize"), skyrideIntervalCount, writer);
writer.writeCloseTag(GMRFSkyrideLikelihoodParser.GROUP_SIZES);
writer.writeOpenTag(GMRFSkyrideLikelihoodParser.PRECISION_PARAMETER);
writeParameter(prior.getParameter("skyride.precision"), 1, writer);
writer.writeCloseTag(GMRFSkyrideLikelihoodParser.PRECISION_PARAMETER);
writer.writeOpenTag(GMRFSkyrideLikelihoodParser.POPULATION_TREE);
writer.writeIDref(TreeModel.TREE_MODEL, modelPrefix + TreeModel.TREE_MODEL);
writer.writeCloseTag(GMRFSkyrideLikelihoodParser.POPULATION_TREE);
writer.writeCloseTag(GMRFSkyrideLikelihoodParser.SKYLINE_LIKELIHOOD);
break;
case SKYGRID:
break;
case SPECIES_YULE:
case SPECIES_YULE_CALIBRATION:
case SPECIES_BIRTH_DEATH:
break;
default:
// generate a coalescent process
writer.writeComment("Generate a coalescent likelihood");
writer.writeOpenTag(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, modelPrefix + COALESCENT) });
writer.writeOpenTag(CoalescentLikelihoodParser.MODEL);
writeNodeHeightPriorModelRef(prior, writer);
writer.writeCloseTag(CoalescentLikelihoodParser.MODEL);
writer.writeOpenTag(CoalescentLikelihoodParser.POPULATION_TREE);
writer.writeIDref(TreeModel.TREE_MODEL, modelPrefix + TreeModel.TREE_MODEL);
writer.writeCloseTag(CoalescentLikelihoodParser.POPULATION_TREE);
writer.writeCloseTag(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD);
}
}
use of dr.util.Attribute in project beast-mcmc by beast-dev.
the class TMRCAStatisticsGenerator method writeTMRCAStatistics.
/**
* Generate tmrca statistics
*
* @param writer the writer
*/
public void writeTMRCAStatistics(XMLWriter writer) {
List<Taxa> taxonSets;
Map<Taxa, Boolean> taxonSetsMono;
if (options.useStarBEAST) {
taxonSets = options.speciesSets;
taxonSetsMono = options.speciesSetsMono;
writer.writeComment("Species Sets");
writer.writeText("");
for (Taxa taxa : taxonSets) {
writer.writeOpenTag(TMRCAStatisticParser.TMRCA_STATISTIC, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, "tmrca(" + taxa.getId() + ")") });
// make tmrca(tree.name) eay to read in log for Tracer
writer.writeOpenTag(TMRCAStatisticParser.MRCA);
writer.writeIDref(TaxaParser.TAXA, taxa.getId());
writer.writeCloseTag(TMRCAStatisticParser.MRCA);
writer.writeIDref(SpeciesTreeModelParser.SPECIES_TREE, SP_TREE);
writer.writeCloseTag(TMRCAStatisticParser.TMRCA_STATISTIC);
if (taxonSetsMono.get(taxa)) {
// && treeModel.getPartitionTreePrior().getNodeHeightPrior() != TreePriorType.YULE
// && options.getKeysFromValue(options.taxonSetsTreeModel, treeModel).size() > 1) {
writer.writeOpenTag(MonophylyStatisticParser.MONOPHYLY_STATISTIC, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, "monophyly(" + taxa.getId() + ")") });
writer.writeOpenTag(MonophylyStatisticParser.MRCA);
writer.writeIDref(TaxaParser.TAXA, taxa.getId());
writer.writeCloseTag(MonophylyStatisticParser.MRCA);
writer.writeIDref(SpeciesTreeModelParser.SPECIES_TREE, SP_TREE);
writer.writeCloseTag(MonophylyStatisticParser.MONOPHYLY_STATISTIC);
}
}
} else {
taxonSets = options.taxonSets;
taxonSetsMono = options.taxonSetsMono;
writer.writeComment("Taxon Sets");
writer.writeText("");
for (Taxa taxa : taxonSets) {
PartitionTreeModel treeModel = options.taxonSetsTreeModel.get(taxa);
writer.writeOpenTag(TMRCAStatisticParser.TMRCA_STATISTIC, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, "tmrca(" + treeModel.getPrefix() + taxa.getId() + ")"), new Attribute.Default<Boolean>(TMRCAStatisticParser.STEM, options.taxonSetsIncludeStem.get(taxa)) });
// make tmrca(tree.name) eay to read in log for Tracer
writer.writeOpenTag(TMRCAStatisticParser.MRCA);
writer.writeIDref(TaxaParser.TAXA, taxa.getId());
writer.writeCloseTag(TMRCAStatisticParser.MRCA);
writer.writeIDref(TreeModel.TREE_MODEL, treeModel.getPrefix() + TreeModel.TREE_MODEL);
writer.writeCloseTag(TMRCAStatisticParser.TMRCA_STATISTIC);
if (taxonSetsMono.get(taxa)) {
// && treeModel.getPartitionTreePrior().getNodeHeightPrior() != TreePriorType.YULE
// && options.getKeysFromValue(options.taxonSetsTreeModel, treeModel).size() > 1) {
writer.writeOpenTag(MonophylyStatisticParser.MONOPHYLY_STATISTIC, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, "monophyly(" + taxa.getId() + ")") });
writer.writeOpenTag(MonophylyStatisticParser.MRCA);
writer.writeIDref(TaxaParser.TAXA, taxa.getId());
writer.writeCloseTag(MonophylyStatisticParser.MRCA);
writer.writeIDref(TreeModel.TREE_MODEL, treeModel.getPrefix() + TreeModel.TREE_MODEL);
writer.writeCloseTag(MonophylyStatisticParser.MONOPHYLY_STATISTIC);
}
}
}
}
use of dr.util.Attribute in project beast-mcmc by beast-dev.
the class TMRCAStatisticsGenerator method writeTaxonSets.
/**
* Generate additional taxon sets
*
* @param writer the writer
* @param taxonSets a list of taxa to write
*/
public void writeTaxonSets(XMLWriter writer, List<Taxa> taxonSets) {
writer.writeText("");
for (Taxa taxa : taxonSets) {
writer.writeOpenTag(TaxaParser.TAXA, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, taxa.getId()) });
for (int j = 0; j < taxa.getTaxonCount(); j++) {
writer.writeIDref(TaxonParser.TAXON, taxa.getTaxon(j).getId());
}
writer.writeCloseTag(TaxaParser.TAXA);
}
}
use of dr.util.Attribute in project beast-mcmc by beast-dev.
the class TreeLikelihoodGenerator method writeTreeLikelihood.
/**
* Write the tree likelihood XML block.
*
* @param id the id of the tree likelihood
* @param num the likelihood number
* @param partition the partition to write likelihood block for
* @param writer the writer
*/
private void writeTreeLikelihood(String tag, String id, int num, PartitionData partition, XMLWriter writer) {
PartitionSubstitutionModel substModel = partition.getPartitionSubstitutionModel();
PartitionTreeModel treeModel = partition.getPartitionTreeModel();
PartitionClockModel clockModel = partition.getPartitionClockModel();
writer.writeComment("Likelihood for tree given sequence data");
String prefix;
if (num > 0) {
prefix = partition.getPrefix() + substModel.getPrefixCodon(num);
} else {
prefix = partition.getPrefix();
}
String idString = prefix + id;
Attribute[] attributes;
if (tag.equals(MarkovJumpsTreeLikelihoodParser.MARKOV_JUMP_TREE_LIKELIHOOD)) {
AncestralStatesComponentOptions ancestralStatesOptions = (AncestralStatesComponentOptions) options.getComponentOptions(AncestralStatesComponentOptions.class);
boolean saveCompleteHistory = ancestralStatesOptions.isCompleteHistoryLogging(partition);
attributes = new Attribute[] { new Attribute.Default<String>(XMLParser.ID, idString), new Attribute.Default<Boolean>(TreeLikelihoodParser.USE_AMBIGUITIES, substModel.isUseAmbiguitiesTreeLikelihood()), new Attribute.Default<Boolean>(MarkovJumpsTreeLikelihoodParser.USE_UNIFORMIZATION, true), new Attribute.Default<Integer>(MarkovJumpsTreeLikelihoodParser.NUMBER_OF_SIMULANTS, 1), new Attribute.Default<String>(AncestralStateTreeLikelihoodParser.RECONSTRUCTION_TAG_NAME, prefix + AncestralStateTreeLikelihoodParser.RECONSTRUCTION_TAG), new Attribute.Default<String>(MarkovJumpsTreeLikelihoodParser.SAVE_HISTORY, saveCompleteHistory ? "true" : "false") };
} else if (tag.equals(TreeLikelihoodParser.ANCESTRAL_TREE_LIKELIHOOD)) {
attributes = new Attribute[] { new Attribute.Default<String>(XMLParser.ID, idString), new Attribute.Default<Boolean>(TreeLikelihoodParser.USE_AMBIGUITIES, substModel.isUseAmbiguitiesTreeLikelihood()), new Attribute.Default<String>(AncestralStateTreeLikelihoodParser.RECONSTRUCTION_TAG_NAME, prefix + AncestralStateTreeLikelihoodParser.RECONSTRUCTION_TAG) };
} else {
attributes = new Attribute[] { new Attribute.Default<String>(XMLParser.ID, idString), new Attribute.Default<Boolean>(TreeLikelihoodParser.USE_AMBIGUITIES, substModel.isUseAmbiguitiesTreeLikelihood()) };
}
writer.writeOpenTag(tag, attributes);
if (!options.samplePriorOnly) {
if (num > 0) {
writeCodonPatternsRef(prefix, num, substModel.getCodonPartitionCount(), writer);
} else {
writer.writeIDref(SitePatternsParser.PATTERNS, prefix + SitePatternsParser.PATTERNS);
}
} else {
// We just need to use the dummy alignment
writer.writeIDref(AlignmentParser.ALIGNMENT, partition.getAlignment().getId());
}
writer.writeIDref(TreeModel.TREE_MODEL, treeModel.getPrefix() + TreeModel.TREE_MODEL);
if (num > 0) {
writer.writeIDref(GammaSiteModel.SITE_MODEL, substModel.getPrefix(num) + SiteModel.SITE_MODEL);
} else {
writer.writeIDref(GammaSiteModel.SITE_MODEL, substModel.getPrefix() + SiteModel.SITE_MODEL);
}
ClockModelGenerator.writeBranchRatesModelRef(clockModel, writer);
generateInsertionPoint(ComponentGenerator.InsertionPoint.IN_TREE_LIKELIHOOD, partition, prefix, writer);
writer.writeCloseTag(tag);
}
use of dr.util.Attribute in project beast-mcmc by beast-dev.
the class STARBEASTGenerator method writeGeneUnderSpecies.
private void writeGeneUnderSpecies(XMLWriter writer) {
writer.writeComment("Species Tree: Coalescent likelihood for gene trees under species tree");
// speciesCoalescent id="coalescent"
writer.writeOpenTag(MultiSpeciesCoalescentParser.SPECIES_COALESCENT, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, TraitData.TRAIT_SPECIES + "." + COALESCENT) });
writer.writeIDref(TraitData.TRAIT_SPECIES, TraitData.TRAIT_SPECIES);
writer.writeIDref(SpeciesTreeModelParser.SPECIES_TREE, SP_TREE);
writer.writeCloseTag(MultiSpeciesCoalescentParser.SPECIES_COALESCENT);
// exponentialDistributionModel id="pdist"
// writer.writeOpenTag(ExponentialDistributionModel.EXPONENTIAL_DISTRIBUTION_MODEL, new Attribute[]{
// new Attribute.Default<String>(XMLParser.ID, PDIST)});
//
// writer.writeOpenTag(DistributionModelParser.MEAN);
//
// Parameter para = options.getParameter(TraitGuesser.Traits.TRAIT_SPECIES + "." + options.POP_MEAN);
//
// writer.writeTag(ParameterParser.PARAMETER, new Attribute[]{
// new Attribute.Default<String>(XMLParser.ID, TraitGuesser.Traits.TRAIT_SPECIES + "." + options.POP_MEAN),
// new Attribute.Default<String>(ParameterParser.VALUE, Double.toString(para.initial))}, true);
//
// writer.writeCloseTag(DistributionModelParser.MEAN);
//
// writer.writeCloseTag(ExponentialDistributionModel.EXPONENTIAL_DISTRIBUTION_MODEL);
// if (options.speciesTreePrior == TreePriorType.SPECIES_YULE) {
writer.writeComment("Species tree prior: gama2 + gamma4");
writer.writeOpenTag(MixedDistributionLikelihoodParser.DISTRIBUTION_LIKELIHOOD, new Attribute[] { new Attribute.Default<String>(XMLParser.ID, SPOPS) });
// change exponential + gamma2 into gama2 + gamma4
// <distribution0>
writer.writeOpenTag(MixedDistributionLikelihoodParser.DISTRIBUTION0);
// writer.writeIDref(ExponentialDistributionModel.EXPONENTIAL_DISTRIBUTION_MODEL, PDIST);
writer.writeOpenTag(GammaDistributionModel.GAMMA_DISTRIBUTION_MODEL);
writer.writeOpenTag(DistributionModelParser.SHAPE);
writer.writeText("2");
writer.writeCloseTag(DistributionModelParser.SHAPE);
writer.writeOpenTag(DistributionModelParser.SCALE);
Parameter para = options.starBEASTOptions.getParameter(TraitData.TRAIT_SPECIES + "." + options.starBEASTOptions.POP_MEAN);
writeParameter(para, 1, writer);
writer.writeCloseTag(DistributionModelParser.SCALE);
writer.writeCloseTag(GammaDistributionModel.GAMMA_DISTRIBUTION_MODEL);
writer.writeCloseTag(MixedDistributionLikelihoodParser.DISTRIBUTION0);
// <distribution1>
writer.writeOpenTag(MixedDistributionLikelihoodParser.DISTRIBUTION1);
writer.writeOpenTag(GammaDistributionModel.GAMMA_DISTRIBUTION_MODEL);
writer.writeOpenTag(DistributionModelParser.SHAPE);
writer.writeText("4");
writer.writeCloseTag(DistributionModelParser.SHAPE);
writer.writeOpenTag(DistributionModelParser.SCALE);
writer.writeIDref(ParameterParser.PARAMETER, TraitData.TRAIT_SPECIES + "." + options.starBEASTOptions.POP_MEAN);
writer.writeCloseTag(DistributionModelParser.SCALE);
writer.writeCloseTag(GammaDistributionModel.GAMMA_DISTRIBUTION_MODEL);
writer.writeCloseTag(MixedDistributionLikelihoodParser.DISTRIBUTION1);
// <data>
writer.writeOpenTag(MixedDistributionLikelihoodParser.DATA);
writer.writeIDref(ParameterParser.PARAMETER, SpeciesTreeModelParser.SPECIES_TREE + "." + SPLIT_POPS);
writer.writeCloseTag(MixedDistributionLikelihoodParser.DATA);
// <indicators>
writer.writeOpenTag(MixedDistributionLikelihoodParser.INDICATORS);
// Needs special treatment - you have to generate "NS" ones and 2(N-1) zeros, where N is the number of species.
// N "1", 2(N-1) "0"
writer.writeTag(ParameterParser.PARAMETER, new Attribute[] { new Attribute.Default<String>(ParameterParser.VALUE, getIndicatorsParaValue()) }, true);
writer.writeCloseTag(MixedDistributionLikelihoodParser.INDICATORS);
writer.writeCloseTag(MixedDistributionLikelihoodParser.DISTRIBUTION_LIKELIHOOD);
// } else {
// // STPopulationPrior id="stp" log_root="true"
// writer.writeOpenTag(SpeciesTreeBMPrior.STPRIOR, new Attribute[]{
// new Attribute.Default<String>(XMLParser.ID, STP),
// new Attribute.Default<String>(SpeciesTreeBMPrior.LOG_ROOT, "true")});
// writer.writeIDref(SpeciesTreeModelParser.SPECIES_TREE, SP_TREE);
//
// writer.writeOpenTag(SpeciesTreeBMPrior.TIPS);
//
// writer.writeIDref(ExponentialDistributionModel.EXPONENTIAL_DISTRIBUTION_MODEL, PDIST);
//
// writer.writeCloseTag(SpeciesTreeBMPrior.TIPS);
//
// writer.writeOpenTag(SpeciesTreeBMPrior.STSIGMA);
//
// writer.writeTag(ParameterParser.PARAMETER, new Attribute[]{
// // <parameter id="stsigma" value="1" />
// new Attribute.Default<String>(XMLParser.ID, SpeciesTreeBMPrior.STSIGMA.toLowerCase()),
// new Attribute.Default<String>(ParameterParser.VALUE, "1")}, true);
//
// writer.writeCloseTag(SpeciesTreeBMPrior.STSIGMA);
//
// writer.writeCloseTag(SpeciesTreeBMPrior.STPRIOR);
// }
}
Aggregations