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.bin.Console;
26 import jalview.datamodel.AlignmentView;
27 import jalview.datamodel.Point;
28 import jalview.math.MatrixI;
30 import java.io.PrintStream;
33 * Performs Principal Component Analysis on given sequences
35 public class PCA implements Runnable
40 final private AlignmentView seqs;
42 final private ScoreModelI scoreModel;
44 final private SimilarityParamsI similarityParams;
49 private MatrixI pairwiseScores;
51 private MatrixI tridiagonal;
53 private MatrixI eigenMatrix;
56 * Constructor given the sequences to compute for, the similarity model to
57 * use, and a set of parameters for sequence comparison
63 public PCA(AlignmentView sequences, ScoreModelI sm,
64 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 eigenMatrix.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) * eigenMatrix.getValue(i, n));
153 return out / eigenMatrix.getD()[n];
157 * Answers a formatted text report of the PCA calculation results (matrices
158 * and eigenvalues) suitable for display
162 public String getDetails()
164 StringBuilder sb = new StringBuilder(1024);
165 sb.append("PCA calculation using ").append(scoreModel.getName())
166 .append(" sequence similarity matrix\n========\n\n");
167 PrintStream ps = wrapOutputBuffer(sb);
170 * pairwise similarity scores
172 sb.append(" --- OrigT * Orig ---- \n");
173 pairwiseScores.print(ps, "%8.2f");
176 * tridiagonal matrix, with D and E vectors
178 sb.append(" ---Tridiag transform matrix ---\n");
179 sb.append(" --- D vector ---\n");
180 tridiagonal.printD(ps, "%15.4e");
182 sb.append("--- E vector ---\n");
183 tridiagonal.printE(ps, "%15.4e");
187 * eigenvalues matrix, with D vector
189 sb.append(" --- New diagonalization matrix ---\n");
190 eigenMatrix.print(ps, "%8.2f");
191 sb.append(" --- Eigenvalues ---\n");
192 eigenMatrix.printD(ps, "%15.4e");
195 return sb.toString();
199 * Performs the PCA calculation
207 * sequence pairwise similarity scores
209 pairwiseScores = scoreModel.findSimilarities(seqs, similarityParams);
214 tridiagonal = pairwiseScores.copy();
218 * the diagonalization matrix
220 eigenMatrix = tridiagonal.copy();
222 } catch (Exception q)
224 Console.error("Error computing PCA: " + q.getMessage());
230 * Returns a PrintStream that wraps (appends its output to) the given
236 protected PrintStream wrapOutputBuffer(StringBuilder sb)
238 PrintStream ps = new PrintStream(System.out)
241 public void print(String x)
247 public void println()
256 * Answers the N dimensions of the NxN PCA matrix. This is the number of
257 * sequences involved in the pairwise score calculation.
261 public int getHeight()
263 // TODO can any of seqs[] be null?
264 return pairwiseScores.height();// seqs.getSequences().length;
268 * Answers the sequence pairwise similarity scores which were the first step
269 * of the PCA calculation
273 public MatrixI getPairwiseScores()
275 return pairwiseScores;
278 public void setPairwiseScores(MatrixI m)
283 public MatrixI getEigenmatrix()
288 public void setEigenmatrix(MatrixI m)
293 public MatrixI getTridiagonal()
298 public void setTridiagonal(MatrixI tridiagonal)
300 this.tridiagonal = tridiagonal;