use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.
the class GaussianProcessMultilocusSkytrackLikelihood method makeTreeIntervalList.
// OK
private void makeTreeIntervalList(List<Tree> treeList, boolean add) {
if (intervalsList == null) {
intervalsList = new ArrayList<TreeIntervals>();
} else {
intervalsList.clear();
}
for (Tree tree : treeList) {
numIntervals += tree.getExternalNodeCount();
intervalsList.add(new TreeIntervals(tree));
if (add && tree instanceof TreeModel) {
addModel((TreeModel) tree);
}
}
}
use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.
the class GMRFFixedGridLikelihood method run.
public static void run() throws Exception {
NewickImporter importer = new NewickImporter("((((5:0.5,1:0.2):0.5,0:1):0.2,2:0.8):0.2,3:1.4)");
Tree tree = importer.importNextTree();
double[] data = new double[15];
double[] times = new double[15];
data[0] = 1.0;
times[0] = 0.05;
for (int i = 1; i < data.length; i++) {
data[i] = data[i - 1] + 0.5;
times[i] = times[i - 1] + 0.1;
}
GMRFFixedGridLikelihood like = new GMRFFixedGridLikelihood(tree, new Parameter.Default(data), new Parameter.Default(times), 4);
System.out.println(like.getLogLikelihood());
}
use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.
the class CodonPartitionedRobustCounting method setupTraits.
private void setupTraits() {
TreeTrait baseTrait = new TreeTrait.DA() {
public String getTraitName() {
return BASE_TRAIT_PREFIX + codonLabeling.getText();
}
public Intent getIntent() {
return Intent.BRANCH;
}
public double[] getTrait(Tree tree, NodeRef node) {
return getExpectedCountsForBranch(node);
}
public boolean getLoggable() {
return false;
}
};
if (saveCompleteHistory) {
TreeTrait stringTrait = new TreeTrait.SA() {
public String getTraitName() {
return COMPLETE_HISTORY_PREFIX + codonLabeling.getText();
}
public Intent getIntent() {
return Intent.BRANCH;
}
public boolean getFormatAsArray() {
return true;
}
public String[] getTrait(Tree tree, NodeRef node) {
// Lazy simulation of complete histories
double[] count = getExpectedCountsForBranch(node);
List<String> events = new ArrayList<String>();
for (int i = 0; i < numCodons; i++) {
String eventString = completeHistoryPerNode[node.getNumber()][i];
if (eventString != null) {
if (eventString.contains("},{")) {
// There are multiple events
String[] elements = eventString.split("(?<=\\}),(?=\\{)");
for (String e : elements) {
events.add(e);
}
} else {
events.add(eventString);
}
}
}
if (DEBUG) {
double sum = 0.0;
for (double d : count) {
if (d > 0.0) {
sum += 1;
}
}
System.err.println(events.size() + " " + sum);
if (Math.abs(events.size() - sum) > 0.5) {
System.err.println("Error");
for (int i = 0; i < count.length; ++i) {
if (count[i] != 0.0) {
System.err.println(i + ": " + count[i] + completeHistoryPerNode[node.getNumber()][i]);
}
}
System.err.println("Error");
int c = 0;
for (String s : events) {
c++;
System.err.println(c + ":" + s);
}
System.exit(-1);
}
}
String[] array = new String[events.size()];
events.toArray(array);
return array;
}
public boolean getLoggable() {
return true;
}
};
treeTraits.addTrait(stringTrait);
}
TreeTrait unconditionedSum;
if (!TRIAL) {
unconditionedSum = new TreeTrait.D() {
public String getTraitName() {
return UNCONDITIONED_PREFIX + codonLabeling.getText();
}
public Intent getIntent() {
return Intent.WHOLE_TREE;
}
public Double getTrait(Tree tree, NodeRef node) {
return getUnconditionedTraitValue();
}
public boolean getLoggable() {
return false;
}
};
} else {
unconditionedSum = new TreeTrait.DA() {
public String getTraitName() {
return UNCONDITIONED_PREFIX + codonLabeling.getText();
}
public Intent getIntent() {
return Intent.WHOLE_TREE;
}
public double[] getTrait(Tree tree, NodeRef node) {
return getUnconditionedTraitValues();
}
public boolean getLoggable() {
return false;
}
};
}
TreeTrait sumOverTreeTrait = new TreeTrait.SumOverTreeDA(SITE_SPECIFIC_PREFIX + codonLabeling.getText(), baseTrait, includeExternalBranches, includeInternalBranches) {
@Override
public boolean getLoggable() {
return false;
}
};
// This should be the default output in tree logs
TreeTrait sumOverSitesTrait = new TreeTrait.SumAcrossArrayD(codonLabeling.getText(), baseTrait) {
@Override
public boolean getLoggable() {
return true;
}
};
// This should be the default output in columns logs
String name = prefix != null ? prefix + TOTAL_PREFIX + codonLabeling.getText() : TOTAL_PREFIX + codonLabeling.getText();
TreeTrait sumOverSitesAndTreeTrait = new TreeTrait.SumOverTreeD(name, sumOverSitesTrait, includeExternalBranches, includeInternalBranches) {
@Override
public boolean getLoggable() {
return true;
}
};
treeTraitLogger = new TreeTraitLogger(tree, new TreeTrait[] { sumOverSitesAndTreeTrait });
treeTraits.addTrait(baseTrait);
treeTraits.addTrait(unconditionedSum);
treeTraits.addTrait(sumOverSitesTrait);
treeTraits.addTrait(sumOverTreeTrait);
treeTraits.addTrait(sumOverSitesAndTreeTrait);
if (doUnconditionedPerBranch) {
TreeTrait unconditionedBase = new TreeTrait.DA() {
public String getTraitName() {
return UNCONDITIONED_PER_BRANCH_PREFIX + codonLabeling.getText();
}
public Intent getIntent() {
return Intent.BRANCH;
}
public double[] getTrait(Tree tree, NodeRef node) {
return getUnconditionalCountsForBranch(node);
}
public boolean getLoggable() {
// TODO Should be switched to true to log unconditioned values per branch
return false;
}
};
TreeTrait sumUnconditionedOverSitesTrait = new TreeTrait.SumAcrossArrayD(UNCONDITIONED_PER_BRANCH_PREFIX + codonLabeling.getText(), unconditionedBase) {
@Override
public boolean getLoggable() {
return true;
}
};
String nameU = prefix != null ? prefix + UNCONDITIONED_TOTAL_PREFIX + codonLabeling.getText() : UNCONDITIONED_TOTAL_PREFIX + codonLabeling.getText();
TreeTrait sumUnconditionedOverSitesAndTreeTrait = new TreeTrait.SumOverTreeD(nameU, sumUnconditionedOverSitesTrait, includeExternalBranches, includeInternalBranches) {
public boolean getLoggable() {
return true;
}
};
treeTraitLogger = new TreeTraitLogger(tree, new TreeTrait[] { sumOverSitesAndTreeTrait, sumUnconditionedOverSitesAndTreeTrait });
treeTraits.addTrait(unconditionedBase);
treeTraits.addTrait(sumUnconditionedOverSitesTrait);
}
}
use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.
the class ConditionalCladeFrequency method report.
/**
* Creates the report. The estimated posterior of the given tree is printed.
*
* @throws IOException if general I/O error occurs
*/
public void report(Reader r) throws IOException, Importer.ImportException {
System.err.println("making report");
ArrayList<Tree> referenceTrees = new ArrayList<Tree>();
BufferedReader reader = new BufferedReader(r);
String line = reader.readLine();
if (line.toUpperCase().startsWith("#NEXUS")) {
NexusImporter importer = new NexusImporter(reader);
Tree[] trees = importer.importTrees(null);
for (Tree tree : trees) {
referenceTrees.add(tree);
SimpleTree sTree = new SimpleTree(tree);
System.out.println("Estimated marginal posterior by condiational clade frequencies:");
System.out.println(getTreeProbability(sTree) + "\t\t" + sTree);
}
} else {
throw new RuntimeException("Could not read reference tree. Only Nexus format is supported.");
}
System.out.flush();
}
use of dr.evolution.tree.Tree in project beast-mcmc by beast-dev.
the class ConditionalCladeFrequency method analyzeTrace.
/**
* Actually analyzes the trace given the burn-in. Each tree from the trace
* is read and the conditional clade frequencies incremented.
*
* @param verbose if true then progress is logged to stdout
*/
public void analyzeTrace(boolean verbose) {
if (verbose) {
if (traces.length > 1)
System.out.println("Combining " + traces.length + " traces.");
}
// get first tree to extract the taxon
Tree tree = getTree(0);
// read every tree from the trace
for (TreeTrace trace : traces) {
// do some output stuff
int treeCount = trace.getTreeCount(burnin * trace.getStepSize());
double stepSize = treeCount / 60.0;
int counter = 1;
if (verbose) {
System.out.println("Analyzing " + treeCount + " trees...");
System.out.println("0 25 50 75 100");
System.out.println("|--------------|--------------|--------------|--------------|");
System.out.print("*");
}
for (int i = 1; i < treeCount; i++) {
// get the next tree
tree = trace.getTree(i, burnin * trace.getStepSize());
// add the tree and its clades to the frequencies
addTree(tree);
// some more output stuff
if (i >= (int) Math.round(counter * stepSize) && counter <= 60) {
if (verbose) {
System.out.print("*");
System.out.flush();
}
counter += 1;
}
}
if (verbose) {
System.out.println("*");
}
}
}
Aggregations