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.math.MatrixI;
28 import java.io.PrintStream;
31 * Performs Principal Component Analysis on given sequences
33 public class PCA implements Runnable
38 final private AlignmentView seqs;
40 final private ScoreModelI scoreModel;
42 final private SimilarityParamsI similarityParams;
49 private MatrixI eigenvector;
51 private String details;
54 * Constructor given the sequences to compute for, the similarity model to
55 * use, and a set of parameters for sequence comparison
61 public PCA(AlignmentView sequences, ScoreModelI sm, SimilarityParamsI options)
63 this.seqs = sequences;
65 this.similarityParams = options;
72 * Index of diagonal within matrix
74 * @return Returns value of diagonal from matrix
76 public double getEigenvalue(int i)
78 return eigenvector.getD()[i];
93 * @return DOCUMENT ME!
95 public float[][] getComponents(int l, int n, int mm, float factor)
97 float[][] out = new float[getHeight()][3];
99 for (int i = 0; i < getHeight(); i++)
101 out[i][0] = (float) component(i, l) * factor;
102 out[i][1] = (float) component(i, n) * factor;
103 out[i][2] = (float) component(i, mm) * factor;
115 * @return DOCUMENT ME!
117 public double[] component(int n)
119 // n = index of eigenvector
120 double[] out = new double[getHeight()];
122 for (int i = 0; i < out.length; i++)
124 out[i] = component(i, n);
138 * @return DOCUMENT ME!
140 double component(int row, int n)
144 for (int i = 0; i < symm.width(); i++)
146 out += (symm.getValue(row, i) * eigenvector.getValue(i, n));
149 return out / eigenvector.getD()[n];
153 * Answers a formatted text report of the PCA calculation results (matrices
154 * and eigenvalues) suitable for display
158 public String getDetails()
164 * Performs the PCA calculation
170 * print details to a string buffer as they are computed
172 StringBuilder sb = new StringBuilder(1024);
173 sb.append("PCA calculation using ").append(scoreModel.getName())
174 .append(" sequence similarity matrix\n========\n\n");
175 PrintStream ps = wrapOutputBuffer(sb);
179 eigenvector = scoreModel.findSimilarities(seqs, similarityParams);
181 sb.append(" --- OrigT * Orig ---- \n");
182 eigenvector.print(ps, "%8.2f");
184 symm = eigenvector.copy();
188 sb.append(" ---Tridiag transform matrix ---\n");
189 sb.append(" --- D vector ---\n");
190 eigenvector.printD(ps, "%15.4e");
192 sb.append("--- E vector ---\n");
193 eigenvector.printE(ps, "%15.4e");
196 // Now produce the diagonalization matrix
198 } catch (Exception q)
201 sb.append("\n*** Unexpected exception when performing PCA ***\n"
202 + q.getLocalizedMessage());
204 "*** Matrices below may not be fully diagonalised. ***\n");
207 sb.append(" --- New diagonalization matrix ---\n");
208 eigenvector.print(ps, "%8.2f");
209 sb.append(" --- Eigenvalues ---\n");
210 eigenvector.printD(ps, "%15.4e");
213 details = sb.toString();
217 * Returns a PrintStream that wraps (appends its output to) the given
223 protected PrintStream wrapOutputBuffer(StringBuilder sb)
225 PrintStream ps = new PrintStream(System.out)
228 public void print(String x)
234 public void println()
243 * Answers the N dimensions of the NxN PCA matrix. This is the number of
244 * sequences involved in the pairwise score calculation.
248 public int getHeight()
250 // TODO can any of seqs[] be null?
251 return seqs.getSequences().length;