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;
23 import jalview.math.MatrixI;
24 import jalview.schemes.ResidueProperties;
25 import jalview.schemes.ScoreMatrix;
27 import java.io.PrintStream;
30 * Performs Principal Component Analysis on given sequences
32 public class PCA implements Runnable
34 boolean jvCalcMode = true;
42 StringBuilder details = new StringBuilder(1024);
44 private String[] seqs;
46 private ScoreMatrix scoreMatrix;
49 * Creates a new PCA object. By default, uses blosum62 matrix to generate
50 * sequence similarity matrices
53 * Set of amino acid sequences to perform PCA on
55 public PCA(String[] s)
61 * Creates a new PCA object. By default, uses blosum62 matrix to generate
62 * sequence similarity matrices
65 * Set of sequences to perform PCA on
67 * if true, uses standard DNA/RNA matrix for sequence similarity
70 public PCA(String[] s, boolean nucleotides)
72 this(s, nucleotides, null);
75 public PCA(String[] s, boolean nucleotides, String s_m)
79 // BinarySequence[] bs = new BinarySequence[s.length];
82 // while ((ii < s.length) && (s[ii] != null))
84 // bs[ii] = new BinarySequence(s[ii], nucleotides);
89 // BinarySequence[] bs2 = new BinarySequence[s.length];
94 scoreMatrix = ResidueProperties.getScoreMatrix(sm);
96 if (scoreMatrix == null)
98 // either we were given a non-existent score matrix or a scoremodel that
99 // isn't based on a pairwise symbol score matrix
100 scoreMatrix = ResidueProperties
101 .getScoreMatrix(sm = (nucleotides ? "DNA" : "BLOSUM62"));
103 details.append("PCA calculation using " + sm
104 + " sequence similarity matrix\n========\n\n");
106 // while ((ii < s.length) && (s[ii] != null))
108 // bs2[ii] = new BinarySequence(s[ii], nucleotides);
109 // if (scoreMatrix != null)
113 // bs2[ii].matrixEncode(scoreMatrix);
114 // } catch (InvalidSequenceTypeException x)
116 // details.append("Unexpected mismatch of sequence type and score matrix. Calculation will not be valid!\n\n");
123 // while ((count < bs.length) && (bs[count] != null))
128 // double[][] seqmat = new double[count][];
129 // double[][] seqmat2 = new double[count][];
134 // seqmat[i] = bs[i].getDBinary();
135 // seqmat2[i] = bs2[i].getDBinary();
140 // * using a SparseMatrix to hold the encoded sequences matrix
141 // * greatly speeds up matrix multiplication as these are mostly zero
143 // m = new SparseMatrix(seqmat);
144 // m2 = new Matrix(seqmat2);
152 * Index of diagonal within matrix
154 * @return Returns value of diagonal from matrix
156 public double getEigenvalue(int i)
158 return eigenvector.getD()[i];
173 * @return DOCUMENT ME!
175 public float[][] getComponents(int l, int n, int mm, float factor)
177 float[][] out = new float[getHeight()][3];
179 for (int i = 0; i < getHeight(); i++)
181 out[i][0] = (float) component(i, l) * factor;
182 out[i][1] = (float) component(i, n) * factor;
183 out[i][2] = (float) component(i, mm) * factor;
195 * @return DOCUMENT ME!
197 public double[] component(int n)
199 // n = index of eigenvector
200 double[] out = new double[getHeight()];
202 for (int i = 0; i < out.length; i++)
204 out[i] = component(i, n);
218 * @return DOCUMENT ME!
220 double component(int row, int n)
224 for (int i = 0; i < symm.width(); i++)
226 out += (symm.getValue(row, i) * eigenvector.getValue(i, n));
229 return out / eigenvector.getD()[n];
232 public String getDetails()
234 return details.toString();
243 PrintStream ps = new PrintStream(System.out)
246 public void print(String x)
252 public void println()
254 details.append("\n");
258 // long now = System.currentTimeMillis();
261 details.append("PCA Calculation Mode is "
262 + (jvCalcMode ? "Jalview variant" : "Original SeqSpace")
265 eigenvector = scoreMatrix.computePairwiseScores(seqs);
267 details.append(" --- OrigT * Orig ---- \n");
268 eigenvector.print(ps, "%8.2f");
270 symm = eigenvector.copy();
274 details.append(" ---Tridiag transform matrix ---\n");
275 details.append(" --- D vector ---\n");
276 eigenvector.printD(ps, "%15.4e");
278 details.append("--- E vector ---\n");
279 eigenvector.printE(ps, "%15.4e");
282 // Now produce the diagonalization matrix
284 } catch (Exception q)
287 details.append("\n*** Unexpected exception when performing PCA ***\n"
288 + q.getLocalizedMessage());
289 details.append("*** Matrices below may not be fully diagonalised. ***\n");
292 details.append(" --- New diagonalization matrix ---\n");
293 eigenvector.print(ps, "%8.2f");
294 details.append(" --- Eigenvalues ---\n");
295 eigenvector.printD(ps, "%15.4e");
298 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
299 * (int ev=0;ev<symm.rows; ev++) {
301 * ps.print(","+component(seq, ev)); } ps.println(); }
303 // System.out.println(("PCA.run() took "
304 // + (System.currentTimeMillis() - now) + "ms"));
307 public void setJvCalcMode(boolean calcMode)
309 this.jvCalcMode = calcMode;
313 * Answers the N dimensions of the NxN PCA matrix. This is the number of
314 * sequences involved in the pairwise score calculation.
318 public int getHeight()
320 // TODO can any of seqs[] be null?