*/
package jalview.analysis;
-import jalview.datamodel.BinarySequence;
-import jalview.datamodel.BinarySequence.InvalidSequenceTypeException;
import jalview.math.Matrix;
import jalview.math.MatrixI;
-import jalview.math.SparseMatrix;
import jalview.schemes.ResidueProperties;
import jalview.schemes.ScoreMatrix;
{
boolean jvCalcMode = true;
- MatrixI m;
-
MatrixI symm;
- MatrixI m2;
-
double[] eigenvalue;
MatrixI eigenvector;
StringBuilder details = new StringBuilder(1024);
+ private String[] seqs;
+
+ private ScoreMatrix scoreMatrix;
+
/**
* Creates a new PCA object. By default, uses blosum62 matrix to generate
* sequence similarity matrices
public PCA(String[] s, boolean nucleotides, String s_m)
{
-
- 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 smtrx = null;
+ 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)
{
- smtrx = ResidueProperties.getScoreMatrix(sm);
+ scoreMatrix = ResidueProperties.getScoreMatrix(sm);
}
- if (smtrx == null)
+ 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
- smtrx = ResidueProperties.getScoreMatrix(sm = (nucleotides ? "DNA"
- : "BLOSUM62"));
+ 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 (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);
+ // 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);
}
/**
- * Returns the matrix used in PCA calculation
- *
- * @return
- */
-
- public MatrixI getM()
- {
- return m;
- }
-
- /**
* Returns Eigenvalue
*
* @param i
*/
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;
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);
}
details.append("PCA Calculation Mode is "
+ (jvCalcMode ? "Jalview variant" : "Original SeqSpace")
+ "\n");
- MatrixI mt = m.transpose();
-
- details.append(" --- OrigT * Orig ---- \n");
- eigenvector = mt.preMultiply(jvCalcMode ? m2 : m);
+ // MatrixI mt = m.transpose();
+ // eigenvector = mt.preMultiply(jvCalcMode ? m2 : m);
+ eigenvector = computePairwiseScores();
+ details.append(" --- OrigT * Orig ---- \n");
eigenvector.print(ps, "%8.2f");
symm = eigenvector.copy();
// + (System.currentTimeMillis() - now) + "ms"));
}
+ /**
+ * 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>
+ */
+ MatrixI computePairwiseScores()
+ {
+ double[][] values = new double[seqs.length][];
+ for (int row = 0; row < seqs.length; row++)
+ {
+ values[row] = new double[seqs.length];
+ for (int col = 0; col < seqs.length; col++)
+ {
+ 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;
+ }
+ }
+ return new Matrix(values);
+ }
+
public void setJvCalcMode(boolean calcMode)
{
this.jvCalcMode = 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()
+ {
+ // TODO can any of seqs[] be null?
+ return seqs.length;
+ }
}
package jalview.schemes;
import jalview.analysis.scoremodels.PairwiseSeqScoreModel;
-import jalview.api.analysis.ScoreModelI;
-public class ScoreMatrix extends PairwiseSeqScoreModel implements
- ScoreModelI
+public class ScoreMatrix extends PairwiseSeqScoreModel
{
String name;
return getPairwiseScore(A1.charAt(0), A2.charAt(0));
}
+ @Override
public int getPairwiseScore(char c, char d)
{
int pog = 0;
int b = (type == 0) ? ResidueProperties.aaIndex[d]
: ResidueProperties.nucleotideIndex[d];
+ /*
+ * hack to convert unassigned / unknown (including gap)
+ * to index of unknown (X for amino acids, N for nucleotide)
+ * TODO: statically assign gap characters to this index?
+ */
+ if (type == 0)
+ {
+ if (a == ResidueProperties.maxProteinIndex)
+ {
+ a = ResidueProperties.aaIndex['X'];
+ }
+ if (b == ResidueProperties.maxProteinIndex)
+ {
+ b = ResidueProperties.aaIndex['X'];
+ }
+ }
+ if (type != 0)
+ {
+ if (a == ResidueProperties.maxNucleotideIndex)
+ {
+ a = ResidueProperties.nucleotideIndex['N'];
+ }
+ if (b == ResidueProperties.maxNucleotideIndex)
+ {
+ b = ResidueProperties.nucleotideIndex['N'];
+ }
+ }
pog = matrix[a][b];
} catch (Exception e)
{
/**
* pretty print the matrix
*/
+ @Override
public String toString()
{
return outputMatrix(false);
public void run()
{
- pca = new PCA(seqstrings.getSequenceStrings(' '), nucleotide,
+ String[] sequenceStrings = seqstrings.getSequenceStrings(' ');
+ pca = new PCA(sequenceStrings, nucleotide,
score_matrix);
pca.setJvCalcMode(jvCalcMode);
pca.run();
ii++;
}
- top = pca.getM().height() - 1;
+ int height = pca.getHeight();
+ // top = pca.getM().height() - 1;
+ top = height - 1;
points = new Vector<SequencePoint>();
float[][] scores = pca.getComponents(top - 1, top - 2, top - 3, 100);
- for (int i = 0; i < pca.getM().height(); i++)
+ for (int i = 0; i < height; i++)
{
SequencePoint sp = new SequencePoint(seqs[i], scores[i]);
points.addElement(sp);
public void updateRc(RotatableCanvasI rc)
{
- rc.setPoints(points, pca.getM().height());
+ rc.setPoints(points, pca.getHeight());
}
public boolean isNucleotide()
// note: actual indices for components are dim1-1, etc (patch for JAL-1123)
float[][] scores = pca.getComponents(dim1 - 1, dim2 - 1, dim3 - 1, 100);
- for (int i = 0; i < pca.getM().height(); i++)
+ for (int i = 0; i < pca.getHeight(); i++)
{
points.elementAt(i).coord = scores[i];
}
--- /dev/null
+package jalview.analysis;
+
+import static org.testng.Assert.assertEquals;
+
+import jalview.math.MatrixI;
+
+import org.testng.annotations.Test;
+
+public class PCATest
+{
+ @Test(groups = "Functional")
+ public void testComputePairwiseScores()
+ {
+ String[] seqs = new String[] { "FKL", "R-D", "QIA", "GWC" };
+ PCA pca = new PCA(seqs, false, "BLOSUM62");
+
+ MatrixI pairwise = pca.computePairwiseScores();
+
+ /*
+ * should be NxN where N = number of sequences
+ */
+ assertEquals(pairwise.height(), 4);
+ assertEquals(pairwise.width(), 4);
+
+ /*
+ * should be symmetrical (because BLOSUM62 is)
+ */
+ for (int i = 0; i < pairwise.height(); i++)
+ {
+ for (int j = 0; j < pairwise.width(); j++)
+ {
+ assertEquals(pairwise.getValue(i, j), pairwise.getValue(j, i),
+ "Not symmetric");
+ }
+ }
+ /*
+ * verify expected BLOSUM dot product scores
+ * Note: gap is treated like 'X' [22] in the matrix
+ */
+ // F.F + K.K + L.L = 6 + 5 + 4 = 15
+ assertEquals(pairwise.getValue(0, 0), 15d);
+ // R.R + X.X + D.D = 5 + -1 + 6 = 10
+ assertEquals(pairwise.getValue(1, 1), 10d);
+ // Q.Q + I.I + A.A = 5 + 4 + 4 = 13
+ assertEquals(pairwise.getValue(2, 2), 13d);
+ // G.G + W.W + C.C = 6 + 11 + 9 = 26
+ assertEquals(pairwise.getValue(3, 3), 26d);
+ // F.R + K.X + L.D = -3 + -1 + -3 = -8
+ assertEquals(pairwise.getValue(0, 1), -8d);
+ // F.Q + K.I + L.A = -3 + -3 + -1 = -7
+ assertEquals(pairwise.getValue(0, 2), -7d);
+ // F.G + K.W + L.C = -3 + -3 + -1 = -7
+ assertEquals(pairwise.getValue(0, 3), -7d);
+ // R.Q + X.I + D.A = 1 + -1 + -2 = -2
+ assertEquals(pairwise.getValue(1, 2), -2d);
+ // R.G + X.W + D.C = -2 + -2 + -3 = -7
+ assertEquals(pairwise.getValue(1, 3), -7d);
+ // Q.G + I.W + A.C = -2 + -3 + 0 = -5
+ assertEquals(pairwise.getValue(2, 3), -5d);
+ }
+}
import java.util.Map;
import org.testng.annotations.BeforeClass;
-import org.testng.annotations.Test;
public class ScoreMatrixPrinter
{
JvOptionPane.setMockResponse(JvOptionPane.CANCEL_OPTION);
}
- @Test(groups = { "Functional" })
public void printAllMatrices()
{
for (Map.Entry<String, ScoreModelI> sm : ResidueProperties.scoreMatrices
}
}
- @Test(groups = { "Functional" })
public void printHTMLMatrices()
{
for (Map.Entry<String, ScoreModelI> _sm : ResidueProperties.scoreMatrices
--- /dev/null
+package jalview.schemes;
+
+import static org.testng.Assert.assertEquals;
+
+import org.testng.annotations.Test;
+
+public class ScoreMatrixTest
+{
+ @Test(groups = "Functional")
+ public void testSymmetric()
+ {
+ verifySymmetric(ResidueProperties.getScoreMatrix("BLOSUM62"));
+ verifySymmetric(ResidueProperties.getScoreMatrix("PAM250"));
+ verifySymmetric(ResidueProperties.getScoreMatrix("DNA"));
+ }
+
+ private void verifySymmetric(ScoreMatrix sm)
+ {
+ int[][] m = sm.getMatrix();
+ int rows = m.length;
+ for (int row = 0; row < rows; row++)
+ {
+ assertEquals(m[row].length, rows);
+ for (int col = 0; col < rows; col++)
+ {
+ // if (m[row][col] != m[col][row])
+ // {
+ // System.out.println(String.format("%s [%d, %d] [%s, %s]",
+ // sm.getName(), m[row][col], m[col][row],
+ // ResidueProperties.aa[row],
+ // ResidueProperties.aa[col]));
+ // }
+ assertEquals(m[row][col], m[col][row], String.format("%s [%s, %s]",
+ sm.getName(), ResidueProperties.aa[row],
+ ResidueProperties.aa[col]));
+ }
+ }
+
+ /*
+ * also check the score matrix is sized for
+ * the number of symbols scored, plus gap
+ */
+ assertEquals(rows, (sm.isDNA() ? ResidueProperties.maxNucleotideIndex
+ : ResidueProperties.maxProteinIndex) + 1);
+ }
+}