JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / bin / jalview / workers / ConsensusThread.js
1 Clazz.declarePackage ("jalview.workers");\r
2 Clazz.load (["jalview.api.AlignCalcWorkerI", "jalview.workers.AlignCalcWorker"], "jalview.workers.ConsensusThread", ["jalview.analysis.AAFrequency"], function () {\r
3 c$ = Clazz.decorateAsClass (function () {\r
4 this.hconsensus = null;\r
5 this.aseqs = null;\r
6 Clazz.instantialize (this, arguments);\r
7 }, jalview.workers, "ConsensusThread", jalview.workers.AlignCalcWorker, jalview.api.AlignCalcWorkerI);\r
8 Clazz.overrideMethod (c$, "getNewWorker", \r
9 function () {\r
10 return  new jalview.workers.ConsensusThread (this.alignViewport, this.ap);\r
11 });\r
12 Clazz.overrideMethod (c$, "run1", \r
13 function (state) {\r
14 while (!Thread.interrupted ()) {\r
15 if (this.alignViewport.isClosed ()) {\r
16 this.abortAndDestroy ();\r
17 return;\r
18 }try {\r
19 switch (state) {\r
20 case 0:\r
21 if (this.calcMan.isPending (this)) return;\r
22 this.calcMan.notifyStart (this);\r
23 var consensus = this.getConsensusAnnotation ();\r
24 if (consensus == null || this.calcMan.isPending (this)) {\r
25 this.calcMan.workerComplete (this);\r
26 return;\r
27 }state = 3;\r
28 break;\r
29 case 3:\r
30 while (!this.calcMan.notifyWorking (this)) {\r
31 if (this.ap != null) {\r
32 this.ap.paintAlignment (false);\r
33 }try {\r
34 if (this.sleepAndReturn (200, state)) return;\r
35 } catch (e) {\r
36 if (Clazz.exceptionOf (e, InterruptedException)) {\r
37 state = 2;\r
38 break;\r
39 } else {\r
40 throw e;\r
41 }\r
42 }\r
43 }\r
44 if (this.alignViewport.isClosed ()) {\r
45 this.abortAndDestroy ();\r
46 state = 2;\r
47 break;\r
48 }this.alignment = this.alignViewport.getAlignment ();\r
49 this.aWidth = -1;\r
50 if (this.alignment == null || (this.aWidth = this.alignment.getWidth ()) < 0) {\r
51 state = 2;\r
52 }this.eraseConsensus (this.aWidth);\r
53 state = (this.initializeCalc () ? 4 : 2);\r
54 break;\r
55 case 4:\r
56 this.iFirst = this.iLast;\r
57 this.iLast = Math.min (this.iLast + this.nPer, this.aWidth);\r
58 if (this.iLast == this.iFirst) {\r
59 state = 2;\r
60 } else {\r
61 this.computeConsensus ();\r
62 if (this.sleepAndReturn (0, state)) return;\r
63 }break;\r
64 case 2:\r
65 this.finalizeCalc ();\r
66 this.updateAlignment ();\r
67 this.notifyDone ();\r
68 return;\r
69 }\r
70 } catch (e$$) {\r
71 if (Clazz.exceptionOf (e$$, OutOfMemoryError)) {\r
72 var error = e$$;\r
73 {\r
74 this.calcMan.workerCannotRun (this);\r
75 this.ap.raiseOOMWarning ("calculating consensus", error);\r
76 }\r
77 } else {\r
78 var e = e$$;\r
79 {\r
80 System.out.println ("Error in ConsensusThread: " + e);\r
81 e.printStackTrace ();\r
82 this.calcMan.workerComplete (this);\r
83 }\r
84 }\r
85 }\r
86 }\r
87 }, "~N");\r
88 Clazz.defineMethod (c$, "initializeCalc", \r
89 function () {\r
90 this.iLast = 0;\r
91 this.hconsensus =  new Array (this.aWidth);\r
92 this.aseqs = this.getSequences ();\r
93 return true;\r
94 });\r
95 Clazz.defineMethod (c$, "computeConsensus", \r
96 function () {\r
97 this.started = System.currentTimeMillis ();\r
98 jalview.analysis.AAFrequency.calculate (this.aseqs, this.iFirst, this.iLast, this.hconsensus, true);\r
99 if (System.currentTimeMillis () - this.started < 500) this.nPer *= 2;\r
100 });\r
101 Clazz.defineMethod (c$, "finalizeCalc", \r
102 function () {\r
103 this.alignViewport.setSequenceConsensusHash (this.hconsensus);\r
104 });\r
105 Clazz.defineMethod (c$, "updateAlignment", \r
106 function () {\r
107 this.setColourSchemeConsensus (this.hconsensus);\r
108 this.updateResultAnnotation (true);\r
109 });\r
110 Clazz.defineMethod (c$, "eraseConsensus", \r
111 function (aWidth) {\r
112 var consensus = this.getConsensusAnnotation ();\r
113 consensus.annotations =  new Array (aWidth);\r
114 }, "~N");\r
115 Clazz.defineMethod (c$, "getSequences", \r
116 function () {\r
117 return this.alignViewport.getAlignment ().getSequencesArray ();\r
118 });\r
119 Clazz.defineMethod (c$, "setColourSchemeConsensus", \r
120 function (hconsensus) {\r
121 var globalColourScheme = this.alignViewport.getGlobalColourScheme ();\r
122 if (globalColourScheme != null) {\r
123 globalColourScheme.setConsensus (hconsensus);\r
124 }}, "~A");\r
125 Clazz.defineMethod (c$, "getConsensusAnnotation", \r
126 function () {\r
127 return this.alignViewport.getAlignmentConsensusAnnotation ();\r
128 });\r
129 Clazz.overrideMethod (c$, "updateAnnotation", \r
130 function () {\r
131 this.updateResultAnnotation (false);\r
132 });\r
133 Clazz.defineMethod (c$, "updateResultAnnotation", \r
134 function (immediate) {\r
135 var consensus = this.getConsensusAnnotation ();\r
136 var hconsensus = this.getViewportConsensus ();\r
137 if (immediate || !this.calcMan.isWorking (this) && consensus != null && hconsensus != null) {\r
138 this.deriveConsensus (consensus, hconsensus);\r
139 }}, "~B");\r
140 Clazz.defineMethod (c$, "deriveConsensus", \r
141 function (consensusAnnotation, consensusData) {\r
142 var nseq = this.getSequences ().length;\r
143 jalview.analysis.AAFrequency.completeConsensus (consensusAnnotation, consensusData, 0, consensusData.length, this.alignViewport.isIgnoreGapsConsensus (), this.alignViewport.isShowSequenceLogo (), nseq);\r
144 }, "jalview.datamodel.AlignmentAnnotation,~A");\r
145 Clazz.defineMethod (c$, "getViewportConsensus", \r
146 function () {\r
147 return this.alignViewport.getSequenceConsensusHash ();\r
148 });\r
149 });\r