| 1 |
package test.dr.evomodel.branchratemodel; |
| 2 |
|
| 3 |
import dr.evolution.alignment.SitePatterns; |
| 4 |
import dr.evolution.datatype.Nucleotides; |
| 5 |
import dr.evolution.util.TaxonList; |
| 6 |
import dr.evomodel.branchratemodel.RandomLocalClockModel; |
| 7 |
import dr.evomodel.coalescent.CoalescentLikelihood; |
| 8 |
import dr.evomodel.coalescent.ConstantPopulationModel; |
| 9 |
import dr.evomodel.operators.ExchangeOperator; |
| 10 |
import dr.evomodel.operators.SubtreeSlideOperator; |
| 11 |
import dr.evomodel.operators.WilsonBalding; |
| 12 |
import dr.oldevomodel.sitemodel.GammaSiteModel; |
| 13 |
import dr.oldevomodel.substmodel.FrequencyModel; |
| 14 |
import dr.oldevomodel.substmodel.HKY; |
| 15 |
import dr.evomodel.tree.RateCovarianceStatistic; |
| 16 |
import dr.evomodel.tree.RateStatistic; |
| 17 |
import dr.oldevomodel.treelikelihood.TreeLikelihood; |
| 18 |
import dr.evomodelxml.branchratemodel.RandomLocalClockModelParser; |
| 19 |
import dr.evomodelxml.coalescent.ConstantPopulationModelParser; |
| 20 |
import dr.oldevomodelxml.sitemodel.GammaSiteModelParser; |
| 21 |
import dr.oldevomodelxml.substmodel.HKYParser; |
| 22 |
import dr.evomodelxml.tree.RateStatisticParser; |
| 23 |
import dr.oldevomodelxml.treelikelihood.TreeLikelihoodParser; |
| 24 |
import dr.inference.distribution.DistributionLikelihood; |
| 25 |
import dr.inference.loggers.ArrayLogFormatter; |
| 26 |
import dr.inference.loggers.MCLogger; |
| 27 |
import dr.inference.loggers.TabDelimitedFormatter; |
| 28 |
import dr.inference.mcmc.MCMC; |
| 29 |
import dr.inference.mcmc.MCMCOptions; |
| 30 |
import dr.inference.model.*; |
| 31 |
import dr.inference.operators.*; |
| 32 |
import dr.inference.trace.ArrayTraceList; |
| 33 |
import dr.inference.trace.Trace; |
| 34 |
import dr.inference.trace.TraceCorrelation; |
| 35 |
import dr.inferencexml.model.CompoundLikelihoodParser; |
| 36 |
import dr.math.MathUtils; |
| 37 |
import dr.math.distributions.GammaDistribution; |
| 38 |
import dr.math.distributions.PoissonDistribution; |
| 39 |
import junit.framework.Test; |
| 40 |
import junit.framework.TestSuite; |
| 41 |
import test.dr.inference.trace.TraceCorrelationAssert; |
| 42 |
|
| 43 |
import java.util.ArrayList; |
| 44 |
import java.util.List; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public class RandomLocalClockTestProblem extends TraceCorrelationAssert { |
| 53 |
|
| 54 |
public RandomLocalClockTestProblem(String name) { |
| 55 |
super(name); |
| 56 |
} |
| 57 |
|
| 58 |
public void setUp() throws Exception { |
| 59 |
super.setUp(); |
| 60 |
|
| 61 |
MathUtils.setSeed(666); |
| 62 |
|
| 63 |
createAlignment(PRIMATES_TAXON_SEQUENCE, Nucleotides.INSTANCE); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
public void testRandomLocalClock() throws Exception { |
| 68 |
Parameter popSize = new Parameter.Default(ConstantPopulationModelParser.POPULATION_SIZE, 0.077, 0, Double.POSITIVE_INFINITY); |
| 69 |
ConstantPopulationModel constantModel = createRandomInitialTree(popSize); |
| 70 |
|
| 71 |
CoalescentLikelihood coalescent = new CoalescentLikelihood(treeModel, null, new ArrayList<TaxonList>(), constantModel); |
| 72 |
coalescent.setId("coalescent"); |
| 73 |
|
| 74 |
|
| 75 |
Parameter ratesParameter = new Parameter.Default(RandomLocalClockModelParser.RATES, 10, 1); |
| 76 |
Parameter rateIndicatorParameter = new Parameter.Default(RandomLocalClockModelParser.RATE_INDICATORS, 10, 1); |
| 77 |
Parameter meanRateParameter = new Parameter.Default(RandomLocalClockModelParser.CLOCK_RATE, 1, 1.0); |
| 78 |
|
| 79 |
RandomLocalClockModel branchRateModel = new RandomLocalClockModel(treeModel, meanRateParameter, |
| 80 |
rateIndicatorParameter, ratesParameter, false, 0.5); |
| 81 |
|
| 82 |
SumStatistic rateChanges = new SumStatistic("rateChangeCount", true, null); |
| 83 |
rateChanges.addStatistic(rateIndicatorParameter); |
| 84 |
|
| 85 |
RateStatistic meanRate = new RateStatistic("meanRate", treeModel, branchRateModel, true, true, RateStatisticParser.MEAN); |
| 86 |
RateStatistic coefficientOfVariation = new RateStatistic(RateStatisticParser.COEFFICIENT_OF_VARIATION, treeModel, branchRateModel, |
| 87 |
true, true, RateStatisticParser.COEFFICIENT_OF_VARIATION); |
| 88 |
RateCovarianceStatistic covariance = new RateCovarianceStatistic("covariance", treeModel, branchRateModel); |
| 89 |
|
| 90 |
|
| 91 |
Parameter freqs = new Parameter.Default(alignment.getStateFrequencies()); |
| 92 |
Parameter kappa = new Parameter.Default(HKYParser.KAPPA, 1.0, 0, Double.POSITIVE_INFINITY); |
| 93 |
|
| 94 |
FrequencyModel f = new FrequencyModel(Nucleotides.INSTANCE, freqs); |
| 95 |
HKY hky = new HKY(kappa, f); |
| 96 |
|
| 97 |
|
| 98 |
GammaSiteModel siteModel = new GammaSiteModel(hky); |
| 99 |
Parameter mu = new Parameter.Default(GammaSiteModelParser.MUTATION_RATE, 1.0, 0, Double.POSITIVE_INFINITY); |
| 100 |
siteModel.setMutationRateParameter(mu); |
| 101 |
|
| 102 |
|
| 103 |
SitePatterns patterns = new SitePatterns(alignment, null, 0, -1, 1, true); |
| 104 |
|
| 105 |
TreeLikelihood treeLikelihood = new TreeLikelihood(patterns, treeModel, siteModel, branchRateModel, null, |
| 106 |
false, false, true, false, false); |
| 107 |
treeLikelihood.setId(TreeLikelihoodParser.TREE_LIKELIHOOD); |
| 108 |
|
| 109 |
|
| 110 |
OperatorSchedule schedule = new SimpleOperatorSchedule(); |
| 111 |
|
| 112 |
MCMCOperator operator = new ScaleOperator(kappa, 0.75); |
| 113 |
operator.setWeight(1.0); |
| 114 |
schedule.addOperator(operator); |
| 115 |
|
| 116 |
operator = new ScaleOperator(ratesParameter, 0.75); |
| 117 |
operator.setWeight(10.0); |
| 118 |
schedule.addOperator(operator); |
| 119 |
|
| 120 |
operator = new BitFlipOperator(rateIndicatorParameter, 15.0, true); |
| 121 |
schedule.addOperator(operator); |
| 122 |
|
| 123 |
operator = new ScaleOperator(popSize, 0.75); |
| 124 |
operator.setWeight(3.0); |
| 125 |
schedule.addOperator(operator); |
| 126 |
|
| 127 |
Parameter rootHeight = treeModel.getRootHeightParameter(); |
| 128 |
rootHeight.setId(TREE_HEIGHT); |
| 129 |
operator = new ScaleOperator(rootHeight, 0.75); |
| 130 |
operator.setWeight(3.0); |
| 131 |
schedule.addOperator(operator); |
| 132 |
|
| 133 |
Parameter internalHeights = treeModel.createNodeHeightsParameter(false, true, false); |
| 134 |
operator = new UniformOperator(internalHeights, 30.0); |
| 135 |
schedule.addOperator(operator); |
| 136 |
|
| 137 |
operator = new SubtreeSlideOperator(treeModel, 15.0, 0.0077, true, false, false, false, AdaptationMode.ADAPTATION_ON, AdaptableMCMCOperator.DEFAULT_ADAPTATION_TARGET); |
| 138 |
schedule.addOperator(operator); |
| 139 |
|
| 140 |
operator = new ExchangeOperator(ExchangeOperator.NARROW, treeModel, 15.0); |
| 141 |
|
| 142 |
schedule.addOperator(operator); |
| 143 |
|
| 144 |
operator = new ExchangeOperator(ExchangeOperator.WIDE, treeModel, 3.0); |
| 145 |
|
| 146 |
schedule.addOperator(operator); |
| 147 |
|
| 148 |
operator = new WilsonBalding(treeModel, 3.0); |
| 149 |
|
| 150 |
schedule.addOperator(operator); |
| 151 |
|
| 152 |
|
| 153 |
OneOnXPrior likelihood1 = new OneOnXPrior(); |
| 154 |
likelihood1.addData(popSize); |
| 155 |
OneOnXPrior likelihood2 = new OneOnXPrior(); |
| 156 |
likelihood2.addData(kappa); |
| 157 |
DistributionLikelihood likelihood3 = new DistributionLikelihood(new GammaDistribution(0.5, 2.0), 0.0); |
| 158 |
likelihood3.addData(ratesParameter); |
| 159 |
DistributionLikelihood likelihood4 = new DistributionLikelihood(new PoissonDistribution(1.0), 0.0); |
| 160 |
likelihood4.addData(rateChanges); |
| 161 |
|
| 162 |
List<Likelihood> likelihoods = new ArrayList<Likelihood>(); |
| 163 |
likelihoods.add(likelihood1); |
| 164 |
likelihoods.add(likelihood2); |
| 165 |
likelihoods.add(likelihood3); |
| 166 |
likelihoods.add(likelihood4); |
| 167 |
likelihoods.add(coalescent); |
| 168 |
Likelihood prior = new CompoundLikelihood(0, likelihoods); |
| 169 |
prior.setId(CompoundLikelihoodParser.PRIOR); |
| 170 |
|
| 171 |
likelihoods.clear(); |
| 172 |
likelihoods.add(treeLikelihood); |
| 173 |
Likelihood likelihood = new CompoundLikelihood(-1, likelihoods); |
| 174 |
|
| 175 |
likelihoods.clear(); |
| 176 |
likelihoods.add(prior); |
| 177 |
likelihoods.add(likelihood); |
| 178 |
Likelihood posterior = new CompoundLikelihood(0, likelihoods); |
| 179 |
posterior.setId(CompoundLikelihoodParser.POSTERIOR); |
| 180 |
|
| 181 |
|
| 182 |
ArrayLogFormatter formatter = new ArrayLogFormatter(false); |
| 183 |
|
| 184 |
MCLogger[] loggers = new MCLogger[2]; |
| 185 |
loggers[0] = new MCLogger(formatter, 1000, false); |
| 186 |
loggers[0].add(posterior); |
| 187 |
loggers[0].add(prior); |
| 188 |
loggers[0].add(treeLikelihood); |
| 189 |
loggers[0].add(rootHeight); |
| 190 |
loggers[0].add(kappa); |
| 191 |
|
| 192 |
loggers[0].add(rateChanges); |
| 193 |
loggers[0].add(coefficientOfVariation); |
| 194 |
loggers[0].add(covariance); |
| 195 |
loggers[0].add(popSize); |
| 196 |
loggers[0].add(coalescent); |
| 197 |
|
| 198 |
loggers[1] = new MCLogger(new TabDelimitedFormatter(System.out), 10000, false); |
| 199 |
loggers[1].add(posterior); |
| 200 |
loggers[1].add(treeLikelihood); |
| 201 |
loggers[1].add(rootHeight); |
| 202 |
loggers[1].add(meanRate); |
| 203 |
loggers[1].add(rateChanges); |
| 204 |
|
| 205 |
|
| 206 |
MCMC mcmc = new MCMC("mcmc1"); |
| 207 |
MCMCOptions options = new MCMCOptions(1000000); |
| 208 |
|
| 209 |
mcmc.setShowOperatorAnalysis(true); |
| 210 |
mcmc.init(options, posterior, schedule, loggers); |
| 211 |
mcmc.run(); |
| 212 |
|
| 213 |
|
| 214 |
System.out.println(mcmc.getTimer().toString()); |
| 215 |
|
| 216 |
|
| 217 |
List<Trace> traces = formatter.getTraces(); |
| 218 |
ArrayTraceList traceList = new ArrayTraceList("RandomLocalClockTest", traces, 0); |
| 219 |
|
| 220 |
for (int i = 1; i < traces.size(); i++) { |
| 221 |
traceList.analyseTrace(i); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
TraceCorrelation likelihoodStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(CompoundLikelihoodParser.POSTERIOR)); |
| 236 |
assertExpectation(CompoundLikelihoodParser.POSTERIOR, likelihoodStats, -1818.26); |
| 237 |
|
| 238 |
likelihoodStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(CompoundLikelihoodParser.PRIOR)); |
| 239 |
assertExpectation(CompoundLikelihoodParser.PRIOR, likelihoodStats, -2.70143); |
| 240 |
|
| 241 |
likelihoodStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TreeLikelihoodParser.TREE_LIKELIHOOD)); |
| 242 |
assertExpectation(TreeLikelihoodParser.TREE_LIKELIHOOD, likelihoodStats, -1815.56); |
| 243 |
|
| 244 |
TraceCorrelation treeHeightStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TREE_HEIGHT)); |
| 245 |
assertExpectation(TREE_HEIGHT, treeHeightStats, 6.363E-2); |
| 246 |
|
| 247 |
TraceCorrelation kappaStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(HKYParser.KAPPA)); |
| 248 |
assertExpectation(HKYParser.KAPPA, kappaStats, 30.0394); |
| 249 |
|
| 250 |
TraceCorrelation rateChangeStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("rateChangeCount")); |
| 251 |
assertExpectation("rateChangeCount", rateChangeStats, 0.40786); |
| 252 |
|
| 253 |
TraceCorrelation coefficientOfVariationStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(RateStatisticParser.COEFFICIENT_OF_VARIATION)); |
| 254 |
assertExpectation(RateStatisticParser.COEFFICIENT_OF_VARIATION, coefficientOfVariationStats, 7.02408E-2); |
| 255 |
|
| 256 |
TraceCorrelation covarianceStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("covariance")); |
| 257 |
assertExpectation("covariance", covarianceStats, 0.47952); |
| 258 |
|
| 259 |
TraceCorrelation popStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(ConstantPopulationModelParser.POPULATION_SIZE)); |
| 260 |
assertExpectation(ConstantPopulationModelParser.POPULATION_SIZE, popStats, 9.67405E-2); |
| 261 |
|
| 262 |
TraceCorrelation coalescentStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("coalescent")); |
| 263 |
assertExpectation("coalescent", coalescentStats, 7.29521); |
| 264 |
} |
| 265 |
|
| 266 |
public static Test suite() { |
| 267 |
return new TestSuite(RandomLocalClockTestProblem.class); |
| 268 |
} |
| 269 |
} |