use of beast.util.TreeParser in project beast2 by CompEvol.
the class BEASTTestCase method getTree.
public static Tree getTree(Alignment data, String tree) throws Exception {
TreeParser t = new TreeParser();
t.initByName("taxa", data, "newick", tree, "IsLabelledNewick", true);
return t;
}
use of beast.util.TreeParser in project beast2 by CompEvol.
the class ExchangeOperatorTest method testNarrowExchange.
void testNarrowExchange(String sourceTree, String targetTree, int runs, Alignment data) throws Exception {
// first test going from source to target
double match = 0;
for (int i = 0; i < runs; i++) {
TreeParser tree = new TreeParser();
tree.initByName("taxa", data, "newick", sourceTree, "IsLabelledNewick", true);
State state = new State();
state.initByName("stateNode", tree);
state.initialise();
Exchange operator = new Exchange();
operator.initByName("isNarrow", true, "tree", tree, "weight", 1.0);
double logHR = operator.proposal();
String treeString = tree.getRoot().toNewick();
if (treeString.equals(targetTree) && !Double.isInfinite(logHR)) {
// proportion of accepts equals min(HR, 1.0)
match += Math.min(Math.exp(logHR), 1.0);
}
}
System.out.println(" Matches: " + match * 100.0 / runs + "%");
// now test going from target to source
double match2 = 0;
for (int i = 0; i < runs; i++) {
TreeParser tree = new TreeParser();
tree.initByName("taxa", data, "newick", targetTree, "IsLabelledNewick", true);
State state = new State();
state.initByName("stateNode", tree);
state.initialise();
Exchange operator = new Exchange();
operator.initByName("isNarrow", true, "tree", tree, "weight", 1.0);
double logHR = operator.proposal();
String treeString = tree.getRoot().toNewick();
if (treeString.equals(sourceTree) && !Double.isInfinite(logHR)) {
// proportion of accepts equals min(HR, 1.0)
match2 += Math.min(Math.exp(logHR), 1.0);
}
}
System.out.println(" Matches: " + match2 * 100.0 / runs + "%");
assertTrue("difference(" + 100 * (match - match2) / runs + ") exceeds 1.0%", 100.0 * Math.abs(match - match2) / runs < 1.0);
}
use of beast.util.TreeParser in project beast2 by CompEvol.
the class ScaleOperatorTest method testTreeScaling.
@Test
public void testTreeScaling() {
String newick = "((0:1.0,1:1.0)4:1.0,(2:1.0,3:1.0)5:0.5)6:0.0;";
TreeParser tree = new TreeParser(newick, false, false, false, 0);
Node[] node = tree.getNodesAsArray();
ScaleOperator operator = new ScaleOperator();
operator.initByName("tree", tree, "weight", 1.0);
operator.proposal();
// leaf node
node = tree.getNodesAsArray();
assertEquals(0.0, node[0].getHeight(), EPSILON);
assertEquals(0.0, node[1].getHeight(), EPSILON);
// leaf node, not scaled
assertEquals(0.5, node[2].getHeight(), EPSILON);
assertEquals(0.5, node[3].getHeight(), EPSILON);
// internal nodes, all scaled
// first determine scale factor
double scale = node[4].getHeight() / 1.0;
assertEquals(1.0 * scale, node[4].getHeight(), EPSILON);
assertEquals(1.5 * scale, node[5].getHeight(), EPSILON);
assertEquals(2.0 * scale, node[6].getHeight(), EPSILON);
}
use of beast.util.TreeParser in project beast2 by CompEvol.
the class TreeAnnotatorTest method setUp.
@Before
public void setUp() throws Exception {
final String[] treesString = new String[] { "((A:1,B:1):1,(C:1,D:1):1);", "(((A:1,B:1):1,C:2):2,D:3);", "((A:2,(B:1,C:1):1):2,D:3);" };
final String[] treesSAString = new String[] { "((((0:0.5,1:1.0):1.0,2:2.0):1.0,3:0.0):2.0,4:4.0);", "((((0:1.0,2:1.5):1.0,1:2.5):0.5,3:0.0):2.0,4:4.0);", "(((0:0.5,2:1.0):1.0,1:2.0):3.0,(3:0.2,4:2.2):1.8);", "((((0:1.0,2:1.5):1.0,1:2.5):0.2,4:1.7):0.3,3:0.0):0.0;" };
treeAnnotator = new TreeAnnotator();
treeAnnotatorSA = new TreeAnnotator();
trees = new Tree[treesString.length];
for (int i = 0; i < trees.length; i++) {
trees[i] = new TreeParser(treesString[i], false, false, true, 1);
}
treesSA = new Tree[treesSAString.length];
for (int i = 0; i < treesSA.length; i++) {
treesSA[i] = new TreeParser(treesSAString[i], false, false, false, 0);
}
cladeSystem = new CladeSystem();
cladeSystemSA = new CladeSystem();
}
use of beast.util.TreeParser in project beast2 by CompEvol.
the class TreeAnnotatorTest method testNewickTargetTree.
@Test
public void testNewickTargetTree() throws Exception {
// create target tree file in temp folder
TreeParser tree = new TreeParser("((A,B),(C,D))");
String tmpFolder = org.fest.util.Files.temporaryFolder().toString();
File target = new File(tmpFolder + "/target.tree");
PrintStream outfile = new PrintStream(target);
tree.init(outfile);
outfile.println();
tree.log(0, outfile);
outfile.println();
tree.close(outfile);
outfile.close();
// create input tree set
File source = new File(tmpFolder + "/source.trees");
outfile = new PrintStream(source);
for (String treeString : new String[] { "((A:1,B:1):1,(C:1,D:1):1);", "(((A:1,B:1):1,C:2):2,D:3);", "((A:2,(B:1,C:1):1):2,D:3);" }) {
outfile.println(treeString);
}
;
outfile.close();
File summary = new File(tmpFolder + "/summary.tree");
// run tree annotator
String[] args = new String[] { "-target", target.getPath(), source.getPath(), summary.getPath() };
TreeAnnotator.main(args);
// make sure we get output
String summaryString = BeautiDoc.load(summary);
System.out.println(summaryString);
}
Aggregations