2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3 * Copyright (C) 2014 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 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/>.
17 * The Jalview Authors are detailed in the 'AUTHORS' file.
19 package jalview.analysis;
23 import jalview.datamodel.*;
24 import jalview.datamodel.BinarySequence.InvalidSequenceTypeException;
25 import jalview.math.*;
26 import jalview.schemes.ResidueProperties;
27 import jalview.schemes.ScoreMatrix;
30 * Performs Principal Component Analysis on given sequences
35 public class PCA implements Runnable
47 StringBuffer details = new StringBuffer();
50 * Creates a new PCA object. By default, uses blosum62 matrix to generate
51 * sequence similarity matrices
54 * Set of amino acid sequences to perform PCA on
56 public PCA(String[] s)
62 * Creates a new PCA object. By default, uses blosum62 matrix to generate
63 * sequence similarity matrices
66 * Set of sequences to perform PCA on
68 * if true, uses standard DNA/RNA matrix for sequence similarity
71 public PCA(String[] s, boolean nucleotides)
74 BinarySequence[] bs = new BinarySequence[s.length];
77 while ((ii < s.length) && (s[ii] != null))
79 bs[ii] = new BinarySequence(s[ii], nucleotides);
84 BinarySequence[] bs2 = new BinarySequence[s.length];
87 String sm = nucleotides ? "DNA" : "BLOSUM62";
88 ScoreMatrix smtrx = ResidueProperties.getScoreMatrix(sm);
89 details.append("PCA calculation using " + sm
90 + " sequence similarity matrix\n========\n\n");
91 while ((ii < s.length) && (s[ii] != null))
93 bs2[ii] = new BinarySequence(s[ii], nucleotides);
98 bs2[ii].matrixEncode(smtrx);
99 } catch (InvalidSequenceTypeException x)
101 details.append("Unexpected mismatch of sequence type and score matrix. Calculation will not be valid!\n\n");
107 // System.out.println("Created binary encoding");
111 while ((count < bs.length) && (bs[count] != null))
116 double[][] seqmat = new double[count][bs[0].getDBinary().length];
117 double[][] seqmat2 = new double[count][bs2[0].getDBinary().length];
122 seqmat[i] = bs[i].getDBinary();
123 seqmat2[i] = bs2[i].getDBinary();
127 // System.out.println("Created array");
129 // System.out.println(" --- Original matrix ---- ");
130 m = new Matrix(seqmat, count, bs[0].getDBinary().length);
131 m2 = new Matrix(seqmat2, count, bs2[0].getDBinary().length);
136 * Returns the matrix used in PCA calculation
138 * @return java.math.Matrix object
150 * Index of diagonal within matrix
152 * @return Returns value of diagonal from matrix
154 public double getEigenvalue(int i)
156 return eigenvector.d[i];
171 * @return DOCUMENT ME!
173 public float[][] getComponents(int l, int n, int mm, float factor)
175 float[][] out = new float[m.rows][3];
177 for (int i = 0; i < m.rows; i++)
179 out[i][0] = (float) component(i, l) * factor;
180 out[i][1] = (float) component(i, n) * factor;
181 out[i][2] = (float) component(i, mm) * factor;
193 * @return DOCUMENT ME!
195 public double[] component(int n)
197 // n = index of eigenvector
198 double[] out = new double[m.rows];
200 for (int i = 0; i < m.rows; i++)
202 out[i] = component(i, n);
216 * @return DOCUMENT ME!
218 double component(int row, int n)
222 for (int i = 0; i < symm.cols; i++)
224 out += (symm.value[row][i] * eigenvector.value[i][n]);
227 return out / eigenvector.d[n];
230 public String getDetails()
232 return details.toString();
240 details.append("PCA Calculation Mode is "
241 + (jvCalcMode ? "Jalview variant" : "Original SeqSpace") + "\n");
242 Matrix mt = m.transpose();
244 details.append(" --- OrigT * Orig ---- \n");
247 eigenvector = mt.preMultiply(m); // standard seqspace comparison matrix
251 eigenvector = mt.preMultiply(m2); // jalview variation on seqsmace method
254 PrintStream ps = new PrintStream(System.out)
256 public void print(String x)
261 public void println()
263 details.append("\n");
267 eigenvector.print(ps);
269 symm = eigenvector.copy();
273 details.append(" ---Tridiag transform matrix ---\n");
274 details.append(" --- D vector ---\n");
275 eigenvector.printD(ps);
277 details.append("--- E vector ---\n");
278 eigenvector.printE(ps);
281 // Now produce the diagonalization matrix
284 details.append(" --- New diagonalization matrix ---\n");
285 eigenvector.print(ps);
286 details.append(" --- Eigenvalues ---\n");
287 eigenvector.printD(ps);
290 * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
291 * (int ev=0;ev<symm.rows; ev++) {
293 * ps.print(","+component(seq, ev)); } ps.println(); }
297 boolean jvCalcMode = true;
299 public void setJvCalcMode(boolean calcMode)
301 this.jvCalcMode = calcMode;