Debian Codegraph @

src/dr/evomodel/continuous/TreeTraitSimulator.java

raw symbols SCIP
1 /*
2 * TreeTraitSimulator.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 * Created by IntelliJ IDEA.
36 * User: mandevgill
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 // for(int i = 0; i < numTaxa; i++){
58 // for(int j = 0; j < dimTrait; j++){
59 // temp[j] = simValues[i*dimTrait + j];
60 // }
61 // traitTree.setTaxonAttribute(i,traitName,temp);
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 // System.err.print("taxon: " + taxon.getId());
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 // System.err.println(" : " + sb.toString());
79 // System.err.println(" : " + taxon.getAttribute(traitName));
80 }
81 // System.err.println("Wasn't that fun?");
82
83
84 // tree.
85 // System.exit(-1);
86 // return traitTree;
87 }
88
89
90 // **************************************************************
91 // XMLObjectParser
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 // System.err.println("Getting here!");
102 // System.exit(-1);
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 // return
110 treeTraitSimulator.simulate();
111 return treeTraitSimulator;
112 }
113
114 //************************************************************************
115 // AbstractXMLObjectParser implementation
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 }