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.analysis.scoremodels.PIDModel;
24 import jalview.api.analysis.DistanceScoreModelI;
25 import jalview.api.analysis.ScoreModelI;
26 import jalview.api.analysis.SimilarityParamsI;
27 import jalview.api.analysis.SimilarityScoreModelI;
28 import jalview.datamodel.AlignmentView;
29 import jalview.math.MatrixI;
31 import java.io.PrintStream;
34 * Performs Principal Component Analysis on given sequences
36 public class PCA implements Runnable
38 boolean jvCalcMode = true;
46 StringBuilder details = new StringBuilder(1024);
48 private AlignmentView seqs;
50 private ScoreModelI scoreModel;
52 private SimilarityParamsI similarityParams;
54 public PCA(AlignmentView s, ScoreModelI sm, SimilarityParamsI options)
57 this.similarityParams = options;
60 details.append("PCA calculation using " + sm.getName()
61 + " sequence similarity matrix\n========\n\n");
68 * Index of diagonal within matrix
70 * @return Returns value of diagonal from matrix
72 public double getEigenvalue(int i)
74 return eigenvector.getD()[i];
89 * @return DOCUMENT ME!
91 public float[][] getComponents(int l, int n, int mm, float factor)
93 float[][] out = new float[getHeight()][3];
95 for (int i = 0; i < getHeight(); i++)
97 out[i][0] = (float) component(i, l) * factor;
98 out[i][1] = (float) component(i, n) * factor;
99 out[i][2] = (float) component(i, mm) * factor;
111 * @return DOCUMENT ME!
113 public double[] component(int n)
115 // n = index of eigenvector
116 double[] out = new double[getHeight()];
118 for (int i = 0; i < out.length; i++)
120 out[i] = component(i, n);
134 * @return DOCUMENT ME!
136 double component(int row, int n)
140 for (int i = 0; i < symm.width(); i++)
142 out += (symm.getValue(row, i) * eigenvector.getValue(i, n));
145 return out / eigenvector.getD()[n];
148 public String getDetails()
150 return details.toString();
159 PrintStream ps = new PrintStream(System.out)
162 public void print(String x)
168 public void println()
170 details.append("\n");
174 // long now = System.currentTimeMillis();
177 details.append("PCA Calculation Mode is "
178 + (jvCalcMode ? "Jalview variant" : "Original SeqSpace")
181 eigenvector = computeSimilarity(seqs);
183 details.append(" --- OrigT * Orig ---- \n");
184 eigenvector.print(ps, "%8.2f");
186 symm = eigenvector.copy();
190 details.append(" ---Tridiag transform matrix ---\n");
191 details.append(" --- D vector ---\n");
192 eigenvector.printD(ps, "%15.4e");
194 details.append("--- E vector ---\n");
195 eigenvector.printE(ps, "%15.4e");
198 // Now produce the diagonalization matrix
200 } catch (Exception q)
203 details.append("\n*** Unexpected exception when performing PCA ***\n"
204 + q.getLocalizedMessage());
205 details.append("*** Matrices below may not be fully diagonalised. ***\n");
208 details.append(" --- New diagonalization matrix ---\n");
209 eigenvector.print(ps, "%8.2f");
210 details.append(" --- Eigenvalues ---\n");
211 eigenvector.printD(ps, "%15.4e");
214 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
215 * (int ev=0;ev<symm.rows; ev++) {
217 * ps.print(","+component(seq, ev)); } ps.println(); }
219 // System.out.println(("PCA.run() took "
220 // + (System.currentTimeMillis() - now) + "ms"));
224 * Computes a pairwise similarity matrix for the given sequence regions using
225 * the configured score model. If the score model is a similarity model, then
226 * it computes the result directly. If it is a distance model, then use it to
227 * compute pairwise distances, and convert these to similarity scores.
232 MatrixI computeSimilarity(AlignmentView av)
234 MatrixI result = null;
235 if (scoreModel instanceof SimilarityScoreModelI)
237 result = ((SimilarityScoreModelI) scoreModel).findSimilarities(av,
239 if (scoreModel instanceof PIDModel)
242 * scale % identities to width of alignment for backwards
243 * compatibility with Jalview 2.10.1 SeqSpace PCA calculation
245 result.multiply(av.getWidth() / 100d);
248 else if (scoreModel instanceof DistanceScoreModelI)
251 * find distances and convert to similarity scores
252 * reverseRange(false) preserves but reverses the min-max range
254 result = ((DistanceScoreModelI) scoreModel).findDistances(av,
256 result.reverseRange(false);
261 .println("Unexpected type of score model, cannot calculate similarity");
267 public void setJvCalcMode(boolean calcMode)
269 this.jvCalcMode = calcMode;
273 * Answers the N dimensions of the NxN PCA matrix. This is the number of
274 * sequences involved in the pairwise score calculation.
278 public int getHeight()
280 // TODO can any of seqs[] be null?
281 return seqs.getSequences().length;