2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.analysis;
\r
21 import jalview.datamodel.*;
\r
23 import jalview.math.*;
\r
28 * Performs Principal Component Analysis on given sequences
\r
31 * @version $Revision$
\r
33 public class PCA implements Runnable
\r
38 double[] eigenvalue;
\r
40 StringBuffer details = new StringBuffer();
\r
44 * Creates a new PCA object.
\r
46 * @param s Set of sequences to perform PCA on
\r
48 public PCA(String[] s)
\r
51 BinarySequence[] bs = new BinarySequence[s.length];
\r
54 while ((ii < s.length) && (s[ii] != null))
\r
56 bs[ii] = new BinarySequence(s[ii]);
\r
61 BinarySequence[] bs2 = new BinarySequence[s.length];
\r
64 while ((ii < s.length) && (s[ii] != null))
\r
66 bs2[ii] = new BinarySequence(s[ii]);
\r
67 bs2[ii].blosumEncode();
\r
71 //System.out.println("Created binary encoding");
\r
75 while ((count < bs.length) && (bs[count] != null))
\r
80 double[][] seqmat = new double[count][bs[0].getDBinary().length];
\r
81 double[][] seqmat2 = new double[count][bs2[0].getDBinary().length];
\r
86 seqmat[i] = bs[i].getDBinary();
\r
87 seqmat2[i] = bs2[i].getDBinary();
\r
91 //System.out.println("Created array");
\r
93 // System.out.println(" --- Original matrix ---- ");
\r
94 m = new Matrix(seqmat, count, bs[0].getDBinary().length);
\r
95 m2 = new Matrix(seqmat2, count, bs2[0].getDBinary().length);
\r
100 * Returns the matrix used in PCA calculation
\r
102 * @return java.math.Matrix object
\r
105 public Matrix getM()
\r
111 * Returns Eigenvalue
\r
113 * @param i Index of diagonal within matrix
\r
115 * @return Returns value of diagonal from matrix
\r
117 public double getEigenvalue(int i)
\r
119 return eigenvector.d[i];
\r
125 * @param l DOCUMENT ME!
\r
126 * @param n DOCUMENT ME!
\r
127 * @param mm DOCUMENT ME!
\r
128 * @param factor DOCUMENT ME!
\r
130 * @return DOCUMENT ME!
\r
132 public float[][] getComponents(int l, int n, int mm, float factor)
\r
134 float[][] out = new float[m.rows][3];
\r
136 for (int i = 0; i < m.rows; i++)
\r
138 out[i][0] = (float) component(i, l) * factor;
\r
139 out[i][1] = (float) component(i, n) * factor;
\r
140 out[i][2] = (float) component(i, mm) * factor;
\r
149 * @param n DOCUMENT ME!
\r
151 * @return DOCUMENT ME!
\r
153 public double[] component(int n)
\r
155 // n = index of eigenvector
\r
156 double[] out = new double[m.rows];
\r
158 for (int i = 0; i < m.rows; i++)
\r
160 out[i] = component(i, n);
\r
169 * @param row DOCUMENT ME!
\r
170 * @param n DOCUMENT ME!
\r
172 * @return DOCUMENT ME!
\r
174 double component(int row, int n)
\r
178 for (int i = 0; i < symm.cols; i++)
\r
180 out += (symm.value[row][i] * eigenvector.value[i][n]);
\r
183 return out / eigenvector.d[n];
\r
186 public String getDetails()
\r
188 return details.toString();
\r
197 Matrix mt = m.transpose();
\r
199 details.append(" --- OrigT * Orig ---- \n");
\r
200 eigenvector = mt.preMultiply(m2);
\r
202 PrintStream ps = new PrintStream(System.out)
\r
204 public void print(String x) {
\r
207 public void println()
\r
209 details.append("\n");
\r
214 eigenvector.print( ps );
\r
216 symm = eigenvector.copy();
\r
218 eigenvector.tred();
\r
220 details.append(" ---Tridiag transform matrix ---\n");
\r
221 details.append(" --- D vector ---\n");
\r
222 eigenvector.printD(ps);
\r
224 details.append("--- E vector ---\n");
\r
225 eigenvector.printE(ps);
\r
228 // Now produce the diagonalization matrix
\r
229 eigenvector.tqli();
\r
232 details.append(" --- New diagonalization matrix ---\n");
\r
233 details.append(" --- Eigenvalues ---\n");
\r
234 eigenvector.printD(ps);
\r
237 // taps.println("Transformed sequences = ");
\r
238 // Matrix trans = m.preMultiply(eigenvector);
\r
239 // trans.print(System.out);
\r