| 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.continuous; |
| 27 |
|
| 28 |
import dr.evolution.tree.FlexibleTree; |
| 29 |
import dr.evolution.tree.MutableTree; |
| 30 |
import dr.evolution.tree.NodeRef; |
| 31 |
import dr.evolution.tree.Tree; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class SimulateTrait { |
| 42 |
|
| 43 |
public static final String SIMULATE_TRAIT = "traitTree"; |
| 44 |
public static final String TRAIT_NAME = "traitName"; |
| 45 |
public static final String CLONE = "clone"; |
| 46 |
public static final String INITIAL_VALUE = "initialValue"; |
| 47 |
public static final String MODEL = "model"; |
| 48 |
public static final String TREE = "tree"; |
| 49 |
|
| 50 |
public SimulateTrait(DiffusionModel diffusionModel, String traitName) { |
| 51 |
|
| 52 |
this.diffusionModel = diffusionModel; |
| 53 |
this.traitName = traitName; |
| 54 |
} |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public Tree simulate(Tree tree, double value, boolean clone) { |
| 62 |
|
| 63 |
Tree binaryTree = null; |
| 64 |
|
| 65 |
if (clone) { |
| 66 |
binaryTree = new FlexibleTree(tree); |
| 67 |
((FlexibleTree)binaryTree).resolveTree(); |
| 68 |
} else { |
| 69 |
binaryTree = tree; |
| 70 |
} |
| 71 |
simulate((MutableTree)binaryTree, binaryTree.getRoot(), value); |
| 72 |
|
| 73 |
return binaryTree; |
| 74 |
} |
| 75 |
|
| 76 |
private void simulate(MutableTree tree, NodeRef node, double value) { |
| 77 |
|
| 78 |
tree.setNodeAttribute(node, traitName, new Double(value)); |
| 79 |
int childCount = tree.getChildCount(node); |
| 80 |
double height = tree.getNodeHeight(node); |
| 81 |
for (int i = 0; i < childCount; i++) { |
| 82 |
NodeRef child = tree.getChild(node, i); |
| 83 |
|
| 84 |
simulate(tree, child, diffusionModel.simulateForward(value, height - tree.getNodeHeight(child))); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 93 |
|
| 94 |
public String getParserName() { return SIMULATE_TRAIT; } |
| 95 |
|
| 96 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 97 |
|
| 98 |
String traitName = xo.getStringAttribute(TRAIT_NAME); |
| 99 |
boolean clone = xo.getBooleanAttribute(CLONE); |
| 100 |
double initialValue = xo.getDoubleAttribute(INITIAL_VALUE); |
| 101 |
|
| 102 |
DiffusionModel diffusionModel = (DiffusionModel)xo.getChild(DiffusionModel.class); |
| 103 |
Tree tree = (Tree)xo.getChild(Tree.class); |
| 104 |
|
| 105 |
SimulateTrait simulateTrait = new SimulateTrait(diffusionModel, traitName); |
| 106 |
Tree simTree = simulateTrait.simulate(tree, initialValue, clone); |
| 107 |
return simTree; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public XMLSyntaxRule[] getSyntaxRules() { return rules; } |
| 115 |
|
| 116 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[] { |
| 117 |
new StringAttributeRule(TRAIT_NAME, "The name to be given to the trait to be simulated"), |
| 118 |
AttributeRule.newBooleanRule(CLONE), |
| 119 |
AttributeRule.newDoubleRule(INITIAL_VALUE), |
| 120 |
new ElementRule(DiffusionModel.class), |
| 121 |
new ElementRule(Tree.class) |
| 122 |
}; |
| 123 |
|
| 124 |
public String getParserDescription() { |
| 125 |
return "Simulates a trait on a tree"; |
| 126 |
} |
| 127 |
|
| 128 |
public Class getReturnType() { return Tree.class; } |
| 129 |
}; |
| 130 |
|
| 131 |
DiffusionModel diffusionModel = null; |
| 132 |
String traitName = null; |
| 133 |
} |
| 134 |
|