Debian Codegraph @

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