| 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.treemetrics.*; |
| 30 |
import dr.evomodel.tree.TreeMetricStatistic; |
| 31 |
import dr.inference.model.Statistic; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class TreeMetricStatisticParser extends AbstractXMLObjectParser { |
| 37 |
|
| 38 |
public static final String TREE_METRIC_STATISTIC = "treeMetricStatistic"; |
| 39 |
public static final String TARGET = "target"; |
| 40 |
public static final String REFERENCE = "reference"; |
| 41 |
public static final String TYPE = "type"; |
| 42 |
|
| 43 |
public String getParserName() { |
| 44 |
return TREE_METRIC_STATISTIC; |
| 45 |
} |
| 46 |
|
| 47 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 48 |
|
| 49 |
TreeMetric.Type type = null; |
| 50 |
if (xo.hasAttribute(TYPE)) { |
| 51 |
final String s = xo.getStringAttribute(TYPE); |
| 52 |
for (TreeMetric.Type t : TreeMetric.Type.values()) { |
| 53 |
if (t.toString().toLowerCase().equals(s.toLowerCase())) { |
| 54 |
type = t; |
| 55 |
break; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if (type == null) { |
| 60 |
throw new XMLParseException("Tree metric type, " + s + ", is not recognised"); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
final String name = xo.getAttribute(Statistic.NAME, xo.hasId() ? xo.getId() : |
| 65 |
(type == null ? "topology" : type.toString())); |
| 66 |
final Tree target = (Tree) xo.getElementFirstChild(TARGET); |
| 67 |
final Tree reference = (Tree) xo.getElementFirstChild(REFERENCE); |
| 68 |
|
| 69 |
TreeMetric treeMetric = null; |
| 70 |
if (type != null) { |
| 71 |
switch (type) { |
| 72 |
case ROBINSON_FOULDS: |
| 73 |
treeMetric = new RobinsonFouldsMetric(); |
| 74 |
break; |
| 75 |
case BRANCH_SCORE: |
| 76 |
treeMetric = new BranchScoreMetric(); |
| 77 |
break; |
| 78 |
case ROOTED_BRANCH_SCORE: |
| 79 |
treeMetric = new RootedBranchScoreMetric(); |
| 80 |
break; |
| 81 |
case CLADE_HEIGHT: |
| 82 |
treeMetric = new CladeHeightMetric(); |
| 83 |
break; |
| 84 |
case KENDALL_COLIJN: |
| 85 |
treeMetric = new KendallColijnPathDifferenceMetric(0.5); |
| 86 |
break; |
| 87 |
case STEEL_PENNY: |
| 88 |
treeMetric = new SteelPennyPathDifferenceMetric(); |
| 89 |
break; |
| 90 |
default: |
| 91 |
throw new IllegalArgumentException("Unknown tree metric type"); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
return new TreeMetricStatistic(name, reference, target, treeMetric); |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public String getParserDescription() { |
| 104 |
return "A statistic that returns the distance between two trees. " |
| 105 |
+ " with method=\"topology\", return a 0 for identity and a 1 for difference. " |
| 106 |
+ "With other methods return the distance metric associated with that method."; |
| 107 |
} |
| 108 |
|
| 109 |
public Class getReturnType() { |
| 110 |
return TreeMetricStatistic.class; |
| 111 |
} |
| 112 |
|
| 113 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 114 |
return rules; |
| 115 |
} |
| 116 |
|
| 117 |
private final XMLSyntaxRule[] rules = { |
| 118 |
new StringAttributeRule(Statistic.NAME, |
| 119 |
"A name for this statistic primarily for the purposes of logging", |
| 120 |
true), |
| 121 |
new StringAttributeRule(TYPE, "tree metric name", true), |
| 122 |
new ElementRule(TARGET, new XMLSyntaxRule[]{new ElementRule( |
| 123 |
Tree.class)}), |
| 124 |
new ElementRule(REFERENCE, new XMLSyntaxRule[]{new ElementRule( |
| 125 |
Tree.class)}), |
| 126 |
}; |
| 127 |
} |