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.Cache;
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, SimilarityParamsI options)
65 this.seqs = sequences;
67 this.similarityParams = options;
74 * Index of diagonal within matrix
76 * @return Returns value of diagonal from matrix
78 public double getEigenvalue(int i)
80 return eigenMatrix.getD()[i];
95 * @return DOCUMENT ME!
97 public Point[] getComponents(int l, int n, int mm, float factor)
99 Point[] out = new Point[getHeight()];
101 for (int i = 0; i < getHeight(); i++)
103 float x = (float) component(i, l) * factor;
104 float y = (float) component(i, n) * factor;
105 float z = (float) component(i, mm) * factor;
106 out[i] = new Point(x, y, z);
118 * @return DOCUMENT ME!
120 public double[] component(int n)
122 // n = index of eigenvector
123 double[] out = new double[getHeight()];
125 for (int i = 0; i < out.length; i++)
127 out[i] = component(i, n);
141 * @return DOCUMENT ME!
143 double component(int row, int n)
147 for (int i = 0; i < pairwiseScores.width(); i++)
149 out += (pairwiseScores.getValue(row, i) * eigenMatrix.getValue(i, n));
152 return out / eigenMatrix.getD()[n];
156 * Answers a formatted text report of the PCA calculation results (matrices
157 * and eigenvalues) suitable for display
161 public String getDetails()
163 StringBuilder sb = new StringBuilder(1024);
164 sb.append("PCA calculation using ").append(scoreModel.getName())
165 .append(" sequence similarity matrix\n========\n\n");
166 PrintStream ps = wrapOutputBuffer(sb);
169 * pairwise similarity scores
171 sb.append(" --- OrigT * Orig ---- \n");
172 pairwiseScores.print(ps, "%8.2f");
175 * tridiagonal matrix, with D and E vectors
177 sb.append(" ---Tridiag transform matrix ---\n");
178 sb.append(" --- D vector ---\n");
179 tridiagonal.printD(ps, "%15.4e");
181 sb.append("--- E vector ---\n");
182 tridiagonal.printE(ps, "%15.4e");
186 * eigenvalues matrix, with D vector
188 sb.append(" --- New diagonalization matrix ---\n");
189 eigenMatrix.print(ps, "%8.2f");
190 sb.append(" --- Eigenvalues ---\n");
191 eigenMatrix.printD(ps, "%15.4e");
194 return sb.toString();
198 * Performs the PCA calculation
206 * sequence pairwise similarity scores
208 pairwiseScores = scoreModel.findSimilarities(seqs, similarityParams);
213 tridiagonal = pairwiseScores.copy();
217 * the diagonalization matrix
219 eigenMatrix = tridiagonal.copy();
221 } catch (Exception q)
223 Cache.log.error("Error computing PCA: " + q.getMessage());
229 * Returns a PrintStream that wraps (appends its output to) the given
235 protected PrintStream wrapOutputBuffer(StringBuilder sb)
237 PrintStream ps = new PrintStream(System.out)
240 public void print(String x)
246 public void println()
255 * Answers the N dimensions of the NxN PCA matrix. This is the number of
256 * sequences involved in the pairwise score calculation.
260 public int getHeight()
262 // TODO can any of seqs[] be null?
263 return pairwiseScores.height();// seqs.getSequences().length;
267 * Answers the sequence pairwise similarity scores which were the first step
268 * of the PCA calculation
272 public MatrixI getPairwiseScores()
274 return pairwiseScores;
277 public void setPairwiseScores(MatrixI m)
282 public MatrixI getEigenmatrix()
287 public void setEigenmatrix(MatrixI m)
292 public MatrixI getTridiagonal()
297 public void setTridiagonal(MatrixI tridiagonal)
299 this.tridiagonal = tridiagonal;