From: gmungoc Date: Wed, 25 Apr 2018 11:00:40 +0000 (+0100) Subject: JAL-1767 minor field tidying prior to functional changes X-Git-Tag: Release_2_11_1_0~78^2~9^2~20 X-Git-Url: http://source.jalview.org/gitweb/?p=jalview.git;a=commitdiff_plain;h=11ec9d4540710b65b82b8b901de07b441c130887 JAL-1767 minor field tidying prior to functional changes --- diff --git a/src/jalview/analysis/PCA.java b/src/jalview/analysis/PCA.java index 42a168d..d11d322 100755 --- a/src/jalview/analysis/PCA.java +++ b/src/jalview/analysis/PCA.java @@ -32,28 +32,37 @@ import java.io.PrintStream; */ public class PCA implements Runnable { - MatrixI symm; - - double[] eigenvalue; + /* + * inputs + */ + final private AlignmentView seqs; - MatrixI eigenvector; + final private ScoreModelI scoreModel; - StringBuilder details = new StringBuilder(1024); + final private SimilarityParamsI similarityParams; - final private AlignmentView seqs; + /* + * outputs + */ + private MatrixI symm; - private ScoreModelI scoreModel; + private MatrixI eigenvector; - private SimilarityParamsI similarityParams; + private String details; - public PCA(AlignmentView s, ScoreModelI sm, SimilarityParamsI options) + /** + * Constructor given the sequences to compute for, the similarity model to + * use, and a set of parameters for sequence comparison + * + * @param sequences + * @param sm + * @param options + */ + public PCA(AlignmentView sequences, ScoreModelI sm, SimilarityParamsI options) { - this.seqs = s; - this.similarityParams = options; + this.seqs = sequences; this.scoreModel = sm; - - details.append("PCA calculation using " + sm.getName() - + " sequence similarity matrix\n========\n\n"); + this.similarityParams = options; } /** @@ -140,49 +149,47 @@ public class PCA implements Runnable 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 { 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(); @@ -191,25 +198,45 @@ 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( + 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 - * This method simply returns the result of in.preMultiply(this) - * - * @param in - * - * @return - * @throws IllegalArgumentException - * if the number of rows in the post-multiplier is not equal to the - * number of columns in the multiplicand (this) - * @see #preMultiply(Matrix) - */ @Override public MatrixI postMultiply(MatrixI in) { @@ -234,11 +205,6 @@ public class Matrix implements MatrixI return in.preMultiply(this); } - /** - * Answers a new matrix with a copy of the values in this one - * - * @return - */ @Override public MatrixI copy() { @@ -479,11 +445,11 @@ public class Matrix implements MatrixI { iter++; - if (iter == maxIter) + if (iter == MAX_ITER) { throw new Exception(MessageManager.formatMessage( "exception.matrix_too_many_iteration", new String[] - { "tqli", Integer.valueOf(maxIter).toString() })); + { "tqli", Integer.valueOf(MAX_ITER).toString() })); } else { @@ -743,11 +709,11 @@ public class Matrix implements MatrixI { iter++; - if (iter == maxIter) + if (iter == MAX_ITER) { throw new Exception(MessageManager.formatMessage( "exception.matrix_too_many_iteration", new String[] - { "tqli2", Integer.valueOf(maxIter).toString() })); + { "tqli2", Integer.valueOf(MAX_ITER).toString() })); } else { diff --git a/src/jalview/math/MatrixI.java b/src/jalview/math/MatrixI.java index 5b93c76..dd7aaf1 100644 --- a/src/jalview/math/MatrixI.java +++ b/src/jalview/math/MatrixI.java @@ -22,6 +22,10 @@ package jalview.math; import java.io.PrintStream; +/** + * An interface that describes a rectangular matrix of double values and + * operations on it + */ public interface MatrixI { /** @@ -63,12 +67,49 @@ public interface MatrixI */ double[] getRow(int i); + /** + * Answers a new matrix with a copy of the values in this one + * + * @return + */ MatrixI copy(); + /** + * Returns a new matrix which is the transpose of this one + * + * @return + */ MatrixI transpose(); + /** + * Returns a new matrix which is the result of premultiplying this matrix by + * the supplied argument. If this of size AxB (A rows and B columns), and the + * argument is CxA (C rows and A columns), the result is of size CxB. + * + * @param in + * + * @return + * @throws IllegalArgumentException + * if the number of columns in the pre-multiplier is not equal to + * the number of rows in the multiplicand (this) + */ MatrixI preMultiply(MatrixI m); + /** + * Returns a new matrix which is the result of postmultiplying this matrix by + * the supplied argument. If this of size AxB (A rows and B columns), and the + * argument is BxC (B rows and C columns), the result is of size AxC. + *

+ * This method simply returns the result of in.preMultiply(this) + * + * @param in + * + * @return + * @throws IllegalArgumentException + * if the number of rows in the post-multiplier is not equal to the + * number of columns in the multiplicand (this) + * @see #preMultiply(Matrix) + */ MatrixI postMultiply(MatrixI m); double[] getD(); diff --git a/src/jalview/math/SparseMatrix.java b/src/jalview/math/SparseMatrix.java index 86592a0..e24cda5 100644 --- a/src/jalview/math/SparseMatrix.java +++ b/src/jalview/math/SparseMatrix.java @@ -45,11 +45,8 @@ public class SparseMatrix extends Matrix */ public SparseMatrix(double[][] v) { - rows = v.length; - if (rows > 0) - { - cols = v[0].length; - } + super(v.length, v.length > 0 ? v[0].length : 0); + sparseColumns = new SparseDoubleArray[cols]; /* diff --git a/src/jalview/viewmodel/PCAModel.java b/src/jalview/viewmodel/PCAModel.java index 5e7fca2..0dcdbca 100644 --- a/src/jalview/viewmodel/PCAModel.java +++ b/src/jalview/viewmodel/PCAModel.java @@ -32,24 +32,30 @@ import java.util.Vector; public class PCAModel { - private volatile PCA pca; - - int top; + /* + * inputs + */ + private final AlignmentView seqstrings; - AlignmentView seqstrings; + private final SequenceI[] seqs; - SequenceI[] seqs; + private final SimilarityParamsI similarityParams; /* - * Name of score model used to calculate PCA + * options - score model, nucleotide / protein */ - ScoreModelI scoreModel; + private ScoreModelI scoreModel; private boolean nucleotide = false; - private Vector points; + /* + * outputs + */ + private volatile PCA pca; - private SimilarityParamsI similarityParams; + int top; + + private Vector points; /** * Constructor given sequence data, score model and score calculation @@ -88,7 +94,7 @@ public class PCAModel // top = pca.getM().height() - 1; top = height - 1; - points = new Vector(); + points = new Vector<>(); float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100); for (int i = 0; i < height; i++)