2 * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3 * Copyright (C) 2009 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.analysis;
23 import jalview.datamodel.*;
24 import jalview.math.*;
27 * Performs Principal Component Analysis on given sequences
32 public class PCA implements Runnable
44 StringBuffer details = new StringBuffer();
47 * Creates a new PCA object.
50 * Set of sequences to perform PCA on
52 public PCA(String[] s)
55 BinarySequence[] bs = new BinarySequence[s.length];
58 while ((ii < s.length) && (s[ii] != null))
60 bs[ii] = new BinarySequence(s[ii]);
65 BinarySequence[] bs2 = new BinarySequence[s.length];
68 while ((ii < s.length) && (s[ii] != null))
70 bs2[ii] = new BinarySequence(s[ii]);
71 bs2[ii].blosumEncode();
75 // System.out.println("Created binary encoding");
79 while ((count < bs.length) && (bs[count] != null))
84 double[][] seqmat = new double[count][bs[0].getDBinary().length];
85 double[][] seqmat2 = new double[count][bs2[0].getDBinary().length];
90 seqmat[i] = bs[i].getDBinary();
91 seqmat2[i] = bs2[i].getDBinary();
95 // System.out.println("Created array");
97 // System.out.println(" --- Original matrix ---- ");
98 m = new Matrix(seqmat, count, bs[0].getDBinary().length);
99 m2 = new Matrix(seqmat2, count, bs2[0].getDBinary().length);
104 * Returns the matrix used in PCA calculation
106 * @return java.math.Matrix object
118 * Index of diagonal within matrix
120 * @return Returns value of diagonal from matrix
122 public double getEigenvalue(int i)
124 return eigenvector.d[i];
139 * @return DOCUMENT ME!
141 public float[][] getComponents(int l, int n, int mm, float factor)
143 float[][] out = new float[m.rows][3];
145 for (int i = 0; i < m.rows; i++)
147 out[i][0] = (float) component(i, l) * factor;
148 out[i][1] = (float) component(i, n) * factor;
149 out[i][2] = (float) component(i, mm) * factor;
161 * @return DOCUMENT ME!
163 public double[] component(int n)
165 // n = index of eigenvector
166 double[] out = new double[m.rows];
168 for (int i = 0; i < m.rows; i++)
170 out[i] = component(i, n);
184 * @return DOCUMENT ME!
186 double component(int row, int n)
190 for (int i = 0; i < symm.cols; i++)
192 out += (symm.value[row][i] * eigenvector.value[i][n]);
195 return out / eigenvector.d[n];
198 public String getDetails()
200 return details.toString();
208 Matrix mt = m.transpose();
210 details.append(" --- OrigT * Orig ---- \n");
211 eigenvector = mt.preMultiply(m2);
213 PrintStream ps = new PrintStream(System.out)
215 public void print(String x)
220 public void println()
222 details.append("\n");
226 eigenvector.print(ps);
228 symm = eigenvector.copy();
232 details.append(" ---Tridiag transform matrix ---\n");
233 details.append(" --- D vector ---\n");
234 eigenvector.printD(ps);
236 details.append("--- E vector ---\n");
237 eigenvector.printE(ps);
240 // Now produce the diagonalization matrix
243 details.append(" --- New diagonalization matrix ---\n");
244 details.append(" --- Eigenvalues ---\n");
245 eigenvector.printD(ps);
248 // taps.println("Transformed sequences = ");
249 // Matrix trans = m.preMultiply(eigenvector);
250 // trans.print(System.out);