2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.analysis;
22 import jalview.datamodel.*;
23 import jalview.math.*;
26 * Performs Principal Component Analysis on given sequences
31 public class PCA implements Runnable
43 StringBuffer details = new StringBuffer();
46 * Creates a new PCA object.
49 * Set of sequences to perform PCA on
51 public PCA(String[] s)
54 BinarySequence[] bs = new BinarySequence[s.length];
57 while ((ii < s.length) && (s[ii] != null))
59 bs[ii] = new BinarySequence(s[ii]);
64 BinarySequence[] bs2 = new BinarySequence[s.length];
67 while ((ii < s.length) && (s[ii] != null))
69 bs2[ii] = new BinarySequence(s[ii]);
70 bs2[ii].blosumEncode();
74 // System.out.println("Created binary encoding");
78 while ((count < bs.length) && (bs[count] != null))
83 double[][] seqmat = new double[count][bs[0].getDBinary().length];
84 double[][] seqmat2 = new double[count][bs2[0].getDBinary().length];
89 seqmat[i] = bs[i].getDBinary();
90 seqmat2[i] = bs2[i].getDBinary();
94 // System.out.println("Created array");
96 // System.out.println(" --- Original matrix ---- ");
97 m = new Matrix(seqmat, count, bs[0].getDBinary().length);
98 m2 = new Matrix(seqmat2, count, bs2[0].getDBinary().length);
103 * Returns the matrix used in PCA calculation
105 * @return java.math.Matrix object
117 * Index of diagonal within matrix
119 * @return Returns value of diagonal from matrix
121 public double getEigenvalue(int i)
123 return eigenvector.d[i];
138 * @return DOCUMENT ME!
140 public float[][] getComponents(int l, int n, int mm, float factor)
142 float[][] out = new float[m.rows][3];
144 for (int i = 0; i < m.rows; i++)
146 out[i][0] = (float) component(i, l) * factor;
147 out[i][1] = (float) component(i, n) * factor;
148 out[i][2] = (float) component(i, mm) * factor;
160 * @return DOCUMENT ME!
162 public double[] component(int n)
164 // n = index of eigenvector
165 double[] out = new double[m.rows];
167 for (int i = 0; i < m.rows; i++)
169 out[i] = component(i, n);
183 * @return DOCUMENT ME!
185 double component(int row, int n)
189 for (int i = 0; i < symm.cols; i++)
191 out += (symm.value[row][i] * eigenvector.value[i][n]);
194 return out / eigenvector.d[n];
197 public String getDetails()
199 return details.toString();
207 Matrix mt = m.transpose();
209 details.append(" --- OrigT * Orig ---- \n");
210 // eigenvector = mt.preMultiply(m); // standard seqspace comparison matrix
211 eigenvector = mt.preMultiply(m2); // jalview variation on seqsmace method
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 eigenvector.print(ps);
245 details.append(" --- Eigenvalues ---\n");
246 eigenvector.printD(ps);
249 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
250 * (int ev=0;ev<symm.rows; ev++) {
252 * ps.print(","+component(seq, ev)); } ps.println(); }