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 (Consensus"+Thread.currentThread().getName()+") Waiting around.");
66 ap.paintAlignment(false);
69 } catch (Exception ex)
74 if (alignViewport.isClosed())
79 AlignmentI alignment = alignViewport.getAlignment();
83 if (alignment == null || (aWidth = alignment.getWidth()) < 0)
85 calcMan.workerComplete(this);
89 eraseConsensus(aWidth);
90 computeConsensus(alignment);
91 updateResultAnnotation(true);
95 ap.paintAlignment(true);
97 } catch (OutOfMemoryError error)
99 calcMan.disableWorker(this);
100 ap.raiseOOMWarning("calculating consensus", error);
104 * e.g. ArrayIndexOutOfBoundsException can happen due to a race condition
105 * - alignment was edited at same time as calculation was running
107 calcMan.workerComplete(this);
112 * Clear out any existing consensus annotations
115 * the width (number of columns) of the annotated alignment
117 protected void eraseConsensus(int aWidth)
119 AlignmentAnnotation consensus = getConsensusAnnotation();
120 consensus.annotations = new Annotation[aWidth];
121 AlignmentAnnotation gap = getGapAnnotation();
124 gap.annotations = new Annotation[aWidth];
131 protected void computeConsensus(AlignmentI alignment)
134 SequenceI[] aseqs = getSequences();
135 int width = alignment.getWidth();
136 ProfilesI hconsensus = AAFrequency.calculate(aseqs, width, 0, width,
139 alignViewport.setSequenceConsensusHash(hconsensus);
140 setColourSchemeConsensus(hconsensus);
146 protected SequenceI[] getSequences()
148 return alignViewport.getAlignment().getSequencesArray();
154 protected void setColourSchemeConsensus(ProfilesI hconsensus)
156 ResidueShaderI cs = alignViewport.getResidueShading();
159 cs.setConsensus(hconsensus);
164 * Get the Consensus annotation for the alignment
168 protected AlignmentAnnotation getConsensusAnnotation()
170 return alignViewport.getAlignmentConsensusAnnotation();
174 * Get the Gap annotation for the alignment
178 protected AlignmentAnnotation getGapAnnotation()
180 return alignViewport.getAlignmentGapAnnotation();
184 * update the consensus annotation from the sequence profile data using
185 * current visualization settings.
188 public void updateAnnotation()
190 updateResultAnnotation(false);
193 public void updateResultAnnotation(boolean immediate)
195 AlignmentAnnotation consensus = getConsensusAnnotation();
196 ProfilesI hconsensus = (ProfilesI) getViewportConsensus();
197 if (immediate || !calcMan.isWorking(this) && consensus != null
198 && hconsensus != null)
200 deriveConsensus(consensus, hconsensus);
201 AlignmentAnnotation gap = getGapAnnotation();
204 deriveGap(gap, hconsensus);
210 * Convert the computed consensus data into the desired annotation for
213 * @param consensusAnnotation
214 * the annotation to be populated
216 * the computed consensus data
218 protected void deriveConsensus(AlignmentAnnotation consensusAnnotation,
219 ProfilesI hconsensus)
222 long nseq = getSequences().length;
223 AAFrequency.completeConsensus(consensusAnnotation, hconsensus,
224 hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
225 alignViewport.isIgnoreGapsConsensus(),
226 alignViewport.isShowSequenceLogo(), nseq);
230 * Convert the computed consensus data into a gap annotation row for display.
232 * @param gapAnnotation
233 * the annotation to be populated
235 * the computed consensus data
237 protected void deriveGap(AlignmentAnnotation gapAnnotation,
238 ProfilesI hconsensus)
240 long nseq = getSequences().length;
241 AAFrequency.completeGapAnnot(gapAnnotation, hconsensus,
242 hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
247 * Get the consensus data stored on the viewport.
251 protected Object getViewportConsensus()
253 // TODO convert ComplementConsensusThread to use Profile
254 return alignViewport.getSequenceConsensusHash();