/* * AntigenicPlotter.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.tools; import dr.app.beast.BeastVersion; import dr.app.util.Arguments; import dr.evolution.io.Importer; import dr.evolution.io.NexusImporter; import dr.evolution.tree.NodeRef; import dr.evolution.tree.Tree; import dr.geo.KMLCoordinates; import dr.geo.KernelDensityEstimator2D; import dr.geo.contouring.*; import dr.inference.trace.*; import dr.math.Procrustes; import dr.util.Version; import org.apache.commons.math.linear.*; import org.apache.commons.math.stat.StatUtils; import org.apache.commons.math.stat.correlation.Covariance; import org.jdom.Element; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; import javax.transaction.xa.Xid; import java.awt.geom.AffineTransform; import java.awt.geom.Point2D; import java.io.*; import java.util.*; /* * @author Marc Suchard and Andrew Rambaut */ public class AntigenicPlotter { private final static Version version = new BeastVersion(); public final static ContourMode CONTOUR_MODE = ContourMode.SNYDER; public final static double HPD_VALUE = 0.95; public static final int GRIDSIZE = 200; public AntigenicPlotter(int burnin, boolean tabFormat, boolean discreteModel, final String inputFileName, final String treeFileName, final String outputFileName ) throws IOException { double[][] reference = null; List tipLabels = null; if (treeFileName != null) { System.out.println("Reading tree file..."); NexusImporter importer = new NexusImporter(new FileReader(treeFileName)); try { Tree tree = importer.importNextTree(); reference = new double[tree.getExternalNodeCount()][2]; tipLabels = new ArrayList(); for (int i = 0; i < tree.getExternalNodeCount(); i++) { NodeRef tip = tree.getExternalNode(i); tipLabels.add(tree.getNodeTaxon(tip).getId()); reference[i][0] = (Double)tree.getNodeAttribute(tip, "antigenic1"); reference[i][1] = (Double)tree.getNodeAttribute(tip, "antigenic2"); } } catch (Importer.ImportException e) { e.printStackTrace(); return; } } System.out.println("Reading log file..."); FileReader fileReader = new FileReader(inputFileName); try { File file = new File(inputFileName); LogFileTraces traces = new LogFileTraces(inputFileName, file); traces.loadTraces(); if (burnin == -1) { burnin = (int) (traces.getMaxState() / 10); } traces.setBurnIn(burnin); System.out.println(); System.out.println("burnIn <= " + burnin); System.out.println("maxState = " + traces.getMaxState()); System.out.println(); int traceCount = traces.getTraceCount(); if (discreteModel) { // for the discrete model, there are 4 sets of traces, pairs coordinates, cluster allocations, and cluster sizes traceCount /= 4; } else { // for continuous, just pairs of coordinates traceCount /= 2; } int stateCount = traces.getStateCount(); double[][][] data; String[] labels = new String[traceCount]; if (tipLabels != null) { data = new double[stateCount][tipLabels.size()][2]; } else { data = new double[stateCount][traceCount][2]; } for (int i = 0; i < traceCount; i++) { String name = traces.getTraceName(i * 2); name = name.substring(0, name.length() - 1); if (tipLabels != null) { int index = tipLabels.indexOf(name); if (index != -1) { for (int j = 0; j < stateCount; j++) { data[j][index][0] = traces.getStateValue(i * 2, j); data[j][index][1] = traces.getStateValue((i * 2) + 1, j); } } } else { for (int j = 0; j < stateCount; j++) { data[j][i][0] = traces.getStateValue(i * 2, j); data[j][i][1] = traces.getStateValue((i * 2) + 1, j); } labels[i] = name; } } int[][] clusterIndices = null; int[][] clusterSizes = null; if (discreteModel) { clusterIndices = new int[stateCount][traceCount]; clusterSizes = new int[stateCount][traceCount]; for (int i = 0; i < traceCount; i++) { for (int j = 0; j < stateCount; j++) { clusterIndices[j][i] = (int)traces.getStateValue((traceCount * 2) + i, j); clusterSizes[j][i] = (int)traces.getStateValue((traceCount * 3) + i, j); } } Map clusterMap = new HashMap(); for (int i = 0; i < stateCount; i++) { BitSet[] clusters = new BitSet[clusterIndices[i].length]; for (int j = 0; j < clusterIndices[i].length; j++) { BitSet bits = clusters[clusterIndices[i][j]]; if (bits == null) { bits = new BitSet(); clusters[clusterIndices[i][j]] = bits; } bits.set(j); Integer count = clusterMap.get(bits); if (count == null) { count = 0; } clusterMap.put(bits, count+1); } Arrays.sort(clusters, new Comparator() { public int compare(BitSet bitSet1, BitSet bitSet2) { if (bitSet1 == null) { return -1; } if (bitSet2 == null) { return 1; } return bitSet2.cardinality() - bitSet1.cardinality(); } }); } for (BitSet bits : clusterMap.keySet()) { int count = clusterMap.get(bits); if (count > 1) { System.out.print(count); for (int i = bits.nextSetBit(0); i >= 0; i = bits.nextSetBit(i+1)) { System.out.print("\t" + labels[i]); } System.out.println(); } } } if (tipLabels != null) { labels = new String[tipLabels.size()]; tipLabels.toArray(labels); } if (reference != null) { procrustinate(data, reference); } else { procrustinate(data); } if (tabFormat) { writeTabformat(outputFileName, labels, data); } else { if (discreteModel) { writeKML(outputFileName, labels, data, clusterIndices, clusterSizes); } else { writeKML(outputFileName, labels, data); } } } catch (Exception e) { System.err.println("Error Parsing Input File: " + e.getMessage()); e.printStackTrace(System.err); return; } fileReader.close(); } private void procrustinate(final double[][][] data) { RealMatrix Xstar = new Array2DRowRealMatrix(data[data.length - 1]); for (int i = 0; i < data.length - 1; i++) { RealMatrix X = new Array2DRowRealMatrix(data[i]); RealMatrix Xnew = Procrustes.procrustinate(X, Xstar, true, true); data[i] = Xnew.getData(); } } private void procrustinate(final double[][][] data, final double[][] reference) { RealMatrix Xstar = new Array2DRowRealMatrix(reference); for (int i = 0; i < data.length; i++) { RealMatrix X = new Array2DRowRealMatrix(data[i]); RealMatrix Xnew = Procrustes.procrustinate(X, Xstar, true, true); data[i] = Xnew.getData(); } } private void writeTabformat(String fileName, String[] labels, double[][][] data) { int[] traceOrder = sortTraces(labels); try { PrintWriter writer = new PrintWriter(new FileWriter(fileName)); writer.print("state"); for (int j = 0; j < data[0].length; j++) { writer.print("\t"+labels[traceOrder[j]] + "_1"); writer.print("\t"+labels[traceOrder[j]] + "_2"); } writer.println(); for (int i = 0; i < data.length; i++) { writer.print(i); for (int j = 0; j < data[i].length; j++) { writer.print("\t" + data[i][traceOrder[j]][0]+"\t"+data[i][traceOrder[j]][1]); } writer.println(); } writer.close(); } catch (IOException e) { System.err.println("Error opening file: " + fileName); System.exit(-1); } } private void writeKML(String fileName, String[] labels, double[][][] data) { int[] traceOrder = sortTraces(labels); // Element hpdSchema = new Element("Schema"); // hpdSchema.setAttribute("id", "HPD_Schema"); // hpdSchema.addContent(new Element("SimpleField") // .setAttribute("name", "Label") // .setAttribute("type", "string") // .addContent(new Element("displayName").addContent("Label"))); // hpdSchema.addContent(new Element("SimpleField") // .setAttribute("name", "Point") // .setAttribute("type", "double") // .addContent(new Element("displayName").addContent("Point"))); // hpdSchema.addContent(new Element("SimpleField") // .setAttribute("name", "Year") // .setAttribute("type", "double") // .addContent(new Element("displayName").addContent("Year"))); // hpdSchema.addContent(new Element("SimpleField") // .setAttribute("name", "HPD") // .setAttribute("type", "double") // .addContent(new Element("displayName").addContent("HPD"))); Element traceSchema = new Element("Schema"); traceSchema.setAttribute("id", "Trace_Schema"); traceSchema.addContent(new Element("SimpleField") .setAttribute("name", "Label") .setAttribute("type", "string") .addContent(new Element("displayName").addContent("Label"))); traceSchema.addContent(new Element("SimpleField") .setAttribute("name", "Trace") .setAttribute("type", "double") .addContent(new Element("displayName").addContent("Trace"))); traceSchema.addContent(new Element("SimpleField") .setAttribute("name", "Year") .setAttribute("type", "double") .addContent(new Element("displayName").addContent("Year"))); traceSchema.addContent(new Element("SimpleField") .setAttribute("name", "State") .setAttribute("type", "double") .addContent(new Element("displayName").addContent("State"))); Element centroidSchema = new Element("Schema"); centroidSchema.setAttribute("id", "Centroid_Schema"); centroidSchema.addContent(new Element("SimpleField") .setAttribute("name", "Label") .setAttribute("type", "string") .addContent(new Element("displayName").addContent("Label"))); centroidSchema.addContent(new Element("SimpleField") .setAttribute("name", "Year") .setAttribute("type", "double") .addContent(new Element("displayName").addContent("Year"))); centroidSchema.addContent(new Element("SimpleField") .setAttribute("name", "Trace") .setAttribute("type", "double") .addContent(new Element("displayName").addContent("Trace"))); // final Element contourFolderElement = new Element("Folder"); // Element contourFolderNameElement = new Element("name"); // contourFolderNameElement.addContent("HPDs"); // contourFolderElement.addContent(contourFolderNameElement); final Element traceFolderElement = new Element("Folder"); Element traceFolderNameElement = new Element("name"); traceFolderNameElement.addContent("traces"); traceFolderElement.addContent(traceFolderNameElement); final Element centroidFolderElement = new Element("Folder"); Element centroidFolderNameElement = new Element("name"); centroidFolderNameElement.addContent("centroids"); centroidFolderElement.addContent(centroidFolderNameElement); Element documentNameElement = new Element("name"); String documentName = fileName; if (documentName.endsWith(".kml")) documentName = documentName.replace(".kml", ""); documentNameElement.addContent(documentName); final Element documentElement = new Element("Document"); documentElement.addContent(documentNameElement); documentElement.addContent(traceSchema); documentElement.addContent(centroidSchema); // documentElement.addContent(hpdSchema); documentElement.addContent(centroidFolderElement); documentElement.addContent(traceFolderElement); // documentElement.addContent(contourFolderElement); final Element rootElement = new Element("kml"); rootElement.addContent(documentElement); Element traceElement = generateTraceElement(labels, data, traceOrder); traceFolderElement.addContent(traceElement); Element centroidElement = generateCentroidElement(labels, data, traceOrder); centroidFolderElement.addContent(centroidElement); // Element contourElement = generateKDEElement(0.95, labels, data, traceOrder); // contourFolderElement.addContent(contourElement); PrintStream resultsStream; try { resultsStream = new PrintStream(new File(fileName)); XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat().setTextMode(Format.TextMode.PRESERVE)); xmlOutputter.output(rootElement, resultsStream); } catch (IOException e) { System.err.println("Error opening file: " + fileName); System.exit(-1); } } /** * Discrete KML * @param fileName * @param labels * @param data * @param clusterIndices * @param clusterSizes */ private void writeKML(String fileName, String[] labels, double[][][] data, int[][] clusterIndices, int[][] clusterSizes) { int[] traceOrder = sortTraces(labels); Element traceSchema = new Element("Schema"); traceSchema.setAttribute("id", "Cluster_Schema"); traceSchema.addContent(new Element("SimpleField") .setAttribute("name", "Label") .setAttribute("type", "string") .addContent(new Element("displayName").addContent("Label"))); traceSchema.addContent(new Element("SimpleField") .setAttribute("name", "Number") .setAttribute("type", "double") .addContent(new Element("displayName").addContent("Number"))); traceSchema.addContent(new Element("SimpleField") .setAttribute("name", "Year") .setAttribute("type", "double") .addContent(new Element("displayName").addContent("Year"))); traceSchema.addContent(new Element("SimpleField") .setAttribute("name", "State") .setAttribute("type", "double") .addContent(new Element("displayName").addContent("State"))); Element virusSchema = new Element("Schema"); virusSchema.setAttribute("id", "Virus_Schema"); virusSchema.addContent(new Element("SimpleField") .setAttribute("name", "Label") .setAttribute("type", "string") .addContent(new Element("displayName").addContent("Label"))); virusSchema.addContent(new Element("SimpleField") .setAttribute("name", "Year") .setAttribute("type", "double") .addContent(new Element("displayName").addContent("Year"))); virusSchema.addContent(new Element("SimpleField") .setAttribute("name", "Trace") .setAttribute("type", "double") .addContent(new Element("displayName").addContent("Trace"))); // final Element contourFolderElement = new Element("Folder"); // Element contourFolderNameElement = new Element("name"); // contourFolderNameElement.addContent("HPDs"); // contourFolderElement.addContent(contourFolderNameElement); final Element traceFolderElement = new Element("Folder"); Element traceFolderNameElement = new Element("name"); traceFolderNameElement.addContent("traces"); traceFolderElement.addContent(traceFolderNameElement); final Element clustersFolderElement = new Element("Folder"); Element clustersFolderNameElement = new Element("name"); clustersFolderNameElement.addContent("clusters"); clustersFolderElement.addContent(clustersFolderNameElement); Element documentNameElement = new Element("name"); String documentName = fileName; if (documentName.endsWith(".kml")) documentName = documentName.replace(".kml", ""); documentNameElement.addContent(documentName); final Element documentElement = new Element("Document"); documentElement.addContent(documentNameElement); documentElement.addContent(traceSchema); documentElement.addContent(virusSchema); // documentElement.addContent(hpdSchema); documentElement.addContent(clustersFolderElement); documentElement.addContent(traceFolderElement); // documentElement.addContent(contourFolderElement); final Element rootElement = new Element("kml"); rootElement.addContent(documentElement); Element traceElement = generateTraceElement(labels, data, traceOrder); traceFolderElement.addContent(traceElement); Element clustersElement = generateClusterElement(labels, data, clusterIndices, clusterSizes, traceOrder); clustersFolderElement.addContent(clustersElement); // Element contourElement = generateKDEElement(0.95, labels, data, traceOrder); // contourFolderElement.addContent(contourElement); PrintStream resultsStream; try { resultsStream = new PrintStream(new File(fileName)); XMLOutputter xmlOutputter = new XMLOutputter(Format.getPrettyFormat().setTextMode(Format.TextMode.PRESERVE)); xmlOutputter.output(rootElement, resultsStream); } catch (IOException e) { System.err.println("Error opening file: " + fileName); System.exit(-1); } } private int[] sortTraces(final String[] labels) { Map orderMap = new HashMap(); List