2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
18 package jalview.analysis;
22 import jalview.datamodel.*;
23 import jalview.datamodel.BinarySequence.InvalidSequenceTypeException;
24 import jalview.math.*;
25 import jalview.schemes.ResidueProperties;
26 import jalview.schemes.ScoreMatrix;
29 * Performs Principal Component Analysis on given sequences
34 public class PCA implements Runnable
46 StringBuffer details = new StringBuffer();
49 * Creates a new PCA object.
50 * By default, uses blosum62 matrix to generate sequence similarity matrices
52 * Set of amino acid sequences to perform PCA on
54 public PCA(String[] s)
60 * Creates a new PCA object.
61 * By default, uses blosum62 matrix to generate sequence similarity matrices
63 * Set of sequences to perform PCA on
64 * @param nucleotides if true, uses standard DNA/RNA matrix for sequence similarity calculation.
66 public PCA(String[] s, boolean nucleotides)
69 BinarySequence[] bs = new BinarySequence[s.length];
72 while ((ii < s.length) && (s[ii] != null))
74 bs[ii] = new BinarySequence(s[ii],nucleotides);
79 BinarySequence[] bs2 = new BinarySequence[s.length];
82 String sm=nucleotides ? "DNA" : "BLOSUM62";
83 ScoreMatrix smtrx=ResidueProperties.getScoreMatrix(sm);
84 details.append("PCA calculation using "+sm+" sequence similarity matrix\n========\n\n");
86 while ((ii < s.length) && (s[ii] != null))
88 bs2[ii] = new BinarySequence(s[ii], nucleotides);
93 bs2[ii].matrixEncode(smtrx);
94 } catch (InvalidSequenceTypeException x)
96 details.append("Unexpected mismatch of sequence type and score matrix. Calculation will not be valid!\n\n");
102 // System.out.println("Created binary encoding");
106 while ((count < bs.length) && (bs[count] != null))
111 double[][] seqmat = new double[count][bs[0].getDBinary().length];
112 double[][] seqmat2 = new double[count][bs2[0].getDBinary().length];
117 seqmat[i] = bs[i].getDBinary();
118 seqmat2[i] = bs2[i].getDBinary();
122 // System.out.println("Created array");
124 // System.out.println(" --- Original matrix ---- ");
125 m = new Matrix(seqmat, count, bs[0].getDBinary().length);
126 m2 = new Matrix(seqmat2, count, bs2[0].getDBinary().length);
131 * Returns the matrix used in PCA calculation
133 * @return java.math.Matrix object
145 * Index of diagonal within matrix
147 * @return Returns value of diagonal from matrix
149 public double getEigenvalue(int i)
151 return eigenvector.d[i];
166 * @return DOCUMENT ME!
168 public float[][] getComponents(int l, int n, int mm, float factor)
170 float[][] out = new float[m.rows][3];
172 for (int i = 0; i < m.rows; i++)
174 out[i][0] = (float) component(i, l) * factor;
175 out[i][1] = (float) component(i, n) * factor;
176 out[i][2] = (float) component(i, mm) * factor;
188 * @return DOCUMENT ME!
190 public double[] component(int n)
192 // n = index of eigenvector
193 double[] out = new double[m.rows];
195 for (int i = 0; i < m.rows; i++)
197 out[i] = component(i, n);
211 * @return DOCUMENT ME!
213 double component(int row, int n)
217 for (int i = 0; i < symm.cols; i++)
219 out += (symm.value[row][i] * eigenvector.value[i][n]);
222 return out / eigenvector.d[n];
225 public String getDetails()
227 return details.toString();
235 details.append("PCA Calculation Mode is "+(jvCalcMode ? "Jalview variant" : "Original SeqSpace")+"\n");
236 Matrix mt = m.transpose();
238 details.append(" --- OrigT * Orig ---- \n");
241 eigenvector = mt.preMultiply(m); // standard seqspace comparison matrix
245 eigenvector = mt.preMultiply(m2); // jalview variation on seqsmace method
248 PrintStream ps = new PrintStream(System.out)
250 public void print(String x)
255 public void println()
257 details.append("\n");
261 eigenvector.print(ps);
263 symm = eigenvector.copy();
267 details.append(" ---Tridiag transform matrix ---\n");
268 details.append(" --- D vector ---\n");
269 eigenvector.printD(ps);
271 details.append("--- E vector ---\n");
272 eigenvector.printE(ps);
275 // Now produce the diagonalization matrix
278 details.append(" --- New diagonalization matrix ---\n");
279 eigenvector.print(ps);
280 details.append(" --- Eigenvalues ---\n");
281 eigenvector.printD(ps);
284 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
285 * (int ev=0;ev<symm.rows; ev++) {
287 * ps.print(","+component(seq, ev)); } ps.println(); }
291 boolean jvCalcMode = true;
293 public void setJvCalcMode(boolean calcMode)
295 this.jvCalcMode = calcMode;