JAL-1807 explicit imports (jalview.workers)
[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   int alWidth;
53
54   @Override
55   public void run()
56   {
57     try
58     {
59       calcMan.notifyStart(this); // updatingConservation = true;
60
61       while (!calcMan.notifyWorking(this))
62       {
63         try
64         {
65           if (ap != null)
66           {
67             // ap.paintAlignment(false);
68           }
69           Thread.sleep(200);
70         } catch (Exception ex)
71         {
72           ex.printStackTrace();
73         }
74       }
75       if (alignViewport.isClosed())
76       {
77         abortAndDestroy();
78         return;
79       }
80       List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
81       AlignmentI alignment = alignViewport.getAlignment();
82       conservation = alignViewport.getAlignmentConservationAnnotation();
83       quality = alignViewport.getAlignmentQualityAnnot();
84       ourAnnot.add(conservation);
85       ourAnnot.add(quality);
86       ourAnnots = ourAnnot;
87       ConsPercGaps = alignViewport.getConsPercGaps();
88       // AlignViewport.UPDATING_CONSERVATION = true;
89
90       if (alignment == null || (alWidth = alignment.getWidth()) < 0)
91       {
92         calcMan.workerComplete(this);
93         // .updatingConservation = false;
94         // AlignViewport.UPDATING_CONSERVATION = false;
95
96         return;
97       }
98       try
99       {
100         cons = Conservation.calculateConservation("All",
101                 ResidueProperties.propHash, 3,
102                 alignment.getSequences(), 0, alWidth - 1, false,
103                 ConsPercGaps, quality != null);
104       } catch (IndexOutOfBoundsException x)
105       {
106         // probable race condition. just finish and return without any fuss.
107         calcMan.workerComplete(this);
108         return;
109       }
110       updateResultAnnotation(true);
111     } catch (OutOfMemoryError error)
112     {
113       ap.raiseOOMWarning("calculating conservation", error);
114       calcMan.workerCannotRun(this);
115       // alignViewport.conservation = null;
116       // this.alignViewport.quality = null;
117
118     }
119     calcMan.workerComplete(this);
120
121     if (ap != null)
122     {
123       ap.paintAlignment(true);
124     }
125
126   }
127
128   private void updateResultAnnotation(boolean b)
129   {
130     if (b || !calcMan.isWorking(this) && cons != null
131             && conservation != null && quality != null)
132     {
133       alignViewport.setConservation(cons);
134       cons.completeAnnotations(conservation, quality, 0, alWidth);
135     }
136   }
137
138   @Override
139   public void updateAnnotation()
140   {
141     updateResultAnnotation(false);
142
143   }
144 }