JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / src / jalview / workers / ConservationThread.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.Conservation;
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.schemes.ResidueProperties;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 public class ConservationThread extends AlignCalcWorker implements
35         AlignCalcWorkerI
36 {
37
38   private int ConsPercGaps = 25; // JBPNote : This should be a configurable
39                                  // property!
40
41         @Override
42         public AlignCalcWorkerI getNewWorker() {
43                 return new ConservationThread(alignViewport, ap);
44         }
45
46   public ConservationThread(AlignViewportI alignViewport,
47           AlignmentViewPanel alignPanel)
48   {
49     super(alignViewport, alignPanel);
50     ConsPercGaps = alignViewport.getConsPercGaps();
51   }
52
53   private Conservation cons;
54
55   AlignmentAnnotation conservation, quality;
56
57         @Override
58         protected void run1(int state) {
59                 while (!interrupted()) {
60                         try {
61                                 switch (state) {
62                                 case INIT:
63                                         if (calcMan.isPending(this))
64                                                 return;
65                                         calcMan.notifyStart(this);
66                                         state = LOOP_STANDBY;
67                                         break;
68                                 case LOOP_STANDBY:
69                                         while (!calcMan.notifyWorking(this)) {
70                                                 if (ap != null) {
71                                                         ap.paintAlignment(false);
72                                                 }
73                                                 try {
74                                                         if (sleepAndReturn(200, state))
75                                                                 return;
76                                                 } catch (InterruptedException e) {
77                                                         state = DONE;
78                                                         break;
79                                                 }
80                                         }
81                                         if (alignViewport.isClosed()) {
82                                                 abortAndDestroy();
83                                                 state = DONE;
84                                                 break;
85                                         }
86                       List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
87                       alignment = alignViewport.getAlignment();
88                       conservation = alignViewport.getAlignmentConservationAnnotation();
89                       quality = alignViewport.getAlignmentQualityAnnot();
90                       ourAnnot.add(conservation);
91                       ourAnnot.add(quality);
92                       ourAnnots = ourAnnot;
93                       ConsPercGaps = alignViewport.getConsPercGaps();
94                       // AlignViewport.UPDATING_CONSERVATION = true;
95
96                       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
97                       {
98                         calcMan.workerComplete(this);
99                         // .updatingConservation = false;
100                         // AlignViewport.UPDATING_CONSERVATION = false;
101
102                         return;
103                       }
104                                         state = LOOP_CALCULATE;
105                                         break;
106                                 case LOOP_CALCULATE:
107                                         iFirst = iLast;
108                                         nPer = aWidth  + 1; // for now - one big chunk
109                                         iLast = Math.min(iLast + nPer, aWidth);
110                                         if (iLast == iFirst) {
111                                                 state = DONE;
112                                         } else {
113                                                 computeConsensus();
114                                                 if (sleepAndReturn(0, state))
115                                                         return;
116                                         }
117                                         break;
118                                 case DONE:
119                       updateResultAnnotation(true);
120                                         notifyDone();
121                                         return;
122                                 }
123                         } catch (OutOfMemoryError error) {
124                                 calcMan.workerCannotRun(this);
125               ap.raiseOOMWarning("calculating conservation", error);
126                         } catch (Throwable e) {
127                                 System.out.println("Error in ConsensusThread: " + e);
128                                 e.printStackTrace();
129                                 calcMan.workerComplete(this);
130                         }
131                 }
132         }
133
134         private void computeConsensus() {
135     cons = Conservation.calculateConservation("All",
136         ResidueProperties.propHash, 3,
137         alignment.getSequences(), 0, aWidth - 1, false,
138         ConsPercGaps, quality != null);
139         }
140
141
142   private void updateResultAnnotation(boolean b)
143   {
144     if (b || !calcMan.isWorking(this) && cons != null
145             && conservation != null && quality != null)
146     {
147       alignViewport.setConservation(cons);
148       cons.completeAnnotations(conservation, quality, 0, aWidth);
149     }
150   }
151
152   @Override
153   public void updateAnnotation()
154   {
155     updateResultAnnotation(false);
156
157   }
158
159 }