| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 44 |
|
| 45 |
public class TreeGeoJSONGenerator { |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 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(); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
} |