/* * CompleteHistoryLogger.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.treelikelihood.utilities; import dr.evomodel.treelikelihood.MarkovJumpsBeagleTreeLikelihood; import dr.evomodel.treelikelihood.MarkovJumpsTraitProvider; import dr.evolution.tree.NodeRef; import dr.evolution.tree.Tree; import dr.evolution.tree.TreeTrait; import dr.inference.loggers.LogColumn; import dr.inference.loggers.Loggable; import dr.inference.markovjumps.StateHistory; import dr.util.Citable; import dr.util.Citation; import dr.util.CommonCitations; import java.awt.*; import java.io.Serializable; import java.util.*; import java.util.List; import java.util.logging.Logger; /** * A class to conveniently log a complete state history of a continuous-time Markov chain along a tree * simulated using the Uniformization Method *
* This work is supported by NSF grant 0856099 * * Minin VN and Suchard MA (2008) Counting labeled transitions in continous-time Markov models of evolution. * Journal of Mathematical Biology, 56, 391-412. * * Rodrigue N, Philippe H and Lartillot N (2006) Uniformization for sampling realizations of Markov processes: * applications to Bayesian implementations of codon substitution models. Bioinformatics, 24, 56-62. * * Hobolth A and Stone E (2009) Simulation from endpoint-conditioned, continuous-time Markov chains on a finite * state space, with applications to molecular evolution. Annals of Applied Statistics, 3, 1204-1231. * * @author Marc A. Suchard * @author Philippe Lemey * @author Andrew Rambaut */ public class CompleteHistoryLogger implements Loggable, Citable { public static final String TOTAL_COUNT_NAME = "totalChangeCount"; public static final String COMPLETE_HISTORY_NAME = "completeHistory"; public CompleteHistoryLogger(MarkovJumpsTraitProvider treeLikelihood, HistoryFilter filter, boolean internal, boolean external) { this.tree = treeLikelihood.getTreeModel(); this.patternCount = treeLikelihood.getPatternCount(); this.internal = internal; this.external = external; treeTraitHistory = new TreeTrait[patternCount]; for (int site = 0; site < patternCount; ++site) { String traitName = (patternCount == 1) ? MarkovJumpsBeagleTreeLikelihood.HISTORY : MarkovJumpsBeagleTreeLikelihood.HISTORY + "_" + (site + 1); treeTraitHistory[site] = treeLikelihood.getTreeTrait(traitName); if (treeTraitHistory[site] == null) { throw new RuntimeException("Tree '" + treeLikelihood.getId() + "' does not have a complete history trait at site " + (site + 1)); } } treeTraitCount = treeLikelihood.getTreeTrait(MarkovJumpsBeagleTreeLikelihood.TOTAL_COUNTS); if (treeTraitCount == null) { throw new RuntimeException("No sum"); } if (filter == null) { this.filter = new HistoryFilter.Default(); } else { this.filter = filter; Logger.getLogger("dr.app.beagle").info("\tWith filter: " + filter.getDescription() + "\n"); } } private static int parseListString(String listString, int currentOffset, List list) { while (currentOffset < listString.length()) { // Skip leading separators if (listString.startsWith(",", currentOffset)) { currentOffset++; } if (listString.startsWith("{", currentOffset)) { // Need to make a new list List newList = new ArrayList(); currentOffset = parseListString(listString, currentOffset + 1, newList); list.add(newList); } else if (listString.startsWith("}", currentOffset)) { // the list is ended return currentOffset + 1; } else { // Parse until next ',' or '}' int nextComma = listString.indexOf(",", currentOffset); int nextClose = listString.indexOf("}", currentOffset); if (nextComma < 0) nextComma = listString.length() - 1; if (nextClose < 0) nextClose = listString.length() - 1; int nextOffset = Math.min(nextComma, nextClose); list.add(listString.substring(currentOffset, nextOffset).trim()); currentOffset = nextOffset; } } return currentOffset; } public static Serializable parseValue(String value) { List nestedParse = new ArrayList