2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.analysis;
25 import jalview.datamodel.*;
26 import jalview.datamodel.BinarySequence.InvalidSequenceTypeException;
27 import jalview.math.*;
28 import jalview.schemes.ResidueProperties;
29 import jalview.schemes.ScoreMatrix;
32 * Performs Principal Component Analysis on given sequences
37 public class PCA implements Runnable
49 StringBuffer details = new StringBuffer();
52 * Creates a new PCA object. By default, uses blosum62 matrix to generate
53 * sequence similarity matrices
56 * Set of amino acid sequences to perform PCA on
58 public PCA(String[] s)
64 * Creates a new PCA object. By default, uses blosum62 matrix to generate
65 * sequence similarity matrices
68 * Set of sequences to perform PCA on
70 * if true, uses standard DNA/RNA matrix for sequence similarity
73 public PCA(String[] s, boolean nucleotides)
75 this(s, nucleotides, null);
78 public PCA(String[] s, boolean nucleotides, String s_m)
81 BinarySequence[] bs = new BinarySequence[s.length];
84 while ((ii < s.length) && (s[ii] != null))
86 bs[ii] = new BinarySequence(s[ii], nucleotides);
91 BinarySequence[] bs2 = new BinarySequence[s.length];
93 ScoreMatrix smtrx = null;
97 smtrx = ResidueProperties.getScoreMatrix(sm);
101 // either we were given a non-existent score matrix or a scoremodel that
102 // isn't based on a pairwise symbol score matrix
103 smtrx = ResidueProperties.getScoreMatrix(sm = (nucleotides ? "DNA"
106 details.append("PCA calculation using " + sm
107 + " sequence similarity matrix\n========\n\n");
108 while ((ii < s.length) && (s[ii] != null))
110 bs2[ii] = new BinarySequence(s[ii], nucleotides);
115 bs2[ii].matrixEncode(smtrx);
116 } catch (InvalidSequenceTypeException x)
118 details.append("Unexpected mismatch of sequence type and score matrix. Calculation will not be valid!\n\n");
124 // System.out.println("Created binary encoding");
128 while ((count < bs.length) && (bs[count] != null))
133 double[][] seqmat = new double[count][bs[0].getDBinary().length];
134 double[][] seqmat2 = new double[count][bs2[0].getDBinary().length];
139 seqmat[i] = bs[i].getDBinary();
140 seqmat2[i] = bs2[i].getDBinary();
144 // System.out.println("Created array");
146 // System.out.println(" --- Original matrix ---- ");
147 m = new Matrix(seqmat, count, bs[0].getDBinary().length);
148 m2 = new Matrix(seqmat2, count, bs2[0].getDBinary().length);
153 * Returns the matrix used in PCA calculation
155 * @return java.math.Matrix object
167 * Index of diagonal within matrix
169 * @return Returns value of diagonal from matrix
171 public double getEigenvalue(int i)
173 return eigenvector.d[i];
188 * @return DOCUMENT ME!
190 public float[][] getComponents(int l, int n, int mm, float factor)
192 float[][] out = new float[m.rows][3];
194 for (int i = 0; i < m.rows; i++)
196 out[i][0] = (float) component(i, l) * factor;
197 out[i][1] = (float) component(i, n) * factor;
198 out[i][2] = (float) component(i, mm) * factor;
210 * @return DOCUMENT ME!
212 public double[] component(int n)
214 // n = index of eigenvector
215 double[] out = new double[m.rows];
217 for (int i = 0; i < m.rows; i++)
219 out[i] = component(i, n);
233 * @return DOCUMENT ME!
235 double component(int row, int n)
239 for (int i = 0; i < symm.cols; i++)
241 out += (symm.value[row][i] * eigenvector.value[i][n]);
244 return out / eigenvector.d[n];
247 public String getDetails()
249 return details.toString();
257 PrintStream ps = new PrintStream(System.out)
259 public void print(String x)
264 public void println()
266 details.append("\n");
272 details.append("PCA Calculation Mode is "
273 + (jvCalcMode ? "Jalview variant" : "Original SeqSpace")
275 Matrix mt = m.transpose();
277 details.append(" --- OrigT * Orig ---- \n");
280 eigenvector = mt.preMultiply(m); // standard seqspace comparison matrix
284 eigenvector = mt.preMultiply(m2); // jalview variation on seqsmace
288 eigenvector.print(ps);
290 symm = eigenvector.copy();
294 details.append(" ---Tridiag transform matrix ---\n");
295 details.append(" --- D vector ---\n");
296 eigenvector.printD(ps);
298 details.append("--- E vector ---\n");
299 eigenvector.printE(ps);
302 // Now produce the diagonalization matrix
304 } catch (Exception q)
307 details.append("\n*** Unexpected exception when performing PCA ***\n"
308 + q.getLocalizedMessage());
309 details.append("*** Matrices below may not be fully diagonalised. ***\n");
312 details.append(" --- New diagonalization matrix ---\n");
313 eigenvector.print(ps);
314 details.append(" --- Eigenvalues ---\n");
315 eigenvector.printD(ps);
318 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
319 * (int ev=0;ev<symm.rows; ev++) {
321 * ps.print(","+component(seq, ev)); } ps.println(); }
325 boolean jvCalcMode = true;
327 public void setJvCalcMode(boolean calcMode)
329 this.jvCalcMode = calcMode;