X-Git-Url: http://source.jalview.org/gitweb/?p=jalviewjs.git;a=blobdiff_plain;f=site%2Fj2s%2Fjalview%2Fanalysis%2FPCA.js;h=e0d68db1f3fcfb4314d72952245b78bc02805611;hp=7375cfb86b88e35e244a01365214a6ca2522cfca;hb=b9b7a352eee79b7764c3b09c9d19663075061d8c;hpb=7301a2415adab88038b291fc54caeeb3a5a47a44 diff --git a/site/j2s/jalview/analysis/PCA.js b/site/j2s/jalview/analysis/PCA.js index 7375cfb..e0d68db 100644 --- a/site/j2s/jalview/analysis/PCA.js +++ b/site/j2s/jalview/analysis/PCA.js @@ -1,163 +1,163 @@ -Clazz.declarePackage ("jalview.analysis"); -Clazz.load (["java.lang.StringBuffer"], "jalview.analysis.PCA", ["jalview.datamodel.BinarySequence", "jalview.math.Matrix", "jalview.schemes.ResidueProperties"], function () { -c$ = Clazz.decorateAsClass (function () { -this.m = null; -this.symm = null; -this.m2 = null; -this.eigenvalue = null; -this.eigenvector = null; -this.details = null; -this.jvCalcMode = true; -Clazz.instantialize (this, arguments); -}, jalview.analysis, "PCA", null, Runnable); -Clazz.prepareFields (c$, function () { -this.details = new StringBuffer (); -}); -Clazz.makeConstructor (c$, -function (s) { -this.construct (s, false); -}, "~A"); -Clazz.makeConstructor (c$, -function (s, nucleotides) { -this.construct (s, nucleotides, null); -}, "~A,~B"); -Clazz.makeConstructor (c$, -function (s, nucleotides, s_m) { -var bs = new Array (s.length); -var ii = 0; -while ((ii < s.length) && (s[ii] != null)) { -bs[ii] = new jalview.datamodel.BinarySequence (s[ii], nucleotides); -bs[ii].encode (); -ii++; -} -var bs2 = new Array (s.length); -ii = 0; -var smtrx = null; -var sm = s_m; -if (sm != null) { -smtrx = jalview.schemes.ResidueProperties.getScoreMatrix (sm); -}if (smtrx == null) { -smtrx = jalview.schemes.ResidueProperties.getScoreMatrix (sm = (nucleotides ? "DNA" : "BLOSUM62")); -}this.details.append ("PCA calculation using " + sm + " sequence similarity matrix\n========\n\n"); -while ((ii < s.length) && (s[ii] != null)) { -bs2[ii] = new jalview.datamodel.BinarySequence (s[ii], nucleotides); -if (smtrx != null) { -try { -bs2[ii].matrixEncode (smtrx); -} catch (x) { -if (Clazz.exceptionOf (x, jalview.datamodel.BinarySequence.InvalidSequenceTypeException)) { -this.details.append ("Unexpected mismatch of sequence type and score matrix. Calculation will not be valid!\n\n"); -} else { -throw x; -} -} -}ii++; -} -var count = 0; -while ((count < bs.length) && (bs[count] != null)) { -count++; -} -var seqmat = Clazz.newDoubleArray (count, bs[0].getDBinary ().length, 0); -var seqmat2 = Clazz.newDoubleArray (count, bs2[0].getDBinary ().length, 0); -var i = 0; -while (i < count) { -seqmat[i] = bs[i].getDBinary (); -seqmat2[i] = bs2[i].getDBinary (); -i++; -} -this.m = new jalview.math.Matrix (seqmat, count, bs[0].getDBinary ().length); -this.m2 = new jalview.math.Matrix (seqmat2, count, bs2[0].getDBinary ().length); -}, "~A,~B,~S"); -Clazz.defineMethod (c$, "getM", -function () { -return this.m; -}); -Clazz.defineMethod (c$, "getEigenvalue", -function (i) { -return this.eigenvector.d[i]; -}, "~N"); -Clazz.defineMethod (c$, "getComponents", -function (l, n, mm, factor) { -var out = Clazz.newFloatArray (this.m.rows, 3, 0); -for (var i = 0; i < this.m.rows; i++) { -out[i][0] = this.component (i, l) * factor; -out[i][1] = this.component (i, n) * factor; -out[i][2] = this.component (i, mm) * factor; -} -return out; -}, "~N,~N,~N,~N"); -Clazz.defineMethod (c$, "component", -function (n) { -var out = Clazz.newDoubleArray (this.m.rows, 0); -for (var i = 0; i < this.m.rows; i++) { -out[i] = this.component (i, n); -} -return out; -}, "~N"); -Clazz.defineMethod (c$, "component", -function (row, n) { -var out = 0.0; -for (var i = 0; i < this.symm.cols; i++) { -out += (this.symm.value[row][i] * this.eigenvector.value[i][n]); -} -return out / this.eigenvector.d[n]; -}, "~N,~N"); -Clazz.defineMethod (c$, "getDetails", -function () { -return this.details.toString (); -}); -Clazz.overrideMethod (c$, "run", -function () { -var ps = ((Clazz.isClassDefined ("jalview.analysis.PCA$1") ? 0 : jalview.analysis.PCA.$PCA$1$ ()), Clazz.innerTypeInstance (jalview.analysis.PCA$1, this, null, System.out)); -try { -this.details.append ("PCA Calculation Mode is " + (this.jvCalcMode ? "Jalview variant" : "Original SeqSpace") + "\n"); -var mt = this.m.transpose (); -this.details.append (" --- OrigT * Orig ---- \n"); -if (!this.jvCalcMode) { -this.eigenvector = mt.preMultiply (this.m); -} else { -this.eigenvector = mt.preMultiply (this.m2); -}this.eigenvector.print (ps); -this.symm = this.eigenvector.copy (); -this.eigenvector.tred (); -this.details.append (" ---Tridiag transform matrix ---\n"); -this.details.append (" --- D vector ---\n"); -this.eigenvector.printD (ps); -ps.println (); -this.details.append ("--- E vector ---\n"); -this.eigenvector.printE (ps); -ps.println (); -this.eigenvector.tqli (); -} catch (q) { -if (Clazz.exceptionOf (q, Exception)) { -q.printStackTrace (); -this.details.append ("\n*** Unexpected exception when performing PCA ***\n" + q.getLocalizedMessage ()); -this.details.append ("*** Matrices below may not be fully diagonalised. ***\n"); -} else { -throw q; -} -} -this.details.append (" --- New diagonalization matrix ---\n"); -this.eigenvector.print (ps); -this.details.append (" --- Eigenvalues ---\n"); -this.eigenvector.printD (ps); -ps.println (); -}); -Clazz.defineMethod (c$, "setJvCalcMode", -function (calcMode) { -this.jvCalcMode = calcMode; -}, "~B"); -c$.$PCA$1$ = function () { -Clazz.pu$h(self.c$); -c$ = Clazz.declareAnonymous (jalview.analysis, "PCA$1", java.io.PrintStream); -Clazz.defineMethod (c$, "print", -function (x) { -this.b$["jalview.analysis.PCA"].details.append (x); -}, "~S"); -Clazz.defineMethod (c$, "println", -function () { -this.b$["jalview.analysis.PCA"].details.append ("\n"); -}); -c$ = Clazz.p0p (); -}; -}); +Clazz.declarePackage ("jalview.analysis"); +Clazz.load (["java.lang.StringBuffer"], "jalview.analysis.PCA", ["jalview.datamodel.BinarySequence", "jalview.math.Matrix", "jalview.schemes.ResidueProperties"], function () { +c$ = Clazz.decorateAsClass (function () { +this.m = null; +this.symm = null; +this.m2 = null; +this.eigenvalue = null; +this.eigenvector = null; +this.details = null; +this.jvCalcMode = true; +Clazz.instantialize (this, arguments); +}, jalview.analysis, "PCA", null, Runnable); +Clazz.prepareFields (c$, function () { +this.details = new StringBuffer (); +}); +Clazz.makeConstructor (c$, +function (s) { +this.construct (s, false); +}, "~A"); +Clazz.makeConstructor (c$, +function (s, nucleotides) { +this.construct (s, nucleotides, null); +}, "~A,~B"); +Clazz.makeConstructor (c$, +function (s, nucleotides, s_m) { +var bs = new Array (s.length); +var ii = 0; +while ((ii < s.length) && (s[ii] != null)) { +bs[ii] = new jalview.datamodel.BinarySequence (s[ii], nucleotides); +bs[ii].encode (); +ii++; +} +var bs2 = new Array (s.length); +ii = 0; +var smtrx = null; +var sm = s_m; +if (sm != null) { +smtrx = jalview.schemes.ResidueProperties.getScoreMatrix (sm); +}if (smtrx == null) { +smtrx = jalview.schemes.ResidueProperties.getScoreMatrix (sm = (nucleotides ? "DNA" : "BLOSUM62")); +}this.details.append ("PCA calculation using " + sm + " sequence similarity matrix\n========\n\n"); +while ((ii < s.length) && (s[ii] != null)) { +bs2[ii] = new jalview.datamodel.BinarySequence (s[ii], nucleotides); +if (smtrx != null) { +try { +bs2[ii].matrixEncode (smtrx); +} catch (x) { +if (Clazz.exceptionOf (x, jalview.datamodel.BinarySequence.InvalidSequenceTypeException)) { +this.details.append ("Unexpected mismatch of sequence type and score matrix. Calculation will not be valid!\n\n"); +} else { +throw x; +} +} +}ii++; +} +var count = 0; +while ((count < bs.length) && (bs[count] != null)) { +count++; +} +var seqmat = Clazz.newDoubleArray (count, bs[0].getDBinary ().length, 0); +var seqmat2 = Clazz.newDoubleArray (count, bs2[0].getDBinary ().length, 0); +var i = 0; +while (i < count) { +seqmat[i] = bs[i].getDBinary (); +seqmat2[i] = bs2[i].getDBinary (); +i++; +} +this.m = new jalview.math.Matrix (seqmat, count, bs[0].getDBinary ().length); +this.m2 = new jalview.math.Matrix (seqmat2, count, bs2[0].getDBinary ().length); +}, "~A,~B,~S"); +Clazz.defineMethod (c$, "getM", +function () { +return this.m; +}); +Clazz.defineMethod (c$, "getEigenvalue", +function (i) { +return this.eigenvector.d[i]; +}, "~N"); +Clazz.defineMethod (c$, "getComponents", +function (l, n, mm, factor) { +var out = Clazz.newFloatArray (this.m.rows, 3, 0); +for (var i = 0; i < this.m.rows; i++) { +out[i][0] = this.component (i, l) * factor; +out[i][1] = this.component (i, n) * factor; +out[i][2] = this.component (i, mm) * factor; +} +return out; +}, "~N,~N,~N,~N"); +Clazz.defineMethod (c$, "component", +function (n) { +var out = Clazz.newDoubleArray (this.m.rows, 0); +for (var i = 0; i < this.m.rows; i++) { +out[i] = this.component (i, n); +} +return out; +}, "~N"); +Clazz.defineMethod (c$, "component", +function (row, n) { +var out = 0.0; +for (var i = 0; i < this.symm.cols; i++) { +out += (this.symm.value[row][i] * this.eigenvector.value[i][n]); +} +return out / this.eigenvector.d[n]; +}, "~N,~N"); +Clazz.defineMethod (c$, "getDetails", +function () { +return this.details.toString (); +}); +Clazz.overrideMethod (c$, "run", +function () { +var ps = ((Clazz.isClassDefined ("jalview.analysis.PCA$1") ? 0 : jalview.analysis.PCA.$PCA$1$ ()), Clazz.innerTypeInstance (jalview.analysis.PCA$1, this, null, System.out)); +try { +this.details.append ("PCA Calculation Mode is " + (this.jvCalcMode ? "Jalview variant" : "Original SeqSpace") + "\n"); +var mt = this.m.transpose (); +this.details.append (" --- OrigT * Orig ---- \n"); +if (!this.jvCalcMode) { +this.eigenvector = mt.preMultiply (this.m); +} else { +this.eigenvector = mt.preMultiply (this.m2); +}this.eigenvector.print (ps); +this.symm = this.eigenvector.copy (); +this.eigenvector.tred (); +this.details.append (" ---Tridiag transform matrix ---\n"); +this.details.append (" --- D vector ---\n"); +this.eigenvector.printD (ps); +ps.println (); +this.details.append ("--- E vector ---\n"); +this.eigenvector.printE (ps); +ps.println (); +this.eigenvector.tqli (); +} catch (q) { +if (Clazz.exceptionOf (q, Exception)) { +q.printStackTrace (); +this.details.append ("\n*** Unexpected exception when performing PCA ***\n" + q.getLocalizedMessage ()); +this.details.append ("*** Matrices below may not be fully diagonalised. ***\n"); +} else { +throw q; +} +} +this.details.append (" --- New diagonalization matrix ---\n"); +this.eigenvector.print (ps); +this.details.append (" --- Eigenvalues ---\n"); +this.eigenvector.printD (ps); +ps.println (); +}); +Clazz.defineMethod (c$, "setJvCalcMode", +function (calcMode) { +this.jvCalcMode = calcMode; +}, "~B"); +c$.$PCA$1$ = function () { +Clazz.pu$h(self.c$); +c$ = Clazz.declareAnonymous (jalview.analysis, "PCA$1", java.io.PrintStream); +Clazz.defineMethod (c$, "print", +function (x) { +this.b$["jalview.analysis.PCA"].details.append (x); +}, "~S"); +Clazz.defineMethod (c$, "println", +function () { +this.b$["jalview.analysis.PCA"].details.append ("\n"); +}); +c$ = Clazz.p0p (); +}; +});