| 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.TreeTrait; |
| 30 |
import dr.evolution.tree.TreeTraitProvider; |
| 31 |
import dr.evolution.tree.TreeUtils; |
| 32 |
import dr.evolution.util.Taxa; |
| 33 |
import dr.evolution.util.TaxonList; |
| 34 |
import dr.evomodel.tree.AncestralTrait; |
| 35 |
import dr.evomodel.tree.TreeModel; |
| 36 |
import dr.xml.*; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class AncestralTraitParser extends AbstractXMLObjectParser { |
| 41 |
|
| 42 |
public static final String ANCESTRAL_TRAIT = "ancestralTrait"; |
| 43 |
public static final String ANCESTRAL_STATE = "ancestralState"; |
| 44 |
public static final String NAME = "name"; |
| 45 |
public static final String MRCA = "mrca"; |
| 46 |
public static final String TRAIT_NAME = "traitName"; |
| 47 |
public static final String STATES = "states"; |
| 48 |
|
| 49 |
public String getParserName() { |
| 50 |
return ANCESTRAL_TRAIT; |
| 51 |
} |
| 52 |
|
| 53 |
public String[] getParserNames() { |
| 54 |
|
| 55 |
return new String[]{ |
| 56 |
getParserName(), ANCESTRAL_STATE |
| 57 |
}; |
| 58 |
} |
| 59 |
|
| 60 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 61 |
|
| 62 |
String traitName = xo.getAttribute(TRAIT_NAME, STATES); |
| 63 |
String name = xo.getAttribute(NAME, traitName); |
| 64 |
Tree tree = (Tree) xo.getChild(Tree.class); |
| 65 |
TreeTraitProvider treeTraitProvider = (TreeTraitProvider) xo.getChild(TreeTraitProvider.class); |
| 66 |
|
| 67 |
TaxonList taxa = null; |
| 68 |
if (xo.hasChildNamed(MRCA)) { |
| 69 |
taxa = (TaxonList) xo.getElementFirstChild(MRCA); |
| 70 |
} |
| 71 |
|
| 72 |
TreeTrait trait = treeTraitProvider.getTreeTrait(traitName); |
| 73 |
if (trait == null) { |
| 74 |
throw new XMLParseException("A trait called, " + traitName + ", was not available from the TreeTraitProvider supplied to " + getParserName() + (xo.hasId() ? ", with ID " + xo.getId() : "")); |
| 75 |
} |
| 76 |
try { |
| 77 |
return new AncestralTrait(name, trait, tree, taxa); |
| 78 |
} catch (TreeUtils.MissingTaxonException mte) { |
| 79 |
throw new XMLParseException("Taxon, " + mte + ", in " + getParserName() + "was not found in the tree."); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public String getParserDescription() { |
| 88 |
return "A statistic that has as its value the height of the most recent common ancestor of a set of taxa in a given tree"; |
| 89 |
} |
| 90 |
|
| 91 |
public Class getReturnType() { |
| 92 |
return AncestralTrait.class; |
| 93 |
} |
| 94 |
|
| 95 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 96 |
return rules; |
| 97 |
} |
| 98 |
|
| 99 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 100 |
new StringAttributeRule(NAME, "A name for this statistic primarily for the purposes of logging", true), |
| 101 |
new StringAttributeRule(TRAIT_NAME, "The name of the trait to log", true), |
| 102 |
new ElementRule(TreeModel.class), |
| 103 |
new ElementRule(TreeTraitProvider.class), |
| 104 |
new ElementRule(MRCA, new XMLSyntaxRule[]{new ElementRule(Taxa.class)}, "The MRCA to reconstruct the trait at (default root node)", true) |
| 105 |
}; |
| 106 |
} |