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