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.ProfilesI;
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)
128 SequenceI[] aseqs = getSequences();
129 int width = alignment.getWidth();
130 ProfilesI hconsensus = AAFrequency.calculate(aseqs, width, 0,
133 alignViewport.setSequenceConsensusHash(hconsensus);
134 setColourSchemeConsensus(hconsensus);
140 protected SequenceI[] getSequences()
142 return alignViewport.getAlignment().getSequencesArray();
148 protected void setColourSchemeConsensus(ProfilesI hconsensus)
150 ColourSchemeI globalColourScheme = alignViewport
151 .getGlobalColourScheme();
152 if (globalColourScheme != null)
154 globalColourScheme.setConsensus(hconsensus);
159 * Get the Consensus annotation for the alignment
163 protected AlignmentAnnotation getConsensusAnnotation()
165 return alignViewport.getAlignmentConsensusAnnotation();
169 * update the consensus annotation from the sequence profile data using
170 * current visualization settings.
173 public void updateAnnotation()
175 updateResultAnnotation(false);
178 public void updateResultAnnotation(boolean immediate)
180 AlignmentAnnotation consensus = getConsensusAnnotation();
181 ProfilesI hconsensus = (ProfilesI) getViewportConsensus();
182 if (immediate || !calcMan.isWorking(this) && consensus != null
183 && hconsensus != null)
185 deriveConsensus(consensus, hconsensus);
190 * Convert the computed consensus data into the desired annotation for
193 * @param consensusAnnotation
194 * the annotation to be populated
196 * the computed consensus data
198 protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
199 ProfilesI hconsensus)
202 long nseq = getSequences().length;
203 AAFrequency.completeConsensus(consensusAnnotation, hconsensus,
204 hconsensus.getStartColumn(),
205 hconsensus.getEndColumn() + 1,
206 alignViewport.isIgnoreGapsConsensus(),
207 alignViewport.isShowSequenceLogo(), nseq);
211 * Get the consensus data stored on the viewport.
215 protected Object getViewportConsensus()
217 // TODO convert ComplementConsensusThread to use Profile
218 return alignViewport.getSequenceConsensusHash();