| 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.evomodel.epidemiology.casetocase; |
| 27 |
|
| 28 |
import dr.evolution.tree.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.tree.TreeUtils; |
| 31 |
import dr.evolution.util.Date; |
| 32 |
import dr.evolution.util.Taxon; |
| 33 |
import dr.evolution.util.TaxonList; |
| 34 |
import dr.inference.model.CompoundParameter; |
| 35 |
import dr.inference.model.Parameter; |
| 36 |
import dr.inference.model.ParameterParser; |
| 37 |
import dr.xml.*; |
| 38 |
|
| 39 |
import java.util.logging.Logger; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public class PartitionedTreeModelParser extends AbstractXMLObjectParser { |
| 50 |
|
| 51 |
public static final String ROOT_HEIGHT = "rootHeight"; |
| 52 |
public static final String LEAF_HEIGHT = "leafHeight"; |
| 53 |
public static final String LEAF_TRAIT = "leafTrait"; |
| 54 |
|
| 55 |
public static final String NODE_HEIGHTS = "nodeHeights"; |
| 56 |
public static final String NODE_RATES = "nodeRates"; |
| 57 |
public static final String NODE_TRAITS = "nodeTraits"; |
| 58 |
public static final String MULTIVARIATE_TRAIT = "traitDimension"; |
| 59 |
public static final String INITIAL_VALUE = "initialValue"; |
| 60 |
|
| 61 |
public static final String ROOT_NODE = "rootNode"; |
| 62 |
public static final String INTERNAL_NODES = "internalNodes"; |
| 63 |
public static final String LEAF_NODES = "leafNodes"; |
| 64 |
|
| 65 |
public static final String LEAF_HEIGHTS = "leafHeights"; |
| 66 |
|
| 67 |
public static final String FIRE_TREE_EVENTS = "fireTreeEvents"; |
| 68 |
|
| 69 |
public static final String TAXON = "taxon"; |
| 70 |
public static final String NAME = "name"; |
| 71 |
|
| 72 |
public static final String OUTBREAK = "outbreak"; |
| 73 |
public static final String STARTING_TT_FILE = "startingTransmissionTreeFile"; |
| 74 |
|
| 75 |
public PartitionedTreeModelParser() { |
| 76 |
rules = new XMLSyntaxRule[]{ |
| 77 |
new ElementRule(Tree.class), |
| 78 |
new ElementRule(ROOT_HEIGHT, Parameter.class, "A parameter definition with id only (cannot be a reference!)", false), |
| 79 |
new ElementRule(NODE_HEIGHTS, |
| 80 |
new XMLSyntaxRule[]{ |
| 81 |
AttributeRule.newBooleanRule(ROOT_NODE, true, "If true the root height is included in the parameter"), |
| 82 |
AttributeRule.newBooleanRule(INTERNAL_NODES, true, "If true the internal node heights (minus the root) are included in the parameter"), |
| 83 |
new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)") |
| 84 |
}, 1, Integer.MAX_VALUE), |
| 85 |
new ElementRule(LEAF_HEIGHT, |
| 86 |
new XMLSyntaxRule[]{ |
| 87 |
AttributeRule.newStringRule(TAXON, false, "The name of the taxon for the leaf"), |
| 88 |
new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)") |
| 89 |
}, 0, Integer.MAX_VALUE), |
| 90 |
new ElementRule(NODE_TRAITS, |
| 91 |
new XMLSyntaxRule[]{ |
| 92 |
AttributeRule.newStringRule(NAME, false, "The name of the trait attribute in the taxa"), |
| 93 |
AttributeRule.newBooleanRule(ROOT_NODE, true, "If true the root trait is included in the parameter"), |
| 94 |
AttributeRule.newBooleanRule(INTERNAL_NODES, true, "If true the internal node traits (minus the root) are included in the parameter"), |
| 95 |
AttributeRule.newBooleanRule(LEAF_NODES, true, "If true the leaf node traits are included in the parameter"), |
| 96 |
AttributeRule.newIntegerRule(MULTIVARIATE_TRAIT, true, "The number of dimensions (if multivariate)"), |
| 97 |
AttributeRule.newDoubleRule(INITIAL_VALUE, true, "The initial value(s)"), |
| 98 |
AttributeRule.newBooleanRule(FIRE_TREE_EVENTS, true, "Whether to fire tree events if the traits change"), |
| 99 |
new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)") |
| 100 |
}, 0, Integer.MAX_VALUE), |
| 101 |
new ElementRule(NODE_RATES, |
| 102 |
new XMLSyntaxRule[]{ |
| 103 |
AttributeRule.newBooleanRule(ROOT_NODE, true, "If true the root rate is included in the parameter"), |
| 104 |
AttributeRule.newBooleanRule(INTERNAL_NODES, true, "If true the internal node rate (minus the root) are included in the parameter"), |
| 105 |
AttributeRule.newBooleanRule(LEAF_NODES, true, "If true the leaf node rate are included in the parameter"), |
| 106 |
AttributeRule.newDoubleRule(INITIAL_VALUE, true, "The initial value(s)"), |
| 107 |
new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)") |
| 108 |
}, 0, Integer.MAX_VALUE), |
| 109 |
new ElementRule(LEAF_TRAIT, |
| 110 |
new XMLSyntaxRule[]{ |
| 111 |
AttributeRule.newStringRule(TAXON, false, "The name of the taxon for the leaf"), |
| 112 |
AttributeRule.newStringRule(NAME, false, "The name of the trait attribute in the taxa"), |
| 113 |
new ElementRule(Parameter.class, "A parameter definition with id only (cannot be a reference!)") |
| 114 |
}, 0, Integer.MAX_VALUE), |
| 115 |
new ElementRule(LEAF_HEIGHTS, |
| 116 |
new XMLSyntaxRule[]{ |
| 117 |
new ElementRule(TaxonList.class, "A set of taxa for which leaf heights are required"), |
| 118 |
new ElementRule(Parameter.class, "A compound parameter containing the leaf heights") |
| 119 |
}, true), |
| 120 |
new ElementRule(OUTBREAK, AbstractOutbreak.class, "The case data"), |
| 121 |
AttributeRule.newStringRule(STARTING_TT_FILE, true) |
| 122 |
}; |
| 123 |
} |
| 124 |
|
| 125 |
public String getParserName() { |
| 126 |
return PartitionedTreeModel.PARTITIONED_TREE_MODEL; |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 133 |
|
| 134 |
Tree tree = (Tree) xo.getChild(Tree.class); |
| 135 |
|
| 136 |
AbstractOutbreak outbreak = (AbstractOutbreak)xo.getElementFirstChild(OUTBREAK); |
| 137 |
PartitionedTreeModel treeModel; |
| 138 |
|
| 139 |
if(xo.hasAttribute(STARTING_TT_FILE)){ |
| 140 |
treeModel = new PartitionedTreeModel(xo.getId(), tree, outbreak, xo.getStringAttribute(STARTING_TT_FILE)); |
| 141 |
} else { |
| 142 |
treeModel = new PartitionedTreeModel(xo.getId(), tree, outbreak); |
| 143 |
} |
| 144 |
|
| 145 |
Logger.getLogger("dr.evomodel").info("Creating the partitioned tree model, '" + xo.getId() + "'"); |
| 146 |
|
| 147 |
for (int i = 0; i < xo.getChildCount(); i++) { |
| 148 |
if (xo.getChild(i) instanceof XMLObject) { |
| 149 |
|
| 150 |
XMLObject cxo = (XMLObject) xo.getChild(i); |
| 151 |
|
| 152 |
if (cxo.getName().equals(ROOT_HEIGHT)) { |
| 153 |
|
| 154 |
ParameterParser.replaceParameter(cxo, treeModel.getRootHeightParameter()); |
| 155 |
|
| 156 |
} else if (cxo.getName().equals(LEAF_HEIGHT)) { |
| 157 |
|
| 158 |
String taxonName; |
| 159 |
if (cxo.hasAttribute(TAXON)) { |
| 160 |
taxonName = cxo.getStringAttribute(TAXON); |
| 161 |
} else { |
| 162 |
throw new XMLParseException("taxa element missing from leafHeight element in treeModel element"); |
| 163 |
} |
| 164 |
|
| 165 |
int index = treeModel.getTaxonIndex(taxonName); |
| 166 |
if (index == -1) { |
| 167 |
throw new XMLParseException("taxon " + taxonName + " not found for leafHeight element in treeModel element"); |
| 168 |
} |
| 169 |
NodeRef node = treeModel.getExternalNode(index); |
| 170 |
|
| 171 |
Parameter newParameter = treeModel.getLeafHeightParameter(node); |
| 172 |
|
| 173 |
ParameterParser.replaceParameter(cxo, newParameter); |
| 174 |
|
| 175 |
Taxon taxon = treeModel.getTaxon(index); |
| 176 |
|
| 177 |
setPrecisionBounds(newParameter, taxon); |
| 178 |
|
| 179 |
} else if (cxo.getName().equals(LEAF_HEIGHTS)) { |
| 180 |
|
| 181 |
|
| 182 |
TaxonList taxa = (TaxonList)cxo.getChild(TaxonList.class); |
| 183 |
Parameter offsetParameter = (Parameter)cxo.getChild(Parameter.class); |
| 184 |
|
| 185 |
CompoundParameter leafHeights = new CompoundParameter("leafHeights"); |
| 186 |
for (Taxon taxon : taxa) { |
| 187 |
int index = treeModel.getTaxonIndex(taxon); |
| 188 |
if (index == -1) { |
| 189 |
throw new XMLParseException("taxon " + taxon.getId() + " not found for leafHeight element in treeModel element"); |
| 190 |
} |
| 191 |
NodeRef node = treeModel.getExternalNode(index); |
| 192 |
|
| 193 |
Parameter newParameter = treeModel.getLeafHeightParameter(node); |
| 194 |
|
| 195 |
leafHeights.addParameter(newParameter); |
| 196 |
|
| 197 |
setPrecisionBounds(newParameter, taxon); |
| 198 |
} |
| 199 |
|
| 200 |
ParameterParser.replaceParameter(cxo, leafHeights); |
| 201 |
|
| 202 |
} else if (cxo.getName().equals(NODE_HEIGHTS)) { |
| 203 |
|
| 204 |
boolean rootNode = cxo.getAttribute(ROOT_NODE, false); |
| 205 |
boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false); |
| 206 |
boolean leafNodes = cxo.getAttribute(LEAF_NODES, false); |
| 207 |
|
| 208 |
if (!rootNode && !internalNodes && !leafNodes) { |
| 209 |
throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeHeights element"); |
| 210 |
} |
| 211 |
|
| 212 |
ParameterParser.replaceParameter(cxo, treeModel.createNodeHeightsParameter(rootNode, internalNodes, leafNodes)); |
| 213 |
|
| 214 |
} else if (cxo.getName().equals(NODE_RATES)) { |
| 215 |
|
| 216 |
boolean rootNode = cxo.getAttribute(ROOT_NODE, false); |
| 217 |
boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false); |
| 218 |
boolean leafNodes = cxo.getAttribute(LEAF_NODES, false); |
| 219 |
double[] initialValues = null; |
| 220 |
|
| 221 |
if (cxo.hasAttribute(INITIAL_VALUE)) { |
| 222 |
initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE); |
| 223 |
} |
| 224 |
|
| 225 |
if (!rootNode && !internalNodes && !leafNodes) { |
| 226 |
throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeRates element"); |
| 227 |
} |
| 228 |
|
| 229 |
ParameterParser.replaceParameter(cxo, treeModel.createNodeRatesParameter(initialValues, rootNode, internalNodes, leafNodes)); |
| 230 |
|
| 231 |
} else if (cxo.getName().equals(NODE_TRAITS)) { |
| 232 |
|
| 233 |
boolean rootNode = cxo.getAttribute(ROOT_NODE, false); |
| 234 |
boolean internalNodes = cxo.getAttribute(INTERNAL_NODES, false); |
| 235 |
boolean leafNodes = cxo.getAttribute(LEAF_NODES, false); |
| 236 |
boolean fireTreeEvents = cxo.getAttribute(FIRE_TREE_EVENTS, false); |
| 237 |
String name = cxo.getAttribute(NAME, "trait"); |
| 238 |
int dim = cxo.getAttribute(MULTIVARIATE_TRAIT, 1); |
| 239 |
|
| 240 |
double[] initialValues = null; |
| 241 |
if (cxo.hasAttribute(INITIAL_VALUE)) { |
| 242 |
initialValues = cxo.getDoubleArrayAttribute(INITIAL_VALUE); |
| 243 |
} |
| 244 |
|
| 245 |
if (!rootNode && !internalNodes && !leafNodes) { |
| 246 |
throw new XMLParseException("one or more of root, internal or leaf nodes must be selected for the nodeTraits element"); |
| 247 |
} |
| 248 |
|
| 249 |
ParameterParser.replaceParameter(cxo, treeModel.createNodeTraitsParameter(name, dim, initialValues, rootNode, internalNodes, leafNodes, fireTreeEvents)); |
| 250 |
|
| 251 |
} else if (cxo.getName().equals(LEAF_TRAIT)) { |
| 252 |
|
| 253 |
String name = cxo.getAttribute(NAME, "trait"); |
| 254 |
|
| 255 |
String taxonName; |
| 256 |
if (cxo.hasAttribute(TAXON)) { |
| 257 |
taxonName = cxo.getStringAttribute(TAXON); |
| 258 |
} else { |
| 259 |
throw new XMLParseException("taxa element missing from leafTrait element in treeModel element"); |
| 260 |
} |
| 261 |
|
| 262 |
int index = treeModel.getTaxonIndex(taxonName); |
| 263 |
if (index == -1) { |
| 264 |
throw new XMLParseException("taxon '" + taxonName + "' not found for leafTrait element in treeModel element"); |
| 265 |
} |
| 266 |
NodeRef node = treeModel.getExternalNode(index); |
| 267 |
|
| 268 |
Parameter parameter = treeModel.getNodeTraitParameter(node, name); |
| 269 |
|
| 270 |
if (parameter == null) |
| 271 |
throw new XMLParseException("trait '" + name + "' not found for leafTrait (taxon, " + taxonName + ") element in treeModel element"); |
| 272 |
|
| 273 |
ParameterParser.replaceParameter(cxo, parameter); |
| 274 |
|
| 275 |
} else { |
| 276 |
if(!cxo.getName().equals(OUTBREAK)) { |
| 277 |
throw new XMLParseException("illegal child element in " + getParserName() + ": " + cxo.getName()); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
} else if (xo.getChild(i) instanceof Tree) { |
| 282 |
|
| 283 |
} else { |
| 284 |
throw new XMLParseException("illegal child element in " + getParserName() + ": " + xo.getChildName(i) + " " + xo.getChild(i)); |
| 285 |
} |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
Logger.getLogger("dr.evomodel").info(" initial tree topology = " + TreeUtils.uniqueNewick(treeModel, treeModel.getRoot())); |
| 293 |
Logger.getLogger("dr.evomodel").info(" tree height = " + treeModel.getNodeHeight(treeModel.getRoot())); |
| 294 |
return treeModel; |
| 295 |
} |
| 296 |
|
| 297 |
private void setPrecisionBounds(Parameter newParameter, Taxon taxon) { |
| 298 |
Date date = taxon.getDate(); |
| 299 |
if (date != null) { |
| 300 |
double precision = date.getUncertainty(); |
| 301 |
if (precision > 0.0) { |
| 302 |
|
| 303 |
double upper = Taxon.getHeightFromDate(date); |
| 304 |
double lower = Taxon.getHeightFromDate(date); |
| 305 |
if (date.isBackwards()) { |
| 306 |
upper += precision; |
| 307 |
} else { |
| 308 |
lower -= precision; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
newParameter.addBounds(new Parameter.DefaultBounds(upper, lower, 1)); |
| 313 |
|
| 314 |
|
| 315 |
newParameter.setParameterValue(0, (upper + lower) / 2); |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
public String getParserDescription() { |
| 325 |
return "This element represents a model of a phylogenetic tree together with the partitioning of its nodes " + |
| 326 |
"into connected subgraphs to represent the transmission tree."; |
| 327 |
} |
| 328 |
|
| 329 |
public Class getReturnType() { |
| 330 |
return PartitionedTreeModel.class; |
| 331 |
} |
| 332 |
|
| 333 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 334 |
return rules; |
| 335 |
} |
| 336 |
|
| 337 |
private final XMLSyntaxRule[] rules; |
| 338 |
} |