Debian Codegraph @

src/dr/app/tempest/RootToTip.java

raw symbols SCIP
1 /*
2 * RootToTip.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.tempest;
27
28 import dr.app.beauti.options.DateGuesser;
29 import dr.app.util.Arguments;
30 import dr.app.tools.NexusExporter;
31 import dr.evolution.io.Importer;
32 import dr.evolution.io.NexusImporter;
33 import dr.evolution.io.TreeImporter;
34 import dr.evolution.tree.Tree;
35 import dr.evolution.util.TaxonList;
36 import dr.stats.Regression;
37 import dr.stats.Variate;
38 import dr.util.Version;
39
40 import java.io.FileReader;
41 import java.io.IOException;
42 import java.io.PrintStream;
43 import java.io.PrintWriter;
44 import java.util.ArrayList;
45 import java.util.List;
46
47 /*
48 * Essentially a command line version of TempEst. Written to
49 * perform the analysis on sets of trees.
50 *
51 * @author Andrew Rambaut
52 */
53
54 public class RootToTip {
55
56 private final static Version version = new Version() {
57 @Override
58 public String getVersion() {
59 return "1.5";
60 }
61
62 @Override
63 public String getVersionString() {
64 return "v1.5";
65 }
66
67 @Override
68 public String getBuildString() {
69 return "";
70 }
71
72 @Override
73 public String getDateString() {
74 return "2003-2015";
75 }
76
77 @Override
78 public String[] getCredits() {
79 return new String[0];
80 }
81
82 @Override
83 public String getHTMLCredits() {
84 return "";
85 }
86 };
87
88 public RootToTip(int burnin, String dateOrder, final boolean keepRoot, String outgroup,
89 boolean writeTree, String inputFileName, String outputFileName) throws IOException {
90
91 System.out.println("Reading tree(s)...");
92
93 boolean firstTree = true;
94 FileReader fileReader = new FileReader(inputFileName);
95 TreeImporter importer = new NexusImporter(fileReader);
96
97 List<Regression> regressions = new ArrayList<Regression>();
98 List<Tree> trees = new ArrayList<Tree>();
99
100 DateGuesser dg = new DateGuesser();
101 dg.fromLast = false;
102 if (dateOrder.equals("FIRST")) {
103 dg.order = 0;
104 } else if (dateOrder.equals("LAST")) {
105 dg.order = 0;
106 dg.fromLast = true;
107 } else {
108 dg.order = Integer.parseInt(dateOrder) - 1;
109 if (dg.order < 0 || dg.order > 100) {
110 System.err.println("Error Parsing order of date field: " + dateOrder);
111 }
112 }
113
114 TaxonList taxa = null;
115 TemporalRooting temporalRooting = null;
116
117 try {
118 while (importer.hasTree()) {
119 Tree tree = importer.importNextTree();
120
121 if (firstTree) {
122 taxa = tree;
123
124 dg.guessDates(taxa);
125
126 temporalRooting = new TemporalRooting(taxa);
127
128 firstTree = false;
129 }
130
131 if (totalTrees >= burnin) {
132 Tree rootedTree = tree;
133
134 if (!keepRoot) {
135 rootedTree = temporalRooting.findRoot(tree, TemporalRooting.RootingFunction.CORRELATION);
136 }
137
138 regressions.add(temporalRooting.getRootToTipRegression(rootedTree));
139
140 if (writeTree) {
141 trees.add(rootedTree);
142 }
143 totalTreesUsed += 1;
144 }
145 totalTrees += 1;
146
147 }
148 } catch (Importer.ImportException e) {
149 System.err.println("Error Parsing Input Tree: " + e.getMessage());
150 return;
151 }
152 fileReader.close();
153
154 PrintWriter printWriter;
155
156 if (!writeTree && outputFileName != null) {
157 printWriter = new PrintWriter(outputFileName);
158 } else {
159 printWriter = new PrintWriter(System.out);
160 }
161
162 if (regressions.size() == 1) {
163 Regression r = regressions.get(0);
164
165 Variate dates = r.getXData();
166 Variate distances = r.getYData();
167
168 printWriter.println("date\tdistance");
169 for (int i = 0; i < dates.getCount(); i++) {
170 printWriter.println(dates.get(i) + "\t" + distances.get(i));
171 }
172 printWriter.println();
173 printWriter.println("Regression slope = " + r.getGradient());
174 printWriter.println("X-Intercept = " + r.getXIntercept());
175 printWriter.println("Y-Intercept = " + r.getYIntercept());
176 printWriter.println("Residual mean squared = " + r.getResidualMeanSquared());
177 printWriter.println("R^2 = " + r.getRSquared());
178 printWriter.println("Correlation coefficient = " + r.getCorrelationCoefficient());
179
180 } else {
181 printWriter.println("tree\tslope\tx-intercept\ty-intercept\tcorrelation");
182 int i = 1;
183 for (Regression r : regressions) {
184 printWriter.print("\t" + i);
185 printWriter.print("\t" + r.getGradient());
186 printWriter.print("\t" + r.getXIntercept());
187 printWriter.println("\t" + r.getYIntercept());
188 printWriter.println("\t" + r.getCorrelationCoefficient());
189 }
190
191 }
192
193 printWriter.close();
194
195 if (writeTree) {
196 PrintStream printStream;
197
198 if (outputFileName != null) {
199 printStream = new PrintStream(outputFileName);
200 } else {
201 printStream = new PrintStream(System.out);
202 }
203
204 NexusExporter exporter = new NexusExporter(printStream);
205 Tree[] treeArray = new Tree[trees.size()];
206 trees.toArray(treeArray);
207
208 exporter.exportTrees(treeArray);
209
210 printStream.close();
211 }
212 }
213
214 int totalTrees = 0;
215 int totalTreesUsed = 0;
216
217 public static void printTitle() {
218 System.out.println();
219 centreLine("RootToTip " + version.getVersionString() + ", " + version.getDateString(), 60);
220 centreLine("Root to tip distance vs. time of sampling", 60);
221 centreLine("by", 60);
222 centreLine("Andrew Rambaut", 60);
223 System.out.println();
224 System.out.println();
225 }
226
227 public static void centreLine(String line, int pageWidth) {
228 int n = pageWidth - line.length();
229 int n1 = n / 2;
230 for (int i = 0; i < n1; i++) {
231 System.out.print(" ");
232 }
233 System.out.println(line);
234 }
235
236
237 public static void printUsage(Arguments arguments) {
238
239 arguments.printUsage("roottotip", "<input-file-name> [<output-file-name>]");
240 System.out.println();
241 System.out.println(" Example: roottotip -burnin 100 test.trees rootToTip.txt");
242 System.out.println();
243 }
244
245 //Main method
246 public static void main(String[] args) throws IOException {
247
248 String inputFileName = null;
249 String outputFileName = null;
250
251 printTitle();
252
253 Arguments arguments = new Arguments(
254 new Arguments.Option[]{
255 new Arguments.IntegerOption("burnin", "the number of trees to be ignored as 'burn-in' [default = 0]"),
256 new Arguments.StringOption("dateorder", "date_order", "order of date field in taxon name: first, last, 1, 2 etc. [default = last]"),
257 // new Arguments.StringOption("outgroup", "{taxon list}", "one or more taxa that will be used to root the tree(s) [default = find root]"),
258 new Arguments.Option("keeproot", "keep the existing root of the input trees [default = estimate root]"),
259 new Arguments.Option("writetree", "Write the optimally rooted tree to the output file"),
260 new Arguments.Option("help", "option to print this message"),
261 });
262
263 try {
264 arguments.parseArguments(args);
265 } catch (Arguments.ArgumentException ae) {
266 System.out.println(ae);
267 printUsage(arguments);
268 System.exit(1);
269 }
270
271 if (arguments.hasOption("help")) {
272 printUsage(arguments);
273 System.exit(0);
274 }
275
276 int burnin = 0;
277 if (arguments.hasOption("burnin")) {
278 burnin = arguments.getIntegerOption("burnin");
279 }
280
281 String dateOrder = "LAST";
282 if (arguments.hasOption("dateorder")) {
283 dateOrder = arguments.getStringOption("dateorder").toUpperCase();
284 }
285
286 String outgroup = null;
287 if (arguments.hasOption("outgroup")) {
288 outgroup = arguments.getStringOption("dateorder").toUpperCase();
289 }
290
291 boolean keepRoot = arguments.hasOption("keeproot");
292
293 boolean writeTree = arguments.hasOption("writetree");
294
295 String[] args2 = arguments.getLeftoverArguments();
296
297 if (args2.length > 2) {
298 System.err.println("Unknown option: " + args2[2]);
299 System.err.println();
300 printUsage(arguments);
301 System.exit(1);
302 }
303
304 if (args2.length == 0) {
305 System.err.println("Missing input file name");
306 printUsage(arguments);
307 System.exit(1);
308 }
309
310
311 inputFileName = args2[0];
312 if (args2.length == 2) {
313 outputFileName = args2[1];
314 }
315
316 new RootToTip(burnin,
317 dateOrder,
318 keepRoot,
319 outgroup,
320 writeTree,
321 inputFileName,
322 outputFileName
323 );
324
325 System.exit(0);
326 }
327
328 }