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