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.math.MatrixI;
28 import java.io.PrintStream;
31 * Performs Principal Component Analysis on given sequences
33 public class PCA implements Runnable
35 boolean jvCalcMode = true;
43 StringBuilder details = new StringBuilder(1024);
45 private String[] seqs;
47 private ScoreMatrix scoreMatrix;
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);
76 public PCA(String[] s, boolean nucleotides, String s_m)
84 scoreMatrix = (ScoreMatrix) ((PairwiseDistanceModel) ScoreModels
86 .forName(sm)).getScoreModel();
88 if (scoreMatrix == null)
90 // either we were given a non-existent score matrix or a scoremodel that
91 // isn't based on a pairwise symbol score matrix
92 scoreMatrix = ScoreModels.getInstance().getDefaultModel(!nucleotides);
94 details.append("PCA calculation using " + sm
95 + " sequence similarity matrix\n========\n\n");
102 * Index of diagonal within matrix
104 * @return Returns value of diagonal from matrix
106 public double getEigenvalue(int i)
108 return eigenvector.getD()[i];
123 * @return DOCUMENT ME!
125 public float[][] getComponents(int l, int n, int mm, float factor)
127 float[][] out = new float[getHeight()][3];
129 for (int i = 0; i < getHeight(); i++)
131 out[i][0] = (float) component(i, l) * factor;
132 out[i][1] = (float) component(i, n) * factor;
133 out[i][2] = (float) component(i, mm) * factor;
145 * @return DOCUMENT ME!
147 public double[] component(int n)
149 // n = index of eigenvector
150 double[] out = new double[getHeight()];
152 for (int i = 0; i < out.length; i++)
154 out[i] = component(i, n);
168 * @return DOCUMENT ME!
170 double component(int row, int n)
174 for (int i = 0; i < symm.width(); i++)
176 out += (symm.getValue(row, i) * eigenvector.getValue(i, n));
179 return out / eigenvector.getD()[n];
182 public String getDetails()
184 return details.toString();
193 PrintStream ps = new PrintStream(System.out)
196 public void print(String x)
202 public void println()
204 details.append("\n");
208 // long now = System.currentTimeMillis();
211 details.append("PCA Calculation Mode is "
212 + (jvCalcMode ? "Jalview variant" : "Original SeqSpace")
215 eigenvector = scoreMatrix.computePairwiseScores(seqs);
217 details.append(" --- OrigT * Orig ---- \n");
218 eigenvector.print(ps, "%8.2f");
220 symm = eigenvector.copy();
224 details.append(" ---Tridiag transform matrix ---\n");
225 details.append(" --- D vector ---\n");
226 eigenvector.printD(ps, "%15.4e");
228 details.append("--- E vector ---\n");
229 eigenvector.printE(ps, "%15.4e");
232 // Now produce the diagonalization matrix
234 } catch (Exception q)
237 details.append("\n*** Unexpected exception when performing PCA ***\n"
238 + q.getLocalizedMessage());
239 details.append("*** Matrices below may not be fully diagonalised. ***\n");
242 details.append(" --- New diagonalization matrix ---\n");
243 eigenvector.print(ps, "%8.2f");
244 details.append(" --- Eigenvalues ---\n");
245 eigenvector.printD(ps, "%15.4e");
248 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
249 * (int ev=0;ev<symm.rows; ev++) {
251 * ps.print(","+component(seq, ev)); } ps.println(); }
253 // System.out.println(("PCA.run() took "
254 // + (System.currentTimeMillis() - now) + "ms"));
257 public void setJvCalcMode(boolean calcMode)
259 this.jvCalcMode = calcMode;
263 * Answers the N dimensions of the NxN PCA matrix. This is the number of
264 * sequences involved in the pairwise score calculation.
268 public int getHeight()
270 // TODO can any of seqs[] be null?