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 SecondaryStructureConsensusThread extends AlignCalcWorker
35 public SecondaryStructureConsensusThread(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 ssConsensus = getSSConsensusAnnotation();
53 AlignmentAnnotation gap = getGapAnnotation();
54 if ((ssConsensus == null && gap == null) || calcMan.isPending(this))
56 calcMan.workerComplete(this);
59 while (!calcMan.notifyWorking(this))
65 ap.paintAlignment(false, 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 eraseSSConsensus(aWidth);
89 computeSSConsensus(alignment);
90 updateResultAnnotation(true);
94 ap.paintAlignment(true, 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 eraseSSConsensus(int aWidth)
118 AlignmentAnnotation ssConsensus = getSSConsensusAnnotation();
119 if (ssConsensus != null)
121 ssConsensus.annotations = new Annotation[aWidth];
123 AlignmentAnnotation gap = getGapAnnotation();
126 gap.annotations = new Annotation[aWidth];
133 protected void computeSSConsensus(AlignmentI alignment)
136 SequenceI[] aseqs = getSequences();
137 int width = alignment.getWidth();
138 ProfilesI hSSConsensus = AAFrequency.calculateSS(aseqs, width, 0, width,
141 alignViewport.setSequenceSSConsensusHash(hSSConsensus);
142 setColourSchemeConsensus(hSSConsensus);
148 protected SequenceI[] getSequences()
150 return alignViewport.getAlignment().getSequencesArray();
156 protected void setColourSchemeConsensus(ProfilesI hSSconsensus)
158 ResidueShaderI cs = alignViewport.getResidueShading();
161 cs.setSsConsensus(hSSconsensus);
166 * Get the Consensus annotation for the alignment
170 protected AlignmentAnnotation getSSConsensusAnnotation()
172 return alignViewport.getAlignmentSecondaryStructureConsensusAnnotation();
176 * Get the Gap annotation for the alignment
180 protected AlignmentAnnotation getGapAnnotation()
182 return alignViewport.getAlignmentGapAnnotation();
186 * update the consensus annotation from the sequence profile data using
187 * current visualization settings.
190 public void updateAnnotation()
192 updateResultAnnotation(false);
195 public void updateResultAnnotation(boolean immediate)
197 AlignmentAnnotation ssConsensus = getSSConsensusAnnotation();
198 ProfilesI hSSConsensus = (ProfilesI) getViewportSSConsensus();
199 if (immediate || !calcMan.isWorking(this) && ssConsensus != null
200 && hSSConsensus != null)
202 deriveSSConsensus(ssConsensus, hSSConsensus);
203 AlignmentAnnotation gap = getGapAnnotation();
206 deriveGap(gap, hSSConsensus);
212 * Convert the computed consensus data into the desired annotation for
215 * @param consensusAnnotation
216 * the annotation to be populated
218 * the computed consensus data
220 protected void deriveSSConsensus(AlignmentAnnotation ssConsensusAnnotation,
221 ProfilesI hSSConsensus)
224 long nseq = getSequences().length;
225 AAFrequency.completeSSConsensus(ssConsensusAnnotation, hSSConsensus,
226 hSSConsensus.getStartColumn(), hSSConsensus.getEndColumn() + 1,
227 alignViewport.isIgnoreGapsConsensus(),
228 alignViewport.isShowSequenceLogo(), nseq);
232 * Convert the computed consensus data into a gap annotation row for display.
234 * @param gapAnnotation
235 * the annotation to be populated
237 * the computed consensus data
239 protected void deriveGap(AlignmentAnnotation gapAnnotation,
240 ProfilesI hconsensus)
242 long nseq = getSequences().length;
243 AAFrequency.completeGapAnnot(gapAnnotation, hconsensus,
244 hconsensus.getStartColumn(), hconsensus.getEndColumn() + 1,
249 * Get the consensus data stored on the viewport.
253 protected Object getViewportSSConsensus()
255 // TODO convert ComplementConsensusThread to use Profile
256 return alignViewport.getSequenceSSConsensusHash();