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