2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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.
21 package jalview.workers;
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.ProfileI;
30 import jalview.datamodel.SequenceI;
31 import jalview.schemes.ColourSchemeI;
33 public class ConsensusThread extends AlignCalcWorker
35 public ConsensusThread(AlignViewportI alignViewport,
36 AlignmentViewPanel alignPanel)
38 super(alignViewport, alignPanel);
44 if (calcMan.isPending(this))
48 calcMan.notifyStart(this);
49 long started = System.currentTimeMillis();
52 AlignmentAnnotation consensus = getConsensusAnnotation();
53 if (consensus == null || calcMan.isPending(this))
55 calcMan.workerComplete(this);
58 while (!calcMan.notifyWorking(this))
60 // System.err.println("Thread (Consensus"+Thread.currentThread().getName()+") Waiting around.");
65 ap.paintAlignment(false);
68 } catch (Exception ex)
73 if (alignViewport.isClosed())
78 AlignmentI alignment = alignViewport.getAlignment();
82 if (alignment == null || (aWidth = alignment.getWidth()) < 0)
84 calcMan.workerComplete(this);
88 eraseConsensus(aWidth);
89 computeConsensus(alignment);
90 updateResultAnnotation(true);
94 ap.paintAlignment(true);
96 } catch (OutOfMemoryError error)
98 calcMan.disableWorker(this);
99 ap.raiseOOMWarning("calculating consensus", error);
103 * e.g. ArrayIndexOutOfBoundsException can happen due to a race condition
104 * - alignment was edited at same time as calculation was running
106 calcMan.workerComplete(this);
111 * Clear out any existing consensus annotations
114 * the width (number of columns) of the annotated alignment
116 protected void eraseConsensus(int aWidth)
118 AlignmentAnnotation consensus = getConsensusAnnotation();
119 consensus.annotations = new Annotation[aWidth];
125 protected void computeConsensus(AlignmentI alignment)
127 ProfileI[] hconsensus = new ProfileI[alignment.getWidth()];
129 SequenceI[] aseqs = getSequences();
130 AAFrequency.calculate(aseqs, 0, alignment.getWidth(), hconsensus, true);
132 alignViewport.setSequenceConsensusHash(hconsensus);
133 setColourSchemeConsensus(hconsensus);
139 protected SequenceI[] getSequences()
141 return alignViewport.getAlignment().getSequencesArray();
147 protected void setColourSchemeConsensus(ProfileI[] hconsensus)
149 ColourSchemeI globalColourScheme = alignViewport
150 .getGlobalColourScheme();
151 if (globalColourScheme != null)
153 globalColourScheme.setConsensus(hconsensus);
158 * Get the Consensus annotation for the alignment
162 protected AlignmentAnnotation getConsensusAnnotation()
164 return alignViewport.getAlignmentConsensusAnnotation();
168 * update the consensus annotation from the sequence profile data using
169 * current visualization settings.
172 public void updateAnnotation()
174 updateResultAnnotation(false);
177 public void updateResultAnnotation(boolean immediate)
179 AlignmentAnnotation consensus = getConsensusAnnotation();
180 ProfileI[] hconsensus = (ProfileI[]) getViewportConsensus();
181 if (immediate || !calcMan.isWorking(this) && consensus != null
182 && hconsensus != null)
184 deriveConsensus(consensus, hconsensus);
189 * Convert the computed consensus data into the desired annotation for
192 * @param consensusAnnotation
193 * the annotation to be populated
195 * the computed consensus data
197 protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
198 ProfileI[] hconsensus)
200 long nseq = getSequences().length;
201 AAFrequency.completeConsensus(consensusAnnotation, hconsensus, 0,
202 hconsensus.length, alignViewport.isIgnoreGapsConsensus(),
203 alignViewport.isShowSequenceLogo(), nseq);
207 * Get the consensus data stored on the viewport.
211 protected Object[] getViewportConsensus()
213 // TODO convert ComplementConsensusThread to use Profile
214 return alignViewport.getSequenceConsensusHash();