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.DistanceScoreModelI;
24 import jalview.api.analysis.ScoreModelI;
25 import jalview.api.analysis.SimilarityScoreModelI;
26 import jalview.datamodel.AlignmentView;
27 import jalview.math.MatrixI;
29 import java.io.PrintStream;
32 * Performs Principal Component Analysis on given sequences
34 public class PCA implements Runnable
36 boolean jvCalcMode = true;
44 StringBuilder details = new StringBuilder(1024);
46 private AlignmentView seqs;
48 private ScoreModelI scoreModel;
50 public PCA(AlignmentView s, ScoreModelI sm)
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 details.append("PCA Calculation Mode is "
173 + (jvCalcMode ? "Jalview variant" : "Original SeqSpace")
176 eigenvector = computeSimilarity(seqs);
178 details.append(" --- OrigT * Orig ---- \n");
179 eigenvector.print(ps, "%8.2f");
181 symm = eigenvector.copy();
185 details.append(" ---Tridiag transform matrix ---\n");
186 details.append(" --- D vector ---\n");
187 eigenvector.printD(ps, "%15.4e");
189 details.append("--- E vector ---\n");
190 eigenvector.printE(ps, "%15.4e");
193 // Now produce the diagonalization matrix
195 } catch (Exception q)
198 details.append("\n*** Unexpected exception when performing PCA ***\n"
199 + q.getLocalizedMessage());
200 details.append("*** Matrices below may not be fully diagonalised. ***\n");
203 details.append(" --- New diagonalization matrix ---\n");
204 eigenvector.print(ps, "%8.2f");
205 details.append(" --- Eigenvalues ---\n");
206 eigenvector.printD(ps, "%15.4e");
209 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
210 * (int ev=0;ev<symm.rows; ev++) {
212 * ps.print(","+component(seq, ev)); } ps.println(); }
214 // System.out.println(("PCA.run() took "
215 // + (System.currentTimeMillis() - now) + "ms"));
219 * Computes a pairwise similarity matrix for the given sequence regions using
220 * the configured score model. If the score model is a similarity model, then
221 * it computes the result directly. If it is a distance model, then use it to
222 * compute pairwise distances, and convert these to similarity scores by
223 * substracting from the maximum value.
228 MatrixI computeSimilarity(AlignmentView av)
230 MatrixI result = null;
231 if (scoreModel instanceof SimilarityScoreModelI)
233 result = ((SimilarityScoreModelI) scoreModel).findSimilarities(av);
235 else if (scoreModel instanceof DistanceScoreModelI)
237 result = ((DistanceScoreModelI) scoreModel).findDistances(av);
238 result.reverseRange(false);
243 .println("Unexpected type of score model, cannot calculate similarity");
249 public void setJvCalcMode(boolean calcMode)
251 this.jvCalcMode = calcMode;
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 seqs.getSequences().length;