2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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);
77 public PCA(String[] s, boolean nucleotides, String s_m)
80 BinarySequence[] bs = new BinarySequence[s.length];
83 while ((ii < s.length) && (s[ii] != null))
85 bs[ii] = new BinarySequence(s[ii], nucleotides);
90 BinarySequence[] bs2 = new BinarySequence[s.length];
92 ScoreMatrix smtrx = null;
96 smtrx = ResidueProperties.getScoreMatrix(sm);
100 // either we were given a non-existent score matrix or a scoremodel that isn't based on a pairwise symbol score matrix
101 smtrx = ResidueProperties.getScoreMatrix(sm=(nucleotides ? "DNA" : "BLOSUM62"));
103 details.append("PCA calculation using " + sm
104 + " sequence similarity matrix\n========\n\n");
105 while ((ii < s.length) && (s[ii] != null))
107 bs2[ii] = new BinarySequence(s[ii], nucleotides);
112 bs2[ii].matrixEncode(smtrx);
113 } catch (InvalidSequenceTypeException x)
115 details.append("Unexpected mismatch of sequence type and score matrix. Calculation will not be valid!\n\n");
121 // System.out.println("Created binary encoding");
125 while ((count < bs.length) && (bs[count] != null))
130 double[][] seqmat = new double[count][bs[0].getDBinary().length];
131 double[][] seqmat2 = new double[count][bs2[0].getDBinary().length];
136 seqmat[i] = bs[i].getDBinary();
137 seqmat2[i] = bs2[i].getDBinary();
141 // System.out.println("Created array");
143 // System.out.println(" --- Original matrix ---- ");
144 m = new Matrix(seqmat, count, bs[0].getDBinary().length);
145 m2 = new Matrix(seqmat2, count, bs2[0].getDBinary().length);
150 * Returns the matrix used in PCA calculation
152 * @return java.math.Matrix object
164 * Index of diagonal within matrix
166 * @return Returns value of diagonal from matrix
168 public double getEigenvalue(int i)
170 return eigenvector.d[i];
185 * @return DOCUMENT ME!
187 public float[][] getComponents(int l, int n, int mm, float factor)
189 float[][] out = new float[m.rows][3];
191 for (int i = 0; i < m.rows; i++)
193 out[i][0] = (float) component(i, l) * factor;
194 out[i][1] = (float) component(i, n) * factor;
195 out[i][2] = (float) component(i, mm) * factor;
207 * @return DOCUMENT ME!
209 public double[] component(int n)
211 // n = index of eigenvector
212 double[] out = new double[m.rows];
214 for (int i = 0; i < m.rows; i++)
216 out[i] = component(i, n);
230 * @return DOCUMENT ME!
232 double component(int row, int n)
236 for (int i = 0; i < symm.cols; i++)
238 out += (symm.value[row][i] * eigenvector.value[i][n]);
241 return out / eigenvector.d[n];
244 public String getDetails()
246 return details.toString();
254 PrintStream ps = new PrintStream(System.out)
256 public void print(String x)
261 public void println()
263 details.append("\n");
268 details.append("PCA Calculation Mode is "
269 + (jvCalcMode ? "Jalview variant" : "Original SeqSpace") + "\n");
270 Matrix mt = m.transpose();
272 details.append(" --- OrigT * Orig ---- \n");
275 eigenvector = mt.preMultiply(m); // standard seqspace comparison matrix
279 eigenvector = mt.preMultiply(m2); // jalview variation on seqsmace method
282 eigenvector.print(ps);
284 symm = eigenvector.copy();
288 details.append(" ---Tridiag transform matrix ---\n");
289 details.append(" --- D vector ---\n");
290 eigenvector.printD(ps);
292 details.append("--- E vector ---\n");
293 eigenvector.printE(ps);
296 // Now produce the diagonalization matrix
298 } catch (Exception q)
301 details.append("\n*** Unexpected exception when performing PCA ***\n"+q.getLocalizedMessage());
302 details.append("*** Matrices below may not be fully diagonalised. ***\n");
305 details.append(" --- New diagonalization matrix ---\n");
306 eigenvector.print(ps);
307 details.append(" --- Eigenvalues ---\n");
308 eigenvector.printD(ps);
311 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
312 * (int ev=0;ev<symm.rows; ev++) {
314 * ps.print(","+component(seq, ev)); } ps.println(); }
318 boolean jvCalcMode = true;
320 public void setJvCalcMode(boolean calcMode)
322 this.jvCalcMode = calcMode;