X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fworkers%2FVisibleFeaturesAnnotationTracks.java;fp=src%2Fjalview%2Fworkers%2FVisibleFeaturesAnnotationTracks.java;h=baa80385ccc595ab9f7376270b4a30c7b6f9aee1;hb=c3c8eb857e5544602671ea205da67f082de5a306;hp=0000000000000000000000000000000000000000;hpb=e9b0f1619b7b33b9d2ebb3f64d7cefe563c24db8;p=jalview.git
diff --git a/src/jalview/workers/VisibleFeaturesAnnotationTracks.java b/src/jalview/workers/VisibleFeaturesAnnotationTracks.java
new file mode 100644
index 0000000..baa8038
--- /dev/null
+++ b/src/jalview/workers/VisibleFeaturesAnnotationTracks.java
@@ -0,0 +1,183 @@
+/*
+ * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
+ * Copyright (C) $$Year-Rel$$ The Jalview Authors
+ *
+ * This file is part of Jalview.
+ *
+ * Jalview is free software: you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
+ *
+ * Jalview is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE. See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview. If not, see .
+ * The Jalview Authors are detailed in the 'AUTHORS' file.
+ */
+package jalview.workers;
+
+import java.beans.PropertyChangeEvent;
+import java.beans.PropertyChangeListener;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import jalview.api.AlignViewportI;
+import jalview.api.FeaturesDisplayedI;
+import jalview.datamodel.SequenceFeature;
+import jalview.gui.FeatureRenderer;
+
+public class VisibleFeaturesAnnotationTracks implements FeatureSetCounterI
+{
+ AlignViewportI ourViewport = null;
+
+ jalview.api.FeatureRenderer ourFr = null;
+
+ PropertyChangeListener ourListener = new PropertyChangeListener()
+ {
+
+ @Override
+ public void propertyChange(PropertyChangeEvent evt)
+ {
+ if (ourViewport != null) // could check source is
+ // ourFr.getChangeSupport...
+ {
+ updateFeatureAnnotationTracks();
+ }
+ }
+ };
+
+ public VisibleFeaturesAnnotationTracks(AlignViewportI viewport,
+ FeatureRenderer fr)
+ {
+ ourViewport = viewport;
+ ourFr = fr;
+ registerListener();
+ }
+
+ void registerListener()
+ {
+ ourFr.addPropertyChangeListener(ourListener);
+ }
+
+ public void tidyUp()
+ {
+ if (ourFr != null)
+ {
+ ourFr.removePropertyChangeListener(ourListener);
+ }
+ if (ourViewport != null && ourViewport.getCalcManager() != null)
+ {
+ if (ourWorker != null)
+ {
+ ourWorker.abortAndDestroy();
+ ourViewport.getCalcManager()
+ .removeRegisteredWorkersOfClass(ourWorker.getClass());
+ }
+ ourWorker = null;
+ ourViewport = null;
+ }
+ }
+
+ public void updateFeatureAnnotationTracks()
+ {
+ // if tracks are turned off, this returns null.
+ FeaturesDisplayedI featuresDisp = ourViewport
+ .isShowSequenceFeatureCounts()
+ ? ourViewport.getFeaturesDisplayed()
+ : null;
+ Set visibleFeatures = new HashSet();
+ if (featuresDisp != null)
+ {
+ visibleFeatures.addAll(featuresDisp.getVisibleFeatures());
+ }
+ if (dispFeatures.equals(visibleFeatures))
+ {
+ // all the same features displayed
+ return;
+ }
+ // otherwise set up tracks accordingly
+
+ /*
+ * and register the counter
+ */
+ if (ourWorker != null)
+ {
+ Set toRemove = new HashSet(),
+ toAdd = new HashSet();
+ toRemove.addAll(dispFeatures);
+ toRemove.removeAll(visibleFeatures);
+ dispFeatures = visibleFeatures;
+ ourWorker.removeOldAnnotations(toRemove.toArray(new String[0]));
+
+ }
+ else
+ {
+ dispFeatures = visibleFeatures;
+ ourWorker = new ColumnCounterSetWorker(ourViewport,
+ ourFr.getAlignPanel(), this);
+ }
+ ourViewport.getCalcManager().registerWorker(ourWorker);
+ ourViewport.getCalcManager().startWorker(ourWorker);
+ }
+
+ ColumnCounterSetWorker ourWorker = null;
+
+ Set dispFeatures = Collections.EMPTY_SET;
+
+ @Override
+ public int[] count(String residue, List features)
+ {
+ final Set ourDispFeatures = dispFeatures;
+ int[] obs = new int[ourDispFeatures.size()];
+ SequenceFeature[] sfs = features.toArray(new SequenceFeature[0]);
+ for (SequenceFeature sf : sfs)
+ {
+ /*
+ * Here we inspect the type of the sequence feature.
+ * You can also test sf.description, sf.score, sf.featureGroup,
+ * sf.strand, sf.phase, sf.begin, sf.end
+ * or sf.getValue(attributeName) for GFF 'column 9' properties
+ */
+ int pos = 0;
+ for (String type : ourDispFeatures)
+ {
+ if (type.equals(sf.type))
+ {
+ obs[pos]++;
+ }
+ pos++;
+ }
+ }
+ return obs;
+ }
+
+ @Override
+ public String[] getNames()
+ {
+ return dispFeatures.toArray(new String[0]);
+ }
+
+ @Override
+ public String[] getDescriptions()
+ {
+ return dispFeatures.toArray(new String[0]);
+ }
+
+ @Override
+ public int[] getMaxColour()
+ {
+ return new int[] { 0, 0, 255 };
+ }
+
+ @Override
+ public int[] getMinColour()
+ {
+ return new int[] { 0, 255, 255 };
+ }
+}
\ No newline at end of file