| 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.MutableTreeModel; |
| 29 |
import dr.evolution.tree.NodeRef; |
| 30 |
import dr.evolution.tree.Tree; |
| 31 |
import dr.evolution.tree.TreeUtils; |
| 32 |
import dr.evolution.util.Taxa; |
| 33 |
import dr.evolution.util.Taxon; |
| 34 |
import dr.evolution.util.TaxonList; |
| 35 |
import dr.evomodel.continuous.AncestralTaxonInTree; |
| 36 |
import dr.evomodel.tree.*; |
| 37 |
import dr.inference.model.FastMatrixParameter; |
| 38 |
import dr.inference.model.Parameter; |
| 39 |
import dr.inference.model.ParameterParser; |
| 40 |
import dr.xml.*; |
| 41 |
|
| 42 |
import java.util.ArrayList; |
| 43 |
import java.util.List; |
| 44 |
|
| 45 |
import static dr.evomodelxml.tree.TreeModelParser.*; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public class AncestralTraitTreeModelParser extends AbstractXMLObjectParser { |
| 51 |
|
| 52 |
public static final String ANCESTRAL_TRAIT_TREE_MODEL = "ancestralTraitTreeModel"; |
| 53 |
|
| 54 |
public static final String ANCESTOR = "ancestor"; |
| 55 |
|
| 56 |
public String getParserName() { |
| 57 |
return ANCESTRAL_TRAIT_TREE_MODEL; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 64 |
|
| 65 |
MutableTreeModel tree = (MutableTreeModel) xo.getChild(MutableTreeModel.class); |
| 66 |
List<AncestralTaxonInTree> ancestors = parseAllAncestors(tree, xo); |
| 67 |
|
| 68 |
int index = tree.getExternalNodeCount(); |
| 69 |
for (AncestralTaxonInTree ancestor : ancestors) { |
| 70 |
ancestor.setIndex(index); |
| 71 |
ancestor.setNode(new NodeRef() { |
| 72 |
@Override |
| 73 |
public int getNumber() { |
| 74 |
return 0; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public void setNumber(int n) { |
| 79 |
|
| 80 |
} |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
if (xo.hasChildNamed(NODE_TRAITS)) { |
| 87 |
for (XMLObject cxo : xo.getAllChildren(NODE_TRAITS)) { |
| 88 |
parseNodeTraits(cxo, tree, ancestors); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
return new AncestralTraitTreeModel(xo.getId(), tree, ancestors); |
| 93 |
} |
| 94 |
|
| 95 |
private static void parseNodeTraits(XMLObject cxo, Tree tree, List<AncestralTaxonInTree> ancestors) |
| 96 |
throws XMLParseException { |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
String name = cxo.getAttribute(NAME, "trait"); |
| 104 |
int dim = cxo.getAttribute(MULTIVARIATE_TRAIT, 1); |
| 105 |
|
| 106 |
double[] initialValues = null; |
| 107 |
if (cxo.hasAttribute(INITIAL_VALUE)) { |
| 108 |
initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE); |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
final int rowDim = dim; |
| 116 |
final int colDim = tree.getExternalNodeCount() + ancestors.size(); |
| 117 |
|
| 118 |
FastMatrixParameter parameter = new FastMatrixParameter(name, rowDim, colDim, 0.0); |
| 119 |
parameter.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY, |
| 120 |
rowDim * colDim)); |
| 121 |
|
| 122 |
int parameterIndex = 0; |
| 123 |
for (int i = 0; i < tree.getExternalNodeCount(); i++) { |
| 124 |
|
| 125 |
|
| 126 |
++parameterIndex; |
| 127 |
|
| 128 |
} |
| 129 |
|
| 130 |
for (AncestralTaxonInTree ancestor : ancestors) { |
| 131 |
++parameterIndex; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
ParameterParser.replaceParameter(cxo, parameter); |
| 136 |
} |
| 137 |
|
| 138 |
private static List<AncestralTaxonInTree> parseAllAncestors(MutableTreeModel tree, XMLObject xo) throws XMLParseException { |
| 139 |
int index = tree.getExternalNodeCount(); |
| 140 |
List<AncestralTaxonInTree> ancestors = new ArrayList<AncestralTaxonInTree>(); |
| 141 |
for (XMLObject cxo : xo.getAllChildren(ANCESTOR)) { |
| 142 |
ancestors.add(parseAncestor(tree, cxo, index)); |
| 143 |
++index; |
| 144 |
} |
| 145 |
return ancestors; |
| 146 |
} |
| 147 |
|
| 148 |
private static AncestralTaxonInTree parseAncestor(MutableTreeModel tree, XMLObject xo, final int index) throws XMLParseException { |
| 149 |
|
| 150 |
Taxon ancestor = (Taxon) xo.getChild(Taxon.class); |
| 151 |
|
| 152 |
TaxonList descendants = MonophylyStatisticParser.parseTaxonListOrTaxa( |
| 153 |
xo.getChild(MonophylyStatisticParser.MRCA)); |
| 154 |
|
| 155 |
Parameter pseudoBranchLength = (Parameter) xo.getChild(Parameter.class); |
| 156 |
|
| 157 |
AncestralTaxonInTree ancestorInTree; |
| 158 |
try { |
| 159 |
|
| 160 |
NodeRef node = new NodeRef() { |
| 161 |
@Override |
| 162 |
public int getNumber() { |
| 163 |
return index; |
| 164 |
} |
| 165 |
|
| 166 |
@Override |
| 167 |
public void setNumber(int n) { |
| 168 |
throw new RuntimeException("Do not set"); |
| 169 |
} |
| 170 |
}; |
| 171 |
|
| 172 |
ancestorInTree = new AncestralTaxonInTree(ancestor, tree, descendants, pseudoBranchLength, |
| 173 |
node, index); |
| 174 |
} catch (TreeUtils.MissingTaxonException e) { |
| 175 |
throw new XMLParseException("Unable to find taxa for " + ancestor.getId()); |
| 176 |
} |
| 177 |
|
| 178 |
return ancestorInTree; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
public String getParserDescription() { |
| 186 |
return "This element represents a transformed model of the tree."; |
| 187 |
} |
| 188 |
|
| 189 |
public Class getReturnType() { |
| 190 |
return TransformedTreeModel.class; |
| 191 |
} |
| 192 |
|
| 193 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 194 |
return rules; |
| 195 |
} |
| 196 |
|
| 197 |
private final XMLSyntaxRule[] rules = |
| 198 |
new XMLSyntaxRule[]{ |
| 199 |
new ElementRule(MutableTreeModel.class), |
| 200 |
|
| 201 |
new ElementRule(ANCESTOR, new XMLSyntaxRule[] { |
| 202 |
new ElementRule(Taxon.class), |
| 203 |
new ElementRule(Parameter.class), |
| 204 |
new ElementRule(MonophylyStatisticParser.MRCA, new XMLSyntaxRule[]{ |
| 205 |
new XORRule( |
| 206 |
new ElementRule(Taxon.class, 1, Integer.MAX_VALUE), |
| 207 |
new ElementRule(Taxa.class) |
| 208 |
) |
| 209 |
}), |
| 210 |
}, 0, Integer.MAX_VALUE), |
| 211 |
nodeTraitsRule, |
| 212 |
}; |
| 213 |
} |