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.api.analysis.ScoreModelI;
24 import jalview.api.analysis.SimilarityParamsI;
25 import jalview.datamodel.AlignmentView;
26 import jalview.datamodel.Point;
27 import jalview.math.MatrixI;
29 import java.io.PrintStream;
32 * Performs Principal Component Analysis on given sequences
34 public class PCA implements Runnable
39 final private AlignmentView seqs;
41 final private ScoreModelI scoreModel;
43 final private SimilarityParamsI similarityParams;
48 private MatrixI pairwiseScores;
50 private MatrixI afterTred;
52 private MatrixI eigenvector;
54 private String details;
57 * Constructor given the sequences to compute for, the similarity model to
58 * use, and a set of parameters for sequence comparison
64 public PCA(AlignmentView sequences, ScoreModelI sm, SimilarityParamsI options)
66 this.seqs = sequences;
68 this.similarityParams = options;
75 * Index of diagonal within matrix
77 * @return Returns value of diagonal from matrix
79 public double getEigenvalue(int i)
81 return eigenvector.getD()[i];
96 * @return DOCUMENT ME!
98 public Point[] getComponents(int l, int n, int mm, float factor)
100 Point[] out = new Point[getHeight()];
102 for (int i = 0; i < getHeight(); i++)
104 float x = (float) component(i, l) * factor;
105 float y = (float) component(i, n) * factor;
106 float z = (float) component(i, mm) * factor;
107 out[i] = new Point(x, y, z);
119 * @return DOCUMENT ME!
121 public double[] component(int n)
123 // n = index of eigenvector
124 double[] out = new double[getHeight()];
126 for (int i = 0; i < out.length; i++)
128 out[i] = component(i, n);
142 * @return DOCUMENT ME!
144 double component(int row, int n)
148 for (int i = 0; i < pairwiseScores.width(); i++)
150 out += (pairwiseScores.getValue(row, i) * eigenvector.getValue(i, n));
153 return out / eigenvector.getD()[n];
157 * Answers a formatted text report of the PCA calculation results (matrices
158 * and eigenvalues) suitable for display
162 public String getDetails()
166 StringBuilder sb = new StringBuilder(1024);
167 sb.append("PCA calculation using ").append(scoreModel.getName())
168 .append(" sequence similarity matrix\n========\n\n");
169 PrintStream ps = wrapOutputBuffer(sb);
171 sb.append(" --- OrigT * Orig ---- \n");
172 pairwiseScores.print(ps, "%8.2f");
174 sb.append(" ---Tridiag transform matrix ---\n");
175 sb.append(" --- D vector ---\n");
176 afterTred.printD(ps, "%15.4e");
178 sb.append("--- E vector ---\n");
179 afterTred.printE(ps, "%15.4e");
182 sb.append(" --- New diagonalization matrix ---\n");
183 eigenvector.print(ps, "%8.2f");
184 sb.append(" --- Eigenvalues ---\n");
185 eigenvector.printD(ps, "%15.4e");
188 return sb.toString();
193 * Performs the PCA calculation
199 * print details to a string buffer as they are computed
201 StringBuilder sb = new StringBuilder(1024);
202 sb.append("PCA calculation using ").append(scoreModel.getName())
203 .append(" sequence similarity matrix\n========\n\n");
204 PrintStream ps = wrapOutputBuffer(sb);
208 eigenvector = scoreModel.findSimilarities(seqs, similarityParams);
210 sb.append(" --- OrigT * Orig ---- \n");
211 eigenvector.print(ps, "%8.2f");
213 pairwiseScores = eigenvector.copy();
217 afterTred = eigenvector.copy();
219 sb.append(" ---Tridiag transform matrix ---\n");
220 sb.append(" --- D vector ---\n");
221 afterTred.printD(ps, "%15.4e");
223 sb.append("--- E vector ---\n");
224 afterTred.printE(ps, "%15.4e");
227 // Now produce the diagonalization matrix
229 } catch (Exception q)
232 sb.append("\n*** Unexpected exception when performing PCA ***\n"
233 + q.getLocalizedMessage());
235 "*** Matrices below may not be fully diagonalised. ***\n");
238 sb.append(" --- New diagonalization matrix ---\n");
239 eigenvector.print(ps, "%8.2f");
240 sb.append(" --- Eigenvalues ---\n");
241 eigenvector.printD(ps, "%15.4e");
244 details = sb.toString();
248 * Returns a PrintStream that wraps (appends its output to) the given
254 protected PrintStream wrapOutputBuffer(StringBuilder sb)
256 PrintStream ps = new PrintStream(System.out)
259 public void print(String x)
265 public void println()
274 * Answers the N dimensions of the NxN PCA matrix. This is the number of
275 * sequences involved in the pairwise score calculation.
279 public int getHeight()
281 // TODO can any of seqs[] be null?
282 return pairwiseScores.height();// seqs.getSequences().length;
286 * Answers the sequence pairwise similarity scores which were the first step
287 * of the PCA calculation
291 public MatrixI getPairwiseScores()
293 return pairwiseScores;
296 public void setPairwiseScores(MatrixI m)
301 public MatrixI getEigenmatrix()
306 public void setEigenmatrix(MatrixI m)
311 public void setDetails(String d)