Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve two operators, and provide a starting tree generator #19

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
267 changes: 267 additions & 0 deletions examples/bears-not-all-leaves.xml

Large diffs are not rendered by default.

59 changes: 48 additions & 11 deletions src/beast/evolution/operators/LeafToSampledAncestorJump.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package beast.evolution.operators;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import beast.core.Description;
import beast.core.Input;
import beast.core.parameter.IntegerParameter;
import beast.core.parameter.RealParameter;
import beast.evolution.alignment.Taxon;
import beast.evolution.tree.Node;
import beast.evolution.tree.Tree;
import beast.util.Randomizer;
Expand All @@ -24,15 +29,46 @@ public class LeafToSampledAncestorJump extends TreeOperator {

public Input<RealParameter> rInput =
new Input<RealParameter>("removalProbability", "The probability of an individual to be removed from the process immediately after the sampling");

public Input<List<Taxon>> sampledTaxa =
new Input<List<Taxon>>(
"sampledTaxa",
"Taxa that this operator should be allowed to let jump between sampled ancestor and leaf. Default: All non-recent leaves.",
new ArrayList<>());

protected List<Integer> validLeaves = new ArrayList<Integer>();

@Override
public void initAndValidate() {
if (sampledTaxa.get().size() == 0) {
validLeaves = new ArrayList<Integer>(treeInput.get().getLeafNodeCount());
for (Node leaf: treeInput.get().getExternalNodes()) {
if (leaf.getHeight() > 1e-6) {
validLeaves.add(leaf.getNr());
}
}
} else {
List<Taxon> taxa = sampledTaxa.get();
List<String> taxaNames = new ArrayList<String>(taxa.size());
for (Taxon taxon: taxa) {
taxaNames.add(taxon.getID());
}
validLeaves = new ArrayList<Integer>(taxa.size());
Integer i = 0;
for (String leaf: treeInput.get().getTaxaNames()) {
if (taxaNames.contains(leaf)) {
validLeaves.add(i);
}
i += 1;
}
}
// System.out.println("Nodes to be jumped:");
// System.out.println(Arrays.toString(validLeaves.toArray()));
}

@Override
public double proposal() {

double newHeight, newRange, oldRange;
double newHeight, logNewRange, logOldRange;
int categoryCount = 1;
if (categoriesInput.get() != null) {

Expand All @@ -41,20 +77,21 @@ public double proposal() {

Tree tree = treeInput.get();

int leafNodeCount = tree.getLeafNodeCount();
int leafNodeCount = validLeaves.size();

Node leaf = tree.getNode(Randomizer.nextInt(leafNodeCount));
Node leaf = tree.getNode(validLeaves.get(Randomizer.nextInt(leafNodeCount)));
Node parent = leaf.getParent();

if (leaf.isDirectAncestor()) {
oldRange = (double) 1;
logOldRange = (double) 0;
if (parent.isRoot()) {
final double randomNumber = Randomizer.nextExponential(1);
newHeight = parent.getHeight() + randomNumber;
newRange = Math.exp(randomNumber);
logNewRange = randomNumber;
} else {
newRange = parent.getParent().getHeight() - parent.getHeight();
double newRange = parent.getParent().getHeight() - parent.getHeight();
newHeight = parent.getHeight() + Randomizer.nextDouble() * newRange;
logNewRange = Math.log(newRange);
}

if (categoriesInput.get() != null) {
Expand All @@ -63,15 +100,15 @@ public double proposal() {
categoriesInput.get().setValue(index, newValue);
}
} else {
newRange = (double) 1;
logNewRange = (double) 0;
//make sure that the branch where a new sampled node to appear is not above that sampled node
if (getOtherChild(parent, leaf).getHeight() >= leaf.getHeight()) {
return Double.NEGATIVE_INFINITY;
}
if (parent.isRoot()) {
oldRange = Math.exp(parent.getHeight() - leaf.getHeight());
logOldRange = parent.getHeight() - leaf.getHeight();
} else {
oldRange = parent.getParent().getHeight() - leaf.getHeight();
logOldRange = Math.log(parent.getParent().getHeight() - leaf.getHeight());
}
newHeight = leaf.getHeight();
if (categoriesInput.get() != null) {
Expand All @@ -86,6 +123,6 @@ public double proposal() {
return Double.NEGATIVE_INFINITY;
}

return Math.log(newRange/oldRange);
return logNewRange - logOldRange;
}
}
Loading