| 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.evoxml; |
| 27 |
|
| 28 |
import dr.evolution.tree.*; |
| 29 |
import dr.evolution.util.Taxon; |
| 30 |
import dr.evolution.util.TaxonList; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
import java.util.HashSet; |
| 34 |
import java.util.Set; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class RescaledTreeParser extends AbstractXMLObjectParser { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public final static String RESCALED_TREE = "rescaledTree"; |
| 48 |
public final static String CLADE = "clade"; |
| 49 |
public final static String HEIGHT = "height"; |
| 50 |
|
| 51 |
public String getParserName() { return RESCALED_TREE; } |
| 52 |
|
| 53 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 54 |
|
| 55 |
Tree tree = (Tree)xo.getChild(Tree.class); |
| 56 |
|
| 57 |
|
| 58 |
SimpleTree rescaledTree = new SimpleTree(tree); |
| 59 |
|
| 60 |
|
| 61 |
for (int i = 0; i < rescaledTree.getInternalNodeCount(); i++) { |
| 62 |
rescaledTree.setNodeHeight(rescaledTree.getInternalNode(i), Double.NEGATIVE_INFINITY); |
| 63 |
} |
| 64 |
|
| 65 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 66 |
if (xo.getChild(i) instanceof XMLObject) { |
| 67 |
XMLObject cxo = (XMLObject)xo.getChild(i); |
| 68 |
if (cxo.getName().equals(CLADE)) { |
| 69 |
TaxonList taxa = (TaxonList)cxo.getChild(TaxonList.class); |
| 70 |
|
| 71 |
Set<String> leafSet = new HashSet<String>(); |
| 72 |
for (Taxon taxon : taxa) { |
| 73 |
leafSet.add(taxon.getId()); |
| 74 |
} |
| 75 |
|
| 76 |
NodeRef mrca = TreeUtils.getCommonAncestorNode(rescaledTree, leafSet); |
| 77 |
if (mrca == null || TreeUtils.getLeafCount(rescaledTree, mrca) != leafSet.size()) { |
| 78 |
throw new XMLParseException("Clade defined by taxon Set, " + taxa.getId() + ", is not found in the guide tree"); |
| 79 |
} |
| 80 |
|
| 81 |
if (cxo.hasAttribute(HEIGHT)) { |
| 82 |
double height = cxo.getDoubleAttribute(HEIGHT); |
| 83 |
rescaledTree.setNodeHeight(mrca, height); |
| 84 |
} |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
if (xo.hasAttribute(HEIGHT)) { |
| 90 |
rescaledTree.setNodeHeight(rescaledTree.getRoot(), xo.getDoubleAttribute(HEIGHT)); |
| 91 |
} |
| 92 |
|
| 93 |
interpolateHeights(rescaledTree, rescaledTree.getRoot()); |
| 94 |
|
| 95 |
return rescaledTree; |
| 96 |
} |
| 97 |
|
| 98 |
private double interpolateHeights(MutableTree tree, NodeRef node) { |
| 99 |
if (!tree.isExternal(node)) { |
| 100 |
|
| 101 |
double maxHeight = Double.NEGATIVE_INFINITY; |
| 102 |
|
| 103 |
for (int i = 0; i < tree.getChildCount(node); i++) { |
| 104 |
NodeRef child = tree.getChild(node, i); |
| 105 |
|
| 106 |
double h = interpolateHeights(tree, child); |
| 107 |
|
| 108 |
if (h > maxHeight) { |
| 109 |
maxHeight = h; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
double height = tree.getNodeHeight(node); |
| 114 |
if (Double.isInfinite(height)) { |
| 115 |
int count = 1; |
| 116 |
NodeRef parent = tree.getParent(node); |
| 117 |
while (parent != null && Double.isInfinite(tree.getNodeHeight(parent))) { |
| 118 |
parent = tree.getParent(parent); |
| 119 |
count ++; |
| 120 |
} |
| 121 |
|
| 122 |
if (parent == null) { |
| 123 |
height = maxHeight + count; |
| 124 |
} else { |
| 125 |
height = (tree.getNodeHeight(parent) + maxHeight) / 2.0; |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
tree.setNodeHeight(node, height); |
| 130 |
|
| 131 |
return height; |
| 132 |
} else { |
| 133 |
return tree.getNodeHeight(node); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
public String getParserDescription() { |
| 138 |
return "This element rescales a given tree with a set of clade heights."; |
| 139 |
} |
| 140 |
|
| 141 |
public Class getReturnType() { return Tree.class; } |
| 142 |
|
| 143 |
public XMLSyntaxRule[] getSyntaxRules() { return rules; } |
| 144 |
|
| 145 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[] { |
| 146 |
AttributeRule.newDoubleRule(HEIGHT, true), |
| 147 |
new ElementRule(Tree.class), |
| 148 |
new ElementRule(CLADE, new XMLSyntaxRule[] { |
| 149 |
AttributeRule.newDoubleRule(HEIGHT, true), |
| 150 |
new ElementRule(TaxonList.class) |
| 151 |
}, 0, Integer.MAX_VALUE) |
| 152 |
}; |
| 153 |
} |