Debian Codegraph @

src/dr/app/beauti/components/continuous/ContinuousComponentGenerator.java

raw symbols SCIP
1 /*
2 * ContinuousComponentGenerator.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.components.continuous;
27
28 import dr.app.beauti.generator.BaseComponentGenerator;
29 import dr.app.beauti.options.*;
30 import dr.app.beauti.types.OperatorType;
31 import dr.app.beauti.util.XMLWriter;
32 import dr.evolution.datatype.ContinuousDataType;
33 import dr.evolution.util.Taxon;
34 import dr.evomodel.continuous.ContinuousDiffusionStatistic;
35 import dr.evomodelxml.tree.TreeLoggerParser;
36 import dr.inference.model.ParameterParser;
37 import dr.util.Attribute;
38 import dr.xml.AttributeParser;
39
40 /**
41 * @author Andrew Rambaut
42 * @version $Id$
43 */
44
45 public class ContinuousComponentGenerator extends BaseComponentGenerator {
46
47 protected ContinuousComponentGenerator(BeautiOptions options) {
48 super(options);
49 }
50
51 public boolean usesInsertionPoint(InsertionPoint point) {
52
53 if (options.getDataPartitions(ContinuousDataType.INSTANCE).size() == 0) {
54 // Empty, so do nothing
55 return false;
56 }
57
58 switch (point) {
59 case IN_TAXON:
60 case AFTER_SITE_MODEL:
61 case AFTER_TREE_LIKELIHOOD:
62 case IN_OPERATORS:
63 case IN_MCMC_PRIOR:
64 case IN_MCMC_LIKELIHOOD:
65 case IN_FILE_LOG_PARAMETERS:
66 case IN_FILE_LOG_LIKELIHOODS:
67 case IN_TREES_LOG:
68 return true;
69 default:
70 return false;
71 }
72
73 }// END: usesInsertionPoint
74
75 protected void generate(final InsertionPoint point, final Object item, final String prefix, final XMLWriter writer) {
76
77 ContinuousComponentOptions component = (ContinuousComponentOptions) options
78 .getComponentOptions(ContinuousComponentOptions.class);
79
80 switch (point) {
81 case IN_TAXON:
82 Taxon taxon = (Taxon)item;
83 writeTaxonTraits(taxon, writer);
84 break;
85 case AFTER_SITE_MODEL:
86 writeMultivariateDiffusionModels(writer, component);
87 break;
88 case AFTER_TREE_LIKELIHOOD:
89 writeMultivariateTreeLikelihoods(writer, component);
90 break;
91 case IN_OPERATORS:
92 writePrecisionGibbsOperators(writer, component);
93 break;
94 case IN_MCMC_PRIOR:
95 writeMultivariatePriors(writer, component);
96 break;
97 case IN_MCMC_LIKELIHOOD:
98 writeMultivariateTreeLikelihoodIdRefs(writer, component);
99 break;
100 case IN_FILE_LOG_PARAMETERS:
101 writeParameterIdRefs(writer, component);
102 break;
103 case IN_FILE_LOG_LIKELIHOODS:
104 writeMultivariateTreeLikelihoodIdRefs(writer, component);
105 break;
106 case IN_TREES_LOG:
107 writeTreeLogEntries((PartitionTreeModel)item, writer);
108 break;
109 default:
110 throw new IllegalArgumentException(
111 "This insertion point is not implemented for "
112 + this.getClass().getName());
113 }
114
115 }// END: generate
116
117 protected String getCommentLabel() {
118 return "Multivariate diffusion model";
119 }
120
121 private void writeTaxonTraits(Taxon taxon, XMLWriter writer) {
122 for (AbstractPartitionData partition : options.getDataPartitions(ContinuousDataType.INSTANCE)) {
123 writer.writeOpenTag(AttributeParser.ATTRIBUTE, new Attribute[]{
124 new Attribute.Default<String>(Attribute.NAME, partition.getName())});
125
126 StringBuilder sb = new StringBuilder();
127 boolean first = true;
128 for (TraitData trait : partition.getTraits()) {
129 if (!first) {
130 sb.append(" ");
131 }
132
133 if (taxon.containsAttribute(trait.getName())) {
134 sb.append(taxon.getAttribute(trait.getName()).toString());
135 } else {
136 sb.append("?");
137 }
138 first = false;
139 }
140 writer.writeText(sb.toString());
141 writer.writeCloseTag(AttributeParser.ATTRIBUTE);
142 }
143 }
144
145 private void writeMultivariateDiffusionModels(XMLWriter writer,
146 ContinuousComponentOptions component) {
147
148 boolean first = false;
149
150 for (PartitionSubstitutionModel model : component.getOptions().getPartitionSubstitutionModels(ContinuousDataType.INSTANCE)) {
151 String precisionMatrixId = model.getName() + ".precision";
152
153 if (!first) { writer.writeBlankLine(); } else { first = false; }
154
155 writeMultivariateDiffusionModel(writer, model, precisionMatrixId);
156
157 writer.writeBlankLine();
158
159 writeMultivariateWishartPrior(writer, model, precisionMatrixId);
160 }
161 }
162
163 private void writeMultivariateDiffusionModel(XMLWriter writer,
164 PartitionSubstitutionModel model,
165 String precisionMatrixId) {
166
167 writer.writeOpenTag("multivariateDiffusionModel",
168 new Attribute[] {
169 new Attribute.Default<String>("id", model.getName() + ".diffusionModel")
170 });
171
172 writer.writeOpenTag("precisionMatrix");
173 writer.writeOpenTag("matrixParameter",
174 new Attribute[] {
175 new Attribute.Default<String>("id", precisionMatrixId)
176 });
177
178 for (int i = 0; i < model.getContinuousTraitCount(); i++) {
179 StringBuilder sb = new StringBuilder();
180 for (int j = 0; j < model.getContinuousTraitCount(); j++) {
181 if (j > 0) {
182 sb.append(" ");
183 }
184 if (i == j) {
185 sb.append(0.05);
186 } else {
187 sb.append(0.002);
188 }
189 }
190 writer.writeTag("parameter",
191 new Attribute[]{
192 new Attribute.Default<String>("id", precisionMatrixId + ".col" + (i + 1)),
193 new Attribute.Default<String>("value", sb.toString())
194 }, true);
195 }
196
197 writer.writeCloseTag("matrixParameter");
198 writer.writeCloseTag("precisionMatrix");
199 writer.writeCloseTag("multivariateDiffusionModel");
200 }
201
202 private void writeMultivariateWishartPrior(XMLWriter writer,
203 PartitionSubstitutionModel model,
204 String precisionMatrixId) {
205
206 int n = model.getContinuousTraitCount();
207
208 writer.writeOpenTag("multivariateWishartPrior",
209 new Attribute[] {
210 new Attribute.Default<String>("id", model.getName() + ".precisionPrior"),
211 new Attribute.Default<String>("df", "" + n),
212 });
213
214 writer.writeOpenTag("scaleMatrix");
215 writer.writeOpenTag("matrixParameter");
216
217 for (int i = 0; i < n; i++) {
218 StringBuilder sb = new StringBuilder();
219 for (int j = 0; j < n; j++) {
220 if (j > 0) {
221 sb.append(" ");
222 }
223 if (i == j) {
224 sb.append(1.0);
225 } else {
226 sb.append(0.0);
227 }
228 }
229 writer.writeTag("parameter",
230 new Attribute[]{
231 new Attribute.Default<String>("value", sb.toString())
232 }, true);
233
234 }
235
236 writer.writeCloseTag("matrixParameter");
237 writer.writeCloseTag("scaleMatrix");
238
239 writer.writeOpenTag("data");
240 writer.writeIDref("parameter", precisionMatrixId);
241 writer.writeCloseTag("data");
242
243 writer.writeCloseTag("multivariateWishartPrior");
244 }
245
246 private void writeMultivariateTreeLikelihoods(XMLWriter writer,
247 ContinuousComponentOptions component) {
248
249 boolean first = true;
250
251 for (AbstractPartitionData partitionData : component.getOptions().getDataPartitions(ContinuousDataType.INSTANCE)) {
252 PartitionSubstitutionModel model = partitionData.getPartitionSubstitutionModel();
253 String diffusionModelId = model.getName() + ".diffusionModel";
254 String treeModelId = partitionData.getPartitionTreeModel().getPrefix() + "treeModel";
255
256 if (!first) { writer.writeBlankLine(); } else { first = false; }
257
258 if (model.getContinuousSubstModelType() != ContinuousSubstModelType.HOMOGENOUS &&
259 model.getContinuousSubstModelType() != ContinuousSubstModelType.DRIFT) {
260
261 if (ContinuousComponentOptions.USE_ARBITRARY_BRANCH_RATE_MODEL) {
262 writeArbitraryRateBranchRateModel(writer, partitionData, treeModelId);
263 } else {
264 writeRelaxedBranchRateModel(writer, partitionData, treeModelId);
265 }
266 }
267
268 writer.writeBlankLine();
269
270 writeMultivariateTreeLikelihood(writer, partitionData, diffusionModelId, treeModelId);
271
272 String precisionMatrixId = model.getName() + ".precision";
273
274 writeDiffusionStatistics(writer, partitionData, treeModelId, precisionMatrixId,
275 partitionData.getName() + ".traitLikelihood");
276 }
277 }
278
279
280
281 private void writeRelaxedBranchRateModel(XMLWriter writer,
282 AbstractPartitionData partitionData,
283 String treeModelId) {
284
285 String prefix = partitionData.getName() + ".";
286
287 writer.writeOpenTag("discretizedBranchRates",
288 new Attribute[] {
289 new Attribute.Default<String>("id",
290 prefix + "diffusion.branchRates"),
291 });
292
293 writer.writeIDref("treeModel", treeModelId);
294
295 writer.writeOpenTag("distribution");
296
297 if (partitionData.getPartitionSubstitutionModel().getContinuousSubstModelType() == ContinuousSubstModelType.LOGNORMAL_RRW) {
298 writer.writeOpenTag("logNormalDistributionModel",
299 new Attribute[]{ new Attribute.Default<String>("meanInRealSpace", "true") });
300
301 writer.writeOpenTag("mean");
302 writer.writeTag("parameter",
303 new Attribute[]{
304 new Attribute.Default<String>("value", "1.0")
305 }, true);
306
307 writer.writeCloseTag("mean");
308
309 writer.writeOpenTag("stdev");
310 writer.writeTag("parameter",
311 new Attribute[]{
312 new Attribute.Default<String>("id", prefix + ContinuousComponentOptions.STDEV),
313 new Attribute.Default<String>("value", "1.0"),
314 new Attribute.Default<String>("lower", "0.0")
315 }, true);
316
317 writer.writeCloseTag("stdev");
318 writer.writeCloseTag("logNormalDistributionModel");
319 } else {
320 writer.writeOpenTag("onePGammaDistributionModel");
321 writer.writeOpenTag("shape");
322 switch (partitionData.getPartitionSubstitutionModel().getContinuousSubstModelType()) {
323 case CAUCHY_RRW:
324 writer.writeComment("half DF (i.e., df = 1)");
325 writer.writeTag("parameter",
326 new Attribute[]{
327 // don't think this needs an id
328 // new Attribute.Default<String>("id", "halfDF"),
329 new Attribute.Default<String>("value", "0.5")
330 }, true);
331 break;
332 case GAMMA_RRW:
333 writer.writeComment("half DF");
334 writer.writeTag("parameter",
335 new Attribute[]{
336 new Attribute.Default<String>("id", prefix + ContinuousComponentOptions.HALF_DF),
337 new Attribute.Default<String>("value", "0.5")
338 }, true);
339 break;
340 case LOGNORMAL_RRW:
341 case DRIFT:
342 case HOMOGENOUS:
343 throw new IllegalArgumentException("Shouldn't be here");
344 default:
345 throw new IllegalArgumentException("Unknown continuous substitution type");
346 }
347 writer.writeCloseTag("shape");
348 writer.writeCloseTag("onePGammaDistributionModel");
349 }
350 writer.writeCloseTag("distribution");
351
352
353 writer.writeOpenTag("rateCategories");
354 writer.writeTag("parameter", new Attribute.Default<String>("id", prefix + "rrwCategories"), true);
355 writer.writeCloseTag("rateCategories");
356
357 writer.writeCloseTag("discretizedBranchRates");
358 }
359
360 private void writeArbitraryRateBranchRateModel(XMLWriter writer,
361 AbstractPartitionData partitionData,
362 String treeModelId) {
363
364 String prefix = partitionData.getName() + ".";
365
366 writer.writeOpenTag("arbitraryBranchRates",
367 new Attribute[] {
368 new Attribute.Default<String>("id",
369 prefix + "diffusion.branchRates"),
370 });
371
372 writer.writeIDref("treeModel", treeModelId);
373 writer.writeOpenTag("rates");
374 writer.writeTag("parameter",
375 new Attribute[]{
376 new Attribute.Default<String>("id", prefix + "diffusion.rates"),
377 new Attribute.Default<String>("lower", "0.0")
378 }, true);
379 writer.writeCloseTag("rates");
380
381 writer.writeCloseTag("arbitraryBranchRates");
382
383 writer.writeOpenTag("distributionLikelihood",
384 new Attribute[]{
385 new Attribute.Default<String>("id", prefix + "diffusion.prior")
386 });
387
388 writer.writeOpenTag("data");
389 writer.writeIDref("parameter", prefix + "diffusion.rates");
390 writer.writeCloseTag("data");
391
392 writer.writeOpenTag("distribution");
393
394 if (partitionData.getPartitionSubstitutionModel().getContinuousSubstModelType() == ContinuousSubstModelType.LOGNORMAL_RRW) {
395 writer.writeOpenTag("logNormalDistributionModel",
396 new Attribute[]{ new Attribute.Default<String>("meanInRealSpace", "true") });
397
398 writer.writeOpenTag("mean");
399 writer.writeTag("parameter",
400 new Attribute[]{
401 new Attribute.Default<String>("value", "1.0")
402 }, true);
403
404 writer.writeCloseTag("mean");
405
406 writer.writeOpenTag("stdev");
407 writer.writeTag("parameter",
408 new Attribute[]{
409 new Attribute.Default<String>("id", prefix + ContinuousComponentOptions.STDEV),
410 new Attribute.Default<String>("value", "1.0"),
411 new Attribute.Default<String>("lower", "0.0")
412 }, true);
413
414 writer.writeCloseTag("stdev");
415 writer.writeCloseTag("logNormalDistributionModel");
416 } else {
417 writer.writeOpenTag("onePGammaDistributionModel");
418 writer.writeOpenTag("shape");
419 switch (partitionData.getPartitionSubstitutionModel().getContinuousSubstModelType()) {
420 case CAUCHY_RRW:
421 writer.writeComment("half DF (i.e., df = 1)");
422 writer.writeTag("parameter",
423 new Attribute[]{
424 // don't think this needs an id
425 // new Attribute.Default<String>("id", "halfDF"),
426 new Attribute.Default<String>("value", "0.5")
427 }, true);
428 break;
429 case GAMMA_RRW:
430 writer.writeComment("half DF");
431 writer.writeTag("parameter",
432 new Attribute[]{
433 new Attribute.Default<String>("id", prefix + ContinuousComponentOptions.HALF_DF),
434 new Attribute.Default<String>("value", "0.5")
435 }, true);
436 break;
437 case LOGNORMAL_RRW:
438 case DRIFT:
439 case HOMOGENOUS:
440 throw new IllegalArgumentException("Shouldn't be here");
441 default:
442 throw new IllegalArgumentException("Unknown continuous substitution type");
443 }
444 writer.writeCloseTag("shape");
445 writer.writeCloseTag("onePGammaDistributionModel");
446 }
447 writer.writeCloseTag("distribution");
448
449 writer.writeCloseTag("distributionLikelihood");
450 }
451
452 private void writeMultivariateTreeLikelihood(XMLWriter writer,
453 AbstractPartitionData partitionData,
454 String diffusionModelId,
455 String treeModelId) {
456
457 int traitDimension = 1; // todo - set this to trait dimension
458 writer.writeOpenTag("multivariateTraitLikelihood",
459 new Attribute[] {
460 new Attribute.Default<String>("id", partitionData.getName() + ".traitLikelihood"),
461 new Attribute.Default<String>("traitName", partitionData.getName()),
462 new Attribute.Default<String>("useTreeLength", "true"),
463 new Attribute.Default<String>("scaleByTime", "true"),
464 new Attribute.Default<String>("reportAsMultivariate", "true"),
465 new Attribute.Default<String>("reciprocalRates", "true"),
466 new Attribute.Default<String>("integrateInternalTraits", "true")
467 });
468
469 writer.writeIDref("multivariateDiffusionModel", diffusionModelId);
470
471
472 ContinuousComponentOptions component = (ContinuousComponentOptions) options
473 .getComponentOptions(ContinuousComponentOptions.class);
474
475 PartitionSubstitutionModel model = partitionData.getPartitionSubstitutionModel();
476
477 if (component.useLambda(model)) {
478 writer.writeOpenTag("transformedTreeModel");
479 writer.writeIDref("treeModel", treeModelId);
480 writer.writeTag("parameter", new Attribute[] {
481 new Attribute.Default<String>("id", partitionData.getName() + "." + ContinuousComponentOptions.LAMBDA),
482 new Attribute.Default<String>("value", "0.5"),
483 new Attribute.Default<String>("lower", "0.0"),
484 new Attribute.Default<String>("upper", "1.0")
485 }, true);
486 writer.writeCloseTag("transformedTreeModel");
487 } else {
488 writer.writeIDref("treeModel", treeModelId);
489 }
490
491 writer.writeOpenTag("traitParameter");
492 writer.writeTag("parameter", new Attribute.Default<String>("id", "leaf." + partitionData.getName()), true);
493 writer.writeCloseTag("traitParameter");
494
495 if (partitionData.getPartitionSubstitutionModel().getContinuousSubstModelType() == ContinuousSubstModelType.DRIFT) {
496 writer.writeOpenTag("driftModels");
497 for (int i = 0; i < traitDimension; i++) {
498 writer.writeOpenTag("strictClockBranchRates");
499 writer.writeTag("parameter", new Attribute[]{
500 new Attribute.Default<String>("id", partitionData.getName() + "." + ContinuousComponentOptions.DRIFT_RATE +
501 (traitDimension > 1 ? "." + i : "")),
502 new Attribute.Default<String>("value", "0.0"),
503 }, true);
504 writer.writeCloseTag("strictClockBranchRates");
505 }
506 writer.writeCloseTag("driftModels");
507 }
508
509 if (model.getJitterWindow() > 0.0) {
510 StringBuilder sb = new StringBuilder(Double.toString(model.getJitterWindow()));
511 for (int i = 1; i < model.getContinuousTraitCount(); i++) {
512 sb.append(" ").append(Double.toString(model.getJitterWindow()));
513 }
514 writer.writeOpenTag("jitter", new Attribute[] {
515 new Attribute.Default<String>("window", sb.toString()),
516 new Attribute.Default<String>("duplicatesOnly", "true")
517 });
518 writer.writeTag("parameter", new Attribute.Default<String>("idref", "leaf." + partitionData.getName()), true);
519 writer.writeCloseTag("jitter");
520 }
521
522 writer.writeOpenTag("conjugateRootPrior");
523
524 writer.writeOpenTag("meanParameter");
525 StringBuilder sb = new StringBuilder();
526 for (int j = 0; j < partitionData.getTraits().size(); j++) {
527 if (j > 0) {
528 sb.append(" ");
529 }
530 sb.append(0.0);
531 }
532
533 writer.writeTag("parameter", new Attribute.Default<String>("value", sb.toString()), true);
534 writer.writeCloseTag("meanParameter");
535
536 writer.writeOpenTag("priorSampleSize");
537 writer.writeTag("parameter", new Attribute.Default<String>("value", "0.000001"), true);
538 writer.writeCloseTag("priorSampleSize");
539
540 writer.writeCloseTag("conjugateRootPrior");
541
542 if (partitionData.getPartitionSubstitutionModel().getContinuousSubstModelType() != ContinuousSubstModelType.HOMOGENOUS) {
543 if (ContinuousComponentOptions.USE_ARBITRARY_BRANCH_RATE_MODEL) {
544 writer.writeIDref("arbitraryBranchRates", partitionData.getName() + "." + "diffusion.branchRates");
545 } else {
546 writer.writeIDref("discretizedBranchRates", partitionData.getName() + "." + "diffusion.branchRates");
547 }
548 }
549
550 writer.writeCloseTag("multivariateTraitLikelihood");
551
552 if (traitDimension > 1) {
553 writer.writeOpenTag("compoundParameter",
554 new Attribute.Default<String>("id", partitionData.getName() + "." + ContinuousComponentOptions.DRIFT_RATE));
555 for (int i = 0; i < traitDimension; i++) { // todo iterate over dimension of trait
556 writer.writeTag("parameter", new Attribute.Default<String>("idref", partitionData.getName() + "." + ContinuousComponentOptions.DRIFT_RATE + "." + i), true);
557 }
558 writer.writeCloseTag("priorSampleSize");
559 }
560
561 }
562
563 private void writeDiffusionStatistics(XMLWriter writer, AbstractPartitionData partitionData,
564 String treeModelId, String precisionMatrixId, String traitLikelihoodId) {
565 String prefix = partitionData.getName() + ".";
566
567 if (partitionData.getTraits().size() == 2) {
568 writer.writeOpenTag("correlation",
569 new Attribute[] {
570 new Attribute.Default<String>("id", prefix + "correlation"),
571 new Attribute.Default<Integer>("dimension1", 1),
572 new Attribute.Default<Integer>("dimension2", 2)
573 });
574 writer.writeIDref("matrixParameter", precisionMatrixId);
575 writer.writeCloseTag("correlation");
576 }
577
578 /*
579 writer.writeOpenTag("treeLengthStatistic",
580 new Attribute[] {
581 new Attribute.Default<String>("id", prefix + "treeLength")
582 });
583 writer.writeIDref("treeModel", treeModelId);
584 writer.writeCloseTag("treeLengthStatistic");
585
586
587 writer.writeOpenTag("productStatistic",
588 new Attribute[] {
589 new Attribute.Default<String>("id", prefix + "treeLengthPrecision1")
590 });
591 writer.writeIDref("treeLengthStatistic", prefix + "treeLength");
592 writer.writeOpenTag("subStatistic",
593 new Attribute[] {
594 new Attribute.Default<String>("id", prefix + "precision1"),
595 new Attribute.Default<String>("dimension", "0")
596 });
597 writer.writeIDref("parameter", prefix + "precision.col1");
598 writer.writeCloseTag("subStatistic");
599 writer.writeCloseTag("productStatistic");
600
601 writer.writeOpenTag("productStatistic",
602 new Attribute[] {
603 new Attribute.Default<String>("id", prefix + "treeLengthPrecision2")
604 });
605 writer.writeIDref("treeLengthStatistic", prefix + "treeLength");
606 writer.writeOpenTag("subStatistic",
607 new Attribute[] {
608 new Attribute.Default<String>("id", prefix + "precision2"),
609 new Attribute.Default<String>("dimension", "1")
610 });
611 writer.writeIDref("parameter", prefix + "precision.col2");
612 writer.writeCloseTag("subStatistic");
613 writer.writeCloseTag("productStatistic");
614 */
615
616 writer.writeOpenTag("matrixInverse",
617 new Attribute[] {
618 new Attribute.Default<String>("id", prefix + "varCovar")
619 });
620 writer.writeIDref("matrixParameter", precisionMatrixId);
621 writer.writeCloseTag("matrixInverse");
622
623 writer.writeOpenTag(ContinuousDiffusionStatistic.CONTINUOUS_DIFFUSION_STATISTIC, (partitionData.getPartitionSubstitutionModel().isLatitudeLongitude() ?
624 new Attribute[] {
625 new Attribute.Default<String>("id", prefix + "diffusionRate"),
626 new Attribute.Default<String>("greatCircleDistance", "true")
627 } :
628 new Attribute[] {
629 new Attribute.Default<String>("id", prefix + "diffusionRate"),
630 }));
631 writer.writeIDref("multivariateTraitLikelihood", traitLikelihoodId);
632 writer.writeCloseTag(ContinuousDiffusionStatistic.CONTINUOUS_DIFFUSION_STATISTIC);
633 }
634
635 private void writePrecisionGibbsOperators(XMLWriter writer,
636 ContinuousComponentOptions component) {
637
638 for (AbstractPartitionData partitionData : component.getOptions().getDataPartitions(ContinuousDataType.INSTANCE)) {
639 writePrecisionGibbsOperator(writer, component, partitionData);
640 }
641 }
642
643 private void writePrecisionGibbsOperator(final XMLWriter writer,
644 final ContinuousComponentOptions component,
645 AbstractPartitionData partitionData
646 ) {
647 writer.writeOpenTag(ContinuousComponentOptions.PRECISION_GIBBS_OPERATOR,
648 new Attribute[] {
649 new Attribute.Default<String>("weight", "" + partitionData.getTraits().size())
650 });
651 writer.writeIDref("multivariateTraitLikelihood", partitionData.getName() + ".traitLikelihood");
652 writer.writeIDref("multivariateWishartPrior", partitionData.getPartitionSubstitutionModel().getName() + ".precisionPrior");
653 writer.writeCloseTag(ContinuousComponentOptions.PRECISION_GIBBS_OPERATOR);
654
655 }
656
657 private void writeParameterIdRefs(final XMLWriter writer, final ContinuousComponentOptions component) {
658 for (AbstractPartitionData partitionData : component.getOptions().getDataPartitions(ContinuousDataType.INSTANCE)) {
659 PartitionSubstitutionModel model = partitionData.getPartitionSubstitutionModel();
660 writer.writeIDref("matrixParameter", model.getName() + ".precision");
661
662 String prefix = partitionData.getName() + ".";
663 if (partitionData.getTraits().size() == 2) {
664 writer.writeIDref("correlation", prefix + "correlation");
665 // writer.writeIDref("treeLengthStatistic", prefix + "treeLength");
666 // writer.writeIDref("productStatistic", prefix + "treeLengthPrecision1");
667 // writer.writeIDref("productStatistic", prefix + "treeLengthPrecision2");
668 }
669 writer.writeIDref("matrixInverse", prefix + "varCovar");
670 writer.writeIDref(ContinuousDiffusionStatistic.CONTINUOUS_DIFFUSION_STATISTIC, prefix + "diffusionRate");
671
672 if (partitionData.getPartitionSubstitutionModel().getContinuousSubstModelType() == ContinuousSubstModelType.GAMMA_RRW) {
673 writer.writeIDref(ParameterParser.PARAMETER, prefix + ContinuousComponentOptions.HALF_DF);
674 } else if (partitionData.getPartitionSubstitutionModel().getContinuousSubstModelType() == ContinuousSubstModelType.LOGNORMAL_RRW) {
675 writer.writeIDref(ParameterParser.PARAMETER, prefix + ContinuousComponentOptions.STDEV);
676 } else if (partitionData.getPartitionSubstitutionModel().getContinuousSubstModelType() == ContinuousSubstModelType.DRIFT) {
677 writer.writeIDref(ParameterParser.PARAMETER, prefix + ContinuousComponentOptions.DRIFT_RATE);
678 }
679
680 if (component.useLambda(model)) {
681 writer.writeIDref("parameter", model.getName() + "." + ContinuousComponentOptions.LAMBDA);
682 }
683 }
684 }
685
686 private void writeMultivariatePriors(XMLWriter writer,
687 ContinuousComponentOptions component) {
688
689 if (ContinuousComponentOptions.USE_ARBITRARY_BRANCH_RATE_MODEL) {
690 for (AbstractPartitionData partitionData : component.getOptions().getDataPartitions(ContinuousDataType.INSTANCE)) {
691 PartitionSubstitutionModel model = partitionData.getPartitionSubstitutionModel();
692 if (model.getContinuousSubstModelType() != ContinuousSubstModelType.HOMOGENOUS &&
693 model.getContinuousSubstModelType() != ContinuousSubstModelType.DRIFT) {
694 writer.writeIDref("distributionLikelihood", partitionData.getName() + "." + "diffusion.prior");
695 }
696 }
697 }
698 for (AbstractPartitionData partitionData : component.getOptions().getDataPartitions(ContinuousDataType.INSTANCE)) {
699 writer.writeIDref("multivariateWishartPrior", partitionData.getName() + ".precisionPrior");
700 }
701
702 }
703
704 private void writeMultivariateTreeLikelihoodIdRefs(XMLWriter writer,
705 ContinuousComponentOptions component) {
706
707 for (AbstractPartitionData partitionData : component.getOptions().getDataPartitions(ContinuousDataType.INSTANCE)) {
708 writer.writeIDref("multivariateTraitLikelihood", partitionData.getName() + ".traitLikelihood");
709 }
710 }
711
712 private void writeTreeLogEntries(PartitionTreeModel treeModel, XMLWriter writer) {
713 for (AbstractPartitionData partitionData : options.getDataPartitions(ContinuousDataType.INSTANCE)) {
714 if (partitionData.getPartitionTreeModel() == treeModel) {
715 PartitionSubstitutionModel model = partitionData.getPartitionSubstitutionModel();
716 writer.writeIDref("multivariateDiffusionModel", model.getName() + ".diffusionModel");
717 writer.writeIDref("multivariateTraitLikelihood", partitionData.getName() + ".traitLikelihood");
718 if (model.getContinuousSubstModelType() != ContinuousSubstModelType.HOMOGENOUS) {
719 writer.writeOpenTag(TreeLoggerParser.TREE_TRAIT,
720 new Attribute[] {
721 new Attribute.Default<String>(TreeLoggerParser.NAME, "rate"),
722 new Attribute.Default<String>(TreeLoggerParser.TAG, partitionData.getName() + ".rate"),
723 });
724 if (ContinuousComponentOptions.USE_ARBITRARY_BRANCH_RATE_MODEL) {
725 writer.writeIDref("arbitraryBranchRates", partitionData.getName() + "." + "diffusion.branchRates");
726 } else {
727 writer.writeIDref("discretizedBranchRates", partitionData.getName() + "." + "diffusion.branchRates");
728 }
729
730 writer.writeCloseTag(TreeLoggerParser.TREE_TRAIT);
731 }
732 }
733 }
734 }
735 }