/* * CategoryOutbreak.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.epidemiology.casetocase; import dr.evolution.util.Date; import dr.evolution.util.Taxa; import dr.evolution.util.Taxon; import dr.evomodel.epidemiology.casetocase.periodpriors.AbstractPeriodPriorDistribution; import dr.inference.model.Model; import dr.inference.model.Parameter; import dr.inference.model.ProductStatistic; import dr.inference.model.Variable; import dr.xml.*; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; /** * Outbreak class for within-case coalescent. * * Each case belongs to an infectious (and latent) category which corresponds to one of a list of probability * distributions (most likely gamma or exponential) for the length of the infectious (latent) period. The XML rules for * the outbreak class ask for at least one ParametricDistributionModel. * Assignment of outbreak to distributions should be handled in whatever script or GUI writes the XML. * * Intended for situations where no data on infection times exists. * * User: Matthew Hall * Date: 17/12/2013 */ public class CategoryOutbreak extends AbstractOutbreak { public static final String CATEGORY_OUTBREAK = "categoryOutbreak"; private final HashSet latentCategories; private final HashSet infectiousCategories; private final HashMap latentMap; private final HashMap infectiousMap; private final HashMap weightMap; private double[][] distances; public CategoryOutbreak(String name, Taxa taxa, boolean hasGeography, boolean hasLatentPeriods, HashMap infectiousMap, HashMap latentMap){ super(name, taxa, hasLatentPeriods, hasGeography); cases = new ArrayList(); latentCategories = new HashSet(); infectiousCategories = new HashSet(); this.latentMap = latentMap; this.infectiousMap = infectiousMap; for(AbstractPeriodPriorDistribution hyperprior : infectiousMap.values()){ addModel(hyperprior); } for(Parameter hyperprior : latentMap.values()){ addVariable(hyperprior); } weightMap = new HashMap(); } private void addCase(String caseID, double endTime, Parameter coords, Parameter infectionPosition, Taxa associatedTaxa, double indexPriorWeight, String infectiousCategory, String latentCategory){ CategoryCase thisCase; if(latentCategory==null){ thisCase = new CategoryCase(caseID, endTime, coords, infectionPosition, associatedTaxa, indexPriorWeight, infectiousCategory); } else { thisCase = new CategoryCase(caseID, endTime, coords, infectionPosition, associatedTaxa, indexPriorWeight, infectiousCategory, latentCategory); latentCategories.add(latentCategory); } weightMap.put(thisCase, indexPriorWeight); infectiousCategories.add(infectiousCategory); cases.add(thisCase); infectedSize++; addModel(thisCase); } private void addNoninfectedCase(String caseID, Parameter coords){ CategoryCase thisCase = new CategoryCase(caseID, Double.POSITIVE_INFINITY, coords, null, null, 0.0, null); thisCase.setEverInfected(false); cases.add(thisCase); addModel(thisCase); } public HashMap getWeightMap(){ return weightMap; } public HashSet getLatentCategories(){ return latentCategories; } public HashSet getInfectiousCategories(){ return infectiousCategories; } public int getLatentCategoryCount(){ return latentCategories.size(); } public int getInfectiousCategoryCount(){ return infectiousCategories.size(); } public HashMap getLatentMap(){ return latentMap; } public HashMap getInfectiousMap(){ return infectiousMap; } public Parameter getLatentPeriod(String category){ return latentMap.get(category); } public AbstractPeriodPriorDistribution getInfectiousCategoryPrior(String category){ return infectiousMap.get(category); } public String getInfectiousCategory(AbstractCase aCase){ return ((CategoryCase)aCase).getInfectiousCategory(); } public String getLatentCategory(AbstractCase aCase){ return ((CategoryCase)aCase).getLatentCategory(); } public double getLatentPeriod(AbstractCase aCase) { return latentMap.get(((CategoryCase)aCase).getLatentCategory()).getParameterValue(0); } public double getDistance(AbstractCase a, AbstractCase b) { if(distances==null){ throw new RuntimeException("Distance matrix has not been initialised"); } return distances[getCaseIndex(a)][getCaseIndex(b)]; } private void setDistanceMatrix(double[][] distances){ this.distances = distances; } private void buildDistanceMatrix(){ distances = new double[cases.size()][cases.size()]; if(hasGeography){ for(int i=0; i examinationTimes; private CategoryCase(String name, String caseID, double endTime, Parameter coords, Parameter infectionBranchPosition, Taxa associatedTaxa, double indexPriorWeight, String infectiousCategory, String latentCategory){ super(name); wasEverInfected = associatedTaxa != null; this.caseID = caseID; this.infectiousCategory = infectiousCategory; this.infectionBranchPosition = infectionBranchPosition; if(infectionBranchPosition!=null) { addVariable(infectionBranchPosition); } endOfInfectiousTime = endTime; this.associatedTaxa = associatedTaxa; this.coords = coords; this.indexPriorWeight = indexPriorWeight; this.latentCategory = latentCategory; examinationTimes = new ArrayList(); if(wasEverInfected) { for (Taxon taxon : associatedTaxa) { examinationTimes.add(taxon.getDate()); } } } private CategoryCase(String name, String caseID, double endTime, Parameter coords, Parameter infectionBranchPosition, Taxa associatedTaxa, String infectiousCategory){ this(name, caseID, endTime, coords, infectionBranchPosition, associatedTaxa, 1.0, infectiousCategory, null); } private CategoryCase(String caseID, double endTime, Parameter coords, Parameter infectionBranchPosition, Taxa associatedTaxa, double indexPriorWeight, String infectiousCategory){ this(CATEGORY_CASE, caseID, endTime, coords, infectionBranchPosition, associatedTaxa, indexPriorWeight, infectiousCategory, null); } private CategoryCase(String caseID, double endTime, Parameter coords, Parameter infectionBranchPosition, Taxa associatedTaxa, double indexPriorWeight, String infectiousCategory, String latentCategory){ this(CATEGORY_CASE, caseID, endTime, coords, infectionBranchPosition, associatedTaxa, indexPriorWeight, infectiousCategory, latentCategory); } //noninfected susceptible constructor public String getLatentCategory(){ return latentCategory; } public String getInfectiousCategory(){ return infectiousCategory; } public double getIndexPriorWeight() { return indexPriorWeight;} public boolean noninfectiousYet(double time) { return time>endOfInfectiousTime; } public ArrayList getExaminationTimes(){ return examinationTimes; } protected void handleModelChangedEvent(Model model, Object object, int index) { fireModelChanged(); } protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { fireModelChanged(this); } protected void storeState() { // nothing to do? } protected void restoreState() { // nothing to do? } protected void acceptState() { //nothing to do? } public double[] getCoords() { return new double[]{coords.getParameterValue(0), coords.getParameterValue(1)}; } } public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { //for the outbreak public static final String HAS_GEOGRAPHY = "hasGeography"; public static final String HAS_LATENT_PERIODS = "hasLatentPeriods"; public static final String INFECTIOUS_PERIOD_PRIOR = "infectiousPeriodPrior"; public static final String LATENT_PERIODS = "latentPeriods"; public static final String DISTANCE_MATRIX = "distanceMatrix"; //for the cases public static final String CASE_ID = "hostID"; public static final String END_TIME = "endTime"; public static final String COORDINATES = "spatialCoordinates"; public static final String INFECTION_TIME_BRANCH_POSITION = "infectionTimeBranchPosition"; public static final String LATENT_CATEGORY = "latentCategory"; public static final String INFECTIOUS_CATEGORY = "infectiousCategory"; public static final String WAS_EVER_INFECTED = "wasEverInfected"; public static final String INDEX_PRIOR_WEIGHT = "indexPriorWeight"; //for the normal-gamma priors public Object parseXMLObject(XMLObject xo) throws XMLParseException { final boolean hasGeography = xo.hasAttribute(HAS_GEOGRAPHY) && xo.getBooleanAttribute(HAS_GEOGRAPHY); final boolean hasLatentPeriods = Boolean.parseBoolean((String)xo.getAttribute(HAS_LATENT_PERIODS)); final Taxa taxa = (Taxa) xo.getChild(Taxa.class); HashMap infMap = new HashMap(); HashMap latMap = new HashMap(); for(int i=0; i