| 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.generator; |
| 27 |
|
| 28 |
import dr.evomodel.treedatalikelihood.TreeDataLikelihood; |
| 29 |
import dr.evomodelxml.treedatalikelihood.TreeDataLikelihoodParser; |
| 30 |
import dr.evomodelxml.treelikelihood.MarkovJumpsTreeLikelihoodParser; |
| 31 |
import dr.app.beauti.components.ComponentFactory; |
| 32 |
import dr.app.beauti.components.ancestralstates.AncestralStatesComponentOptions; |
| 33 |
import dr.app.beauti.options.*; |
| 34 |
import dr.app.beauti.types.MicroSatModelType; |
| 35 |
import dr.app.beauti.util.XMLWriter; |
| 36 |
import dr.evolution.datatype.DataType; |
| 37 |
import dr.evomodel.branchratemodel.BranchRateModel; |
| 38 |
import dr.oldevomodel.sitemodel.GammaSiteModel; |
| 39 |
import dr.oldevomodel.sitemodel.SiteModel; |
| 40 |
import dr.oldevomodel.substmodel.AsymmetricQuadraticModel; |
| 41 |
import dr.oldevomodel.substmodel.LinearBiasModel; |
| 42 |
import dr.oldevomodel.substmodel.TwoPhaseModel; |
| 43 |
import dr.evomodel.tree.TreeModel; |
| 44 |
import dr.evomodelxml.branchratemodel.StrictClockBranchRatesParser; |
| 45 |
import dr.evomodelxml.tree.MicrosatelliteSamplerTreeModelParser; |
| 46 |
import dr.oldevomodelxml.treelikelihood.AncestralStateTreeLikelihoodParser; |
| 47 |
import dr.oldevomodelxml.treelikelihood.MicrosatelliteSamplerTreeLikelihoodParser; |
| 48 |
import dr.oldevomodelxml.treelikelihood.TreeLikelihoodParser; |
| 49 |
import dr.evoxml.AlignmentParser; |
| 50 |
import dr.evoxml.SitePatternsParser; |
| 51 |
import dr.util.Attribute; |
| 52 |
import dr.xml.XMLParser; |
| 53 |
|
| 54 |
import java.util.List; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public class TreeLikelihoodGenerator extends Generator { |
| 62 |
|
| 63 |
private static final boolean GENERATE_TREE_LIKELIHOOD = false; |
| 64 |
|
| 65 |
public TreeLikelihoodGenerator(BeautiOptions options, ComponentFactory[] components) { |
| 66 |
super(options, components); |
| 67 |
} |
| 68 |
|
| 69 |
public void writeAllTreeLikelihoods(XMLWriter writer) throws GeneratorException { |
| 70 |
for (List<PartitionData> partitions : options.multiPartitionLists) { |
| 71 |
writeTreeDataLikelihood(partitions, writer); |
| 72 |
writer.writeText(""); |
| 73 |
} |
| 74 |
|
| 75 |
for (AbstractPartitionData partition : options.otherPartitions) { |
| 76 |
|
| 77 |
if (partition.getTaxonList() != null) { |
| 78 |
if (partition instanceof PartitionData) { |
| 79 |
writeTreeLikelihood((PartitionData) partition, writer); |
| 80 |
writer.writeText(""); |
| 81 |
} else if (partition instanceof PartitionPattern) { |
| 82 |
writeTreeLikelihood((PartitionPattern) partition, writer); |
| 83 |
writer.writeText(""); |
| 84 |
} else { |
| 85 |
throw new GeneratorException("Find unrecognized partition:\n" + partition.getName()); |
| 86 |
} |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
public void writeTreeDataLikelihood(List<PartitionData> partitions, XMLWriter writer) { |
| 92 |
|
| 93 |
PartitionSubstitutionModel substModel = partitions.get(0).getPartitionSubstitutionModel(); |
| 94 |
PartitionTreeModel treeModel = partitions.get(0).getPartitionTreeModel(); |
| 95 |
PartitionClockModel clockModel = partitions.get(0).getPartitionClockModel(); |
| 96 |
|
| 97 |
String prefix = treeModel.getPrefix() + clockModel.getPrefix(); |
| 98 |
String idString = prefix + "treeLikelihood"; |
| 99 |
|
| 100 |
Attribute[] attributes = new Attribute[]{ |
| 101 |
new Attribute.Default<String>(XMLParser.ID, idString), |
| 102 |
new Attribute.Default<Boolean>(TreeDataLikelihoodParser.USE_AMBIGUITIES, substModel.isUseAmbiguitiesTreeLikelihood()) |
| 103 |
}; |
| 104 |
|
| 105 |
writer.writeComment("Likelihood for tree given sequence data"); |
| 106 |
writer.writeOpenTag(TreeDataLikelihoodParser.TREE_DATA_LIKELIHOOD, attributes); |
| 107 |
|
| 108 |
for (PartitionData partition : partitions) { |
| 109 |
substModel = partition.getPartitionSubstitutionModel(); |
| 110 |
PartitionClockModel cm = partition.getPartitionClockModel(); |
| 111 |
if (clockModel == null) { |
| 112 |
clockModel = cm; |
| 113 |
} else if (clockModel != cm) { |
| 114 |
throw new RuntimeException("All the partitions in a TreeDataLikelihood should share the same clock model."); |
| 115 |
} |
| 116 |
|
| 117 |
if (substModel.getCodonHeteroPattern() != null) { |
| 118 |
|
| 119 |
for (int num = 1; num <= substModel.getCodonPartitionCount(); num++) { |
| 120 |
String prefix1 = partition.getPrefix() + substModel.getPrefixCodon(num); |
| 121 |
|
| 122 |
writer.writeOpenTag(TreeDataLikelihoodParser.PARTITION); |
| 123 |
writeCodonPatternsRef(prefix1, num, substModel.getCodonPartitionCount(), writer); |
| 124 |
|
| 125 |
writer.writeIDref(GammaSiteModel.SITE_MODEL, substModel.getPrefix(num) + SiteModel.SITE_MODEL); |
| 126 |
|
| 127 |
writer.writeCloseTag(TreeDataLikelihoodParser.PARTITION); |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
} else { |
| 132 |
String prefix1 = partition.getPrefix(); |
| 133 |
writer.writeOpenTag(TreeDataLikelihoodParser.PARTITION); |
| 134 |
writer.writeIDref(SitePatternsParser.PATTERNS, prefix1 + SitePatternsParser.PATTERNS); |
| 135 |
writer.writeIDref(GammaSiteModel.SITE_MODEL, substModel.getPrefix() + SiteModel.SITE_MODEL); |
| 136 |
writer.writeCloseTag(TreeDataLikelihoodParser.PARTITION); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
writer.writeIDref(TreeModel.TREE_MODEL, treeModel.getPrefix() + TreeModel.TREE_MODEL); |
| 143 |
ClockModelGenerator.writeBranchRatesModelRef(clockModel, writer); |
| 144 |
|
| 145 |
|
| 146 |
writer.writeCloseTag(TreeDataLikelihoodParser.TREE_DATA_LIKELIHOOD); |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public void writeTreeLikelihood(PartitionData partition, XMLWriter writer) { |
| 157 |
|
| 158 |
AncestralStatesComponentOptions ancestralStatesOptions = (AncestralStatesComponentOptions) options |
| 159 |
.getComponentOptions(AncestralStatesComponentOptions.class); |
| 160 |
|
| 161 |
PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel(); |
| 162 |
|
| 163 |
if (model.isDolloModel()) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
String treeLikelihoodTag = TreeLikelihoodParser.TREE_LIKELIHOOD; |
| 168 |
if (ancestralStatesOptions.usingAncestralStates(partition)) { |
| 169 |
treeLikelihoodTag = TreeLikelihoodParser.ANCESTRAL_TREE_LIKELIHOOD; |
| 170 |
if (ancestralStatesOptions.isCountingStates(partition)) { |
| 171 |
|
| 172 |
|
| 173 |
if (!ancestralStatesOptions.dNdSRobustCounting(partition)) { |
| 174 |
treeLikelihoodTag = MarkovJumpsTreeLikelihoodParser.MARKOV_JUMP_TREE_LIKELIHOOD; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
if (model.getDataType().getType() == DataType.NUCLEOTIDES && model.getCodonHeteroPattern() != null) { |
| 180 |
|
| 181 |
for (int i = 1; i <= model.getCodonPartitionCount(); i++) { |
| 182 |
writeTreeLikelihood(treeLikelihoodTag, TreeLikelihoodParser.TREE_LIKELIHOOD, i, partition, writer); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
} else { |
| 187 |
writeTreeLikelihood(treeLikelihoodTag, TreeLikelihoodParser.TREE_LIKELIHOOD, -1, partition, writer); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
private void writeTreeLikelihood(String tag, String id, int num, PartitionData partition, XMLWriter writer) { |
| 200 |
|
| 201 |
PartitionSubstitutionModel substModel = partition.getPartitionSubstitutionModel(); |
| 202 |
PartitionTreeModel treeModel = partition.getPartitionTreeModel(); |
| 203 |
PartitionClockModel clockModel = partition.getPartitionClockModel(); |
| 204 |
|
| 205 |
writer.writeComment("Likelihood for tree given sequence data"); |
| 206 |
|
| 207 |
String prefix; |
| 208 |
if (num > 0) { |
| 209 |
prefix = partition.getPrefix() + substModel.getPrefixCodon(num); |
| 210 |
} else { |
| 211 |
prefix = partition.getPrefix(); |
| 212 |
} |
| 213 |
|
| 214 |
String idString = prefix + id; |
| 215 |
|
| 216 |
Attribute[] attributes; |
| 217 |
if (tag.equals(MarkovJumpsTreeLikelihoodParser.MARKOV_JUMP_TREE_LIKELIHOOD)) { |
| 218 |
AncestralStatesComponentOptions ancestralStatesOptions = (AncestralStatesComponentOptions) options |
| 219 |
.getComponentOptions(AncestralStatesComponentOptions.class); |
| 220 |
boolean saveCompleteHistory = ancestralStatesOptions.isCompleteHistoryLogging(partition); |
| 221 |
attributes = new Attribute[]{ |
| 222 |
new Attribute.Default<String>(XMLParser.ID, idString), |
| 223 |
new Attribute.Default<Boolean>(TreeLikelihoodParser.USE_AMBIGUITIES, substModel.isUseAmbiguitiesTreeLikelihood()), |
| 224 |
new Attribute.Default<Boolean>(MarkovJumpsTreeLikelihoodParser.USE_UNIFORMIZATION, true), |
| 225 |
new Attribute.Default<Integer>(MarkovJumpsTreeLikelihoodParser.NUMBER_OF_SIMULANTS, 1), |
| 226 |
new Attribute.Default<String>(AncestralStateTreeLikelihoodParser.RECONSTRUCTION_TAG_NAME, prefix + AncestralStateTreeLikelihoodParser.RECONSTRUCTION_TAG), |
| 227 |
new Attribute.Default<String>(MarkovJumpsTreeLikelihoodParser.SAVE_HISTORY, saveCompleteHistory ? "true" : "false"), |
| 228 |
new Attribute.Default<String>(MarkovJumpsTreeLikelihoodParser.LOG_HISTORY, saveCompleteHistory ? "true" : "false"), |
| 229 |
}; |
| 230 |
} else if (tag.equals(TreeLikelihoodParser.ANCESTRAL_TREE_LIKELIHOOD)) { |
| 231 |
attributes = new Attribute[]{ |
| 232 |
new Attribute.Default<String>(XMLParser.ID, idString), |
| 233 |
new Attribute.Default<Boolean>(TreeLikelihoodParser.USE_AMBIGUITIES, substModel.isUseAmbiguitiesTreeLikelihood()), |
| 234 |
new Attribute.Default<String>(AncestralStateTreeLikelihoodParser.RECONSTRUCTION_TAG_NAME, prefix + AncestralStateTreeLikelihoodParser.RECONSTRUCTION_TAG), |
| 235 |
}; |
| 236 |
} else { |
| 237 |
attributes = new Attribute[]{ |
| 238 |
new Attribute.Default<String>(XMLParser.ID, idString), |
| 239 |
new Attribute.Default<Boolean>(TreeLikelihoodParser.USE_AMBIGUITIES, substModel.isUseAmbiguitiesTreeLikelihood()) |
| 240 |
}; |
| 241 |
} |
| 242 |
|
| 243 |
writer.writeOpenTag(tag, attributes); |
| 244 |
|
| 245 |
if (!options.samplePriorOnly) { |
| 246 |
if (num > 0) { |
| 247 |
writeCodonPatternsRef(prefix, num, substModel.getCodonPartitionCount(), writer); |
| 248 |
} else { |
| 249 |
writer.writeIDref(SitePatternsParser.PATTERNS, prefix + SitePatternsParser.PATTERNS); |
| 250 |
} |
| 251 |
} else { |
| 252 |
|
| 253 |
writer.writeIDref(AlignmentParser.ALIGNMENT, partition.getAlignment().getId()); |
| 254 |
} |
| 255 |
|
| 256 |
writer.writeIDref(TreeModel.TREE_MODEL, treeModel.getPrefix() + TreeModel.TREE_MODEL); |
| 257 |
|
| 258 |
if (num > 0) { |
| 259 |
writer.writeIDref(GammaSiteModel.SITE_MODEL, substModel.getPrefix(num) + SiteModel.SITE_MODEL); |
| 260 |
} else { |
| 261 |
writer.writeIDref(GammaSiteModel.SITE_MODEL, substModel.getPrefix() + SiteModel.SITE_MODEL); |
| 262 |
} |
| 263 |
|
| 264 |
ClockModelGenerator.writeBranchRatesModelRef(clockModel, writer); |
| 265 |
|
| 266 |
generateInsertionPoint(ComponentGenerator.InsertionPoint.IN_TREE_LIKELIHOOD, partition, prefix, writer); |
| 267 |
|
| 268 |
writer.writeCloseTag(tag); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public void writeTreeLikelihoodReferences(XMLWriter writer) { |
| 276 |
for (List<PartitionData> partitions : options.multiPartitionLists) { |
| 277 |
|
| 278 |
PartitionTreeModel treeModel = partitions.get(0).getPartitionTreeModel(); |
| 279 |
PartitionClockModel clockModel = partitions.get(0).getPartitionClockModel(); |
| 280 |
|
| 281 |
String prefix = treeModel.getPrefix() + clockModel.getPrefix(); |
| 282 |
String idString = prefix + "treeLikelihood"; |
| 283 |
|
| 284 |
writer.writeIDref(TreeDataLikelihoodParser.TREE_DATA_LIKELIHOOD, idString); |
| 285 |
} |
| 286 |
|
| 287 |
for (AbstractPartitionData partition : options.otherPartitions) { |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
AncestralStatesComponentOptions ancestralStatesOptions = (AncestralStatesComponentOptions) options |
| 292 |
.getComponentOptions(AncestralStatesComponentOptions.class); |
| 293 |
|
| 294 |
PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel(); |
| 295 |
|
| 296 |
if (model.isDolloModel()) { |
| 297 |
return; |
| 298 |
} |
| 299 |
|
| 300 |
String treeLikelihoodTag = TreeLikelihoodParser.TREE_LIKELIHOOD; |
| 301 |
if (ancestralStatesOptions.usingAncestralStates(partition)) { |
| 302 |
treeLikelihoodTag = TreeLikelihoodParser.ANCESTRAL_TREE_LIKELIHOOD; |
| 303 |
if (ancestralStatesOptions.isCountingStates(partition)) { |
| 304 |
|
| 305 |
|
| 306 |
if (!ancestralStatesOptions.dNdSRobustCounting(partition)) { |
| 307 |
treeLikelihoodTag = MarkovJumpsTreeLikelihoodParser.MARKOV_JUMP_TREE_LIKELIHOOD; |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
if (model.getDataType().getType() == DataType.NUCLEOTIDES && model.getCodonHeteroPattern() != null) { |
| 313 |
|
| 314 |
for (int i = 1; i <= model.getCodonPartitionCount(); i++) { |
| 315 |
writeTreeLikelihoodRef(treeLikelihoodTag, TreeLikelihoodParser.TREE_LIKELIHOOD, i, partition, writer); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
} else { |
| 320 |
writeTreeLikelihoodRef(treeLikelihoodTag, TreeLikelihoodParser.TREE_LIKELIHOOD, -1, partition, writer); |
| 321 |
} |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
private void writeTreeLikelihoodRef(String tag, String id, int num, AbstractPartitionData partition, XMLWriter writer) { |
| 326 |
|
| 327 |
PartitionSubstitutionModel substModel = partition.getPartitionSubstitutionModel(); |
| 328 |
|
| 329 |
String prefix; |
| 330 |
if (num > 0) { |
| 331 |
prefix = partition.getPrefix() + substModel.getPrefixCodon(num); |
| 332 |
} else { |
| 333 |
prefix = partition.getPrefix(); |
| 334 |
} |
| 335 |
|
| 336 |
String idString = prefix + id; |
| 337 |
|
| 338 |
writer.writeIDref(tag, idString); |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
public void writeTreeLikelihood(PartitionPattern partition, XMLWriter writer) { |
| 348 |
PartitionSubstitutionModel substModel = partition.getPartitionSubstitutionModel(); |
| 349 |
|
| 350 |
PartitionClockModel clockModel = partition.getPartitionClockModel(); |
| 351 |
|
| 352 |
writer.writeComment("Microsatellite Sampler Tree Likelihood"); |
| 353 |
|
| 354 |
writer.writeOpenTag(MicrosatelliteSamplerTreeLikelihoodParser.TREE_LIKELIHOOD, |
| 355 |
new Attribute[]{new Attribute.Default<String>(XMLParser.ID, |
| 356 |
partition.getPrefix() + MicrosatelliteSamplerTreeLikelihoodParser.TREE_LIKELIHOOD)}); |
| 357 |
|
| 358 |
writeMicrosatSubstModelRef(substModel, writer); |
| 359 |
|
| 360 |
writer.writeIDref(MicrosatelliteSamplerTreeModelParser.TREE_MICROSATELLITE_SAMPLER_MODEL, |
| 361 |
partition.getName() + "." + MicrosatelliteSamplerTreeModelParser.TREE_MICROSATELLITE_SAMPLER_MODEL); |
| 362 |
|
| 363 |
switch (clockModel.getClockType()) { |
| 364 |
case STRICT_CLOCK: |
| 365 |
writer.writeIDref(StrictClockBranchRatesParser.STRICT_CLOCK_BRANCH_RATES, clockModel.getPrefix() |
| 366 |
+ BranchRateModel.BRANCH_RATES); |
| 367 |
break; |
| 368 |
case UNCORRELATED: |
| 369 |
case RANDOM_LOCAL_CLOCK: |
| 370 |
case AUTOCORRELATED: |
| 371 |
throw new UnsupportedOperationException("Microsatellite only supports strict clock model"); |
| 372 |
|
| 373 |
default: |
| 374 |
throw new IllegalArgumentException("Unknown clock model"); |
| 375 |
} |
| 376 |
|
| 377 |
writer.writeCloseTag(MicrosatelliteSamplerTreeLikelihoodParser.TREE_LIKELIHOOD); |
| 378 |
} |
| 379 |
|
| 380 |
public void writeMicrosatSubstModelRef(PartitionSubstitutionModel model, XMLWriter writer) { |
| 381 |
if (model.getPhase() != MicroSatModelType.Phase.ONE_PHASE) { |
| 382 |
writer.writeIDref(TwoPhaseModel.TWO_PHASE_MODEL, model.getPrefix() + TwoPhaseModel.TWO_PHASE_MODEL); |
| 383 |
} else if (model.getMutationBias() != MicroSatModelType.MutationalBias.UNBIASED) { |
| 384 |
writer.writeIDref(LinearBiasModel.LINEAR_BIAS_MODEL, model.getPrefix() + LinearBiasModel.LINEAR_BIAS_MODEL); |
| 385 |
} else { |
| 386 |
writer.writeIDref(AsymmetricQuadraticModel.ASYMQUAD_MODEL, model.getPrefix() + AsymmetricQuadraticModel.ASYMQUAD_MODEL); |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
public boolean canUseMultiPartition(AbstractPartitionData partition) { |
| 396 |
|
| 397 |
AncestralStatesComponentOptions ancestralStatesOptions = (AncestralStatesComponentOptions) options |
| 398 |
.getComponentOptions(AncestralStatesComponentOptions.class); |
| 399 |
|
| 400 |
PartitionSubstitutionModel model = partition.getPartitionSubstitutionModel(); |
| 401 |
|
| 402 |
if (model.isDolloModel()) { |
| 403 |
return false; |
| 404 |
} |
| 405 |
|
| 406 |
if (ancestralStatesOptions.usingAncestralStates(partition)) { |
| 407 |
return false; |
| 408 |
} |
| 409 |
|
| 410 |
if (model.getDataType().getType() != DataType.NUCLEOTIDES && model.getDataType().getType() != DataType.AMINO_ACIDS) { |
| 411 |
return false; |
| 412 |
} |
| 413 |
|
| 414 |
return true; |
| 415 |
} |
| 416 |
|
| 417 |
} |