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)
83 scoreMatrix = ResidueProperties.getScoreMatrix(sm);
85 if (scoreMatrix == null)
87 // either we were given a non-existent score matrix or a scoremodel that
88 // isn't based on a pairwise symbol score matrix
89 scoreMatrix = ResidueProperties
90 .getScoreMatrix(sm = (nucleotides ? "DNA" : "BLOSUM62"));
92 details.append("PCA calculation using " + sm
93 + " sequence similarity matrix\n========\n\n");
100 * Index of diagonal within matrix
102 * @return Returns value of diagonal from matrix
104 public double getEigenvalue(int i)
106 return eigenvector.getD()[i];
121 * @return DOCUMENT ME!
123 public float[][] getComponents(int l, int n, int mm, float factor)
125 float[][] out = new float[getHeight()][3];
127 for (int i = 0; i < getHeight(); i++)
129 out[i][0] = (float) component(i, l) * factor;
130 out[i][1] = (float) component(i, n) * factor;
131 out[i][2] = (float) component(i, mm) * factor;
143 * @return DOCUMENT ME!
145 public double[] component(int n)
147 // n = index of eigenvector
148 double[] out = new double[getHeight()];
150 for (int i = 0; i < out.length; i++)
152 out[i] = component(i, n);
166 * @return DOCUMENT ME!
168 double component(int row, int n)
172 for (int i = 0; i < symm.width(); i++)
174 out += (symm.getValue(row, i) * eigenvector.getValue(i, n));
177 return out / eigenvector.getD()[n];
180 public String getDetails()
182 return details.toString();
191 PrintStream ps = new PrintStream(System.out)
194 public void print(String x)
200 public void println()
202 details.append("\n");
206 // long now = System.currentTimeMillis();
209 details.append("PCA Calculation Mode is "
210 + (jvCalcMode ? "Jalview variant" : "Original SeqSpace")
213 eigenvector = scoreMatrix.computePairwiseScores(seqs);
215 details.append(" --- OrigT * Orig ---- \n");
216 eigenvector.print(ps, "%8.2f");
218 symm = eigenvector.copy();
222 details.append(" ---Tridiag transform matrix ---\n");
223 details.append(" --- D vector ---\n");
224 eigenvector.printD(ps, "%15.4e");
226 details.append("--- E vector ---\n");
227 eigenvector.printE(ps, "%15.4e");
230 // Now produce the diagonalization matrix
232 } catch (Exception q)
235 details.append("\n*** Unexpected exception when performing PCA ***\n"
236 + q.getLocalizedMessage());
237 details.append("*** Matrices below may not be fully diagonalised. ***\n");
240 details.append(" --- New diagonalization matrix ---\n");
241 eigenvector.print(ps, "%8.2f");
242 details.append(" --- Eigenvalues ---\n");
243 eigenvector.printD(ps, "%15.4e");
246 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
247 * (int ev=0;ev<symm.rows; ev++) {
249 * ps.print(","+component(seq, ev)); } ps.println(); }
251 // System.out.println(("PCA.run() took "
252 // + (System.currentTimeMillis() - now) + "ms"));
255 public void setJvCalcMode(boolean calcMode)
257 this.jvCalcMode = calcMode;
261 * Answers the N dimensions of the NxN PCA matrix. This is the number of
262 * sequences involved in the pairwise score calculation.
266 public int getHeight()
268 // TODO can any of seqs[] be null?