| 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.evomodelxml.coalescent; |
| 27 |
|
| 28 |
import dr.evolution.tree.SimpleNode; |
| 29 |
import dr.evolution.tree.SimpleTree; |
| 30 |
import dr.evolution.tree.Tree; |
| 31 |
import dr.evolution.util.Taxa; |
| 32 |
import dr.evolution.util.Taxon; |
| 33 |
import dr.evolution.util.TaxonList; |
| 34 |
import dr.evomodel.coalescent.CoalescentSimulator; |
| 35 |
import dr.evomodel.coalescent.DemographicModel; |
| 36 |
import dr.evomodelxml.tree.TreeModelParser; |
| 37 |
import dr.inference.distribution.ParametricDistributionModel; |
| 38 |
import dr.xml.*; |
| 39 |
|
| 40 |
import java.util.ArrayList; |
| 41 |
import java.util.List; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public class CoalescentSimulatorParser extends AbstractXMLObjectParser { |
| 49 |
|
| 50 |
public static final String COALESCENT_SIMULATOR = "coalescentSimulator"; |
| 51 |
public static final String HEIGHT = "height"; |
| 52 |
|
| 53 |
public String getParserName() { |
| 54 |
return COALESCENT_SIMULATOR; |
| 55 |
} |
| 56 |
|
| 57 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 58 |
|
| 59 |
CoalescentSimulator simulator = new CoalescentSimulator(); |
| 60 |
|
| 61 |
DemographicModel demoModel = (DemographicModel) xo.getChild(DemographicModel.class); |
| 62 |
List<TaxonList> taxonLists = new ArrayList<TaxonList>(); |
| 63 |
List<Tree> subtrees = new ArrayList<Tree>(); |
| 64 |
|
| 65 |
double height = xo.getAttribute(HEIGHT, Double.NaN); |
| 66 |
|
| 67 |
|
| 68 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 69 |
final Object child = xo.getChild(i); |
| 70 |
|
| 71 |
|
| 72 |
if (child instanceof Tree) { |
| 73 |
subtrees.add((Tree) child); |
| 74 |
} else if (child instanceof TaxonList) { |
| 75 |
taxonLists.add((TaxonList) child); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
if (taxonLists.size() == 0) { |
| 80 |
if (subtrees.size() == 1) { |
| 81 |
return subtrees.get(0); |
| 82 |
} |
| 83 |
throw new XMLParseException("Expected at least one taxonList or two subtrees in " |
| 84 |
+ getParserName() + " element."); |
| 85 |
} |
| 86 |
|
| 87 |
Taxa remainingTaxa = new Taxa(); |
| 88 |
for (int i = 0; i < taxonLists.size(); i++) { |
| 89 |
remainingTaxa.addTaxa(taxonLists.get(i)); |
| 90 |
} |
| 91 |
|
| 92 |
for (int i = 0; i < subtrees.size(); i++) { |
| 93 |
remainingTaxa.removeTaxa(subtrees.get(i)); |
| 94 |
} |
| 95 |
|
| 96 |
try { |
| 97 |
Tree[] trees = new Tree[subtrees.size() + remainingTaxa.getTaxonCount()]; |
| 98 |
|
| 99 |
for (int i = 0; i < subtrees.size(); i++) { |
| 100 |
trees[i] = subtrees.get(i); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
for (int i = 0; i < remainingTaxa.getTaxonCount(); i++) { |
| 105 |
Taxa tip = new Taxa(); |
| 106 |
tip.addTaxon(remainingTaxa.getTaxon(i)); |
| 107 |
trees[i + subtrees.size()] = simulator.simulateTree(tip, demoModel); |
| 108 |
} |
| 109 |
|
| 110 |
return simulator.simulateTree(trees, demoModel, height, trees.length != 1); |
| 111 |
|
| 112 |
} catch (IllegalArgumentException iae) { |
| 113 |
throw new XMLParseException(iae.getMessage()); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public String getParserDescription() { |
| 122 |
return "This element returns a simulated tree under the given demographic model. The element can " + |
| 123 |
"be nested to simulate with monophyletic clades. The tree will be rescaled to the given height."; |
| 124 |
} |
| 125 |
|
| 126 |
public Class getReturnType() { |
| 127 |
return CoalescentSimulator.class; |
| 128 |
} |
| 129 |
|
| 130 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 131 |
return rules; |
| 132 |
} |
| 133 |
|
| 134 |
private final XMLSyntaxRule[] rules = { |
| 135 |
AttributeRule.newDoubleRule(HEIGHT, true, ""), |
| 136 |
new ElementRule(Tree.class, 0, Integer.MAX_VALUE), |
| 137 |
new ElementRule(TaxonList.class, 0, Integer.MAX_VALUE), |
| 138 |
new ElementRule(DemographicModel.class, 0, Integer.MAX_VALUE), |
| 139 |
}; |
| 140 |
} |