JAL-2483 JAL-2481 fixed findAllFeatures to handle null group correctly
[jalview.git] / src / jalview / viewmodel / seqfeatures / FeatureRendererModel.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
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.
11  *  
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.
16  * 
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.
20  */
21 package jalview.viewmodel.seqfeatures;
22
23 import jalview.api.AlignViewportI;
24 import jalview.api.FeatureColourI;
25 import jalview.api.FeaturesDisplayedI;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.renderer.seqfeatures.FeatureRenderer;
30 import jalview.schemes.FeatureColour;
31 import jalview.util.ColorUtils;
32
33 import java.awt.Color;
34 import java.beans.PropertyChangeListener;
35 import java.beans.PropertyChangeSupport;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collections;
39 import java.util.HashMap;
40 import java.util.HashSet;
41 import java.util.Hashtable;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
45 import java.util.concurrent.ConcurrentHashMap;
46
47 public abstract class FeatureRendererModel implements
48         jalview.api.FeatureRenderer
49 {
50
51   /**
52    * global transparency for feature
53    */
54   protected float transparency = 1.0f;
55
56   protected Map<String, FeatureColourI> featureColours = new ConcurrentHashMap<String, FeatureColourI>();
57
58   protected Map<String, Boolean> featureGroups = new ConcurrentHashMap<String, Boolean>();
59
60   protected String[] renderOrder;
61
62   Map<String, Float> featureOrder = null;
63
64   protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
65           this);
66
67   protected AlignViewportI av;
68
69   @Override
70   public AlignViewportI getViewport()
71   {
72     return av;
73   }
74
75   public FeatureRendererSettings getSettings()
76   {
77     return new FeatureRendererSettings(this);
78   }
79
80   public void transferSettings(FeatureRendererSettings fr)
81   {
82     this.renderOrder = fr.renderOrder;
83     this.featureGroups = fr.featureGroups;
84     this.featureColours = fr.featureColours;
85     this.transparency = fr.transparency;
86     this.featureOrder = fr.featureOrder;
87   }
88
89   /**
90    * update from another feature renderer
91    * 
92    * @param fr
93    *          settings to copy
94    */
95   public void transferSettings(jalview.api.FeatureRenderer _fr)
96   {
97     FeatureRenderer fr = (FeatureRenderer) _fr;
98     FeatureRendererSettings frs = new FeatureRendererSettings(fr);
99     this.renderOrder = frs.renderOrder;
100     this.featureGroups = frs.featureGroups;
101     this.featureColours = frs.featureColours;
102     this.transparency = frs.transparency;
103     this.featureOrder = frs.featureOrder;
104     if (av != null && av != fr.getViewport())
105     {
106       // copy over the displayed feature settings
107       if (_fr.getFeaturesDisplayed() != null)
108       {
109         FeaturesDisplayedI fd = getFeaturesDisplayed();
110         if (fd == null)
111         {
112           setFeaturesDisplayedFrom(_fr.getFeaturesDisplayed());
113         }
114         else
115         {
116           synchronized (fd)
117           {
118             fd.clear();
119             for (String type : _fr.getFeaturesDisplayed()
120                     .getVisibleFeatures())
121             {
122               fd.setVisible(type);
123             }
124           }
125         }
126       }
127     }
128   }
129
130   public void setFeaturesDisplayedFrom(FeaturesDisplayedI featuresDisplayed)
131   {
132     av.setFeaturesDisplayed(new FeaturesDisplayed(featuresDisplayed));
133   }
134
135   @Override
136   public void setVisible(String featureType)
137   {
138     FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
139     if (fdi == null)
140     {
141       av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
142     }
143     if (!fdi.isRegistered(featureType))
144     {
145       pushFeatureType(Arrays.asList(new String[] { featureType }));
146     }
147     fdi.setVisible(featureType);
148   }
149
150   @Override
151   public void setAllVisible(List<String> featureTypes)
152   {
153     FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
154     if (fdi == null)
155     {
156       av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
157     }
158     List<String> nft = new ArrayList<String>();
159     for (String featureType : featureTypes)
160     {
161       if (!fdi.isRegistered(featureType))
162       {
163         nft.add(featureType);
164       }
165     }
166     if (nft.size() > 0)
167     {
168       pushFeatureType(nft);
169     }
170     fdi.setAllVisible(featureTypes);
171   }
172
173   /**
174    * push a set of new types onto the render order stack. Note - this is a
175    * direct mechanism rather than the one employed in updateRenderOrder
176    * 
177    * @param types
178    */
179   private void pushFeatureType(List<String> types)
180   {
181
182     int ts = types.size();
183     String neworder[] = new String[(renderOrder == null ? 0
184             : renderOrder.length) + ts];
185     types.toArray(neworder);
186     if (renderOrder != null)
187     {
188       System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
189       System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
190     }
191     renderOrder = neworder;
192   }
193
194   protected Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
195
196   public Map<String, float[][]> getMinMax()
197   {
198     return minmax;
199   }
200
201   /**
202    * normalise a score against the max/min bounds for the feature type.
203    * 
204    * @param sequenceFeature
205    * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
206    *         (0-255) value.
207    */
208   protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
209   {
210     float[] mm = minmax.get(sequenceFeature.type)[0];
211     final byte[] r = new byte[] { 0, (byte) 255 };
212     if (mm != null)
213     {
214       if (r[0] != 0 || mm[0] < 0.0)
215       {
216         r[0] = 1;
217         r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
218       }
219       else
220       {
221         r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
222       }
223     }
224     return r;
225   }
226
227   boolean newFeatureAdded = false;
228
229   boolean findingFeatures = false;
230
231   protected boolean updateFeatures()
232   {
233     if (av.getFeaturesDisplayed() == null || renderOrder == null
234             || newFeatureAdded)
235     {
236       findAllFeatures();
237       if (av.getFeaturesDisplayed().getVisibleFeatureCount() < 1)
238       {
239         return false;
240       }
241     }
242     // TODO: decide if we should check for the visible feature count first
243     return true;
244   }
245
246   /**
247    * search the alignment for all new features, give them a colour and display
248    * them. Then fires a PropertyChangeEvent on the changeSupport object.
249    * 
250    */
251   protected void findAllFeatures()
252   {
253     synchronized (firing)
254     {
255       if (firing.equals(Boolean.FALSE))
256       {
257         firing = Boolean.TRUE;
258         findAllFeatures(true); // add all new features as visible
259         changeSupport.firePropertyChange("changeSupport", null, null);
260         firing = Boolean.FALSE;
261       }
262     }
263   }
264
265   @Override
266   public List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
267   {
268     List<SequenceFeature> result = new ArrayList<SequenceFeature>();
269     if (!av.areFeaturesDisplayed())
270     {
271       return result;
272     }
273
274     Set<String> visibleFeatures = getFeaturesDisplayed()
275             .getVisibleFeatures();
276     String[] visibleTypes = visibleFeatures
277             .toArray(new String[visibleFeatures.size()]);
278
279     /*
280      * include features at the position provided their feature type is 
281      * displayed, and feature group is null or marked for display
282      */
283     List<SequenceFeature> features = sequence.getFeatures().findFeatures(
284             res, res, visibleTypes);
285
286     for (SequenceFeature sf : features)
287     {
288       if (!featureGroupNotShown(sf))
289       {
290         result.add(sf);
291       }
292     }
293     return result;
294   }
295
296   /**
297    * Searches alignment for all features and updates colours
298    * 
299    * @param newMadeVisible
300    *          if true newly added feature types will be rendered immediately
301    *          TODO: check to see if this method should actually be proxied so
302    *          repaint events can be propagated by the renderer code
303    */
304   @Override
305   public synchronized void findAllFeatures(boolean newMadeVisible)
306   {
307     newFeatureAdded = false;
308
309     if (findingFeatures)
310     {
311       newFeatureAdded = true;
312       return;
313     }
314
315     findingFeatures = true;
316     if (av.getFeaturesDisplayed() == null)
317     {
318       av.setFeaturesDisplayed(new FeaturesDisplayed());
319     }
320     FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
321
322     Set<String> oldfeatures = new HashSet<String>();
323     if (renderOrder != null)
324     {
325       for (int i = 0; i < renderOrder.length; i++)
326       {
327         if (renderOrder[i] != null)
328         {
329           oldfeatures.add(renderOrder[i]);
330         }
331       }
332     }
333
334     AlignmentI alignment = av.getAlignment();
335     List<String> allfeatures = new ArrayList<String>(); // or HashSet?
336
337     for (int i = 0; i < alignment.getHeight(); i++)
338     {
339       SequenceI asq = alignment.getSequenceAt(i);
340       for (String group : asq.getFeatures().getFeatureGroups(true))
341       {
342         /*
343          * features in null group are always displayed; other groups
344          * keep their current visibility; new groups as 'newMadeVisible'
345          */
346         boolean groupDisplayed = true;
347         if (group != null)
348         {
349           if (featureGroups.containsKey(group))
350           {
351             groupDisplayed = featureGroups.get(group);
352           }
353           else
354           {
355             groupDisplayed = newMadeVisible;
356             featureGroups.put(group, groupDisplayed);
357           }
358         }
359         if (groupDisplayed)
360         {
361           Set<String> types = asq.getFeatures().getFeatureTypesForGroups(
362                   true, group);
363           for (String type : types)
364           {
365             if (!allfeatures.contains(type)) // or use HashSet and no test?
366             {
367               allfeatures.add(type);
368             }
369             updateMinMax(asq, type, true); // todo: for all features?
370           }
371         }
372       }
373     }
374
375     /*
376      * mark any new feature types as visible
377      */
378     Collections.sort(allfeatures, String.CASE_INSENSITIVE_ORDER);
379     if (newMadeVisible)
380     {
381       for (String type : allfeatures)
382       {
383         if (!oldfeatures.contains(type))
384         {
385           featuresDisplayed.setVisible(type);
386           setOrder(type, 0);
387         }
388       }
389     }
390
391     updateRenderOrder(allfeatures);
392     findingFeatures = false;
393   }
394
395   /**
396    * Updates the global (alignment) min and max values for a feature type from
397    * the score for a sequence, if the score is not NaN. Values are stored
398    * separately for positional and non-positional features.
399    * 
400    * @param seq
401    * @param featureType
402    * @param positional
403    */
404   protected void updateMinMax(SequenceI seq, String featureType,
405           boolean positional)
406   {
407     float min = seq.getFeatures().getMinimumScore(featureType, positional);
408     if (Float.isNaN(min))
409     {
410       return;
411     }
412
413     float max = seq.getFeatures().getMaximumScore(featureType, positional);
414
415     /*
416      * stored values are 
417      * { {positionalMin, positionalMax}, {nonPositionalMin, nonPositionalMax} }
418      */
419     if (minmax == null)
420     {
421       minmax = new Hashtable<String, float[][]>();
422     }
423     synchronized (minmax)
424     {
425       float[][] mm = minmax.get(featureType);
426       int index = positional ? 0 : 1;
427       if (mm == null)
428       {
429         mm = new float[][] { null, null };
430         minmax.put(featureType, mm);
431       }
432       if (mm[index] == null)
433       {
434         mm[index] = new float[] { min, max };
435       }
436       else
437       {
438         mm[index][0] = Math.min(mm[index][0], min);
439         mm[index][1] = Math.max(mm[index][1], max);
440       }
441     }
442   }
443   protected Boolean firing = Boolean.FALSE;
444
445   /**
446    * replaces the current renderOrder with the unordered features in
447    * allfeatures. The ordering of any types in both renderOrder and allfeatures
448    * is preserved, and all new feature types are rendered on top of the existing
449    * types, in the order given by getOrder or the order given in allFeatures.
450    * Note. this operates directly on the featureOrder hash for efficiency. TODO:
451    * eliminate the float storage for computing/recalling the persistent ordering
452    * New Cability: updates min/max for colourscheme range if its dynamic
453    * 
454    * @param allFeatures
455    */
456   private void updateRenderOrder(List<String> allFeatures)
457   {
458     List<String> allfeatures = new ArrayList<String>(allFeatures);
459     String[] oldRender = renderOrder;
460     renderOrder = new String[allfeatures.size()];
461     boolean initOrders = (featureOrder == null);
462     int opos = 0;
463     if (oldRender != null && oldRender.length > 0)
464     {
465       for (int j = 0; j < oldRender.length; j++)
466       {
467         if (oldRender[j] != null)
468         {
469           if (initOrders)
470           {
471             setOrder(oldRender[j], (1 - (1 + (float) j) / oldRender.length));
472           }
473           if (allfeatures.contains(oldRender[j]))
474           {
475             renderOrder[opos++] = oldRender[j]; // existing features always
476             // appear below new features
477             allfeatures.remove(oldRender[j]);
478             if (minmax != null)
479             {
480               float[][] mmrange = minmax.get(oldRender[j]);
481               if (mmrange != null)
482               {
483                 FeatureColourI fc = featureColours.get(oldRender[j]);
484                 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
485                 {
486                   fc.updateBounds(mmrange[0][0], mmrange[0][1]);
487                 }
488               }
489             }
490           }
491         }
492       }
493     }
494     if (allfeatures.size() == 0)
495     {
496       // no new features - leave order unchanged.
497       return;
498     }
499     int i = allfeatures.size() - 1;
500     int iSize = i;
501     boolean sort = false;
502     String[] newf = new String[allfeatures.size()];
503     float[] sortOrder = new float[allfeatures.size()];
504     for (String newfeat : allfeatures)
505     {
506       newf[i] = newfeat;
507       if (minmax != null)
508       {
509         // update from new features minmax if necessary
510         float[][] mmrange = minmax.get(newf[i]);
511         if (mmrange != null)
512         {
513           FeatureColourI fc = featureColours.get(newf[i]);
514           if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
515           {
516             fc.updateBounds(mmrange[0][0], mmrange[0][1]);
517           }
518         }
519       }
520       if (initOrders || !featureOrder.containsKey(newf[i]))
521       {
522         int denom = initOrders ? allfeatures.size() : featureOrder.size();
523         // new unordered feature - compute persistent ordering at head of
524         // existing features.
525         setOrder(newf[i], i / (float) denom);
526       }
527       // set order from newly found feature from persisted ordering.
528       sortOrder[i] = 2 - featureOrder.get(newf[i]).floatValue();
529       if (i < iSize)
530       {
531         // only sort if we need to
532         sort = sort || sortOrder[i] > sortOrder[i + 1];
533       }
534       i--;
535     }
536     if (iSize > 1 && sort)
537     {
538       jalview.util.QuickSort.sort(sortOrder, newf);
539     }
540     sortOrder = null;
541     System.arraycopy(newf, 0, renderOrder, opos, newf.length);
542   }
543
544   /**
545    * get a feature style object for the given type string. Creates a
546    * java.awt.Color for a featureType with no existing colourscheme.
547    * 
548    * @param featureType
549    * @return
550    */
551   @Override
552   public FeatureColourI getFeatureStyle(String featureType)
553   {
554     FeatureColourI fc = featureColours.get(featureType);
555     if (fc == null)
556     {
557       Color col = ColorUtils.createColourFromName(featureType);
558       fc = new FeatureColour(col);
559       featureColours.put(featureType, fc);
560     }
561     return fc;
562   }
563
564   /**
565    * Returns the configured colour for a particular feature instance. This
566    * includes calculation of 'colour by label', or of a graduated score colour,
567    * if applicable. It does not take into account feature visibility or colour
568    * transparency.
569    * 
570    * @param feature
571    * @return
572    */
573   public Color getColour(SequenceFeature feature)
574   {
575     FeatureColourI fc = getFeatureStyle(feature.getType());
576     return fc.getColor(feature);
577   }
578
579   /**
580    * Answers true unless the feature has a graduated colour scheme and the
581    * feature value lies outside the current threshold for display
582    * 
583    * @param sequenceFeature
584    * @return
585    */
586   protected boolean showFeature(SequenceFeature sequenceFeature)
587   {
588     FeatureColourI fc = getFeatureStyle(sequenceFeature.type);
589     return fc.isColored(sequenceFeature);
590   }
591
592   /**
593    * Answers true if the feature type is currently selected to be displayed,
594    * else false
595    * 
596    * @param type
597    * @return
598    */
599   protected boolean showFeatureOfType(String type)
600   {
601     return type == null ? false : av.getFeaturesDisplayed().isVisible(type);
602   }
603
604   @Override
605   public void setColour(String featureType, FeatureColourI col)
606   {
607     featureColours.put(featureType, col);
608   }
609
610   @Override
611   public void setTransparency(float value)
612   {
613     transparency = value;
614   }
615
616   @Override
617   public float getTransparency()
618   {
619     return transparency;
620   }
621
622   /**
623    * analogous to colour - store a normalized ordering for all feature types in
624    * this rendering context.
625    * 
626    * @param type
627    *          Feature type string
628    * @param position
629    *          normalized priority - 0 means always appears on top, 1 means
630    *          always last.
631    */
632   public float setOrder(String type, float position)
633   {
634     if (featureOrder == null)
635     {
636       featureOrder = new Hashtable<String, Float>();
637     }
638     featureOrder.put(type, new Float(position));
639     return position;
640   }
641
642   /**
643    * get the global priority (0 (top) to 1 (bottom))
644    * 
645    * @param type
646    * @return [0,1] or -1 for a type without a priority
647    */
648   public float getOrder(String type)
649   {
650     if (featureOrder != null)
651     {
652       if (featureOrder.containsKey(type))
653       {
654         return featureOrder.get(type).floatValue();
655       }
656     }
657     return -1;
658   }
659
660   @Override
661   public Map<String, FeatureColourI> getFeatureColours()
662   {
663     return featureColours;
664   }
665
666   /**
667    * Replace current ordering with new ordering
668    * 
669    * @param data
670    *          { String(Type), Colour(Type), Boolean(Displayed) }
671    * @return true if any visible features have been reordered, else false
672    */
673   public boolean setFeaturePriority(Object[][] data)
674   {
675     return setFeaturePriority(data, true);
676   }
677
678   /**
679    * Sets the priority order for features, with the highest priority (displayed
680    * on top) at the start of the data array
681    * 
682    * @param data
683    *          { String(Type), Colour(Type), Boolean(Displayed) }
684    * @param visibleNew
685    *          when true current featureDisplay list will be cleared
686    * @return true if any visible features have been reordered or recoloured,
687    *         else false (i.e. no need to repaint)
688    */
689   public boolean setFeaturePriority(Object[][] data, boolean visibleNew)
690   {
691     /*
692      * note visible feature ordering and colours before update
693      */
694     List<String> visibleFeatures = getDisplayedFeatureTypes();
695     Map<String, FeatureColourI> visibleColours = new HashMap<String, FeatureColourI>(
696             getFeatureColours());
697
698     FeaturesDisplayedI av_featuresdisplayed = null;
699     if (visibleNew)
700     {
701       if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
702       {
703         av.getFeaturesDisplayed().clear();
704       }
705       else
706       {
707         av.setFeaturesDisplayed(av_featuresdisplayed = new FeaturesDisplayed());
708       }
709     }
710     else
711     {
712       av_featuresdisplayed = av.getFeaturesDisplayed();
713     }
714     if (data == null)
715     {
716       return false;
717     }
718     // The feature table will display high priority
719     // features at the top, but these are the ones
720     // we need to render last, so invert the data
721     renderOrder = new String[data.length];
722
723     if (data.length > 0)
724     {
725       for (int i = 0; i < data.length; i++)
726       {
727         String type = data[i][0].toString();
728         setColour(type, (FeatureColourI) data[i][1]);
729         if (((Boolean) data[i][2]).booleanValue())
730         {
731           av_featuresdisplayed.setVisible(type);
732         }
733
734         renderOrder[data.length - i - 1] = type;
735       }
736     }
737
738     /*
739      * get the new visible ordering and return true if it has changed
740      * order or any colour has changed
741      */
742     List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
743     if (!visibleFeatures.equals(reorderedVisibleFeatures))
744     {
745       /*
746        * the list of ordered visible features has changed
747        */
748       return true;
749     }
750
751     /*
752      * return true if any feature colour has changed
753      */
754     for (String feature : visibleFeatures)
755     {
756       if (visibleColours.get(feature) != getFeatureStyle(feature))
757       {
758         return true;
759       }
760     }
761     return false;
762   }
763
764   /**
765    * @param listener
766    * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
767    */
768   public void addPropertyChangeListener(PropertyChangeListener listener)
769   {
770     changeSupport.addPropertyChangeListener(listener);
771   }
772
773   /**
774    * @param listener
775    * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
776    */
777   public void removePropertyChangeListener(PropertyChangeListener listener)
778   {
779     changeSupport.removePropertyChangeListener(listener);
780   }
781
782   public Set<String> getAllFeatureColours()
783   {
784     return featureColours.keySet();
785   }
786
787   public void clearRenderOrder()
788   {
789     renderOrder = null;
790   }
791
792   public boolean hasRenderOrder()
793   {
794     return renderOrder != null;
795   }
796
797   /**
798    * Returns feature types in ordering of rendering, where last means on top
799    */
800   public List<String> getRenderOrder()
801   {
802     if (renderOrder == null)
803     {
804       return Arrays.asList(new String[] {});
805     }
806     return Arrays.asList(renderOrder);
807   }
808
809   public int getFeatureGroupsSize()
810   {
811     return featureGroups != null ? 0 : featureGroups.size();
812   }
813
814   @Override
815   public List<String> getFeatureGroups()
816   {
817     // conflict between applet and desktop - featureGroups returns the map in
818     // the desktop featureRenderer
819     return (featureGroups == null) ? Arrays.asList(new String[0]) : Arrays
820             .asList(featureGroups.keySet().toArray(new String[0]));
821   }
822
823   public boolean checkGroupVisibility(String group, boolean newGroupsVisible)
824   {
825     if (featureGroups == null)
826     {
827       // then an exception happens next..
828     }
829     if (featureGroups.containsKey(group))
830     {
831       return featureGroups.get(group).booleanValue();
832     }
833     if (newGroupsVisible)
834     {
835       featureGroups.put(group, new Boolean(true));
836       return true;
837     }
838     return false;
839   }
840
841   /**
842    * get visible or invisible groups
843    * 
844    * @param visible
845    *          true to return visible groups, false to return hidden ones.
846    * @return list of groups
847    */
848   @Override
849   public List<String> getGroups(boolean visible)
850   {
851     if (featureGroups != null)
852     {
853       List<String> gp = new ArrayList<String>();
854
855       for (String grp : featureGroups.keySet())
856       {
857         Boolean state = featureGroups.get(grp);
858         if (state.booleanValue() == visible)
859         {
860           gp.add(grp);
861         }
862       }
863       return gp;
864     }
865     return null;
866   }
867
868   @Override
869   public void setGroupVisibility(String group, boolean visible)
870   {
871     featureGroups.put(group, new Boolean(visible));
872   }
873
874   @Override
875   public void setGroupVisibility(List<String> toset, boolean visible)
876   {
877     if (toset != null && toset.size() > 0 && featureGroups != null)
878     {
879       boolean rdrw = false;
880       for (String gst : toset)
881       {
882         Boolean st = featureGroups.get(gst);
883         featureGroups.put(gst, new Boolean(visible));
884         if (st != null)
885         {
886           rdrw = rdrw || (visible != st.booleanValue());
887         }
888       }
889       if (rdrw)
890       {
891         // set local flag indicating redraw needed ?
892       }
893     }
894   }
895
896   @Override
897   public Map<String, FeatureColourI> getDisplayedFeatureCols()
898   {
899     Map<String, FeatureColourI> fcols = new Hashtable<String, FeatureColourI>();
900     if (getViewport().getFeaturesDisplayed() == null)
901     {
902       return fcols;
903     }
904     Set<String> features = getViewport().getFeaturesDisplayed()
905             .getVisibleFeatures();
906     for (String feature : features)
907     {
908       fcols.put(feature, getFeatureStyle(feature));
909     }
910     return fcols;
911   }
912
913   @Override
914   public FeaturesDisplayedI getFeaturesDisplayed()
915   {
916     return av.getFeaturesDisplayed();
917   }
918
919   /**
920    * Returns a (possibly empty) list of visible feature types, in render order
921    * (last is on top)
922    */
923   @Override
924   public List<String> getDisplayedFeatureTypes()
925   {
926     List<String> typ = getRenderOrder();
927     List<String> displayed = new ArrayList<String>();
928     FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
929     if (feature_disp != null)
930     {
931       synchronized (feature_disp)
932       {
933         for (String type : typ)
934         {
935           if (feature_disp.isVisible(type))
936           {
937             displayed.add(type);
938           }
939         }
940       }
941     }
942     return displayed;
943   }
944
945   @Override
946   public List<String> getDisplayedFeatureGroups()
947   {
948     List<String> _gps = new ArrayList<String>();
949     boolean valid = false;
950     for (String gp : getFeatureGroups())
951     {
952       if (checkGroupVisibility(gp, false))
953       {
954         valid = true;
955         _gps.add(gp);
956       }
957       if (!valid)
958       {
959         return null;
960       }
961       else
962       {
963         // gps = new String[_gps.size()];
964         // _gps.toArray(gps);
965       }
966     }
967     return _gps;
968   }
969
970   /**
971    * Answers true if the feature belongs to a feature group which is not
972    * currently displayed, else false
973    * 
974    * @param sequenceFeature
975    * @return
976    */
977   protected boolean featureGroupNotShown(final SequenceFeature sequenceFeature)
978   {
979     return featureGroups != null
980             && sequenceFeature.featureGroup != null
981             && sequenceFeature.featureGroup.length() != 0
982             && featureGroups.containsKey(sequenceFeature.featureGroup)
983             && !featureGroups.get(sequenceFeature.featureGroup)
984                     .booleanValue();
985   }
986
987 }