use of dr.evolution.tree.FlexibleNode in project beast-mcmc by beast-dev.
the class TiterImporter method readExternalNode.
/**
* Reads an external node in.
*/
private FlexibleNode readExternalNode(TaxonList taxonList) throws IOException, ImportException {
FlexibleNode node = new FlexibleNode();
String label = readToken(":(),;");
Taxon taxon;
if (taxonList != null) {
// if a taxon list is given then the taxon must be in it...
int index = taxonList.getTaxonIndex(label);
if (index != -1) {
taxon = taxonList.getTaxon(index);
} else {
throw new UnknownTaxonException("Taxon in tree, '" + label + "' is unknown");
}
} else {
// No taxon list given so create new taxa
taxon = new Taxon(label);
}
node.setTaxon(taxon);
return node;
}
use of dr.evolution.tree.FlexibleNode in project beast-mcmc by beast-dev.
the class TiterImporter method readInternalNode.
/**
* Reads a node in. This could be a polytomy. Calls readBranch on each branch
* in the node.
*/
private FlexibleNode readInternalNode(TaxonList taxonList) throws IOException, ImportException {
FlexibleNode node = new FlexibleNode();
// read the opening '('
final char ch = readCharacter();
assert ch == '(';
// read the first child
node.addChild(readBranch(taxonList));
// an internal node must have at least 2 children
if (getLastDelimiter() != ',') {
throw new BadFormatException("Expecting ',' in tree, but got '" + (char) getLastDelimiter() + "'");
}
// read subsequent children
do {
node.addChild(readBranch(taxonList));
} while (getLastDelimiter() == ',');
// should have had a closing ')'
if (getLastDelimiter() != ')') {
throw new BadFormatException("Missing closing ')' in tree");
}
// If there is a label before the colon, store it:
try {
String label = readToken(",():;");
if (label.length() > 0) {
node.setAttribute("label", label);
}
} catch (IOException ioe) {
// probably an end of file without a terminal ';'
// we are going to allow this and return the nodes...
}
return node;
}
use of dr.evolution.tree.FlexibleNode in project beast-mcmc by beast-dev.
the class TiterImporter method importTrees.
/**
* importTrees.
*/
public Tree[] importTrees(TaxonList taxonList) throws IOException, ImportException {
boolean done = false;
ArrayList<FlexibleTree> array = new ArrayList<FlexibleTree>();
do {
try {
skipUntil("(");
unreadCharacter('(');
FlexibleNode root = readInternalNode(taxonList);
FlexibleTree tree = new FlexibleTree(root, false, true);
array.add(tree);
if (taxonList == null) {
taxonList = tree;
}
if (readCharacter() != ';') {
throw new BadFormatException("Expecting ';' after tree");
}
} catch (EOFException e) {
done = true;
}
} while (!done);
Tree[] trees = new Tree[array.size()];
array.toArray(trees);
return trees;
}
use of dr.evolution.tree.FlexibleNode in project beast-mcmc by beast-dev.
the class TiterImporter method readBranch.
/**
* Reads a branch in. This could be a node or a tip (calls readNode or readTip
* accordingly). It then reads the branch length and SimpleNode that will
* point at the new node or tip.
*/
private FlexibleNode readBranch(TaxonList taxonList) throws IOException, ImportException {
double length = 0.0;
FlexibleNode branch;
if (nextCharacter() == '(') {
// is an internal node
branch = readInternalNode(taxonList);
} else {
// is an external node
branch = readExternalNode(taxonList);
}
final String comment = getLastMetaComment();
if (comment != null) {
branch.setAttribute(COMMENT, comment);
clearLastMetaComment();
}
if (getLastDelimiter() == ':') {
length = readDouble(",():;");
}
branch.setLength(length);
return branch;
}
use of dr.evolution.tree.FlexibleNode in project beast-mcmc by beast-dev.
the class GetDateFromTree method getDate.
private static void getDate(String curD, int index, Tree treeOne) throws ImportException {
// many trees
double rootHeight;
DecimalFormat twoDForm = new DecimalFormat("####0.##");
Tree[] trees = new Tree[combiTrees];
double[][] tips = new double[combiTrees][treeOne.getExternalNodeCount() + 1];
double[] origins = new double[trees.length];
trees[0] = treeOne;
for (int t = 1; t < combiTrees; t++) {
trees[t] = getRandomTree();
if (trees[t] == null)
throw new ImportException("get null random tree");
// System.out.println(t + " => " + trees[t].toString());
}
for (int t = 0; t < trees.length; t++) {
System.out.println(t + " => " + trees[t]);
for (int i = 0; i < trees[t].getTaxonCount(); i++) {
FlexibleNode node = (FlexibleNode) trees[t].getExternalNode(i);
// System.out.println(node.getTaxon() + " has " + node.getHeight());
tips[t][Integer.parseInt(node.getTaxon().getId())] = node.getHeight();
}
rootHeight = ((FlexibleNode) trees[t].getRoot()).getHeight();
origins[t] = Double.valueOf(twoDForm.format(rootHeight + 100.0));
System.out.println("tree " + t + " root height = " + rootHeight + " origin = " + origins[t]);
System.out.println("\n");
}
if (index < 0) {
printXML(tips[0]);
} else {
try {
outputBDSSXML(curD, index, tips, origins, trees);
// outputExponetialXML(curD, index, tips, origins, trees);
} catch (IOException e) {
// To change body of catch statement use File | Settings | File Templates.
e.printStackTrace();
}
}
System.out.println("\n");
}
Aggregations