Debian Codegraph @

src/dr/evomodel/branchmodel/RandomBranchModel.java

raw symbols SCIP
1 /*
2 * RandomBranchModel.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.branchmodel;
27
28 import java.util.LinkedHashMap;
29 import java.util.LinkedList;
30 import java.util.List;
31
32 import org.apache.commons.math.random.MersenneTwister;
33
34 import dr.evomodel.substmodel.FrequencyModel;
35 import dr.evomodel.substmodel.codon.GY94CodonModel;
36 import dr.evomodel.substmodel.SubstitutionModel;
37 import dr.evolution.datatype.Codons;
38 import dr.evolution.datatype.DataType;
39 import dr.evolution.tree.NodeRef;
40 import dr.evomodel.tree.TreeModel;
41 import dr.inference.model.AbstractModel;
42 import dr.inference.model.Model;
43 import dr.inference.model.Parameter;
44 import dr.inference.model.Variable;
45 import dr.inference.model.Variable.ChangeType;
46 import dr.math.MathUtils;
47
48 /**
49 * @author Filip Bielejec
50 * @version $Id$
51 *
52 */
53
54 @SuppressWarnings("serial")
55 public class RandomBranchModel extends AbstractModel implements BranchModel {
56
57 public static final String RANDOM_BRANCH_MODEL = "randomBranchModel";
58 private final TreeModel treeModel;
59 private GY94CodonModel baseSubstitutionModel;
60 private LinkedList<SubstitutionModel> substitutionModels;
61 private LinkedHashMap<NodeRef, Integer> branchAssignmentMap;
62
63 // TODO: parse distribution model for e_i
64 // TODO: parse substitution model (hardcoded now)
65 // TODO: parse parameter that follows trend (hardcoded now)
66 private double rate;
67
68 private static MersenneTwister random;
69
70 public RandomBranchModel(TreeModel treeModel, //
71 GY94CodonModel baseSubstitutionModel, //
72 double rate, //
73 boolean hasSeed,
74 long seed
75 ) {
76
77 super(RANDOM_BRANCH_MODEL);
78
79 this.treeModel = treeModel;
80 this.baseSubstitutionModel = baseSubstitutionModel;
81
82 this.rate = rate;
83
84 if( hasSeed) {
85
86 // use fixed seed for e_i
87 random = new MersenneTwister(
88 seed);
89 } else {
90
91 //use BEAST seed
92 random = new MersenneTwister( MathUtils.nextLong() );
93
94 }//END: seed check
95
96
97 setup();
98
99 }// END: Constructor
100
101 private void setup() {
102
103 DataType dataType = baseSubstitutionModel.getDataType();
104 FrequencyModel freqModel = baseSubstitutionModel.getFrequencyModel();
105 Parameter kappaParameter = new Parameter.Default("kappa", 1,
106 baseSubstitutionModel.getKappa());
107
108 substitutionModels = new LinkedList<SubstitutionModel>();
109 branchAssignmentMap = new LinkedHashMap<NodeRef, Integer>();
110
111 int branchClass = 0;
112 for (NodeRef node : treeModel.getNodes()) {
113 if (!treeModel.isRoot(node)) {
114
115 double nodeHeight = treeModel.getNodeHeight(node);
116 double parentHeight = treeModel.getNodeHeight(treeModel
117 .getParent(node));
118
119 double time = 0.5 * (parentHeight + nodeHeight);
120
121 double baseOmega = baseSubstitutionModel.getOmega();
122
123 double fixed = baseOmega * time;
124
125 double epsilon = (Math.log(1-random.nextDouble()) / (-rate) ); //Math.exp((random.nextGaussian() * stdev + mean));
126
127 double value = fixed + epsilon;
128
129 Parameter omegaParameter = new Parameter.Default("omega", 1,
130 value);
131
132 GY94CodonModel gy94 = new GY94CodonModel((Codons) dataType,
133 omegaParameter, kappaParameter, freqModel);
134
135 substitutionModels.add(gy94);
136 branchAssignmentMap.put(node, branchClass);
137 branchClass++;
138 }//END: root check
139 }// END: nodes loop
140
141 }// END: setup
142
143 @Override
144 public Mapping getBranchModelMapping(NodeRef branch) {
145
146 final int branchClass = branchAssignmentMap.get(branch);
147
148 return new Mapping() {
149 public int[] getOrder() {
150 return new int[] { branchClass };
151 }
152
153 public double[] getWeights() {
154 return new double[] { 1.0 };
155 }
156 };
157 }
158
159 @Override
160 public List<SubstitutionModel> getSubstitutionModels() {
161 return substitutionModels;
162 }
163
164 @Override
165 public SubstitutionModel getRootSubstitutionModel() {
166 // int rootClass = branchAssignmentMap.get(treeModel.getRoot());
167 // return substitutionModels.get(rootClass);
168 throw new RuntimeException("Not implemented!");
169 }
170
171 @Override
172 public FrequencyModel getRootFrequencyModel() {
173 return getRootSubstitutionModel().getFrequencyModel();
174 }
175
176 @Override
177 public boolean requiresMatrixConvolution() {
178 return false;
179 }
180
181 @Override
182 protected void handleModelChangedEvent(Model model, Object object, int index) {
183 fireModelChanged();
184 }
185
186 @Override
187 protected void handleVariableChangedEvent(Variable variable, int index,
188 ChangeType type) {
189 }
190
191 @Override
192 protected void storeState() {
193 }
194
195 @Override
196 protected void restoreState() {
197 }
198
199 @Override
200 protected void acceptState() {
201 }
202
203 }// END: class