Debian Codegraph @

src/dr/evomodel/substmodel/codon/PCACodonModel.java

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