-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathDolloComponentGenerator.java
More file actions
311 lines (266 loc) · 12.4 KB
/
Copy pathDolloComponentGenerator.java
File metadata and controls
311 lines (266 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/*
* DolloComponentGenerator.java
*
* Copyright © 2002-2024 the BEAST Development Team
* http://beast.community/about
*
* This file is part of BEAST.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership and licensing.
*
* BEAST is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* BEAST is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with BEAST; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301 USA
*
*/
package dr.app.beauti.components.dollo;
import dr.app.beauti.generator.BaseComponentGenerator;
import dr.app.beauti.options.*;
import dr.app.beauti.util.XMLWriter;
import dr.evolution.datatype.DataType;
import dr.evomodel.branchratemodel.BranchRateModel;
import dr.evomodel.tree.DefaultTreeModel;
import dr.evomodelxml.branchratemodel.DiscretizedBranchRatesParser;
import dr.evomodelxml.branchratemodel.RandomLocalClockModelParser;
import dr.evomodelxml.branchratemodel.StrictClockBranchRatesParser;
import dr.evomodelxml.substmodel.MutationDeathModelParser;
import dr.evomodelxml.treelikelihood.ALSTreeLikelihoodParser;
import dr.evoxml.AlignmentParser;
import dr.evoxml.MutationDeathTypeParser;
import dr.evoxml.SitePatternsParser;
import dr.inference.model.ParameterParser;
import dr.util.Attribute;
import dr.xml.XMLParser;
import static dr.evomodelxml.siteratemodel.SiteModelParser.*;
import static dr.evomodelxml.substmodel.MutationDeathModelParser.MD_MODEL;
import static dr.evomodelxml.treelikelihood.BeagleTreeLikelihoodParser.TREE_LIKELIHOOD;
import static dr.evomodelxml.treelikelihood.BeagleTreeLikelihoodParser.USE_AMBIGUITIES;
/**
* @author Marc Suchard
*/
public class DolloComponentGenerator extends BaseComponentGenerator {
protected DolloComponentGenerator(BeautiOptions options) {
super(options);
}
public boolean usesInsertionPoint(InsertionPoint point) {
DolloComponentOptions component = (DolloComponentOptions) options
.getComponentOptions(DolloComponentOptions.class);
if (!component.isActive()) {
return false;
}
switch (point) {
case AFTER_TAXA:
case AFTER_SUBSTITUTION_MODEL:
case IN_FILE_LOG_PARAMETERS:
// case IN_MCMC_LIKELIHOOD:
// case AFTER_TREE_LIKELIHOOD:
// case IN_OPERATORS:
// case IN_TREES_LOG:
// case AFTER_TREES_LOG:
// case AFTER_MCMC:
return true;
default:
return false;
}
}// END: usesInsertionPoint
protected void generate(final InsertionPoint point, final Object item, final String prefix, final XMLWriter writer) {
DolloComponentOptions component = (DolloComponentOptions) options
.getComponentOptions(DolloComponentOptions.class);
if (!includeStochasticDollo()) {
return;
}
switch (point) {
case AFTER_TAXA:
writeDataType(writer);
break;
case AFTER_SUBSTITUTION_MODEL:
writeDolloSubstitutionModels(writer, component);
break;
// case AFTER_TREE_LIKELIHOOD:
// writeDolloTreeLikelihoods(writer, component);
// break;
case IN_OPERATORS:
break;
// case IN_MCMC_LIKELIHOOD:
// writeDolloTreeLikelihoodReferences(writer);
// break;
case IN_SCREEN_LOG:
writeScreenLogEntries(writer, component);
break;
case IN_FILE_LOG_PARAMETERS:
writeLog(writer, component);
break;
case IN_TREES_LOG:
break;
case AFTER_TREES_LOG:
break;
case AFTER_MCMC:
break;
default:
throw new IllegalArgumentException(
"This insertion point is not implemented for "
+ this.getClass().getName());
}
}// END: generate
private void writeScreenLogEntries(XMLWriter writer, DolloComponentOptions component) {
}
// private void writeDolloTreeLikelihoodReferences(XMLWriter writer) {
//
// for (AbstractPartitionData partition : options.dataPartitions) {
// PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel();
// String prefix = partition.getName();
// if (model.isDolloModel()) {
// writer.writeIDref(ALSTreeLikelihoodParser.LIKE_NAME,
// prefix + ALSTreeLikelihoodParser.LIKE_NAME);
// }
// }
// }
private void writeLog(XMLWriter writer, DolloComponentOptions component) {
for (AbstractPartitionData partition : options.dataPartitions) {
PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel();
if (model.isDolloModel()) {
String prefix = partition.getName() + ".";
writer.writeIDref(ParameterParser.PARAMETER,
component.getOptions().getParameter(prefix + DolloComponentOptions.DEATH_RATE).getName());
}
}
}
private void writeDataType(XMLWriter writer) {
writer.writeOpenTag(MutationDeathTypeParser.MODEL_NAME,
new Attribute.Default<String>(XMLParser.ID, DolloComponentOptions.DATA_NAME));
writer.writeTag(MutationDeathTypeParser.EXTANT,
new Attribute.Default<String>(MutationDeathTypeParser.CODE, "1"), true);
writer.writeTag(MutationDeathTypeParser.STATE,
new Attribute.Default<String>(MutationDeathTypeParser.CODE, "0"), true);
writer.writeTag(MutationDeathTypeParser.AMBIGUITY,
new Attribute.Default<String>(MutationDeathTypeParser.CODE, "-"), true); // Some users use this specification
writer.writeTag(MutationDeathTypeParser.AMBIGUITY,
new Attribute.Default<String>(MutationDeathTypeParser.CODE, "?"), true);
writer.writeCloseTag(MutationDeathTypeParser.MODEL_NAME);
}
private boolean includeStochasticDollo() {
for (AbstractPartitionData partition : options.dataPartitions) {
PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel();
if (model.isDolloModel()) {
return true;
}
}
return false;
}
@Override
protected String getCommentLabel() {
return "Stochastic Dollo";
}
private void writeDolloSubstitutionModels(XMLWriter writer, DolloComponentOptions component) {
// generate tree likelihoods for stochastic Dollo partitions
for (AbstractPartitionData partition : options.dataPartitions) {
PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel();
if (model.isDolloModel()) {
writeDolloSubstitutionModel(partition, writer, component);
writeDolloSiteModel(partition, writer, component);
writeDolloTreeLikelihood(TREE_LIKELIHOOD, -1, partition, writer);
}
}
}
private void writeDolloSubstitutionModel(AbstractPartitionData partition, XMLWriter writer, DolloComponentOptions component) {
String prefix = partition.getName() + ".";
// String prefix = partition.getPrefix(); // TODO Fix
writer.writeOpenTag(MD_MODEL,
new Attribute.Default<String>(XMLParser.ID, prefix + DolloComponentOptions.MODEL_NAME ));
writeParameter(prefix + DolloComponentOptions.DEATH_RATE,
component.getOptions().getParameter(prefix + DolloComponentOptions.DEATH_RATE), writer);
writer.writeTag(DataType.DATA_TYPE, new Attribute.Default<String>(XMLParser.IDREF, DolloComponentOptions.DATA_NAME), true);
writer.writeCloseTag(MD_MODEL);
writer.write("\n");
}
private void writeDolloSiteModel(AbstractPartitionData partition, XMLWriter writer, DolloComponentOptions components) {
String prefix = partition.getName() + ".";
writer.writeOpenTag(SITE_MODEL,
new Attribute[]{new Attribute.Default<String>(XMLParser.ID, prefix + SITE_MODEL)});
writer.writeOpenTag(SUBSTITUTION_MODEL);
writer.writeIDref(MutationDeathModelParser.MD_MODEL, prefix + DolloComponentOptions.MODEL_NAME);
writer.writeCloseTag(SUBSTITUTION_MODEL);
PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel();
if (model.hasCodonPartitions()) {
writeParameter(RELATIVE_RATE, "mu", model, writer);
}
if (model.isGammaHetero()) {
writer.writeOpenTag(GAMMA_SHAPE,
new Attribute.Default<String>(GAMMA_CATEGORIES, "" + model.getGammaCategories()));
writeParameter(prefix + "alpha", model, writer);
writer.writeCloseTag(GAMMA_SHAPE);
}
if (model.isInvarHetero()) {
writeParameter(PROPORTION_INVARIANT, "pInv", model, writer);
}
writer.writeCloseTag(SITE_MODEL);
}
// private void writeDolloTreeLikelihoods(XMLWriter writer, DolloComponentOptions component) {
//
// // generate tree likelihoods for stochastic Dollo partitions
// for (AbstractPartitionData partition : options.dataPartitions) {
// PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel();
// if (model.isDolloModel()) {
// writeDolloTreeLikelihood(ALSTreeLikelihoodParser.LIKE_NAME, -1, partition, writer);
// }
// }
// }
private void writeDolloTreeLikelihood(String id, int num, AbstractPartitionData partition, XMLWriter writer) {
PartitionSubstitutionModel substModel = partition.getPartitionSubstitutionModel();
PartitionTreeModel treeModel = partition.getPartitionTreeModel();
PartitionClockModel clockModel = partition.getPartitionClockModel();
String prefix = partition.getName() + ".";
String oldPrefix = partition.getPrefix(); // TODO Get working
writer.writeComment("Likelihood for tree given a stochastic Dollo model");
writer.writeOpenTag(
ALSTreeLikelihoodParser.LIKE_NAME,
new Attribute[]{
new Attribute.Default<String>(XMLParser.ID, oldPrefix + id),
new Attribute.Default<Boolean>(USE_AMBIGUITIES, true),
new Attribute.Default<Boolean>(ALSTreeLikelihoodParser.INTEGRATE_GAIN_RATE, true)}
);
if (!options.samplePriorOnly) {
writer.writeIDref(SitePatternsParser.PATTERNS, partition.getPrefix() + SitePatternsParser.PATTERNS);
} else {
// We just need to use the dummy alignment
if (partition instanceof PartitionData) {
writer.writeIDref(AlignmentParser.ALIGNMENT, ((PartitionData) partition).getAlignment().getId());
}
}
writer.writeIDref(DefaultTreeModel.TREE_MODEL, treeModel.getPrefix() + DefaultTreeModel.TREE_MODEL);
writer.writeIDref(SITE_MODEL, prefix + SITE_MODEL);
writer.writeTag(ALSTreeLikelihoodParser.OBSERVATION_PROCESS,
new Attribute.Default<String>(ALSTreeLikelihoodParser.OBSERVATION_TYPE,ALSTreeLikelihoodParser.ANY_TIP),
true);
switch (clockModel.getClockType()) {
case STRICT_CLOCK:
writer.writeIDref(StrictClockBranchRatesParser.STRICT_CLOCK_BRANCH_RATES, clockModel.getPrefix()
+ BranchRateModel.BRANCH_RATES);
break;
case UNCORRELATED:
writer.writeIDref(DiscretizedBranchRatesParser.DISCRETIZED_BRANCH_RATES, clockModel.getPrefix()
+ BranchRateModel.BRANCH_RATES);
break;
case RANDOM_LOCAL_CLOCK:
writer.writeIDref(RandomLocalClockModelParser.LOCAL_BRANCH_RATES, clockModel.getPrefix()
+ BranchRateModel.BRANCH_RATES);
break;
case AUTOCORRELATED:
throw new UnsupportedOperationException("Autocorrelated relaxed clock model not implemented yet");
default:
throw new IllegalArgumentException("Unknown clock model");
}
writer.writeCloseTag(ALSTreeLikelihoodParser.LIKE_NAME);
}
}// END: class