/* * DiscreteTreeToKML.java * * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard * * This file is part of BEAST. * See the NOTICE file distributed with this work for additional * information regarding copyright ownership and licensing. * * BEAST is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * BEAST is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with BEAST; if not, write to the * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, * Boston, MA 02110-1301 USA */ package dr.app.phylogeography.tools; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; import java.util.List; import java.util.StringTokenizer; import dr.app.util.Arguments; import jebl.evolution.io.ImportException; import jebl.evolution.io.NexusImporter; import jebl.evolution.io.TreeImporter; import jebl.evolution.trees.RootedTree; /** * @author Philippe Lemey * @author Andrew Rambaut * @author Marc A. Suchard */ public class DiscreteTreeToKML { public static final String HELP = "help"; public static final String ANNOTATION = "annotation"; public static final String COORDINATES = "coordinates"; public static final String TIMESCALER = "timescaler"; public static final String MRSD = "mrsd"; public static final String BWC = "bwc"; public static final String BWM = "bwm"; public static final String DIVIDER = "divider"; public static final String USP = "usp"; public static final String BW = "bw"; public static final String BCUSE = "bcuse"; public static final String BCOLOR = "bcolor"; public static final String BSTARTCOLOR = "bstartcolor"; public static final String BENDCOLOR = "bendcolor"; public static final String ARCHBRANCH = "archbranch"; public static final String ARCHHEIGHT = "archheight"; public static final String ALTITUDE = "altitude"; public static final String TEMP = "temp"; public static final String CIRCLESEGMENTS = "circlesegments"; public static final String RADIUS = "radius"; public static final String AUTORADIUS = "autoradius"; public static final String CIRCLEOP = "circleop"; public static final String COORDSFORTAXA = "coordsfortaxa"; public static final String SLICES = "slices"; public static final String SLICEBW = "slicebw"; public static final String SLICEMIDPOINT = "slicemidpoint"; public static final String[] use = new String[] {"heights","posteriors"}; public static final String[] arch = new String[] {"distance","time"}; public static final String[] falseTrue = new String[] {"false","true"}; private static final PrintStream progressStream = System.out; private static final String commandName = "discrete_tree_to_kml"; public static void printUsage(Arguments arguments) { arguments.printUsage(commandName, "[] []"); //TODO: set this right progressStream.println(); progressStream.println(" Example: " + commandName + " -coordinates coordinates.txt input.tre"); progressStream.println(); } private static int[] countLinesAndTokens(String coordinatesFileString){ int lineCounter = 0; int tokenCounter = 0; int[] container = new int[2]; try{ BufferedReader reader1 = new BufferedReader(new FileReader(coordinatesFileString)); String current1 = reader1.readLine(); while (current1 != null && !reader1.equals("")) { lineCounter++; if (lineCounter == 1) { StringTokenizer tokens = new StringTokenizer(current1); while (tokens.hasMoreTokens()) { tokenCounter++; tokens.nextToken(); } } current1 = reader1.readLine(); } } catch (IOException e) { System.err.println("Error reading " + coordinatesFileString); System.exit(1); } container[0] = lineCounter; container[1] = tokenCounter; return container; } private static void readLocationsCoordinates(String coordinatesFileString, String[][] locationsAndCoordinates){ try { BufferedReader reader2 = new BufferedReader(new FileReader(coordinatesFileString)); String current2 = reader2.readLine(); int counter2 = 0; while (current2 != null && !reader2.equals("")) { StringTokenizer tokens2 = new StringTokenizer(current2); for (int i = 0; i < locationsAndCoordinates[0].length; i++) { locationsAndCoordinates[counter2][i] = tokens2.nextToken(); progressStream.print(locationsAndCoordinates[counter2][i]+"\t"); } progressStream.print("\r"); counter2 ++; current2 = reader2.readLine(); } } catch (IOException e) { e.printStackTrace(); return; } } public static double[] parseVariableLengthDoubleArray(String inString) throws Arguments.ArgumentException { List returnList = new ArrayList(); StringTokenizer st = new StringTokenizer(inString,","); while(st.hasMoreTokens()) { try { returnList.add(Double.parseDouble(st.nextToken())); } catch (NumberFormatException e) { throw new Arguments.ArgumentException(); } } if (returnList.size()>0) { double[] doubleArray = new double[returnList.size()]; for(int i=0; i