debe45d81fad11be9142e0afc87f8789e855547a
[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.schemes.ColourSchemeI;
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 void run()
43   {
44     if (calcMan.isPending(this))
45     {
46       return;
47     }
48     calcMan.notifyStart(this);
49     long started = System.currentTimeMillis();
50     try
51     {
52       AlignmentAnnotation consensus = getConsensusAnnotation();
53       if (consensus == null || calcMan.isPending(this))
54       {
55         calcMan.workerComplete(this);
56         return;
57       }
58       while (!calcMan.notifyWorking(this))
59       {
60         // System.err.println("Thread (Consensus"+Thread.currentThread().getName()+") Waiting around.");
61         try
62         {
63           if (ap != null)
64           {
65             ap.paintAlignment(false);
66           }
67           Thread.sleep(200);
68         } catch (Exception ex)
69         {
70           ex.printStackTrace();
71         }
72       }
73       if (alignViewport.isClosed())
74       {
75         abortAndDestroy();
76         return;
77       }
78       AlignmentI alignment = alignViewport.getAlignment();
79
80       int aWidth = -1;
81
82       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
83       {
84         calcMan.workerComplete(this);
85         return;
86       }
87
88       eraseConsensus(aWidth);
89       computeConsensus(alignment);
90       updateResultAnnotation(true);
91
92       if (ap != null)
93       {
94         ap.paintAlignment(true);
95       }
96     } catch (OutOfMemoryError error)
97     {
98       calcMan.disableWorker(this);
99       ap.raiseOOMWarning("calculating consensus", error);
100     } finally
101     {
102       /*
103        * e.g. ArrayIndexOutOfBoundsException can happen due to a race condition
104        * - alignment was edited at same time as calculation was running
105        */
106       calcMan.workerComplete(this);
107     }
108   }
109
110   /**
111    * Clear out any existing consensus annotations
112    * 
113    * @param aWidth
114    *          the width (number of columns) of the annotated alignment
115    */
116   protected void eraseConsensus(int aWidth)
117   {
118     AlignmentAnnotation consensus = getConsensusAnnotation();
119     consensus.annotations = new Annotation[aWidth];
120   }
121
122   /**
123    * @param alignment
124    */
125   protected void computeConsensus(AlignmentI alignment)
126   {
127
128     SequenceI[] aseqs = getSequences();
129     int width = alignment.getWidth();
130     ProfilesI hconsensus = AAFrequency.calculate(aseqs, width, 0,
131             width, true);
132
133     alignViewport.setSequenceConsensusHash(hconsensus);
134     setColourSchemeConsensus(hconsensus);
135   }
136
137   /**
138    * @return
139    */
140   protected SequenceI[] getSequences()
141   {
142     return alignViewport.getAlignment().getSequencesArray();
143   }
144
145   /**
146    * @param hconsensus
147    */
148   protected void setColourSchemeConsensus(ProfilesI hconsensus)
149   {
150     ColourSchemeI globalColourScheme = alignViewport
151             .getGlobalColourScheme();
152     if (globalColourScheme != null)
153     {
154       globalColourScheme.setConsensus(hconsensus);
155     }
156   }
157
158   /**
159    * Get the Consensus annotation for the alignment
160    * 
161    * @return
162    */
163   protected AlignmentAnnotation getConsensusAnnotation()
164   {
165     return alignViewport.getAlignmentConsensusAnnotation();
166   }
167
168   /**
169    * update the consensus annotation from the sequence profile data using
170    * current visualization settings.
171    */
172   @Override
173   public void updateAnnotation()
174   {
175     updateResultAnnotation(false);
176   }
177
178   public void updateResultAnnotation(boolean immediate)
179   {
180     AlignmentAnnotation consensus = getConsensusAnnotation();
181     ProfilesI hconsensus = (ProfilesI) getViewportConsensus();
182     if (immediate || !calcMan.isWorking(this) && consensus != null
183             && hconsensus != null)
184     {
185       deriveConsensus(consensus, hconsensus);
186     }
187   }
188
189   /**
190    * Convert the computed consensus data into the desired annotation for
191    * display.
192    * 
193    * @param consensusAnnotation
194    *          the annotation to be populated
195    * @param hconsensus
196    *          the computed consensus data
197    */
198   protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
199           ProfilesI hconsensus)
200   {
201
202     long nseq = getSequences().length;
203     AAFrequency.completeConsensus(consensusAnnotation, hconsensus,
204             hconsensus.getStartColumn(),
205             hconsensus.getEndColumn() + 1,
206             alignViewport.isIgnoreGapsConsensus(),
207             alignViewport.isShowSequenceLogo(), nseq);
208   }
209
210   /**
211    * Get the consensus data stored on the viewport.
212    * 
213    * @return
214    */
215   protected Object getViewportConsensus()
216   {
217     // TODO convert ComplementConsensusThread to use Profile
218     return alignViewport.getSequenceConsensusHash();
219   }
220 }