Debian Codegraph @

src/dr/evomodel/substmodel/DependentProductChainSubstitutionModel.java

raw symbols SCIP
1 /*
2 * DependentProductChainSubstitutionModel.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.substmodel;
27
28 import dr.evomodel.siteratemodel.SiteRateModel;
29 import dr.inference.model.Parameter;
30 import dr.math.KroneckerOperation;
31
32 import java.util.List;
33
34 /**
35 * A derived class from ProductChainSubstitutionModel that introduces dependence between sites.
36 *
37 * @author Marc A. Suchard
38 * @author Yu-Nong Gong
39 * @version 08/01/2010
40 */
41 public class DependentProductChainSubstitutionModel extends ProductChainSubstitutionModel {
42
43 public DependentProductChainSubstitutionModel(String name, List<SubstitutionModel> baseModels,
44 Parameter dependenceParameter) {
45 this(name, baseModels, null, dependenceParameter);
46 }
47
48 public DependentProductChainSubstitutionModel(String name, List<SubstitutionModel> baseModels,
49 List<SiteRateModel> rateModels, Parameter dependenceParameter) {
50 super(name, baseModels, rateModels);
51 this.dependenceParameter = dependenceParameter;
52
53 eigenSystemCES = new ColtEigenSystem(stateCount);
54
55 }
56
57 public EigenDecomposition getEigenDecomposition() {
58 synchronized (this) {
59 if (updateMatrix) {
60 computeKroneckerSumsAndProducts();
61 }
62 }
63 return eigenDecomposition;
64 }
65
66 public void getInfinitesimalMatrix(double[] out) {
67 getEigenDecomposition(); // Updates rate matrix if necessary
68 System.arraycopy(rateMatrix, 0, out, 0, stateCount * stateCount);
69 }
70
71 private void computeKroneckerSumsAndProducts() {
72
73 int currentStateSize = stateSizes[0];
74 double[] currentRate = new double[currentStateSize * currentStateSize];
75 baseModels.get(0).getInfinitesimalMatrix(currentRate);
76 currentRate = scaleForProductChain(currentRate, 0);
77
78 for (int i = 1; i < numBaseModel; i++) {
79 SubstitutionModel nextModel = baseModels.get(i);
80 int nextStateSize = stateSizes[i];
81 double[] nextRate = new double[nextStateSize * nextStateSize];
82 nextModel.getInfinitesimalMatrix(nextRate);
83 nextRate = scaleForProductChain(nextRate, i);
84 currentRate = KroneckerOperation.sum(currentRate, currentStateSize, nextRate, nextStateSize);
85 currentStateSize *= nextStateSize;
86 }
87
88 double dependence = dependenceParameter.getParameterValue(0);
89
90 /* put dependence parameter into anti-diagonal elements*/
91 for (int j = 0; j < stateCount; j++) {
92 currentRate[(j + 1) * stateCount - (j + 1)] = dependence;
93 }
94
95 /* put dependence parameter into co-varying sites */
96
97 /* take nucleotide for example, A, T, C, and G as 0, 1, 2, and 3 */
98 String[] letA = new String[(int) Math.sqrt(stateCount)];
99 String[] letB = new String[(int) Math.sqrt(stateCount)];
100 String[] letAB = new String[stateCount];
101 int count = 0;
102
103 for (int letI = 0; letI < letA.length; letI++) {
104 letA[letI] = String.valueOf(count);
105 letB[letI] = String.valueOf(count);
106 count++;
107 }
108
109 /* generate two-state array {(00), (01), (02), (03), (10), ..., (33)} */
110 int letABi = 0;
111 for (int letAi = 0; letAi < letA.length; letAi++) {
112 for (int letBi = 0; letBi < letB.length; letBi++) {
113 letAB[letABi] = letA[letAi] + " " + letB[letBi];
114 letABi++;
115 }
116 }
117
118 /* assign dependence parameter to co-varying sites */
119 for (int j = 0; j < letAB.length; j++) {
120 for (int k = 0; k < letAB.length; k++) {
121 String[] Spl1 = letAB[j].split(" ");
122 String[] Spl2 = letAB[k].split(" ");
123
124 if (!Spl1[0].equals(Spl2[0]) && !Spl1[1].equals(Spl2[1])) {
125 /* Is co-varying site */
126 currentRate[j * stateCount + k] = dependence;
127 }
128
129 }
130 }
131
132 /* Rescale diagonal elements in currentRate */
133 double diagonalSum = 0;
134 for (int k = 0; k < stateCount; k++) {
135 for (int l = k * stateCount; l < ((k + 1) * stateCount); l++) {
136 if (l != (k * stateCount + k)) {
137 diagonalSum += currentRate[l];
138 }
139 }
140 /* assign value to diagonal element */
141 currentRate[k * stateCount + k] = -(diagonalSum);
142 diagonalSum = 0;
143 }
144
145 rateMatrix = currentRate;
146
147 // Call standard eigendecomposition on currentRate
148 decompose();
149
150 }
151
152 private void decompose() {
153
154 double[][] q = new double[stateCount][stateCount];
155 for (int i = 0; i < stateCount; i++) {
156 System.arraycopy(rateMatrix, i * stateCount, q[i], 0, stateCount);
157 }
158
159 eigenDecomposition = eigenSystemCES.decomposeMatrix(q);
160 //System.err.println("eigenSystem null? " + (eigenSystem == null ? "yes" : "no"));
161 //eigenDecomposition = eigenSystem.decomposeMatrix(q);
162
163 updateMatrix = false;
164 }
165
166 private final Parameter dependenceParameter;
167
168 private final EigenSystem eigenSystemCES; //= new ColtEigenSystem();
169
170 }