From: gmungoc Date: Mon, 7 May 2018 08:59:44 +0000 (+0100) Subject: JAL-1767 save PCA D and E vectors instead of 'details' text X-Git-Tag: Release_2_11_1_0~78^2~9^2~4 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=7335cf4c36225b8f425e6b5c452e0529d5b36de5;p=jalview.git JAL-1767 save PCA D and E vectors instead of 'details' text --- diff --git a/schemas/jalview.xsd b/schemas/jalview.xsd index 109b83b..acf842b 100755 --- a/schemas/jalview.xsd +++ b/schemas/jalview.xsd @@ -699,17 +699,12 @@ - - - - The text report of the calculation - - - + + + - diff --git a/src/jalview/analysis/PCA.java b/src/jalview/analysis/PCA.java index b3268df..d51f00e 100755 --- a/src/jalview/analysis/PCA.java +++ b/src/jalview/analysis/PCA.java @@ -22,6 +22,7 @@ package jalview.analysis; import jalview.api.analysis.ScoreModelI; import jalview.api.analysis.SimilarityParamsI; +import jalview.bin.Cache; import jalview.datamodel.AlignmentView; import jalview.datamodel.Point; import jalview.math.MatrixI; @@ -47,11 +48,9 @@ public class PCA implements Runnable */ private MatrixI pairwiseScores; - private MatrixI afterTred; + private MatrixI tridiagonal; - private MatrixI eigenvector; - - private String details; + private MatrixI eigenMatrix; /** * Constructor given the sequences to compute for, the similarity model to @@ -78,7 +77,7 @@ public class PCA implements Runnable */ public double getEigenvalue(int i) { - return eigenvector.getD()[i]; + return eigenMatrix.getD()[i]; } /** @@ -147,10 +146,10 @@ public class PCA implements Runnable for (int i = 0; i < pairwiseScores.width(); i++) { - out += (pairwiseScores.getValue(row, i) * eigenvector.getValue(i, n)); + out += (pairwiseScores.getValue(row, i) * eigenMatrix.getValue(i, n)); } - return out / eigenvector.getD()[n]; + return out / eigenMatrix.getD()[n]; } /** @@ -161,32 +160,38 @@ public class PCA implements Runnable */ public String getDetails() { - return details; - /* StringBuilder sb = new StringBuilder(1024); sb.append("PCA calculation using ").append(scoreModel.getName()) .append(" sequence similarity matrix\n========\n\n"); PrintStream ps = wrapOutputBuffer(sb); + /* + * pairwise similarity scores + */ sb.append(" --- OrigT * Orig ---- \n"); pairwiseScores.print(ps, "%8.2f"); + /* + * tridiagonal matrix, with D and E vectors + */ sb.append(" ---Tridiag transform matrix ---\n"); sb.append(" --- D vector ---\n"); - afterTred.printD(ps, "%15.4e"); + tridiagonal.printD(ps, "%15.4e"); ps.println(); sb.append("--- E vector ---\n"); - afterTred.printE(ps, "%15.4e"); + tridiagonal.printE(ps, "%15.4e"); ps.println(); + /* + * eigenvalues matrix, with D vector + */ sb.append(" --- New diagonalization matrix ---\n"); - eigenvector.print(ps, "%8.2f"); + eigenMatrix.print(ps, "%8.2f"); sb.append(" --- Eigenvalues ---\n"); - eigenvector.printD(ps, "%15.4e"); + eigenMatrix.printD(ps, "%15.4e"); ps.println(); return sb.toString(); - */ } /** @@ -195,53 +200,29 @@ public class PCA implements Runnable @Override public void run() { - /* - * 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 { - eigenvector = scoreModel.findSimilarities(seqs, similarityParams); - - sb.append(" --- OrigT * Orig ---- \n"); - eigenvector.print(ps, "%8.2f"); - - pairwiseScores = eigenvector.copy(); - - eigenvector.tred(); - - afterTred = eigenvector.copy(); - - sb.append(" ---Tridiag transform matrix ---\n"); - sb.append(" --- D vector ---\n"); - afterTred.printD(ps, "%15.4e"); - ps.println(); - sb.append("--- E vector ---\n"); - afterTred.printE(ps, "%15.4e"); - ps.println(); - - // Now produce the diagonalization matrix - eigenvector.tqli(); + /* + * sequence pairwise similarity scores + */ + pairwiseScores = scoreModel.findSimilarities(seqs, similarityParams); + + /* + * tridiagonal matrix + */ + tridiagonal = pairwiseScores.copy(); + tridiagonal.tred(); + + /* + * the diagonalization matrix + */ + eigenMatrix = tridiagonal.copy(); + eigenMatrix.tqli(); } catch (Exception q) { + Cache.log.error("Error computing PCA: " + q.getMessage()); q.printStackTrace(); - sb.append("\n*** Unexpected exception when performing PCA ***\n" - + q.getLocalizedMessage()); - sb.append( - "*** Matrices below may not be fully diagonalised. ***\n"); } - - sb.append(" --- New diagonalization matrix ---\n"); - eigenvector.print(ps, "%8.2f"); - sb.append(" --- Eigenvalues ---\n"); - eigenvector.printD(ps, "%15.4e"); - ps.println(); - - details = sb.toString(); } /** @@ -300,16 +281,21 @@ public class PCA implements Runnable public MatrixI getEigenmatrix() { - return eigenvector; + return eigenMatrix; } public void setEigenmatrix(MatrixI m) { - eigenvector = m; + eigenMatrix = m; + } + + public MatrixI getTridiagonal() + { + return tridiagonal; } - public void setDetails(String d) + public void setTridiagonal(MatrixI tridiagonal) { - details = d; + this.tridiagonal = tridiagonal; } } diff --git a/src/jalview/gui/Jalview2XML.java b/src/jalview/gui/Jalview2XML.java index 2b3e5ce..c725db8 100644 --- a/src/jalview/gui/Jalview2XML.java +++ b/src/jalview/gui/Jalview2XML.java @@ -70,7 +70,6 @@ import jalview.schemabinding.version2.DBRef; import jalview.schemabinding.version2.DoubleMatrix; import jalview.schemabinding.version2.EigenMatrix; import jalview.schemabinding.version2.EigenMatrixD; -import jalview.schemabinding.version2.EigenMatrixE; import jalview.schemabinding.version2.Features; import jalview.schemabinding.version2.Group; import jalview.schemabinding.version2.HiddenColumns; @@ -104,6 +103,9 @@ import jalview.schemabinding.version2.Setting; import jalview.schemabinding.version2.StructureState; import jalview.schemabinding.version2.ThresholdLine; import jalview.schemabinding.version2.Tree; +import jalview.schemabinding.version2.TridiagonalD; +import jalview.schemabinding.version2.TridiagonalE; +import jalview.schemabinding.version2.TridiagonalMatrix; import jalview.schemabinding.version2.UserColours; import jalview.schemabinding.version2.Viewport; import jalview.schemabinding.version2.types.ColourThreshTypeType; @@ -1622,42 +1624,30 @@ public class Jalview2XML PcaData data = new PcaData(); viewer.setPcaData(data); PCA pca = pcaModel.getPcaData(); - data.setDetails(pca.getDetails()); - MatrixI m = pca.getEigenmatrix(); - EigenMatrix eigenMatrix = new EigenMatrix(); - eigenMatrix.setRows(m.height()); - eigenMatrix.setColumns(m.width()); - data.setEigenMatrix(eigenMatrix); - for (int i = 0; i < m.height(); i++) - { - Row row = new Row(); - for (int j = 0; j < m.width(); j++) - { - row.addD(m.getValue(i, j)); - } - eigenMatrix.addRow(row); - } - EigenMatrixD eigenMatrixD = new EigenMatrixD(); - eigenMatrixD.setD(m.getD()); - data.setEigenMatrixD(eigenMatrixD); - EigenMatrixE eigenMatrixE = new EigenMatrixE(); - eigenMatrixE.setD(m.getE()); - data.setEigenMatrixE(eigenMatrixE); PairwiseMatrix pm = new PairwiseMatrix(); - m = pca.getPairwiseScores(); - pm.setRows(m.height()); - pm.setColumns(m.width()); + MatrixI m = pca.getPairwiseScores(); + saveDoubleMatrix(m, pm); data.setPairwiseMatrix(pm); - for (int i = 0; i < m.height(); i++) - { - Row row = new Row(); - for (int j = 0; j < m.width(); j++) - { - row.addD(m.getValue(i, j)); - } - pm.addRow(row); - } + + TridiagonalMatrix tm = new TridiagonalMatrix(); + m = pca.getTridiagonal(); + saveDoubleMatrix(m, tm); + data.setTridiagonalMatrix(tm); + TridiagonalD tridiagonalD = new TridiagonalD(); + tridiagonalD.setD(m.getD()); + data.setTridiagonalD(tridiagonalD); + TridiagonalE tridiagonalE = new TridiagonalE(); + tridiagonalE.setD(m.getE()); + data.setTridiagonalE(tridiagonalE); + + m = pca.getEigenmatrix(); + EigenMatrix eigenMatrix = new EigenMatrix(); + data.setEigenMatrix(eigenMatrix); + saveDoubleMatrix(m, eigenMatrix); + EigenMatrixD eigenmatrixD = new EigenMatrixD(); + eigenmatrixD.setD(m.getD()); + data.setEigenMatrixD(eigenmatrixD); jms.addPcaViewer(viewer); } catch (Throwable t) @@ -1667,6 +1657,49 @@ public class Jalview2XML } /** + * Stores values from a matrix into an XML element + * + * @param m + * @param xmlMatrix + * @see #loadDoubleMatrix(DoubleMatrix) + */ + protected void saveDoubleMatrix(MatrixI m, DoubleMatrix xmlMatrix) + { + xmlMatrix.setRows(m.height()); + xmlMatrix.setColumns(m.width()); + for (int i = 0; i < m.height(); i++) + { + Row row = new Row(); + for (int j = 0; j < m.width(); j++) + { + row.addD(m.getValue(i, j)); + } + xmlMatrix.addRow(row); + } + } + + /** + * Loads XML matrix data into a new Matrix object + * + * @param mData + * @return + * @see Jalview2XML#saveDoubleMatrix(MatrixI, DoubleMatrix) + */ + protected MatrixI loadDoubleMatrix(DoubleMatrix mData) + { + int rows = mData.getRows(); + double[][] vals = new double[rows][]; + + for (int i = 0; i < rows; i++) + { + vals[i] = mData.getRow(i).getD(); + } + + MatrixI m = new Matrix(vals); + return m; + } + + /** * Save any Varna viewers linked to this sequence. Writes an rnaViewer element * for each viewer, with *