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
44 StringBuilder details = new StringBuilder(1024);
46 final private AlignmentView seqs;
48 private ScoreModelI scoreModel;
50 private SimilarityParamsI similarityParams;
52 public PCA(AlignmentView s, ScoreModelI sm, SimilarityParamsI options)
55 this.similarityParams = options;
58 details.append("PCA calculation using " + sm.getName()
59 + " sequence similarity matrix\n========\n\n");
66 * Index of diagonal within matrix
68 * @return Returns value of diagonal from matrix
70 public double getEigenvalue(int i)
72 return eigenvector.getD()[i];
87 * @return DOCUMENT ME!
89 public float[][] getComponents(int l, int n, int mm, float factor)
91 float[][] out = new float[getHeight()][3];
93 for (int i = 0; i < getHeight(); i++)
95 out[i][0] = (float) component(i, l) * factor;
96 out[i][1] = (float) component(i, n) * factor;
97 out[i][2] = (float) component(i, mm) * factor;
109 * @return DOCUMENT ME!
111 public double[] component(int n)
113 // n = index of eigenvector
114 double[] out = new double[getHeight()];
116 for (int i = 0; i < out.length; i++)
118 out[i] = component(i, n);
132 * @return DOCUMENT ME!
134 double component(int row, int n)
138 for (int i = 0; i < symm.width(); i++)
140 out += (symm.getValue(row, i) * eigenvector.getValue(i, n));
143 return out / eigenvector.getD()[n];
146 public String getDetails()
148 return details.toString();
157 PrintStream ps = new PrintStream(System.out)
160 public void print(String x)
166 public void println()
168 details.append("\n");
172 // long now = System.currentTimeMillis();
175 eigenvector = computeSimilarity();
177 details.append(" --- OrigT * Orig ---- \n");
178 eigenvector.print(ps, "%8.2f");
180 symm = eigenvector.copy();
184 details.append(" ---Tridiag transform matrix ---\n");
185 details.append(" --- D vector ---\n");
186 eigenvector.printD(ps, "%15.4e");
188 details.append("--- E vector ---\n");
189 eigenvector.printE(ps, "%15.4e");
192 // Now produce the diagonalization matrix
194 } catch (Exception q)
197 details.append("\n*** Unexpected exception when performing PCA ***\n"
198 + q.getLocalizedMessage());
199 details.append("*** Matrices below may not be fully diagonalised. ***\n");
202 details.append(" --- New diagonalization matrix ---\n");
203 eigenvector.print(ps, "%8.2f");
204 details.append(" --- Eigenvalues ---\n");
205 eigenvector.printD(ps, "%15.4e");
208 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
209 * (int ev=0;ev<symm.rows; ev++) {
211 * ps.print(","+component(seq, ev)); } ps.println(); }
213 // System.out.println(("PCA.run() took "
214 // + (System.currentTimeMillis() - now) + "ms"));
218 * Computes a pairwise similarity matrix for the given sequence regions using
219 * the configured score model. If the score model is a similarity model, then
220 * it computes the result directly. If it is a distance model, then use it to
221 * compute pairwise distances, and convert these to similarity scores.
226 MatrixI computeSimilarity()
228 MatrixI result = null;
229 if (scoreModel instanceof SimilarityScoreModelI)
231 result = ((SimilarityScoreModelI) scoreModel).findSimilarities(seqs,
233 if (scoreModel instanceof PIDModel)
236 * scale score to width of alignment for backwards
237 * compatibility with Jalview 2.10.1 SeqSpace PCA calculation
239 result.multiply(seqs.getWidth() / 100d);
242 else if (scoreModel instanceof DistanceScoreModelI)
245 * find distances and convert to similarity scores
246 * reverseRange(false) preserves but reverses the min-max range
248 result = ((DistanceScoreModelI) scoreModel).findDistances(seqs,
250 result.reverseRange(false);
255 .println("Unexpected type of score model, cannot calculate similarity");
262 * Answers the N dimensions of the NxN PCA matrix. This is the number of
263 * sequences involved in the pairwise score calculation.
267 public int getHeight()
269 // TODO can any of seqs[] be null?
270 return seqs.getSequences().length;