2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3 * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, 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. By default, uses blosum62 matrix to generate
50 * sequence similarity matrices
53 * Set of amino acid sequences to perform PCA on
55 public PCA(String[] s)
61 * Creates a new PCA object. By default, uses blosum62 matrix to generate
62 * sequence similarity matrices
65 * Set of sequences to perform PCA on
67 * if true, uses standard DNA/RNA matrix for sequence similarity
70 public PCA(String[] s, boolean nucleotides)
73 BinarySequence[] bs = new BinarySequence[s.length];
76 while ((ii < s.length) && (s[ii] != null))
78 bs[ii] = new BinarySequence(s[ii], nucleotides);
83 BinarySequence[] bs2 = new BinarySequence[s.length];
86 String sm = nucleotides ? "DNA" : "BLOSUM62";
87 ScoreMatrix smtrx = ResidueProperties.getScoreMatrix(sm);
88 details.append("PCA calculation using " + sm
89 + " sequence similarity matrix\n========\n\n");
90 while ((ii < s.length) && (s[ii] != null))
92 bs2[ii] = new BinarySequence(s[ii], nucleotides);
97 bs2[ii].matrixEncode(smtrx);
98 } catch (InvalidSequenceTypeException x)
100 details.append("Unexpected mismatch of sequence type and score matrix. Calculation will not be valid!\n\n");
106 // System.out.println("Created binary encoding");
110 while ((count < bs.length) && (bs[count] != null))
115 double[][] seqmat = new double[count][bs[0].getDBinary().length];
116 double[][] seqmat2 = new double[count][bs2[0].getDBinary().length];
121 seqmat[i] = bs[i].getDBinary();
122 seqmat2[i] = bs2[i].getDBinary();
126 // System.out.println("Created array");
128 // System.out.println(" --- Original matrix ---- ");
129 m = new Matrix(seqmat, count, bs[0].getDBinary().length);
130 m2 = new Matrix(seqmat2, count, bs2[0].getDBinary().length);
135 * Returns the matrix used in PCA calculation
137 * @return java.math.Matrix object
149 * Index of diagonal within matrix
151 * @return Returns value of diagonal from matrix
153 public double getEigenvalue(int i)
155 return eigenvector.d[i];
170 * @return DOCUMENT ME!
172 public float[][] getComponents(int l, int n, int mm, float factor)
174 float[][] out = new float[m.rows][3];
176 for (int i = 0; i < m.rows; i++)
178 out[i][0] = (float) component(i, l) * factor;
179 out[i][1] = (float) component(i, n) * factor;
180 out[i][2] = (float) component(i, mm) * factor;
192 * @return DOCUMENT ME!
194 public double[] component(int n)
196 // n = index of eigenvector
197 double[] out = new double[m.rows];
199 for (int i = 0; i < m.rows; i++)
201 out[i] = component(i, n);
215 * @return DOCUMENT ME!
217 double component(int row, int n)
221 for (int i = 0; i < symm.cols; i++)
223 out += (symm.value[row][i] * eigenvector.value[i][n]);
226 return out / eigenvector.d[n];
229 public String getDetails()
231 return details.toString();
239 details.append("PCA Calculation Mode is "
240 + (jvCalcMode ? "Jalview variant" : "Original SeqSpace") + "\n");
241 Matrix mt = m.transpose();
243 details.append(" --- OrigT * Orig ---- \n");
246 eigenvector = mt.preMultiply(m); // standard seqspace comparison matrix
250 eigenvector = mt.preMultiply(m2); // jalview variation on seqsmace method
253 PrintStream ps = new PrintStream(System.out)
255 public void print(String x)
260 public void println()
262 details.append("\n");
266 eigenvector.print(ps);
268 symm = eigenvector.copy();
272 details.append(" ---Tridiag transform matrix ---\n");
273 details.append(" --- D vector ---\n");
274 eigenvector.printD(ps);
276 details.append("--- E vector ---\n");
277 eigenvector.printE(ps);
280 // Now produce the diagonalization matrix
283 details.append(" --- New diagonalization matrix ---\n");
284 eigenvector.print(ps);
285 details.append(" --- Eigenvalues ---\n");
286 eigenvector.printD(ps);
289 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
290 * (int ev=0;ev<symm.rows; ev++) {
292 * ps.print(","+component(seq, ev)); } ps.println(); }
296 boolean jvCalcMode = true;
298 public void setJvCalcMode(boolean calcMode)
300 this.jvCalcMode = calcMode;