use of beast.evolution.tree.Tree in project Babel by rbouckaert.
the class TreeTransitionMarker method run.
@Override
public void run() throws Exception {
tag = tagInput.get();
from = fromInput.get();
to = toInput.get();
markAll = markAllInput.get();
PrintStream out = System.out;
if (outputInput.get() != null) {
out = new PrintStream(outputInput.get());
Log.warning("Writing to file " + outputInput.get().getPath());
}
FastTreeSet trees = new TreeAnnotator().new FastTreeSet(treesInput.get().getAbsolutePath(), 0);
trees.reset();
Tree tree = trees.next();
tree.init(out);
out.println();
trees.reset();
int i = 0;
while (trees.hasNext()) {
tree = trees.next();
out.println();
out.print("tree STATE_" + i + " = ");
mark(tree.getRoot());
final String newick = tree.getRoot().toSortedNewick(new int[1], true);
out.print(newick);
out.print(";");
i++;
}
out.println();
out.println("end;");
}
use of beast.evolution.tree.Tree in project Babel by rbouckaert.
the class BinnedMeanTree method calcFrechetMean.
private Tree calcFrechetMean(Tree[] trees, int start, int end) {
if (trees.length == 0) {
throw new IllegalArgumentException("expected at least one tree");
}
Tree tree = trees[start];
RNNIMetric metric = new RNNIMetric();
boolean progress = true;
int i = start;
// while (progress && i < trees.length) {
while (i < end) {
double d = metric.distance(tree, trees[i]);
progress = ((int) d / (i + 1)) > 0;
if (progress) {
tree = metric.pathelement(tree, trees[i], (int) d / (i + 1));
}
i++;
}
return tree;
}
use of beast.evolution.tree.Tree in project Babel by rbouckaert.
the class FrechetMeanCentroid method run.
@Override
public void run() throws Exception {
long start = System.currentTimeMillis();
mode = modeInput.get();
Tree[] trees = getTrees(treeFileInput.get(), burnInPercentageInput.get());
// if focal tree file is specified, print sum of square RNNI distances and quite
if (focalTreeFileInput.get() != null && !focalTreeFileInput.get().getName().equals("[[none]]")) {
Tree[] focalTrees = getTrees(focalTreeFileInput.get(), 0);
for (Tree tree : focalTrees) {
double sos = sumOfSquaredDistances(trees, tree);
Log.info(tree.getRoot().toNewick());
Log.info("SoS = " + sos);
}
Log.warning("Done");
return;
}
Tree centroid = calcCentroid(trees);
if (randomizationAttemptsInput.get() > 1) {
double bestSoS = sumOfSquaredDistances(trees, centroid);
int onepercent = 1 + randomizationAttemptsInput.get() / 100;
int tenpercent = 1 + randomizationAttemptsInput.get() / 10;
for (int i = 1; i < randomizationAttemptsInput.get(); i++) {
randomise(trees);
Tree newCentroid = calcCentroid(trees);
double sos = sumOfSquaredDistances(trees, newCentroid);
if (sos < bestSoS) {
bestSoS = sos;
centroid = newCentroid;
Log.warning(i + ": " + sos);
}
if (i % onepercent == 0) {
if (i % tenpercent == 0) {
System.err.print("|");
} else {
System.err.print(".");
}
}
}
}
Log.info(centroid.getRoot().toNewick());
Log.warning("SoS = " + sumOfSquaredDistances(trees, centroid));
PrintStream out = System.out;
if (outputInput.get() != null && !outputInput.get().getName().equals("[[none]]")) {
Log.warning("Writing to file " + outputInput.get().getPath());
out = new PrintStream(outputInput.get());
}
centroid.init(out);
out.println();
centroid.log(0l, out);
out.println();
centroid.close(out);
long end = System.currentTimeMillis();
Log.warning("Done " + mode + " in " + (end - start) / 1000 + " seconds");
}
use of beast.evolution.tree.Tree in project Babel by rbouckaert.
the class FrechetMeanCentroid method getTrees.
private Tree[] getTrees(TreeFile treeFile, int burnInPercentage) {
try {
FastTreeSet srcTreeSet = new TreeAnnotator().new FastTreeSet(treeFile.getAbsolutePath(), burnInPercentage);
srcTreeSet.reset();
int n = 0;
while (srcTreeSet.hasNext()) {
srcTreeSet.next();
n++;
}
Tree[] trees = new Tree[n];
srcTreeSet.reset();
n = 0;
while (srcTreeSet.hasNext()) {
trees[n] = srcTreeSet.next();
n++;
}
return trees;
} catch (IOException e) {
e.printStackTrace();
throw new IllegalArgumentException(e.getMessage());
}
}
use of beast.evolution.tree.Tree in project Babel by rbouckaert.
the class FrechetMeanTree method initAndValidate.
@Override
public void initAndValidate() {
super.initAndValidate();
if (treeFileInput.get() != null && !treeFileInput.get().getName().equals("[[none]]")) {
try {
FastTreeSet srcTreeSet = new TreeAnnotator().new FastTreeSet(treeFileInput.get().getAbsolutePath(), burnInPercentageInput.get());
srcTreeSet.reset();
int n = 0;
while (srcTreeSet.hasNext()) {
srcTreeSet.next();
n++;
}
Tree[] trees = new Tree[n];
srcTreeSet.reset();
n = 0;
while (srcTreeSet.hasNext()) {
trees[n] = srcTreeSet.next();
n++;
}
calcFrechetMean(trees);
} catch (IOException e) {
e.printStackTrace();
throw new IllegalArgumentException(e.getMessage());
}
}
}
Aggregations