Debian Codegraph @

src/dr/app/beauti/components/dollo/DolloComponentGenerator.java

raw symbols SCIP
1 /*
2 * DolloComponentGenerator.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.dollo;
27
28 import dr.app.beauti.generator.BaseComponentGenerator;
29 import dr.app.beauti.options.*;
30 import dr.app.beauti.util.XMLWriter;
31 import dr.evolution.datatype.DataType;
32 import dr.evomodel.branchratemodel.BranchRateModel;
33 import dr.oldevomodel.sitemodel.GammaSiteModel;
34 import dr.oldevomodel.sitemodel.SiteModel;
35 import dr.evomodel.tree.TreeModel;
36 import dr.oldevomodelxml.MSSD.ALSSiteModelParser;
37 import dr.oldevomodelxml.MSSD.ALSTreeLikelihoodParser;
38 import dr.evomodelxml.branchratemodel.DiscretizedBranchRatesParser;
39 import dr.evomodelxml.branchratemodel.RandomLocalClockModelParser;
40 import dr.evomodelxml.branchratemodel.StrictClockBranchRatesParser;
41 import dr.oldevomodelxml.sitemodel.GammaSiteModelParser;
42 import dr.oldevomodelxml.substmodel.MutationDeathModelParser;
43 import dr.oldevomodelxml.treelikelihood.TreeLikelihoodParser;
44 import dr.evoxml.AlignmentParser;
45 import dr.evoxml.MutationDeathTypeParser;
46 import dr.evoxml.SitePatternsParser;
47 import dr.inference.model.ParameterParser;
48 import dr.util.Attribute;
49 import dr.xml.XMLParser;
50
51 /**
52 * @author Marc Suchard
53 * @version $Id$
54 */
55
56 public class DolloComponentGenerator extends BaseComponentGenerator {
57
58 protected DolloComponentGenerator(BeautiOptions options) {
59 super(options);
60 }
61
62 public boolean usesInsertionPoint(InsertionPoint point) {
63
64 DolloComponentOptions component = (DolloComponentOptions) options
65 .getComponentOptions(DolloComponentOptions.class);
66
67 if (!component.isActive()) {
68 return false;
69 }
70
71 switch (point) {
72 case AFTER_TAXA:
73 case AFTER_SUBSTITUTION_MODEL:
74 case IN_FILE_LOG_PARAMETERS:
75 // case IN_MCMC_LIKELIHOOD:
76 // case AFTER_TREE_LIKELIHOOD:
77 // case IN_OPERATORS:
78 // case IN_TREES_LOG:
79 // case AFTER_TREES_LOG:
80 // case AFTER_MCMC:
81 return true;
82 default:
83 return false;
84 }
85
86 }// END: usesInsertionPoint
87
88 protected void generate(final InsertionPoint point, final Object item, final String prefix, final XMLWriter writer) {
89
90 DolloComponentOptions component = (DolloComponentOptions) options
91 .getComponentOptions(DolloComponentOptions.class);
92
93 if (!includeStochasticDollo()) {
94 return;
95 }
96
97 switch (point) {
98 case AFTER_TAXA:
99 writeDataType(writer);
100 break;
101 case AFTER_SUBSTITUTION_MODEL:
102 writeDolloSubstitutionModels(writer, component);
103 break;
104 // case AFTER_TREE_LIKELIHOOD:
105 // writeDolloTreeLikelihoods(writer, component);
106 // break;
107 case IN_OPERATORS:
108 break;
109 // case IN_MCMC_LIKELIHOOD:
110 // writeDolloTreeLikelihoodReferences(writer);
111 // break;
112
113 case IN_SCREEN_LOG:
114 writeScreenLogEntries(writer, component);
115 break;
116 case IN_FILE_LOG_PARAMETERS:
117 writeLog(writer, component);
118 break;
119 case IN_TREES_LOG:
120 break;
121 case AFTER_TREES_LOG:
122 break;
123 case AFTER_MCMC:
124 break;
125 default:
126 throw new IllegalArgumentException(
127 "This insertion point is not implemented for "
128 + this.getClass().getName());
129 }
130
131 }// END: generate
132
133 private void writeScreenLogEntries(XMLWriter writer, DolloComponentOptions component) {
134 }
135
136 // private void writeDolloTreeLikelihoodReferences(XMLWriter writer) {
137 //
138 // for (AbstractPartitionData partition : options.dataPartitions) {
139 // PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel();
140 // String prefix = partition.getName();
141 // if (model.isDolloModel()) {
142 // writer.writeIDref(ALSTreeLikelihoodParser.LIKE_NAME,
143 // prefix + ALSTreeLikelihoodParser.LIKE_NAME);
144 // }
145 // }
146 // }
147
148 private void writeLog(XMLWriter writer, DolloComponentOptions component) {
149 for (AbstractPartitionData partition : options.dataPartitions) {
150 PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel();
151 if (model.isDolloModel()) {
152 String prefix = partition.getName() + ".";
153 writer.writeIDref(ParameterParser.PARAMETER,
154 component.getOptions().getParameter(prefix + DolloComponentOptions.DEATH_RATE).getName());
155 }
156 }
157 }
158
159 private void writeDataType(XMLWriter writer) {
160 writer.writeOpenTag(MutationDeathTypeParser.MODEL_NAME,
161 new Attribute.Default<String>(XMLParser.ID, DolloComponentOptions.DATA_NAME));
162 writer.writeTag(MutationDeathTypeParser.EXTANT,
163 new Attribute.Default<String>(MutationDeathTypeParser.CODE, "1"), true);
164 writer.writeTag(MutationDeathTypeParser.STATE,
165 new Attribute.Default<String>(MutationDeathTypeParser.CODE, "0"), true);
166 writer.writeTag(MutationDeathTypeParser.AMBIGUITY,
167 new Attribute.Default<String>(MutationDeathTypeParser.CODE, "-"), true); // Some users use this specification
168 writer.writeTag(MutationDeathTypeParser.AMBIGUITY,
169 new Attribute.Default<String>(MutationDeathTypeParser.CODE, "?"), true);
170 writer.writeCloseTag(MutationDeathTypeParser.MODEL_NAME);
171 }
172
173
174 private boolean includeStochasticDollo() {
175 for (AbstractPartitionData partition : options.dataPartitions) {
176 PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel();
177 if (model.isDolloModel()) {
178 return true;
179 }
180 }
181 return false;
182 }
183
184 @Override
185 protected String getCommentLabel() {
186 return "Stochastic Dollo";
187 }
188
189 private void writeDolloSubstitutionModels(XMLWriter writer, DolloComponentOptions component) {
190
191 // generate tree likelihoods for stochastic Dollo partitions
192 for (AbstractPartitionData partition : options.dataPartitions) {
193 PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel();
194 if (model.isDolloModel()) {
195 writeDolloSubstitutionModel(partition, writer, component);
196 writeDolloSiteModel(partition, writer, component);
197 writeDolloTreeLikelihood(TreeLikelihoodParser.TREE_LIKELIHOOD, -1, partition, writer);
198 }
199 }
200 }
201
202 private void writeDolloSubstitutionModel(AbstractPartitionData partition, XMLWriter writer, DolloComponentOptions component) {
203 String prefix = partition.getName() + ".";
204 // String prefix = partition.getPrefix(); // TODO Fix
205 writer.writeOpenTag(MutationDeathModelParser.MD_MODEL,
206 new Attribute.Default<String>(XMLParser.ID, prefix + DolloComponentOptions.MODEL_NAME ));
207 writeParameter(prefix + DolloComponentOptions.DEATH_RATE,
208 component.getOptions().getParameter(prefix + DolloComponentOptions.DEATH_RATE), writer);
209 writer.writeTag(DataType.DATA_TYPE, new Attribute.Default<String>(XMLParser.IDREF, DolloComponentOptions.DATA_NAME), true);
210 writer.writeCloseTag(MutationDeathModelParser.MD_MODEL);
211 writer.write("\n");
212 }
213
214 private void writeDolloSiteModel(AbstractPartitionData partition, XMLWriter writer, DolloComponentOptions components) {
215 String prefix = partition.getName() + ".";
216 writer.writeOpenTag(ALSSiteModelParser.ALS_SITE_MODEL,
217 new Attribute[]{new Attribute.Default<String>(XMLParser.ID, prefix + SiteModel.SITE_MODEL)});
218
219
220 writer.writeOpenTag(GammaSiteModelParser.SUBSTITUTION_MODEL);
221 writer.writeIDref(MutationDeathModelParser.MD_MODEL, prefix + DolloComponentOptions.MODEL_NAME);
222 writer.writeCloseTag(GammaSiteModelParser.SUBSTITUTION_MODEL);
223
224 PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel();
225 if (model.hasCodonPartitions()) {
226 writeParameter(GammaSiteModelParser.RELATIVE_RATE, "mu", model, writer);
227 }
228
229 if (model.isGammaHetero()) {
230 writer.writeOpenTag(GammaSiteModelParser.GAMMA_SHAPE,
231 new Attribute.Default<String>(GammaSiteModelParser.GAMMA_CATEGORIES, "" + model.getGammaCategories()));
232 writeParameter(prefix + "alpha", model, writer);
233 writer.writeCloseTag(GammaSiteModelParser.GAMMA_SHAPE);
234 }
235
236 if (model.isInvarHetero()) {
237 writeParameter(GammaSiteModelParser.PROPORTION_INVARIANT, "pInv", model, writer);
238 }
239
240 writer.writeCloseTag(ALSSiteModelParser.ALS_SITE_MODEL);
241 }
242
243 // private void writeDolloTreeLikelihoods(XMLWriter writer, DolloComponentOptions component) {
244 //
245 // // generate tree likelihoods for stochastic Dollo partitions
246 // for (AbstractPartitionData partition : options.dataPartitions) {
247 // PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel();
248 // if (model.isDolloModel()) {
249 // writeDolloTreeLikelihood(ALSTreeLikelihoodParser.LIKE_NAME, -1, partition, writer);
250 // }
251 // }
252 // }
253
254 private void writeDolloTreeLikelihood(String id, int num, AbstractPartitionData partition, XMLWriter writer) {
255 PartitionSubstitutionModel substModel = partition.getPartitionSubstitutionModel();
256 PartitionTreeModel treeModel = partition.getPartitionTreeModel();
257 PartitionClockModel clockModel = partition.getPartitionClockModel();
258
259 String prefix = partition.getName() + ".";
260 String oldPrefix = partition.getPrefix(); // TODO Get working
261
262 writer.writeComment("Likelihood for tree given a stochastic Dollo model");
263
264 writer.writeOpenTag(
265 ALSTreeLikelihoodParser.LIKE_NAME,
266 new Attribute[]{
267 new Attribute.Default<String>(XMLParser.ID, oldPrefix + id),
268 new Attribute.Default<Boolean>(TreeLikelihoodParser.USE_AMBIGUITIES, true),
269 new Attribute.Default<Boolean>(ALSTreeLikelihoodParser.INTEGRATE_GAIN_RATE, true)}
270 );
271
272 if (!options.samplePriorOnly) {
273 writer.writeIDref(SitePatternsParser.PATTERNS, partition.getPrefix() + SitePatternsParser.PATTERNS);
274 } else {
275 // We just need to use the dummy alignment
276 if (partition instanceof PartitionData) {
277 writer.writeIDref(AlignmentParser.ALIGNMENT, ((PartitionData) partition).getAlignment().getId());
278 }
279 }
280
281 writer.writeIDref(TreeModel.TREE_MODEL, treeModel.getPrefix() + TreeModel.TREE_MODEL);
282 writer.writeIDref(GammaSiteModel.SITE_MODEL, prefix + SiteModel.SITE_MODEL);
283
284 writer.writeTag(ALSTreeLikelihoodParser.OBSERVATION_PROCESS,
285 new Attribute.Default<String>(ALSTreeLikelihoodParser.OBSERVATION_TYPE,ALSTreeLikelihoodParser.ANY_TIP),
286 true);
287
288 switch (clockModel.getClockType()) {
289 case STRICT_CLOCK:
290 writer.writeIDref(StrictClockBranchRatesParser.STRICT_CLOCK_BRANCH_RATES, clockModel.getPrefix()
291 + BranchRateModel.BRANCH_RATES);
292 break;
293 case UNCORRELATED:
294 writer.writeIDref(DiscretizedBranchRatesParser.DISCRETIZED_BRANCH_RATES, clockModel.getPrefix()
295 + BranchRateModel.BRANCH_RATES);
296 break;
297 case RANDOM_LOCAL_CLOCK:
298 writer.writeIDref(RandomLocalClockModelParser.LOCAL_BRANCH_RATES, clockModel.getPrefix()
299 + BranchRateModel.BRANCH_RATES);
300 break;
301
302 case AUTOCORRELATED:
303 throw new UnsupportedOperationException("Autocorrelated relaxed clock model not implemented yet");
304 default:
305 throw new IllegalArgumentException("Unknown clock model");
306 }
307 writer.writeCloseTag(ALSTreeLikelihoodParser.LIKE_NAME);
308 }
309
310 }// END: class