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.analysis.scoremodels.PairwiseDistanceModel;
24 import jalview.analysis.scoremodels.ScoreMatrix;
25 import jalview.analysis.scoremodels.ScoreModels;
26 import jalview.datamodel.BinarySequence;
27 import jalview.datamodel.BinarySequence.InvalidSequenceTypeException;
28 import jalview.math.Matrix;
30 import java.io.PrintStream;
33 * Performs Principal Component Analysis on given sequences
38 public class PCA implements Runnable
50 StringBuffer details = new StringBuffer();
53 * Creates a new PCA object. By default, uses blosum62 matrix to generate
54 * sequence similarity matrices
57 * Set of amino acid sequences to perform PCA on
59 public PCA(String[] s)
65 * Creates a new PCA object. By default, uses blosum62 matrix to generate
66 * sequence similarity matrices
69 * Set of sequences to perform PCA on
71 * if true, uses standard DNA/RNA matrix for sequence similarity
74 public PCA(String[] s, boolean nucleotides)
76 this(s, nucleotides, null);
79 public PCA(String[] s, boolean nucleotides, String s_m)
82 BinarySequence[] bs = new BinarySequence[s.length];
85 while ((ii < s.length) && (s[ii] != null))
87 bs[ii] = new BinarySequence(s[ii], nucleotides);
92 BinarySequence[] bs2 = new BinarySequence[s.length];
94 ScoreMatrix smtrx = null;
98 smtrx = (ScoreMatrix) ((PairwiseDistanceModel) ScoreModels
100 .forName(sm)).getScoreModel();
104 // either we were given a non-existent score matrix or a scoremodel that
105 // isn't based on a pairwise symbol score matrix
106 smtrx = ScoreModels.getInstance().getDefaultModel(!nucleotides);
108 details.append("PCA calculation using " + sm
109 + " sequence similarity matrix\n========\n\n");
110 while ((ii < s.length) && (s[ii] != null))
112 bs2[ii] = new BinarySequence(s[ii], nucleotides);
117 bs2[ii].matrixEncode(smtrx);
118 } catch (InvalidSequenceTypeException x)
120 details.append("Unexpected mismatch of sequence type and score matrix. Calculation will not be valid!\n\n");
126 // System.out.println("Created binary encoding");
130 while ((count < bs.length) && (bs[count] != null))
135 double[][] seqmat = new double[count][bs[0].getDBinary().length];
136 double[][] seqmat2 = new double[count][bs2[0].getDBinary().length];
141 seqmat[i] = bs[i].getDBinary();
142 seqmat2[i] = bs2[i].getDBinary();
146 // System.out.println("Created array");
148 // System.out.println(" --- Original matrix ---- ");
149 m = new Matrix(seqmat);
150 m2 = new Matrix(seqmat2);
155 * Returns the matrix used in PCA calculation
157 * @return java.math.Matrix object
169 * Index of diagonal within matrix
171 * @return Returns value of diagonal from matrix
173 public double getEigenvalue(int i)
175 return eigenvector.d[i];
190 * @return DOCUMENT ME!
192 public float[][] getComponents(int l, int n, int mm, float factor)
194 float[][] out = new float[m.rows][3];
196 for (int i = 0; i < m.rows; i++)
198 out[i][0] = (float) component(i, l) * factor;
199 out[i][1] = (float) component(i, n) * factor;
200 out[i][2] = (float) component(i, mm) * factor;
212 * @return DOCUMENT ME!
214 public double[] component(int n)
216 // n = index of eigenvector
217 double[] out = new double[m.rows];
219 for (int i = 0; i < m.rows; i++)
221 out[i] = component(i, n);
235 * @return DOCUMENT ME!
237 double component(int row, int n)
241 for (int i = 0; i < symm.cols; i++)
243 out += (symm.value[row][i] * eigenvector.value[i][n]);
246 return out / eigenvector.d[n];
249 public String getDetails()
251 return details.toString();
260 PrintStream ps = new PrintStream(System.out)
263 public void print(String x)
269 public void println()
271 details.append("\n");
277 details.append("PCA Calculation Mode is "
278 + (jvCalcMode ? "Jalview variant" : "Original SeqSpace")
280 Matrix mt = m.transpose();
282 details.append(" --- OrigT * Orig ---- \n");
285 eigenvector = mt.preMultiply(m); // standard seqspace comparison matrix
289 eigenvector = mt.preMultiply(m2); // jalview variation on seqsmace
293 eigenvector.print(ps);
295 symm = eigenvector.copy();
299 details.append(" ---Tridiag transform matrix ---\n");
300 details.append(" --- D vector ---\n");
301 eigenvector.printD(ps);
303 details.append("--- E vector ---\n");
304 eigenvector.printE(ps);
307 // Now produce the diagonalization matrix
309 } catch (Exception q)
312 details.append("\n*** Unexpected exception when performing PCA ***\n"
313 + q.getLocalizedMessage());
314 details.append("*** Matrices below may not be fully diagonalised. ***\n");
317 details.append(" --- New diagonalization matrix ---\n");
318 eigenvector.print(ps);
319 details.append(" --- Eigenvalues ---\n");
320 eigenvector.printD(ps);
323 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
324 * (int ev=0;ev<symm.rows; ev++) {
326 * ps.print(","+component(seq, ev)); } ps.println(); }
330 boolean jvCalcMode = true;
332 public void setJvCalcMode(boolean calcMode)
334 this.jvCalcMode = calcMode;