| 1 |
package test.dr.calibration; |
| 2 |
|
| 3 |
|
| 4 |
import dr.evolution.coalescent.DemographicFunction; |
| 5 |
import dr.evolution.tree.SimpleNode; |
| 6 |
import dr.evolution.tree.SimpleTree; |
| 7 |
import dr.evolution.tree.Tree; |
| 8 |
import dr.evolution.tree.TreeUtils; |
| 9 |
import dr.evolution.util.Taxa; |
| 10 |
import dr.evolution.util.Taxon; |
| 11 |
import dr.evolution.util.TaxonList; |
| 12 |
import dr.evolution.util.Units; |
| 13 |
import dr.evomodel.coalescent.ConstantPopulationModel; |
| 14 |
import dr.evomodel.coalescent.DemographicModel; |
| 15 |
import dr.evomodel.operators.SubtreeSlideOperator; |
| 16 |
import dr.evomodel.speciation.BirthDeathGernhard08Model; |
| 17 |
import dr.evomodel.speciation.SpeciationLikelihood; |
| 18 |
import dr.evomodel.speciation.SpeciationModel; |
| 19 |
import dr.evomodel.tree.*; |
| 20 |
import dr.evomodelxml.coalescent.ConstantPopulationModelParser; |
| 21 |
import dr.evomodelxml.tree.TreeModelParser; |
| 22 |
import dr.inference.distribution.DistributionLikelihood; |
| 23 |
import dr.inference.loggers.ArrayLogFormatter; |
| 24 |
import dr.inference.loggers.MCLogger; |
| 25 |
import dr.inference.mcmc.MCMC; |
| 26 |
import dr.inference.mcmc.MCMCOptions; |
| 27 |
import dr.inference.model.BooleanLikelihood; |
| 28 |
import dr.inference.model.CompoundLikelihood; |
| 29 |
import dr.inference.model.Likelihood; |
| 30 |
import dr.inference.model.Parameter; |
| 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.distributions.LogNormalDistribution; |
| 37 |
import dr.util.NumberFormatter; |
| 38 |
|
| 39 |
import java.io.BufferedWriter; |
| 40 |
import java.io.FileWriter; |
| 41 |
import java.io.IOException; |
| 42 |
import java.text.NumberFormat; |
| 43 |
import java.util.ArrayList; |
| 44 |
import java.util.List; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class TestCalibratedYuleModel { |
| 51 |
protected static final String TL = "TL"; |
| 52 |
protected static final String TREE_HEIGHT = TreeModel.TREE_MODEL + "." + TreeModelParser.ROOT_HEIGHT; |
| 53 |
|
| 54 |
private final BufferedWriter out; |
| 55 |
Taxa taxa; |
| 56 |
private static final double M = 1 + Math.log(10); |
| 57 |
|
| 58 |
public TestCalibratedYuleModel(int treeSize, double S, int chainLength, BufferedWriter out) throws Exception { |
| 59 |
|
| 60 |
this.out = out; |
| 61 |
|
| 62 |
|
| 63 |
TreeModel treeModel = createTreeModel(treeSize); |
| 64 |
|
| 65 |
Parameter brParameter = new Parameter.Default("birthRate", 2.0, 0.0, 100.0); |
| 66 |
|
| 67 |
OperatorSchedule schedule = new SimpleOperatorSchedule(); |
| 68 |
MCMCOperator operator = new SubtreeSlideOperator(treeModel, 10, 1, true, false, false, false, AdaptationMode.ADAPTATION_ON, AdaptableMCMCOperator.DEFAULT_ADAPTATION_TARGET); |
| 69 |
schedule.addOperator(operator); |
| 70 |
|
| 71 |
operator = new ScaleOperator(brParameter, 0.5); |
| 72 |
operator.setWeight(1.0); |
| 73 |
schedule.addOperator(operator); |
| 74 |
|
| 75 |
System.out.println("treeModel = " + TreeUtils.newickNoLengths(treeModel)); |
| 76 |
|
| 77 |
|
| 78 |
out.write(TreeUtils.newickNoLengths(treeModel)); |
| 79 |
out.write("\t"); |
| 80 |
yuleTester(treeModel, schedule, brParameter, S, chainLength); |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
protected TreeModel createTreeModel(int treeSize) throws Exception { |
| 86 |
taxa = new Taxa(); |
| 87 |
for (int i = 0; i < treeSize; i++) { |
| 88 |
taxa.addTaxon(new Taxon("T" + Integer.toString(i))); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
Parameter popSize = new Parameter.Default(treeSize); |
| 94 |
popSize.setId(ConstantPopulationModelParser.POPULATION_SIZE); |
| 95 |
ConstantPopulationModel startingTree = new ConstantPopulationModel(popSize, Units.Type.YEARS); |
| 96 |
|
| 97 |
Tree tree = calibration(taxa, startingTree); |
| 98 |
|
| 99 |
return new TreeModel(tree); |
| 100 |
} |
| 101 |
|
| 102 |
private Tree calibration(final TaxonList taxa, DemographicModel demoModel) throws Exception { |
| 103 |
|
| 104 |
dr.evolution.coalescent.CoalescentSimulator simulator = new dr.evolution.coalescent.CoalescentSimulator(); |
| 105 |
|
| 106 |
DemographicFunction demoFunction = demoModel.getDemographicFunction(); |
| 107 |
|
| 108 |
SimpleNode[] firstHalfNodes = new SimpleNode[taxa.getTaxonCount() / 2]; |
| 109 |
SimpleNode[] secondHalfNodes = new SimpleNode[taxa.getTaxonCount() - taxa.getTaxonCount() / 2]; |
| 110 |
for (int i = 0; i < firstHalfNodes.length; i++) { |
| 111 |
firstHalfNodes[i] = new SimpleNode(); |
| 112 |
firstHalfNodes[i].setTaxon(taxa.getTaxon(i)); |
| 113 |
} |
| 114 |
for (int i = 0; i < secondHalfNodes.length; i++) { |
| 115 |
secondHalfNodes[i] = new SimpleNode(); |
| 116 |
secondHalfNodes[i].setTaxon(taxa.getTaxon(i + taxa.getTaxonCount() / 2)); |
| 117 |
} |
| 118 |
|
| 119 |
SimpleNode firstHalfRootNode = simulator.simulateCoalescent(firstHalfNodes, demoFunction); |
| 120 |
|
| 121 |
SimpleNode[] restNodes = simulator.simulateCoalescent(secondHalfNodes, demoFunction, 0, firstHalfRootNode.getHeight()); |
| 122 |
|
| 123 |
SimpleNode[] together = new SimpleNode[restNodes.length + 1]; |
| 124 |
for (int i = 0; i < restNodes.length; i++) { |
| 125 |
together[i + 1] = restNodes[i]; |
| 126 |
} |
| 127 |
together[0] = firstHalfRootNode; |
| 128 |
|
| 129 |
SimpleNode root = simulator.simulateCoalescent(together, demoFunction); |
| 130 |
|
| 131 |
Tree tree = new SimpleTree(root); |
| 132 |
|
| 133 |
return tree; |
| 134 |
} |
| 135 |
|
| 136 |
private void yuleTester(TreeModel treeModel, OperatorSchedule schedule, Parameter brParameter, double S, int chainLength) |
| 137 |
throws IOException, TreeUtils.MissingTaxonException { |
| 138 |
|
| 139 |
MCMC mcmc = new MCMC("mcmc1"); |
| 140 |
MCMCOptions options = new MCMCOptions(chainLength); |
| 141 |
|
| 142 |
TreeLengthStatistic tls = new TreeLengthStatistic(TL, treeModel); |
| 143 |
TreeHeightStatistic rootHeight = new TreeHeightStatistic(TREE_HEIGHT, treeModel); |
| 144 |
|
| 145 |
SpeciationModel speciationModel = new BirthDeathGernhard08Model("yule", brParameter, null, null, |
| 146 |
BirthDeathGernhard08Model.TreeType.UNSCALED, Units.Type.SUBSTITUTIONS, false); |
| 147 |
Likelihood speciationLikelihood = new SpeciationLikelihood(treeModel, speciationModel, "yule.like"); |
| 148 |
|
| 149 |
Taxa halfTaxa = new Taxa(); |
| 150 |
for (int i = 0; i < taxa.getTaxonCount() / 2; i++) { |
| 151 |
halfTaxa.addTaxon(new Taxon("T" + Integer.toString(i))); |
| 152 |
} |
| 153 |
|
| 154 |
TMRCAStatistic tmrca = new TMRCAStatistic("tmrca(halfTaxa)", treeModel, halfTaxa, false, false); |
| 155 |
DistributionLikelihood logNormalLikelihood = new DistributionLikelihood( |
| 156 |
new LogNormalDistribution(M, S), 0); |
| 157 |
logNormalLikelihood.addData(tmrca); |
| 158 |
|
| 159 |
MonophylyStatistic monophylyStatistic = new MonophylyStatistic("monophyly(halfTaxa)", treeModel, halfTaxa, null); |
| 160 |
BooleanLikelihood booleanLikelihood = new BooleanLikelihood(); |
| 161 |
booleanLikelihood.addData(monophylyStatistic); |
| 162 |
|
| 163 |
|
| 164 |
List<Likelihood> likelihoods = new ArrayList<Likelihood>(); |
| 165 |
likelihoods.add(speciationLikelihood); |
| 166 |
likelihoods.add(logNormalLikelihood); |
| 167 |
likelihoods.add(booleanLikelihood); |
| 168 |
Likelihood prior = new CompoundLikelihood(0, likelihoods); |
| 169 |
prior.setId(CompoundLikelihoodParser.PRIOR); |
| 170 |
|
| 171 |
ArrayLogFormatter logformatter = new ArrayLogFormatter(false); |
| 172 |
|
| 173 |
MCLogger[] loggers = new MCLogger[1]; |
| 174 |
loggers[0] = new MCLogger(logformatter, (int)(options.getChainLength() / 10000), false); |
| 175 |
loggers[0].add(speciationLikelihood); |
| 176 |
loggers[0].add(rootHeight); |
| 177 |
loggers[0].add(tmrca); |
| 178 |
loggers[0].add(tls); |
| 179 |
loggers[0].add(brParameter); |
| 180 |
|
| 181 |
mcmc.setShowOperatorAnalysis(false); |
| 182 |
|
| 183 |
mcmc.init(options, prior, schedule, loggers); |
| 184 |
mcmc.run(); |
| 185 |
|
| 186 |
List<Trace> traces = logformatter.getTraces(); |
| 187 |
ArrayTraceList traceList = new ArrayTraceList("yuleModelTest", traces, 1000); |
| 188 |
|
| 189 |
for (int i = 1; i < traces.size(); i++) { |
| 190 |
traceList.analyseTrace(i); |
| 191 |
} |
| 192 |
|
| 193 |
NumberFormatter formatter = new NumberFormatter(8); |
| 194 |
|
| 195 |
TraceCorrelation tlStats = traceList.getCorrelationStatistics(traceList.getTraceIndex(TL)); |
| 196 |
TraceCorrelation treeHeightStats = traceList.getCorrelationStatistics(traceList.getTraceIndex("tmrca(halfTaxa)")); |
| 197 |
|
| 198 |
out.write(formatter.format(treeHeightStats.getMean())); |
| 199 |
out.write("\t"); |
| 200 |
|
| 201 |
double expectedNodeHeight = Math.pow(Math.E, (M + (Math.pow(S, 2) / 2))); |
| 202 |
|
| 203 |
out.write(formatter.format(expectedNodeHeight)); |
| 204 |
out.write("\t"); |
| 205 |
|
| 206 |
double error = Math.abs((treeHeightStats.getMean() - expectedNodeHeight) / expectedNodeHeight); |
| 207 |
NumberFormat percentFormatter = NumberFormat.getNumberInstance(); |
| 208 |
percentFormatter.setMinimumFractionDigits(5); |
| 209 |
percentFormatter.setMinimumFractionDigits(5); |
| 210 |
|
| 211 |
out.write(percentFormatter.format(error)); |
| 212 |
out.write("\t"); |
| 213 |
|
| 214 |
out.write(Double.toString(tlStats.getESS())); |
| 215 |
|
| 216 |
System.out.println("tmrcaHeight = " + formatter.format(treeHeightStats.getMean()) |
| 217 |
+ "; expectation = " + formatter.format(expectedNodeHeight) |
| 218 |
+ "; error = " + percentFormatter.format(error) |
| 219 |
+ "; tl.ess = " + tlStats.getESS()); |
| 220 |
|
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
public static void main(String[] args) { |
| 225 |
try { |
| 226 |
System.out.println("M = " + M); |
| 227 |
BufferedWriter out = new BufferedWriter(new FileWriter("TestCalibratedYuleModel.txt")); |
| 228 |
out.write("M = \t" + M); |
| 229 |
out.newLine(); |
| 230 |
out.write("chainLeng\ttreeSize\tS\ttreeModel\ttmrcaHeight\texpectation\terror\ttl.ess"); |
| 231 |
out.newLine(); |
| 232 |
|
| 233 |
int[] taxaSchedule = new int[]{4, 8, 16, 32, 48, 64}; |
| 234 |
int[] chainLengthSchedule = new int[]{2000000, 2000000, 4000000, 8000000, 10000000, 10000000}; |
| 235 |
|
| 236 |
if (taxaSchedule.length != chainLengthSchedule.length) throw new Exception (); |
| 237 |
|
| 238 |
double[] S_Schedule = new double[]{0.05, 0.1, 0.2, 0.4}; |
| 239 |
|
| 240 |
for (double S : S_Schedule) { |
| 241 |
|
| 242 |
for (int i = 0; i < taxaSchedule.length; i++) { |
| 243 |
|
| 244 |
int n = taxaSchedule[i]; |
| 245 |
int chainLength = chainLengthSchedule[i]; |
| 246 |
|
| 247 |
System.out.print("chainLeng = " + chainLengthSchedule[i] + "\tS = " + S + "\ttreeSize = " + n + "\t"); |
| 248 |
out.write(chainLengthSchedule[i] + "\t" + S + "\t" + n + "\t"); |
| 249 |
TestCalibratedYuleModel testCalibratedYuleModel = new TestCalibratedYuleModel(n, S, chainLength, out); |
| 250 |
out.newLine(); |
| 251 |
} |
| 252 |
} |
| 253 |
out.close(); |
| 254 |
} catch (Exception e) { |
| 255 |
e.printStackTrace(); |
| 256 |
} |
| 257 |
} |
| 258 |
} |