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 java.awt.Color;
24 import java.beans.PropertyChangeEvent;
25 import java.beans.PropertyChangeListener;
26 import java.util.Collections;
27 import java.util.HashSet;
28 import java.util.List;
32 import jalview.api.AlignViewportI;
33 import jalview.api.FeatureColourI;
34 import jalview.api.FeaturesDisplayedI;
35 import jalview.datamodel.SequenceFeature;
36 import jalview.gui.FeatureRenderer;
38 public class VisibleFeaturesAnnotationTracks implements FeatureSetCounterI
40 AlignViewportI ourViewport = null;
42 jalview.api.FeatureRenderer ourFr = null;
44 PropertyChangeListener ourListener = new PropertyChangeListener()
48 public void propertyChange(PropertyChangeEvent evt)
50 if (ourViewport != null) // could
54 // ourFr.getChangeSupport...
56 updateFeatureAnnotationTracks();
61 public VisibleFeaturesAnnotationTracks(AlignViewportI viewport,
64 ourViewport = viewport;
69 void registerListener()
71 ourFr.addPropertyChangeListener(ourListener);
78 ourFr.removePropertyChangeListener(ourListener);
80 if (ourViewport != null && ourViewport.getCalcManager() != null)
82 if (ourWorker != null)
84 ourWorker.abortAndDestroy();
85 ourViewport.getCalcManager()
86 .removeRegisteredWorkersOfClass(ourWorker.getClass());
93 public void updateFeatureAnnotationTracks()
95 // if tracks are turned off, this returns null.
96 FeaturesDisplayedI featuresDisp = ourViewport
97 .isShowSequenceFeatureCounts()
98 ? ourViewport.getFeaturesDisplayed()
100 // get latest FeatureRenderer, just in case it's different.
101 ourFr = ourFr.getAlignPanel().getFeatureRenderer();
103 Set<String> visibleFeatures = new HashSet();
104 if (featuresDisp != null)
106 visibleFeatures.addAll(featuresDisp.getVisibleFeatures());
108 if (dispFeatures.equals(visibleFeatures))
110 // all the same features displayed
113 // otherwise set up tracks accordingly
115 int[][] minC = new int[visibleFeatures.size()][3],
116 maxC = new int[visibleFeatures.size()][3];
117 Map<String, FeatureColourI> fcs = ourFr.getDisplayedFeatureCols();
119 for (String s : visibleFeatures)
121 FeatureColourI color = fcs.get(s);
122 if (color.isSimpleColour())
124 minC[p] = new int[] { 133, 133, 133 };
125 maxC[p] = new int[] { color.getColour().getRed(),
126 color.getColour().getGreen(), color.getColour().getBlue() };
130 Color min = color.getMinColour(), max = color.getMaxColour();
131 minC[p] = new int[] { min.getRed(), min.getGreen(), min.getBlue() };
132 maxC[p] = new int[] { max.getRed(), max.getGreen(), max.getBlue() };
139 * and register the counter
141 if (ourWorker != null)
143 Set<String> toRemove = new HashSet<String>();
144 toRemove.addAll(dispFeatures);
145 toRemove.removeAll(visibleFeatures);
146 dispFeatures = visibleFeatures;
147 ourWorker.removeOldAnnotations(toRemove.toArray(new String[0]));
152 dispFeatures = visibleFeatures;
153 ourWorker = new ColumnCounterSetWorker(ourViewport,
154 ourFr.getAlignPanel(), this);
156 ourViewport.getCalcManager().registerWorker(ourWorker);
157 ourViewport.getCalcManager().startWorker(ourWorker);
160 ColumnCounterSetWorker ourWorker = null;
162 Set<String> dispFeatures = Collections.EMPTY_SET;
165 public int[] count(String residue, List<SequenceFeature> features)
167 final Set<String> ourDispFeatures = dispFeatures;
168 int[] obs = new int[ourDispFeatures.size()];
169 SequenceFeature[] sfs = features.toArray(new SequenceFeature[0]);
170 for (SequenceFeature sf : sfs)
173 * Here we inspect the type of the sequence feature.
174 * You can also test sf.description, sf.score, sf.featureGroup,
175 * sf.strand, sf.phase, sf.begin, sf.end
176 * or sf.getValue(attributeName) for GFF 'column 9' properties
179 for (String type : ourDispFeatures)
181 if (type.equals(sf.type))
192 public String[] getNames()
194 return dispFeatures.toArray(new String[0]);
198 public String[] getDescriptions()
200 return dispFeatures.toArray(new String[0]);
203 int[][] minColours = null, maxColours = null;
206 public int[] getMaxColour(int row)
208 if (maxColours != null && row >= 0 && row < maxColours.length)
210 return maxColours[row];
212 return new int[] { 0, 0, 255 };
216 public int[] getMinColour(int row)
218 if (minColours != null && row >= 0 && row < minColours.length)
220 return minColours[row];
222 return new int[] { 133, 133, 133 };