Debian Codegraph @

src/dr/evomodel/continuous/SimulateTrait.java

raw symbols SCIP
1 /*
2 * SimulateTrait.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.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 * This class simulates a trait on a tree.
36 *
37 * @author Alexei Drummond
38 * @version $Id: SimulateTrait.java,v 1.5 2004/10/01 22:40:04 alexei Exp $
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 * simulates a trait ona tree.
58 * @param clone if true, use copy of the tree, otherwise use given tree
59 * @return the simulated tree.
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 // XMLObjectParser
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 // AbstractXMLObjectParser implementation
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