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.SimilarityParamsI;
26 import jalview.api.analysis.SimilarityScoreModelI;
27 import jalview.datamodel.AlignmentView;
28 import jalview.math.MatrixI;
30 import java.io.PrintStream;
33 * Performs Principal Component Analysis on given sequences
35 public class PCA implements Runnable
37 boolean jvCalcMode = true;
45 StringBuilder details = new StringBuilder(1024);
47 private AlignmentView seqs;
49 private ScoreModelI scoreModel;
51 private SimilarityParamsI similarityParams;
53 public PCA(AlignmentView s, ScoreModelI sm, SimilarityParamsI options)
56 this.similarityParams = options;
59 details.append("PCA calculation using " + sm.getName()
60 + " sequence similarity matrix\n========\n\n");
67 * Index of diagonal within matrix
69 * @return Returns value of diagonal from matrix
71 public double getEigenvalue(int i)
73 return eigenvector.getD()[i];
88 * @return DOCUMENT ME!
90 public float[][] getComponents(int l, int n, int mm, float factor)
92 float[][] out = new float[getHeight()][3];
94 for (int i = 0; i < getHeight(); i++)
96 out[i][0] = (float) component(i, l) * factor;
97 out[i][1] = (float) component(i, n) * factor;
98 out[i][2] = (float) component(i, mm) * factor;
110 * @return DOCUMENT ME!
112 public double[] component(int n)
114 // n = index of eigenvector
115 double[] out = new double[getHeight()];
117 for (int i = 0; i < out.length; i++)
119 out[i] = component(i, n);
133 * @return DOCUMENT ME!
135 double component(int row, int n)
139 for (int i = 0; i < symm.width(); i++)
141 out += (symm.getValue(row, i) * eigenvector.getValue(i, n));
144 return out / eigenvector.getD()[n];
147 public String getDetails()
149 return details.toString();
158 PrintStream ps = new PrintStream(System.out)
161 public void print(String x)
167 public void println()
169 details.append("\n");
173 // long now = System.currentTimeMillis();
176 details.append("PCA Calculation Mode is "
177 + (jvCalcMode ? "Jalview variant" : "Original SeqSpace")
180 eigenvector = computeSimilarity(seqs);
182 details.append(" --- OrigT * Orig ---- \n");
183 eigenvector.print(ps, "%8.2f");
185 symm = eigenvector.copy();
189 details.append(" ---Tridiag transform matrix ---\n");
190 details.append(" --- D vector ---\n");
191 eigenvector.printD(ps, "%15.4e");
193 details.append("--- E vector ---\n");
194 eigenvector.printE(ps, "%15.4e");
197 // Now produce the diagonalization matrix
199 } catch (Exception q)
202 details.append("\n*** Unexpected exception when performing PCA ***\n"
203 + q.getLocalizedMessage());
204 details.append("*** Matrices below may not be fully diagonalised. ***\n");
207 details.append(" --- New diagonalization matrix ---\n");
208 eigenvector.print(ps, "%8.2f");
209 details.append(" --- Eigenvalues ---\n");
210 eigenvector.printD(ps, "%15.4e");
213 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
214 * (int ev=0;ev<symm.rows; ev++) {
216 * ps.print(","+component(seq, ev)); } ps.println(); }
218 // System.out.println(("PCA.run() took "
219 // + (System.currentTimeMillis() - now) + "ms"));
223 * Computes a pairwise similarity matrix for the given sequence regions using
224 * the configured score model. If the score model is a similarity model, then
225 * it computes the result directly. If it is a distance model, then use it to
226 * compute pairwise distances, and convert these to similarity scores by
227 * substracting from the maximum value.
232 MatrixI computeSimilarity(AlignmentView av)
234 MatrixI result = null;
235 if (scoreModel instanceof SimilarityScoreModelI)
237 result = ((SimilarityScoreModelI) scoreModel).findSimilarities(av,
240 else if (scoreModel instanceof DistanceScoreModelI)
242 result = ((DistanceScoreModelI) scoreModel).findDistances(av,
244 result.reverseRange(false);
249 .println("Unexpected type of score model, cannot calculate similarity");
255 public void setJvCalcMode(boolean calcMode)
257 this.jvCalcMode = calcMode;
261 * Answers the N dimensions of the NxN PCA matrix. This is the number of
262 * sequences involved in the pairwise score calculation.
266 public int getHeight()
268 // TODO can any of seqs[] be null?
269 return seqs.getSequences().length;