Debian Codegraph @

src/dr/app/phylogeography/tools/ContinuousTreeToKML.java

raw symbols SCIP
1 /*
2 * ContinuousTreeToKML.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.phylogeography.tools;
27
28 import dr.app.util.Arguments;
29 import jebl.evolution.io.NexusImporter;
30 import jebl.evolution.io.TreeImporter;
31 import jebl.evolution.io.ImportException;
32 import jebl.evolution.trees.RootedTree;
33
34 import java.io.*;
35
36 /**
37 * @author Philippe Lemey
38 * @author Andrew Rambaut
39 * @author Marc A. Suchard
40 */
41 public class ContinuousTreeToKML {
42 public static final String HELP = "help";
43 public static final String ANNOTATION = "annotation";
44 public static final String ALTITUDE = "altitude";
45 public static final String MRSD = "mrsd";
46 public static final String SLICES = "slices";
47 public static final String SLICEBW = "slicebw";
48 public static final String SLICEMIDPOINT = "slicemidpoint";
49
50 private static final String commandName = "continuous_tree_to_kml";
51 private static final PrintStream progressStream = System.out;
52
53 public static final String[] falseTrue = new String[] {"false","true"};
54
55 public static void printUsage(Arguments arguments) {
56
57 arguments.printUsage(commandName, "[<inputTree-file-name>] [<output-file-name>]"); //TODO: set this right
58 progressStream.println();
59 progressStream.println(" Example: " + commandName + " input.tre output.kml");
60 progressStream.println();
61 }
62
63 public static void main(String[] args) {
64
65 double altitude = 0; // altitutude of the root of the 3D trees
66 double mostRecentDate = 2010; // required to convert heights to calendar dates
67 String coordinateLabel = "loc";
68
69 boolean makeTreeSlices = false;
70 double[] sliceTimes = null;
71 double treeSliceBranchWidth = 3;
72 boolean showBranchAtMidPoint = false; // shows complete branch for slice if time is more recent than the branch's midpoint
73
74 Arguments arguments = new Arguments(
75 new Arguments.Option[]{
76 new Arguments.StringOption(ANNOTATION, "location annotation label", "specifies the label used for location coordinates annotation [default=location]"),
77 new Arguments.RealOption(ALTITUDE,"specifies the altitude of the root of the 3D tree [default=no 3D tree]"),
78 new Arguments.RealOption(MRSD,"specifies the most recent sampling data in fractional years to rescale time [default=2010]"),
79
80 new Arguments.StringOption(SLICES,"time","specifies a slice time-list [default=none]"),
81 new Arguments.StringOption(SLICEMIDPOINT, falseTrue, false,
82 "shows complete branch for sliced tree if time is more recent than the branch's midpoint [default=false"),
83
84 new Arguments.Option(HELP, "option to print this message")
85 });
86
87 try {
88 arguments.parseArguments(args);
89 } catch (Arguments.ArgumentException ae) {
90 progressStream.println(ae);
91 printUsage(arguments);
92 System.exit(1);
93 }
94
95 if (args.length == 0 || arguments.hasOption(HELP)) {
96 printUsage(arguments);
97 System.exit(0);
98 }
99
100
101 if (arguments.hasOption(MRSD)) {
102 mostRecentDate = arguments.getRealOption(MRSD);
103 }
104
105 if (arguments.hasOption(ALTITUDE)) {
106 altitude = arguments.getRealOption(ALTITUDE);
107 }
108
109 String annotationLabel = arguments.getStringOption(ANNOTATION);
110 if (annotationLabel != null){
111 coordinateLabel = annotationLabel;
112 }
113
114 String sliceString = arguments.getStringOption(SLICES);
115 if (sliceString != null) {
116 makeTreeSlices = true;
117 try{
118 sliceTimes = DiscreteTreeToKML.parseVariableLengthDoubleArray(sliceString);
119 } catch (Arguments.ArgumentException ae){
120 System.err.println("error reading slice heights");
121 ae.printStackTrace();
122 return;
123 }
124 makeTreeSlices = true;
125 }
126
127 if (arguments.hasOption(SLICEBW)) {
128 treeSliceBranchWidth = arguments.getRealOption(SLICEBW);
129 }
130
131 String midpointString = arguments.getStringOption(SLICEMIDPOINT);
132 if (midpointString != null && midpointString.compareToIgnoreCase("true") == 0) {
133 showBranchAtMidPoint = true;
134 }
135
136 final String[] args2 = arguments.getLeftoverArguments();
137
138 String inputFileName = null;
139 String outputFileName = null;
140
141 switch (args2.length) {
142 case 0:
143 printUsage(arguments);
144 System.exit(1);
145 case 1:
146 inputFileName = args2[0];
147 outputFileName = inputFileName + ".kml";
148 break;
149 case 2:
150 inputFileName = args2[0];
151 outputFileName = args2[1];
152 break;
153 default: {
154 System.err.println("Unknown option: " + args2[2]);
155 System.err.println();
156 printUsage(arguments);
157 System.exit(1);
158 }
159 }
160
161 RootedTree tree = null;
162
163 try {
164 TreeImporter importer = new NexusImporter(new FileReader(inputFileName));
165 tree = (RootedTree)importer.importNextTree();
166 } catch (ImportException e) {
167 e.printStackTrace();
168 return;
169 } catch (IOException e) {
170 e.printStackTrace();
171 return;
172 }
173
174 ContinuousKML exporter = new ContinuousKML(tree, inputFileName, altitude, mostRecentDate, coordinateLabel);
175
176 try {
177 BufferedWriter out1 = new BufferedWriter(new FileWriter(outputFileName));
178 StringBuffer buffer = new StringBuffer();
179
180 //we write the general tree stuff, but when making slices we do not include everything in the buffer compilation
181 exporter.writeTreeToKML();
182
183 if (makeTreeSlices) {
184 for (int i = 0; i < sliceTimes.length; i++) {
185 // System.out.println(sliceTimes[i]);
186 exporter.writeTreeToKML(sliceTimes[i], treeSliceBranchWidth, showBranchAtMidPoint);
187 }
188 }
189
190 exporter.compileBuffer(buffer,makeTreeSlices);
191 out1.write(buffer.toString());
192 out1.close();
193 } catch (IOException e) {
194 e.printStackTrace();
195 return;
196 }
197
198 }
199 }