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.renderer.ResidueShaderI;
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 AlignmentAnnotation gap = getGapAnnotation();
54 if ((consensus == null && gap == null) || calcMan.isPending(this))
56 calcMan.workerComplete(this);
59 while (!calcMan.notifyWorking(this))
61 // System.err.println("Thread
62 // (Consensus"+Thread.currentThread().getName()+") Waiting around.");
67 ap.paintAlignment(false);
70 } catch (Exception ex)
75 if (alignViewport.isClosed())
80 AlignmentI alignment = alignViewport.getAlignment();
84 if (alignment == null || (aWidth = alignment.getWidth()) < 0)
86 calcMan.workerComplete(this);
90 eraseConsensus(aWidth);
91 computeConsensus(alignment);
92 updateResultAnnotation(true);
96 ap.paintAlignment(true);
98 } catch (OutOfMemoryError error)
100 calcMan.disableWorker(this);
101 ap.raiseOOMWarning("calculating consensus", error);
105 * e.g. ArrayIndexOutOfBoundsException can happen due to a race condition
106 * - alignment was edited at same time as calculation was running
108 calcMan.workerComplete(this);
113 * Clear out any existing consensus annotations
116 * the width (number of columns) of the annotated alignment
118 protected void eraseConsensus(int aWidth)
120 AlignmentAnnotation consensus = getConsensusAnnotation();
121 consensus.annotations = new Annotation[aWidth];
122 AlignmentAnnotation gap = getGapAnnotation();
125 gap.annotations = new Annotation[aWidth];
132 protected void computeConsensus(AlignmentI alignment)
135 SequenceI[] aseqs = getSequences();
136 int width = alignment.getWidth();
137 ProfilesI hconsensus = AAFrequency.calculate(aseqs, width, 0, width,
140 alignViewport.setSequenceConsensusHash(hconsensus);
141 setColourSchemeConsensus(hconsensus);
147 protected SequenceI[] getSequences()
149 return alignViewport.getAlignment().getSequencesArray();
155 protected void setColourSchemeConsensus(ProfilesI hconsensus)
157 ResidueShaderI cs = alignViewport.getResidueShading();
160 cs.setConsensus(hconsensus);
165 * Get the Consensus annotation for the alignment
169 protected AlignmentAnnotation getConsensusAnnotation()
171 return alignViewport.getAlignmentConsensusAnnotation();
175 * Get the Gap annotation for the alignment
179 protected AlignmentAnnotation getGapAnnotation()
181 return alignViewport.getAlignmentGapAnnotation();
185 * update the consensus annotation from the sequence profile data using
186 * current visualization settings.
189 public void updateAnnotation()
191 updateResultAnnotation(false);
194 public void updateResultAnnotation(boolean immediate)
196 AlignmentAnnotation consensus = getConsensusAnnotation();
197 ProfilesI hconsensus = (ProfilesI) getViewportConsensus();
198 if (immediate || !calcMan.isWorking(this) && consensus != null
199 && hconsensus != null)
201 deriveConsensus(consensus, hconsensus);
202 AlignmentAnnotation gap = getGapAnnotation();
205 deriveGap(gap, hconsensus);
211 * Convert the computed consensus data into the desired annotation for
214 * @param consensusAnnotation
215 * the annotation to be populated
217 * the computed consensus data
219 protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
220 ProfilesI hconsensus)
223 long nseq = getSequences().length;
224 AAFrequency.completeConsensus(consensusAnnotation, hconsensus,
225 hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
226 alignViewport.isIgnoreGapsConsensus(),
227 alignViewport.isShowSequenceLogo(), nseq);
231 * Convert the computed consensus data into a gap annotation row for display.
233 * @param gapAnnotation
234 * the annotation to be populated
236 * the computed consensus data
238 protected void deriveGap(AlignmentAnnotation gapAnnotation,
239 ProfilesI hconsensus)
241 long nseq = getSequences().length;
242 AAFrequency.completeGapAnnot(gapAnnotation, hconsensus,
243 hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
248 * Get the consensus data stored on the viewport.
252 protected Object getViewportConsensus()
254 // TODO convert ComplementConsensusThread to use Profile
255 return alignViewport.getSequenceConsensusHash();