58510e5737d34ce881e94da9e4bccc1e6286ca09
[jalview.git] / src / jalview / workers / ConservationThread.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8)
3  * Copyright (C) 2012 J Procter, AM Waterhouse, LM Lui, J Engelhardt, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
10  *  
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package jalview.workers;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import jalview.analysis.Conservation;
24 import jalview.api.AlignCalcWorkerI;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.api.AlignViewportI;
27 import jalview.datamodel.AlignmentAnnotation;
28 import jalview.datamodel.AlignmentI;
29
30 public class ConservationThread extends AlignCalcWorker implements
31         AlignCalcWorkerI
32 {
33
34   private int ConsPercGaps = 25; // JBPNote : This should be a configurable
35                                  // property!
36
37   public ConservationThread(AlignViewportI alignViewport,
38           AlignmentViewPanel alignPanel)
39   {
40     super(alignViewport, alignPanel);
41     ConsPercGaps = alignViewport.getConsPercGaps();
42   }
43
44   private Conservation cons;
45
46   AlignmentAnnotation conservation, quality;
47
48   int alWidth;
49
50   @Override
51   public void run()
52   {
53     try
54     {
55       calcMan.notifyStart(this); // updatingConservation = true;
56
57       while (!calcMan.notifyWorking(this))
58       {
59         try
60         {
61           if (ap != null)
62           {
63             // ap.paintAlignment(false);
64           }
65           Thread.sleep(200);
66         } catch (Exception ex)
67         {
68           ex.printStackTrace();
69         }
70       }
71       if (alignViewport.isClosed())
72       {
73         abortAndDestroy();
74       }
75       List<AlignmentAnnotation> ourAnnot = new ArrayList<AlignmentAnnotation>();
76       AlignmentI alignment = alignViewport.getAlignment();
77       conservation = alignViewport.getAlignmentConservationAnnotation();
78       quality = alignViewport.getAlignmentQualityAnnot();
79       ourAnnot.add(conservation);
80       ourAnnot.add(quality);
81       ourAnnots = ourAnnot;
82
83       // AlignViewport.UPDATING_CONSERVATION = true;
84
85       if (alignment == null || (alWidth = alignment.getWidth()) < 0)
86       {
87         calcMan.workerComplete(this);
88         // .updatingConservation = false;
89         // AlignViewport.UPDATING_CONSERVATION = false;
90
91         return;
92       }
93       try
94       {
95         cons = Conservation.calculateConservation("All",
96                 jalview.schemes.ResidueProperties.propHash, 3,
97                 alignment.getSequences(), 0, alWidth - 1, false,
98                 ConsPercGaps, quality != null);
99       } catch (IndexOutOfBoundsException x)
100       {
101         // probable race condition. just finish and return without any fuss.
102         calcMan.workerComplete(this);
103         return;
104       }
105       updateResultAnnotation(true);
106     } catch (OutOfMemoryError error)
107     {
108       ap.raiseOOMWarning("calculating conservation", error);
109       calcMan.workerCannotRun(this);
110       // alignViewport.conservation = null;
111       // this.alignViewport.quality = null;
112
113     }
114     calcMan.workerComplete(this);
115
116     if (ap != null)
117     {
118       ap.paintAlignment(true);
119     }
120
121   }
122
123   private void updateResultAnnotation(boolean b)
124   {
125     if (b || !calcMan.isWorking(this) && cons != null
126             && conservation != null && quality != null)
127       cons.completeAnnotations(conservation, quality, 0, alWidth);
128   }
129
130   @Override
131   public void updateAnnotation()
132   {
133     updateResultAnnotation(false);
134
135   }
136 }