From eb0d71ed66dcd7d8519eae1f084b76d60b374142 Mon Sep 17 00:00:00 2001 From: gmungoc Date: Thu, 23 Mar 2017 16:39:46 +0000 Subject: [PATCH] JAL-2397 scale PCA by PID to alignment width to match SeqSpace --- src/jalview/analysis/PCA.java | 9 +++++++++ src/jalview/math/Matrix.java | 19 +++++++++++++++++++ src/jalview/math/MatrixI.java | 7 +++++++ test/jalview/math/MatrixTest.java | 12 ++++++++++++ 4 files changed, 47 insertions(+) diff --git a/src/jalview/analysis/PCA.java b/src/jalview/analysis/PCA.java index b481e89..5d2e7e7 100755 --- a/src/jalview/analysis/PCA.java +++ b/src/jalview/analysis/PCA.java @@ -20,6 +20,7 @@ */ package jalview.analysis; +import jalview.analysis.scoremodels.PIDModel; import jalview.api.analysis.DistanceScoreModelI; import jalview.api.analysis.ScoreModelI; import jalview.api.analysis.SimilarityParamsI; @@ -235,6 +236,14 @@ public class PCA implements Runnable { result = ((SimilarityScoreModelI) scoreModel).findSimilarities(av, similarityParams); + if (scoreModel instanceof PIDModel) + { + /* + * scale % identities to width of alignment for backwards + * compatibility with Jalview 2.10.1 SeqSpace PCA calculation + */ + result.multiply(av.getWidth() / 100d); + } } else if (scoreModel instanceof DistanceScoreModelI) { diff --git a/src/jalview/math/Matrix.java b/src/jalview/math/Matrix.java index aaeb8da..821fc66 100755 --- a/src/jalview/math/Matrix.java +++ b/src/jalview/math/Matrix.java @@ -957,4 +957,23 @@ public class Matrix implements MatrixI } } } + + /** + * Multiply every entry in the matrix by the given value. This method is not + * thread-safe. + */ + @Override + public void multiply(double d) + { + for (double[] row : value) + { + if (row != null) + { + for (int i = 0; i < row.length; i++) + { + row[i] *= d; + } + } + } + } } diff --git a/src/jalview/math/MatrixI.java b/src/jalview/math/MatrixI.java index f0603dd..94b9333 100644 --- a/src/jalview/math/MatrixI.java +++ b/src/jalview/math/MatrixI.java @@ -87,4 +87,11 @@ public interface MatrixI * @param maxToZero */ void reverseRange(boolean maxToZero); + + /** + * Multiply all entries by the given value + * + * @param d + */ + void multiply(double d); } diff --git a/test/jalview/math/MatrixTest.java b/test/jalview/math/MatrixTest.java index 61b98f3..bd4108c 100644 --- a/test/jalview/math/MatrixTest.java +++ b/test/jalview/math/MatrixTest.java @@ -502,4 +502,16 @@ public class MatrixTest assertEquals(m1.getValue(1, 2), 15d, DELTA); } + @Test(groups = "Functional") + public void testMultiply() + { + Matrix m = new Matrix(new double[][] { { 2, 3.5, 4 }, { -3.4, 4, 15 } }); + m.multiply(2d); + assertEquals(m.getValue(0, 0), 4d, DELTA); + assertEquals(m.getValue(0, 1), 7d, DELTA); + assertEquals(m.getValue(0, 2), 8d, DELTA); + assertEquals(m.getValue(1, 0), -6.8d, DELTA); + assertEquals(m.getValue(1, 1), 8d, DELTA); + assertEquals(m.getValue(1, 2), 30d, DELTA); + } } -- 1.7.10.2