| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.evomodel.coalescent.structure; |
| 27 |
|
| 28 |
import dr.evolution.coalescent.structure.ColouredTreeIntervals; |
| 29 |
import dr.evolution.coalescent.structure.StructuredCoalescent; |
| 30 |
import dr.evolution.coalescent.structure.StructuredIntervalList; |
| 31 |
import dr.evolution.colouring.ColourChangeMatrix; |
| 32 |
import dr.evomodel.tree.TreeModel; |
| 33 |
import dr.inference.model.*; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
import java.util.logging.Logger; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
@Deprecated |
| 46 |
public class StructuredCoalescentLikelihood extends AbstractModelLikelihood { |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static final String STRUCTURED_COALESCENT_LIKELIHOOD = "structuredCoalescentLikelihood"; |
| 51 |
public static final String META_POPULATION_MODEL = "metaPopulationModel"; |
| 52 |
public static final String MIGRATION_MODEL = "migrationModel"; |
| 53 |
public static final String POPULATION = "population"; |
| 54 |
|
| 55 |
public StructuredCoalescentLikelihood(TreeModel treeModel, MetaPopulationModel metaPopulationModel, ColourSamplerModel colourSamplerModel, MigrationModel migrationModel) { |
| 56 |
this(STRUCTURED_COALESCENT_LIKELIHOOD, treeModel, metaPopulationModel, colourSamplerModel, migrationModel); |
| 57 |
} |
| 58 |
|
| 59 |
public StructuredCoalescentLikelihood(String name, TreeModel treeModel, MetaPopulationModel metaPopulationModel, ColourSamplerModel colourSamplerModel, MigrationModel migrationModel) { |
| 60 |
|
| 61 |
super(name); |
| 62 |
|
| 63 |
this.treeModel = treeModel; |
| 64 |
addModel(treeModel); |
| 65 |
|
| 66 |
this.metaPopulationModel = metaPopulationModel; |
| 67 |
addModel(metaPopulationModel); |
| 68 |
|
| 69 |
this.colourSamplerModel = colourSamplerModel; |
| 70 |
addModel(colourSamplerModel); |
| 71 |
|
| 72 |
this.migrationModel = migrationModel; |
| 73 |
addModel(migrationModel); |
| 74 |
|
| 75 |
addStatistic(migrationWaitingTimesStatistic); |
| 76 |
addStatistic(coalescentWaitingTimeStatistic); |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
protected final void handleModelChangedEvent(Model model, Object object, int index) { |
| 84 |
if (model == treeModel) { |
| 85 |
|
| 86 |
} else { |
| 87 |
|
| 88 |
} |
| 89 |
|
| 90 |
likelihoodKnown = false; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
protected final void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) { |
| 98 |
likelihoodKnown = false; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
protected final void storeState() { |
| 109 |
storedLogLikelihood = logLikelihood; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
protected final void restoreState() { |
| 116 |
logLikelihood = storedLogLikelihood; |
| 117 |
} |
| 118 |
|
| 119 |
protected final void acceptState() { |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
public final Model getModel() { |
| 127 |
return this; |
| 128 |
} |
| 129 |
|
| 130 |
public final double getLogLikelihood() { |
| 131 |
likelihoodKnown = false; |
| 132 |
|
| 133 |
if (!likelihoodKnown) { |
| 134 |
logLikelihood = calculateLogLikelihood(); |
| 135 |
likelihoodKnown = true; |
| 136 |
} |
| 137 |
return logLikelihood; |
| 138 |
} |
| 139 |
|
| 140 |
public final void makeDirty() { |
| 141 |
likelihoodKnown = false; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public double calculateLogLikelihood() { |
| 149 |
|
| 150 |
|
| 151 |
StructuredIntervalList list = new ColouredTreeIntervals(treeModel, colourSamplerModel.getTreeColouring()); |
| 152 |
|
| 153 |
ColourChangeMatrix mm = migrationModel.getMigrationMatrix(); |
| 154 |
|
| 155 |
StructuredCoalescent sc = new StructuredCoalescent(); |
| 156 |
|
| 157 |
double logL = sc.calculateLogLikelihood(colourSamplerModel.getTreeColouring(), list, mm, metaPopulationModel); |
| 158 |
|
| 159 |
return logL; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
private final Statistic migrationWaitingTimesStatistic = new Statistic.Abstract() { |
| 167 |
|
| 168 |
public String getStatisticName() { |
| 169 |
return "migrationWaitingTimes"; |
| 170 |
} |
| 171 |
|
| 172 |
public int getDimension() { |
| 173 |
return metaPopulationModel.getPopulationCount(); |
| 174 |
} |
| 175 |
|
| 176 |
public double getStatisticValue(int dim) { |
| 177 |
double[] migrationRates = migrationModel.getMigrationRates(0); |
| 178 |
return 1.0 / migrationRates[dim]; |
| 179 |
} |
| 180 |
|
| 181 |
}; |
| 182 |
|
| 183 |
private final Statistic coalescentWaitingTimeStatistic = new Statistic.Abstract() { |
| 184 |
|
| 185 |
public String getStatisticName() { |
| 186 |
return "coalescentWaitingTime"; |
| 187 |
} |
| 188 |
|
| 189 |
public int getDimension() { |
| 190 |
return 1; |
| 191 |
} |
| 192 |
|
| 193 |
public double getStatisticValue(int dim) { |
| 194 |
double[] popSizes = metaPopulationModel.getPopulationSizes(0); |
| 195 |
int[] counts = colourSamplerModel.getLeafColourCounts(); |
| 196 |
|
| 197 |
double sum = 0.0; |
| 198 |
for (int i = 0; i < popSizes.length; i++) { |
| 199 |
sum += ((double) counts[i]) / popSizes[i]; |
| 200 |
} |
| 201 |
|
| 202 |
return 1.0 / sum; |
| 203 |
} |
| 204 |
|
| 205 |
}; |
| 206 |
|
| 207 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 208 |
|
| 209 |
public String getParserName() { |
| 210 |
return STRUCTURED_COALESCENT_LIKELIHOOD; |
| 211 |
} |
| 212 |
|
| 213 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 214 |
|
| 215 |
TreeModel treeModel = (TreeModel) xo.getChild(TreeModel.class); |
| 216 |
|
| 217 |
ColourSamplerModel colourSamplerModel = (ColourSamplerModel) xo.getChild(ColourSamplerModel.class); |
| 218 |
|
| 219 |
MigrationModel migrationModel = (MigrationModel) xo.getChild(MigrationModel.class); |
| 220 |
|
| 221 |
MetaPopulationModel metaPopulationModel = (MetaPopulationModel) xo.getChild(MetaPopulationModel.class); |
| 222 |
|
| 223 |
StructuredCoalescentLikelihood likelihood = new StructuredCoalescentLikelihood(treeModel, metaPopulationModel, colourSamplerModel, migrationModel); |
| 224 |
|
| 225 |
Logger.getLogger("dr.evomodel").info("Creating structured coalescent tree prior."); |
| 226 |
|
| 227 |
return likelihood; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
public String getParserDescription() { |
| 235 |
return "This element represents a likelihood function for transmission."; |
| 236 |
} |
| 237 |
|
| 238 |
public Class getReturnType() { |
| 239 |
return StructuredCoalescentLikelihood.class; |
| 240 |
} |
| 241 |
|
| 242 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 243 |
return rules; |
| 244 |
} |
| 245 |
|
| 246 |
private final XMLSyntaxRule[] rules = { |
| 247 |
new ElementRule(TreeModel.class, "The tree."), |
| 248 |
new ElementRule(ColourSamplerModel.class, "The colour sampler model."), |
| 249 |
new ElementRule(MigrationModel.class, "The migration model."), |
| 250 |
new ElementRule(MetaPopulationModel.class, "The meta-population model."), |
| 251 |
}; |
| 252 |
}; |
| 253 |
|
| 254 |
private TreeModel treeModel = null; |
| 255 |
private MetaPopulationModel metaPopulationModel = null; |
| 256 |
private ColourSamplerModel colourSamplerModel = null; |
| 257 |
private MigrationModel migrationModel = null; |
| 258 |
|
| 259 |
private boolean likelihoodKnown = false; |
| 260 |
private double logLikelihood; |
| 261 |
private double storedLogLikelihood; |
| 262 |
} |