Debian Codegraph @

src/dr/app/tools/TreeLengthFinder.java

raw symbols SCIP
1 /*
2 * TreeLengthFinder.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.app.treestat.statistics.TreeLength;
32 import dr.inference.trace.TraceException;
33 import dr.util.Version;
34 import dr.evolution.io.TreeImporter;
35 import dr.evolution.io.NexusImporter;
36 import dr.evolution.io.Importer;
37 import dr.evolution.tree.Tree;
38
39 import java.io.*;
40
41 /**
42 * @author Wai Lok Sibon Li
43 */
44
45 public class TreeLengthFinder {
46
47 private final static Version version = new BeastVersion();
48
49
50 public TreeLengthFinder(int burnin, String inputFileName, String outputFileName/*, boolean verbose,
51 boolean hpds, boolean ess, boolean stdErr,
52 String marginalLikelihood*/) throws IOException, TraceException {
53
54 File parentFile = new File(inputFileName);
55
56 if (parentFile.isDirectory()) {
57 System.out.println("Analysing all trees files below directory: " + inputFileName);
58 } else if (parentFile.isFile()) {
59 System.out.println("Analysing tree file: " + inputFileName);
60 } else {
61 System.err.println(inputFileName + " does not exist!");
62 System.exit(0);
63 }
64
65 if (outputFileName != null) {
66 FileOutputStream outputStream = new FileOutputStream(outputFileName);
67 System.setOut(new PrintStream(outputStream));
68 }
69
70 analyze(parentFile, burnin/*, verbose, new boolean[]{true}, hpds, ess, stdErr, marginalLikelihood*/);
71 }
72
73 /**
74 * Recursively analyzes log files.
75 *
76 * @param file the file to analyze (if this is a directory then the files within it are analyzed)
77 * @param burnin the burnin to use
78 * @throws dr.inference.trace.TraceException
79 * if the trace file is in the wrong format or corrupted
80 */
81 private void analyze(File file, int burnin) throws TraceException {
82
83 if (file.isFile()) {
84 try {
85
86 String name = file.getCanonicalPath();
87
88 report(name, burnin);
89 //TraceAnalysis.report(name, burnin, marginalLikelihood);
90
91 } catch (IOException e) {
92 //e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
93 }
94 } else {
95 File[] files = file.listFiles();
96 for (File f : files) {
97 if (f.isDirectory()) {
98 analyze(f, burnin);
99 //} else if (f.getName().endsWith(".tree") || f.getName().endsWith(".p")) {
100 } else if (f.getName().endsWith(".trees")) {
101 analyze(f, burnin);
102 }
103 }
104 }
105 }
106
107
108 /**
109 * Recursively analyzes trees files.
110 *
111 * @param name the file to analyze (if this is a directory then the files within it are analyzed)
112 * @param burnin the burnin to use
113 */
114 private void report(String name, int burnin) {
115 double treeLength = 0.0;
116 int count = 0;
117
118 try {
119 FileReader fileReader = new FileReader(new File(name));
120 TreeImporter importer = new NexusImporter(fileReader);
121 while (importer.hasTree()) {
122 Tree tree = importer.importNextTree();
123 if(count>=burnin) {
124 treeLength += TreeLength.FACTORY.createStatistic().getSummaryStatistic(tree)[0];
125
126 }
127 count++;
128 }
129 treeLength /= (count-burnin);
130 System.out.println(name + "\t" + burnin + "\t" + treeLength);
131 } catch (Importer.ImportException e) {
132 System.err.println("Error Parsing Input Tree: " + e.getMessage());
133 } catch (IOException e) {
134 System.err.println("Error Parsing Input Tree: " + e.getMessage());
135 }
136 }
137
138 public static void printTitle() {
139 System.out.println();
140 centreLine("TreeLengthFinder " + version.getVersionString() + ", " + version.getDateString(), 60);
141 centreLine("MCMC Output analysis", 60);
142 centreLine("by", 60);
143 centreLine("Andrew Rambaut and Alexei J. Drummond", 60);
144 System.out.println();
145 centreLine("Institute of Evolutionary Biology", 60);
146 centreLine("University of Edinburgh", 60);
147 centreLine("[email protected]", 60);
148 System.out.println();
149 centreLine("Department of Computer Science", 60);
150 centreLine("University of Auckland", 60);
151 centreLine("[email protected]", 60);
152 System.out.println();
153 System.out.println();
154 }
155
156 public static void centreLine(String line, int pageWidth) {
157 int n = pageWidth - line.length();
158 int n1 = n / 2;
159 for (int i = 0; i < n1; i++) {
160 System.out.print(" ");
161 }
162 System.out.println(line);
163 }
164
165
166 public static void printUsage(Arguments arguments) {
167
168 //arguments.printUsage("loganalyser", "[-burnin <burnin>] [-short][-hpd] [-std] [<input-file-name> [<output-file-name>]]");
169 arguments.printUsage("treelengthfinder", "[-burnin <burnin>][<input-file-name> [<output-file-name>]]");
170 System.out.println();
171 System.out.println(" Example: treelengthfinder test.tree");
172 System.out.println(" Example: treelengthfinder -burnin 10000 trees.log out.txt");
173 System.out.println();
174
175 }
176
177 //Main method
178 public static void main(String[] args) throws IOException, TraceException {
179
180 printTitle();
181
182 Arguments arguments = new Arguments(
183 new Arguments.Option[]{
184 new Arguments.IntegerOption("burnin", "the number of states to be considered as 'burn-in'"),
185 /*new Arguments.Option("short", "use this option to produce a short report"),
186 new Arguments.Option("hpd", "use this option to produce hpds for each trace"),
187 new Arguments.Option("ess", "use this option to produce ESSs for each trace"),
188 new Arguments.Option("stdErr", "use this option to produce standard Error"),
189 new Arguments.StringOption("marginal", "trace_name", "specify the trace to use to calculate the marginal likelihood"),*/
190 // new Arguments.Option("html", "format output as html"),
191 // new Arguments.Option("svg", "generate svg graphics"),
192 new Arguments.Option("help", "option to print this message")
193 });
194
195 try {
196 arguments.parseArguments(args);
197 } catch (Arguments.ArgumentException ae) {
198 System.out.println(ae);
199 printUsage(arguments);
200 System.exit(1);
201 }
202
203 if (arguments.hasOption("help")) {
204 printUsage(arguments);
205 System.exit(0);
206 }
207
208 int burnin = -1;
209 if (arguments.hasOption("burnin")) {
210 burnin = arguments.getIntegerOption("burnin");
211 }
212
213
214
215
216 /*boolean hpds = arguments.hasOption("hpd");
217 boolean ess = arguments.hasOption("ess");
218 boolean stdErr = arguments.hasOption("stdErr");
219 boolean shortReport = arguments.hasOption("short");
220
221 String marginalLikelihood = null;
222 if (arguments.hasOption("marginal")) {
223 marginalLikelihood = arguments.getStringOption("marginal");
224 }*/
225
226 String inputFileName = null;
227 String outputFileName = null;
228
229 String[] args2 = arguments.getLeftoverArguments();
230
231 if (args2.length > 2) {
232 System.err.println("Unknown option: " + args2[2]);
233 System.err.println();
234 printUsage(arguments);
235 System.exit(1);
236 }
237
238 if (args2.length > 0) {
239 inputFileName = args2[0];
240 }
241 if (args2.length > 1) {
242 outputFileName = args2[1];
243 }
244
245 if (inputFileName == null) {
246 // No input file name was given so throw up a dialog box...
247 inputFileName = Utils.getLoadFileName("TreeLengthFinder " + version.getVersionString() + " - Select tree file to analyse");
248 }
249
250 if(burnin==-1) {
251 System.out.println("Enter number of trees to burn-in (integer): ");
252 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
253 burnin = Integer.parseInt(br.readLine());
254 }
255
256 new TreeLengthFinder(burnin, inputFileName, outputFileName/*, !shortReport, hpds, ess, stdErr, marginalLikelihood*/);
257
258 System.exit(0);
259 }
260 }