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