*/
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;
}
/**
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();
} 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<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();
+ }
+
+ /**
+ * Returns a PrintStream that wraps (sends its output to) the given
+ * StringBuilder
+ *
+ * @param sb
+ * @return
+ */
+ protected PrintStream wrapOutputBuffer(StringBuilder sb)
+ {
+ PrintStream ps = new PrintStream(System.out)
+ {
+ @Override
+ public void print(String x)
+ {
+ sb.append(x);
+ }
+
+ @Override
+ public void println()
+ {
+ sb.append("\n");
+ }
+ };
+ return ps;
}
/**
public class Matrix implements MatrixI
{
/*
- * the cell values in row-major order
+ * maximum number of iterations for tqli
*/
- private double[][] value;
+ private static final int MAX_ITER = 45;
+ // fudge - add 15 iterations, just in case
/*
* the number of rows
*/
- protected int rows;
+ final protected int rows;
/*
* the number of columns
*/
- protected int cols;
+ final protected int cols;
+
+ /*
+ * the cell values in row-major order
+ */
+ private double[][] value;
protected double[] d; // Diagonal
protected double[] e; // off diagonal
/**
- * maximum number of iterations for tqli
+ * Constructor given number of rows and columns
*
+ * @param colCount
+ * @param rowCount
*/
- private static final int maxIter = 45; // fudge - add 15 iterations, just in
- // case
-
- /**
- * Default constructor
- */
- public Matrix()
+ protected Matrix(int rowCount, int colCount)
{
-
+ rows = rowCount;
+ cols = colCount;
}
/**
}
}
- /**
- * Returns a new matrix which is the transpose of this one
- *
- * @return
- */
@Override
public 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)
- */
@Override
public MatrixI preMultiply(MatrixI in)
{
return out;
}
- /**
- * 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.
- * <p>
- * 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)
{
return in.preMultiply(this);
}
- /**
- * Answers a new matrix with a copy of the values in this one
- *
- * @return
- */
@Override
public MatrixI copy()
{
{
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
{
{
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
{
import java.io.PrintStream;
+/**
+ * An interface that describes a rectangular matrix of double values and
+ * operations on it
+ */
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.
+ * <p>
+ * 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();
*/
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];
/*
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<SequencePoint> points;
+ /*
+ * outputs
+ */
+ private volatile PCA pca;
- private SimilarityParamsI similarityParams;
+ int top;
+
+ private Vector<SequencePoint> points;
/**
* Constructor given sequence data, score model and score calculation
// top = pca.getM().height() - 1;
top = height - 1;
- points = new Vector<SequencePoint>();
+ points = new Vector<>();
float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
for (int i = 0; i < height; i++)