Debian Codegraph @

src/dr/evomodel/treedatalikelihood/HomogenousSubstitutionModelDelegate.java

raw symbols SCIP
1 /*
2 * SubstitutionModelDelegate.java
3 *
4 * Copyright (c) 2002-2016 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.treedatalikelihood;
27
28 import beagle.Beagle;
29 import dr.evolution.tree.Tree;
30 import dr.evomodel.branchmodel.BranchModel;
31 import dr.evomodel.substmodel.EigenDecomposition;
32 import dr.evomodel.substmodel.SubstitutionModel;
33 import dr.util.Timer;
34
35 import java.io.Serializable;
36 import java.util.ArrayDeque;
37 import java.util.ArrayList;
38 import java.util.Deque;
39 import java.util.List;
40
41 /**
42 * A simple substitution model delegate with the same substitution model over the whole tree
43 * @author Andrew Rambaut
44 * @version $Id$
45 */
46 public final class HomogenousSubstitutionModelDelegate implements EvolutionaryProcessDelegate, Serializable {
47
48 private final SubstitutionModel substitutionModel;
49
50 private final int eigenCount = 1;
51 private final int nodeCount;
52
53 private final BufferIndexHelper eigenBufferHelper;
54 private final BufferIndexHelper matrixBufferHelper;
55
56 /**
57 * A class which handles substitution models including epoch models where multiple
58 * substitution models on a branch are convolved.
59 * @param tree
60 * @param branchModel Describes which substitution models use on each branch
61 */
62 public HomogenousSubstitutionModelDelegate(Tree tree, BranchModel branchModel) {
63 this(tree, branchModel, 0);
64 }
65
66 /**
67 * A class which handles substitution models including epoch models where multiple
68 * substitution models on a branch are convolved.
69 * @param tree
70 * @param branchModel Describes which substitution models use on each branch
71 * @param partitionNumber which data partition is this (used to offset eigen and matrix buffer numbers)
72 */
73 public HomogenousSubstitutionModelDelegate(Tree tree, BranchModel branchModel, int partitionNumber) {
74
75 assert(branchModel.getSubstitutionModels().size() == 1) : "this delegate should only be used with simple branch models";
76
77 this.substitutionModel = branchModel.getRootSubstitutionModel();
78
79
80 nodeCount = tree.getNodeCount();
81
82 // two eigen buffers for each decomposition for store and restore.
83 eigenBufferHelper = new BufferIndexHelper(eigenCount, 0, partitionNumber);
84
85 // two matrices for each node less the root
86 matrixBufferHelper = new BufferIndexHelper(nodeCount, 0, partitionNumber);
87
88 }// END: Constructor
89
90 @Override
91 public boolean canReturnComplexDiagonalization() {
92 return substitutionModel.canReturnComplexDiagonalization();
93 }
94
95 @Override
96 public int getEigenBufferCount() {
97 return eigenBufferHelper.getBufferCount();
98 }
99
100 @Override
101 public int getMatrixBufferCount() {
102 return matrixBufferHelper.getBufferCount();
103 }
104
105 @Override
106 public int getSubstitutionModelCount() {
107 return 1;
108 }
109
110 @Override
111 public SubstitutionModel getSubstitutionModel(int index) {
112 assert(index == 0);
113 return substitutionModel;
114 }
115
116 @Override
117 public int getEigenIndex(int bufferIndex) {
118 return eigenBufferHelper.getOffsetIndex(bufferIndex);
119 }
120
121 @Override
122 public int getMatrixIndex(int branchIndex) {
123 return matrixBufferHelper.getOffsetIndex(branchIndex);
124 }
125
126 @Override
127 public double[] getRootStateFrequencies() {
128 return substitutionModel.getFrequencyModel().getFrequencies();
129 }
130
131 @Override
132 public void updateSubstitutionModels(Beagle beagle, boolean flip) {
133 if (flip) {
134 eigenBufferHelper.flipOffset(0);
135 }
136 EigenDecomposition ed = substitutionModel.getEigenDecomposition();
137
138 beagle.setEigenDecomposition(
139 eigenBufferHelper.getOffsetIndex(0),
140 ed.getEigenVectors(),
141 ed.getInverseEigenVectors(),
142 ed.getEigenValues());
143 }
144
145 @Override
146 public void updateTransitionMatrices(Beagle beagle, int[] branchIndices, double[] edgeLengths, int updateCount, boolean flip) {
147
148 int[] probabilityIndices = new int[updateCount];
149
150 for (int i = 0; i < updateCount; i++) {
151 if (flip) {
152 matrixBufferHelper.flipOffset(branchIndices[i]);
153 }
154 probabilityIndices[i] = matrixBufferHelper.getOffsetIndex(branchIndices[i]);
155 }// END: i loop
156
157 beagle.updateTransitionMatrices(eigenBufferHelper.getOffsetIndex(0),
158 probabilityIndices,
159 null, // firstDerivativeIndices
160 null, // secondDerivativeIndices
161 edgeLengths,
162 updateCount);
163
164 }
165
166 @Override
167 public void flipTransitionMatrices(int[] branchIndices, int updateCount) {
168 for (int i = 0; i < updateCount; i++) {
169 matrixBufferHelper.flipOffset(branchIndices[i]);
170 }
171 }
172
173
174 @Override
175 public void storeState() {
176 eigenBufferHelper.storeState();
177 matrixBufferHelper.storeState();
178 }
179
180 @Override
181 public void restoreState() {
182 eigenBufferHelper.restoreState();
183 matrixBufferHelper.restoreState();
184 }
185
186 }// END: class