X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FPCA.java;h=42a168dab78a24d40c6f572fd4e10d404f6dcaab;hb=98b3b695032b6af0c35443044b6d3d00390b3c22;hp=9fc6027fe2644ffd86d0335695a31b26caa79f47;hpb=29433ea97308601bc17d76d376c6e629b0b28fa5;p=jalview.git diff --git a/src/jalview/analysis/PCA.java b/src/jalview/analysis/PCA.java index 9fc6027..42a168d 100755 --- a/src/jalview/analysis/PCA.java +++ b/src/jalview/analysis/PCA.java @@ -20,13 +20,10 @@ */ package jalview.analysis; -import jalview.datamodel.BinarySequence; -import jalview.datamodel.BinarySequence.InvalidSequenceTypeException; -import jalview.math.Matrix; +import jalview.api.analysis.ScoreModelI; +import jalview.api.analysis.SimilarityParamsI; +import jalview.datamodel.AlignmentView; import jalview.math.MatrixI; -import jalview.math.SparseMatrix; -import jalview.schemes.ResidueProperties; -import jalview.schemes.ScoreMatrix; import java.io.PrintStream; @@ -35,128 +32,28 @@ import java.io.PrintStream; */ public class PCA implements Runnable { - boolean jvCalcMode = true; - - MatrixI m; - MatrixI symm; - MatrixI m2; - double[] eigenvalue; MatrixI eigenvector; StringBuilder details = new StringBuilder(1024); - /** - * Creates a new PCA object. By default, uses blosum62 matrix to generate - * sequence similarity matrices - * - * @param s - * Set of amino acid sequences to perform PCA on - */ - public PCA(String[] s) - { - this(s, false); - } + final private AlignmentView seqs; - /** - * 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); - } + private ScoreModelI scoreModel; - public PCA(String[] s, boolean nucleotides, String s_m) - { - - BinarySequence[] bs = new BinarySequence[s.length]; - int ii = 0; + private SimilarityParamsI similarityParams; - while ((ii < s.length) && (s[ii] != null)) - { - bs[ii] = new BinarySequence(s[ii], nucleotides); - bs[ii].encode(); - ii++; - } + public PCA(AlignmentView s, ScoreModelI sm, SimilarityParamsI options) + { + this.seqs = s; + this.similarityParams = options; + this.scoreModel = sm; - BinarySequence[] bs2 = new BinarySequence[s.length]; - ScoreMatrix smtrx = null; - String sm = s_m; - if (sm != null) - { - smtrx = ResidueProperties.getScoreMatrix(sm); - } - if (smtrx == null) - { - // either we were given a non-existent score matrix or a scoremodel that - // isn't based on a pairwise symbol score matrix - smtrx = ResidueProperties.getScoreMatrix(sm = (nucleotides ? "DNA" - : "BLOSUM62")); - } - details.append("PCA calculation using " + sm + details.append("PCA calculation using " + sm.getName() + " sequence similarity matrix\n========\n\n"); - ii = 0; - while ((ii < s.length) && (s[ii] != null)) - { - bs2[ii] = new BinarySequence(s[ii], nucleotides); - if (smtrx != null) - { - try - { - bs2[ii].matrixEncode(smtrx); - } 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); - - } - - /** - * Returns the matrix used in PCA calculation - * - * @return java.math.Matrix object - */ - - public MatrixI getM() - { - return m; } /** @@ -188,9 +85,9 @@ public class PCA implements Runnable */ public float[][] getComponents(int l, int n, int mm, float factor) { - float[][] out = new float[m.height()][3]; + float[][] out = new float[getHeight()][3]; - for (int i = 0; i < m.height(); i++) + for (int i = 0; i < getHeight(); i++) { out[i][0] = (float) component(i, l) * factor; out[i][1] = (float) component(i, n) * factor; @@ -211,9 +108,9 @@ public class PCA implements Runnable public double[] component(int n) { // n = index of eigenvector - double[] out = new double[m.height()]; + double[] out = new double[getHeight()]; - for (int i = 0; i < m.height(); i++) + for (int i = 0; i < out.length; i++) { out[i] = component(i, n); } @@ -272,15 +169,9 @@ public class PCA implements Runnable // long now = System.currentTimeMillis(); try { - details.append("PCA Calculation Mode is " - + (jvCalcMode ? "Jalview variant" : "Original SeqSpace") - + "\n"); - MatrixI mt = m.transpose(); + eigenvector = scoreModel.findSimilarities(seqs, similarityParams); details.append(" --- OrigT * Orig ---- \n"); - - eigenvector = mt.preMultiply(jvCalcMode ? m2 : m); - eigenvector.print(ps, "%8.2f"); symm = eigenvector.copy(); @@ -302,7 +193,8 @@ public class PCA implements Runnable q.printStackTrace(); details.append("\n*** Unexpected exception when performing PCA ***\n" + q.getLocalizedMessage()); - details.append("*** Matrices below may not be fully diagonalised. ***\n"); + details.append( + "*** Matrices below may not be fully diagonalised. ***\n"); } details.append(" --- New diagonalization matrix ---\n"); @@ -320,8 +212,15 @@ public class PCA implements Runnable // + (System.currentTimeMillis() - now) + "ms")); } - public void setJvCalcMode(boolean calcMode) + /** + * Answers the N dimensions of the NxN PCA matrix. This is the number of + * sequences involved in the pairwise score calculation. + * + * @return + */ + public int getHeight() { - this.jvCalcMode = calcMode; + // TODO can any of seqs[] be null? + return seqs.getSequences().length; } }