| 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.app.beauti.components.ComponentFactory; |
| 29 |
import dr.app.beauti.options.*; |
| 30 |
import dr.app.beauti.types.PriorType; |
| 31 |
import dr.app.beauti.types.TreePriorType; |
| 32 |
import dr.app.beauti.util.XMLWriter; |
| 33 |
import dr.evolution.tree.NodeRef; |
| 34 |
import dr.evolution.tree.Tree; |
| 35 |
import dr.evolution.tree.TreeUtils; |
| 36 |
import dr.evolution.util.Taxa; |
| 37 |
import dr.evomodelxml.coalescent.CoalescentSimulatorParser; |
| 38 |
import dr.evomodelxml.coalescent.OldCoalescentSimulatorParser; |
| 39 |
import dr.evomodelxml.coalescent.ConstantPopulationModelParser; |
| 40 |
import dr.evomodelxml.coalescent.ExponentialGrowthModelParser; |
| 41 |
import dr.evoxml.*; |
| 42 |
import dr.util.Attribute; |
| 43 |
import dr.xml.XMLParser; |
| 44 |
|
| 45 |
import java.util.ArrayList; |
| 46 |
import java.util.List; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public class InitialTreeGenerator extends Generator { |
| 54 |
final static public String STARTING_TREE = "startingTree"; |
| 55 |
|
| 56 |
public InitialTreeGenerator(BeautiOptions options, ComponentFactory[] components) { |
| 57 |
super(options, components); |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public void writeStartingTree(PartitionTreeModel model, XMLWriter writer) { |
| 67 |
|
| 68 |
String prefix = model.getPrefix(); |
| 69 |
|
| 70 |
switch (model.getStartingTreeType()) { |
| 71 |
case USER: |
| 72 |
case UPGMA: |
| 73 |
Parameter rootHeight = model.getParameter("treeModel.rootHeight"); |
| 74 |
|
| 75 |
|
| 76 |
writer.writeComment("Construct a starting tree that is compatible with specified clade heights"); |
| 77 |
Attribute[] attributes = (rootHeight.priorType != PriorType.NONE_TREE_PRIOR ? |
| 78 |
new Attribute[] { |
| 79 |
new Attribute.Default<String>(XMLParser.ID, prefix + STARTING_TREE), |
| 80 |
new Attribute.Default<String>(RescaledTreeParser.HEIGHT, "" + rootHeight.getInitial()) |
| 81 |
} : |
| 82 |
new Attribute[] { |
| 83 |
new Attribute.Default<String>(XMLParser.ID, prefix + STARTING_TREE) |
| 84 |
}); |
| 85 |
writer.writeOpenTag(RescaledTreeParser.RESCALED_TREE, attributes); |
| 86 |
writeSourceTree(model, writer); |
| 87 |
if (options.taxonSets != null && options.taxonSets.size() > 0) { |
| 88 |
for (Taxa taxa : options.taxonSets) { |
| 89 |
Double height = options.taxonSetsHeights.get(taxa); |
| 90 |
if (height != null) { |
| 91 |
writer.writeOpenTag(RescaledTreeParser.CLADE, new Attribute.Default<String>(RescaledTreeParser.HEIGHT, height.toString())); |
| 92 |
writer.writeTag("taxa", new Attribute.Default<String>(XMLParser.IDREF, taxa.getId()), true); |
| 93 |
writer.writeCloseTag(RescaledTreeParser.CLADE); |
| 94 |
} else if (options.taxonSetsMono.get(taxa)) { |
| 95 |
|
| 96 |
|
| 97 |
writer.writeOpenTag(RescaledTreeParser.CLADE); |
| 98 |
writer.writeTag("taxa", new Attribute.Default<String>(XMLParser.IDREF, taxa.getId()), true); |
| 99 |
writer.writeCloseTag(RescaledTreeParser.CLADE); |
| 100 |
} |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
writer.writeCloseTag(RescaledTreeParser.RESCALED_TREE); |
| 105 |
break; |
| 106 |
|
| 107 |
case RANDOM: |
| 108 |
|
| 109 |
String simulatorId = prefix + STARTING_TREE; |
| 110 |
|
| 111 |
String taxaId = TaxaParser.TAXA; |
| 112 |
AbstractPartitionData partition = options.getDataPartitions(model).get(0); |
| 113 |
if (!options.hasIdenticalTaxa()) { |
| 114 |
taxaId = partition.getPartitionTreeModel().getPrefix() + TaxaParser.TAXA; |
| 115 |
} |
| 116 |
if (partition instanceof PartitionPattern && ((PartitionPattern) partition).getPatterns().hasMask()) { |
| 117 |
taxaId = partition.getPrefix() + TaxaParser.TAXA; |
| 118 |
} |
| 119 |
|
| 120 |
writer.writeComment("Generate a random starting tree under the coalescent process"); |
| 121 |
if (options.taxonSets != null && options.taxonSets.size() > 0) { |
| 122 |
writeSubTree(simulatorId, taxaId, options.taxonList, model, writer); |
| 123 |
} else { |
| 124 |
writer.writeOpenTag( |
| 125 |
CoalescentSimulatorParser.COALESCENT_SIMULATOR, |
| 126 |
new Attribute[]{ |
| 127 |
new Attribute.Default<String>(XMLParser.ID, simulatorId) |
| 128 |
} |
| 129 |
); |
| 130 |
|
| 131 |
writeTaxaRef(taxaId, model, writer); |
| 132 |
|
| 133 |
writeInitialDemoModelRef(model, writer); |
| 134 |
writer.writeCloseTag(CoalescentSimulatorParser.COALESCENT_SIMULATOR); |
| 135 |
} |
| 136 |
break; |
| 137 |
default: |
| 138 |
throw new IllegalArgumentException("Unknown StartingTreeType"); |
| 139 |
|
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
public void writeSourceTree(PartitionTreeModel model, XMLWriter writer) { |
| 144 |
|
| 145 |
switch (model.getStartingTreeType()) { |
| 146 |
case USER: |
| 147 |
if (model.isNewick()) { |
| 148 |
writeNewickTree(model.getUserStartingTree(), writer); |
| 149 |
} else { |
| 150 |
writeSimpleTree(model.getUserStartingTree(), writer); |
| 151 |
} |
| 152 |
break; |
| 153 |
|
| 154 |
case UPGMA: |
| 155 |
|
| 156 |
writer.writeComment("Construct a rough-and-ready UPGMA tree as an starting tree"); |
| 157 |
writer.writeOpenTag(UPGMATreeParser.UPGMA_TREE); |
| 158 |
writer.writeOpenTag( |
| 159 |
DistanceMatrixParser.DISTANCE_MATRIX, |
| 160 |
new Attribute[]{ |
| 161 |
new Attribute.Default<String>(DistanceMatrixParser.CORRECTION, "JC") |
| 162 |
} |
| 163 |
); |
| 164 |
writer.writeOpenTag(SitePatternsParser.PATTERNS); |
| 165 |
writer.writeComment("To generate UPGMA starting tree, only use the 1st aligment, " |
| 166 |
+ "which may be 1 of many aligments using this tree."); |
| 167 |
writer.writeIDref(AlignmentParser.ALIGNMENT, options.getDataPartitions(model).get(0).getTaxonList().getId()); |
| 168 |
|
| 169 |
writer.writeCloseTag(SitePatternsParser.PATTERNS); |
| 170 |
writer.writeCloseTag(DistanceMatrixParser.DISTANCE_MATRIX); |
| 171 |
writer.writeCloseTag(UPGMATreeParser.UPGMA_TREE); |
| 172 |
break; |
| 173 |
|
| 174 |
case RANDOM: |
| 175 |
throw new IllegalArgumentException("Shouldn't be here"); |
| 176 |
|
| 177 |
default: |
| 178 |
throw new IllegalArgumentException("Unknown StartingTreeType"); |
| 179 |
|
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
private void writeTaxaRef(String taxaId, PartitionTreeModel model, XMLWriter writer) { |
| 184 |
|
| 185 |
Attribute[] taxaAttribute = {new Attribute.Default<String>(XMLParser.IDREF, taxaId)}; |
| 186 |
|
| 187 |
if (options.taxonSets != null && options.taxonSets.size() > 0) { |
| 188 |
|
| 189 |
writer.writeOpenTag(OldCoalescentSimulatorParser.CONSTRAINED_TAXA); |
| 190 |
writer.writeTag(TaxaParser.TAXA, taxaAttribute, true); |
| 191 |
for (Taxa taxa : options.taxonSets) { |
| 192 |
if (options.taxonSetsTreeModel.get(taxa).equals(model)) { |
| 193 |
Parameter statistic = options.getStatistic(taxa); |
| 194 |
|
| 195 |
Attribute mono = new Attribute.Default<Boolean>( |
| 196 |
OldCoalescentSimulatorParser.IS_MONOPHYLETIC, options.taxonSetsMono.get(taxa)); |
| 197 |
|
| 198 |
writer.writeOpenTag(OldCoalescentSimulatorParser.TMRCA_CONSTRAINT, mono); |
| 199 |
|
| 200 |
writer.writeIDref(TaxaParser.TAXA, taxa.getId()); |
| 201 |
|
| 202 |
if (model.getPartitionTreePrior().getNodeHeightPrior() == TreePriorType.YULE_CALIBRATION |
| 203 |
&& statistic.priorType == PriorType.UNIFORM_PRIOR) { |
| 204 |
writeDistribution(statistic, false, writer); |
| 205 |
} |
| 206 |
|
| 207 |
writer.writeCloseTag(OldCoalescentSimulatorParser.TMRCA_CONSTRAINT); |
| 208 |
} |
| 209 |
} |
| 210 |
writer.writeCloseTag(OldCoalescentSimulatorParser.CONSTRAINED_TAXA); |
| 211 |
} else { |
| 212 |
writer.writeTag(TaxaParser.TAXA, taxaAttribute, true); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
private void writeSubTree(String treeId, String taxaId, Taxa taxa, PartitionTreeModel model, XMLWriter writer) { |
| 217 |
|
| 218 |
Double height = options.taxonSetsHeights.get(taxa); |
| 219 |
if (height == null) { |
| 220 |
height = Double.NaN; |
| 221 |
} |
| 222 |
|
| 223 |
Attribute[] attributes = new Attribute[] {}; |
| 224 |
if (treeId != null) { |
| 225 |
if (Double.isNaN(height)) { |
| 226 |
attributes = new Attribute[] { |
| 227 |
new Attribute.Default<String>(XMLParser.ID, treeId) |
| 228 |
}; |
| 229 |
} else { |
| 230 |
attributes = new Attribute[] { |
| 231 |
new Attribute.Default<String>(XMLParser.ID, treeId), |
| 232 |
new Attribute.Default<String>(CoalescentSimulatorParser.HEIGHT, "" + height) |
| 233 |
}; |
| 234 |
|
| 235 |
} |
| 236 |
} else { |
| 237 |
if (!Double.isNaN(height)) { |
| 238 |
attributes = new Attribute[] { |
| 239 |
new Attribute.Default<String>(CoalescentSimulatorParser.HEIGHT, "" + height) |
| 240 |
}; |
| 241 |
|
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
writer.writeOpenTag( |
| 248 |
CoalescentSimulatorParser.COALESCENT_SIMULATOR, |
| 249 |
attributes |
| 250 |
); |
| 251 |
|
| 252 |
List<Taxa> subsets = new ArrayList<Taxa>(); |
| 253 |
|
| 254 |
|
| 255 |
for (Taxa taxa2 : options.taxonSets) { |
| 256 |
boolean sameTree = model.equals(options.taxonSetsTreeModel.get(taxa2)); |
| 257 |
boolean isMono = options.taxonSetsMono.get(taxa2); |
| 258 |
boolean hasHeight = options.taxonSetsHeights.get(taxa2) != null; |
| 259 |
boolean isSubset = taxa.containsAll(taxa2); |
| 260 |
if (sameTree && (isMono || hasHeight) && taxa2 != taxa && isSubset) { |
| 261 |
subsets.add(taxa2); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
List<Taxa> toRemove = new ArrayList<Taxa>(); |
| 266 |
for (Taxa taxa3 : subsets) { |
| 267 |
boolean isSubSubSet = false; |
| 268 |
for (Taxa taxa4 : subsets) { |
| 269 |
if (!taxa4.equals(taxa3) && taxa4.containsAll(taxa3)) { |
| 270 |
isSubSubSet = true; |
| 271 |
} |
| 272 |
} |
| 273 |
if (isSubSubSet) { |
| 274 |
toRemove.add(taxa3); |
| 275 |
} |
| 276 |
} |
| 277 |
subsets.removeAll(toRemove); |
| 278 |
|
| 279 |
for (Taxa taxa5 : subsets) { |
| 280 |
|
| 281 |
writeSubTree(null, null, taxa5, model, writer); |
| 282 |
} |
| 283 |
|
| 284 |
if (taxaId == null) { |
| 285 |
writer.writeIDref(TaxaParser.TAXA, taxa.getId()); |
| 286 |
} else { |
| 287 |
writer.writeIDref(TaxaParser.TAXA, taxaId); |
| 288 |
} |
| 289 |
writeInitialDemoModelRef(model, writer); |
| 290 |
writer.writeCloseTag(CoalescentSimulatorParser.COALESCENT_SIMULATOR); |
| 291 |
} |
| 292 |
|
| 293 |
private void writeInitialDemoModelRef(PartitionTreeModel model, XMLWriter writer) { |
| 294 |
PartitionTreePrior prior = model.getPartitionTreePrior(); |
| 295 |
|
| 296 |
if (prior.getNodeHeightPrior() == TreePriorType.CONSTANT) { |
| 297 |
writer.writeIDref(ConstantPopulationModelParser.CONSTANT_POPULATION_MODEL, prior.getPrefix() + "constant"); |
| 298 |
} else if (prior.getNodeHeightPrior() == TreePriorType.EXPONENTIAL) { |
| 299 |
writer.writeIDref(ExponentialGrowthModelParser.EXPONENTIAL_GROWTH_MODEL, prior.getPrefix() + "exponential"); |
| 300 |
} else { |
| 301 |
writer.writeIDref(ConstantPopulationModelParser.CONSTANT_POPULATION_MODEL, prior.getPrefix() + "initialDemo"); |
| 302 |
} |
| 303 |
|
| 304 |
} |
| 305 |
|
| 306 |
private void writeNewickTree (Tree tree, XMLWriter writer) { |
| 307 |
|
| 308 |
writer.writeComment("The user-specified starting tree in a newick tree format."); |
| 309 |
writer.writeOpenTag( |
| 310 |
NewickParser.NEWICK, |
| 311 |
new Attribute[]{ |
| 312 |
|
| 313 |
|
| 314 |
new Attribute.Default<Boolean>(SimpleTreeParser.USING_DATES, options.useTipDates) |
| 315 |
} |
| 316 |
); |
| 317 |
writer.writeText(TreeUtils.newick(tree)); |
| 318 |
writer.writeCloseTag(NewickParser.NEWICK); |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
private void writeSimpleTree(Tree tree, XMLWriter writer) { |
| 343 |
|
| 344 |
writer.writeComment("The user-specified starting tree in a simple tree format."); |
| 345 |
writer.writeOpenTag( |
| 346 |
SimpleTreeParser.SIMPLE_TREE, |
| 347 |
new Attribute[]{ |
| 348 |
|
| 349 |
|
| 350 |
new Attribute.Default<Object>(DateParser.UNITS, options.units.toString()), |
| 351 |
new Attribute.Default<Boolean>(SimpleTreeParser.USING_DATES, options.useTipDates) |
| 352 |
} |
| 353 |
); |
| 354 |
writeSimpleNode(tree, tree.getRoot(), writer); |
| 355 |
writer.writeCloseTag(SimpleTreeParser.SIMPLE_TREE); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
private void writeSimpleNode(Tree tree, NodeRef node, XMLWriter writer) { |
| 366 |
|
| 367 |
writer.writeOpenTag( |
| 368 |
SimpleNodeParser.NODE, |
| 369 |
new Attribute[]{new Attribute.Default<Double>(SimpleNodeParser.HEIGHT, tree.getNodeHeight(node))} |
| 370 |
); |
| 371 |
|
| 372 |
if (tree.getChildCount(node) == 0) { |
| 373 |
writer.writeIDref(TaxonParser.TAXON, tree.getNodeTaxon(node).getId()); |
| 374 |
} |
| 375 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 376 |
writeSimpleNode(tree, tree.getChild(node, i), writer); |
| 377 |
} |
| 378 |
writer.writeCloseTag(SimpleNodeParser.NODE); |
| 379 |
} |
| 380 |
} |