use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.
the class LocalClockModelParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
LocalClockModel localClockModel;
if (xo.getElementFirstChild(RATE) instanceof BranchRateModel) {
BranchRateModel globalBranchRates = (BranchRateModel) xo.getElementFirstChild(RATE);
localClockModel = new LocalClockModel(tree, globalBranchRates);
} else {
Parameter globalRateParameter = (Parameter) xo.getElementFirstChild(RATE);
localClockModel = new LocalClockModel(tree, globalRateParameter);
}
for (int i = 0; i < xo.getChildCount(); i++) {
if (xo.getChild(i) instanceof XMLObject) {
XMLObject xoc = (XMLObject) xo.getChild(i);
Parameter rateParameter = null;
BranchRateModel branchRates = (BranchRateModel) xoc.getChild(BranchRateModel.class);
if (branchRates == null) {
rateParameter = (Parameter) xoc.getChild(Parameter.class);
}
if (xoc.getName().equals(CLADE)) {
boolean relative = xoc.getAttribute(RELATIVE, false);
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
if (taxonList.getTaxonCount() == 1) {
throw new XMLParseException("A local clock for a clade must be defined by at least two taxa");
}
boolean excludeClade = false;
double stemProportion = 0.0;
if (xoc.hasAttribute(INCLUDE_STEM)) {
// if includeStem=true then assume it is the whole stem
stemProportion = xoc.getBooleanAttribute(INCLUDE_STEM) ? 1.0 : 0.0;
}
if (xoc.hasAttribute(STEM_PROPORTION)) {
stemProportion = xoc.getDoubleAttribute(STEM_PROPORTION);
if (stemProportion < 0.0 || stemProportion > 1.0) {
throw new XMLParseException("A stem proportion should be between 0, 1");
}
}
if (xoc.hasAttribute(EXCLUDE_CLADE)) {
excludeClade = xoc.getBooleanAttribute(EXCLUDE_CLADE);
}
try {
if (branchRates != null) {
localClockModel.addCladeClock(taxonList, branchRates, relative, stemProportion, excludeClade);
} else {
localClockModel.addCladeClock(taxonList, rateParameter, relative, stemProportion, excludeClade);
}
} catch (TreeUtils.MissingTaxonException mte) {
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
}
} else if (xoc.getName().equals(EXTERNAL_BRANCHES)) {
boolean relative = xoc.getAttribute(RELATIVE, false);
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
try {
if (branchRates != null) {
localClockModel.addExternalBranchClock(taxonList, branchRates, relative);
} else {
localClockModel.addExternalBranchClock(taxonList, rateParameter, relative);
}
} catch (TreeUtils.MissingTaxonException mte) {
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
}
} else if (xoc.getName().equals(TRUNK)) {
boolean relative = xoc.getAttribute(RELATIVE, false);
Parameter indexParameter = null;
if (xoc.hasChildNamed(INDEX)) {
indexParameter = (Parameter) xoc.getElementFirstChild(INDEX);
}
TaxonList taxonList = (TaxonList) xoc.getChild(TaxonList.class);
try {
if (branchRates != null) {
localClockModel.addTrunkClock(taxonList, branchRates, indexParameter, relative);
} else {
localClockModel.addTrunkClock(taxonList, rateParameter, indexParameter, relative);
}
} catch (TreeUtils.MissingTaxonException mte) {
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + " was not found in the tree.");
}
}
}
}
Logger.getLogger("dr.evomodel").info("\nUsing local clock branch rate model.");
return localClockModel;
}
use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.
the class RestrictedPartialsParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
String name = xo.getId();
TreeModel tree = (TreeModel) xo.getChild(TreeModel.class);
TaxonList taxa = MonophylyStatisticParser.parseTaxonListOrTaxa(xo.getChild(MonophylyStatisticParser.MRCA));
Parameter meanParameter = (Parameter) xo.getElementFirstChild(MultivariateDistributionLikelihood.MVN_MEAN);
Parameter priorSampleSize = (Parameter) xo.getElementFirstChild(AbstractMultivariateTraitLikelihood.PRIOR_SAMPLE_SIZE);
RestrictedPartials rp = null;
try {
rp = new RestrictedPartials(name, tree, taxa, meanParameter, priorSampleSize);
} catch (TreeUtils.MissingTaxonException e) {
throw new XMLParseException("Unable to find taxa for " + xo.getId());
}
return rp;
}
use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.
the class CoalescentSimulatorParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
CoalescentSimulator simulator = new CoalescentSimulator();
DemographicModel demoModel = (DemographicModel) xo.getChild(DemographicModel.class);
List<TaxonList> taxonLists = new ArrayList<TaxonList>();
List<Tree> subtrees = new ArrayList<Tree>();
double height = xo.getAttribute(HEIGHT, Double.NaN);
Tree constraintsTree = null;
if (xo.hasChildNamed((CONSTRAINTS_TREE))) {
XMLObject cxo = xo.getChild(CONSTRAINTS_TREE);
constraintsTree = (Tree) cxo.getChild(Tree.class);
}
// should have one child that is node
for (int i = 0; i < xo.getChildCount(); i++) {
final Object child = xo.getChild(i);
if (child != constraintsTree) {
// AER - swapped the order of these round because Trees are TaxonLists...
if (child instanceof Tree) {
subtrees.add((Tree) child);
} else if (child instanceof TaxonList) {
taxonLists.add((TaxonList) child);
}
}
}
if (taxonLists.size() == 0) {
if (subtrees.size() == 1) {
return subtrees.get(0);
}
if (constraintsTree == null) {
throw new XMLParseException("Expected at least one taxonList or two subtrees or a constraints tree in " + getParserName() + " element.");
}
}
Taxa remainingTaxa = new Taxa();
for (int i = 0; i < taxonLists.size(); i++) {
remainingTaxa.addTaxa(taxonLists.get(i));
}
for (int i = 0; i < subtrees.size(); i++) {
remainingTaxa.removeTaxa(subtrees.get(i));
}
if (constraintsTree != null) {
remainingTaxa.removeTaxa(constraintsTree);
}
try {
if (constraintsTree != null) {
subtrees.add(simulator.simulateTree(constraintsTree, constraintsTree.getRoot(), demoModel));
}
Tree[] trees = new Tree[subtrees.size() + remainingTaxa.getTaxonCount()];
// add the preset trees
for (int i = 0; i < subtrees.size(); i++) {
trees[i] = subtrees.get(i);
}
// add all the remaining taxa in as single tip trees...
for (int i = 0; i < remainingTaxa.getTaxonCount(); i++) {
Taxa tip = new Taxa();
tip.addTaxon(remainingTaxa.getTaxon(i));
trees[i + subtrees.size()] = simulator.simulateTree(tip, demoModel);
}
return simulator.simulateTree(trees, demoModel, height, trees.length != 1);
} catch (IllegalArgumentException iae) {
throw new XMLParseException(iae.getMessage());
}
}
use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.
the class OldCoalescentSimulatorParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
CoalescentSimulator simulator = new CoalescentSimulator();
DemographicModel demoModel = (DemographicModel) xo.getChild(DemographicModel.class);
List<TaxonList> taxonLists = new ArrayList<TaxonList>();
List<Tree> subtrees = new ArrayList<Tree>();
double rootHeight = xo.getAttribute(ROOT_HEIGHT, -1.0);
if (xo.hasAttribute(RESCALE_HEIGHT)) {
rootHeight = xo.getDoubleAttribute(RESCALE_HEIGHT);
}
// should have one child that is node
for (int i = 0; i < xo.getChildCount(); i++) {
final Object child = xo.getChild(i);
// AER - swapped the order of these round because Trees are TaxonLists...
if (child instanceof Tree) {
subtrees.add((Tree) child);
} else if (child instanceof TaxonList) {
taxonLists.add((TaxonList) child);
} else if (xo.getChildName(i).equals(CONSTRAINED_TAXA)) {
XMLObject constrainedTaxa = (XMLObject) child;
// all taxa
final TaxonList taxa = (TaxonList) constrainedTaxa.getChild(TaxonList.class);
List<CoalescentSimulator.TaxaConstraint> constraints = new ArrayList<CoalescentSimulator.TaxaConstraint>();
final String setsNotCompatibleMessage = "taxa sets not compatible";
for (int nc = 0; nc < constrainedTaxa.getChildCount(); ++nc) {
final Object object = constrainedTaxa.getChild(nc);
if (object instanceof XMLObject) {
final XMLObject constraint = (XMLObject) object;
if (constraint.getName().equals(TMRCA_CONSTRAINT)) {
TaxonList taxaSubSet = (TaxonList) constraint.getChild(TaxonList.class);
ParametricDistributionModel dist = (ParametricDistributionModel) constraint.getChild(ParametricDistributionModel.class);
boolean isMono = constraint.getAttribute(IS_MONOPHYLETIC, true);
final CoalescentSimulator.TaxaConstraint taxaConstraint = new CoalescentSimulator.TaxaConstraint(taxaSubSet, dist, isMono);
int insertPoint;
for (insertPoint = 0; insertPoint < constraints.size(); ++insertPoint) {
// if new <= constraints[insertPoint] insert before insertPoint
final CoalescentSimulator.TaxaConstraint iConstraint = constraints.get(insertPoint);
if (iConstraint.isMonophyletic) {
if (!taxaConstraint.isMonophyletic) {
continue;
}
final TaxonList taxonsip = iConstraint.taxons;
final int nIn = simulator.sizeOfIntersection(taxonsip, taxaSubSet);
if (nIn == taxaSubSet.getTaxonCount()) {
break;
}
if (nIn > 0 && nIn != taxonsip.getTaxonCount()) {
throw new XMLParseException(setsNotCompatibleMessage);
}
} else {
// reached non mono area
if (!taxaConstraint.isMonophyletic) {
if (iConstraint.upper >= taxaConstraint.upper) {
break;
}
} else {
break;
}
}
}
constraints.add(insertPoint, taxaConstraint);
}
}
}
final int nConstraints = constraints.size();
if (nConstraints == 0) {
if (taxa != null) {
taxonLists.add(taxa);
}
} else {
for (int nc = 0; nc < nConstraints; ++nc) {
CoalescentSimulator.TaxaConstraint cnc = constraints.get(nc);
if (!cnc.isMonophyletic) {
for (int nc1 = nc - 1; nc1 >= 0; --nc1) {
CoalescentSimulator.TaxaConstraint cnc1 = constraints.get(nc1);
int x = simulator.sizeOfIntersection(cnc.taxons, cnc1.taxons);
if (x > 0) {
Taxa combinedTaxa = new Taxa(cnc.taxons);
combinedTaxa.addTaxa(cnc1.taxons);
cnc = new CoalescentSimulator.TaxaConstraint(combinedTaxa, cnc.lower, cnc.upper, cnc.isMonophyletic);
constraints.set(nc, cnc);
}
}
}
}
// determine upper bound for each set.
double[] upper = new double[nConstraints];
for (int nc = nConstraints - 1; nc >= 0; --nc) {
final CoalescentSimulator.TaxaConstraint cnc = constraints.get(nc);
if (cnc.realLimits()) {
upper[nc] = cnc.upper;
} else {
upper[nc] = Double.POSITIVE_INFINITY;
}
}
for (int nc = nConstraints - 1; nc >= 0; --nc) {
final CoalescentSimulator.TaxaConstraint cnc = constraints.get(nc);
if (upper[nc] < Double.POSITIVE_INFINITY) {
for (int nc1 = nc - 1; nc1 >= 0; --nc1) {
final CoalescentSimulator.TaxaConstraint cnc1 = constraints.get(nc1);
if (simulator.contained(cnc1.taxons, cnc.taxons)) {
upper[nc1] = Math.min(upper[nc1], upper[nc]);
if (cnc1.realLimits() && cnc1.lower > upper[nc1]) {
throw new XMLParseException(setsNotCompatibleMessage);
}
break;
}
}
}
}
// collect subtrees here
List<Tree> st = new ArrayList<Tree>();
for (int nc = 0; nc < constraints.size(); ++nc) {
final CoalescentSimulator.TaxaConstraint nxt = constraints.get(nc);
// collect all previously built subtrees which are a subset of taxa set to be added
List<Tree> subs = new ArrayList<Tree>();
Taxa newTaxons = new Taxa(nxt.taxons);
for (int k = 0; k < st.size(); ++k) {
final Tree stk = st.get(k);
int x = simulator.sizeOfIntersection(stk, nxt.taxons);
if (x == st.get(k).getTaxonCount()) {
final Tree tree = st.remove(k);
--k;
subs.add(tree);
newTaxons.removeTaxa(tree);
}
}
SimpleTree tree = simulator.simulateTree(newTaxons, demoModel);
final double lower = nxt.realLimits() ? nxt.lower : 0;
if (upper[nc] < Double.MAX_VALUE) {
simulator.attemptToScaleTree(tree, (lower + upper[nc]) / 2);
}
if (subs.size() > 0) {
if (tree.getTaxonCount() > 0)
subs.add(tree);
double h = -1;
if (upper[nc] < Double.MAX_VALUE) {
for (Tree t : subs) {
// protect against 1 taxa tree with height 0
final double rh = t.getNodeHeight(t.getRoot());
h = Math.max(h, rh > 0 ? rh : (lower + upper[nc]) / 2);
}
h = (h + upper[nc]) / 2;
}
tree = simulator.simulateTree(subs.toArray(new Tree[subs.size()]), demoModel, h, true);
}
st.add(tree);
}
// add a taxon list for remaining taxa
if (taxa != null) {
final Taxa list = new Taxa();
for (int j = 0; j < taxa.getTaxonCount(); ++j) {
Taxon taxonj = taxa.getTaxon(j);
for (Tree aSt : st) {
if (aSt.getTaxonIndex(taxonj) >= 0) {
taxonj = null;
break;
}
}
if (taxonj != null) {
list.addTaxon(taxonj);
}
}
if (list.getTaxonCount() > 0) {
taxonLists.add(list);
}
}
if (st.size() > 1) {
final Tree t = simulator.simulateTree(st.toArray(new Tree[st.size()]), demoModel, -1, false);
subtrees.add(t);
} else {
subtrees.add(st.get(0));
}
}
}
}
if (taxonLists.size() == 0) {
if (subtrees.size() == 1) {
return subtrees.get(0);
}
throw new XMLParseException("Expected at least one taxonList or two subtrees in " + getParserName() + " element.");
}
try {
Tree[] trees = new Tree[taxonLists.size() + subtrees.size()];
// simulate each taxonList separately
for (int i = 0; i < taxonLists.size(); i++) {
trees[i] = simulator.simulateTree(taxonLists.get(i), demoModel);
}
// add the preset trees
for (int i = 0; i < subtrees.size(); i++) {
trees[i + taxonLists.size()] = subtrees.get(i);
}
return simulator.simulateTree(trees, demoModel, rootHeight, trees.length != 1);
} catch (IllegalArgumentException iae) {
throw new XMLParseException(iae.getMessage());
}
}
use of dr.evolution.util.TaxonList in project beast-mcmc by beast-dev.
the class TreeModelParser method parseXMLObject.
/**
* @return a tree object based on the XML element it was passed.
*/
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
Tree tree = (Tree) xo.getChild(Tree.class);
boolean fixHeights = xo.getAttribute(FIX_HEIGHTS, false);
boolean fixTree = xo.getAttribute(FIX_TREE, false);
DefaultTreeModel treeModel = new DefaultTreeModel(xo.getId(), tree, fixHeights, fixTree);
Logger.getLogger("dr.evomodel").info("\nCreating the tree model, '" + xo.getId() + "'");
for (int i = 0; i < xo.getChildCount(); i++) {
if (xo.getChild(i) instanceof XMLObject) {
XMLObject cxo = (XMLObject) xo.getChild(i);
if (cxo.getName().equals(ROOT_HEIGHT)) {
ParameterParser.replaceParameter(cxo, treeModel.getRootHeightParameter());
} else if (cxo.getName().equals(LEAF_HEIGHT)) {
String taxonName;
if (cxo.hasAttribute(TAXON)) {
taxonName = cxo.getStringAttribute(TAXON);
} else {
throw new XMLParseException("taxa element missing from leafHeight element in treeModel element");
}
int index = treeModel.getTaxonIndex(taxonName);
if (index == -1) {
throw new XMLParseException("taxon " + taxonName + " not found for leafHeight element in treeModel element");
}
NodeRef node = treeModel.getExternalNode(index);
Parameter newParameter = treeModel.getLeafHeightParameter(node);
ParameterParser.replaceParameter(cxo, newParameter);
Taxon taxon = treeModel.getTaxon(index);
setUncertaintyBounds(newParameter, taxon);
} else if (cxo.getName().equals(LEAF_HEIGHTS)) {
// get a set of leaf height parameters out as a compound parameter...
TaxonList taxa = (TaxonList) cxo.getChild(TaxonList.class);
Parameter offsetParameter = (Parameter) cxo.getChild(Parameter.class);
CompoundParameter leafHeights = new CompoundParameter("leafHeights");
for (Taxon taxon : taxa) {
int index = treeModel.getTaxonIndex(taxon);
if (index == -1) {
throw new XMLParseException("taxon " + taxon.getId() + " not found for leafHeight element in treeModel element");
}
NodeRef node = treeModel.getExternalNode(index);
Parameter newParameter = treeModel.getLeafHeightParameter(node);
leafHeights.addParameter(newParameter);
setUncertaintyBounds(newParameter, taxon);
}
ParameterParser.replaceParameter(cxo, leafHeights);
} else if (cxo.getName().equals(NODE_HEIGHTS)) {
boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
if (!rootNode && !internalNodes && !leafNodes) {
throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeHeights element");
}
ParameterParser.replaceParameter(cxo, treeModel.createNodeHeightsParameter(rootNode, internalNodes, leafNodes));
} else if (cxo.getName().equals(NODE_RATES)) {
boolean rootNode = cxo.getAttribute(ROOT_NODE, false);
boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false);
boolean leafNodes = cxo.getAttribute(LEAF_NODES, false);
double[] initialValues = null;
if (cxo.hasAttribute(INITIAL_VALUE)) {
initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE);
}
if (!rootNode && !internalNodes && !leafNodes) {
throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeRates element");
}
ParameterParser.replaceParameter(cxo, treeModel.createNodeRatesParameter(initialValues, rootNode, internalNodes, leafNodes));
} else if (cxo.getName().equals(NODE_TRAITS)) {
parseNodeTraits(cxo, treeModel);
} else if (cxo.getName().equals(LEAF_TRAIT)) {
String name = cxo.getAttribute(NAME, "trait");
String taxonName;
if (cxo.hasAttribute(TAXON)) {
taxonName = cxo.getStringAttribute(TAXON);
} else {
throw new XMLParseException("taxa element missing from leafTrait element in treeModel element");
}
int index = treeModel.getTaxonIndex(taxonName);
if (index == -1) {
throw new XMLParseException("taxon '" + taxonName + "' not found for leafTrait element in treeModel element");
}
NodeRef node = treeModel.getExternalNode(index);
Parameter parameter = treeModel.getNodeTraitParameter(node, name);
if (parameter == null)
throw new XMLParseException("trait '" + name + "' not found for leafTrait (taxon, " + taxonName + ") element in treeModel element");
ParameterParser.replaceParameter(cxo, parameter);
} else {
throw new XMLParseException("illegal child element in " + getParserName() + ": " + cxo.getName());
}
} else if (xo.getChild(i) instanceof Tree) {
// do nothing - already handled
} else {
throw new XMLParseException("illegal child element in " + getParserName() + ": " + xo.getChildName(i) + " " + xo.getChild(i));
}
}
// Logger.getLogger("dr.evomodel").info(" initial tree topology = " + TreeUtils.uniqueNewick(treeModel, treeModel.getRoot()));
Logger.getLogger("dr.evomodel").info(" taxon count = " + treeModel.getExternalNodeCount());
Logger.getLogger("dr.evomodel").info(" tree height = " + treeModel.getNodeHeight(treeModel.getRoot()));
return treeModel;
}
Aggregations