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;
50 private MatrixI eigenvector;
52 private String details;
55 * Constructor given the sequences to compute for, the similarity model to
56 * use, and a set of parameters for sequence comparison
62 public PCA(AlignmentView sequences, ScoreModelI sm, SimilarityParamsI options)
64 this.seqs = sequences;
66 this.similarityParams = options;
73 * Index of diagonal within matrix
75 * @return Returns value of diagonal from matrix
77 public double getEigenvalue(int i)
79 return eigenvector.getD()[i];
94 * @return DOCUMENT ME!
96 public Point[] getComponents(int l, int n, int mm, float factor)
98 Point[] out = new Point[getHeight()];
100 for (int i = 0; i < getHeight(); i++)
102 float x = (float) component(i, l) * factor;
103 float y = (float) component(i, n) * factor;
104 float z = (float) component(i, mm) * factor;
105 out[i] = new Point(x, y, z);
117 * @return DOCUMENT ME!
119 public double[] component(int n)
121 // n = index of eigenvector
122 double[] out = new double[getHeight()];
124 for (int i = 0; i < out.length; i++)
126 out[i] = component(i, n);
140 * @return DOCUMENT ME!
142 double component(int row, int n)
146 for (int i = 0; i < symm.width(); i++)
148 out += (symm.getValue(row, i) * eigenvector.getValue(i, n));
151 return out / eigenvector.getD()[n];
155 * Answers a formatted text report of the PCA calculation results (matrices
156 * and eigenvalues) suitable for display
160 public String getDetails()
166 * Performs the PCA calculation
172 * print details to a string buffer as they are computed
174 StringBuilder sb = new StringBuilder(1024);
175 sb.append("PCA calculation using ").append(scoreModel.getName())
176 .append(" sequence similarity matrix\n========\n\n");
177 PrintStream ps = wrapOutputBuffer(sb);
181 eigenvector = scoreModel.findSimilarities(seqs, similarityParams);
183 sb.append(" --- OrigT * Orig ---- \n");
184 eigenvector.print(ps, "%8.2f");
186 symm = eigenvector.copy();
190 sb.append(" ---Tridiag transform matrix ---\n");
191 sb.append(" --- D vector ---\n");
192 eigenvector.printD(ps, "%15.4e");
194 sb.append("--- E vector ---\n");
195 eigenvector.printE(ps, "%15.4e");
198 // Now produce the diagonalization matrix
200 } catch (Exception q)
203 sb.append("\n*** Unexpected exception when performing PCA ***\n"
204 + q.getLocalizedMessage());
206 "*** Matrices below may not be fully diagonalised. ***\n");
209 sb.append(" --- New diagonalization matrix ---\n");
210 eigenvector.print(ps, "%8.2f");
211 sb.append(" --- Eigenvalues ---\n");
212 eigenvector.printD(ps, "%15.4e");
215 details = sb.toString();
219 * Returns a PrintStream that wraps (appends its output to) the given
225 protected PrintStream wrapOutputBuffer(StringBuilder sb)
227 PrintStream ps = new PrintStream(System.out)
230 public void print(String x)
236 public void println()
245 * Answers the N dimensions of the NxN PCA matrix. This is the number of
246 * sequences involved in the pairwise score calculation.
250 public int getHeight()
252 // TODO can any of seqs[] be null?
253 return seqs.getSequences().length;