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
41 StringBuilder details = new StringBuilder(1024);
43 final private AlignmentView seqs;
45 private ScoreModelI scoreModel;
47 private SimilarityParamsI similarityParams;
49 public PCA(AlignmentView s, ScoreModelI sm, SimilarityParamsI options)
52 this.similarityParams = options;
55 details.append("PCA calculation using " + sm.getName()
56 + " sequence similarity matrix\n========\n\n");
63 * Index of diagonal within matrix
65 * @return Returns value of diagonal from matrix
67 public double getEigenvalue(int i)
69 return eigenvector.getD()[i];
84 * @return DOCUMENT ME!
86 public float[][] getComponents(int l, int n, int mm, float factor)
88 float[][] out = new float[getHeight()][3];
90 for (int i = 0; i < getHeight(); i++)
92 out[i][0] = (float) component(i, l) * factor;
93 out[i][1] = (float) component(i, n) * factor;
94 out[i][2] = (float) component(i, mm) * factor;
106 * @return DOCUMENT ME!
108 public double[] component(int n)
110 // n = index of eigenvector
111 double[] out = new double[getHeight()];
113 for (int i = 0; i < out.length; i++)
115 out[i] = component(i, n);
129 * @return DOCUMENT ME!
131 double component(int row, int n)
135 for (int i = 0; i < symm.width(); i++)
137 out += (symm.getValue(row, i) * eigenvector.getValue(i, n));
140 return out / eigenvector.getD()[n];
143 public String getDetails()
145 return details.toString();
154 PrintStream ps = new PrintStream(System.out)
157 public void print(String x)
163 public void println()
165 details.append("\n");
169 // long now = System.currentTimeMillis();
172 eigenvector = scoreModel.findSimilarities(seqs, similarityParams);
174 details.append(" --- OrigT * Orig ---- \n");
175 eigenvector.print(ps, "%8.2f");
177 symm = eigenvector.copy();
181 details.append(" ---Tridiag transform matrix ---\n");
182 details.append(" --- D vector ---\n");
183 eigenvector.printD(ps, "%15.4e");
185 details.append("--- E vector ---\n");
186 eigenvector.printE(ps, "%15.4e");
189 // Now produce the diagonalization matrix
191 } catch (Exception q)
194 details.append("\n*** Unexpected exception when performing PCA ***\n"
195 + q.getLocalizedMessage());
197 "*** Matrices below may not be fully diagonalised. ***\n");
200 details.append(" --- New diagonalization matrix ---\n");
201 eigenvector.print(ps, "%8.2f");
202 details.append(" --- Eigenvalues ---\n");
203 eigenvector.printD(ps, "%15.4e");
206 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
207 * (int ev=0;ev<symm.rows; ev++) {
209 * ps.print(","+component(seq, ev)); } ps.println(); }
211 // System.out.println(("PCA.run() took "
212 // + (System.currentTimeMillis() - now) + "ms"));
216 * Answers the N dimensions of the NxN PCA matrix. This is the number of
217 * sequences involved in the pairwise score calculation.
221 public int getHeight()
223 // TODO can any of seqs[] be null?
224 return seqs.getSequences().length;