X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fanalysis%2FPCA.java;h=1cf21fd9f385ef5b03cf325a34ad424f0dced6bc;hb=2cc3a62e1ae63428db854af668e963f1b23af553;hp=eaea7bf9471363c11d87f739fd31f10872b6bb40;hpb=05d2ce87440717d47ba3dc6ef293e25e4543fdd1;p=jalview.git diff --git a/src/jalview/analysis/PCA.java b/src/jalview/analysis/PCA.java index eaea7bf..1cf21fd 100755 --- a/src/jalview/analysis/PCA.java +++ b/src/jalview/analysis/PCA.java @@ -20,144 +20,50 @@ */ package jalview.analysis; -import jalview.datamodel.BinarySequence; -import jalview.datamodel.BinarySequence.InvalidSequenceTypeException; -import jalview.math.Matrix; -import jalview.schemes.ResidueProperties; -import jalview.schemes.ScoreMatrix; +import jalview.api.analysis.ScoreModelI; +import jalview.api.analysis.SimilarityParamsI; +import jalview.datamodel.AlignmentView; +import jalview.datamodel.Point; +import jalview.math.MatrixI; import java.io.PrintStream; /** * Performs Principal Component Analysis on given sequences - * - * @author $author$ - * @version $Revision$ */ public class PCA implements Runnable { - Matrix m; - - Matrix symm; - - Matrix m2; - - double[] eigenvalue; - - Matrix eigenvector; - - StringBuffer details = new StringBuffer(); - - /** - * 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); - } - - /** - * 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. + /* + * inputs */ - public PCA(String[] s, boolean nucleotides) - { - this(s, nucleotides, null); - } + final private AlignmentView seqs; - public PCA(String[] s, boolean nucleotides, String s_m) - { + final private ScoreModelI scoreModel; - BinarySequence[] bs = new BinarySequence[s.length]; - int ii = 0; + final private SimilarityParamsI similarityParams; - while ((ii < s.length) && (s[ii] != null)) - { - bs[ii] = new BinarySequence(s[ii], nucleotides); - bs[ii].encode(); - ii++; - } - - BinarySequence[] bs2 = new BinarySequence[s.length]; - ii = 0; - 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 - + " sequence similarity matrix\n========\n\n"); - 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++; - } - - // System.out.println("Created binary encoding"); - // printMemory(rt); - int count = 0; - - while ((count < bs.length) && (bs[count] != null)) - { - count++; - } - - double[][] seqmat = new double[count][bs[0].getDBinary().length]; - double[][] seqmat2 = new double[count][bs2[0].getDBinary().length]; - int i = 0; - - while (i < count) - { - seqmat[i] = bs[i].getDBinary(); - seqmat2[i] = bs2[i].getDBinary(); - i++; - } + /* + * outputs + */ + private MatrixI symm; - // System.out.println("Created array"); - // printMemory(rt); - // System.out.println(" --- Original matrix ---- "); - m = new Matrix(seqmat); - m2 = new Matrix(seqmat2); + private MatrixI eigenvector; - } + private String details; /** - * Returns the matrix used in PCA calculation + * Constructor given the sequences to compute for, the similarity model to + * use, and a set of parameters for sequence comparison * - * @return java.math.Matrix object + * @param sequences + * @param sm + * @param options */ - - public Matrix getM() + public PCA(AlignmentView sequences, ScoreModelI sm, SimilarityParamsI options) { - return m; + this.seqs = sequences; + this.scoreModel = sm; + this.similarityParams = options; } /** @@ -170,7 +76,7 @@ public class PCA implements Runnable */ public double getEigenvalue(int i) { - return eigenvector.d[i]; + return eigenvector.getD()[i]; } /** @@ -187,15 +93,16 @@ public class PCA implements Runnable * * @return DOCUMENT ME! */ - public float[][] getComponents(int l, int n, int mm, float factor) + public Point[] getComponents(int l, int n, int mm, float factor) { - float[][] out = new float[m.rows][3]; + Point[] out = new Point[getHeight()]; - for (int i = 0; i < m.rows; i++) + for (int i = 0; i < getHeight(); i++) { - out[i][0] = (float) component(i, l) * factor; - out[i][1] = (float) component(i, n) * factor; - out[i][2] = (float) component(i, mm) * factor; + float x = (float) component(i, l) * factor; + float y = (float) component(i, n) * factor; + float z = (float) component(i, mm) * factor; + out[i] = new Point(x, y, z); } return out; @@ -212,9 +119,9 @@ public class PCA implements Runnable public double[] component(int n) { // n = index of eigenvector - double[] out = new double[m.rows]; + double[] out = new double[getHeight()]; - for (int i = 0; i < m.rows; i++) + for (int i = 0; i < out.length; i++) { out[i] = component(i, n); } @@ -236,70 +143,56 @@ public class PCA implements Runnable { double out = 0.0; - for (int i = 0; i < symm.cols; i++) + for (int i = 0; i < symm.width(); i++) { - out += (symm.value[row][i] * eigenvector.value[i][n]); + out += (symm.getValue(row, i) * eigenvector.getValue(i, n)); } - return out / eigenvector.d[n]; + 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); try { - details.append("PCA Calculation Mode is " - + (jvCalcMode ? "Jalview variant" : "Original SeqSpace") - + "\n"); - Matrix mt = m.transpose(); + eigenvector = scoreModel.findSimilarities(seqs, similarityParams); - details.append(" --- OrigT * Orig ---- \n"); - if (!jvCalcMode) - { - eigenvector = mt.preMultiply(m); // standard seqspace comparison matrix - } - else - { - eigenvector = mt.preMultiply(m2); // jalview variation on seqsmace - // method - } - - eigenvector.print(ps); + 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"); - eigenvector.printD(ps); + sb.append(" ---Tridiag transform matrix ---\n"); + sb.append(" --- D vector ---\n"); + eigenvector.printD(ps, "%15.4e"); ps.println(); - details.append("--- E vector ---\n"); - eigenvector.printE(ps); + sb.append("--- E vector ---\n"); + eigenvector.printE(ps, "%15.4e"); ps.println(); // Now produce the diagonalization matrix @@ -307,28 +200,56 @@ public class PCA implements Runnable } 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"); - eigenvector.print(ps); - details.append(" --- Eigenvalues ---\n"); - eigenvector.printD(ps); + sb.append(" --- New diagonalization matrix ---\n"); + eigenvector.print(ps, "%8.2f"); + sb.append(" --- Eigenvalues ---\n"); + eigenvector.printD(ps, "%15.4e"); ps.println(); - /* - * for (int seq=0;seq