2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.1)
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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.analysis;
23 import jalview.datamodel.*;
24 import jalview.datamodel.BinarySequence.InvalidSequenceTypeException;
25 import jalview.math.*;
26 import jalview.schemes.ResidueProperties;
27 import jalview.schemes.ScoreMatrix;
30 * Performs Principal Component Analysis on given sequences
35 public class PCA implements Runnable
47 StringBuffer details = new StringBuffer();
50 * Creates a new PCA object. By default, uses blosum62 matrix to generate
51 * sequence similarity matrices
54 * Set of amino acid sequences to perform PCA on
56 public PCA(String[] s)
62 * Creates a new PCA object. By default, uses blosum62 matrix to generate
63 * sequence similarity matrices
66 * Set of sequences to perform PCA on
68 * if true, uses standard DNA/RNA matrix for sequence similarity
71 public PCA(String[] s, boolean nucleotides)
73 this(s, nucleotides, null);
75 public PCA(String[] s, boolean nucleotides, String s_m)
78 BinarySequence[] bs = new BinarySequence[s.length];
81 while ((ii < s.length) && (s[ii] != null))
83 bs[ii] = new BinarySequence(s[ii], nucleotides);
88 BinarySequence[] bs2 = new BinarySequence[s.length];
90 ScoreMatrix smtrx = null;
94 smtrx = ResidueProperties.getScoreMatrix(sm);
98 // either we were given a non-existent score matrix or a scoremodel that isn't based on a pairwise symbol score matrix
99 smtrx = ResidueProperties.getScoreMatrix(sm=(nucleotides ? "DNA" : "BLOSUM62"));
101 details.append("PCA calculation using " + sm
102 + " sequence similarity matrix\n========\n\n");
103 while ((ii < s.length) && (s[ii] != null))
105 bs2[ii] = new BinarySequence(s[ii], nucleotides);
110 bs2[ii].matrixEncode(smtrx);
111 } catch (InvalidSequenceTypeException x)
113 details.append("Unexpected mismatch of sequence type and score matrix. Calculation will not be valid!\n\n");
119 // System.out.println("Created binary encoding");
123 while ((count < bs.length) && (bs[count] != null))
128 double[][] seqmat = new double[count][bs[0].getDBinary().length];
129 double[][] seqmat2 = new double[count][bs2[0].getDBinary().length];
134 seqmat[i] = bs[i].getDBinary();
135 seqmat2[i] = bs2[i].getDBinary();
139 // System.out.println("Created array");
141 // System.out.println(" --- Original matrix ---- ");
142 m = new Matrix(seqmat, count, bs[0].getDBinary().length);
143 m2 = new Matrix(seqmat2, count, bs2[0].getDBinary().length);
148 * Returns the matrix used in PCA calculation
150 * @return java.math.Matrix object
162 * Index of diagonal within matrix
164 * @return Returns value of diagonal from matrix
166 public double getEigenvalue(int i)
168 return eigenvector.d[i];
183 * @return DOCUMENT ME!
185 public float[][] getComponents(int l, int n, int mm, float factor)
187 float[][] out = new float[m.rows][3];
189 for (int i = 0; i < m.rows; i++)
191 out[i][0] = (float) component(i, l) * factor;
192 out[i][1] = (float) component(i, n) * factor;
193 out[i][2] = (float) component(i, mm) * factor;
205 * @return DOCUMENT ME!
207 public double[] component(int n)
209 // n = index of eigenvector
210 double[] out = new double[m.rows];
212 for (int i = 0; i < m.rows; i++)
214 out[i] = component(i, n);
228 * @return DOCUMENT ME!
230 double component(int row, int n)
234 for (int i = 0; i < symm.cols; i++)
236 out += (symm.value[row][i] * eigenvector.value[i][n]);
239 return out / eigenvector.d[n];
242 public String getDetails()
244 return details.toString();
252 PrintStream ps = new PrintStream(System.out)
254 public void print(String x)
259 public void println()
261 details.append("\n");
266 details.append("PCA Calculation Mode is "
267 + (jvCalcMode ? "Jalview variant" : "Original SeqSpace") + "\n");
268 Matrix mt = m.transpose();
270 details.append(" --- OrigT * Orig ---- \n");
273 eigenvector = mt.preMultiply(m); // standard seqspace comparison matrix
277 eigenvector = mt.preMultiply(m2); // jalview variation on seqsmace method
280 eigenvector.print(ps);
282 symm = eigenvector.copy();
286 details.append(" ---Tridiag transform matrix ---\n");
287 details.append(" --- D vector ---\n");
288 eigenvector.printD(ps);
290 details.append("--- E vector ---\n");
291 eigenvector.printE(ps);
294 // Now produce the diagonalization matrix
296 } catch (Exception q)
299 details.append("\n*** Unexpected exception when performing PCA ***\n"+q.getLocalizedMessage());
300 details.append("*** Matrices below may not be fully diagonalised. ***\n");
303 details.append(" --- New diagonalization matrix ---\n");
304 eigenvector.print(ps);
305 details.append(" --- Eigenvalues ---\n");
306 eigenvector.printD(ps);
309 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
310 * (int ev=0;ev<symm.rows; ev++) {
312 * ps.print(","+component(seq, ev)); } ps.println(); }
316 boolean jvCalcMode = true;
318 public void setJvCalcMode(boolean calcMode)
320 this.jvCalcMode = calcMode;