use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class RandomBranchModelParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Logger.getLogger("dr.evomodel").info("\nUsing random assignment branch model.");
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
GY94CodonModel baseSubstitutionModel = (GY94CodonModel) xo.getElementFirstChild(BASE_MODEL);
long seed = -1;
boolean hasSeed = false;
if (xo.hasAttribute(SEED)) {
seed = xo.getLongIntegerAttribute(SEED);
hasSeed = true;
}
double rate = 1;
if (xo.hasAttribute(RATE)) {
rate = xo.getDoubleAttribute(RATE);
}
return new RandomBranchModel(treeModel, baseSubstitutionModel, rate, hasSeed, seed);
}
use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class BranchCategoriesParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Parameter allocationParameter = (Parameter) xo.getElementFirstChild(ALLOCATION);
CountableBranchCategoryProvider cladeModel;
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class);
if (!xo.getAttribute(RANDOMIZE, true)) {
CountableBranchCategoryProvider.CladeBranchCategoryModel cm = new CountableBranchCategoryProvider.CladeBranchCategoryModel(treeModel, allocationParameter);
for (int i = 0; i < xo.getChildCount(); ++i) {
if (xo.getChild(i) instanceof XMLObject) {
XMLObject xoc = (XMLObject) xo.getChild(i);
if (xoc.getName().equals(LocalClockModelParser.CLADE)) {
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
boolean includeStem = xoc.getAttribute(LocalClockModelParser.INCLUDE_STEM, false);
boolean excludeClade = xoc.getAttribute(LocalClockModelParser.EXCLUDE_CLADE, false);
// XML index-start = 1 not 0
int rateCategory = xoc.getIntegerAttribute(CATEGORY) - 1;
try {
cm.setClade(taxonList, rateCategory, includeStem, excludeClade, false);
} catch (TreeUtils.MissingTaxonException e) {
throw new XMLParseException("Unable to find taxon for clade in countable mixture model: " + e.getMessage());
}
} else if (xoc.getName().equals(LocalClockModelParser.TRUNK)) {
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
boolean includeStem = xoc.getAttribute(LocalClockModelParser.INCLUDE_STEM, false);
boolean excludeClade = xoc.getAttribute(LocalClockModelParser.EXCLUDE_CLADE, false);
// XML index-start = 1 not 0
int rateCategory = xoc.getIntegerAttribute(CATEGORY) - 1;
try {
cm.setClade(taxonList, rateCategory, includeStem, excludeClade, true);
} catch (TreeUtils.MissingTaxonException e) {
throw new XMLParseException("Unable to find taxon for trunk in countable mixture model: " + e.getMessage());
}
}
}
}
cladeModel = cm;
} else {
CountableBranchCategoryProvider.IndependentBranchCategoryModel cm = new CountableBranchCategoryProvider.IndependentBranchCategoryModel(treeModel, allocationParameter);
cm.randomize();
cladeModel = cm;
}
return cladeModel;
}
use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class AncestralSequenceAnnotator method sampleTree.
private FlexibleTree sampleTree(Tree tree, PatternList alignment, GammaSiteRateModel siteModel, BranchRateModel rateModel) {
FlexibleTree flexTree = new FlexibleTree(tree, true);
flexTree.adoptTreeModelOrdering();
FlexibleTree finalTree = new FlexibleTree(tree);
finalTree.adoptTreeModelOrdering();
TreeModel treeModel = new TreeModel(tree);
// Turn off noisy logging by TreeLikelihood constructor
Logger logger = Logger.getLogger("dr.evomodel");
boolean useParentHandlers = logger.getUseParentHandlers();
logger.setUseParentHandlers(false);
// AncestralStateTreeLikelihood likelihood = new AncestralStateTreeLikelihood(
// alignment,
// treeModel,
// siteModel,
// rateModel,
// false, true,
// alignment.getDataType(),
// TAG,
// false);
AncestralStateBeagleTreeLikelihood likelihood = new AncestralStateBeagleTreeLikelihood(alignment, treeModel, new HomogeneousBranchModel(siteModel.getSubstitutionModel()), siteModel, rateModel, null, false, PartialsRescalingScheme.DEFAULT, true, null, alignment.getDataType(), TAG, false, true);
// PatternList patternList, TreeModel treeModel,
// BranchSiteModel branchSiteModel,
// SiteRateModel siteRateModel,
// BranchRateModel branchRateModel,
// boolean useAmbiguities,
// PartialsRescalingScheme scalingScheme,
// Map<Set<String>, Parameter> partialsRestrictions,
// final DataType dataType,
// final String tag,
// SubstitutionModel substModel,
// boolean useMAP,
// boolean returnML) {
// PatternList patternList, TreeModel treeModel,
// SiteModel siteModel, BranchRateModel branchRateModel,
// boolean useAmbiguities, boolean storePartials,
// final DataType dataType,
// final String tag,
// boolean forceRescaling,
// boolean useMAP,
// boolean returnML) {
logger.setUseParentHandlers(useParentHandlers);
// Sample internal nodes
likelihood.makeDirty();
double logLikelihood = likelihood.getLogLikelihood();
System.out.println("The new and old Likelihood (this value should be roughly the same, debug?): " + logLikelihood + ", " + Double.parseDouble(tree.getAttribute(LIKELIHOOD).toString()));
if (Double.parseDouble(tree.getAttribute(LIKELIHOOD).toString()) != logLikelihood) {
/* Newly written check, not sure if this is correct. May need to round values at least */
//throw new RuntimeException("The values of likelihood are not identical");
}
// System.err.printf("New logLikelihood = %4.1f\n", logLikelihood);
flexTree.setAttribute(LIKELIHOOD, logLikelihood);
TreeTrait ancestralStates = likelihood.getTreeTrait(TAG);
for (int i = 0; i < treeModel.getNodeCount(); i++) {
NodeRef node = treeModel.getNode(i);
String sample = ancestralStates.getTraitString(treeModel, node);
String oldSeq = (String) flexTree.getNodeAttribute(flexTree.getNode(i), SEQ_STRING);
if (oldSeq != null) {
char[] seq = (sample.substring(1, sample.length() - 1)).toCharArray();
int length = oldSeq.length();
for (int j = 0; j < length; j++) {
if (oldSeq.charAt(j) == GAP)
seq[j] = GAP;
}
String newSeq = new String(seq);
// if( newSeq.contains("MMMMMMM") ) {
// System.err.println("bad = "+newSeq);
// System.exit(-1);
// }
finalTree.setNodeAttribute(finalTree.getNode(i), NEW_SEQ, newSeq);
}
// Taxon taxon = finalTree.getNodeTaxon(finalTree.getNode(i));
// System.err.println("node: "+(taxon == null ? "null" : taxon.getId())+" "+
// finalTree.getNodeAttribute(finalTree.getNode(i),NEW_SEQ));
}
return finalTree;
}
use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class GMRFMultilocusSkyrideLikelihood method handleModelChangedEvent.
protected void handleModelChangedEvent(Model model, Object object, int index) {
if (model instanceof TreeModel) {
TreeModel treeModel = (TreeModel) model;
int tn = treeList.indexOf(treeModel);
if (tn >= 0) {
// intervalsList.get(tn).setIntervalsUnknown(); // TODO Why is this slower (?) than remaking whole list?
makeTreeIntervalList(treeList, false);
intervalsKnown = false;
likelihoodKnown = false;
} else {
throw new RuntimeException("Unknown tree modified in GMRFMultilocusSkyrideLikelihood");
}
} else {
throw new RuntimeException("Unknown object modified in GMRFMultilocusSkyrideLikelihood");
}
}
use of dr.evomodel.tree.TreeModel in project beast-mcmc by beast-dev.
the class GaussianProcessMultilocusSkytrackLikelihood method handleModelChangedEvent.
//
// protected int getCorrectOldFieldLength() {
// int tips = 0;
// for (Tree tree : treeList) {
// tips += tree.getExternalNodeCount();
// }
// return tips - treeList.size();
// }
protected void handleModelChangedEvent(Model model, Object object, int index) {
if (model instanceof TreeModel) {
TreeModel treeModel = (TreeModel) model;
int tn = treeList.indexOf(treeModel);
if (tn >= 0) {
// intervalsList.get(tn).setIntervalsUnknown(); // TODO Why is this slower (?) than remaking whole list?
makeTreeIntervalList(treeList, false);
intervalsKnown = false;
likelihoodKnown = false;
} else {
throw new RuntimeException("Unknown tree modified in GMRFMultilocusSkyrideLikelihood");
}
} else {
throw new RuntimeException("Unknown object modified in GMRFMultilocusSkyrideLikelihood");
}
}
Aggregations