Debian Codegraph @

src/dr/app/tools/TreeLogAnalyser.java

raw symbols SCIP
1 /*
2 * TreeLogAnalyser.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.evolution.io.Importer;
32 import dr.evolution.io.NexusImporter;
33 import dr.evolution.tree.Tree;
34 import dr.evomodel.tree.TreeTraceAnalysis;
35 import dr.util.Version;
36
37 import java.io.*;
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.Locale;
41
42 /**
43 * @author Alexei Drummond
44 * @author Andrew Rambaut
45 */
46 public class TreeLogAnalyser {
47
48 private final static Version version = new BeastVersion();
49
50 static boolean combine = true;
51
52 public TreeLogAnalyser(int burnin, String inputFileName, String outputFileName, String trueTreeFileName,
53 String exportFileName, double minSupport, double credibleSetProbability, int maxExport, boolean verbose) throws IOException {
54
55 List<File> files = new ArrayList<File>();
56 File inputFile = new File(inputFileName);
57
58 if (inputFile.isDirectory()) {
59 System.out.println("Analysing all tree files below directory: " + inputFileName);
60
61 collectFiles(inputFile, files);
62 } else if (inputFile.isFile()) {
63 System.out.println("Analysing tree file: " + inputFileName);
64
65 files.add(inputFile);
66 } else {
67 System.err.println(inputFileName + " does not exist!");
68 System.exit(0);
69 }
70
71 if( files.size() == 0 ) {
72 System.err.println("No valid files");
73 System.exit(0);
74 }
75
76 if (outputFileName != null) {
77 FileOutputStream outputStream = new FileOutputStream(outputFileName);
78 System.setOut(new PrintStream(outputStream));
79 }
80
81 Tree trueTree = null;
82 if (trueTreeFileName != null) {
83 NexusImporter importer = new NexusImporter(new FileReader(trueTreeFileName));
84 try {
85 trueTree = importer.importNextTree();
86 } catch (Importer.ImportException e) {
87 throw new IOException(e.getMessage());
88 }
89 }
90
91 analyze(files, burnin, trueTree, verbose, exportFileName, minSupport, credibleSetProbability, maxExport, new boolean[]{true});
92 }
93
94 private static void collectFiles(File file, List<File> files) {
95
96 if (file.isFile()) {
97 if (file.getName().endsWith(".tre") || file.getName().endsWith(".trees") || file.getName().endsWith(".t")) {
98 files.add(file);
99 }
100 } else {
101 File[] listFiles = file.listFiles();
102 for (File listFile : listFiles) {
103 collectFiles(listFile, files);
104 }
105 }
106 }
107
108 private static void analyze(List<File> files, int burnin, Tree tree, boolean verbose, String exportFileName,
109 double minSupport, double credibleSetProbability, int maxExport, boolean[] drawHeader) {
110
111 if (combine) {
112 try {
113 Reader[] readers = new Reader[files.size()];
114 for (int i = 0; i < readers.length; i++) {
115 readers[i] = new FileReader(files.get(i));
116 }
117 TreeTraceAnalysis analysis = TreeTraceAnalysis.analyzeLogFile(readers, burnin, verbose);
118 if (exportFileName != null) {
119 PrintStream exportStream = new PrintStream(exportFileName);
120 //System.err.println("Exporting trees ...");
121 analysis.export(exportStream, minSupport, maxExport, verbose);
122 } else {
123 if (verbose) {
124 analysis.report(0.05, credibleSetProbability, (int)(minSupport+.5));
125 } else {
126 final String name = files.size() > 1 ? "combined" : files.get(0).toString();
127 analysis.shortReport(name, tree, drawHeader[0]);
128 drawHeader[0] = false;
129 }
130 }
131
132 } catch (IOException ioe) {
133 //
134 }
135 } else {
136 for (File file : files) {
137 try {
138 final Reader[] readers = {new FileReader(file)};
139 TreeTraceAnalysis analysis = TreeTraceAnalysis.analyzeLogFile(readers, burnin, verbose);
140 if (verbose) {
141 analysis.report((int)(minSupport+.5));
142 } else {
143 analysis.shortReport(file.toString(), tree, drawHeader[0]);
144 drawHeader[0] = false;
145 }
146 } catch (IOException ioe) {
147 //
148 }
149 }
150 }
151 }
152
153 public static void printTitle() {
154 System.out.println();
155 centreLine("TreeLogAnalyser " + version.getVersionString() + ", " + version.getDateString(), 60);
156 centreLine("MCMC Output analysis", 60);
157 centreLine("by", 60);
158 centreLine("Alexei Drummond and Andrew Rambaut", 60);
159 System.out.println();
160 centreLine("Department of Computer Science", 60);
161 centreLine("University of Auckland", 60);
162 centreLine("[email protected]", 60);
163 System.out.println();
164 centreLine("Institute of Evolutionary Biology", 60);
165 centreLine("University of Edinburgh", 60);
166 centreLine("[email protected]", 60);
167 System.out.println();
168 System.out.println();
169 }
170
171 public static void centreLine(String line, int pageWidth) {
172 int n = pageWidth - line.length();
173 int n1 = n / 2;
174 for (int i = 0; i < n1; i++) {
175 System.out.print(" ");
176 }
177 System.out.println(line);
178 }
179
180
181 public static void printUsage(Arguments arguments) {
182
183 arguments.printUsage("treeloganalyser", "<input-file-name> [<true-tree-file-name> [<output-file-name>]]");
184 System.out.println();
185 System.out.println(" Example: treeloganalyser test.trees trueTree.tree out.txt");
186 System.out.println();
187 }
188
189 //Main method
190 public static void main(String[] args) throws java.io.IOException {
191
192 // There is a major issue with languages that use the comma as a decimal separator.
193 // To ensure compatibility between programs in the package, enforce the US locale.
194 Locale.setDefault(Locale.US);
195
196 printTitle();
197
198 Arguments arguments = new Arguments(
199 new Arguments.Option[]{
200 new Arguments.IntegerOption("burnin", "the number of states to be considered as 'burn-in' [default = none]"),
201 new Arguments.StringOption("export", "file-name", "name of file to export"),
202 new Arguments.RealOption("limit", "don't export trees with support lower than limit [default = 0.0]"),
203 new Arguments.RealOption("probability", "credible set probability limit [default = 0.95]"),
204 new Arguments.IntegerOption("max", "export no more than max trees [default = all]"),
205 new Arguments.Option("short", "use this option to produce a short report"),
206 new Arguments.Option("help", "option to print this message")
207 });
208
209 try {
210 arguments.parseArguments(args);
211 } catch (Arguments.ArgumentException ae) {
212 System.out.println(ae);
213 printUsage(arguments);
214 System.exit(1);
215 }
216
217 if (arguments.hasOption("help")) {
218 printUsage(arguments);
219 System.exit(0);
220 }
221
222 int burnin = -1;
223 if (arguments.hasOption("burnin")) {
224 burnin = arguments.getIntegerOption("burnin");
225 }
226
227 boolean shortReport = arguments.hasOption("short");
228
229 String exportFileName = null;
230 if (arguments.hasOption("export")) {
231 exportFileName = arguments.getStringOption("export");
232 }
233
234 double minSupport = 0.0;
235 if (arguments.hasOption("limit")) {
236 minSupport = arguments.getRealOption("limit");
237 }
238
239 double credibleSetProbability = 0.95;
240 if (arguments.hasOption("probability")) {
241 credibleSetProbability = arguments.getRealOption("probability");
242 }
243
244 int maxExport = -1;
245 if (arguments.hasOption("max")) {
246 maxExport = arguments.getIntegerOption("max");
247 }
248
249 String inputFileName = null;
250 String trueTreeFileName = null;
251 String outputFileName = null;
252
253 String[] args2 = arguments.getLeftoverArguments();
254
255 if (args2.length > 3) {
256 System.err.println("Unknown option: " + args2[2]);
257 System.err.println();
258 printUsage(arguments);
259 System.exit(1);
260 }
261
262 if (args2.length > 0) {
263 inputFileName = args2[0];
264 }
265
266 if (args2.length > 1) {
267 trueTreeFileName = args2[1];
268 }
269
270 if (args2.length > 2) {
271 outputFileName = args2[2];
272 }
273
274 if (inputFileName == null) {
275 // No input file name was given so throw up a dialog box...
276 inputFileName = Utils.getLoadFileName("TreeLogAnalyser " + version.getVersionString() + " - Select log file to analyse");
277 }
278
279 new TreeLogAnalyser(burnin, inputFileName, outputFileName, trueTreeFileName, exportFileName,
280 minSupport, credibleSetProbability, maxExport, !shortReport);
281
282 System.exit(0);
283 }
284 }
285