JAL-3878 Add getCalcName to AlignCalcWorkerI.
[jalview.git] / src / jalview / workers / ConsensusThread.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.AAFrequency;
24 import jalview.api.AlignViewportI;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.datamodel.AlignmentAnnotation;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.Annotation;
29 import jalview.datamodel.ProfilesI;
30 import jalview.datamodel.SequenceI;
31 import jalview.renderer.ResidueShaderI;
32
33 public class ConsensusThread extends AlignCalcWorker
34 {
35   public ConsensusThread(AlignViewportI alignViewport,
36           AlignmentViewPanel alignPanel)
37   {
38     super(alignViewport, alignPanel);
39   }
40
41   @Override
42   public String getCalcName()
43   {
44     return "Consensus";
45   }
46
47   @Override
48   public void run()
49   {
50     AlignmentAnnotation consensus = getConsensusAnnotation();
51     AlignmentAnnotation gap = getGapAnnotation();
52     if ((consensus == null && gap == null))
53     {
54       return;
55     }
56     if (alignViewport.isClosed())
57     {
58       abortAndDestroy();
59       return;
60     }
61     AlignmentI alignment = alignViewport.getAlignment();
62
63     int aWidth = -1;
64
65     if (alignment == null || (aWidth = alignment.getWidth()) < 0)
66     {
67       return;
68     }
69
70     eraseConsensus(aWidth);
71     computeConsensus(alignment);
72     updateResultAnnotation(true);
73
74     if (ap != null)
75     {
76       ap.paintAlignment(true, true);
77     }
78   }
79
80   /**
81    * Clear out any existing consensus annotations
82    * 
83    * @param aWidth
84    *          the width (number of columns) of the annotated alignment
85    */
86   protected void eraseConsensus(int aWidth)
87   {
88     AlignmentAnnotation consensus = getConsensusAnnotation();
89     if (consensus != null)
90     {
91       consensus.annotations = new Annotation[aWidth];
92     }
93     AlignmentAnnotation gap = getGapAnnotation();
94     if (gap != null)
95     {
96       gap.annotations = new Annotation[aWidth];
97     }
98   }
99
100   /**
101    * @param alignment
102    */
103   protected void computeConsensus(AlignmentI alignment)
104   {
105
106     SequenceI[] aseqs = getSequences();
107     int width = alignment.getWidth();
108     ProfilesI hconsensus = AAFrequency.calculate(aseqs, width, 0, width,
109             true);
110
111     alignViewport.setSequenceConsensusHash(hconsensus);
112     setColourSchemeConsensus(hconsensus);
113   }
114
115   /**
116    * @return
117    */
118   protected SequenceI[] getSequences()
119   {
120     return alignViewport.getAlignment().getSequencesArray();
121   }
122
123   /**
124    * @param hconsensus
125    */
126   protected void setColourSchemeConsensus(ProfilesI hconsensus)
127   {
128     ResidueShaderI cs = alignViewport.getResidueShading();
129     if (cs != null)
130     {
131       cs.setConsensus(hconsensus);
132     }
133   }
134
135   /**
136    * Get the Consensus annotation for the alignment
137    * 
138    * @return
139    */
140   protected AlignmentAnnotation getConsensusAnnotation()
141   {
142     return alignViewport.getAlignmentConsensusAnnotation();
143   }
144
145   /**
146    * Get the Gap annotation for the alignment
147    * 
148    * @return
149    */
150   protected AlignmentAnnotation getGapAnnotation()
151   {
152     return alignViewport.getAlignmentGapAnnotation();
153   }
154
155   /**
156    * update the consensus annotation from the sequence profile data using
157    * current visualization settings.
158    */
159   @Override
160   public void updateAnnotation()
161   {
162     updateResultAnnotation(false);
163   }
164
165   public void updateResultAnnotation(boolean immediate)
166   {
167     AlignmentAnnotation consensus = getConsensusAnnotation();
168     ProfilesI hconsensus = (ProfilesI) getViewportConsensus();
169     if (immediate || !calcMan.isWorking(this) && consensus != null
170             && hconsensus != null)
171     {
172       deriveConsensus(consensus, hconsensus);
173       AlignmentAnnotation gap = getGapAnnotation();
174       if (gap != null)
175       {
176         deriveGap(gap, hconsensus);
177       }
178     }
179   }
180
181   /**
182    * Convert the computed consensus data into the desired annotation for
183    * display.
184    * 
185    * @param consensusAnnotation
186    *          the annotation to be populated
187    * @param hconsensus
188    *          the computed consensus data
189    */
190   protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
191           ProfilesI hconsensus)
192   {
193     long nseq = getSequences().length;
194     AAFrequency.completeConsensus(consensusAnnotation, hconsensus,
195             hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
196             alignViewport.isIgnoreGapsConsensus(),
197             alignViewport.isShowSequenceLogo(), nseq);
198   }
199
200   /**
201    * Convert the computed consensus data into a gap annotation row for display.
202    * 
203    * @param gapAnnotation
204    *          the annotation to be populated
205    * @param hconsensus
206    *          the computed consensus data
207    */
208   protected void deriveGap(AlignmentAnnotation gapAnnotation,
209           ProfilesI hconsensus)
210   {
211     long nseq = getSequences().length;
212     AAFrequency.completeGapAnnot(gapAnnotation, hconsensus,
213             hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
214             nseq);
215   }
216
217   /**
218    * Get the consensus data stored on the viewport.
219    * 
220    * @return
221    */
222   protected Object getViewportConsensus()
223   {
224     // TODO convert ComplementConsensusThread to use Profile
225     return alignViewport.getSequenceConsensusHash();
226   }
227 }