JAL-3210 fudge for JalviewJS to avoid IntervalStore incompatibility
[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 java.awt.Color;
24 import java.beans.PropertyChangeListener;
25 import java.beans.PropertyChangeSupport;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Comparator;
29 import java.util.HashMap;
30 import java.util.HashSet;
31 import java.util.Hashtable;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.Set;
36 import java.util.concurrent.ConcurrentHashMap;
37
38 import jalview.api.AlignViewportI;
39 import jalview.api.FeatureColourI;
40 import jalview.api.FeaturesDisplayedI;
41 import jalview.datamodel.AlignedCodonFrame;
42 import jalview.datamodel.AlignmentI;
43 import jalview.datamodel.MappedFeatures;
44 import jalview.datamodel.Mapping;
45 import jalview.datamodel.SearchResultMatchI;
46 import jalview.datamodel.SearchResults;
47 import jalview.datamodel.SearchResultsI;
48 import jalview.datamodel.SequenceFeature;
49 import jalview.datamodel.SequenceI;
50 import jalview.datamodel.features.FeatureMatcherSetI;
51 import jalview.datamodel.features.SequenceFeatures;
52 import jalview.renderer.seqfeatures.FeatureRenderer;
53 import jalview.schemes.FeatureColour;
54 import jalview.util.ColorUtils;
55 import jalview.util.Platform;
56
57 public abstract class FeatureRendererModel
58         implements jalview.api.FeatureRenderer
59 {
60   /*
61    * a data bean to hold one row of feature settings from the gui
62    */
63   public static class FeatureSettingsBean
64   {
65     public final String featureType;
66
67     public final FeatureColourI featureColour;
68
69     public final FeatureMatcherSetI filter;
70
71     public final Boolean show;
72
73     public FeatureSettingsBean(String type, FeatureColourI colour,
74             FeatureMatcherSetI theFilter, Boolean isShown)
75     {
76       featureType = type;
77       featureColour = colour;
78       filter = theFilter;
79       show = isShown;
80     }
81   }
82
83   /*
84    * global transparency for feature
85    */
86   protected float transparency = 1.0f;
87
88   /*
89    * colour scheme for each feature type
90    */
91   protected Map<String, FeatureColourI> featureColours = new ConcurrentHashMap<>();
92
93   /*
94    * visibility flag for each feature group
95    */
96   protected Map<String, Boolean> featureGroups = new ConcurrentHashMap<>();
97
98   /*
99    * filters for each feature type
100    */
101   protected Map<String, FeatureMatcherSetI> featureFilters = new HashMap<>();
102
103   protected String[] renderOrder;
104
105   Map<String, Float> featureOrder = null;
106
107   protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
108           this);
109
110   protected AlignViewportI av;
111
112   @Override
113   public AlignViewportI getViewport()
114   {
115     return av;
116   }
117
118   public FeatureRendererSettings getSettings()
119   {
120     return new FeatureRendererSettings(this);
121   }
122
123   public void transferSettings(FeatureRendererSettings fr)
124   {
125     this.renderOrder = fr.renderOrder;
126     this.featureGroups = fr.featureGroups;
127     this.featureColours = fr.featureColours;
128     this.transparency = fr.transparency;
129     this.featureOrder = fr.featureOrder;
130   }
131
132   /**
133    * update from another feature renderer
134    * 
135    * @param fr
136    *          settings to copy
137    */
138   public void transferSettings(jalview.api.FeatureRenderer _fr)
139   {
140     FeatureRenderer fr = (FeatureRenderer) _fr;
141     FeatureRendererSettings frs = new FeatureRendererSettings(fr);
142     this.renderOrder = frs.renderOrder;
143     this.featureGroups = frs.featureGroups;
144     this.featureColours = frs.featureColours;
145     this.featureFilters = frs.featureFilters;
146     this.transparency = frs.transparency;
147     this.featureOrder = frs.featureOrder;
148     if (av != null && av != fr.getViewport())
149     {
150       // copy over the displayed feature settings
151       if (_fr.getFeaturesDisplayed() != null)
152       {
153         FeaturesDisplayedI fd = getFeaturesDisplayed();
154         if (fd == null)
155         {
156           setFeaturesDisplayedFrom(_fr.getFeaturesDisplayed());
157         }
158         else
159         {
160           synchronized (fd)
161           {
162             fd.clear();
163             for (String type : _fr.getFeaturesDisplayed()
164                     .getVisibleFeatures())
165             {
166               fd.setVisible(type);
167             }
168           }
169         }
170       }
171     }
172   }
173
174   public void setFeaturesDisplayedFrom(FeaturesDisplayedI featuresDisplayed)
175   {
176     av.setFeaturesDisplayed(new FeaturesDisplayed(featuresDisplayed));
177   }
178
179   @Override
180   public void setVisible(String featureType)
181   {
182     FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
183     if (fdi == null)
184     {
185       av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
186     }
187     if (!fdi.isRegistered(featureType))
188     {
189       pushFeatureType(Arrays.asList(new String[] { featureType }));
190     }
191     fdi.setVisible(featureType);
192   }
193
194   @Override
195   public void setAllVisible(List<String> featureTypes)
196   {
197     FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
198     if (fdi == null)
199     {
200       av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
201     }
202     List<String> nft = new ArrayList<>();
203     for (String featureType : featureTypes)
204     {
205       if (!fdi.isRegistered(featureType))
206       {
207         nft.add(featureType);
208       }
209     }
210     if (nft.size() > 0)
211     {
212       pushFeatureType(nft);
213     }
214     fdi.setAllVisible(featureTypes);
215   }
216
217   /**
218    * push a set of new types onto the render order stack. Note - this is a
219    * direct mechanism rather than the one employed in updateRenderOrder
220    * 
221    * @param types
222    */
223   private void pushFeatureType(List<String> types)
224   {
225
226     int ts = types.size();
227     String neworder[] = new String[(renderOrder == null ? 0
228             : renderOrder.length) + ts];
229     types.toArray(neworder);
230     if (renderOrder != null)
231     {
232       System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
233       System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
234     }
235     renderOrder = neworder;
236   }
237
238   protected Map<String, float[][]> minmax = new Hashtable<>();
239
240   public Map<String, float[][]> getMinMax()
241   {
242     return minmax;
243   }
244
245   /**
246    * normalise a score against the max/min bounds for the feature type.
247    * 
248    * @param sequenceFeature
249    * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
250    *         (0-255) value.
251    */
252   protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
253   {
254     float[] mm = minmax.get(sequenceFeature.type)[0];
255     final byte[] r = new byte[] { 0, (byte) 255 };
256     if (mm != null)
257     {
258       if (r[0] != 0 || mm[0] < 0.0)
259       {
260         r[0] = 1;
261         r[1] = (byte) ((int) 128.0
262                 + 127.0 * (sequenceFeature.score / mm[1]));
263       }
264       else
265       {
266         r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
267       }
268     }
269     return r;
270   }
271
272   boolean newFeatureAdded = false;
273
274   boolean findingFeatures = false;
275
276   protected boolean updateFeatures()
277   {
278     if (av.getFeaturesDisplayed() == null || renderOrder == null
279             || newFeatureAdded)
280     {
281       findAllFeatures();
282       if (av.getFeaturesDisplayed().getVisibleFeatureCount() < 1)
283       {
284         return false;
285       }
286     }
287     // TODO: decide if we should check for the visible feature count first
288     return true;
289   }
290
291   /**
292    * search the alignment for all new features, give them a colour and display
293    * them. Then fires a PropertyChangeEvent on the changeSupport object.
294    * 
295    */
296   protected void findAllFeatures()
297   {
298     synchronized (firing)
299     {
300       if (firing.equals(Boolean.FALSE))
301       {
302         firing = Boolean.TRUE;
303         findAllFeatures(true); // add all new features as visible
304         changeSupport.firePropertyChange("changeSupport", null, null);
305         firing = Boolean.FALSE;
306       }
307     }
308   }
309
310   @Override
311   public List<SequenceFeature> findFeaturesAtColumn(SequenceI sequence, int column)
312   {
313     /*
314      * include features at the position provided their feature type is 
315      * displayed, and feature group is null or marked for display
316      */
317     List<SequenceFeature> result = new ArrayList<>();
318     if (!av.areFeaturesDisplayed() || getFeaturesDisplayed() == null)
319     {
320       return result;
321     }
322
323     Set<String> visibleFeatures = getFeaturesDisplayed()
324             .getVisibleFeatures();
325     String[] visibleTypes = visibleFeatures
326             .toArray(new String[visibleFeatures.size()]);
327     List<SequenceFeature> features = sequence.findFeatures(column, column,
328             visibleTypes);
329
330     /*
331      * include features unless they are hidden (have no colour), based on 
332      * feature group visibility, or a filter or colour threshold
333      */
334     for (SequenceFeature sf : features)
335     {
336       if (getColour(sf) != null)
337       {
338         result.add(sf);
339       }
340     }
341     return result;
342   }
343
344   /**
345    * Searches alignment for all features and updates colours
346    * 
347    * @param newMadeVisible
348    *          if true newly added feature types will be rendered immediately
349    *          TODO: check to see if this method should actually be proxied so
350    *          repaint events can be propagated by the renderer code
351    */
352   @Override
353   public synchronized void findAllFeatures(boolean newMadeVisible)
354   {
355     newFeatureAdded = false;
356
357     if (findingFeatures)
358     {
359       newFeatureAdded = true;
360       return;
361     }
362
363     findingFeatures = true;
364     if (av.getFeaturesDisplayed() == null)
365     {
366       av.setFeaturesDisplayed(new FeaturesDisplayed());
367     }
368     FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
369
370     Set<String> oldfeatures = new HashSet<>();
371     if (renderOrder != null)
372     {
373       for (int i = 0; i < renderOrder.length; i++)
374       {
375         if (renderOrder[i] != null)
376         {
377           oldfeatures.add(renderOrder[i]);
378         }
379       }
380     }
381
382     AlignmentI alignment = av.getAlignment();
383     List<String> allfeatures = new ArrayList<>();
384
385     for (int i = 0; i < alignment.getHeight(); i++)
386     {
387       SequenceI asq = alignment.getSequenceAt(i);
388       for (String group : asq.getFeatures().getFeatureGroups(true))
389       {
390         boolean groupDisplayed = true;
391         if (group != null)
392         {
393           if (featureGroups.containsKey(group))
394           {
395             groupDisplayed = featureGroups.get(group);
396           }
397           else
398           {
399             groupDisplayed = newMadeVisible;
400             featureGroups.put(group, groupDisplayed);
401           }
402         }
403         if (groupDisplayed)
404         {
405           Set<String> types = asq.getFeatures().getFeatureTypesForGroups(
406                   true, group);
407           for (String type : types)
408           {
409             if (!allfeatures.contains(type)) // or use HashSet and no test?
410             {
411               allfeatures.add(type);
412             }
413             updateMinMax(asq, type, true); // todo: for all features?
414           }
415         }
416       }
417     }
418
419     // uncomment to add new features in alphebetical order (but JAL-2575)
420     // Collections.sort(allfeatures, String.CASE_INSENSITIVE_ORDER);
421     if (newMadeVisible)
422     {
423       for (String type : allfeatures)
424       {
425         if (!oldfeatures.contains(type))
426         {
427           featuresDisplayed.setVisible(type);
428           setOrder(type, 0);
429         }
430       }
431     }
432
433     updateRenderOrder(allfeatures);
434     findingFeatures = false;
435   }
436
437   /**
438    * Updates the global (alignment) min and max values for a feature type from
439    * the score for a sequence, if the score is not NaN. Values are stored
440    * separately for positional and non-positional features.
441    * 
442    * @param seq
443    * @param featureType
444    * @param positional
445    */
446   protected void updateMinMax(SequenceI seq, String featureType,
447           boolean positional)
448   {
449     float min = seq.getFeatures().getMinimumScore(featureType, positional);
450     if (Float.isNaN(min))
451     {
452       return;
453     }
454
455     float max = seq.getFeatures().getMaximumScore(featureType, positional);
456
457     /*
458      * stored values are 
459      * { {positionalMin, positionalMax}, {nonPositionalMin, nonPositionalMax} }
460      */
461     if (minmax == null)
462     {
463       minmax = new Hashtable<>();
464     }
465     synchronized (minmax)
466     {
467       float[][] mm = minmax.get(featureType);
468       int index = positional ? 0 : 1;
469       if (mm == null)
470       {
471         mm = new float[][] { null, null };
472         minmax.put(featureType, mm);
473       }
474       if (mm[index] == null)
475       {
476         mm[index] = new float[] { min, max };
477       }
478       else
479       {
480         mm[index][0] = Math.min(mm[index][0], min);
481         mm[index][1] = Math.max(mm[index][1], max);
482       }
483     }
484   }
485   protected Boolean firing = Boolean.FALSE;
486
487   /**
488    * replaces the current renderOrder with the unordered features in
489    * allfeatures. The ordering of any types in both renderOrder and allfeatures
490    * is preserved, and all new feature types are rendered on top of the existing
491    * types, in the order given by getOrder or the order given in allFeatures.
492    * Note. this operates directly on the featureOrder hash for efficiency. TODO:
493    * eliminate the float storage for computing/recalling the persistent ordering
494    * New Cability: updates min/max for colourscheme range if its dynamic
495    * 
496    * @param allFeatures
497    */
498   private void updateRenderOrder(List<String> allFeatures)
499   {
500     List<String> allfeatures = new ArrayList<>(allFeatures);
501     String[] oldRender = renderOrder;
502     renderOrder = new String[allfeatures.size()];
503     boolean initOrders = (featureOrder == null);
504     int opos = 0;
505     if (oldRender != null && oldRender.length > 0)
506     {
507       for (int j = 0; j < oldRender.length; j++)
508       {
509         if (oldRender[j] != null)
510         {
511           if (initOrders)
512           {
513             setOrder(oldRender[j],
514                     (1 - (1 + (float) j) / oldRender.length));
515           }
516           if (allfeatures.contains(oldRender[j]))
517           {
518             renderOrder[opos++] = oldRender[j]; // existing features always
519             // appear below new features
520             allfeatures.remove(oldRender[j]);
521             if (minmax != null)
522             {
523               float[][] mmrange = minmax.get(oldRender[j]);
524               if (mmrange != null)
525               {
526                 FeatureColourI fc = featureColours.get(oldRender[j]);
527                 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled()
528                         && !fc.isColourByAttribute())
529                 {
530                   fc.updateBounds(mmrange[0][0], mmrange[0][1]);
531                 }
532               }
533             }
534           }
535         }
536       }
537     }
538     if (allfeatures.size() == 0)
539     {
540       // no new features - leave order unchanged.
541       return;
542     }
543     int i = allfeatures.size() - 1;
544     int iSize = i;
545     boolean sort = false;
546     String[] newf = new String[allfeatures.size()];
547     float[] sortOrder = new float[allfeatures.size()];
548     for (String newfeat : allfeatures)
549     {
550       newf[i] = newfeat;
551       if (minmax != null)
552       {
553         // update from new features minmax if necessary
554         float[][] mmrange = minmax.get(newf[i]);
555         if (mmrange != null)
556         {
557           FeatureColourI fc = featureColours.get(newf[i]);
558           if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled()
559                   && !fc.isColourByAttribute())
560           {
561             fc.updateBounds(mmrange[0][0], mmrange[0][1]);
562           }
563         }
564       }
565       if (initOrders || !featureOrder.containsKey(newf[i]))
566       {
567         int denom = initOrders ? allfeatures.size() : featureOrder.size();
568         // new unordered feature - compute persistent ordering at head of
569         // existing features.
570         setOrder(newf[i], i / (float) denom);
571       }
572       // set order from newly found feature from persisted ordering.
573       sortOrder[i] = 2 - featureOrder.get(newf[i]).floatValue();
574       if (i < iSize)
575       {
576         // only sort if we need to
577         sort = sort || sortOrder[i] > sortOrder[i + 1];
578       }
579       i--;
580     }
581     if (iSize > 1 && sort)
582     {
583       jalview.util.QuickSort.sort(sortOrder, newf);
584     }
585     sortOrder = null;
586     System.arraycopy(newf, 0, renderOrder, opos, newf.length);
587   }
588
589   /**
590    * get a feature style object for the given type string. Creates a
591    * java.awt.Color for a featureType with no existing colourscheme.
592    * 
593    * @param featureType
594    * @return
595    */
596   @Override
597   public FeatureColourI getFeatureStyle(String featureType)
598   {
599     FeatureColourI fc = featureColours.get(featureType);
600     if (fc == null)
601     {
602       Color col = ColorUtils.createColourFromName(featureType);
603       fc = new FeatureColour(col);
604       featureColours.put(featureType, fc);
605     }
606     return fc;
607   }
608
609   @Override
610   public Color getColour(SequenceFeature feature)
611   {
612     FeatureColourI fc = getFeatureStyle(feature.getType());
613     return getColor(feature, fc);
614   }
615
616   /**
617    * Answers true if the feature type is currently selected to be displayed,
618    * else false
619    * 
620    * @param type
621    * @return
622    */
623   public boolean showFeatureOfType(String type)
624   {
625     return type == null ? false : (av.getFeaturesDisplayed() == null ? true
626             : av.getFeaturesDisplayed().isVisible(type));
627   }
628
629   @Override
630   public void setColour(String featureType, FeatureColourI col)
631   {
632     featureColours.put(featureType, col);
633   }
634
635   @Override
636   public void setTransparency(float value)
637   {
638     transparency = value;
639   }
640
641   @Override
642   public float getTransparency()
643   {
644     return transparency;
645   }
646
647   /**
648    * analogous to colour - store a normalized ordering for all feature types in
649    * this rendering context.
650    * 
651    * @param type
652    *          Feature type string
653    * @param position
654    *          normalized priority - 0 means always appears on top, 1 means
655    *          always last.
656    */
657   public float setOrder(String type, float position)
658   {
659     if (featureOrder == null)
660     {
661       featureOrder = new Hashtable<>();
662     }
663     featureOrder.put(type, Float.valueOf(position));
664     return position;
665   }
666
667   /**
668    * get the global priority (0 (top) to 1 (bottom))
669    * 
670    * @param type
671    * @return [0,1] or -1 for a type without a priority
672    */
673   public float getOrder(String type)
674   {
675     if (featureOrder != null)
676     {
677       if (featureOrder.containsKey(type))
678       {
679         return featureOrder.get(type).floatValue();
680       }
681     }
682     return -1;
683   }
684
685   @Override
686   public Map<String, FeatureColourI> getFeatureColours()
687   {
688     return featureColours;
689   }
690
691   /**
692    * Replace current ordering with new ordering
693    * 
694    * @param data
695    *          an array of { Type, Colour, Filter, Boolean }
696    * @return true if any visible features have been reordered, else false
697    */
698   public boolean setFeaturePriority(FeatureSettingsBean[] data)
699   {
700     return setFeaturePriority(data, true);
701   }
702
703   /**
704    * Sets the priority order for features, with the highest priority (displayed on
705    * top) at the start of the data array
706    * 
707    * @param data
708    *          an array of { Type, Colour, Filter, Boolean }
709    * @param visibleNew
710    *          when true current featureDisplay list will be cleared
711    * @return true if any visible features have been reordered or recoloured, else
712    *         false (i.e. no need to repaint)
713    */
714   public boolean setFeaturePriority(FeatureSettingsBean[] data,
715           boolean visibleNew)
716   {
717     /*
718      * note visible feature ordering and colours before update
719      */
720     List<String> visibleFeatures = getDisplayedFeatureTypes();
721     Map<String, FeatureColourI> visibleColours = new HashMap<>(
722             getFeatureColours());
723
724     FeaturesDisplayedI av_featuresdisplayed = null;
725     if (visibleNew)
726     {
727       if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
728       {
729         av.getFeaturesDisplayed().clear();
730       }
731       else
732       {
733         av.setFeaturesDisplayed(
734                 av_featuresdisplayed = new FeaturesDisplayed());
735       }
736     }
737     else
738     {
739       av_featuresdisplayed = av.getFeaturesDisplayed();
740     }
741     if (data == null)
742     {
743       return false;
744     }
745     // The feature table will display high priority
746     // features at the top, but these are the ones
747     // we need to render last, so invert the data
748     renderOrder = new String[data.length];
749
750     if (data.length > 0)
751     {
752       for (int i = 0; i < data.length; i++)
753       {
754         String type = data[i].featureType;
755         setColour(type, data[i].featureColour);
756         if (data[i].show)
757         {
758           av_featuresdisplayed.setVisible(type);
759         }
760
761         renderOrder[data.length - i - 1] = type;
762       }
763     }
764
765     /*
766      * get the new visible ordering and return true if it has changed
767      * order or any colour has changed
768      */
769     List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
770     if (!visibleFeatures.equals(reorderedVisibleFeatures))
771     {
772       /*
773        * the list of ordered visible features has changed
774        */
775       return true;
776     }
777
778     /*
779      * return true if any feature colour has changed
780      */
781     for (String feature : visibleFeatures)
782     {
783       if (visibleColours.get(feature) != getFeatureStyle(feature))
784       {
785         return true;
786       }
787     }
788     return false;
789   }
790
791   /**
792    * @param listener
793    * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
794    */
795   public void addPropertyChangeListener(PropertyChangeListener listener)
796   {
797     changeSupport.addPropertyChangeListener(listener);
798   }
799
800   /**
801    * @param listener
802    * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
803    */
804   public void removePropertyChangeListener(PropertyChangeListener listener)
805   {
806     changeSupport.removePropertyChangeListener(listener);
807   }
808
809   public Set<String> getAllFeatureColours()
810   {
811     return featureColours.keySet();
812   }
813
814   public void clearRenderOrder()
815   {
816     renderOrder = null;
817   }
818
819   public boolean hasRenderOrder()
820   {
821     return renderOrder != null;
822   }
823
824   /**
825    * Returns feature types in ordering of rendering, where last means on top
826    */
827   public List<String> getRenderOrder()
828   {
829     if (renderOrder == null)
830     {
831       return Arrays.asList(new String[] {});
832     }
833     return Arrays.asList(renderOrder);
834   }
835
836   public int getFeatureGroupsSize()
837   {
838     return featureGroups != null ? 0 : featureGroups.size();
839   }
840
841   @Override
842   public List<String> getFeatureGroups()
843   {
844     // conflict between applet and desktop - featureGroups returns the map in
845     // the desktop featureRenderer
846     return (featureGroups == null) ? Arrays.asList(new String[0])
847             : Arrays.asList(featureGroups.keySet().toArray(new String[0]));
848   }
849
850   public boolean checkGroupVisibility(String group,
851           boolean newGroupsVisible)
852   {
853     if (featureGroups == null)
854     {
855       // then an exception happens next..
856     }
857     if (featureGroups.containsKey(group))
858     {
859       return featureGroups.get(group).booleanValue();
860     }
861     if (newGroupsVisible)
862     {
863       featureGroups.put(group, Boolean.valueOf(true));
864       return true;
865     }
866     return false;
867   }
868
869   /**
870    * get visible or invisible groups
871    * 
872    * @param visible
873    *          true to return visible groups, false to return hidden ones.
874    * @return list of groups
875    */
876   @Override
877   public List<String> getGroups(boolean visible)
878   {
879     if (featureGroups != null)
880     {
881       List<String> gp = new ArrayList<>();
882
883       for (String grp : featureGroups.keySet())
884       {
885         Boolean state = featureGroups.get(grp);
886         if (state.booleanValue() == visible)
887         {
888           gp.add(grp);
889         }
890       }
891       return gp;
892     }
893     return null;
894   }
895
896   @Override
897   public void setGroupVisibility(String group, boolean visible)
898   {
899     featureGroups.put(group, Boolean.valueOf(visible));
900   }
901
902   @Override
903   public void setGroupVisibility(List<String> toset, boolean visible)
904   {
905     if (toset != null && toset.size() > 0 && featureGroups != null)
906     {
907       boolean rdrw = false;
908       for (String gst : toset)
909       {
910         Boolean st = featureGroups.get(gst);
911         featureGroups.put(gst, Boolean.valueOf(visible));
912         if (st != null)
913         {
914           rdrw = rdrw || (visible != st.booleanValue());
915         }
916       }
917       if (rdrw)
918       {
919         // set local flag indicating redraw needed ?
920       }
921     }
922   }
923
924   @Override
925   public Map<String, FeatureColourI> getDisplayedFeatureCols()
926   {
927     Map<String, FeatureColourI> fcols = new Hashtable<>();
928     if (getViewport().getFeaturesDisplayed() == null)
929     {
930       return fcols;
931     }
932     Set<String> features = getViewport().getFeaturesDisplayed()
933             .getVisibleFeatures();
934     for (String feature : features)
935     {
936       fcols.put(feature, getFeatureStyle(feature));
937     }
938     return fcols;
939   }
940
941   @Override
942   public FeaturesDisplayedI getFeaturesDisplayed()
943   {
944     return av.getFeaturesDisplayed();
945   }
946
947   /**
948    * Returns a (possibly empty) list of visible feature types, in render order
949    * (last is on top)
950    */
951   @Override
952   public List<String> getDisplayedFeatureTypes()
953   {
954     List<String> typ = getRenderOrder();
955     List<String> displayed = new ArrayList<>();
956     FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
957     if (feature_disp != null)
958     {
959       synchronized (feature_disp)
960       {
961         for (String type : typ)
962         {
963           if (feature_disp.isVisible(type))
964           {
965             displayed.add(type);
966           }
967         }
968       }
969     }
970     return displayed;
971   }
972
973   @Override
974   public List<String> getDisplayedFeatureGroups()
975   {
976     List<String> _gps = new ArrayList<>();
977     for (String gp : getFeatureGroups())
978     {
979       if (checkGroupVisibility(gp, false))
980       {
981         _gps.add(gp);
982       }
983     }
984     return _gps;
985   }
986
987   /**
988    * Answers true if the feature belongs to a feature group which is not
989    * currently displayed, else false
990    * 
991    * @param sequenceFeature
992    * @return
993    */
994   public boolean featureGroupNotShown(final SequenceFeature sequenceFeature)
995   {
996     return featureGroups != null
997             && sequenceFeature.featureGroup != null
998             && sequenceFeature.featureGroup.length() != 0
999             && featureGroups.containsKey(sequenceFeature.featureGroup)
1000             && !featureGroups.get(sequenceFeature.featureGroup)
1001                     .booleanValue();
1002   }
1003
1004   /**
1005    * {@inheritDoc}
1006    */
1007   @Override
1008   public List<SequenceFeature> findFeaturesAtResidue(SequenceI sequence,
1009           int fromResNo, int toResNo)
1010   {
1011     List<SequenceFeature> result = new ArrayList<>();
1012     if (!av.areFeaturesDisplayed() || getFeaturesDisplayed() == null)
1013     {
1014       return result;
1015     }
1016
1017     /*
1018      * include features at the position provided their feature type is 
1019      * displayed, and feature group is null or the empty string
1020      * or marked for display
1021      */
1022     List<String> visibleFeatures = getDisplayedFeatureTypes();
1023     String[] visibleTypes = visibleFeatures
1024             .toArray(new String[visibleFeatures.size()]);
1025     List<SequenceFeature> features = sequence.getFeatures().findFeatures(
1026             fromResNo, toResNo, visibleTypes);
1027   
1028     for (SequenceFeature sf : features)
1029     {
1030       if (!featureGroupNotShown(sf) && getColour(sf) != null)
1031       {
1032         result.add(sf);
1033       }
1034     }
1035     return result;
1036   }
1037
1038   /**
1039    * Removes from the list of features any whose group is not shown, or that are
1040    * visible and duplicate the location of a visible feature of the same type.
1041    * Should be used only for features of the same, simple, feature colour (which
1042    * normally implies the same feature type). No filtering is done if
1043    * transparency, or any feature filters, are in force.
1044    * 
1045    * @param features
1046    */
1047   public void filterFeaturesForDisplay(List<SequenceFeature> features)
1048   {
1049     /*
1050      * fudge: JalviewJS's IntervalStore lacks the sort method called :-(
1051      */
1052     if (Platform.isJS())
1053     {
1054       return;
1055     }
1056
1057     /*
1058      * don't remove 'redundant' features if 
1059      * - transparency is applied (feature count affects depth of feature colour)
1060      * - filters are applied (not all features may be displayable)
1061      */
1062     if (features.isEmpty() || transparency != 1f
1063             || !featureFilters.isEmpty())
1064     {
1065       return;
1066     }
1067
1068     SequenceFeatures.sortFeatures(features, true);
1069     SequenceFeature lastFeature = null;
1070
1071     Iterator<SequenceFeature> it = features.iterator();
1072     while (it.hasNext())
1073     {
1074       SequenceFeature sf = it.next();
1075       if (featureGroupNotShown(sf))
1076       {
1077         it.remove();
1078         continue;
1079       }
1080
1081       /*
1082        * a feature is redundant for rendering purposes if it has the
1083        * same extent as another (so would just redraw the same colour);
1084        * (checking type and isContactFeature as a fail-safe here, although
1085        * currently they are guaranteed to match in this context)
1086        */
1087       if (lastFeature != null
1088               && sf.getBegin() == lastFeature.getBegin()
1089               && sf.getEnd() == lastFeature.getEnd()
1090               && sf.isContactFeature() == lastFeature.isContactFeature()
1091               && sf.getType().equals(lastFeature.getType()))
1092       {
1093         it.remove();
1094       }
1095       lastFeature = sf;
1096     }
1097   }
1098
1099   @Override
1100   public Map<String, FeatureMatcherSetI> getFeatureFilters()
1101   {
1102     return featureFilters;
1103   }
1104
1105   @Override
1106   public void setFeatureFilters(Map<String, FeatureMatcherSetI> filters)
1107   {
1108     featureFilters = filters;
1109   }
1110
1111   @Override
1112   public FeatureMatcherSetI getFeatureFilter(String featureType)
1113   {
1114     return featureFilters.get(featureType);
1115   }
1116
1117   @Override
1118   public void setFeatureFilter(String featureType, FeatureMatcherSetI filter)
1119   {
1120     if (filter == null || filter.isEmpty())
1121     {
1122       featureFilters.remove(featureType);
1123     }
1124     else
1125     {
1126       featureFilters.put(featureType, filter);
1127     }
1128   }
1129
1130   /**
1131    * Answers the colour for the feature, or null if the feature is excluded by
1132    * feature group visibility, by filters, or by colour threshold settings. This
1133    * method does not take feature type visibility into account.
1134    * 
1135    * @param sf
1136    * @param fc
1137    * @return
1138    */
1139   public Color getColor(SequenceFeature sf, FeatureColourI fc)
1140   {
1141     /*
1142      * is the feature group displayed?
1143      */
1144     if (featureGroupNotShown(sf))
1145     {
1146       return null;
1147     }
1148
1149     /*
1150      * does the feature pass filters?
1151      */
1152     if (!featureMatchesFilters(sf))
1153     {
1154       return null;
1155     }
1156   
1157     return fc.getColor(sf);
1158   }
1159
1160   /**
1161    * Answers true if there no are filters defined for the feature type, or this
1162    * feature matches the filters. Answers false if the feature fails to match
1163    * filters.
1164    * 
1165    * @param sf
1166    * @return
1167    */
1168   protected boolean featureMatchesFilters(SequenceFeature sf)
1169   {
1170     FeatureMatcherSetI filter = featureFilters.get(sf.getType());
1171     return filter == null ? true : filter.matches(sf);
1172   }
1173
1174   /**
1175    * Answers true unless the specified group is set to hidden. Defaults to true
1176    * if group visibility is not set.
1177    * 
1178    * @param group
1179    * @return
1180    */
1181   public boolean isGroupVisible(String group)
1182   {
1183     if (!featureGroups.containsKey(group))
1184     {
1185       return true;
1186     }
1187     return featureGroups.get(group);
1188   }
1189
1190   /**
1191    * Orders features in render precedence (last in order is last to render, so
1192    * displayed on top of other features)
1193    * 
1194    * @param order
1195    */
1196   public void orderFeatures(Comparator<String> order)
1197   {
1198     Arrays.sort(renderOrder, order);
1199   }
1200
1201   @Override
1202   public MappedFeatures findComplementFeaturesAtResidue(SequenceI sequence,
1203           int pos)
1204   {
1205     SequenceI ds = sequence.getDatasetSequence();
1206     if (ds == null)
1207     {
1208       ds = sequence;
1209     }
1210     final char residue = ds.getCharAt(pos - ds.getStart());
1211
1212     List<SequenceFeature> found = new ArrayList<>();
1213     List<AlignedCodonFrame> mappings = this.av.getAlignment()
1214             .getCodonFrame(sequence);
1215
1216     /*
1217      * fudge: if no mapping found, check the complementary alignment
1218      * todo: only store in one place? StructureSelectionManager?
1219      */
1220     if (mappings.isEmpty())
1221     {
1222       mappings = this.av.getCodingComplement().getAlignment()
1223               .getCodonFrame(sequence);
1224     }
1225
1226     /*
1227      * todo: direct lookup of CDS for peptide and vice-versa; for now,
1228      * have to search through an unordered list of mappings for a candidate
1229      */
1230     Mapping mapping = null;
1231     SequenceI mapFrom = null;
1232
1233     for (AlignedCodonFrame acf : mappings)
1234     {
1235       mapping = acf.getMappingForSequence(sequence);
1236       if (mapping == null || !mapping.getMap().isTripletMap())
1237       {
1238         continue; // we are only looking for 3:1 or 1:3 mappings
1239       }
1240       SearchResultsI sr = new SearchResults();
1241       acf.markMappedRegion(ds, pos, sr);
1242       for (SearchResultMatchI match : sr.getResults())
1243       {
1244         int fromRes = match.getStart();
1245         int toRes = match.getEnd();
1246         mapFrom = match.getSequence();
1247         List<SequenceFeature> fs = findFeaturesAtResidue(
1248                 mapFrom, fromRes, toRes);
1249         for (SequenceFeature sf : fs)
1250         {
1251           if (!found.contains(sf))
1252           {
1253             found.add(sf);
1254           }
1255         }
1256       }
1257
1258       /*
1259        * just take the first mapped features we find
1260        */
1261       if (!found.isEmpty())
1262       {
1263         break;
1264       }
1265     }
1266     if (found.isEmpty())
1267     {
1268       return null;
1269     }
1270
1271     /*
1272      * sort by renderorder, inefficiently
1273      */
1274     List<SequenceFeature> result = new ArrayList<>();
1275     for (String type : renderOrder)
1276     {
1277       for (SequenceFeature sf : found)
1278       {
1279         if (type.equals(sf.getType()))
1280         {
1281           result.add(sf);
1282           if (result.size() == found.size())
1283           {
1284             return new MappedFeatures(mapping, mapFrom, pos, residue,
1285                     result);
1286           }
1287         }
1288       }
1289     }
1290     
1291     return new MappedFeatures(mapping, mapFrom, pos, residue, result);
1292   }
1293
1294   @Override
1295   public boolean isVisible(SequenceFeature feature)
1296   {
1297     if (feature == null)
1298     {
1299       return false;
1300     }
1301     if (getFeaturesDisplayed() == null
1302             || !getFeaturesDisplayed().isVisible(feature.getType()))
1303     {
1304       return false;
1305     }
1306     if (featureGroupNotShown(feature))
1307     {
1308       return false;
1309     }
1310     FeatureColourI fc = featureColours.get(feature.getType());
1311     if (fc != null && fc.isOutwithThreshold(feature))
1312     {
1313       return false;
1314     }
1315     if (!featureMatchesFilters(feature))
1316     {
1317       return false;
1318     }
1319     return true;
1320   }
1321 }