JAL-1620 version bump and release notes
[jalview.git] / src / jalview / workers / ConsensusThread.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2b1)
3  * Copyright (C) 2014 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   private long nseq = -1;
39
40   public ConsensusThread(AlignViewportI alignViewport,
41           AlignmentViewPanel alignPanel)
42   {
43     super(alignViewport, alignPanel);
44   }
45
46   @Override
47   public void run()
48   {
49     if (calcMan.isPending(this))
50     {
51       return;
52     }
53     calcMan.notifyStart(this);
54     long started = System.currentTimeMillis();
55     try
56     {
57       AlignmentAnnotation consensus = alignViewport
58               .getAlignmentConsensusAnnotation();
59       if (consensus == null || calcMan.isPending(this))
60       {
61         calcMan.workerComplete(this);
62         return;
63       }
64       while (!calcMan.notifyWorking(this))
65       {
66         // System.err.println("Thread (Consensus"+Thread.currentThread().getName()+") Waiting around.");
67         try
68         {
69           if (ap != null)
70           {
71             ap.paintAlignment(false);
72           }
73           Thread.sleep(200);
74         } catch (Exception ex)
75         {
76           ex.printStackTrace();
77         }
78       }
79       if (alignViewport.isClosed())
80       {
81         abortAndDestroy();
82         return;
83       }
84       AlignmentI alignment = alignViewport.getAlignment();
85
86       int aWidth = -1;
87
88       if (alignment == null || (aWidth = alignment.getWidth()) < 0)
89       {
90         calcMan.workerComplete(this);
91         // .updatingConservation = false;
92         // AlignViewport.UPDATING_CONSERVATION = false;
93
94         return;
95       }
96       consensus = alignViewport.getAlignmentConsensusAnnotation();
97
98       consensus.annotations = null;
99       consensus.annotations = new Annotation[aWidth];
100       Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash();
101       hconsensus = new Hashtable[aWidth];
102       try
103       {
104         SequenceI aseqs[] = alignment.getSequencesArray();
105         nseq = aseqs.length;
106         AAFrequency.calculate(aseqs, 0, alignment.getWidth(), hconsensus,
107                 true);
108       } catch (ArrayIndexOutOfBoundsException x)
109       {
110         // this happens due to a race condition -
111         // alignment was edited at same time as calculation was running
112         //
113         // calcMan.workerCannotRun(this);
114         calcMan.workerComplete(this);
115         return;
116       }
117       alignViewport.setSequenceConsensusHash(hconsensus);
118       updateResultAnnotation(true);
119       ColourSchemeI globalColourScheme = alignViewport
120               .getGlobalColourScheme();
121       if (globalColourScheme != null)
122       {
123         globalColourScheme.setConsensus(hconsensus);
124       }
125
126     } catch (OutOfMemoryError error)
127     {
128       calcMan.workerCannotRun(this);
129
130       // consensus = null;
131       // hconsensus = null;
132       ap.raiseOOMWarning("calculating consensus", error);
133     }
134
135     calcMan.workerComplete(this);
136     if (ap != null)
137     {
138       ap.paintAlignment(true);
139     }
140   }
141
142   /**
143    * update the consensus annotation from the sequence profile data using
144    * current visualization settings.
145    */
146   @Override
147   public void updateAnnotation()
148   {
149     updateResultAnnotation(false);
150   }
151
152   public void updateResultAnnotation(boolean immediate)
153   {
154     AlignmentAnnotation consensus = alignViewport
155             .getAlignmentConsensusAnnotation();
156     Hashtable[] hconsensus = alignViewport.getSequenceConsensusHash();
157     if (immediate || !calcMan.isWorking(this) && consensus != null
158             && hconsensus != null)
159     {
160       AAFrequency.completeConsensus(consensus, hconsensus, 0,
161               hconsensus.length, alignViewport.getIgnoreGapsConsensus(),
162               alignViewport.isShowSequenceLogo(), nseq);
163     }
164   }
165 }