| 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.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.tree.TreeUtils; |
| 31 |
import dr.evomodel.tree.StarTreeModel; |
| 32 |
import dr.evomodel.tree.TreeModel; |
| 33 |
import dr.inference.model.Parameter; |
| 34 |
import dr.inference.model.ParameterParser; |
| 35 |
import dr.xml.*; |
| 36 |
|
| 37 |
import java.util.logging.Logger; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
public class StarTreeModelParser extends AbstractXMLObjectParser { |
| 43 |
|
| 44 |
public static final String STAR_TREE_MODEL = "starTreeModel"; |
| 45 |
|
| 46 |
public static final String SHARE_ROOT = "sharedRootHeight"; |
| 47 |
|
| 48 |
public static final String ROOT_HEIGHT = "rootHeight"; |
| 49 |
public static final String LEAF_HEIGHT = "leafHeight"; |
| 50 |
public static final String LEAF_TRAIT = "leafTrait"; |
| 51 |
|
| 52 |
|
| 53 |
public static final String NODE_RATES = "nodeRates"; |
| 54 |
public static final String NODE_TRAITS = "nodeTraits"; |
| 55 |
public static final String MULTIVARIATE_TRAIT = "traitDimension"; |
| 56 |
public static final String INITIAL_VALUE = "initialValue"; |
| 57 |
|
| 58 |
public static final String ROOT_NODE = "rootNode"; |
| 59 |
public static final String INTERNAL_NODES = "internalNodes"; |
| 60 |
public static final String LEAF_NODES = "leafNodes"; |
| 61 |
public static final String FIRE_TREE_EVENTS = "fireTreeEvents"; |
| 62 |
|
| 63 |
public static final String TAXON = "taxon"; |
| 64 |
public static final String NAME = "name"; |
| 65 |
|
| 66 |
public StarTreeModelParser() { |
| 67 |
rules = new XMLSyntaxRule[]{ |
| 68 |
new ElementRule(Tree.class), |
| 69 |
new XORRule( |
| 70 |
new ElementRule(ROOT_HEIGHT, Parameter.class, "A parameter definition with id only (cannot be a reference!)", false), |
| 71 |
new ElementRule(SHARE_ROOT, TreeModel.class) |
| 72 |
), |
| 73 |
new ElementRule(LEAF_HEIGHT, |
| 74 |
new XMLSyntaxRule[]{ |
| 75 |
AttributeRule.newStringRule(TAXON, false, "The name of the taxon for the leaf"), |
| 76 |
new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)") |
| 77 |
}, 0, Integer.MAX_VALUE), |
| 78 |
new ElementRule(LEAF_TRAIT, |
| 79 |
new XMLSyntaxRule[]{ |
| 80 |
AttributeRule.newStringRule(TAXON, false, "The name of the taxon for the leaf"), |
| 81 |
AttributeRule.newStringRule(NAME, false, "The name of the trait attribute in the taxa"), |
| 82 |
new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)") |
| 83 |
}, 0, Integer.MAX_VALUE) |
| 84 |
}; |
| 85 |
} |
| 86 |
|
| 87 |
public String getParserName() { |
| 88 |
return STAR_TREE_MODEL; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 95 |
|
| 96 |
Tree tree = (Tree) xo.getChild(Tree.class); |
| 97 |
StarTreeModel treeModel = new StarTreeModel(xo.getId(), tree); |
| 98 |
|
| 99 |
Logger.getLogger("dr.evomodel").info("Creating a star tree model, '" + xo.getId() + "'"); |
| 100 |
|
| 101 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 102 |
if (xo.getChild(i) instanceof XMLObject) { |
| 103 |
|
| 104 |
XMLObject cxo = (XMLObject) xo.getChild(i); |
| 105 |
|
| 106 |
if (cxo.getName().equals(ROOT_HEIGHT)) { |
| 107 |
|
| 108 |
if (cxo.getRawChild(0) instanceof Reference) { |
| 109 |
|
| 110 |
|
| 111 |
throw new XMLParseException("Can not provide idref to a new root height parameter"); |
| 112 |
} else { |
| 113 |
ParameterParser.replaceParameter(cxo, treeModel.getRootHeightParameter()); |
| 114 |
} |
| 115 |
|
| 116 |
} else if (cxo.getName().equals(SHARE_ROOT)) { |
| 117 |
TreeModel sharedRoot = (TreeModel) cxo.getChild(TreeModel.class); |
| 118 |
treeModel.setSharedRootHeightParameter(sharedRoot); |
| 119 |
} else if (cxo.getName().equals(LEAF_HEIGHT)) { |
| 120 |
|
| 121 |
String taxonName; |
| 122 |
if (cxo.hasAttribute(TAXON)) { |
| 123 |
taxonName = cxo.getStringAttribute(TAXON); |
| 124 |
} else { |
| 125 |
throw new XMLParseException("taxa element missing from leafHeight element in treeModel element"); |
| 126 |
} |
| 127 |
|
| 128 |
int index = treeModel.getTaxonIndex(taxonName); |
| 129 |
if (index == -1) { |
| 130 |
throw new XMLParseException("taxon " + taxonName + " not found for leafHeight element in treeModel element"); |
| 131 |
} |
| 132 |
NodeRef node = treeModel.getExternalNode(index); |
| 133 |
ParameterParser.replaceParameter(cxo, treeModel.getLeafHeightParameter(node)); |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
} else if (cxo.getName().equals(LEAF_TRAIT)) { |
| 185 |
|
| 186 |
String name = cxo.getAttribute(NAME, "trait"); |
| 187 |
|
| 188 |
String taxonName; |
| 189 |
if (cxo.hasAttribute(TAXON)) { |
| 190 |
taxonName = cxo.getStringAttribute(TAXON); |
| 191 |
} else { |
| 192 |
throw new XMLParseException("taxa element missing from leafTrait element in treeModel element"); |
| 193 |
} |
| 194 |
|
| 195 |
int index = treeModel.getTaxonIndex(taxonName); |
| 196 |
if (index == -1) { |
| 197 |
throw new XMLParseException("taxon '" + taxonName + "' not found for leafTrait element in treeModel element"); |
| 198 |
} |
| 199 |
NodeRef node = treeModel.getExternalNode(index); |
| 200 |
|
| 201 |
Parameter parameter = treeModel.getNodeTraitParameter(node, name); |
| 202 |
|
| 203 |
if (parameter == null) |
| 204 |
throw new XMLParseException("trait '" + name + "' not found for leafTrait (taxon, " + taxonName + ") element in treeModel element"); |
| 205 |
|
| 206 |
ParameterParser.replaceParameter(cxo, parameter); |
| 207 |
|
| 208 |
|
| 209 |
} else { |
| 210 |
throw new XMLParseException("illegal child element in " + getParserName() + ": " + cxo.getName()); |
| 211 |
} |
| 212 |
|
| 213 |
} else if (xo.getChild(i) instanceof Tree) { |
| 214 |
|
| 215 |
} else { |
| 216 |
throw new XMLParseException("illegal child element in " + getParserName() + ": " + xo.getChildName(i) + " " + xo.getChild(i)); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
Logger.getLogger("dr.evomodel").info(" initial tree topology = " + TreeUtils.uniqueNewick(treeModel, treeModel.getRoot())); |
| 221 |
Logger.getLogger("dr.evomodel").info(" tree height = " + treeModel.getNodeHeight(treeModel.getRoot())); |
| 222 |
return treeModel; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public String getParserDescription() { |
| 230 |
return "This element represents a model of the tree. The tree model includes and attributes of the nodes " + |
| 231 |
"including the age (or <i>height</i>) and the rate of evolution at each node in the tree."; |
| 232 |
} |
| 233 |
|
| 234 |
public String getExample() { |
| 235 |
return |
| 236 |
"<!-- the tree model as special sockets for attaching parameters to various aspects of the tree -->\n" + |
| 237 |
"<!-- The treeModel below shows the standard setup with a parameter associated with the root height -->\n" + |
| 238 |
"<!-- a parameter associated with the internal node heights (minus the root height) and -->\n" + |
| 239 |
"<!-- a parameter associates with all the internal node heights -->\n" + |
| 240 |
"<!-- Notice that these parameters are overlapping -->\n" + |
| 241 |
"<!-- The parameters are subsequently used in operators to propose changes to the tree node heights -->\n" + |
| 242 |
"<treeModel id=\"treeModel1\">\n" + |
| 243 |
" <tree idref=\"startingTree\"/>\n" + |
| 244 |
" <rootHeight>\n" + |
| 245 |
" <parameter id=\"treeModel1.rootHeight\"/>\n" + |
| 246 |
" </rootHeight>\n" + |
| 247 |
" <nodeHeights internalNodes=\"true\" rootNode=\"false\">\n" + |
| 248 |
" <parameter id=\"treeModel1.internalNodeHeights\"/>\n" + |
| 249 |
" </nodeHeights>\n" + |
| 250 |
" <nodeHeights internalNodes=\"true\" rootNode=\"true\">\n" + |
| 251 |
" <parameter id=\"treeModel1.allInternalNodeHeights\"/>\n" + |
| 252 |
" </nodeHeights>\n" + |
| 253 |
"</treeModel>"; |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
public Class getReturnType() { |
| 258 |
return StarTreeModel.class; |
| 259 |
} |
| 260 |
|
| 261 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 262 |
return rules; |
| 263 |
} |
| 264 |
|
| 265 |
private final XMLSyntaxRule[] rules; |
| 266 |
} |