Debian Codegraph @

src/dr/evomodel/branchmodel/lineagespecific/LineageSpecificBranchModel.java

raw symbols SCIP
1 /*
2 * LineageSpecificBranchModel.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.lineagespecific;
27
28 import java.util.*;
29
30 import dr.evomodel.branchmodel.BranchModel;
31 import dr.evomodel.branchmodel.HomogeneousBranchModel;
32 import dr.evomodel.siteratemodel.GammaSiteRateModel;
33 import dr.evomodel.substmodel.FrequencyModel;
34 import dr.evomodel.substmodel.codon.MG94CodonModel;
35 import dr.evomodel.substmodel.SubstitutionModel;
36 import dr.evomodel.treelikelihood.BeagleTreeLikelihood;
37 import dr.evomodel.treelikelihood.PartialsRescalingScheme;
38 import dr.app.beagle.tools.BeagleSequenceSimulator;
39 import dr.app.beagle.tools.Partition;
40 import dr.evolution.alignment.Alignment;
41 import dr.evolution.alignment.ConvertAlignment;
42 import dr.evolution.datatype.Codons;
43 import dr.evolution.datatype.GeneticCode;
44 import dr.evolution.datatype.Nucleotides;
45 import dr.evolution.io.NewickImporter;
46 import dr.evolution.tree.NodeRef;
47 import dr.evomodel.branchratemodel.BranchRateModel;
48 import dr.evomodel.branchratemodel.CountableBranchCategoryProvider;
49 import dr.evomodel.branchratemodel.DefaultBranchRateModel;
50 import dr.evomodel.tree.TreeModel;
51 import dr.inference.model.AbstractModel;
52 import dr.inference.model.Model;
53 import dr.inference.model.Parameter;
54 import dr.inference.model.Variable;
55 import dr.inference.model.Variable.ChangeType;
56 import dr.math.MathUtils;
57 import dr.util.Author;
58 import dr.util.Citable;
59 import dr.util.Citation;
60
61 /**
62 * @author Filip Bielejec
63 * @author Guy Baele
64 * @author Marc A. Suchard
65 * @version $Id$
66 */
67 @SuppressWarnings("serial")
68 public class LineageSpecificBranchModel extends AbstractModel implements BranchModel, Citable {
69
70 public static final String LINEAGE_SPECIFIC_BRANCH_MODEL = "lineageSpecificBranchModel";
71
72 private static final boolean DEBUG = false;
73
74 private boolean setupMapping = true;
75
76 private Map<NodeRef, Mapping> nodeMap;
77 private List<SubstitutionModel> substitutionModels;
78 private TreeModel treeModel;
79 private FrequencyModel rootFrequencyModel;
80
81 // for discrete categories
82 private CountableBranchCategoryProvider categoriesProvider;
83 private Parameter categoriesParameter;
84
85 public LineageSpecificBranchModel(TreeModel treeModel, //
86 FrequencyModel rootFrequencyModel, //
87 final List<SubstitutionModel> substitutionModels, //
88 // CountableBranchCategoryProvider categoriesProvider, //
89 Parameter categoriesParameter //
90 ) {
91
92 super("");
93
94 this.treeModel = treeModel;
95 this.substitutionModels = substitutionModels;
96 // this.categoriesProvider = categoriesProvider;
97 this.categoriesParameter = categoriesParameter;
98 this.rootFrequencyModel = rootFrequencyModel;
99
100 this.categoriesProvider = new CountableBranchCategoryProvider.IndependentBranchCategoryModel(treeModel, categoriesParameter);
101
102 this.nodeMap = new HashMap<NodeRef, Mapping>();
103
104 for (SubstitutionModel model : this.substitutionModels) {
105 addModel(model);
106 }
107
108 addModel(this.treeModel);
109 addModel(this.rootFrequencyModel);
110 addModel((Model) this.categoriesProvider);
111 addVariable(this.categoriesParameter);
112
113 }// END: Constructor
114
115 @Override
116 public Mapping getBranchModelMapping(NodeRef branch) {
117
118 if (setupMapping) {
119 setupNodeMap(branch);
120 }
121
122 return nodeMap.get(branch);
123 }//END: getBranchModelMapping
124
125 private void setupNodeMap(NodeRef branch) {
126
127 if (branch != treeModel.getRoot()) {//TODO: neccessary?
128
129 int branchCategory = categoriesProvider.getBranchCategory(
130 treeModel, branch);
131 final int uCategory = (int) categoriesParameter.getParameterValue(branchCategory);
132
133 if (DEBUG) {
134 System.out.println("branch length: " + treeModel.getBranchLength(branch) + ", " + "category:" + uCategory);
135 }// END: DEBUG
136
137 nodeMap.put(branch, new Mapping() {
138
139 @Override
140 public int[] getOrder() {
141 return new int[] { uCategory };
142 }// END: getOrder
143
144 @Override
145 public double[] getWeights() {
146 return new double[] { 1.0 };
147 }// END: getWeights
148
149 });
150
151 }//END: root check
152
153 }// END: setupNodeMap
154
155 @Override
156 public List<SubstitutionModel> getSubstitutionModels() {
157 return substitutionModels;
158 }
159
160 @Override
161 public SubstitutionModel getRootSubstitutionModel() {
162 throw new RuntimeException("This method should never be called!");
163 }
164
165 @Override
166 public FrequencyModel getRootFrequencyModel() {
167 return rootFrequencyModel;
168 }
169
170 @Override
171 public boolean requiresMatrixConvolution() {
172 return false;
173 }
174
175 @Override
176 protected void handleModelChangedEvent(Model model, Object object, int index) {
177
178 fireModelChanged();
179
180 }
181
182 @Override
183 protected void handleVariableChangedEvent(Variable variable, int index,
184 ChangeType type) {
185 fireModelChanged();
186 }
187
188 @Override
189 protected void storeState() {
190 //
191 }// END: storeState
192
193 @Override
194 protected void restoreState() {
195 setupMapping = true;
196 }// END: restoreState
197
198 @Override
199 protected void acceptState() {
200 //
201 }// END: acceptState
202
203 public static void main(String[] args) {
204
205 try {
206
207 // the seed of the BEAST
208 MathUtils.setSeed(666);
209
210 // create tree
211 NewickImporter importer = new NewickImporter(
212 "(SimSeq1:73.7468,(SimSeq2:25.256989999999995,SimSeq3:45.256989999999995):18.48981);");
213 TreeModel tree = new TreeModel(importer.importTree(null));
214
215 // create site model
216 GammaSiteRateModel siteRateModel = new GammaSiteRateModel(
217 "siteModel");
218
219 // create branch rate model
220 BranchRateModel branchRateModel = new DefaultBranchRateModel();
221
222 int sequenceLength = 10;
223 ArrayList<Partition> partitionsList = new ArrayList<Partition>();
224
225 // create Frequency Model
226 Parameter freqs = new Parameter.Default(new double[]{
227 0.0163936, //
228 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, //
229 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, //
230 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, //
231 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, //
232 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344, 0.01639344 //
233 });
234 FrequencyModel freqModel = new FrequencyModel(Codons.UNIVERSAL,
235 freqs);
236
237 // create substitution model
238 Parameter alpha = new Parameter.Default(1, 10);
239 Parameter beta = new Parameter.Default(1, 5);
240 MG94CodonModel mg94 = new MG94CodonModel(Codons.UNIVERSAL, alpha, beta, freqModel);
241
242 HomogeneousBranchModel substitutionModel = new HomogeneousBranchModel(mg94);
243
244 // create partition
245 Partition partition1 = new Partition(tree, //
246 substitutionModel,//
247 siteRateModel, //
248 branchRateModel, //
249 freqModel, //
250 0, // from
251 sequenceLength - 1, // to
252 1 // every
253 );
254
255 partitionsList.add(partition1);
256
257 // feed to sequence simulator and generate data
258 BeagleSequenceSimulator simulator = new BeagleSequenceSimulator(
259 partitionsList);
260
261 Alignment alignment = simulator.simulate(false, false);
262
263 ConvertAlignment convert = new ConvertAlignment(Nucleotides.INSTANCE,
264 GeneticCode.UNIVERSAL, alignment);
265
266
267 List<SubstitutionModel> substModels = new ArrayList<SubstitutionModel>();
268 for (int i = 0; i < 2; i++) {
269 // alpha = new Parameter.Default(1, 10 );
270 // beta = new Parameter.Default(1, 5 );
271 // mg94 = new MG94CodonModel(Codons.UNIVERSAL, alpha, beta,
272 // freqModel);
273 substModels.add(mg94);
274 }
275
276 Parameter uCategories = new Parameter.Default(2, 0);
277 // CountableBranchCategoryProvider provider = new CountableBranchCategoryProvider.IndependentBranchCategoryModel(tree, uCategories);
278
279 LineageSpecificBranchModel branchSpecific = new LineageSpecificBranchModel(tree, freqModel, substModels, //provider,
280 uCategories);
281
282 BeagleTreeLikelihood like = new BeagleTreeLikelihood(convert, //
283 tree, //
284 branchSpecific, //
285 siteRateModel, //
286 branchRateModel, //
287 null, //
288 false, //
289 PartialsRescalingScheme.DEFAULT, true);
290
291 BeagleTreeLikelihood gold = new BeagleTreeLikelihood(convert, //
292 tree, //
293 substitutionModel, //
294 siteRateModel, //
295 branchRateModel, //
296 null, //
297 false, //
298 PartialsRescalingScheme.DEFAULT, true);
299
300 System.out.println("likelihood (gold) = " + gold.getLogLikelihood());
301 System.out.println("likelihood = " + like.getLogLikelihood());
302
303 } catch (Exception e) {
304 e.printStackTrace();
305 }
306
307 }// END: main
308
309 @Override
310 public Citation.Category getCategory() {
311 return Citation.Category.MOLECULAR_CLOCK;
312 }
313
314 @Override
315 public String getDescription() {
316 return "Lineage Specific Branch model";
317 }
318
319 public List<Citation> getCitations() {
320 return Collections.singletonList(
321 new Citation(new Author[] { new Author("F", "Bielejec"),
322 new Author("P", "Lemey"), new Author("G", "Baele"), new Author("A", "Rambaut"),
323 new Author("MA", "Suchard") }, Citation.Status.IN_PREPARATION));
324 }// END: getCitations
325
326 }// END: class