Debian Codegraph @

src/dr/app/beauti/components/tipdatesampling/TipDateSamplingComponentGenerator.java

raw symbols SCIP
1 /*
2 * TipDateSamplingComponentGenerator.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.tipdatesampling;
27
28 import dr.app.beauti.options.AbstractPartitionData;
29 import dr.app.beauti.options.PartitionTreeModel;
30 import dr.app.beauti.util.XMLWriter;
31 import dr.app.beauti.types.TipDateSamplingType;
32 import dr.app.beauti.generator.BaseComponentGenerator;
33 import dr.app.beauti.options.BeautiOptions;
34 import dr.evolution.util.Date;
35 import dr.evolution.util.Taxa;
36 import dr.evolution.util.Taxon;
37 import dr.evolution.util.TaxonList;
38 import dr.evoxml.TaxonParser;
39 import dr.inference.model.ParameterParser;
40 import dr.util.Attribute;
41 import dr.xml.XMLParser;
42
43 import java.util.HashSet;
44 import java.util.Set;
45
46 /**
47 * @author Andrew Rambaut
48 * @version $Id$
49 */
50 public class TipDateSamplingComponentGenerator extends BaseComponentGenerator {
51
52 public TipDateSamplingComponentGenerator(final BeautiOptions options) {
53 super(options);
54 }
55
56 public boolean usesInsertionPoint(final InsertionPoint point) {
57 TipDateSamplingComponentOptions comp = (TipDateSamplingComponentOptions)options.getComponentOptions(TipDateSamplingComponentOptions.class);
58
59 if (comp.tipDateSamplingType == TipDateSamplingType.NO_SAMPLING) {
60 return false;
61 }
62
63 switch (point) {
64 case IN_TREE_MODEL:
65 case IN_FILE_LOG_PARAMETERS:
66 return true;
67 case AFTER_TREE_MODEL:
68 return options.getPartitionTreeModels().size() > 1 || comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT;
69 case IN_MCMC_PRIOR:
70 return comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT;
71 default:
72 return false;
73 }
74 }
75
76 protected void generate(final InsertionPoint point, final Object item, final String prefix, final XMLWriter writer) {
77 TipDateSamplingComponentOptions comp = (TipDateSamplingComponentOptions)options.getComponentOptions(TipDateSamplingComponentOptions.class);
78
79 TaxonList taxa = comp.getTaxonSet();
80
81 switch (point) {
82 case IN_TREE_MODEL: {
83 writeLeafHeightParameters(writer, (PartitionTreeModel)item, taxa);
84 } break;
85 case AFTER_TREE_MODEL:
86 if (options.getPartitionTreeModels().size() > 1) {
87 // we have multiple treeModels with some or all the same taxa - create a JointParameter for each...
88
89 writeJointParameters(writer, taxa);
90 }
91
92 if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT) {
93 writer.writeOpenTag("compoundParameter",
94 new Attribute[]{
95 new Attribute.Default<String>(XMLParser.ID, "treeModel.tipDates"),
96 }
97 );
98 for (int i = 0; i < taxa.getTaxonCount(); i++) {
99 Taxon taxon = taxa.getTaxon(i);
100 writer.writeIDref(ParameterParser.PARAMETER, "age(" + taxon.getId() + ")");
101 }
102
103 writer.writeCloseTag("compoundParameter");
104 }
105 break;
106 case IN_MCMC_PRIOR:
107 if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_INDIVIDUALLY ||
108 comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_PRECISION) {
109 // nothing to do - individual parameter priors are written automatically
110 } else if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT) {
111
112 }
113 break;
114 case IN_FILE_LOG_PARAMETERS:
115 if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_INDIVIDUALLY ||
116 comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_PRECISION) {
117 for (int i = 0; i < taxa.getTaxonCount(); i++) {
118 Taxon taxon = taxa.getTaxon(i);
119 writer.writeIDref(ParameterParser.PARAMETER, "age(" + taxon.getId() + ")");
120 }
121 } else if (comp.tipDateSamplingType == TipDateSamplingType.SAMPLE_JOINT) {
122 writer.writeIDref(ParameterParser.PARAMETER, "treeModel.tipDates");
123 }
124 break;
125 default:
126 throw new IllegalArgumentException("This insertion point is not implemented for " + this.getClass().getName());
127 }
128
129 }
130
131 private void writeJointParameters(XMLWriter writer, TaxonList taxa) {
132 for (int i = 0; i < taxa.getTaxonCount(); i++) {
133 Taxon taxon = taxa.getTaxon(i);
134
135 Set<PartitionTreeModel> treeModels = new HashSet<PartitionTreeModel>();
136 for (PartitionTreeModel treeModel : options.getPartitionTreeModels()) {
137 for (AbstractPartitionData data : options.getDataPartitions(treeModel)) {
138 if (data.getTaxonList().asList().contains(taxon)) {
139 treeModels.add(treeModel);
140 }
141 }
142 }
143
144 // if we are sampling within precisions then only include this leaf if precision > 0
145 if (treeModels.size() > 0) {
146 writer.writeOpenTag("jointParameter",
147 new Attribute[]{
148 new Attribute.Default<String>(XMLParser.ID, "age(" + taxon.getId() + ")")
149 }
150 );
151
152 for (PartitionTreeModel treeModel : treeModels) {
153 writer.writeTag(ParameterParser.PARAMETER, new Attribute.Default<String>(XMLParser.IDREF, treeModel.getPrefix() + "age(" + taxon.getId() + ")"), true);
154 }
155
156 writer.writeCloseTag("jointParameter");
157 }
158 }
159 }
160
161
162 private void writeLeafHeightParameters(XMLWriter writer, PartitionTreeModel item, TaxonList taxa) {
163 // only include this taxon as a leaf height if it found in this partition.
164 PartitionTreeModel treeModel = (PartitionTreeModel)item;
165
166 Set<Taxon> taxonSet = new HashSet<Taxon>();
167 for (AbstractPartitionData data : options.getDataPartitions(treeModel)) {
168 if (data.getTaxonList() != null) {
169 for (Taxon taxon : data.getTaxonList()) {
170 taxonSet.add(taxon);
171 }
172 }
173 }
174
175 for (int i = 0; i < taxa.getTaxonCount(); i++) {
176 Taxon taxon = taxa.getTaxon(i);
177
178 if (taxonSet.contains(taxon)) {
179 // if we are sampling within precisions then only include this leaf if precision > 0
180
181 writer.writeOpenTag("leafHeight",
182 new Attribute[]{
183 new Attribute.Default<String>(TaxonParser.TAXON, taxon.getId()),
184 }
185 );
186 writer.writeTag(ParameterParser.PARAMETER, new Attribute.Default<String>(XMLParser.ID, treeModel.getPrefix() + "age(" + taxon.getId() + ")"), true);
187 writer.writeCloseTag("leafHeight");
188 }
189 }
190 }
191
192 protected String getCommentLabel() {
193 return "Tip date sampling";
194 }
195
196 }