| 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 |
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 |
|
| 53 |
|
| 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 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
return true; |
| 82 |
default: |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 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 |
|
| 105 |
|
| 106 |
|
| 107 |
case IN_OPERATORS: |
| 108 |
break; |
| 109 |
|
| 110 |
|
| 111 |
|
| 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 |
} |
| 132 |
|
| 133 |
private void writeScreenLogEntries(XMLWriter writer, DolloComponentOptions component) { |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 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); |
| 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 |
|
| 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 |
|
| 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 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 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(); |
| 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 |
|
| 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 |
} |