2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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;
21 import jalview.datamodel.*;
23 import jalview.math.*;
28 * Performs Principal Component Analysis on given sequences
33 public class PCA implements Runnable
40 StringBuffer details = new StringBuffer();
44 * Creates a new PCA object.
46 * @param s Set of sequences to perform PCA on
48 public PCA(String[] s)
51 BinarySequence[] bs = new BinarySequence[s.length];
54 while ((ii < s.length) && (s[ii] != null))
56 bs[ii] = new BinarySequence(s[ii]);
61 BinarySequence[] bs2 = new BinarySequence[s.length];
64 while ((ii < s.length) && (s[ii] != null))
66 bs2[ii] = new BinarySequence(s[ii]);
67 bs2[ii].blosumEncode();
71 //System.out.println("Created binary encoding");
75 while ((count < bs.length) && (bs[count] != null))
80 double[][] seqmat = new double[count][bs[0].getDBinary().length];
81 double[][] seqmat2 = new double[count][bs2[0].getDBinary().length];
86 seqmat[i] = bs[i].getDBinary();
87 seqmat2[i] = bs2[i].getDBinary();
91 //System.out.println("Created array");
93 // System.out.println(" --- Original matrix ---- ");
94 m = new Matrix(seqmat, count, bs[0].getDBinary().length);
95 m2 = new Matrix(seqmat2, count, bs2[0].getDBinary().length);
100 * Returns the matrix used in PCA calculation
102 * @return java.math.Matrix object
113 * @param i Index of diagonal within matrix
115 * @return Returns value of diagonal from matrix
117 public double getEigenvalue(int i)
119 return eigenvector.d[i];
125 * @param l DOCUMENT ME!
126 * @param n DOCUMENT ME!
127 * @param mm DOCUMENT ME!
128 * @param factor DOCUMENT ME!
130 * @return DOCUMENT ME!
132 public float[][] getComponents(int l, int n, int mm, float factor)
134 float[][] out = new float[m.rows][3];
136 for (int i = 0; i < m.rows; i++)
138 out[i][0] = (float) component(i, l) * factor;
139 out[i][1] = (float) component(i, n) * factor;
140 out[i][2] = (float) component(i, mm) * factor;
149 * @param n DOCUMENT ME!
151 * @return DOCUMENT ME!
153 public double[] component(int n)
155 // n = index of eigenvector
156 double[] out = new double[m.rows];
158 for (int i = 0; i < m.rows; i++)
160 out[i] = component(i, n);
169 * @param row DOCUMENT ME!
170 * @param n DOCUMENT ME!
172 * @return DOCUMENT ME!
174 double component(int row, int n)
178 for (int i = 0; i < symm.cols; i++)
180 out += (symm.value[row][i] * eigenvector.value[i][n]);
183 return out / eigenvector.d[n];
186 public String getDetails()
188 return details.toString();
197 Matrix mt = m.transpose();
199 details.append(" --- OrigT * Orig ---- \n");
200 eigenvector = mt.preMultiply(m2);
202 PrintStream ps = new PrintStream(System.out)
204 public void print(String x) {
207 public void println()
209 details.append("\n");
214 eigenvector.print( ps );
216 symm = eigenvector.copy();
220 details.append(" ---Tridiag transform matrix ---\n");
221 details.append(" --- D vector ---\n");
222 eigenvector.printD(ps);
224 details.append("--- E vector ---\n");
225 eigenvector.printE(ps);
228 // Now produce the diagonalization matrix
232 details.append(" --- New diagonalization matrix ---\n");
233 details.append(" --- Eigenvalues ---\n");
234 eigenvector.printD(ps);
237 // taps.println("Transformed sequences = ");
238 // Matrix trans = m.preMultiply(eigenvector);
239 // trans.print(System.out);