| 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.tree; |
| 27 |
|
| 28 |
import dr.evolution.tree.Tree; |
| 29 |
import dr.evolution.tree.TreeUtils; |
| 30 |
import dr.evolution.util.Taxa; |
| 31 |
import dr.evolution.util.TaxonList; |
| 32 |
import dr.evomodel.tree.TMRCAStatistic; |
| 33 |
import dr.inference.model.Statistic; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public class TMRCAStatisticParser extends AbstractXMLObjectParser { |
| 47 |
|
| 48 |
public static final String TMRCA_STATISTIC = "tmrcaStatistic"; |
| 49 |
public static final String MRCA = "mrca"; |
| 50 |
public static final String ABSOLUTE = "absolute"; |
| 51 |
|
| 52 |
|
| 53 |
public static final String PARENT = "forParent"; |
| 54 |
public static final String STEM = "includeStem"; |
| 55 |
|
| 56 |
|
| 57 |
public String getParserName() { |
| 58 |
return TMRCA_STATISTIC; |
| 59 |
} |
| 60 |
|
| 61 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 62 |
|
| 63 |
String name = xo.getAttribute(Statistic.NAME, xo.getId()); |
| 64 |
Tree tree = (Tree) xo.getChild(Tree.class); |
| 65 |
TaxonList taxa = null; |
| 66 |
|
| 67 |
if (xo.hasChildNamed(MRCA)) { |
| 68 |
taxa = (TaxonList) xo.getElementFirstChild(MRCA); |
| 69 |
} |
| 70 |
boolean isAbsolute = xo.getAttribute(ABSOLUTE, false); |
| 71 |
boolean includeStem = false; |
| 72 |
if (xo.hasAttribute(PARENT) && xo.hasAttribute(STEM)) { |
| 73 |
throw new XMLParseException("Please use either " + PARENT + " or " + STEM + "!"); |
| 74 |
} else if (xo.hasAttribute(PARENT)) { |
| 75 |
includeStem = xo.getBooleanAttribute(PARENT); |
| 76 |
} else if (xo.hasAttribute(STEM)) { |
| 77 |
includeStem = xo.getBooleanAttribute(STEM); |
| 78 |
} |
| 79 |
|
| 80 |
try { |
| 81 |
return new TMRCAStatistic(name, tree, taxa, isAbsolute, includeStem); |
| 82 |
} catch (TreeUtils.MissingTaxonException mte) { |
| 83 |
throw new XMLParseException( |
| 84 |
"Taxon, " + mte + ", in " + getParserName() + "was not found in the tree."); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public String getParserDescription() { |
| 93 |
return "A statistic that has as its value the height of the most recent common ancestor " + |
| 94 |
"of a set of taxa in a given tree. "; |
| 95 |
} |
| 96 |
|
| 97 |
public Class getReturnType() { |
| 98 |
return TMRCAStatistic.class; |
| 99 |
} |
| 100 |
|
| 101 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 102 |
return rules; |
| 103 |
} |
| 104 |
|
| 105 |
private final XMLSyntaxRule[] rules = { |
| 106 |
new ElementRule(Tree.class), |
| 107 |
new StringAttributeRule("name", |
| 108 |
"A name for this statistic primarily for the purposes of logging", true), |
| 109 |
AttributeRule.newBooleanRule(ABSOLUTE, true), |
| 110 |
new ElementRule(MRCA, |
| 111 |
new XMLSyntaxRule[]{new ElementRule(Taxa.class)}, true), |
| 112 |
new OrRule( |
| 113 |
new XMLSyntaxRule[]{ |
| 114 |
AttributeRule.newBooleanRule(PARENT, true), |
| 115 |
AttributeRule.newBooleanRule(STEM, true) |
| 116 |
} |
| 117 |
) |
| 118 |
}; |
| 119 |
|
| 120 |
} |