/* * GPSkytrackAnalysis.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.evomodel.coalescent; import dr.evolution.io.Importer; import dr.inference.model.Parameter; //import dr.evolution.io.NexusImporter; //import dr.evolution.io.TreeImporter; //import dr.evolution.tree.Tree; //import dr.inference.operators.AdaptationMode; //import dr.inference.trace.AbstractTraceList; import dr.inference.trace.LogFileTraces; import dr.inference.trace.TraceException; import dr.evomodel.coalescent.operators.GaussianProcessSkytrackBlockUpdateOperator; //import dr.inference.trace.TraceFactory; import dr.stats.DiscreteStatistics; //import dr.util.FileHelpers; //import dr.util.HeapSort; import dr.util.TabularData; //import no.uib.cipr.matrix.SymmTridiagEVD; //import no.uib.cipr.matrix.*; import java.io.*; //import java.io.PrintWriter; import java.util.Arrays; import java.util.StringTokenizer; /** * @author Joseph Heled */ public class GPSkytrackAnalysis extends TabularData { // TabularData private final double[] xPoints; private final double[] means; private final double[] medians; private final double[] hpdLower; private final double[] hpdHigh; private final double [][] gValues; private final double [][] tValues; private final double [][] newGvalues; private final double [][] popValues; // final File gvalues = FileHelpers.getFile("gvalues.txt"); // final File locations = FileHelpers.getFile("locations.txt"); // private final double[] HPDLevels; private Parameter numGridPoints; // each bin covers xPoints[-1]/coalBins.length // private int[] coalBins; // private final boolean quantiles; // GaussianProcessSkytrackLikelihood gpLikelihood = (GaussianProcessSkytrackLikelihood) xo.getChild(GaussianProcessSkytrackLikelihood.class); // return new GaussianProcessSkytrackBlockUpdateOperator(gpLikelihood, weight, mode, scaleFactor, // maxIterations, stopValue); // TODO: Error in loadTraces() because a String {..} is being converted to "real/double" // To make my life more miserable I will not use logFileTraces class and do it by hand public GPSkytrackAnalysis(File log, double burnIn, Parameter numGridPoints) throws IOException, Importer.ImportException, TraceException { GaussianProcessSkytrackBlockUpdateOperator GPOperator=new GaussianProcessSkytrackBlockUpdateOperator(); this.numGridPoints=numGridPoints; LogFileTraces ltraces = new LogFileTraces(log.getCanonicalPath(), log); // ltraces.changeTraceType(1, TraceFactory.TraceType.STRING); ltraces.loadTraces(); // System.exit(-1); ltraces.setBurnIn(0); final int runLengthIncludingBurnin = ltraces.getStateCount(); int intBurnIn = (int) Math.floor(burnIn < 1 ? runLengthIncludingBurnin * burnIn : burnIn); final int nStates = runLengthIncludingBurnin - intBurnIn; ltraces.setBurnIn(intBurnIn * ltraces.getStepSize()); assert ltraces.getStateCount() == nStates; xPoints = new double[(int) numGridPoints.getParameterValue(0)+1]; means = new double[(int) numGridPoints.getParameterValue(0)+1]; medians = new double[(int) numGridPoints.getParameterValue(0)+1]; hpdHigh = new double[(int) numGridPoints.getParameterValue(0)+1]; hpdLower = new double[(int) numGridPoints.getParameterValue(0)+1]; int numbPointsColumn = -1; int gvaluesColumn=-1; int xvaluesColumn=-1; int lambdaColumn = -1; int precColumn = -1; int tmrcaColumn=-1; for (int n = 0; n < ltraces.getTraceCount(); ++n) { final String traceName = ltraces.getTraceName(n); System.err.println(traceName); if (traceName.equals("skyride.points")) { numbPointsColumn = n; } else if (traceName.equals("skyride.lambda_bound")) { lambdaColumn = n; } else if (traceName.equals("skyride.precision")) { precColumn = n; } else if (traceName.equals("skyride.tmrca")) { tmrcaColumn = n; } else if (traceName.equals("changePoints")){ xvaluesColumn=n; } else if (traceName.equals("Gvalues")){ gvaluesColumn=n; } } // System.err.println("columns"+tmrcaColumn+" tmrca"+xvaluesColumn+" and"+gvaluesColumn); if (numbPointsColumn < 0 || lambdaColumn < 0 || precColumn<0 || tmrcaColumn<0 || xvaluesColumn<0 || gvaluesColumn<0) { throw new TraceException("incorrect trace column names: unable to find correct columns for summary"); } // TODO: Check if it is ok to define the grid from 0 to max(TMRCA) always double binSize = 0; // double hSum = -0; // System.err.println("states"+nStates); int [] numPoints = new int[nStates]; double[] lambda = new double[nStates]; double[] kappa= new double[nStates]; double tmrca=0; // double binSize=0; double tempTmrca=0.0; int maxpts=0; for (int ns = 0; ns < nStates; ++ns) { lambda[ns]= (Double) ltraces.getTrace(lambdaColumn).getValue(ns); numPoints[ns]=(int)Math.round((Double) ltraces.getTrace(numbPointsColumn).getValue(ns)); kappa[ns]=(Double) ltraces.getTrace(precColumn).getValue(ns); tempTmrca=(Double) ltraces.getTrace(tmrcaColumn).getValue(ns); // System.err.println(tempTmrca); System.exit(-1); if (tempTmrca>tmrca){tmrca=tempTmrca;} if (numPoints[ns]>maxpts) {maxpts=numPoints[ns];} } binSize = tmrca / numGridPoints.getParameterValue(0); xPoints[0]=0.0; for (int np=1;np