Debian Codegraph @

src/dr/app/beauti/generator/TreePriorGenerator.java

raw symbols SCIP
1 /*
2 * TreePriorGenerator.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.app.beauti.generator;
27
28 import dr.app.beauti.components.ComponentFactory;
29 import dr.app.beauti.options.*;
30 import dr.app.beauti.types.StartingTreeType;
31 import dr.app.beauti.types.TreePriorParameterizationType;
32 import dr.app.beauti.types.TreePriorType;
33 import dr.app.beauti.util.XMLWriter;
34 import dr.evolution.util.Taxa;
35 import dr.evolution.util.Units;
36 import dr.evomodel.tree.TreeModel;
37 import dr.evomodelxml.CSVExporterParser;
38 import dr.evomodelxml.coalescent.*;
39 import dr.evomodelxml.speciation.*;
40 import dr.evoxml.TaxaParser;
41 import dr.inference.distribution.ExponentialDistributionModel;
42 import dr.inference.distribution.ExponentialMarkovModel;
43 import dr.inference.model.ParameterParser;
44 import dr.inferencexml.distribution.DistributionModelParser;
45 import dr.inferencexml.distribution.ExponentialMarkovModelParser;
46 import dr.inferencexml.distribution.MixedDistributionLikelihoodParser;
47 import dr.inferencexml.model.SumStatisticParser;
48 import dr.util.Attribute;
49 import dr.xml.XMLParser;
50
51 /**
52 * @author Alexei Drummond
53 * @author Andrew Rambaut
54 */
55 public class TreePriorGenerator extends Generator {
56
57 public TreePriorGenerator(BeautiOptions options, ComponentFactory[] components) {
58 super(options, components);
59 }
60
61 // void writeTreePrior(PartitionTreePrior prior, PartitionTreeModel model, XMLWriter writer) { // for species, partitionName.treeModel
62 // setModelPrefix(prior.getPrefix()); // only has prefix, if (options.getPartitionTreePriors().size() > 1)
63 //
64 // writePriorLikelihood(prior, model, writer);
65 // }
66
67 /**
68 * Write a tree prior (coalescent or speciational) model
69 *
70 * @param prior the partition tree prior
71 * @param writer the writer
72 */
73 void writeTreePriorModel(PartitionTreePrior prior, XMLWriter writer) {
74
75 String prefix = prior.getPrefix();
76
77 String initialPopSize = null;
78
79 TreePriorType nodeHeightPrior = prior.getNodeHeightPrior();
80 Units.Type units = options.units;
81 TreePriorParameterizationType parameterization = prior.getParameterization();
82
83 switch (nodeHeightPrior) {
84 case CONSTANT:
85 writer.writeComment("A prior assumption that the population size has remained constant",
86 "throughout the time spanned by the genealogy.");
87 writer.writeOpenTag(
88 ConstantPopulationModelParser.CONSTANT_POPULATION_MODEL,
89 new Attribute[]{
90 new Attribute.Default<String>(XMLParser.ID, prefix + "constant"),
91 new Attribute.Default<String>("units", Units.Utils.getDefaultUnitName(options.units))
92 }
93 );
94
95 writer.writeOpenTag(ConstantPopulationModelParser.POPULATION_SIZE);
96 writeParameter("constant.popSize", prior, writer);
97 writer.writeCloseTag(ConstantPopulationModelParser.POPULATION_SIZE);
98 writer.writeCloseTag(ConstantPopulationModelParser.CONSTANT_POPULATION_MODEL);
99
100 break;
101
102 case EXPONENTIAL:
103 // generate an exponential prior tree
104
105 writer.writeComment("A prior assumption that the population size has grown exponentially",
106 "throughout the time spanned by the genealogy.");
107 writer.writeOpenTag(
108 ExponentialGrowthModelParser.EXPONENTIAL_GROWTH_MODEL,
109 new Attribute[]{
110 new Attribute.Default<String>(XMLParser.ID, prefix + "exponential"),
111 new Attribute.Default<String>("units", Units.Utils.getDefaultUnitName(options.units))
112 }
113 );
114
115 // write pop size socket
116 writer.writeOpenTag(ExponentialGrowthModelParser.POPULATION_SIZE);
117 writeParameter("exponential.popSize", prior, writer);
118 writer.writeCloseTag(ExponentialGrowthModelParser.POPULATION_SIZE);
119
120 if (parameterization == TreePriorParameterizationType.GROWTH_RATE) {
121 // write growth rate socket
122 writer.writeOpenTag(ExponentialGrowthModelParser.GROWTH_RATE);
123 writeParameter("exponential.growthRate", prior, writer);
124 writer.writeCloseTag(ExponentialGrowthModelParser.GROWTH_RATE);
125 } else {
126 // write doubling time socket
127 writer.writeOpenTag(ExponentialGrowthModelParser.DOUBLING_TIME);
128 writeParameter("exponential.doublingTime", prior, writer);
129 writer.writeCloseTag(ExponentialGrowthModelParser.DOUBLING_TIME);
130 }
131
132 writer.writeCloseTag(ExponentialGrowthModelParser.EXPONENTIAL_GROWTH_MODEL);
133
134 break;
135
136 case LOGISTIC:
137 // generate an exponential prior tree
138 writer.writeComment("A prior assumption that the population size has grown logistically",
139 "throughout the time spanned by the genealogy.");
140 writer.writeOpenTag(
141 LogisticGrowthModelParser.LOGISTIC_GROWTH_MODEL,
142 new Attribute[]{
143 new Attribute.Default<String>(XMLParser.ID, prefix + "logistic"),
144 new Attribute.Default<String>("units", Units.Utils.getDefaultUnitName(options.units))
145 }
146 );
147
148 // write pop size socket
149 writer.writeOpenTag(LogisticGrowthModelParser.POPULATION_SIZE);
150 writeParameter("logistic.popSize", prior, writer);
151 writer.writeCloseTag(LogisticGrowthModelParser.POPULATION_SIZE);
152
153 if (parameterization == TreePriorParameterizationType.GROWTH_RATE) {
154 // write growth rate socket
155 writer.writeOpenTag(LogisticGrowthModelParser.GROWTH_RATE);
156 writeParameter("logistic.growthRate", prior, writer);
157 writer.writeCloseTag(LogisticGrowthModelParser.GROWTH_RATE);
158 } else {
159 // write doubling time socket
160 writer.writeOpenTag(LogisticGrowthModelParser.DOUBLING_TIME);
161 writeParameter("logistic.doublingTime", prior, writer);
162 writer.writeCloseTag(LogisticGrowthModelParser.DOUBLING_TIME);
163 }
164
165 // write logistic t50 socket
166 writer.writeOpenTag(LogisticGrowthModelParser.TIME_50);
167
168 // if (options.clockModelOptions.getRateOptionClockModel() == FixRateType.FIX_MEAN
169 // || options.clockModelOptions.getRateOptionClockModel() == FixRateType.RELATIVE_TO) {
170 // writer.writeComment("No calibration");
171 // writer.writeComment("logistic.t50 initial always has to < treeRootHeight initial");
172 // dr.app.beauti.options.Parameter priorPara = prior.getParameter("logistic.t50");
173 //
174 // double initRootHeight;
175 // if (options.isShareSameTreePrior()) {
176 // initRootHeight = priorPara.initial;
177 // for (PartitionTreeModel tree : options.getPartitionTreeModels()) {
178 // double tmpRootHeight = tree.getParameter("treeModel.rootHeight").initial;
179 // if (initRootHeight > tmpRootHeight) { // take min
180 // initRootHeight = tmpRootHeight;
181 // }
182 // }
183 // } else {
184 // initRootHeight = prior.getTreeModel().getParameter("treeModel.rootHeight").initial;
185 // }
186 // // logistic.t50 initial always has to < treeRootHeight initial
187 // if (priorPara.initial >= initRootHeight) {
188 // priorPara.initial = initRootHeight / 2; // tree prior.initial has to < treeRootHeight.initial
189 // }
190 // } else {
191 // writer.writeComment("Has calibration");
192 //
193 // throw new IllegalArgumentException("This function is not available in this release !");
194 // }
195
196 writeParameter("logistic.t50", prior, writer);
197 writer.writeCloseTag(LogisticGrowthModelParser.TIME_50);
198
199 writer.writeCloseTag(LogisticGrowthModelParser.LOGISTIC_GROWTH_MODEL);
200
201 initialPopSize = "logistic.popSize";
202
203 break;
204
205 case EXPANSION:
206 // generate an exponential prior tree
207 writer.writeComment("A prior assumption that the population size has grown exponentially",
208 "from some ancestral population size in the past.");
209 writer.writeOpenTag(
210 ExpansionModelParser.EXPANSION_MODEL,
211 new Attribute[]{
212 new Attribute.Default<String>(XMLParser.ID, prefix + "expansion"),
213 new Attribute.Default<String>("units", Units.Utils.getDefaultUnitName(options.units))
214 }
215 );
216
217 // write pop size socket
218 writeParameter(ExpansionModelParser.POPULATION_SIZE, "expansion.popSize", prior, writer);
219
220 if (parameterization == TreePriorParameterizationType.GROWTH_RATE) {
221 // write growth rate socket
222 writeParameter(ExpansionModelParser.GROWTH_RATE, "expansion.growthRate", prior, writer);
223 } else {
224 // write doubling time socket
225 writeParameter(ExpansionModelParser.DOUBLING_TIME, "expansion.doublingTime", prior, writer);
226 }
227
228 // write ancestral proportion socket
229 writeParameter(ExpansionModelParser.ANCESTRAL_POPULATION_PROPORTION, "expansion.ancestralProportion", prior, writer);
230
231 writer.writeCloseTag(ExpansionModelParser.EXPANSION_MODEL);
232
233 initialPopSize = "expansion.popSize";
234
235 break;
236
237 case YULE:
238 case YULE_CALIBRATION:
239 if (nodeHeightPrior == TreePriorType.YULE_CALIBRATION) {
240 writer.writeComment("Calibrated Yule: Heled J, Drummond AJ (2011), Syst Biol, doi: " +
241 "10.1093/sysbio/syr087");
242 } else {
243 writer.writeComment("A prior on the distribution node heights defined given",
244 "a Yule speciation process (a pure birth process).");
245 }
246 writer.writeOpenTag(
247 YuleModelParser.YULE_MODEL,
248 new Attribute[]{
249 new Attribute.Default<String>(XMLParser.ID, prefix + YuleModelParser.YULE),
250 new Attribute.Default<String>("units", Units.Utils.getDefaultUnitName(units))
251 }
252 );
253
254 writeParameter(YuleModelParser.BIRTH_RATE, "yule.birthRate", prior, writer);
255 writer.writeCloseTag(YuleModelParser.YULE_MODEL);
256
257 break;
258
259 case BIRTH_DEATH:
260 case BIRTH_DEATH_INCOMPLETE_SAMPLING:
261 writer.writeComment("A prior on the distribution node heights defined given");
262 writer.writeComment(nodeHeightPrior == TreePriorType.BIRTH_DEATH_INCOMPLETE_SAMPLING ?
263 BirthDeathModelParser.getCitationRHO() : BirthDeathModelParser.getCitation());
264 writer.writeOpenTag(
265 BirthDeathModelParser.BIRTH_DEATH_MODEL,
266 new Attribute[]{
267 new Attribute.Default<String>(XMLParser.ID, prefix + BirthDeathModelParser.BIRTH_DEATH),
268 new Attribute.Default<String>("units", Units.Utils.getDefaultUnitName(units))
269 }
270 );
271
272 writeParameter(BirthDeathModelParser.BIRTHDIFF_RATE, BirthDeathModelParser.MEAN_GROWTH_RATE_PARAM_NAME, prior, writer);
273 writeParameter(BirthDeathModelParser.RELATIVE_DEATH_RATE, BirthDeathModelParser.RELATIVE_DEATH_RATE_PARAM_NAME, prior, writer);
274
275 if (nodeHeightPrior == TreePriorType.BIRTH_DEATH_INCOMPLETE_SAMPLING) {
276 writeParameter(BirthDeathModelParser.SAMPLE_PROB,
277 BirthDeathModelParser.BIRTH_DEATH + "." + BirthDeathModelParser.SAMPLE_PROB, prior, writer);
278 }
279
280 writer.writeCloseTag(BirthDeathModelParser.BIRTH_DEATH_MODEL);
281
282 break;
283
284 case BIRTH_DEATH_SERIAL_SAMPLING:
285 writer.writeComment(BirthDeathSerialSamplingModelParser.getCitationPsiOrg());
286
287 writer.writeOpenTag(
288 BirthDeathSerialSamplingModelParser.BIRTH_DEATH_SERIAL_MODEL,
289 new Attribute[]{
290 new Attribute.Default<String>(XMLParser.ID, prefix + BirthDeathSerialSamplingModelParser.BDSS),
291 new Attribute.Default<String>("units", Units.Utils.getDefaultUnitName(units)),
292 new Attribute.Default<Boolean>(BirthDeathSerialSamplingModelParser.HAS_FINAL_SAMPLE, false)
293 }
294 );
295
296 writeParameter(BirthDeathSerialSamplingModelParser.LAMBDA,
297 BirthDeathSerialSamplingModelParser.BDSS + "." + BirthDeathSerialSamplingModelParser.LAMBDA, prior, writer);
298 writeParameter(BirthDeathSerialSamplingModelParser.RELATIVE_MU,
299 BirthDeathSerialSamplingModelParser.BDSS + "." + BirthDeathSerialSamplingModelParser.RELATIVE_MU, prior, writer);
300 // writeParameter(BirthDeathSerialSamplingModelParser.SAMPLE_PROBABILITY,
301 // BirthDeathSerialSamplingModelParser.BDSS + "." + BirthDeathSerialSamplingModelParser.SAMPLE_PROBABILITY, prior, writer);
302 writeParameter(BirthDeathSerialSamplingModelParser.PSI,
303 BirthDeathSerialSamplingModelParser.BDSS + "." + BirthDeathSerialSamplingModelParser.PSI, prior, writer);
304 writeParameter(BirthDeathSerialSamplingModelParser.ORIGIN,
305 BirthDeathSerialSamplingModelParser.BDSS + "." + BirthDeathSerialSamplingModelParser.ORIGIN, prior, writer);
306
307 writer.writeCloseTag(BirthDeathSerialSamplingModelParser.BIRTH_DEATH_SERIAL_MODEL);
308
309 break;
310
311 case BIRTH_DEATH_BASIC_REPRODUCTIVE_NUMBER:
312 writer.writeComment(BirthDeathSerialSamplingModelParser.getCitationRT());
313
314 writer.writeOpenTag(
315 BirthDeathEpidemiologyModelParser.BIRTH_DEATH_EPIDEMIOLOGY,
316 new Attribute[]{
317 new Attribute.Default<String>(XMLParser.ID, prefix + BirthDeathEpidemiologyModelParser.BIRTH_DEATH_EPIDEMIOLOGY),
318 new Attribute.Default<String>("units", Units.Utils.getDefaultUnitName(units))
319 }
320 );
321
322 writeParameter(BirthDeathEpidemiologyModelParser.R0,
323 BirthDeathEpidemiologyModelParser.R0, prior, writer);
324 writeParameter(BirthDeathEpidemiologyModelParser.RECOVERY_RATE,
325 BirthDeathEpidemiologyModelParser.RECOVERY_RATE, prior, writer);
326 writeParameter(BirthDeathEpidemiologyModelParser.SAMPLING_PROBABILITY,
327 BirthDeathEpidemiologyModelParser.SAMPLING_PROBABILITY, prior, writer);
328 writeParameter(BirthDeathEpidemiologyModelParser.ORIGIN,
329 BirthDeathEpidemiologyModelParser.ORIGIN, prior, writer);
330
331 writer.writeCloseTag(BirthDeathEpidemiologyModelParser.BIRTH_DEATH_EPIDEMIOLOGY);
332
333 break;
334 }
335
336 if (nodeHeightPrior != TreePriorType.CONSTANT && nodeHeightPrior != TreePriorType.EXPONENTIAL) {
337 // If the node height prior is not one of these two then we need to simulate a
338 // random starting tree under a constant size coalescent.
339 writer.writeComment("This is a simple constant population size coalescent model",
340 "that is used to generate an initial tree for the chain.");
341 writer.writeOpenTag(
342 ConstantPopulationModelParser.CONSTANT_POPULATION_MODEL,
343 new Attribute[]{
344 new Attribute.Default<String>(XMLParser.ID, prefix + "initialDemo"),
345 new Attribute.Default<String>("units", Units.Utils.getDefaultUnitName(units))
346 }
347 );
348
349 writer.writeOpenTag(ConstantPopulationModelParser.POPULATION_SIZE);
350 if (initialPopSize != null) {
351 writer.writeIDref(ParameterParser.PARAMETER, prefix + initialPopSize);
352 } else {
353 writeParameter(prefix + "initialDemo.popSize", 1, 100.0, Double.NaN, Double.NaN, writer);
354 }
355 writer.writeCloseTag(ConstantPopulationModelParser.POPULATION_SIZE);
356 writer.writeCloseTag(ConstantPopulationModelParser.CONSTANT_POPULATION_MODEL);
357 }
358
359 // if (nodeHeightPrior == TreePriorType.BIRTH_DEATH_BASIC_REPRODUCTIVE_NUMBER) {
360 // writer.writeComment("R0 = b/(b*d+s*r)");
361 // writer.writeOpenTag(RPNcalculatorStatisticParser.RPN_STATISTIC,
362 // new Attribute[]{
363 // new Attribute.Default<String>(XMLParser.ID, modelPrefix + "R0")
364 // });
365 //
366 // writer.writeOpenTag(RPNcalculatorStatisticParser.VARIABLE,
367 // new Attribute[]{
368 // new Attribute.Default<String>(Statistic.NAME, modelPrefix + "b")
369 // });
370 // writeParameterRef(modelPrefix + BirthDeathSerialSamplingModelParser.BDSS + "." + BirthDeathSerialSamplingModelParser.LAMBDA, writer);
371 // writer.writeCloseTag(RPNcalculatorStatisticParser.VARIABLE);
372 //
373 // writer.writeOpenTag(RPNcalculatorStatisticParser.VARIABLE,
374 // new Attribute[]{
375 // new Attribute.Default<String>(Statistic.NAME, modelPrefix + "d")
376 // });
377 // writeParameterRef(modelPrefix + BirthDeathSerialSamplingModelParser.BDSS + "." + BirthDeathSerialSamplingModelParser.RELATIVE_MU, writer);
378 // writer.writeCloseTag(RPNcalculatorStatisticParser.VARIABLE);
379 //
380 // writer.writeOpenTag(RPNcalculatorStatisticParser.VARIABLE,
381 // new Attribute[]{
382 // new Attribute.Default<String>(Statistic.NAME, modelPrefix + "s")
383 // });
384 // writeParameterRef(modelPrefix + BirthDeathSerialSamplingModelParser.BDSS + "." + BirthDeathSerialSamplingModelParser.PSI, writer);
385 // writer.writeCloseTag(RPNcalculatorStatisticParser.VARIABLE);
386 //
387 // writer.writeOpenTag(RPNcalculatorStatisticParser.VARIABLE,
388 // new Attribute[]{
389 // new Attribute.Default<String>(Statistic.NAME, modelPrefix + "r")
390 // });
391 // writeParameterRef(modelPrefix + BirthDeathSerialSamplingModelParser.BDSS + "." + BirthDeathSerialSamplingModelParser.R, writer);
392 // writer.writeCloseTag(RPNcalculatorStatisticParser.VARIABLE);
393 //
394 // writer.writeOpenTag(RPNcalculatorStatisticParser.EXPRESSION,
395 // new Attribute[]{
396 // new Attribute.Default<String>(Statistic.NAME, modelPrefix + "R0")
397 // });
398 // writer.writeText(modelPrefix + "b " + modelPrefix + "b " + modelPrefix + "d " + "* " + modelPrefix + "s " + modelPrefix + "r " + "* + /");
399 // writer.writeCloseTag(RPNcalculatorStatisticParser.EXPRESSION);
400 //
401 // writer.writeCloseTag(RPNcalculatorStatisticParser.RPN_STATISTIC);
402 // }
403 }
404
405 /**
406 * Write the prior on node heights (coalescent or speciational models)
407 *
408 * @param model PartitionTreeModel
409 * @param writer the writer
410 */
411 void writePriorLikelihood(PartitionTreeModel model, XMLWriter writer) {
412
413 //tree model prefix
414 String prefix = model.getPrefix();
415
416 PartitionTreePrior prior = model.getPartitionTreePrior();
417 TreePriorType treePrior = prior.getNodeHeightPrior();
418
419 // String priorPrefix = prior.getPrefix();
420
421 switch (treePrior) {
422 case YULE:
423 case BIRTH_DEATH:
424 case BIRTH_DEATH_INCOMPLETE_SAMPLING:
425 case BIRTH_DEATH_SERIAL_SAMPLING:
426 case BIRTH_DEATH_BASIC_REPRODUCTIVE_NUMBER:
427 case YULE_CALIBRATION:
428 // generate a speciational process
429 writer.writeComment("Generate a speciation likelihood for Yule or Birth Death");
430 writer.writeOpenTag(
431 SpeciationLikelihoodParser.SPECIATION_LIKELIHOOD,
432 new Attribute[]{
433 new Attribute.Default<String>(XMLParser.ID, prefix + "speciation")
434 }
435 );
436
437 // write pop size socket
438 writer.writeOpenTag(SpeciationLikelihoodParser.MODEL);
439 writeNodeHeightPriorModelRef(prior, writer);
440 writer.writeCloseTag(SpeciationLikelihoodParser.MODEL);
441 writer.writeOpenTag(SpeciationLikelihoodParser.TREE);
442 writer.writeIDref(TreeModel.TREE_MODEL, prefix + TreeModel.TREE_MODEL);
443 writer.writeCloseTag(SpeciationLikelihoodParser.TREE);
444
445 if (treePrior == TreePriorType.YULE_CALIBRATION) {
446
447 if (options.treeModelOptions.isNodeCalibrated(model) == 0) {
448 writer.writeOpenTag(SpeciationLikelihoodParser.CALIBRATION,
449 new Attribute[]{
450 new Attribute.Default<String>(SpeciationLikelihoodParser.CORRECTION, prior.getCalibCorrectionType().toString())
451 });
452 writer.writeOpenTag(SpeciationLikelihoodParser.POINT);
453
454 String taxaId;
455 if (options.hasIdenticalTaxa()) {
456 taxaId = TaxaParser.TAXA;
457 } else {
458 taxaId = options.getDataPartitions(model).get(0).getPrefix() + TaxaParser.TAXA;
459 }
460 writer.writeIDref(TaxaParser.TAXA, taxaId);
461
462 writeDistribution(model.getParameter("treeModel.rootHeight"), true, writer);
463
464 writer.writeCloseTag(SpeciationLikelihoodParser.POINT);
465 writer.writeCloseTag(SpeciationLikelihoodParser.CALIBRATION);
466
467 } else if (options.treeModelOptions.isNodeCalibrated(model) == 1) {
468 // should be only 1 calibrated internal node with monophyletic for each tree at moment
469 Taxa t = (Taxa) options.getKeysFromValue(options.taxonSetsTreeModel, model).get(0);
470 Parameter nodeCalib = options.getStatistic(t);
471
472 writer.writeOpenTag(SpeciationLikelihoodParser.CALIBRATION,
473 new Attribute[]{
474 new Attribute.Default<String>(SpeciationLikelihoodParser.CORRECTION, prior.getCalibCorrectionType().toString())
475 });
476 writer.writeOpenTag(SpeciationLikelihoodParser.POINT);
477
478 writer.writeIDref(TaxaParser.TAXA, t.getId());
479 writeDistribution(nodeCalib, true, writer);
480
481 writer.writeCloseTag(SpeciationLikelihoodParser.POINT);
482 writer.writeCloseTag(SpeciationLikelihoodParser.CALIBRATION);
483
484 if (!options.treeModelOptions.isNodeCalibrated(nodeCalib)) {
485 throw new IllegalArgumentException("Calibrated Yule model requires a calibration to be specified for node, " +
486 nodeCalib.getName() + ".");
487 }
488 }
489 }
490
491 writer.writeCloseTag(SpeciationLikelihoodParser.SPECIATION_LIKELIHOOD);
492
493 break;
494
495 // AR - removed this special case for Logistic - it causes terrible problems.
496 // Need to put informative priors on Logistic parameters.
497 // case LOGISTIC:
498 // writer.writeComment("Generate a boolean likelihood for Coalescent: Logistic Growth");
499 // writer.writeOpenTag(
500 // BooleanLikelihoodParser.BOOLEAN_LIKELIHOOD,
501 // new Attribute[]{new Attribute.Default<String>(XMLParser.ID, modelPrefix + "booleanLikelihood1")}
502 // );
503 // writer.writeOpenTag(
504 // TestStatisticParser.TEST_STATISTIC,
505 // new Attribute[]{
506 // new Attribute.Default<String>(XMLParser.ID, "test1"),
507 // new Attribute.Default<String>(Statistic.NAME, "test1")
508 // }
509 // );
510 // writer.writeIDref(ParameterParser.PARAMETER, priorPrefix + "logistic.t50");
511 // writer.writeOpenTag("lessThan");
512 // writer.writeIDref(ParameterParser.PARAMETER, modelPrefix + "treeModel.rootHeight");
513 // writer.writeCloseTag("lessThan");
514 // writer.writeCloseTag(TestStatisticParser.TEST_STATISTIC);
515 // writer.writeCloseTag(BooleanLikelihoodParser.BOOLEAN_LIKELIHOOD);
516 //
517 // writer.writeOpenTag(
518 // CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD,
519 // new Attribute[]{new Attribute.Default<String>(XMLParser.ID, modelPrefix + COALESCENT)}
520 // );
521 // writer.writeOpenTag(CoalescentLikelihoodParser.MODEL);
522 // writeNodeHeightPriorModelRef(prior, writer);
523 // writer.writeCloseTag(CoalescentLikelihoodParser.MODEL);
524 // writer.writeOpenTag(CoalescentLikelihoodParser.POPULATION_TREE);
525 // writer.writeIDref(TreeModel.TREE_MODEL, modelPrefix + TreeModel.TREE_MODEL);
526 // writer.writeCloseTag(CoalescentLikelihoodParser.POPULATION_TREE);
527 // writer.writeCloseTag(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD);
528 //
529 // break;
530
531 case SKYLINE:
532 // generate a Bayesian skyline plot
533 writer.writeComment("Generate a generalizedSkyLineLikelihood for Bayesian Skyline");
534 writer.writeOpenTag(
535 BayesianSkylineLikelihoodParser.SKYLINE_LIKELIHOOD,
536 new Attribute[]{
537 new Attribute.Default<String>(XMLParser.ID, prefix + "skyline"),
538 new Attribute.Default<String>("linear",
539 prior.getSkylineModel() == TreePriorParameterizationType.LINEAR_SKYLINE ? "true" : "false")
540 }
541 );
542
543 // write pop size socket
544 writer.writeOpenTag(BayesianSkylineLikelihoodParser.POPULATION_SIZES);
545 if (prior.getSkylineModel() == TreePriorParameterizationType.LINEAR_SKYLINE) {
546 writeParameter(prior.getParameter("skyline.popSize"), prior.getSkylineGroupCount() + 1, writer);
547 } else {
548 writeParameter(prior.getParameter("skyline.popSize"), prior.getSkylineGroupCount(), writer);
549 }
550 writer.writeCloseTag(BayesianSkylineLikelihoodParser.POPULATION_SIZES);
551
552 // write group size socket
553 writer.writeOpenTag(BayesianSkylineLikelihoodParser.GROUP_SIZES);
554 writeParameter(prior.getParameter("skyline.groupSize"), prior.getSkylineGroupCount(), writer);
555 writer.writeCloseTag(BayesianSkylineLikelihoodParser.GROUP_SIZES);
556
557 writer.writeOpenTag(CoalescentLikelihoodParser.POPULATION_TREE);
558 writer.writeIDref(TreeModel.TREE_MODEL, prefix + TreeModel.TREE_MODEL);
559 writer.writeCloseTag(CoalescentLikelihoodParser.POPULATION_TREE);
560
561 writer.writeCloseTag(BayesianSkylineLikelihoodParser.SKYLINE_LIKELIHOOD);
562
563 writer.writeText("");
564 writeExponentialMarkovLikelihood(prior, writer);
565
566 break;
567
568 case EXTENDED_SKYLINE:
569 // different format
570 break;
571
572 case GMRF_SKYRIDE:
573 writer.writeComment("Generate a gmrfSkyrideLikelihood for GMRF Bayesian Skyride process");
574 writer.writeOpenTag(
575 GMRFSkyrideLikelihoodParser.SKYLINE_LIKELIHOOD,
576 new Attribute[]{
577 new Attribute.Default<String>(XMLParser.ID, prefix + "skyride"),
578 new Attribute.Default<String>(GMRFSkyrideLikelihoodParser.TIME_AWARE_SMOOTHING,
579 prior.getSkyrideSmoothing() == TreePriorParameterizationType.TIME_AWARE_SKYRIDE ? "true" : "false"),
580 new Attribute.Default<String>(GMRFSkyrideLikelihoodParser.RANDOMIZE_TREE,
581 //TODO For GMRF, tree model/tree prior combination not implemented by BEAST yet. The validation is in BeastGenerator.checkOptions()
582 options.getPartitionTreeModels(prior).get(0).getStartingTreeType() == StartingTreeType.UPGMA ? "true" : "false"),
583 }
584 );
585
586 int skyrideIntervalCount = options.taxonList.getTaxonCount() - 1;
587 writer.writeOpenTag(GMRFSkyrideLikelihoodParser.POPULATION_PARAMETER);
588 writer.writeComment("skyride.logPopSize is in log units unlike other popSize");
589 writeParameter(prior.getParameter("skyride.logPopSize"), skyrideIntervalCount, writer);
590 writer.writeCloseTag(GMRFSkyrideLikelihoodParser.POPULATION_PARAMETER);
591
592 writer.writeOpenTag(GMRFSkyrideLikelihoodParser.GROUP_SIZES);
593 writeParameter(prior.getParameter("skyride.groupSize"), skyrideIntervalCount, writer);
594 writer.writeCloseTag(GMRFSkyrideLikelihoodParser.GROUP_SIZES);
595
596 writer.writeOpenTag(GMRFSkyrideLikelihoodParser.PRECISION_PARAMETER);
597 writeParameter(prior.getParameter("skyride.precision"), 1, writer);
598 writer.writeCloseTag(GMRFSkyrideLikelihoodParser.PRECISION_PARAMETER);
599
600 writer.writeOpenTag(GMRFSkyrideLikelihoodParser.POPULATION_TREE);
601 writer.writeIDref(TreeModel.TREE_MODEL, prefix + TreeModel.TREE_MODEL);
602 writer.writeCloseTag(GMRFSkyrideLikelihoodParser.POPULATION_TREE);
603
604 writer.writeCloseTag(GMRFSkyrideLikelihoodParser.SKYLINE_LIKELIHOOD);
605
606 break;
607
608 case SKYGRID:
609 break;
610
611 default:
612 // generate a coalescent process
613 writer.writeComment("Generate a coalescent likelihood");
614 writer.writeOpenTag(
615 CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD,
616 new Attribute[]{new Attribute.Default<String>(XMLParser.ID, prefix + COALESCENT)}
617 );
618 writer.writeOpenTag(CoalescentLikelihoodParser.MODEL);
619 writeNodeHeightPriorModelRef(prior, writer);
620 writer.writeCloseTag(CoalescentLikelihoodParser.MODEL);
621 writer.writeOpenTag(CoalescentLikelihoodParser.POPULATION_TREE);
622 writer.writeIDref(TreeModel.TREE_MODEL, prefix + TreeModel.TREE_MODEL);
623 writer.writeCloseTag(CoalescentLikelihoodParser.POPULATION_TREE);
624 writer.writeCloseTag(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD);
625 }
626 }
627
628 void writeNodeHeightPriorModelRef(PartitionTreePrior prior, XMLWriter writer) {
629 TreePriorType treePrior = prior.getNodeHeightPrior();
630 String priorPrefix = prior.getPrefix();
631
632 switch (treePrior) {
633 case CONSTANT:
634 writer.writeIDref(ConstantPopulationModelParser.CONSTANT_POPULATION_MODEL, priorPrefix + "constant");
635 break;
636 case EXPONENTIAL:
637 writer.writeIDref(ExponentialGrowthModelParser.EXPONENTIAL_GROWTH_MODEL, priorPrefix + "exponential");
638 break;
639 case LOGISTIC:
640 writer.writeIDref(LogisticGrowthModelParser.LOGISTIC_GROWTH_MODEL, priorPrefix + "logistic");
641 break;
642 case EXPANSION:
643 writer.writeIDref(ExpansionModelParser.EXPANSION_MODEL, priorPrefix + "expansion");
644 break;
645 case SKYLINE:
646 writer.writeIDref(BayesianSkylineLikelihoodParser.SKYLINE_LIKELIHOOD, priorPrefix + "skyline");
647 break;
648 case GMRF_SKYRIDE:
649 writer.writeIDref(GMRFSkyrideLikelihoodParser.SKYLINE_LIKELIHOOD, priorPrefix + "skyride");
650 break;
651 case SKYGRID:
652 writer.writeIDref(GMRFSkyrideLikelihoodParser.SKYGRID_LIKELIHOOD, priorPrefix + "skygrid");
653 break;
654 case YULE:
655 case YULE_CALIBRATION:
656 writer.writeIDref(YuleModelParser.YULE_MODEL, priorPrefix + YuleModelParser.YULE);
657 break;
658 case BIRTH_DEATH:
659 case BIRTH_DEATH_INCOMPLETE_SAMPLING:
660 writer.writeIDref(BirthDeathModelParser.BIRTH_DEATH_MODEL, priorPrefix + BirthDeathModelParser.BIRTH_DEATH);
661 break;
662 case BIRTH_DEATH_SERIAL_SAMPLING:
663 writer.writeIDref(BirthDeathSerialSamplingModelParser.BIRTH_DEATH_SERIAL_MODEL,
664 priorPrefix + BirthDeathSerialSamplingModelParser.BDSS);
665 break;
666 case BIRTH_DEATH_BASIC_REPRODUCTIVE_NUMBER:
667 writer.writeIDref(BirthDeathEpidemiologyModelParser.BIRTH_DEATH_EPIDEMIOLOGY,
668 priorPrefix + BirthDeathEpidemiologyModelParser.BIRTH_DEATH_EPIDEMIOLOGY);
669 break;
670 default:
671 throw new IllegalArgumentException("No tree prior has been specified so cannot refer to it");
672 }
673 }
674
675 void writeMultiLociTreePriors(PartitionTreePrior prior, XMLWriter writer) {
676
677 String priorPrefix = prior.getPrefix();
678
679 if (prior.getNodeHeightPrior() == TreePriorType.SKYGRID) {
680
681 writer.writeComment("Generate a gmrfSkyGridLikelihood for the Bayesian SkyGrid process");
682 writer.writeOpenTag(
683 GMRFSkyrideLikelihoodParser.SKYGRID_LIKELIHOOD,
684 new Attribute[]{
685 new Attribute.Default<String>(XMLParser.ID, priorPrefix + "skygrid"),
686 }
687 );
688
689 int skyGridIntervalCount = prior.getSkyGridCount();
690 double skyGridInterval = prior.getSkyGridInterval();
691
692 writer.writeOpenTag(GMRFSkyrideLikelihoodParser.POPULATION_PARAMETER);
693 writer.writeComment("skygrid.logPopSize is in log units unlike other popSize");
694 writeParameter(prior.getParameter("skygrid.logPopSize"), skyGridIntervalCount, writer);
695 writer.writeCloseTag(GMRFSkyrideLikelihoodParser.POPULATION_PARAMETER);
696
697 writer.writeOpenTag(GMRFSkyrideLikelihoodParser.PRECISION_PARAMETER);
698 writeParameter(prior.getParameter("skygrid.precision"), 1, writer);
699 writer.writeCloseTag(GMRFSkyrideLikelihoodParser.PRECISION_PARAMETER);
700
701 writer.writeOpenTag(GMRFSkyrideLikelihoodParser.NUM_GRID_POINTS);
702 Parameter numGridPoint = prior.getParameter("skygrid.numGridPoints");
703 numGridPoint.setInitial(skyGridIntervalCount - 1);
704 writeParameter(numGridPoint, 1, writer);
705 writer.writeCloseTag(GMRFSkyrideLikelihoodParser.NUM_GRID_POINTS);
706
707 writer.writeOpenTag(GMRFSkyrideLikelihoodParser.CUT_OFF);
708 Parameter cutOff = prior.getParameter("skygrid.cutOff");
709 cutOff.setInitial(skyGridInterval);
710 writeParameter(cutOff, 1, writer);
711 writer.writeCloseTag(GMRFSkyrideLikelihoodParser.CUT_OFF);
712
713 writer.writeOpenTag(GMRFSkyrideLikelihoodParser.POPULATION_TREE);
714 // TODO Add all linked trees
715 if (options.isShareSameTreePrior()) {
716 for (PartitionTreeModel thisModel : options.getPartitionTreeModels()) {
717 writer.writeIDref(TreeModel.TREE_MODEL, thisModel.getPrefix() + TreeModel.TREE_MODEL);
718 }
719 } else {
720 writer.writeIDref(TreeModel.TREE_MODEL, options.getPartitionTreeModels(prior).get(0).getPrefix() + TreeModel.TREE_MODEL);
721 }
722 writer.writeCloseTag(GMRFSkyrideLikelihoodParser.POPULATION_TREE);
723
724 writer.writeCloseTag(GMRFSkyrideLikelihoodParser.SKYGRID_LIKELIHOOD);
725
726 } else if (prior.getNodeHeightPrior() == TreePriorType.EXTENDED_SKYLINE) {
727
728 final String tagName = VariableDemographicModelParser.MODEL_NAME;
729 writer.writeComment("Generate a variableDemographic for extended Bayesian skyline process");
730 writer.writeOpenTag(tagName, new Attribute[]{
731 new Attribute.Default<String>(XMLParser.ID, priorPrefix + VariableDemographicModelParser.demoElementName),
732 new Attribute.Default<String>(VariableDemographicModelParser.TYPE, prior.getExtendedSkylineModel().toString()),
733 // use midpoint by default (todo) would be nice to have a user 'tickable' option
734 new Attribute.Default<String>(VariableDemographicModelParser.USE_MIDPOINTS, "true")
735 }
736 );
737
738 Parameter popSize = prior.getParameter(VariableDemographicModelParser.demoElementName + ".popSize");
739 Parameter populationMean = prior.getParameter(VariableDemographicModelParser.demoElementName + ".populationMean");
740 popSize.setInitial(populationMean.getInitial());
741
742 writer.writeOpenTag(VariableDemographicModelParser.POPULATION_SIZES);
743 writer.writeComment("popSize value = populationMean value");
744 writer.writeTag(ParameterParser.PARAMETER,
745 new Attribute[]{
746 new Attribute.Default<String>(XMLParser.ID, priorPrefix + VariableDemographicModelParser.demoElementName + ".popSize"),
747 new Attribute.Default<String>(ParameterParser.VALUE, Double.toString(popSize.getInitial()))}, true);
748 // writeParameter(popSize, -1, writer);
749 writer.writeCloseTag(VariableDemographicModelParser.POPULATION_SIZES);
750
751 // Parameter indicators = prior.getParameter(VariableDemographicModelParser.demoElementName + ".indicators");
752 writer.writeOpenTag(VariableDemographicModelParser.INDICATOR_PARAMETER);
753 writer.writeTag(ParameterParser.PARAMETER,
754 new Attribute[]{
755 new Attribute.Default<String>(XMLParser.ID, priorPrefix + VariableDemographicModelParser.demoElementName + ".indicators"),
756 new Attribute.Default<String>(ParameterParser.VALUE, Double.toString(0.0))}, true); // also 0.0
757 // writeParameter(prior.getParameter(VariableDemographicModelParser.demoElementName + ".indicators"), -1, writer); // not need dimension
758 writer.writeCloseTag(VariableDemographicModelParser.INDICATOR_PARAMETER);
759
760 writer.writeOpenTag(VariableDemographicModelParser.POPULATION_TREES);
761
762 if (options.isShareSameTreePrior()) {
763 for (PartitionTreeModel model : options.getPartitionTreeModels()) {
764 writer.writeOpenTag(VariableDemographicModelParser.POP_TREE, new Attribute[]{
765 new Attribute.Default<String>(SpeciesBindingsParser.PLOIDY, Double.toString(model.getPloidyType().getValue()))
766 }
767 );
768 writer.writeIDref(TreeModel.TREE_MODEL, model.getPrefix() + TreeModel.TREE_MODEL);
769 writer.writeCloseTag(VariableDemographicModelParser.POP_TREE);
770 }
771 } else {//TODO correct for not sharing same prior?
772 writer.writeOpenTag(VariableDemographicModelParser.POP_TREE, new Attribute[]{
773 new Attribute.Default<String>(SpeciesBindingsParser.PLOIDY, Double.toString(options.getPartitionTreeModels(prior).get(0).getPloidyType().getValue()))
774 }
775 );
776 writer.writeIDref(TreeModel.TREE_MODEL, options.getPartitionTreeModels(prior).get(0).getPrefix() + TreeModel.TREE_MODEL);
777 writer.writeCloseTag(VariableDemographicModelParser.POP_TREE);
778 }
779
780 writer.writeCloseTag(VariableDemographicModelParser.POPULATION_TREES);
781
782 writer.writeCloseTag(tagName);
783
784 writer.writeOpenTag(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD, new Attribute.Default<String>(XMLParser.ID, priorPrefix + COALESCENT));
785 writer.writeOpenTag(CoalescentLikelihoodParser.MODEL);
786 writer.writeIDref(tagName, priorPrefix + VariableDemographicModelParser.demoElementName);
787 writer.writeCloseTag(CoalescentLikelihoodParser.MODEL);
788 writer.writeComment("Take population Tree from demographic");
789 writer.writeCloseTag(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD);
790
791 writer.writeOpenTag(SumStatisticParser.SUM_STATISTIC,
792 new Attribute[]{
793 new Attribute.Default<String>(XMLParser.ID, priorPrefix + VariableDemographicModelParser.demoElementName + ".populationSizeChanges"),
794 new Attribute.Default<String>("elementwise", "true")
795 });
796 writer.writeIDref(ParameterParser.PARAMETER, priorPrefix + VariableDemographicModelParser.demoElementName + ".indicators");
797 writer.writeCloseTag(SumStatisticParser.SUM_STATISTIC);
798 writer.writeOpenTag(ExponentialDistributionModel.EXPONENTIAL_DISTRIBUTION_MODEL,
799 new Attribute[]{
800 new Attribute.Default<String>(XMLParser.ID, priorPrefix + VariableDemographicModelParser.demoElementName + ".populationMeanDist")
801 //,new Attribute.Default<String>("elementwise", "true")
802 });
803 writer.writeOpenTag(DistributionModelParser.MEAN);
804
805 writer.writeComment("prefer populationMean value = 1");
806 populationMean = prior.getParameter(VariableDemographicModelParser.demoElementName + ".populationMean");
807 writer.writeTag(ParameterParser.PARAMETER,
808 new Attribute[]{
809 new Attribute.Default<String>(XMLParser.ID, priorPrefix + VariableDemographicModelParser.demoElementName + ".populationMean"),
810 new Attribute.Default<String>(ParameterParser.VALUE, Double.toString(populationMean.getInitial()))}, true);
811
812 writer.writeCloseTag(DistributionModelParser.MEAN);
813 writer.writeCloseTag(ExponentialDistributionModel.EXPONENTIAL_DISTRIBUTION_MODEL);
814 }
815 }
816
817 void writeParameterLog(PartitionTreePrior prior, XMLWriter writer) {
818
819 String priorPrefix = prior.getPrefix();
820
821 switch (prior.getNodeHeightPrior()) {
822
823 case CONSTANT:
824 writeParameterRef(priorPrefix + "constant.popSize", writer);
825 break;
826 case EXPONENTIAL:
827 writeParameterRef(priorPrefix + "exponential.popSize", writer);
828 if (prior.getParameterization() == TreePriorParameterizationType.GROWTH_RATE) {
829 writeParameterRef(priorPrefix + "exponential.growthRate", writer);
830 } else {
831 writeParameterRef(priorPrefix + "exponential.doublingTime", writer);
832 }
833 break;
834 case LOGISTIC:
835 writeParameterRef(priorPrefix + "logistic.popSize", writer);
836 if (prior.getParameterization() == TreePriorParameterizationType.GROWTH_RATE) {
837 writeParameterRef(priorPrefix + "logistic.growthRate", writer);
838 } else {
839 writeParameterRef(priorPrefix + "logistic.doublingTime", writer);
840 }
841 writeParameterRef(priorPrefix + "logistic.t50", writer);
842 break;
843 case EXPANSION:
844 writeParameterRef(priorPrefix + "expansion.popSize", writer);
845 if (prior.getParameterization() == TreePriorParameterizationType.GROWTH_RATE) {
846 writeParameterRef(priorPrefix + "expansion.growthRate", writer);
847 } else {
848 writeParameterRef(priorPrefix + "expansion.doublingTime", writer);
849 }
850 writeParameterRef(priorPrefix + "expansion.ancestralProportion", writer);
851 break;
852 case SKYLINE:
853 writeParameterRef(priorPrefix + "skyline.popSize", writer);
854 writeParameterRef(priorPrefix + "skyline.groupSize", writer);
855 break;
856 case EXTENDED_SKYLINE:
857 writer.writeIDref(SumStatisticParser.SUM_STATISTIC, "demographic.populationSizeChanges");
858 writeParameterRef(priorPrefix + "demographic.populationMean", writer);
859 writeParameterRef(priorPrefix + "demographic.popSize", writer);
860 writeParameterRef(priorPrefix + "demographic.indicators", writer);
861 break;
862 case SKYGRID:
863 writeParameterRef(priorPrefix + "skygrid.precision", writer);
864 writeParameterRef(priorPrefix + "skygrid.logPopSize", writer);
865 writeParameterRef(priorPrefix + "skygrid.cutOff", writer);
866 // writeParameterRef(priorPrefix + "skygrid.groupSize", writer);
867 break;
868 case GMRF_SKYRIDE:
869 writeParameterRef(priorPrefix + "skyride.precision", writer);
870 writeParameterRef(priorPrefix + "skyride.logPopSize", writer);
871 writeParameterRef(priorPrefix + "skyride.groupSize", writer);
872 break;
873 case YULE:
874 case YULE_CALIBRATION:
875 writeParameterRef(priorPrefix + "yule.birthRate", writer);
876 break;
877 case BIRTH_DEATH:
878 case BIRTH_DEATH_INCOMPLETE_SAMPLING:
879 writeParameterRef(priorPrefix + BirthDeathModelParser.MEAN_GROWTH_RATE_PARAM_NAME, writer);
880 writeParameterRef(priorPrefix + BirthDeathModelParser.RELATIVE_DEATH_RATE_PARAM_NAME, writer);
881 if (prior.getNodeHeightPrior() == TreePriorType.BIRTH_DEATH_INCOMPLETE_SAMPLING)
882 writeParameterRef(priorPrefix + BirthDeathModelParser.BIRTH_DEATH + "."
883 + BirthDeathModelParser.SAMPLE_PROB, writer);
884 break;
885 case BIRTH_DEATH_SERIAL_SAMPLING:
886 writeParameterRef(priorPrefix + BirthDeathSerialSamplingModelParser.BDSS + "."
887 + BirthDeathSerialSamplingModelParser.LAMBDA, writer);
888 writeParameterRef(priorPrefix + BirthDeathSerialSamplingModelParser.BDSS + "."
889 + BirthDeathSerialSamplingModelParser.RELATIVE_MU, writer);
890 //Issue 656
891 // writeParameterRef(modelPrefix + BirthDeathSerialSamplingModelParser.BDSS + "."
892 // + BirthDeathSerialSamplingModelParser.SAMPLE_PROBABILITY, writer);
893 writeParameterRef(priorPrefix + BirthDeathSerialSamplingModelParser.BDSS + "."
894 + BirthDeathSerialSamplingModelParser.PSI, writer);
895 writeParameterRef(priorPrefix + BirthDeathSerialSamplingModelParser.BDSS + "."
896 + BirthDeathSerialSamplingModelParser.ORIGIN, writer);
897 break;
898 case BIRTH_DEATH_BASIC_REPRODUCTIVE_NUMBER:
899 writeParameterRef(priorPrefix + BirthDeathEpidemiologyModelParser.R0, writer);
900 writeParameterRef(priorPrefix + BirthDeathEpidemiologyModelParser.RECOVERY_RATE, writer);
901 writeParameterRef(priorPrefix + BirthDeathEpidemiologyModelParser.SAMPLING_PROBABILITY, writer);
902 writeParameterRef(priorPrefix + BirthDeathEpidemiologyModelParser.ORIGIN, writer);
903 default:
904 throw new IllegalArgumentException("No tree prior has been specified so cannot refer to it");
905 }
906
907 }
908
909 void writeEBSPAnalysisToCSVfile(PartitionTreePrior prior, XMLWriter writer) {
910
911 String priorPrefix = prior.getPrefix();
912
913 String logFileName = options.logFileName;
914
915 if (prior.getNodeHeightPrior() == TreePriorType.EXTENDED_SKYLINE) {
916 writer.writeOpenTag(EBSPAnalysisParser.VD_ANALYSIS, new Attribute[]{
917 new Attribute.Default<String>(XMLParser.ID, priorPrefix + "demographic.analysis"),
918 new Attribute.Default<Double>(EBSPAnalysisParser.BURN_IN, 0.1),
919 new Attribute.Default<Boolean>(VariableDemographicModelParser.USE_MIDPOINTS, true)}
920 );
921
922 writer.writeOpenTag(EBSPAnalysisParser.LOG_FILE_NAME);
923 writer.writeText(logFileName);
924 writer.writeCloseTag(EBSPAnalysisParser.LOG_FILE_NAME);
925
926 writer.writeOpenTag(EBSPAnalysisParser.TREE_FILE_NAMES);
927 for (String treeFN : options.treeFileName) {
928 writer.writeOpenTag(EBSPAnalysisParser.TREE_LOG);
929 writer.writeText(treeFN);
930 writer.writeCloseTag(EBSPAnalysisParser.TREE_LOG);
931 }
932 writer.writeCloseTag(EBSPAnalysisParser.TREE_FILE_NAMES);
933
934 writer.writeOpenTag(EBSPAnalysisParser.MODEL_TYPE);
935 writer.writeText(prior.getExtendedSkylineModel().toString());
936 writer.writeCloseTag(EBSPAnalysisParser.MODEL_TYPE);
937
938 writer.writeOpenTag(EBSPAnalysisParser.POPULATION_FIRST_COLUMN);
939 writer.writeText(VariableDemographicModelParser.demoElementName + ".popSize1");
940 writer.writeCloseTag(EBSPAnalysisParser.POPULATION_FIRST_COLUMN);
941
942 writer.writeOpenTag(EBSPAnalysisParser.INDICATORS_FIRST_COLUMN);
943 writer.writeText(VariableDemographicModelParser.demoElementName + ".indicators1");
944 writer.writeCloseTag(EBSPAnalysisParser.INDICATORS_FIRST_COLUMN);
945
946 writer.writeCloseTag(EBSPAnalysisParser.VD_ANALYSIS);
947
948 writer.writeOpenTag(CSVExporterParser.CSV_EXPORT,
949 new Attribute[]{
950 new Attribute.Default<String>(CSVExporterParser.FILE_NAME,
951 logFileName.subSequence(0, logFileName.length() - 4) + ".csv"), //.log
952 new Attribute.Default<String>(CSVExporterParser.SEPARATOR, ",")
953 });
954 writer.writeOpenTag(CSVExporterParser.COLUMNS);
955 writer.writeIDref(EBSPAnalysisParser.VD_ANALYSIS, priorPrefix + "demographic.analysis");
956 writer.writeCloseTag(CSVExporterParser.COLUMNS);
957 writer.writeCloseTag(CSVExporterParser.CSV_EXPORT);
958 }
959 }
960
961 private void writeExponentialMarkovLikelihood(PartitionTreePrior prior, XMLWriter writer) {
962
963 writer.writeOpenTag(
964 ExponentialMarkovModel.EXPONENTIAL_MARKOV_MODEL,
965 new Attribute[]{new Attribute.Default<String>(XMLParser.ID, prior.getPrefix() + "eml1"),
966 new Attribute.Default<String>("jeffreys", "true")}
967 );
968
969 writeParameterRef(ExponentialMarkovModelParser.CHAIN_PARAMETER, prior.getPrefix() + "skyline.popSize", writer);
970
971 writer.writeCloseTag(ExponentialMarkovModel.EXPONENTIAL_MARKOV_MODEL);
972 }
973
974 public static void writePriorLikelihoodReferenceLog(PartitionTreePrior prior, PartitionTreeModel model, XMLWriter writer) {
975
976 String prefix = model.getPrefix();
977
978 switch (prior.getNodeHeightPrior()) {
979
980 case YULE:
981 case YULE_CALIBRATION:
982 case BIRTH_DEATH:
983 case BIRTH_DEATH_INCOMPLETE_SAMPLING:
984 case BIRTH_DEATH_SERIAL_SAMPLING:
985 case BIRTH_DEATH_BASIC_REPRODUCTIVE_NUMBER:
986 writer.writeIDref(SpeciationLikelihoodParser.SPECIATION_LIKELIHOOD, prefix + "speciation");
987 break;
988 case SKYLINE:
989 writer.writeIDref(BayesianSkylineLikelihoodParser.SKYLINE_LIKELIHOOD, prefix + "skyline");
990 // writer.writeIDref(ExponentialMarkovModel.EXPONENTIAL_MARKOV_MODEL, modelPrefix + "eml1");
991 break;
992 case GMRF_SKYRIDE:
993 writer.writeIDref(GMRFSkyrideLikelihoodParser.SKYLINE_LIKELIHOOD, prefix + "skyride");
994 break;
995 case SKYGRID:
996 writer.writeIDref(GMRFSkyrideLikelihoodParser.SKYLINE_LIKELIHOOD, prefix + "skygrid");
997 // only 1 coalescent, so write it separately after this method
998 break;
999 case LOGISTIC:
1000 // writer.writeIDref(BooleanLikelihoodParser.BOOLEAN_LIKELIHOOD, modelPrefix + "booleanLikelihood1");
1001 writer.writeIDref(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD, prefix + COALESCENT);
1002 break;
1003 case EXTENDED_SKYLINE:
1004 // only 1 coalescent, so write it separately after this method
1005 default:
1006 writer.writeIDref(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD, prefix + COALESCENT);
1007 }
1008 }
1009
1010 // id is written in writePriorLikelihood (PartitionTreePrior prior, PartitionTreeModel model, XMLWriter writer)
1011 public void writePriorLikelihoodReference(PartitionTreePrior prior, PartitionTreeModel model, XMLWriter writer) {
1012
1013 String prefix = model.getPrefix();
1014
1015 switch (prior.getNodeHeightPrior()) {
1016
1017 case YULE:
1018 case YULE_CALIBRATION:
1019 case BIRTH_DEATH:
1020 case BIRTH_DEATH_INCOMPLETE_SAMPLING:
1021 case BIRTH_DEATH_SERIAL_SAMPLING:
1022 case BIRTH_DEATH_BASIC_REPRODUCTIVE_NUMBER:
1023 writer.writeIDref(SpeciationLikelihoodParser.SPECIATION_LIKELIHOOD, prefix + "speciation");
1024 break;
1025 case SKYLINE:
1026 writer.writeIDref(BayesianSkylineLikelihoodParser.SKYLINE_LIKELIHOOD, prefix + "skyline");
1027 writer.writeIDref(ExponentialMarkovModel.EXPONENTIAL_MARKOV_MODEL, prefix + "eml1");
1028 break;
1029 case GMRF_SKYRIDE:
1030 writer.writeIDref(GMRFSkyrideLikelihoodParser.SKYLINE_LIKELIHOOD, prefix + "skyride");
1031 break;
1032 case SKYGRID:
1033 // writer.writeIDref(GMRFSkyrideLikelihoodParser.SKYLINE_LIKELIHOOD, prefix + "skygrid");
1034 // only 1 coalescent, so write it separately after this method
1035 break;
1036 // case LOGISTIC:
1037 // writer.writeIDref(BooleanLikelihoodParser.BOOLEAN_LIKELIHOOD, prefix + "booleanLikelihood1");
1038 // writer.writeIDref(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD, modelPrefix + COALESCENT);
1039 // break;
1040 case EXTENDED_SKYLINE:
1041 // only 1 coalescent, so write it in writeEBSPVariableDemographicReference
1042 default:
1043 writer.writeIDref(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD, prefix + COALESCENT);
1044 }
1045 }
1046
1047 public void writeMultiLociLikelihoodReference(PartitionTreePrior prior, XMLWriter writer) {
1048
1049 String prefix = prior.getPrefix();
1050
1051 //TODO: make suitable for *BEAST
1052 if (prior.getNodeHeightPrior() == TreePriorType.EXTENDED_SKYLINE) {
1053
1054 writer.writeIDref(CoalescentLikelihoodParser.COALESCENT_LIKELIHOOD, prefix + COALESCENT); // only 1 coalescent
1055
1056 writer.writeOpenTag(MixedDistributionLikelihoodParser.DISTRIBUTION_LIKELIHOOD);
1057
1058 writer.writeOpenTag(MixedDistributionLikelihoodParser.DISTRIBUTION0);
1059 writer.writeIDref(ExponentialDistributionModel.EXPONENTIAL_DISTRIBUTION_MODEL, prefix + "demographic.populationMeanDist");
1060 writer.writeCloseTag(MixedDistributionLikelihoodParser.DISTRIBUTION0);
1061
1062 writer.writeOpenTag(MixedDistributionLikelihoodParser.DISTRIBUTION1);
1063 writer.writeIDref(ExponentialDistributionModel.EXPONENTIAL_DISTRIBUTION_MODEL, prefix + "demographic.populationMeanDist");
1064 writer.writeCloseTag(MixedDistributionLikelihoodParser.DISTRIBUTION1);
1065
1066 writeParameterRef(MixedDistributionLikelihoodParser.DATA, prefix + "demographic.popSize", writer);
1067
1068 writeParameterRef(MixedDistributionLikelihoodParser.INDICATORS, prefix + "demographic.indicators", writer);
1069
1070 writer.writeCloseTag(MixedDistributionLikelihoodParser.DISTRIBUTION_LIKELIHOOD);
1071 } else if (prior.getNodeHeightPrior() == TreePriorType.SKYGRID) {
1072 writer.writeIDref(GMRFSkyrideLikelihoodParser.SKYGRID_LIKELIHOOD, prefix + "skygrid");
1073 }
1074
1075 }
1076 }