| 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.NodeRef; |
| 29 |
import dr.evolution.tree.Tree; |
| 30 |
import dr.evolution.util.Taxon; |
| 31 |
import dr.evomodel.tree.TreeModel; |
| 32 |
import dr.xml.*; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
public class TreeTraitSimulator { |
| 39 |
|
| 40 |
public static final String SIMULATE_TREE_TRAITS = "treeTraitSimulator"; |
| 41 |
public static final String TREE_TRAIT_NAME = "traitName"; |
| 42 |
|
| 43 |
public TreeTraitSimulator(TreeTraitNormalDistributionModel treeTraitModel, TreeModel traitTree, String traitName) { |
| 44 |
this.traitTree = traitTree; |
| 45 |
this.treeTraitModel = treeTraitModel; |
| 46 |
this.traitName = traitName; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
public void simulate() { |
| 51 |
|
| 52 |
double[] simValues = (double[]) treeTraitModel.nextRandom(); |
| 53 |
int numTaxa = traitTree.getTaxonCount(); |
| 54 |
int dimTrait = treeTraitModel.getDimTrait(); |
| 55 |
double temp[] = new double[dimTrait]; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
System.err.println("Playing with generated values..."); |
| 65 |
Tree treeModel = treeTraitModel.getTree(); |
| 66 |
|
| 67 |
for (int i = 0; i < numTaxa; ++i) { |
| 68 |
NodeRef node = treeModel.getExternalNode(i); |
| 69 |
Taxon taxon = treeModel.getNodeTaxon(node); |
| 70 |
|
| 71 |
|
| 72 |
StringBuilder sb = new StringBuilder(); |
| 73 |
for (int j = 0; j < dimTrait; ++j) { |
| 74 |
if (j > 0) sb.append(" "); |
| 75 |
sb.append(simValues[i * dimTrait + j]); |
| 76 |
} |
| 77 |
taxon.setAttribute(traitName, sb.toString()); |
| 78 |
|
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 95 |
|
| 96 |
public String getParserName() { |
| 97 |
return SIMULATE_TREE_TRAITS; |
| 98 |
} |
| 99 |
|
| 100 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
String traitName = xo.getStringAttribute(TREE_TRAIT_NAME); |
| 105 |
TreeTraitNormalDistributionModel treeTraitModel = (TreeTraitNormalDistributionModel) xo.getChild(TreeTraitNormalDistributionModel.class); |
| 106 |
TreeModel traitTree = (TreeModel) xo.getChild(TreeModel.class); |
| 107 |
|
| 108 |
TreeTraitSimulator treeTraitSimulator = new TreeTraitSimulator(treeTraitModel, traitTree, traitName); |
| 109 |
|
| 110 |
treeTraitSimulator.simulate(); |
| 111 |
return treeTraitSimulator; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 119 |
return rules; |
| 120 |
} |
| 121 |
|
| 122 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 123 |
new StringAttributeRule(TREE_TRAIT_NAME, "The name to be given to the trait to be simulated"), |
| 124 |
new ElementRule(TreeTraitNormalDistributionModel.class), |
| 125 |
new ElementRule(TreeModel.class) |
| 126 |
}; |
| 127 |
|
| 128 |
public String getParserDescription() { |
| 129 |
return "Simulates a trait on a tree"; |
| 130 |
} |
| 131 |
|
| 132 |
public Class getReturnType() { |
| 133 |
return TreeTraitSimulator.class; |
| 134 |
} |
| 135 |
}; |
| 136 |
|
| 137 |
TreeModel traitTree = null; |
| 138 |
TreeTraitNormalDistributionModel treeTraitModel = null; |
| 139 |
String traitName = null; |
| 140 |
} |