| 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.beast.BeastVersion; |
| 29 |
import dr.app.phylogeography.tools.kml.Location; |
| 30 |
import dr.app.util.Arguments; |
| 31 |
import dr.util.Version; |
| 32 |
import jebl.evolution.graphs.Node; |
| 33 |
import jebl.evolution.io.ImportException; |
| 34 |
import jebl.evolution.io.NexusImporter; |
| 35 |
import jebl.evolution.io.TreeImporter; |
| 36 |
import jebl.evolution.trees.RootedTree; |
| 37 |
import jebl.math.Random; |
| 38 |
import org.jdom.Document; |
| 39 |
import org.jdom.Element; |
| 40 |
import org.jdom.output.Format; |
| 41 |
import org.jdom.output.XMLOutputter; |
| 42 |
|
| 43 |
import java.io.*; |
| 44 |
import java.util.ArrayList; |
| 45 |
import java.util.HashMap; |
| 46 |
import java.util.List; |
| 47 |
import java.util.Map; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class TreeDensityKML { |
| 54 |
private static final String STATE_ATTRIBUTE_NAME = "states"; |
| 55 |
|
| 56 |
private final static Version version = new BeastVersion(); |
| 57 |
|
| 58 |
private final Map<String, Location> locationMap; |
| 59 |
|
| 60 |
public TreeDensityKML(int burnin, |
| 61 |
int skipEvery, |
| 62 |
String inputFileName, |
| 63 |
String outputFileName) throws IOException { |
| 64 |
|
| 65 |
locationMap = readCoordinates("locations.txt"); |
| 66 |
|
| 67 |
System.out.println("Reading trees..."); |
| 68 |
|
| 69 |
Element root = new Element("kml"); |
| 70 |
|
| 71 |
Element doc = new Element("Document"); |
| 72 |
doc.addContent(generateElement("name", inputFileName)); |
| 73 |
|
| 74 |
root.addContent(doc); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
List<Element> trees = new ArrayList<Element>(); |
| 98 |
|
| 99 |
boolean firstTree = true; |
| 100 |
FileReader fileReader = new FileReader(inputFileName); |
| 101 |
TreeImporter importer = new NexusImporter(fileReader); |
| 102 |
try { |
| 103 |
while (importer.hasTree()) { |
| 104 |
RootedTree tree = (RootedTree)importer.importNextTree(); |
| 105 |
|
| 106 |
if (firstTree) { |
| 107 |
firstTree = false; |
| 108 |
} |
| 109 |
|
| 110 |
if (totalTrees >= burnin) { |
| 111 |
if (totalTrees % skipEvery == 0) { |
| 112 |
trees.add(generateKMLTree("Tree_" + totalTrees, tree)); |
| 113 |
|
| 114 |
if (totalTrees % 50 == 0) { |
| 115 |
System.out.print("."); |
| 116 |
} |
| 117 |
totalTreesUsed += 1; |
| 118 |
} |
| 119 |
} |
| 120 |
totalTrees += 1; |
| 121 |
|
| 122 |
} |
| 123 |
|
| 124 |
System.out.println(); |
| 125 |
|
| 126 |
} catch (ImportException e) { |
| 127 |
System.err.println("Error Parsing Input Tree: " + e.getMessage()); |
| 128 |
return; |
| 129 |
} |
| 130 |
fileReader.close(); |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
doc.addContent(trees); |
| 135 |
|
| 136 |
try { |
| 137 |
|
| 138 |
BufferedWriter out = new BufferedWriter(new FileWriter(outputFileName)); |
| 139 |
Document document = new Document(root); |
| 140 |
|
| 141 |
try { |
| 142 |
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat()); |
| 143 |
outputter.output(document, out); |
| 144 |
} |
| 145 |
catch (IOException e) { |
| 146 |
System.err.println(e); |
| 147 |
} |
| 148 |
out.close(); |
| 149 |
} catch (IOException e) { |
| 150 |
e.printStackTrace(); |
| 151 |
return; |
| 152 |
} |
| 153 |
|
| 154 |
} |
| 155 |
|
| 156 |
private Element generateKMLTree(String name, RootedTree tree) { |
| 157 |
|
| 158 |
|
| 159 |
Element element = generateContainer("Folder", name, null, null); |
| 160 |
|
| 161 |
double longNoise = Random.nextGaussian() * 0.5; |
| 162 |
double latNoise = Random.nextGaussian() * 0.5; |
| 163 |
|
| 164 |
int nodeNumber = 0; |
| 165 |
for (Node node : tree.getNodes()) { |
| 166 |
nodeNumber++; |
| 167 |
|
| 168 |
if (!tree.isRoot(node)) { |
| 169 |
String state = (String)node.getAttribute(STATE_ATTRIBUTE_NAME); |
| 170 |
Location location = locationMap.get(state); |
| 171 |
if (location == null) { |
| 172 |
throw new RuntimeException("No location called " + state + " in location list"); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
String nodeName = name + "_node" + nodeNumber; |
| 178 |
|
| 179 |
|
| 180 |
Node parentNode = tree.getParent(node); |
| 181 |
String parentState = (String)parentNode.getAttribute(STATE_ATTRIBUTE_NAME); |
| 182 |
|
| 183 |
Location parentLocation = locationMap.get(parentState); |
| 184 |
if (parentLocation == null) { |
| 185 |
throw new RuntimeException("No location called " + parentState + " in location list"); |
| 186 |
} |
| 187 |
|
| 188 |
Element branch = generateContainer("Placemark", nodeName, null, null); |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
Element lineString = new Element("LineString"); |
| 193 |
lineString.addContent(generateElement("altitudeMode", "clampToGround")); |
| 194 |
|
| 195 |
Element coordinates = new Element("coordinates"); |
| 196 |
coordinates.addContent(""+ (parentLocation.getLongitude() + longNoise)+","+ (parentLocation.getLatitude() + latNoise)+"\r"); |
| 197 |
coordinates.addContent(""+ (location.getLongitude() + longNoise) +","+ (location.getLatitude() + latNoise) +"\r"); |
| 198 |
lineString.addContent(coordinates); |
| 199 |
|
| 200 |
branch.addContent(lineString); |
| 201 |
|
| 202 |
element.addContent(branch); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
return element; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
private int getIntegerNodeAttribute(Node node, String attributeName) { |
| 228 |
if (node.getAttribute(attributeName) == null) { |
| 229 |
throw new RuntimeException("Attribute, " + attributeName + ", missing from node"); |
| 230 |
} |
| 231 |
return (Integer)node.getAttribute(attributeName); |
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
private Element generateContainer(String elementTag, String name, String description, String styleURL) { |
| 236 |
Element element = new Element(elementTag); |
| 237 |
if (name != null) { |
| 238 |
element.addContent(generateElement("name", name)); |
| 239 |
} |
| 240 |
if (description != null) { |
| 241 |
element.addContent(generateElement("description", description)); |
| 242 |
} |
| 243 |
if (styleURL != null) { |
| 244 |
element.addContent(generateElement("styleUrl", styleURL)); |
| 245 |
} |
| 246 |
return element; |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
private Element generateElement(String elementName, String content) { |
| 251 |
Element e = new Element(elementName); |
| 252 |
e.addContent(content); |
| 253 |
return e; |
| 254 |
} |
| 255 |
|
| 256 |
private static Map<String, Location> readCoordinates(String fileName) { |
| 257 |
Map<String, Location> locationMap = new HashMap<String, Location>(); |
| 258 |
|
| 259 |
try { |
| 260 |
BufferedReader reader = new BufferedReader(new FileReader(fileName)); |
| 261 |
String line = reader.readLine(); |
| 262 |
while (line != null && line.trim().length() > 0) { |
| 263 |
String[] parts = line.split("\t"); |
| 264 |
Location location; |
| 265 |
if (parts.length == 4) { |
| 266 |
location = new Location(parts[0], parts[1], Double.parseDouble(parts[2]), Double.parseDouble(parts[3])); |
| 267 |
} else if (parts.length == 3) { |
| 268 |
location = new Location(parts[0], parts[0], Double.parseDouble(parts[1]), Double.parseDouble(parts[2])); |
| 269 |
} else { |
| 270 |
throw new RuntimeException("Wrong number of columns in coordinates file"); |
| 271 |
} |
| 272 |
|
| 273 |
locationMap.put(location.getState(), location); |
| 274 |
line = reader.readLine(); |
| 275 |
} |
| 276 |
} catch (IOException e) { |
| 277 |
e.printStackTrace(); |
| 278 |
} |
| 279 |
return locationMap; |
| 280 |
} |
| 281 |
|
| 282 |
int totalTrees = 0; |
| 283 |
int totalTreesUsed = 0; |
| 284 |
|
| 285 |
public static void printTitle() { |
| 286 |
System.out.println(); |
| 287 |
centreLine("TreeDensityKML " + version.getVersionString() + ", " + version.getDateString(), 60); |
| 288 |
centreLine("by", 60); |
| 289 |
centreLine("Andrew Rambaut", 60); |
| 290 |
System.out.println(); |
| 291 |
System.out.println(); |
| 292 |
} |
| 293 |
|
| 294 |
public static void centreLine(String line, int pageWidth) { |
| 295 |
int n = pageWidth - line.length(); |
| 296 |
int n1 = n / 2; |
| 297 |
for (int i = 0; i < n1; i++) { |
| 298 |
System.out.print(" "); |
| 299 |
} |
| 300 |
System.out.println(line); |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
public static void printUsage(Arguments arguments) { |
| 305 |
|
| 306 |
arguments.printUsage("TreeDensityKML", "<input-file-name> [<output-file-name>]"); |
| 307 |
System.out.println(); |
| 308 |
System.out.println(" Example: TreeDensityKML -burnin 100 test.trees test.kml"); |
| 309 |
System.out.println(); |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
public static void main(String[] args) throws IOException { |
| 314 |
|
| 315 |
String inputFileName = null; |
| 316 |
String outputFileName = null; |
| 317 |
|
| 318 |
printTitle(); |
| 319 |
|
| 320 |
Arguments arguments = new Arguments( |
| 321 |
new Arguments.Option[]{ |
| 322 |
new Arguments.IntegerOption("burnin", "the number of states to be considered as 'burn-in' [default = 0]"), |
| 323 |
new Arguments.IntegerOption("skip", "skip over this many trees per sample [default = 1]"), |
| 324 |
new Arguments.Option("help", "option to print this message") |
| 325 |
}); |
| 326 |
|
| 327 |
try { |
| 328 |
arguments.parseArguments(args); |
| 329 |
} catch (Arguments.ArgumentException ae) { |
| 330 |
System.out.println(ae); |
| 331 |
printUsage(arguments); |
| 332 |
System.exit(1); |
| 333 |
} |
| 334 |
|
| 335 |
if (arguments.hasOption("help")) { |
| 336 |
printUsage(arguments); |
| 337 |
System.exit(0); |
| 338 |
} |
| 339 |
|
| 340 |
int burnin = -1; |
| 341 |
if (arguments.hasOption("burnin")) { |
| 342 |
burnin = arguments.getIntegerOption("burnin"); |
| 343 |
} |
| 344 |
|
| 345 |
int skipEvery = 1; |
| 346 |
if (arguments.hasOption("skip")) { |
| 347 |
skipEvery = arguments.getIntegerOption("skip"); |
| 348 |
} |
| 349 |
|
| 350 |
String[] args2 = arguments.getLeftoverArguments(); |
| 351 |
|
| 352 |
if (args2.length > 2) { |
| 353 |
System.err.println("Unknown option: " + args2[2]); |
| 354 |
System.err.println(); |
| 355 |
printUsage(arguments); |
| 356 |
System.exit(1); |
| 357 |
} |
| 358 |
|
| 359 |
if (args2.length == 2) { |
| 360 |
inputFileName = args2[0]; |
| 361 |
outputFileName = args2[1]; |
| 362 |
} else { |
| 363 |
System.err.println("Missing input or output file name"); |
| 364 |
printUsage(arguments); |
| 365 |
System.exit(1); |
| 366 |
} |
| 367 |
|
| 368 |
new TreeDensityKML(burnin, |
| 369 |
skipEvery, |
| 370 |
inputFileName, |
| 371 |
outputFileName |
| 372 |
); |
| 373 |
|
| 374 |
System.exit(0); |
| 375 |
} |
| 376 |
|
| 377 |
} |