JAL-3878 Add getCalcName to AlignCalcWorkerI.
[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.AlignViewportI;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28
29 import java.util.ArrayList;
30 import java.util.List;
31
32 public class ConservationThread extends AlignCalcWorker
33 {
34
35   private int ConsPercGaps = 25; // JBPNote : This should be a configurable
36                                  // property!
37
38   public ConservationThread(AlignViewportI alignViewport,
39           AlignmentViewPanel alignPanel)
40   {
41     super(alignViewport, alignPanel);
42     ConsPercGaps = alignViewport.getConsPercGaps();
43   }
44
45   private Conservation cons;
46
47   AlignmentAnnotation conservation, quality;
48
49   int alWidth;
50
51   @Override
52   public String getCalcName()
53   {
54     return "Conservation";
55   }
56
57   @Override
58   public void run()
59   {
60     if ((alignViewport == null) || (calcMan == null)
61             || (alignViewport.isClosed()))
62     {
63       abortAndDestroy();
64       return;
65     }
66     List<AlignmentAnnotation> ourAnnot = new ArrayList<>();
67     AlignmentI alignment = alignViewport.getAlignment();
68     conservation = alignViewport.getAlignmentConservationAnnotation();
69     quality = alignViewport.getAlignmentQualityAnnot();
70     ourAnnot.add(conservation);
71     ourAnnot.add(quality);
72     ourAnnots = ourAnnot;
73     ConsPercGaps = alignViewport.getConsPercGaps();
74     // AlignViewport.UPDATING_CONSERVATION = true;
75
76     if (alignment == null || (alWidth = alignment.getWidth()) < 0)
77     {
78       // .updatingConservation = false;
79       // AlignViewport.UPDATING_CONSERVATION = false;
80       return;
81     }
82     try
83     {
84       cons = Conservation.calculateConservation("All",
85               alignment.getSequences(), 0, alWidth - 1, false,
86               ConsPercGaps, quality != null);
87     } catch (IndexOutOfBoundsException x)
88     {
89       // probable race condition. just finish and return without any fuss.
90       return;
91     }
92     updateResultAnnotation(true);
93
94     if ((alignViewport == null) || (calcMan == null)
95             || (alignViewport.isClosed()))
96     {
97       abortAndDestroy();
98       return;
99     }
100     if (ap != null)
101     {
102       ap.paintAlignment(true, true);
103     }
104
105   }
106
107   private void updateResultAnnotation(boolean b)
108   {
109     if (b || !calcMan.isWorking(this) && cons != null
110             && conservation != null && quality != null)
111     {
112       alignViewport.setConservation(cons);
113       cons.completeAnnotations(conservation, quality, 0, alWidth);
114     }
115   }
116
117   @Override
118   public void updateAnnotation()
119   {
120     updateResultAnnotation(false);
121
122   }
123 }