2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 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
23 import jalview.datamodel.*;
\r
24 import jalview.math.*;
\r
27 * Performs Principal Component Analysis on given sequences
\r
30 * @version $Revision$
\r
38 double[] eigenvalue;
\r
40 StringBuffer details = new StringBuffer();
\r
43 * Creates a new PCA object.
\r
45 * @param s Set of sequences to perform PCA on
\r
47 public PCA(String[] s)
\r
50 BinarySequence[] bs = new BinarySequence[s.length];
\r
53 while ( (ii < s.length) && (s[ii] != null))
\r
55 bs[ii] = new BinarySequence(s[ii]);
\r
60 BinarySequence[] bs2 = new BinarySequence[s.length];
\r
63 while ( (ii < s.length) && (s[ii] != null))
\r
65 bs2[ii] = new BinarySequence(s[ii]);
\r
66 bs2[ii].blosumEncode();
\r
70 //System.out.println("Created binary encoding");
\r
74 while ( (count < bs.length) && (bs[count] != null))
\r
79 double[][] seqmat = new double[count][bs[0].getDBinary().length];
\r
80 double[][] seqmat2 = new double[count][bs2[0].getDBinary().length];
\r
85 seqmat[i] = bs[i].getDBinary();
\r
86 seqmat2[i] = bs2[i].getDBinary();
\r
90 //System.out.println("Created array");
\r
92 // System.out.println(" --- Original matrix ---- ");
\r
93 m = new Matrix(seqmat, count, bs[0].getDBinary().length);
\r
94 m2 = new Matrix(seqmat2, count, bs2[0].getDBinary().length);
\r
99 * Returns the matrix used in PCA calculation
\r
101 * @return java.math.Matrix object
\r
104 public Matrix getM()
\r
110 * Returns Eigenvalue
\r
112 * @param i Index of diagonal within matrix
\r
114 * @return Returns value of diagonal from matrix
\r
116 public double getEigenvalue(int i)
\r
118 return eigenvector.d[i];
\r
124 * @param l DOCUMENT ME!
\r
125 * @param n DOCUMENT ME!
\r
126 * @param mm DOCUMENT ME!
\r
127 * @param factor DOCUMENT ME!
\r
129 * @return DOCUMENT ME!
\r
131 public float[][] getComponents(int l, int n, int mm, float factor)
\r
133 float[][] out = new float[m.rows][3];
\r
135 for (int i = 0; i < m.rows; i++)
\r
137 out[i][0] = (float) component(i, l) * factor;
\r
138 out[i][1] = (float) component(i, n) * factor;
\r
139 out[i][2] = (float) component(i, mm) * factor;
\r
148 * @param n DOCUMENT ME!
\r
150 * @return DOCUMENT ME!
\r
152 public double[] component(int n)
\r
154 // n = index of eigenvector
\r
155 double[] out = new double[m.rows];
\r
157 for (int i = 0; i < m.rows; i++)
\r
159 out[i] = component(i, n);
\r
168 * @param row DOCUMENT ME!
\r
169 * @param n DOCUMENT ME!
\r
171 * @return DOCUMENT ME!
\r
173 double component(int row, int n)
\r
177 for (int i = 0; i < symm.cols; i++)
\r
179 out += (symm.value[row][i] * eigenvector.value[i][n]);
\r
182 return out / eigenvector.d[n];
\r
185 public String getDetails()
\r
187 return details.toString();
\r
195 Matrix mt = m.transpose();
\r
197 details.append(" --- OrigT * Orig ---- \n");
\r
198 eigenvector = mt.preMultiply(m2);
\r
200 PrintStream ps = new PrintStream(System.out)
\r
202 public void print(String x)
\r
207 public void println()
\r
209 details.append("\n");
\r
213 eigenvector.print(ps);
\r
215 symm = eigenvector.copy();
\r
217 eigenvector.tred();
\r
219 details.append(" ---Tridiag transform matrix ---\n");
\r
220 details.append(" --- D vector ---\n");
\r
221 eigenvector.printD(ps);
\r
223 details.append("--- E vector ---\n");
\r
224 eigenvector.printE(ps);
\r
227 // Now produce the diagonalization matrix
\r
228 eigenvector.tqli();
\r
230 details.append(" --- New diagonalization matrix ---\n");
\r
231 details.append(" --- Eigenvalues ---\n");
\r
232 eigenvector.printD(ps);
\r
235 // taps.println("Transformed sequences = ");
\r
236 // Matrix trans = m.preMultiply(eigenvector);
\r
237 // trans.print(System.out);
\r