*/
package jalview.analysis;
-import jalview.math.Matrix;
+import jalview.api.analysis.ScoreModelI;
+import jalview.api.analysis.SimilarityParamsI;
+import jalview.datamodel.AlignmentView;
import jalview.math.MatrixI;
-import jalview.schemes.ResidueProperties;
-import jalview.schemes.ScoreMatrix;
import java.io.PrintStream;
*/
public class PCA implements Runnable
{
- boolean jvCalcMode = true;
-
- MatrixI symm;
+ /*
+ * inputs
+ */
+ final private AlignmentView seqs;
- double[] eigenvalue;
+ final private ScoreModelI scoreModel;
- MatrixI eigenvector;
+ final private SimilarityParamsI similarityParams;
- StringBuilder details = new StringBuilder(1024);
+ /*
+ * outputs
+ */
+ private MatrixI symm;
- private String[] seqs;
+ private MatrixI eigenvector;
- private ScoreMatrix scoreMatrix;
+ private String details;
/**
- * Creates a new PCA object. By default, uses blosum62 matrix to generate
- * sequence similarity matrices
+ * Constructor given the sequences to compute for, the similarity model to
+ * use, and a set of parameters for sequence comparison
*
- * @param s
- * Set of amino acid sequences to perform PCA on
+ * @param sequences
+ * @param sm
+ * @param options
*/
- public PCA(String[] s)
+ public PCA(AlignmentView sequences, ScoreModelI sm, SimilarityParamsI options)
{
- this(s, false);
- }
-
- /**
- * Creates a new PCA object. By default, uses blosum62 matrix to generate
- * sequence similarity matrices
- *
- * @param s
- * Set of sequences to perform PCA on
- * @param nucleotides
- * if true, uses standard DNA/RNA matrix for sequence similarity
- * calculation.
- */
- public PCA(String[] s, boolean nucleotides)
- {
- this(s, nucleotides, null);
- }
-
- public PCA(String[] s, boolean nucleotides, String s_m)
- {
- this.seqs = s;
-
- // BinarySequence[] bs = new BinarySequence[s.length];
- // int ii = 0;
- //
- // while ((ii < s.length) && (s[ii] != null))
- // {
- // bs[ii] = new BinarySequence(s[ii], nucleotides);
- // bs[ii].encode();
- // ii++;
- // }
- //
- // BinarySequence[] bs2 = new BinarySequence[s.length];
- scoreMatrix = null;
- String sm = s_m;
- if (sm != null)
- {
- scoreMatrix = ResidueProperties.getScoreMatrix(sm);
- }
- if (scoreMatrix == null)
- {
- // either we were given a non-existent score matrix or a scoremodel that
- // isn't based on a pairwise symbol score matrix
- scoreMatrix = ResidueProperties
- .getScoreMatrix(sm = (nucleotides ? "DNA" : "BLOSUM62"));
- }
- details.append("PCA calculation using " + sm
- + " sequence similarity matrix\n========\n\n");
- // ii = 0;
- // while ((ii < s.length) && (s[ii] != null))
- // {
- // bs2[ii] = new BinarySequence(s[ii], nucleotides);
- // if (scoreMatrix != null)
- // {
- // try
- // {
- // bs2[ii].matrixEncode(scoreMatrix);
- // } catch (InvalidSequenceTypeException x)
- // {
- // details.append("Unexpected mismatch of sequence type and score matrix. Calculation will not be valid!\n\n");
- // }
- // }
- // ii++;
- // }
- //
- // int count = 0;
- // while ((count < bs.length) && (bs[count] != null))
- // {
- // count++;
- // }
- //
- // double[][] seqmat = new double[count][];
- // double[][] seqmat2 = new double[count][];
- //
- // int i = 0;
- // while (i < count)
- // {
- // seqmat[i] = bs[i].getDBinary();
- // seqmat2[i] = bs2[i].getDBinary();
- // i++;
- // }
- //
- // /*
- // * using a SparseMatrix to hold the encoded sequences matrix
- // * greatly speeds up matrix multiplication as these are mostly zero
- // */
- // m = new SparseMatrix(seqmat);
- // m2 = new Matrix(seqmat2);
-
+ this.seqs = sequences;
+ this.scoreModel = sm;
+ this.similarityParams = options;
}
/**
return out / eigenvector.getD()[n];
}
+ /**
+ * Answers a formatted text report of the PCA calculation results (matrices
+ * and eigenvalues) suitable for display
+ *
+ * @return
+ */
public String getDetails()
{
- return details.toString();
+ return details;
}
/**
- * DOCUMENT ME!
+ * Performs the PCA calculation
*/
@Override
public void run()
{
- PrintStream ps = new PrintStream(System.out)
- {
- @Override
- public void print(String x)
- {
- details.append(x);
- }
-
- @Override
- public void println()
- {
- details.append("\n");
- }
- };
+ /*
+ * print details to a string buffer as they are computed
+ */
+ StringBuilder sb = new StringBuilder(1024);
+ sb.append("PCA calculation using ").append(scoreModel.getName())
+ .append(" sequence similarity matrix\n========\n\n");
+ PrintStream ps = wrapOutputBuffer(sb);
- // long now = System.currentTimeMillis();
try
{
- details.append("PCA Calculation Mode is "
- + (jvCalcMode ? "Jalview variant" : "Original SeqSpace")
- + "\n");
-
- // MatrixI mt = m.transpose();
- // eigenvector = mt.preMultiply(jvCalcMode ? m2 : m);
- eigenvector = computePairwiseScores();
+ eigenvector = scoreModel.findSimilarities(seqs, similarityParams);
- details.append(" --- OrigT * Orig ---- \n");
+ sb.append(" --- OrigT * Orig ---- \n");
eigenvector.print(ps, "%8.2f");
symm = eigenvector.copy();
eigenvector.tred();
- details.append(" ---Tridiag transform matrix ---\n");
- details.append(" --- D vector ---\n");
+ sb.append(" ---Tridiag transform matrix ---\n");
+ sb.append(" --- D vector ---\n");
eigenvector.printD(ps, "%15.4e");
ps.println();
- details.append("--- E vector ---\n");
+ sb.append("--- E vector ---\n");
eigenvector.printE(ps, "%15.4e");
ps.println();
} catch (Exception q)
{
q.printStackTrace();
- details.append("\n*** Unexpected exception when performing PCA ***\n"
+ sb.append("\n*** Unexpected exception when performing PCA ***\n"
+ q.getLocalizedMessage());
- details.append("*** Matrices below may not be fully diagonalised. ***\n");
+ sb.append(
+ "*** Matrices below may not be fully diagonalised. ***\n");
}
- details.append(" --- New diagonalization matrix ---\n");
+ sb.append(" --- New diagonalization matrix ---\n");
eigenvector.print(ps, "%8.2f");
- details.append(" --- Eigenvalues ---\n");
+ sb.append(" --- Eigenvalues ---\n");
eigenvector.printD(ps, "%15.4e");
ps.println();
- /*
- * for (int seq=0;seq<symm.rows;seq++) { ps.print("\"Seq"+seq+"\""); for
- * (int ev=0;ev<symm.rows; ev++) {
- *
- * ps.print(","+component(seq, ev)); } ps.println(); }
- */
- // System.out.println(("PCA.run() took "
- // + (System.currentTimeMillis() - now) + "ms"));
+
+ details = sb.toString();
}
/**
- * Computes an NxN matrix where N is the number of sequences, and entry [i, j]
- * is sequence[i] pairwise multiplied with sequence[j], as a sum of scores
- * computed using the current score matrix. For example
- * <ul>
- * <li>Sequences:</li>
- * <li>FKL</li>
- * <li>RSD</li>
- * <li>QIA</li>
- * <li>GWC</li>
- * <li>Score matrix is BLOSUM62</li>
- * <li>product [0, 0] = F.F + K.K + L.L = 6 + 5 + 4 = 15</li>
- * <li>product [2, 1] = R.R + S.S + D.D = 5 + 4 + 6 = 15</li>
- * <li>product [2, 2] = Q.Q + I.I + A.A = 5 + 4 + 4 = 13</li>
- * <li>product [3, 3] = G.G + W.W + C.C = 6 + 11 + 9 = 26</li>
- * <li>product[0, 1] = F.R + K.S + L.D = -3 + 0 + -3 = -7
- * <li>and so on</li>
- * </ul>
+ * Returns a PrintStream that wraps (sends its output to) the given
+ * StringBuilder
+ *
+ * @param sb
+ * @return
*/
- MatrixI computePairwiseScores()
+ protected PrintStream wrapOutputBuffer(StringBuilder sb)
{
- double[][] values = new double[seqs.length][];
- for (int row = 0; row < seqs.length; row++)
+ PrintStream ps = new PrintStream(System.out)
{
- values[row] = new double[seqs.length];
- for (int col = 0; col < seqs.length; col++)
+ @Override
+ public void print(String x)
{
- int total = 0;
- int width = Math.min(seqs[row].length(), seqs[col].length());
- for (int i = 0; i < width; i++)
- {
- char c1 = seqs[row].charAt(i);
- char c2 = seqs[col].charAt(i);
- int score = scoreMatrix.getPairwiseScore(c1, c2);
- total += score;
- }
- values[row][col] = total;
+ sb.append(x);
}
- }
- return new Matrix(values);
- }
- public void setJvCalcMode(boolean calcMode)
- {
- this.jvCalcMode = calcMode;
+ @Override
+ public void println()
+ {
+ sb.append("\n");
+ }
+ };
+ return ps;
}
/**
public int getHeight()
{
// TODO can any of seqs[] be null?
- return seqs.length;
+ return seqs.getSequences().length;
}
}