Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * TreeGeoJSONGenerator.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.phylogeography.tools.kml.*;
29 import jebl.evolution.graphs.Node;
30 import jebl.evolution.io.ImportException;
31 import jebl.evolution.io.NexusImporter;
32 import jebl.evolution.io.TreeImporter;
33 import jebl.evolution.trees.RootedTree;
34
35 import java.awt.*;
36 import java.io.*;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.List;
40 import java.util.Map;
41
42 /**
43 * @author Andrew Rambaut
44 */
45 public class TreeGeoJSONGenerator {
46 /*
47 { "type": "FeatureCollection",
48 "features": [
49 { "type": "Feature",
50 "geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
51 "properties": {"prop0": "value0"}
52 },
53 { "type": "Feature",
54 "geometry": {
55 "type": "LineString",
56 "coordinates": [
57 [102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
58 ]
59 },
60 "properties": {
61 "prop0": "value0",
62 "prop1": 0.0
63 }
64 },
65 { "type": "Feature",
66 "geometry": {
67 "type": "Polygon",
68 "coordinates": [
69 [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0],
70 [100.0, 1.0], [100.0, 0.0] ]
71 ]
72 },
73 "properties": {
74 "prop0": "value0",
75 "prop1": {"this": "that"}
76 }
77 }
78 ]
79 }
80 */
81
82
83 private final RootedTree tree;
84 private final Map<String, Location> locationMap = new HashMap<String, Location>();
85 private final List<GeoPoint> points = new ArrayList<GeoPoint>();
86 private final List<GeoLine> lines = new ArrayList<GeoLine>();
87
88 public TreeGeoJSONGenerator(RootedTree tree, List<Location> locationList) {
89 this.tree = tree;
90
91 if (locationList != null) {
92 double minLat = Double.MAX_VALUE;
93 double maxLat = -Double.MAX_VALUE;
94 double minLong = Double.MAX_VALUE;
95 double maxLong = -Double.MAX_VALUE;
96
97 for (Location location : locationList) {
98 if (location.getLatitude() < minLat) {
99 minLat = location.getLatitude();
100 }
101 if (location.getLatitude() > maxLat) {
102 maxLat = location.getLatitude();
103 }
104
105 if (location.getLongitude() < minLong) {
106 minLong = location.getLongitude();
107 }
108 if (location.getLongitude() > maxLong) {
109 maxLong = location.getLongitude();
110 }
111 locationMap.put(location.getState(), location);
112 }
113
114 }
115
116 traverseTree(tree, tree.getRootNode());
117 }
118
119 private GeoPoint traverseTree(RootedTree tree, Node node) {
120 GeoPoint point = new GeoPoint();
121 point.time = tree.getHeight(node);
122 point.location = (String)node.getAttribute("location");
123 point.host = (String)node.getAttribute("host");
124 point.longitude = locationMap.get(point.location).getLongitude();
125 point.latitude = locationMap.get(point.location).getLatitude();
126
127 if (tree.isExternal(node)) {
128 point.label = tree.getTaxon(node).getName();
129 point.probability = 1;
130
131 } else {
132 point.label = "";
133 point.probability = (Double)node.getAttribute("posterior");
134
135 for (Node child : tree.getChildren(node)) {
136
137 GeoPoint point1 = traverseTree(tree, child);
138
139 GeoLine line = new GeoLine();
140 line.label = "";
141
142 line.time0 = point.time;
143 line.time1 = point1.time;
144
145 line.location0 = point.location;
146 line.location1 = point1.location;
147
148 line.host0 = point.host;
149 line.host1 = point1.host;
150
151 line.longitude0 = point.longitude;
152 line.longitude1 = point1.longitude;
153
154 line.latitude0 = point.latitude;
155 line.latitude1 = point1.latitude;
156
157 lines.add(line);
158 }
159 }
160
161 points.add(point);
162
163 return point;
164 }
165
166 public void generate(String documentName, PrintWriter writer) {
167 writer.println("{ \"type\": \"FeatureCollection\",");
168 writer.println("\t\"features\": [");
169 for (GeoPoint point : points) {
170 generatePoint(writer, point);
171 }
172 writer.println("\t]");
173 writer.println("},");
174 writer.println("{ \"type\": \"FeatureCollection\",");
175 writer.println("\t\"features\": [");
176 for (GeoLine line : lines) {
177 generateLine(writer, line);
178 }
179 writer.println("\t]");
180 writer.println("}");
181
182 }
183
184 public void generatePoint(PrintWriter writer, GeoPoint point) {
185 writer.println("\t\t{ \"type\": \"Feature\",");
186 writer.println("\t\t\t\"geometry\": {\"type\": \"Point\", \"coordinates\": [" + point.longitude + ", " + point.latitude + "]},");
187 writer.println("\t\t\t\"properties\": {\"label\": \"" + point.label + "\", \"time\": \"" + point.time + "\", \"probability\": \"" + point.probability +
188 "\", \"location\": \"" + point.location + "\", \"host\": \"" + point.host + "\"}");
189 writer.println("},");
190 }
191
192 public void generateLine(PrintWriter writer, GeoLine line) {
193 writer.println("\t\t{ \"type\": \"Feature\",");
194 writer.println("\t\t\t\"geometry\": {\"type\": \"LineString\", \"coordinates\": [[" + line.longitude0 + ", " + line.latitude0 + "], [" + line.longitude1 + ", " + line.latitude1 + "]]},");
195 writer.println("\t\t\t\"properties\": {\"label\": \"" + line.label + "\", \"time0\": \"" + line.time0 + "\", \"time0\": \"" + line.time1 +
196 "\", \"location0\": \"" + line.location0 + "\", \"location1\": \"" + line.location1 +
197 "\", \"host\": \"" + line.host0 + "\", \"host1\": \"" + line.host1 + "\"}");
198 writer.println("},");
199 }
200
201 class GeoPoint {
202 String label;
203 double time;
204 double probability;
205 String location;
206 String host;
207 double longitude;
208 double latitude;
209 }
210
211 class GeoLine {
212 String label;
213 double time0;
214 double time1;
215 String location0;
216 String location1;
217 String host0;
218 String host1;
219 double longitude0;
220 double latitude0;
221 double longitude1;
222 double latitude1;
223 }
224
225 public static void main(String[] args) {
226
227
228 String inputTreeFile = args[0];
229 RootedTree tree = null;
230
231 try {
232 TreeImporter importer = new NexusImporter(new FileReader(inputTreeFile));
233 tree = (RootedTree)importer.importNextTree();
234 } catch (ImportException e) {
235 e.printStackTrace();
236 return;
237 } catch (IOException e) {
238 e.printStackTrace();
239 return;
240 }
241
242 TreeGeoJSONGenerator generator = new TreeGeoJSONGenerator(tree, null);
243 try {
244 generator.generate("", new PrintWriter(new File("output.geojson")));
245 } catch (FileNotFoundException e) {
246 e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
247 }
248 }
249
250 }