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