Debian Codegraph @

src/dr/oldevomodel/substmodel/PCACodonModel.java

raw symbols SCIP
1 /*
2 * PCACodonModel.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.oldevomodel.substmodel;
27
28 import dr.evolution.datatype.Codons;
29 import dr.oldevomodelxml.substmodel.PCACodonModelParser;
30 import dr.inference.model.Parameter;
31
32 /**
33 * PCA model of codon evolution
34 *
35 * @author Stefan Zoller
36 */
37 public class PCACodonModel extends AbstractCodonModel {
38 // principal components, means, scale factors
39 protected AbstractPCARateMatrix rateMatrix;
40
41 protected byte[] rateMap;
42
43 private Parameter pcFactors;
44
45 /**
46 * constructor
47 *
48 * @param codonDataType Data type as Codons.UNIVERSAL
49 * @param pcaType Rate matrix with PCs, means, scale factors
50 * @param pcaDimensionParameter Scalars for PCs
51 * @param freqModel Frequency model
52 */
53 public PCACodonModel(Codons codonDataType,
54 AbstractPCARateMatrix pcaType,
55 Parameter pcaDimensionParameter,
56 FrequencyModel freqModel)
57 {
58 super(PCACodonModelParser.PCA_CODON_MODEL, codonDataType, freqModel);
59
60 this.rateMatrix = pcaType;
61
62 // initialize scalars for principal components
63 this.pcFactors = pcaDimensionParameter;
64 double[] startFacs = pcaType.getStartFacs();
65 double facSum = 0.0;
66 for(int i=0; i<pcFactors.getDimension(); i++) {
67 facSum += startFacs[i];
68 }
69 for(int i=0; i<pcFactors.getDimension(); i++) {
70 pcFactors.setParameterValueQuietly(i, startFacs[i]/facSum);
71 }
72 addVariable(pcFactors);
73 pcFactors.addBounds(new Parameter.DefaultBounds(Double.POSITIVE_INFINITY, 0.0,
74 pcFactors.getDimension()));
75 }
76
77
78 // setup substitution matrix
79 public void setupRelativeRates() {
80 double[] m = rateMatrix.getMeans();
81 double[] sc = rateMatrix.getScales();
82 for (int i = 0; i < rateCount; i++) {
83 relativeRates[i] = m[i];
84 }
85 for(int j = 0; j < pcFactors.getDimension(); j++) {
86 double[] pc = rateMatrix.getPCAt(j);
87 double f = pcFactors.getParameterValue(j);
88 for (int i = 0; i < rateCount; i++) {
89 relativeRates[i] += f*pc[i]*sc[i];
90 }
91 }
92 for (int i = 0; i < rateCount; i++) {
93 if(relativeRates[i]<0) {
94 relativeRates[i] = 0.0;
95 }
96 }
97 }
98
99 protected void ratesChanged() {
100 }
101
102 protected void frequenciesChanged() {
103 }
104
105 /**
106 * Getter and setter for Parameter pcFactor
107 */
108
109 public double getPcFactor(int dim) {
110 return pcFactors.getParameterValue(dim);
111 }
112
113 public double[] getPcFactor() {
114 return pcFactors.getParameterValues();
115 }
116
117 public void setPcFactor(int dim, double fac) {
118 pcFactors.setParameterValue(dim, fac);
119 updateMatrix = true;
120 }
121
122 public void setPcFactor(double[] fac) {
123 for(int i=0; i<pcFactors.getDimension(); i++) {
124 pcFactors.setParameterValue(i, fac[i]);
125 }
126 updateMatrix = true;
127 }
128
129 // **************************************************************
130 // XHTMLable IMPLEMENTATION
131 // **************************************************************
132
133 public String toXHTML() {
134 StringBuffer buffer = new StringBuffer();
135
136 buffer.append("<em>PCA Codon Model</em>");
137
138 return buffer.toString();
139 }
140
141
142 static String format1 = "%2.1e";
143 static String format2 = "%2.4e";
144
145 public String printQ() {
146 double[][] myQ = getQ();
147 if (myQ != null) {
148 StringBuffer sb = new StringBuffer();
149 for (int i = 0; i < myQ.length; i++) {
150 for(int j = 0; j < myQ[0].length; j++) {
151 sb.append(String.format(format1, myQ[i][j]));
152 sb.append("\t");
153 }
154 sb.append("\n");
155 }
156 return sb.toString();
157 } else {
158 return "No Q ready.";
159 }
160 }
161
162 public String printRelRates() {
163 StringBuffer sb = new StringBuffer();
164 for (int i = 0; i < relativeRates.length; i++) {
165 sb.append(String.format(format2, relativeRates[i]));
166 sb.append("\t");
167 }
168 sb.append("\n");
169 return sb.toString();
170 }
171 }