use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.
the class GaussianProcessSkytrackLikelihoodParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
XMLObject cxo = xo.getChild(POPULATION_PARAMETER);
Parameter popParameter = (Parameter) cxo.getChild(Parameter.class);
cxo = xo.getChild(PRECISION_PARAMETER);
Parameter precParameter = (Parameter) cxo.getChild(Parameter.class);
// cxo = xo.getChild(LAMBDA_BOUND_PARAMETER);
// Parameter lambda_bound = (Parameter) cxo.getChild(Parameter.class);
//
// cxo = xo.getChild(LAMBDA_PARAMETER);
// Parameter lambda_parameter = (Parameter) cxo.getChild(Parameter.class);
cxo = xo.getChild(POPULATION_TREE);
List<Tree> treeList = new ArrayList<Tree>();
for (int i = 0; i < cxo.getChildCount(); i++) {
Object testObject = cxo.getChild(i);
if (testObject instanceof Tree) {
treeList.add((TreeModel) testObject);
}
}
// TreeModel treeModel = (TreeModel) cxo.getChild(TreeModel.class);
// cxo = xo.getChild(GROUP_SIZES);
// Parameter groupParameter = null;
// if (cxo != null) {
// groupParameter = (Parameter) cxo.getChild(Parameter.class);
//
// if (popParameter.getDimension() != groupParameter.getDimension())
// throw new XMLParseException("Population and group size parameters must have the same length");
// }
Parameter lambda_parameter;
if (xo.getChild(LAMBDA_PARAMETER) != null) {
cxo = xo.getChild(LAMBDA_PARAMETER);
lambda_parameter = (Parameter) cxo.getChild(Parameter.class);
} else {
lambda_parameter = new Parameter.Default(1.0);
}
Parameter GPtype;
if (xo.getChild(GPTYPE) != null) {
cxo = xo.getChild(GPTYPE);
GPtype = (Parameter) cxo.getChild(Parameter.class);
} else {
GPtype = new Parameter.Default(1.0);
}
Parameter Tmrca;
if (xo.getChild(TMRCA) != null) {
cxo = xo.getChild(TMRCA);
Tmrca = (Parameter) cxo.getChild(Parameter.class);
} else {
Tmrca = new Parameter.Default(1.0);
}
Parameter numPoints;
if (xo.getChild(NUMBER_POINTS) != null) {
cxo = xo.getChild(NUMBER_POINTS);
numPoints = (Parameter) cxo.getChild(Parameter.class);
} else {
numPoints = new Parameter.Default(1.0);
}
Parameter CoalCounts;
if (xo.getChild(COALCOUNT) != null) {
cxo = xo.getChild(COALCOUNT);
CoalCounts = (Parameter) cxo.getChild(Parameter.class);
} else {
CoalCounts = new Parameter.Default(1.0);
}
Parameter GPcounts;
if (xo.getChild(GPCOUNTS) != null) {
cxo = xo.getChild(GPCOUNTS);
GPcounts = (Parameter) cxo.getChild(Parameter.class);
} else {
GPcounts = new Parameter.Default(1.0);
}
Parameter coalfactor;
if (xo.getChild(COALFACTOR) != null) {
cxo = xo.getChild(COALFACTOR);
coalfactor = (Parameter) cxo.getChild(Parameter.class);
} else {
coalfactor = new Parameter.Default(1.0);
}
Parameter lambda_bound;
if (xo.getChild(LAMBDA_BOUND_PARAMETER) != null) {
cxo = xo.getChild(LAMBDA_BOUND_PARAMETER);
lambda_bound = (Parameter) cxo.getChild(Parameter.class);
} else {
lambda_bound = new Parameter.Default(1.0);
}
Parameter alpha_parameter;
if (xo.getChild(ALPHA_PARAMETER) != null) {
cxo = xo.getChild(ALPHA_PARAMETER);
alpha_parameter = (Parameter) cxo.getChild(Parameter.class);
} else {
alpha_parameter = new Parameter.Default(0.001);
}
Parameter beta_parameter;
if (xo.getChild(BETA_PARAMETER) != null) {
cxo = xo.getChild(BETA_PARAMETER);
beta_parameter = (Parameter) cxo.getChild(Parameter.class);
} else {
beta_parameter = new Parameter.Default(0.001);
}
Parameter change_points;
if (xo.getChild(CHANGE_POINTS) != null) {
cxo = xo.getChild(CHANGE_POINTS);
change_points = (Parameter) cxo.getChild(Parameter.class);
} else {
change_points = new Parameter.Default(0, 1);
}
if (xo.getAttribute(RANDOMIZE_TREE, false)) {
for (Tree tree : treeList) {
if (tree instanceof TreeModel) {
GaussianProcessSkytrackLikelihood.checkTree((TreeModel) tree);
} else {
throw new XMLParseException("Can not randomize a fixed tree");
}
}
}
// XMLObject latentChild = xo.getChild(LATENT_PARAMETER);
// Parameter latentPoints = (Parameter) latentChild.getChild(Parameter.class);
boolean rescaleByRootHeight = xo.getAttribute(RESCALE_BY_ROOT_ISSUE, true);
if (treeList.size() == 1) {
return new GaussianProcessSkytrackLikelihood(treeList, precParameter, rescaleByRootHeight, lambda_bound, lambda_parameter, popParameter, alpha_parameter, beta_parameter, change_points, GPtype, GPcounts, coalfactor, CoalCounts, numPoints, Tmrca);
} else {
return new GaussianProcessMultilocusSkytrackLikelihood(treeList, precParameter, rescaleByRootHeight, lambda_bound, lambda_parameter, popParameter, alpha_parameter, beta_parameter, change_points, GPtype, GPcounts, coalfactor, CoalCounts, numPoints, Tmrca);
}
}
use of dr.evolution.tree.Tree 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.tree.Tree 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);
// 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);
}
}
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.");
}
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));
}
try {
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.tree.Tree in project beast-mcmc by beast-dev.
the class GMRFSkyrideLikelihoodParser method parseXMLObject.
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
XMLObject cxo = xo.getChild(POPULATION_PARAMETER);
Parameter popParameter = (Parameter) cxo.getChild(Parameter.class);
cxo = xo.getChild(PRECISION_PARAMETER);
Parameter precParameter = (Parameter) cxo.getChild(Parameter.class);
cxo = xo.getChild(POPULATION_TREE);
List<Tree> treeList = new ArrayList<Tree>();
for (int i = 0; i < cxo.getChildCount(); i++) {
Object testObject = cxo.getChild(i);
if (testObject instanceof Tree) {
treeList.add((TreeModel) testObject);
}
}
// TreeModel treeModel = (TreeModel) cxo.getChild(TreeModel.class);
cxo = xo.getChild(GROUP_SIZES);
Parameter groupParameter = null;
if (cxo != null) {
groupParameter = (Parameter) cxo.getChild(Parameter.class);
if (popParameter.getDimension() != groupParameter.getDimension())
throw new XMLParseException("Population and group size parameters must have the same length");
}
Parameter lambda;
if (xo.getChild(LAMBDA_PARAMETER) != null) {
cxo = xo.getChild(LAMBDA_PARAMETER);
lambda = (Parameter) cxo.getChild(Parameter.class);
} else {
lambda = new Parameter.Default(LAMBDA_PARAMETER, 1.0);
}
Parameter gridPoints = null;
if (xo.getChild(GRID_POINTS) != null) {
cxo = xo.getChild(GRID_POINTS);
gridPoints = (Parameter) cxo.getChild(Parameter.class);
}
Parameter numGridPoints = null;
if (xo.getChild(NUM_GRID_POINTS) != null) {
cxo = xo.getChild(NUM_GRID_POINTS);
numGridPoints = (Parameter) cxo.getChild(Parameter.class);
}
Parameter cutOff = null;
if (xo.getChild(CUT_OFF) != null) {
cxo = xo.getChild(CUT_OFF);
cutOff = (Parameter) cxo.getChild(Parameter.class);
}
Parameter phi = null;
if (xo.getChild(PHI_PARAMETER) != null) {
cxo = xo.getChild(PHI_PARAMETER);
phi = (Parameter) cxo.getChild(Parameter.class);
}
List<Parameter> lastObservedIndex = null;
if (xo.hasChildNamed(LAST_OBSERVED_INDEX)) {
lastObservedIndex = new ArrayList<Parameter>();
cxo = xo.getChild(LAST_OBSERVED_INDEX);
final int numObsInd = cxo.getChildCount();
for (int i = 0; i < numObsInd; ++i) {
lastObservedIndex.add((Parameter) cxo.getChild(i));
}
}
Parameter ploidyFactors = null;
if (xo.getChild(PLOIDY) != null) {
cxo = xo.getChild(PLOIDY);
ploidyFactors = (Parameter) cxo.getChild(Parameter.class);
} else {
ploidyFactors = new Parameter.Default(PLOIDY, treeList.size());
for (int i = 0; i < treeList.size(); i++) {
ploidyFactors.setParameterValue(i, 1.0);
}
}
Parameter betaParameter = null;
if (xo.hasChildNamed(SINGLE_BETA)) {
betaParameter = (Parameter) xo.getElementFirstChild(SINGLE_BETA);
}
List<Parameter> betaList = null;
if (xo.getChild(BETA_PARAMETER) != null) {
betaList = new ArrayList<Parameter>();
cxo = xo.getChild(BETA_PARAMETER);
final int numBeta = cxo.getChildCount();
for (int i = 0; i < numBeta; ++i) {
betaList.add((Parameter) cxo.getChild(i));
}
}
MatrixParameter dMatrix = null;
if (xo.getChild(COVARIATE_MATRIX) != null) {
cxo = xo.getChild(COVARIATE_MATRIX);
dMatrix = (MatrixParameter) cxo.getChild(MatrixParameter.class);
}
boolean timeAwareSmoothing = GMRFSkyrideLikelihood.TIME_AWARE_IS_ON_BY_DEFAULT;
if (xo.hasAttribute(TIME_AWARE_SMOOTHING)) {
timeAwareSmoothing = xo.getBooleanAttribute(TIME_AWARE_SMOOTHING);
}
if (dMatrix != null) {
if (dMatrix.getRowDimension() != popParameter.getDimension())
throw new XMLParseException("Design matrix row dimension must equal the population parameter length.");
if (dMatrix.getColumnDimension() != betaParameter.getDimension())
throw new XMLParseException("Design matrix column dimension must equal the regression coefficient length.");
}
List<Parameter> covPrecParam = null;
if (xo.hasChildNamed(COV_PREC_PARAM)) {
covPrecParam = new ArrayList<Parameter>();
cxo = xo.getChild(COV_PREC_PARAM);
final int numCovPrec = cxo.getChildCount();
for (int i = 0; i < numCovPrec; ++i) {
covPrecParam.add((Parameter) cxo.getChild(i));
}
}
List<MatrixParameter> covariates = null;
if (xo.hasChildNamed(COVARIATES)) {
covariates = new ArrayList<MatrixParameter>();
cxo = xo.getChild(COVARIATES);
final int numCov = cxo.getChildCount();
for (int i = 0; i < numCov; ++i) {
covariates.add((MatrixParameter) cxo.getChild(i));
}
}
if ((covariates != null && betaList == null) || (covariates == null && betaList != null))
throw new XMLParseException("Must specify both a set of regression coefficients and a design matrix.");
if (xo.getAttribute(RANDOMIZE_TREE, false)) {
for (Tree tree : treeList) {
if (tree instanceof TreeModel) {
GMRFSkyrideLikelihood.checkTree((TreeModel) tree);
} else {
throw new XMLParseException("Can not randomize a fixed tree");
}
}
}
boolean rescaleByRootHeight = xo.getAttribute(RESCALE_BY_ROOT_ISSUE, true);
Logger.getLogger("dr.evomodel").info("The " + SKYLINE_LIKELIHOOD + " has " + (timeAwareSmoothing ? "time aware smoothing" : "uniform smoothing"));
if (xo.getAttribute(OLD_SKYRIDE, true) && xo.getName().compareTo(SKYGRID_LIKELIHOOD) != 0) {
return new GMRFSkyrideLikelihood(treeList, popParameter, groupParameter, precParameter, lambda, betaParameter, dMatrix, timeAwareSmoothing, rescaleByRootHeight);
} else {
if (xo.getChild(GRID_POINTS) != null) {
return new GMRFMultilocusSkyrideLikelihood(treeList, popParameter, groupParameter, precParameter, lambda, betaParameter, dMatrix, timeAwareSmoothing, gridPoints, covariates, ploidyFactors, lastObservedIndex, covPrecParam, betaList);
} else {
return new GMRFMultilocusSkyrideLikelihood(treeList, popParameter, groupParameter, precParameter, lambda, betaParameter, dMatrix, timeAwareSmoothing, cutOff.getParameterValue(0), (int) numGridPoints.getParameterValue(0), phi, ploidyFactors);
}
}
}
use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.
the class DnDsLoggerParser method parseXMLObject.
@Override
public Object parseXMLObject(XMLObject xo) throws XMLParseException {
String[] names = DnDsLogger.traitNames;
TreeTrait[] foundTraits = new TreeTrait[names.length];
for (int i = 0; i < xo.getChildCount(); i++) {
Object obj = xo.getChild(i);
if (obj instanceof CodonPartitionedRobustCounting) {
CodonPartitionedRobustCounting thisCount = (CodonPartitionedRobustCounting) obj;
for (int j = 0; j < names.length; j++) {
TreeTrait trait = thisCount.getTreeTrait(names[j]);
if (trait != null) {
foundTraits[j] = trait;
}
}
}
}
for (int i = 0; i < foundTraits.length; i++) {
if (foundTraits[i] == null) {
throw new XMLParseException("Unable to find trait '" + names[i] + "'");
}
}
Tree tree = (Tree) xo.getChild(Tree.class);
// Use AttributeRules for options here
boolean useSmoothing = xo.getAttribute(USE_SMOOTHING, true);
boolean useDnMinusDs = xo.getAttribute(USE_DNMINUSDS, false);
boolean conditionalCounts = xo.getAttribute(COUNTS, false);
boolean synonymous = xo.getAttribute(SYNONYMOUS, false);
return new DnDsLogger(xo.getId(), tree, foundTraits, useSmoothing, useDnMinusDs, conditionalCounts, synonymous);
}
Aggregations