Debian Codegraph @

src/dr/app/checkpoint/DebugTreeImporter.java

raw symbols SCIP
1 /*
2 * DebugTreeImporter.java
3 *
4 * Copyright (c) 2002-2017 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.checkpoint;
27
28 import dr.evolution.io.Importer;
29 import dr.evolution.io.NewickImporter;
30 import dr.evolution.tree.Tree;
31
32 import java.io.*;
33
34 /**
35 * @author Guy Baele
36 */
37 public class DebugTreeImporter {
38
39 private static final boolean DEBUG = true;
40
41 //just trolling
42 public static boolean isInteger(String s) {
43 try {
44 Integer.parseInt(s);
45 } catch(NumberFormatException e) {
46 return false;
47 } catch(NullPointerException e) {
48 return false;
49 }
50 // only got here if we didn't return false
51 return true;
52 }
53
54 //args[0]: saved dump file
55 //args[1]: new XML file
56 //args[2]: new tree (Newick format)
57 //args[3]: output file
58 //e.g.: dump.trump.160 worobey-con-sc-resume-162-taxa.xml newtree.nwk restart.dump
59 public static void main (String[] args) {
60
61 if (args.length != 4) {
62 throw new RuntimeException("Incorrect number of arguments.");
63 }
64
65 try {
66 FileReader fileIn = new FileReader(args[2]);
67 BufferedReader in = new BufferedReader(fileIn);
68
69 String fullTree = in.readLine();
70
71 NewickImporter importer = new NewickImporter(fullTree);
72 Tree tree = importer.importNextTree();
73
74 //assume rootHeight in dumpfile is ALWAYS followed by all the nodeHeights
75 System.out.println("root height: " + tree.getNodeHeight(tree.getRoot()));
76
77 FileReader dumpFileIn = new FileReader(args[0]);
78 BufferedReader dumpIn = new BufferedReader(dumpFileIn);
79
80 FileWriter dumpFileOut = new FileWriter(args[3]);
81 BufferedWriter dumpOut = new BufferedWriter(dumpFileOut);
82
83 String original = dumpIn.readLine();
84 String[] fields = original.split("\t");
85
86 //write new rootHeight to new output dump file
87 while (!fields[0].equals("treeModel.rootHeight")) {
88 if (DEBUG) {
89 System.out.println(original);
90 }
91 dumpOut.write(original + "\n");
92
93 original = dumpIn.readLine();
94 fields = original.split("\t");
95 }
96
97 fields[2] = Double.toString(tree.getNodeHeight(tree.getRoot()));
98 for (int i = 0; i < fields.length; i++) {
99 dumpOut.write(fields[i] + "\t");
100 }
101 dumpOut.write("\n");
102
103 //write all new node heights to new output dump file
104 int nodeCount = tree.getNodeCount();
105 if (DEBUG) {
106 System.out.println(nodeCount + " nodes found in tree.");
107 }
108 for (int i = 0; i < (nodeCount-1); i++) {
109 if (DEBUG) {
110 System.out.println(tree.getNode(i).getNumber() + "\t" + tree.getNodeHeight(tree.getNode(i)));
111 }
112 dumpOut.write(tree.getNode(i).getNumber() + "\t1\t" + tree.getNodeHeight(tree.getNode(i)) + "\n");
113 }
114
115 //skip all the node heights in the original dump file
116 //no clue as to how many there are ...
117 //best I can tell only node heights have integers as parameter names
118 original = dumpIn.readLine();
119 if (DEBUG) {
120 System.out.println(original);
121 }
122 fields = original.split("\t");
123 while (isInteger(fields[0])) {
124 original = dumpIn.readLine();
125 fields = original.split("\t");
126 }
127
128 while (!fields[0].equals("treeModel")) {
129 dumpOut.write(original + "\n");
130 original = dumpIn.readLine();
131 if (DEBUG) {
132 System.out.println(original);
133 }
134 fields = original.split("\t");
135 }
136
137 dumpOut.write(fields[0] + "\t" + fullTree);
138
139 dumpOut.flush();
140 dumpOut.close();
141
142 } catch (FileNotFoundException fnfe) {
143 throw new RuntimeException("Tree file not found.");
144 } catch (IOException ioe) {
145 throw new RuntimeException("Unable to read file: " + ioe.getMessage());
146 } catch (Importer.ImportException ie) {
147 throw new RuntimeException("Unable to import tree: " + ie.getMessage());
148 }
149 }
150
151 }