JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / site / j2s / jalview / analysis / PCA.js
1 Clazz.declarePackage ("jalview.analysis");\r
2 Clazz.load (["java.lang.StringBuffer"], "jalview.analysis.PCA", ["jalview.datamodel.BinarySequence", "jalview.math.Matrix", "jalview.schemes.ResidueProperties"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.m = null;\r
5 this.symm = null;\r
6 this.m2 = null;\r
7 this.eigenvalue = null;\r
8 this.eigenvector = null;\r
9 this.details = null;\r
10 this.jvCalcMode = true;\r
11 Clazz.instantialize (this, arguments);\r
12 }, jalview.analysis, "PCA", null, Runnable);\r
13 Clazz.prepareFields (c$, function () {\r
14 this.details =  new StringBuffer ();\r
15 });\r
16 Clazz.makeConstructor (c$, \r
17 function (s) {\r
18 this.construct (s, false);\r
19 }, "~A");\r
20 Clazz.makeConstructor (c$, \r
21 function (s, nucleotides) {\r
22 this.construct (s, nucleotides, null);\r
23 }, "~A,~B");\r
24 Clazz.makeConstructor (c$, \r
25 function (s, nucleotides, s_m) {\r
26 var bs =  new Array (s.length);\r
27 var ii = 0;\r
28 while ((ii < s.length) && (s[ii] != null)) {\r
29 bs[ii] =  new jalview.datamodel.BinarySequence (s[ii], nucleotides);\r
30 bs[ii].encode ();\r
31 ii++;\r
32 }\r
33 var bs2 =  new Array (s.length);\r
34 ii = 0;\r
35 var smtrx = null;\r
36 var sm = s_m;\r
37 if (sm != null) {\r
38 smtrx = jalview.schemes.ResidueProperties.getScoreMatrix (sm);\r
39 }if (smtrx == null) {\r
40 smtrx = jalview.schemes.ResidueProperties.getScoreMatrix (sm = (nucleotides ? "DNA" : "BLOSUM62"));\r
41 }this.details.append ("PCA calculation using " + sm + " sequence similarity matrix\n========\n\n");\r
42 while ((ii < s.length) && (s[ii] != null)) {\r
43 bs2[ii] =  new jalview.datamodel.BinarySequence (s[ii], nucleotides);\r
44 if (smtrx != null) {\r
45 try {\r
46 bs2[ii].matrixEncode (smtrx);\r
47 } catch (x) {\r
48 if (Clazz.exceptionOf (x, jalview.datamodel.BinarySequence.InvalidSequenceTypeException)) {\r
49 this.details.append ("Unexpected mismatch of sequence type and score matrix. Calculation will not be valid!\n\n");\r
50 } else {\r
51 throw x;\r
52 }\r
53 }\r
54 }ii++;\r
55 }\r
56 var count = 0;\r
57 while ((count < bs.length) && (bs[count] != null)) {\r
58 count++;\r
59 }\r
60 var seqmat =  Clazz.newDoubleArray (count, bs[0].getDBinary ().length, 0);\r
61 var seqmat2 =  Clazz.newDoubleArray (count, bs2[0].getDBinary ().length, 0);\r
62 var i = 0;\r
63 while (i < count) {\r
64 seqmat[i] = bs[i].getDBinary ();\r
65 seqmat2[i] = bs2[i].getDBinary ();\r
66 i++;\r
67 }\r
68 this.m =  new jalview.math.Matrix (seqmat, count, bs[0].getDBinary ().length);\r
69 this.m2 =  new jalview.math.Matrix (seqmat2, count, bs2[0].getDBinary ().length);\r
70 }, "~A,~B,~S");\r
71 Clazz.defineMethod (c$, "getM", \r
72 function () {\r
73 return this.m;\r
74 });\r
75 Clazz.defineMethod (c$, "getEigenvalue", \r
76 function (i) {\r
77 return this.eigenvector.d[i];\r
78 }, "~N");\r
79 Clazz.defineMethod (c$, "getComponents", \r
80 function (l, n, mm, factor) {\r
81 var out =  Clazz.newFloatArray (this.m.rows, 3, 0);\r
82 for (var i = 0; i < this.m.rows; i++) {\r
83 out[i][0] = this.component (i, l) * factor;\r
84 out[i][1] = this.component (i, n) * factor;\r
85 out[i][2] = this.component (i, mm) * factor;\r
86 }\r
87 return out;\r
88 }, "~N,~N,~N,~N");\r
89 Clazz.defineMethod (c$, "component", \r
90 function (n) {\r
91 var out =  Clazz.newDoubleArray (this.m.rows, 0);\r
92 for (var i = 0; i < this.m.rows; i++) {\r
93 out[i] = this.component (i, n);\r
94 }\r
95 return out;\r
96 }, "~N");\r
97 Clazz.defineMethod (c$, "component", \r
98 function (row, n) {\r
99 var out = 0.0;\r
100 for (var i = 0; i < this.symm.cols; i++) {\r
101 out += (this.symm.value[row][i] * this.eigenvector.value[i][n]);\r
102 }\r
103 return out / this.eigenvector.d[n];\r
104 }, "~N,~N");\r
105 Clazz.defineMethod (c$, "getDetails", \r
106 function () {\r
107 return this.details.toString ();\r
108 });\r
109 Clazz.overrideMethod (c$, "run", \r
110 function () {\r
111 var ps = ((Clazz.isClassDefined ("jalview.analysis.PCA$1") ? 0 : jalview.analysis.PCA.$PCA$1$ ()), Clazz.innerTypeInstance (jalview.analysis.PCA$1, this, null, System.out));\r
112 try {\r
113 this.details.append ("PCA Calculation Mode is " + (this.jvCalcMode ? "Jalview variant" : "Original SeqSpace") + "\n");\r
114 var mt = this.m.transpose ();\r
115 this.details.append (" --- OrigT * Orig ---- \n");\r
116 if (!this.jvCalcMode) {\r
117 this.eigenvector = mt.preMultiply (this.m);\r
118 } else {\r
119 this.eigenvector = mt.preMultiply (this.m2);\r
120 }this.eigenvector.print (ps);\r
121 this.symm = this.eigenvector.copy ();\r
122 this.eigenvector.tred ();\r
123 this.details.append (" ---Tridiag transform matrix ---\n");\r
124 this.details.append (" --- D vector ---\n");\r
125 this.eigenvector.printD (ps);\r
126 ps.println ();\r
127 this.details.append ("--- E vector ---\n");\r
128 this.eigenvector.printE (ps);\r
129 ps.println ();\r
130 this.eigenvector.tqli ();\r
131 } catch (q) {\r
132 if (Clazz.exceptionOf (q, Exception)) {\r
133 q.printStackTrace ();\r
134 this.details.append ("\n*** Unexpected exception when performing PCA ***\n" + q.getLocalizedMessage ());\r
135 this.details.append ("*** Matrices below may not be fully diagonalised. ***\n");\r
136 } else {\r
137 throw q;\r
138 }\r
139 }\r
140 this.details.append (" --- New diagonalization matrix ---\n");\r
141 this.eigenvector.print (ps);\r
142 this.details.append (" --- Eigenvalues ---\n");\r
143 this.eigenvector.printD (ps);\r
144 ps.println ();\r
145 });\r
146 Clazz.defineMethod (c$, "setJvCalcMode", \r
147 function (calcMode) {\r
148 this.jvCalcMode = calcMode;\r
149 }, "~B");\r
150 c$.$PCA$1$ = function () {\r
151 Clazz.pu$h(self.c$);\r
152 c$ = Clazz.declareAnonymous (jalview.analysis, "PCA$1", java.io.PrintStream);\r
153 Clazz.defineMethod (c$, "print", \r
154 function (x) {\r
155 this.b$["jalview.analysis.PCA"].details.append (x);\r
156 }, "~S");\r
157 Clazz.defineMethod (c$, "println", \r
158 function () {\r
159 this.b$["jalview.analysis.PCA"].details.append ("\n");\r
160 });\r
161 c$ = Clazz.p0p ();\r
162 };\r
163 });\r