Debian Codegraph @

src/dr/app/tools/NodeStateAnalyser.java

raw symbols SCIP
1 /*
2 * NodeStateAnalyser.java
3 *
4 * Copyright (c) 2002-2015 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.tools;
27
28 import dr.app.beast.BeastVersion;
29 import dr.app.util.Arguments;
30 import dr.app.util.Utils;
31 import dr.util.Version;
32 import dr.evolution.io.TreeImporter;
33 import dr.evolution.io.NexusImporter;
34 import dr.evolution.io.Importer;
35 import dr.evolution.tree.Tree;
36 import dr.evolution.tree.NodeRef;
37
38 import java.io.*;
39 import java.util.*;
40
41
42 public class NodeStateAnalyser {
43
44 private final static Version version = new BeastVersion();
45
46
47 public NodeStateAnalyser(int burnin, double mrsd, double scale, String inputFileName, String outputFileName) throws IOException {
48
49 File parentFile = new File(inputFileName);
50
51 if (parentFile.isDirectory()) {
52 System.out.println("Analysing all trees files below directory: " + inputFileName);
53 } else if (parentFile.isFile()) {
54 System.out.println("Analysing tree file: " + inputFileName);
55 } else {
56 System.err.println(inputFileName + " does not exist!");
57 System.exit(0);
58 }
59
60 if (outputFileName != null) {
61 FileOutputStream outputStream = new FileOutputStream(outputFileName);
62 System.setOut(new PrintStream(outputStream));
63 }
64
65 analyze(parentFile, burnin, mrsd, scale);
66 }
67
68 /**
69 * Recursively analyzes log files.
70 *
71 * @param file the file to analyze (if this is a directory then the files within it are analyzed)
72 * @param burnin the burnin to use
73 * @throws dr.inference.trace.TraceException
74 * if the trace file is in the wrong format or corrupted
75 */
76 private void analyze(File file, int burnin, double mrsd, double scale) {
77
78 if (file.isFile()) {
79 try {
80
81 String name = file.getCanonicalPath();
82
83 report(name, burnin, mrsd, scale);
84 //TraceAnalysis.report(name, burnin, marginalLikelihood);
85
86 } catch (IOException e) {
87 //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
88 }
89 } else {
90 File[] files = file.listFiles();
91 for (File f : files) {
92 if (f.isDirectory()) {
93 analyze(f, burnin, mrsd, scale);
94 } else if (f.getName().endsWith(".trees")) {
95 analyze(f, burnin, mrsd, scale);
96 }
97 }
98 }
99 }
100
101
102 /**
103 * Recursively analyzes trees files.
104 *
105 * @param name the file to analyze (if this is a directory then the files within it are analyzed)
106 * @param burnin the burnin to use
107 */
108 private void report(String name, int burnin, double mrsd, double scale) {
109 int count = 0;
110
111 Map<String, List<Double>> heightMap = new HashMap<String, List<Double>>();
112 Set<String> states = new TreeSet<String>();
113
114 try {
115 FileReader fileReader = new FileReader(new File(name));
116 TreeImporter importer = new NexusImporter(fileReader);
117 while (importer.hasTree()) {
118 Tree tree = importer.importNextTree();
119 if(count>=burnin) {
120 for (int i = 0; i < tree.getInternalNodeCount(); i++) {
121 NodeRef node = tree.getInternalNode(i);
122 Object value = tree.getNodeAttribute(node, "state");
123 if (value != null) {
124 String state = value.toString();
125 List<Double> heights = heightMap.get(state);
126 if (heights == null) {
127 heights = new ArrayList<Double>();
128 heightMap.put(state, heights);
129 states.add(state);
130 }
131 double h = tree.getNodeHeight(node) * scale;
132 if (Double.isNaN(mrsd)) {
133 heights.add(h);
134 } else {
135 heights.add(mrsd - h);
136 }
137 } else {
138 System.out.println("Node missing state");
139 }
140 }
141
142 }
143 count++;
144 }
145
146 boolean first = true;
147 int maxCount = 0;
148 for (String state : states) {
149 if (first) {
150 first = false;
151 } else {
152 System.out.print("\t");
153 }
154 System.out.print(state);
155
156 List<Double> heights = heightMap.get(state);
157 if (heights.size() > maxCount) {
158 maxCount = heights.size();
159 }
160 }
161 System.out.println();
162
163 for (int i = 0; i < maxCount; i++) {
164 first = true;
165 for (String state : states) {
166 if (first) {
167 first = false;
168 } else {
169 System.out.print("\t");
170 }
171
172 List<Double> heights = heightMap.get(state);
173 if (i < heights.size()) {
174 System.out.print(heights.get(i));
175 }
176 }
177 System.out.println();
178 }
179
180 } catch (Importer.ImportException e) {
181 System.err.println("Error Parsing Input Tree: " + e.getMessage());
182 } catch (IOException e) {
183 System.err.println("Error Parsing Input Tree: " + e.getMessage());
184 }
185 }
186
187 public static void printTitle() {
188 System.out.println();
189 centreLine("NodeStateAnalyser " + version.getVersionString() + ", " + version.getDateString(), 60);
190 centreLine("MCMC Output analysis", 60);
191 centreLine("by", 60);
192 centreLine("Andrew Rambaut and Alexei J. Drummond", 60);
193 System.out.println();
194 centreLine("Institute of Evolutionary Biology", 60);
195 centreLine("University of Edinburgh", 60);
196 centreLine("[email protected]", 60);
197 System.out.println();
198 centreLine("Department of Computer Science", 60);
199 centreLine("University of Auckland", 60);
200 centreLine("[email protected]", 60);
201 System.out.println();
202 System.out.println();
203 }
204
205 public static void centreLine(String line, int pageWidth) {
206 int n = pageWidth - line.length();
207 int n1 = n / 2;
208 for (int i = 0; i < n1; i++) {
209 System.out.print(" ");
210 }
211 System.out.println(line);
212 }
213
214
215 public static void printUsage(Arguments arguments) {
216
217 arguments.printUsage("nodestateanalyser", "[-burnin <burnin>][<input-file-name> [<output-file-name>]]");
218 System.out.println();
219 System.out.println(" Example: nodestateanalyser test.tree");
220 System.out.println(" Example: nodestateanalyser -burnin 10000 trees.log out.txt");
221 System.out.println();
222
223 }
224
225 //Main method
226 public static void main(String[] args) throws IOException {
227
228 printTitle();
229
230 Arguments arguments = new Arguments(
231 new Arguments.Option[]{
232 new Arguments.IntegerOption("burnin", "the number of states to be considered as 'burn-in'"),
233 new Arguments.RealOption("mrsd","specifies the most recent sampling data in fractional years to rescale time [default=0]"),
234 new Arguments.RealOption("scale","Provide a scaling factor for the node heights [default=1]"),
235 new Arguments.Option("help", "option to print this message")
236 });
237
238 try {
239 arguments.parseArguments(args);
240 } catch (Arguments.ArgumentException ae) {
241 System.out.println(ae);
242 printUsage(arguments);
243 System.exit(1);
244 }
245
246 if (arguments.hasOption("help")) {
247 printUsage(arguments);
248 System.exit(0);
249 }
250
251 int burnin = -1;
252 if (arguments.hasOption("burnin")) {
253 burnin = arguments.getIntegerOption("burnin");
254 }
255
256 double scale = 1.0;
257 if (arguments.hasOption("scale")) {
258 scale = arguments.getRealOption("scale");
259 }
260
261
262 double mrsd = Double.NaN;
263 if (arguments.hasOption("mrsd")) {
264 mrsd = arguments.getRealOption("mrsd");
265 }
266
267 String inputFileName = null;
268 String outputFileName = null;
269
270 String[] args2 = arguments.getLeftoverArguments();
271
272 if (args2.length > 2) {
273 System.err.println("Unknown option: " + args2[2]);
274 System.err.println();
275 printUsage(arguments);
276 System.exit(1);
277 }
278
279 if (args2.length > 0) {
280 inputFileName = args2[0];
281 }
282 if (args2.length > 1) {
283 outputFileName = args2[1];
284 }
285
286 if (inputFileName == null) {
287 // No input file name was given so throw up a dialog box...
288 inputFileName = Utils.getLoadFileName("NodeStateAnalyser " + version.getVersionString() + " - Select tree file to analyse");
289 }
290
291 if(burnin==-1) {
292 System.out.println("Enter number of trees to burn-in (integer): ");
293 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
294 burnin = Integer.parseInt(br.readLine());
295 }
296
297 new NodeStateAnalyser(burnin, mrsd, scale, inputFileName, outputFileName);
298
299 System.exit(0);
300 }
301 }