use of beast.evolution.tree.Tree in project beast2 by CompEvol.
the class StarBeastStartState method getInitialisedStateNodes.
@Override
public void getInitialisedStateNodes(final List<StateNode> stateNodes) {
if (hasCalibrations) {
stateNodes.add((Tree) calibratedYule.get().treeInput.get());
} else {
stateNodes.add(speciesTreeInput.get());
}
for (final Tree g : genes.get()) {
stateNodes.add(g);
}
final RealParameter popm = popMean.get();
if (popm != null) {
stateNodes.add(popm);
}
final RealParameter brate = birthRate.get();
if (brate != null) {
stateNodes.add(brate);
}
final SpeciesTreePrior speciesTreePrior = speciesTreePriorInput.get();
if (speciesTreePrior != null) {
final RealParameter popb = speciesTreePrior.popSizesBottomInput.get();
if (popb != null) {
stateNodes.add(popb);
}
final RealParameter popt = speciesTreePrior.popSizesTopInput.get();
if (popt != null) {
stateNodes.add(popt);
}
}
}
use of beast.evolution.tree.Tree in project beast2 by CompEvol.
the class StarBeastStartState method initWithCalibrations.
private void initWithCalibrations() throws MathException {
final CalibratedYuleModel cYule = calibratedYule.get();
final Tree spTree = (Tree) cYule.treeInput.get();
final List<CalibrationPoint> cals = cYule.calibrationsInput.get();
final CalibratedYuleModel cym = new CalibratedYuleModel();
cym.getOutputs().addAll(cYule.getOutputs());
for (final CalibrationPoint cal : cals) {
cym.setInputValue("calibrations", cal);
}
cym.setInputValue("tree", spTree);
cym.setInputValue("type", CalibratedYuleModel.Type.NONE);
cym.initAndValidate();
final Tree t = cym.compatibleInitialTree();
assert spTree.getLeafNodeCount() == t.getLeafNodeCount();
spTree.assignFromWithoutID(t);
// final CalibratedYuleInitialTree ct = new CalibratedYuleInitialTree();
// ct.initByName("initial", spTree, "calibrations", cYule.calibrationsInput.get());
// ct.initStateNodes();
final double rootHeight = spTree.getRoot().getHeight();
randomInitGeneTrees(rootHeight);
cYule.initAndValidate();
}
use of beast.evolution.tree.Tree in project beast2 by CompEvol.
the class StarBeastStartState method initWithMRCACalibrations.
private void initWithMRCACalibrations(List<MRCAPrior> calibrations) {
final Tree spTree = speciesTreeInput.get();
final RandomTree rnd = new RandomTree();
rnd.setInputValue("taxonset", spTree.getTaxonset());
for (final MRCAPrior cal : calibrations) {
rnd.setInputValue("constraint", cal);
}
ConstantPopulation pf = new ConstantPopulation();
pf.setInputValue("popSize", new RealParameter("1.0"));
rnd.setInputValue("populationModel", pf);
rnd.initAndValidate();
spTree.assignFromWithoutID((Tree) rnd);
final double rootHeight = spTree.getRoot().getHeight();
randomInitGeneTrees(rootHeight);
}
use of beast.evolution.tree.Tree in project beast2 by CompEvol.
the class YuleModel method sample.
/**
* Sampling only implemented for no-origin case currently.
*/
@Override
public void sample(State state, Random random) {
if (sampledFlag)
return;
sampledFlag = true;
// Cause conditional parameters to be sampled
sampleConditions(state, random);
Tree tree = (Tree) treeInput.get();
RealParameter birthRate = birthDiffRateParameterInput.get();
// Simulate tree conditional on new parameters
List<Node> activeLineages = new ArrayList<>();
for (Node oldLeaf : tree.getExternalNodes()) {
Node newLeaf = new Node(oldLeaf.getID());
newLeaf.setNr(oldLeaf.getNr());
newLeaf.setHeight(0.0);
activeLineages.add(newLeaf);
}
int nextNr = activeLineages.size();
double t = 0.0;
while (activeLineages.size() > 1) {
int k = activeLineages.size();
double a = birthRate.getValue() * k;
t += -Math.log(random.nextDouble()) / a;
Node node1 = activeLineages.get(random.nextInt(k));
Node node2;
do {
node2 = activeLineages.get(random.nextInt(k));
} while (node2.equals(node1));
Node newParent = new Node();
newParent.setNr(nextNr++);
newParent.setHeight(t);
newParent.addChild(node1);
newParent.addChild(node2);
activeLineages.remove(node1);
activeLineages.remove(node2);
activeLineages.add(newParent);
}
tree.assignFromWithoutID(new Tree(activeLineages.get(0)));
}
use of beast.evolution.tree.Tree in project beast2 by CompEvol.
the class NodeReheight method calcMaxHeight.
/**
* calculate maximum height that node nodeIndex can become restricted
* by nodes on the left and right
*/
private double calcMaxHeight(final int[] reverseOrder, final int nodeIndex) {
// find maximum height between two species. Only upper right part is populated
final double[][] maxHeight = new double[nrOfSpecies][nrOfSpecies];
for (int i = 0; i < nrOfSpecies; i++) {
Arrays.fill(maxHeight[i], Double.POSITIVE_INFINITY);
}
// calculate for every species tree the maximum allowable merge point
for (int i = 0; i < nrOfGeneTrees; i++) {
final Tree tree = geneTreesInput.get().get(i);
findMaximaInGeneTree(tree.getRoot(), new boolean[nrOfSpecies], m_taxonMap.get(i), maxHeight);
}
// find species on the left of selected node
final boolean[] isLowerSpecies = new boolean[nrOfSpecies];
final Node[] nodes = treeInput.get().getNodesAsArray();
for (int i = 0; i < nodeIndex; i++) {
final Node node = nodes[reverseOrder[i]];
if (node.isLeaf()) {
isLowerSpecies[node.getNr()] = true;
}
}
// find species on the right of selected node
final boolean[] isUpperSpecies = new boolean[nrOfSpecies];
for (int i = nodeIndex + 1; i < nodes.length; i++) {
final Node node = nodes[reverseOrder[i]];
if (node.isLeaf()) {
isUpperSpecies[node.getNr()] = true;
}
}
// find max
double max = Double.POSITIVE_INFINITY;
for (int i = 0; i < nrOfSpecies; i++) {
if (isLowerSpecies[i]) {
for (int j = 0; j < nrOfSpecies; j++) {
if (j != i && isUpperSpecies[j]) {
final int x = Math.min(i, j);
final int y = Math.max(i, j);
max = Math.min(max, maxHeight[x][y]);
}
}
}
}
return max;
}
Aggregations