use of dr.evolution.io.NewickImporter in project beast-mcmc by beast-dev.
the class TreeSpaceLogger method readTrees.
private void readTrees(String filename) {
try {
FileReader fr = new FileReader(new File(filename));
BufferedReader br = new BufferedReader(fr);
String line;
List<Tree> trees = new ArrayList<Tree>();
List<Integer> islands = new ArrayList<Integer>();
while ((line = br.readLine()) != null) {
// read trees
String[] tokens = line.split("\\s++");
NewickImporter importer = new NewickImporter(line);
Tree t = importer.importNextTree();
trees.add(t);
islands.add(Integer.parseInt(tokens[0]));
}
br.close();
fr.close();
this.trees = new String[trees.size()];
this.islands = new int[trees.size()];
int maxIsland = 0;
for (int i = 0; i < trees.size(); i++) {
Tree tree = trees.get(i);
String newick = TreeUtils.uniqueNewick(tree, tree.getRoot());
this.trees[i] = newick;
this.islands[i] = islands.get(i) - 1;
if (islands.get(i) > maxIsland) {
maxIsland = islands.get(i);
}
}
transitions = new int[trees.size()][trees.size()];
steps = new int[trees.size()][trees.size()];
islandTransitions = new int[maxIsland][maxIsland];
islandDwellTimes = new int[maxIsland];
// pathes = new HashMap[trees.size()][trees.size()];
// for (int i = 0; i < trees.size(); i++) {
// for (int j = 0; j < trees.size(); j++) {
// pathes[i][j] = new HashMap<String, Integer>();
// }
// }
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ImportException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
use of dr.evolution.io.NewickImporter in project beast-mcmc by beast-dev.
the class ExchangeOperatorTest method testWideExchangeOperator2.
public void testWideExchangeOperator2() throws IOException, ImportException {
// probability of picking (A,B) node is 1/(2n-2) = 1/8
// probability of swapping with D is 1/2
// total = 1/16
//probability of picking {D} node is 1/(2n-2) = 1/8
//probability of picking {A,B} is 1/5
// total = 1/40
//total = 1/16 + 1/40 = 0.0625 + 0.025 = 0.0875
// new test:
// probability of picking (A,B) node is 1/(2n-2) = 1/8
// probability of swapping with D is 1/(2n-3) = 1/7
// total = 1/56
//probability of picking {D} node is 1/(2n-2) = 1/8
//probability of picking {A,B} is 1/(2n-3) = 1/7
// total = 1/56
//total = 1/56 + 1/56 = 1/28
System.out.println("Test 1: Forward");
String treeMatch = "(((D,C),(A,B)),E);";
int count = 0;
int reps = 1000000;
for (int i = 0; i < reps; i++) {
TreeModel treeModel = new TreeModel("treeModel", tree5);
ExchangeOperator operator = new ExchangeOperator(ExchangeOperator.WIDE, treeModel, 1.0);
operator.doOperation();
String tree = TreeUtils.newickNoLengths(treeModel);
if (tree.equals(treeMatch)) {
count += 1;
}
}
double p_1 = (double) count / (double) reps;
System.out.println("Number of proposals:\t" + count);
System.out.println("Number of tries:\t" + reps);
System.out.println("Number of ratio:\t" + p_1);
System.out.println("Number of expected ratio:\t" + 1.0 / 28.0);
assertExpectation(1.0 / 28.0, p_1, reps);
// since this operator is supposed to be symmetric it got a hastings ratio of one
// this means, it should propose the same move just backwards with the same probability
// BUT:
// (((D:2.0,C:2.0):1.0,(A:1.0,B:1.0):2.0):1.0,E:4.0) -> ((((A,B),C),D),E)
// probability of picking (A,B) node is 1/(2n-2) = 1/8
// probability of swapping with D is 1/3
// total = 1/24
//probability of picking {D} node is 1/(2n-2) = 1/8
//probability of picking {A,B} is 1/4
// total = 1/32
//total = 1/24 + 1/32 = 7/96 = 0.07291666666
// new test:
// probability of picking (A,B) node is 1/(2n-2) = 1/8
// probability of swapping with D is 1/(2n-3) = 1/7
// total = 1/56
//probability of picking {D} node is 1/(2n-2) = 1/8
//probability of picking {A,B} is 1/(2n-3) = 1/7
// total = 1/56
//total = 1/56 + 1/56 = 1/28
System.out.println("Test 2: Backward");
treeMatch = "((((A,B),C),D),E);";
NewickImporter importer = new NewickImporter("(((D:2.0,C:2.0):1.0,(A:1.0,B:1.0):2.0):1.0,E:4.0);");
FlexibleTree tree5_2 = (FlexibleTree) importer.importTree(null);
count = 0;
for (int i = 0; i < reps; i++) {
TreeModel treeModel = new TreeModel("treeModel", tree5_2);
ExchangeOperator operator = new ExchangeOperator(ExchangeOperator.WIDE, treeModel, 1.0);
operator.doOperation();
String tree = TreeUtils.newickNoLengths(treeModel);
if (tree.equals(treeMatch)) {
count += 1;
}
}
double p_2 = (double) count / (double) reps;
System.out.println("Number of proposals:\t" + count);
System.out.println("Number of tries:\t" + reps);
System.out.println("Number of ratio:\t" + p_2);
System.out.println("Number of expected ratio:\t" + 1.0 / 28.0);
assertExpectation(1.0 / 28.0, p_2, reps);
}
use of dr.evolution.io.NewickImporter in project beast-mcmc by beast-dev.
the class RLYModelTest method setUp.
public void setUp() throws Exception {
super.setUp();
MathUtils.setSeed(666);
NewickImporter importer = new NewickImporter("(((((A:1.0,B:1.0):1.0,C:2.0),D:3.0):1.0, E:4.0),F:5.0);");
tree = (FlexibleTree) importer.importTree(null);
}
use of dr.evolution.io.NewickImporter in project beast-mcmc by beast-dev.
the class YuleLikelihoodTest method setUp.
public void setUp() throws Exception {
super.setUp();
NewickImporter importer = new NewickImporter("((1:1.0,2:1.0):1.0,(3:1.0,4:1.0):1.0);");
tree = (FlexibleTree) importer.importTree(null);
}
use of dr.evolution.io.NewickImporter in project beast-mcmc by beast-dev.
the class YuleModelTest method setUp.
public void setUp() throws Exception {
super.setUp();
MathUtils.setSeed(666);
NewickImporter importer = new NewickImporter("((1:1.0,2:1.0):1.0,(3:1.0,4:1.0):1.0);");
tree = (FlexibleTree) importer.importTree(null);
}
Aggregations