JAL-1807 includes ?j2sdebug flag and DebugJS._(msg)
[jalviewjs.git] / src / jalview / workers / StrucConsensusThread.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.workers;
22
23 import jalview.analysis.StructureFrequency;
24 import jalview.api.AlignCalcWorkerI;
25 import jalview.api.AlignViewportI;
26 import jalview.api.AlignmentViewPanel;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.SequenceI;
31
32 import java.util.Hashtable;
33
34 /**
35  * 
36  * untested
37  * 
38  * @author Bob Hanson 
39  *
40  */
41 public class StrucConsensusThread extends AlignCalcWorker implements
42         AlignCalcWorkerI
43 {
44
45         private AlignmentAnnotation rnaStruc = null;
46   private AlignmentAnnotation strucConsensus;
47
48   private Hashtable[] hStrucConsensus;
49
50   private long nseq = -1;
51
52         private AlignmentAnnotation[] aa;
53         private SequenceI[] arr;
54
55
56   @Override
57         public AlignCalcWorkerI getNewWorker() {
58                 return new StrucConsensusThread(alignViewport, ap);
59         }
60
61         public StrucConsensusThread(AlignViewportI alignViewport,
62           AlignmentViewPanel alignPanel)
63   {
64     super(alignViewport, alignPanel);
65   }
66
67         @Override
68         protected void run1(int state) {
69                 if (alignViewport.isClosed()) {
70                         abortAndDestroy();
71                         return;
72                 }
73                 while (!interrupted()) {
74                         try {
75                                 switch (state) {
76                                 case INIT:
77                                         if (calcMan.isPending(this))
78                                                 return;
79                                         calcMan.notifyStart(this);
80                                         state = LOOP_STANDBY;
81                                         break;
82                                 case LOOP_STANDBY:
83                                         while (!calcMan.notifyWorking(this)) {
84                                                 if (ap != null) {
85                                                         ap.paintAlignment(false);
86                                                 }
87                                                 try {
88                                                         if (sleepAndReturn(200, state))
89                                                                 return;
90                                                 } catch (InterruptedException e) {
91                                                         state = DONE;
92                                                         break;
93                                                 }
94                                         }
95                                         if (alignViewport.isClosed()) {
96                                                 abortAndDestroy();
97                                                 state = DONE;
98                                                 break;
99                                         }
100                                         alignment = alignViewport.getAlignment();
101                                         aWidth = -1;
102                                         if (alignment == null || (aWidth = alignment.getWidth()) < 0) {
103                                                 state = DONE;
104                                         }
105                                         strucConsensus = alignViewport.getAlignmentStrucConsensusAnnotation();
106                                         hStrucConsensus = alignViewport.getRnaStructureConsensusHash();
107                                         strucConsensus.annotations = null;
108                                         strucConsensus.annotations = new Annotation[aWidth];
109
110                                         hStrucConsensus = new Hashtable[aWidth];
111
112                                         aa = alignViewport.getAlignment().getAlignmentAnnotation();
113                       // select rna struct to use for calculation
114                       for (int i = 0; i < aa.length; i++)
115                       {
116                         if (aa[i].getRNAStruc() != null && aa[i].isValidStruc())
117                         {
118                           rnaStruc = aa[i];
119                           break;
120                         }
121                       }
122                       // check to see if its valid
123
124                       if (rnaStruc == null || !rnaStruc.isValidStruc())
125                       {
126                         calcMan.workerComplete(this);
127                         return;
128                       }
129                 arr = alignment.getSequencesArray();
130                 nseq = arr.length;
131
132                                         state = LOOP_CALCULATE;
133                                         break;
134                                 case LOOP_CALCULATE:
135                                         iFirst = iLast;
136                                         iLast = Math.min(iLast + nPer, aWidth);
137                                         if (iLast == iFirst) {
138                                                 state = DONE;
139                                         } else {
140                         StructureFrequency.calculate(arr, 0,
141                 alignment.getWidth(), hStrucConsensus, true, rnaStruc);
142                                                 if (sleepAndReturn(0, state))
143                                                         return;
144                                         }
145                                         break;
146                                 case DONE:
147                       alignViewport.setRnaStructureConsensusHash(hStrucConsensus);
148                       // TODO AlignmentAnnotation rnaStruc!!!
149                       updateResultAnnotation(true);
150                       if (alignViewport.getGlobalColourScheme() != null)
151                       {
152                         alignViewport.getGlobalColourScheme().setConsensus(hStrucConsensus);
153                       }
154                                         notifyDone();
155                                         return;
156                                 }
157                         } catch (OutOfMemoryError error) {
158                                 calcMan.workerCannotRun(this);
159               ap.raiseOOMWarning("calculating RNA structure consensus", error);
160                         } catch (Throwable e) {
161                                 System.out.println("Error in ConsensusThread: " + e);
162                                 e.printStackTrace();
163                                 calcMan.workerComplete(this);
164                         }
165                 }
166         }
167
168   /**
169    * update the consensus annotation from the sequence profile data using
170    * current visualization settings.
171    */
172   @Override
173   public void updateAnnotation()
174   {
175     updateResultAnnotation(false);
176   }
177
178   public void updateResultAnnotation(boolean immediate)
179   {
180     if (immediate || !calcMan.isWorking(this) && strucConsensus != null
181             && hStrucConsensus != null)
182     {
183       StructureFrequency.completeConsensus(strucConsensus, hStrucConsensus,
184               0, hStrucConsensus.length,
185               alignViewport.isIgnoreGapsConsensus(),
186               alignViewport.isShowSequenceLogo(), nseq);
187     }
188   }
189
190 }