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, 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, 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 if (consensus != null)
123 consensus.annotations = new Annotation[aWidth];
125 AlignmentAnnotation gap = getGapAnnotation();
128 gap.annotations = new Annotation[aWidth];
135 protected void computeConsensus(AlignmentI alignment)
138 SequenceI[] aseqs = getSequences();
139 int width = alignment.getWidth();
140 ProfilesI hconsensus = AAFrequency.calculate(aseqs, width, 0, width,
143 alignViewport.setSequenceConsensusHash(hconsensus);
144 setColourSchemeConsensus(hconsensus);
150 protected SequenceI[] getSequences()
152 return alignViewport.getAlignment().getSequencesArray();
158 protected void setColourSchemeConsensus(ProfilesI hconsensus)
160 ResidueShaderI cs = alignViewport.getResidueShading();
163 cs.setConsensus(hconsensus);
168 * Get the Consensus annotation for the alignment
172 protected AlignmentAnnotation getConsensusAnnotation()
174 return alignViewport.getAlignmentConsensusAnnotation();
178 * Get the Gap annotation for the alignment
182 protected AlignmentAnnotation getGapAnnotation()
184 return alignViewport.getAlignmentGapAnnotation();
188 * update the consensus annotation from the sequence profile data using
189 * current visualization settings.
192 public void updateAnnotation()
194 updateResultAnnotation(false);
197 public void updateResultAnnotation(boolean immediate)
199 AlignmentAnnotation consensus = getConsensusAnnotation();
200 ProfilesI hconsensus = (ProfilesI) getViewportConsensus();
201 if (immediate || !calcMan.isWorking(this) && consensus != null
202 && hconsensus != null)
204 deriveConsensus(consensus, hconsensus);
205 AlignmentAnnotation gap = getGapAnnotation();
208 deriveGap(gap, hconsensus);
214 * Convert the computed consensus data into the desired annotation for
217 * @param consensusAnnotation
218 * the annotation to be populated
220 * the computed consensus data
222 protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
223 ProfilesI hconsensus)
226 long nseq = getSequences().length;
227 AAFrequency.completeConsensus(consensusAnnotation, hconsensus,
228 hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
229 alignViewport.isIgnoreGapsConsensus(),
230 alignViewport.isShowSequenceLogo(), nseq);
234 * Convert the computed consensus data into a gap annotation row for display.
236 * @param gapAnnotation
237 * the annotation to be populated
239 * the computed consensus data
241 protected void deriveGap(AlignmentAnnotation gapAnnotation,
242 ProfilesI hconsensus)
244 long nseq = getSequences().length;
245 AAFrequency.completeGapAnnot(gapAnnotation, hconsensus,
246 hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
251 * Get the consensus data stored on the viewport.
255 protected Object getViewportConsensus()
257 // TODO convert ComplementConsensusThread to use Profile
258 return alignViewport.getSequenceConsensusHash();