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.api.AlignViewportI;
24 import jalview.api.AlignmentViewPanel;
25 import jalview.datamodel.AlignmentAnnotation;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.Annotation;
28 import jalview.datamodel.SequenceFeature;
29 import jalview.datamodel.SequenceI;
30 import jalview.renderer.seqfeatures.FeatureRenderer;
31 import jalview.util.ColorUtils;
32 import jalview.util.Comparison;
34 import java.awt.Color;
35 import java.util.ArrayList;
36 import java.util.List;
39 * A class to compute an alignment annotation with column counts of any
40 * properties of interest of positions in an alignment. <br>
41 * This is designed to be extensible, by supplying to the constructor an object
42 * that computes a count for each residue position, based on the residue value
43 * and any sequence features at that position.
46 class ColumnCounterWorker extends AlignCalcWorker
48 FeatureCounterI counter;
51 * Constructor registers the annotation for the given alignment frame
56 public ColumnCounterWorker(AlignViewportI viewport,
57 AlignmentViewPanel panel, FeatureCounterI counter)
59 super(viewport, panel);
60 ourAnnots = new ArrayList<AlignmentAnnotation>();
61 this.counter = counter;
62 calcMan.registerWorker(this);
66 * method called under control of AlignCalcManager to recompute the annotation
67 * when the alignment changes
74 calcMan.notifyStart(this);
76 while (!calcMan.notifyWorking(this))
81 } catch (InterruptedException ex)
86 if (alignViewport.isClosed())
92 if (alignViewport.getAlignment() != null)
97 } catch (IndexOutOfBoundsException x)
99 // probable race condition. just finish and return without any fuss.
103 } catch (OutOfMemoryError error)
105 ap.raiseOOMWarning("calculating feature counts", error);
106 calcMan.disableWorker(this);
109 calcMan.workerComplete(this);
114 ap.adjustAnnotationHeight();
115 ap.paintAlignment(true);
121 * Scan each column of the alignment to calculate a count by feature type. Set
122 * the count as the value of the alignment annotation for that feature type.
124 void computeAnnotations()
126 FeatureRenderer fr = new FeatureRenderer(alignViewport);
127 // TODO use the commented out code once JAL-2075 is fixed
128 // to get adequate performance on genomic length sequence
129 AlignmentI alignment = alignViewport.getAlignment();
130 // AlignmentView alignmentView = alignViewport.getAlignmentView(false);
131 // AlignmentI alignment = alignmentView.getVisibleAlignment(' ');
133 // int width = alignmentView.getWidth();
134 int width = alignment.getWidth();
135 int height = alignment.getHeight();
136 int[] counts = new int[width];
139 for (int col = 0; col < width; col++)
142 for (int row = 0; row < height; row++)
144 count += countFeaturesAt(alignment, col, row, fr);
147 max = Math.max(count, max);
150 Annotation[] anns = new Annotation[width];
152 * add non-zero counts as annotations
154 for (int i = 0; i < counts.length; i++)
156 int count = counts[i];
159 Color color = ColorUtils.getGraduatedColour(count, 0, Color.cyan,
161 String str = String.valueOf(count);
162 anns[i] = new Annotation(str, str, '0', count, color);
167 * construct or update the annotation
169 AlignmentAnnotation ann = alignViewport.getAlignment()
170 .findOrCreateAnnotation(counter.getName(),
171 counter.getDescription(), false, null, null);
172 ann.description = counter.getDescription();
173 ann.showAllColLabels = true;
174 ann.scaleColLabel = true;
175 ann.graph = AlignmentAnnotation.BAR_GRAPH;
176 ann.annotations = anns;
177 setGraphMinMax(ann, anns);
178 ann.validateRangeAndDisplay();
179 if (!ourAnnots.contains(ann))
186 * Returns a count of any feature types present at the specified position of
194 int countFeaturesAt(AlignmentI alignment, int col, int row,
197 SequenceI seq = alignment.getSequenceAt(row);
202 if (col >= seq.getLength())
204 return 0;// sequence doesn't extend this far
206 char res = seq.getCharAt(col);
207 if (Comparison.isGap(res))
211 int pos = seq.findPosition(col);
214 * compute a count for any displayed features at residue
216 // NB have to adjust pos if using AlignmentView.getVisibleAlignment
218 List<SequenceFeature> features = fr.findFeaturesAtRes(seq, pos);
219 int count = this.counter.count(String.valueOf(res), features);
224 * Method called when the user changes display options that may affect how the
225 * annotation is rendered, but do not change its values. Currently no such
226 * options affect user-defined annotation, so this method does nothing.
229 public void updateAnnotation()
235 * Answers true to indicate that if this worker's annotation is deleted from
236 * the display, the worker should also be removed. This prevents it running
237 * and recreating the annotation when the alignment changes.
240 public boolean isDeletable()