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.viewmodel.seqfeatures;
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;
36 import java.util.concurrent.ConcurrentHashMap;
38 import jalview.api.AlignViewportI;
39 import jalview.api.FeatureColourI;
40 import jalview.api.FeaturesDisplayedI;
41 import jalview.datamodel.AlignedCodonFrame;
42 import jalview.datamodel.AlignedCodonFrame.SequenceToSequenceMapping;
43 import jalview.datamodel.AlignmentI;
44 import jalview.datamodel.MappedFeatures;
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;
57 public abstract class FeatureRendererModel
58 implements jalview.api.FeatureRenderer
61 * a data bean to hold one row of feature settings from the gui
63 public static class FeatureSettingsBean
65 public final String featureType;
67 public final FeatureColourI featureColour;
69 public final FeatureMatcherSetI filter;
71 public final Boolean show;
73 public FeatureSettingsBean(String type, FeatureColourI colour,
74 FeatureMatcherSetI theFilter, Boolean isShown)
77 featureColour = colour;
84 * global transparency for feature
86 protected float transparency = 1.0f;
89 * colour scheme for each feature type
91 protected Map<String, FeatureColourI> featureColours = new ConcurrentHashMap<>();
94 * visibility flag for each feature group
96 protected Map<String, Boolean> featureGroups = new ConcurrentHashMap<>();
99 * filters for each feature type
101 protected Map<String, FeatureMatcherSetI> featureFilters = new HashMap<>();
103 protected String[] renderOrder;
105 Map<String, Float> featureOrder = null;
107 protected AlignViewportI av;
109 private PropertyChangeSupport changeSupport = new PropertyChangeSupport(
113 public AlignViewportI getViewport()
118 public FeatureRendererSettings getSettings()
120 return new FeatureRendererSettings(this);
123 public void transferSettings(FeatureRendererSettings fr)
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;
133 * update from another feature renderer
138 public void transferSettings(jalview.api.FeatureRenderer _fr)
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())
150 // copy over the displayed feature settings
151 if (_fr.getFeaturesDisplayed() != null)
153 FeaturesDisplayedI fd = getFeaturesDisplayed();
156 setFeaturesDisplayedFrom(_fr.getFeaturesDisplayed());
163 for (String type : _fr.getFeaturesDisplayed()
164 .getVisibleFeatures())
174 public void setFeaturesDisplayedFrom(FeaturesDisplayedI featuresDisplayed)
176 av.setFeaturesDisplayed(new FeaturesDisplayed(featuresDisplayed));
180 public void setVisible(String featureType)
182 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
185 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
187 if (!fdi.isRegistered(featureType))
189 pushFeatureType(Arrays.asList(new String[] { featureType }));
191 fdi.setVisible(featureType);
195 public void setAllVisible(List<String> featureTypes)
197 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
200 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
202 List<String> nft = new ArrayList<>();
203 for (String featureType : featureTypes)
205 if (!fdi.isRegistered(featureType))
207 nft.add(featureType);
212 pushFeatureType(nft);
214 fdi.setAllVisible(featureTypes);
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
223 private void pushFeatureType(List<String> types)
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)
232 System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
233 System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
235 renderOrder = neworder;
238 protected Map<String, float[][]> minmax = new Hashtable<>();
240 public Map<String, float[][]> getMinMax()
246 * normalise a score against the max/min bounds for the feature type.
248 * @param sequenceFeature
249 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
252 protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
254 float[] mm = minmax.get(sequenceFeature.type)[0];
255 final byte[] r = new byte[] { 0, (byte) 255 };
258 if (r[0] != 0 || mm[0] < 0.0)
261 r[1] = (byte) ((int) 128.0
262 + 127.0 * (sequenceFeature.score / mm[1]));
266 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
272 boolean newFeatureAdded = false;
274 boolean findingFeatures = false;
276 protected boolean updateFeatures()
278 if (av.getFeaturesDisplayed() == null || renderOrder == null
282 if (av.getFeaturesDisplayed().getVisibleFeatureCount() < 1)
287 // TODO: decide if we should check for the visible feature count first
292 * search the alignment for all new features, give them a colour and display
293 * them. Then fires a PropertyChangeEvent on the changeSupport object.
296 protected void findAllFeatures()
298 synchronized (firing)
300 if (firing.equals(Boolean.FALSE))
302 firing = Boolean.TRUE;
303 findAllFeatures(true); // add all new features as visible
304 notifyFeaturesChanged();
305 firing = Boolean.FALSE;
311 public void notifyFeaturesChanged()
313 changeSupport.firePropertyChange("changeSupport", null, null);
317 public List<SequenceFeature> findFeaturesAtColumn(SequenceI sequence, int column)
320 * include features at the position provided their feature type is
321 * displayed, and feature group is null or marked for display
323 List<SequenceFeature> result = new ArrayList<>();
324 if (!av.areFeaturesDisplayed() || getFeaturesDisplayed() == null)
329 Set<String> visibleFeatures = getFeaturesDisplayed()
330 .getVisibleFeatures();
331 String[] visibleTypes = visibleFeatures
332 .toArray(new String[visibleFeatures.size()]);
333 List<SequenceFeature> features = sequence.findFeatures(column, column,
337 * include features unless they are hidden (have no colour), based on
338 * feature group visibility, or a filter or colour threshold
340 for (SequenceFeature sf : features)
342 if (getColour(sf) != null)
351 * Searches alignment for all features and updates colours
353 * @param newMadeVisible
354 * if true newly added feature types will be rendered immediately
355 * TODO: check to see if this method should actually be proxied so
356 * repaint events can be propagated by the renderer code
359 public synchronized void findAllFeatures(boolean newMadeVisible)
361 newFeatureAdded = false;
365 newFeatureAdded = true;
369 findingFeatures = true;
370 if (av.getFeaturesDisplayed() == null)
372 av.setFeaturesDisplayed(new FeaturesDisplayed());
374 FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
376 Set<String> oldfeatures = new HashSet<>();
377 if (renderOrder != null)
379 for (int i = 0; i < renderOrder.length; i++)
381 if (renderOrder[i] != null)
383 oldfeatures.add(renderOrder[i]);
388 AlignmentI alignment = av.getAlignment();
389 List<String> allfeatures = new ArrayList<>();
391 for (int i = 0; i < alignment.getHeight(); i++)
393 SequenceI asq = alignment.getSequenceAt(i);
394 for (String group : asq.getFeatures().getFeatureGroups(true))
396 boolean groupDisplayed = true;
399 if (featureGroups.containsKey(group))
401 groupDisplayed = featureGroups.get(group);
405 groupDisplayed = newMadeVisible;
406 featureGroups.put(group, groupDisplayed);
411 Set<String> types = asq.getFeatures().getFeatureTypesForGroups(
413 for (String type : types)
415 if (!allfeatures.contains(type)) // or use HashSet and no test?
417 allfeatures.add(type);
419 updateMinMax(asq, type, true); // todo: for all features?
425 // uncomment to add new features in alphebetical order (but JAL-2575)
426 // Collections.sort(allfeatures, String.CASE_INSENSITIVE_ORDER);
429 for (String type : allfeatures)
431 if (!oldfeatures.contains(type))
433 featuresDisplayed.setVisible(type);
439 updateRenderOrder(allfeatures);
440 findingFeatures = false;
444 * Updates the global (alignment) min and max values for a feature type from
445 * the score for a sequence, if the score is not NaN. Values are stored
446 * separately for positional and non-positional features.
452 protected void updateMinMax(SequenceI seq, String featureType,
455 float min = seq.getFeatures().getMinimumScore(featureType, positional);
456 if (Float.isNaN(min))
461 float max = seq.getFeatures().getMaximumScore(featureType, positional);
465 * { {positionalMin, positionalMax}, {nonPositionalMin, nonPositionalMax} }
469 minmax = new Hashtable<>();
471 synchronized (minmax)
473 float[][] mm = minmax.get(featureType);
474 int index = positional ? 0 : 1;
477 mm = new float[][] { null, null };
478 minmax.put(featureType, mm);
480 if (mm[index] == null)
482 mm[index] = new float[] { min, max };
486 mm[index][0] = Math.min(mm[index][0], min);
487 mm[index][1] = Math.max(mm[index][1], max);
491 protected Boolean firing = Boolean.FALSE;
494 * replaces the current renderOrder with the unordered features in
495 * allfeatures. The ordering of any types in both renderOrder and allfeatures
496 * is preserved, and all new feature types are rendered on top of the existing
497 * types, in the order given by getOrder or the order given in allFeatures.
498 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
499 * eliminate the float storage for computing/recalling the persistent ordering
500 * New Cability: updates min/max for colourscheme range if its dynamic
504 private void updateRenderOrder(List<String> allFeatures)
506 List<String> allfeatures = new ArrayList<>(allFeatures);
507 String[] oldRender = renderOrder;
508 renderOrder = new String[allfeatures.size()];
509 boolean initOrders = (featureOrder == null);
511 if (oldRender != null && oldRender.length > 0)
513 for (int j = 0; j < oldRender.length; j++)
515 if (oldRender[j] != null)
519 setOrder(oldRender[j],
520 (1 - (1 + (float) j) / oldRender.length));
522 if (allfeatures.contains(oldRender[j]))
524 renderOrder[opos++] = oldRender[j]; // existing features always
525 // appear below new features
526 allfeatures.remove(oldRender[j]);
529 float[][] mmrange = minmax.get(oldRender[j]);
532 FeatureColourI fc = featureColours.get(oldRender[j]);
533 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled()
534 && !fc.isColourByAttribute())
536 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
544 if (allfeatures.size() == 0)
546 // no new features - leave order unchanged.
549 int i = allfeatures.size() - 1;
551 boolean sort = false;
552 String[] newf = new String[allfeatures.size()];
553 float[] sortOrder = new float[allfeatures.size()];
554 for (String newfeat : allfeatures)
559 // update from new features minmax if necessary
560 float[][] mmrange = minmax.get(newf[i]);
563 FeatureColourI fc = featureColours.get(newf[i]);
564 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled()
565 && !fc.isColourByAttribute())
567 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
571 if (initOrders || !featureOrder.containsKey(newf[i]))
573 int denom = initOrders ? allfeatures.size() : featureOrder.size();
574 // new unordered feature - compute persistent ordering at head of
575 // existing features.
576 setOrder(newf[i], i / (float) denom);
578 // set order from newly found feature from persisted ordering.
579 sortOrder[i] = 2 - featureOrder.get(newf[i]).floatValue();
582 // only sort if we need to
583 sort = sort || sortOrder[i] > sortOrder[i + 1];
587 if (iSize > 1 && sort)
589 jalview.util.QuickSort.sort(sortOrder, newf);
592 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
596 * get a feature style object for the given type string. Creates a
597 * java.awt.Color for a featureType with no existing colourscheme.
603 public FeatureColourI getFeatureStyle(String featureType)
605 FeatureColourI fc = featureColours.get(featureType);
608 Color col = ColorUtils.createColourFromName(featureType);
609 fc = new FeatureColour(col);
610 featureColours.put(featureType, fc);
616 public Color getColour(SequenceFeature feature)
618 FeatureColourI fc = getFeatureStyle(feature.getType());
619 return getColor(feature, fc);
623 * Answers true if the feature type is currently selected to be displayed,
629 public boolean showFeatureOfType(String type)
631 return type == null ? false : (av.getFeaturesDisplayed() == null ? true
632 : av.getFeaturesDisplayed().isVisible(type));
636 public void setColour(String featureType, FeatureColourI col)
638 featureColours.put(featureType, col);
642 public void setTransparency(float value)
644 transparency = value;
648 public float getTransparency()
654 * analogous to colour - store a normalized ordering for all feature types in
655 * this rendering context.
658 * Feature type string
660 * normalized priority - 0 means always appears on top, 1 means
663 public float setOrder(String type, float position)
665 if (featureOrder == null)
667 featureOrder = new Hashtable<>();
669 featureOrder.put(type, Float.valueOf(position));
674 * get the global priority (0 (top) to 1 (bottom))
677 * @return [0,1] or -1 for a type without a priority
679 public float getOrder(String type)
681 if (featureOrder != null)
683 if (featureOrder.containsKey(type))
685 return featureOrder.get(type).floatValue();
692 public Map<String, FeatureColourI> getFeatureColours()
694 return featureColours;
698 * Replace current ordering with new ordering
701 * an array of { Type, Colour, Filter, Boolean }
702 * @return true if any visible features have been reordered, else false
704 public boolean setFeaturePriority(FeatureSettingsBean[] data)
706 return setFeaturePriority(data, true);
710 * Sets the priority order for features, with the highest priority (displayed on
711 * top) at the start of the data array
714 * an array of { Type, Colour, Filter, Boolean }
716 * when true current featureDisplay list will be cleared
717 * @return true if any visible features have been reordered or recoloured, else
718 * false (i.e. no need to repaint)
720 public boolean setFeaturePriority(FeatureSettingsBean[] data,
724 * note visible feature ordering and colours before update
726 List<String> visibleFeatures = getDisplayedFeatureTypes();
727 Map<String, FeatureColourI> visibleColours = new HashMap<>(
728 getFeatureColours());
730 FeaturesDisplayedI av_featuresdisplayed = null;
733 if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
735 av.getFeaturesDisplayed().clear();
739 av.setFeaturesDisplayed(
740 av_featuresdisplayed = new FeaturesDisplayed());
745 av_featuresdisplayed = av.getFeaturesDisplayed();
751 // The feature table will display high priority
752 // features at the top, but these are the ones
753 // we need to render last, so invert the data
754 renderOrder = new String[data.length];
758 for (int i = 0; i < data.length; i++)
760 String type = data[i].featureType;
761 setColour(type, data[i].featureColour);
764 av_featuresdisplayed.setVisible(type);
767 renderOrder[data.length - i - 1] = type;
772 * get the new visible ordering and return true if it has changed
773 * order or any colour has changed
775 List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
776 if (!visibleFeatures.equals(reorderedVisibleFeatures))
779 * the list of ordered visible features has changed
785 * return true if any feature colour has changed
787 for (String feature : visibleFeatures)
789 if (visibleColours.get(feature) != getFeatureStyle(feature))
799 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
801 public void addPropertyChangeListener(PropertyChangeListener listener)
803 changeSupport.addPropertyChangeListener(listener);
808 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
810 public void removePropertyChangeListener(PropertyChangeListener listener)
812 changeSupport.removePropertyChangeListener(listener);
815 public Set<String> getAllFeatureColours()
817 return featureColours.keySet();
820 public void clearRenderOrder()
825 public boolean hasRenderOrder()
827 return renderOrder != null;
831 * Returns feature types in ordering of rendering, where last means on top
833 public List<String> getRenderOrder()
835 if (renderOrder == null)
837 return Arrays.asList(new String[] {});
839 return Arrays.asList(renderOrder);
842 public int getFeatureGroupsSize()
844 return featureGroups != null ? 0 : featureGroups.size();
848 public List<String> getFeatureGroups()
850 // conflict between applet and desktop - featureGroups returns the map in
851 // the desktop featureRenderer
852 return (featureGroups == null) ? Arrays.asList(new String[0])
853 : Arrays.asList(featureGroups.keySet().toArray(new String[0]));
856 public boolean checkGroupVisibility(String group,
857 boolean newGroupsVisible)
859 if (featureGroups == null)
861 // then an exception happens next..
863 if (featureGroups.containsKey(group))
865 return featureGroups.get(group).booleanValue();
867 if (newGroupsVisible)
869 featureGroups.put(group, Boolean.valueOf(true));
876 * get visible or invisible groups
879 * true to return visible groups, false to return hidden ones.
880 * @return list of groups
883 public List<String> getGroups(boolean visible)
885 if (featureGroups != null)
887 List<String> gp = new ArrayList<>();
889 for (String grp : featureGroups.keySet())
891 Boolean state = featureGroups.get(grp);
892 if (state.booleanValue() == visible)
903 public void setGroupVisibility(String group, boolean visible)
905 featureGroups.put(group, Boolean.valueOf(visible));
909 public void setGroupVisibility(List<String> toset, boolean visible)
911 if (toset != null && toset.size() > 0 && featureGroups != null)
913 boolean rdrw = false;
914 for (String gst : toset)
916 Boolean st = featureGroups.get(gst);
917 featureGroups.put(gst, Boolean.valueOf(visible));
920 rdrw = rdrw || (visible != st.booleanValue());
925 // set local flag indicating redraw needed ?
931 public Map<String, FeatureColourI> getDisplayedFeatureCols()
933 Map<String, FeatureColourI> fcols = new Hashtable<>();
934 if (getViewport().getFeaturesDisplayed() == null)
938 Set<String> features = getViewport().getFeaturesDisplayed()
939 .getVisibleFeatures();
940 for (String feature : features)
942 fcols.put(feature, getFeatureStyle(feature));
948 public FeaturesDisplayedI getFeaturesDisplayed()
950 return av.getFeaturesDisplayed();
954 * Returns a (possibly empty) list of visible feature types, in render order
958 public List<String> getDisplayedFeatureTypes()
960 List<String> typ = getRenderOrder();
961 List<String> displayed = new ArrayList<>();
962 FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
963 if (feature_disp != null)
965 synchronized (feature_disp)
967 for (String type : typ)
969 if (feature_disp.isVisible(type))
980 public List<String> getDisplayedFeatureGroups()
982 List<String> _gps = new ArrayList<>();
983 for (String gp : getFeatureGroups())
985 if (checkGroupVisibility(gp, false))
994 * Answers true if the feature belongs to a feature group which is not
995 * currently displayed, else false
997 * @param sequenceFeature
1000 public boolean featureGroupNotShown(final SequenceFeature sequenceFeature)
1002 return featureGroups != null
1003 && sequenceFeature.featureGroup != null
1004 && sequenceFeature.featureGroup.length() != 0
1005 && featureGroups.containsKey(sequenceFeature.featureGroup)
1006 && !featureGroups.get(sequenceFeature.featureGroup)
1014 public List<SequenceFeature> findFeaturesAtResidue(SequenceI sequence,
1015 int fromResNo, int toResNo)
1017 List<SequenceFeature> result = new ArrayList<>();
1018 if (!av.areFeaturesDisplayed() || getFeaturesDisplayed() == null)
1024 * include features at the position provided their feature type is
1025 * displayed, and feature group is null or the empty string
1026 * or marked for display
1028 List<String> visibleFeatures = getDisplayedFeatureTypes();
1029 String[] visibleTypes = visibleFeatures
1030 .toArray(new String[visibleFeatures.size()]);
1031 List<SequenceFeature> features = sequence.getFeatures().findFeatures(
1032 fromResNo, toResNo, visibleTypes);
1034 for (SequenceFeature sf : features)
1036 if (!featureGroupNotShown(sf) && getColour(sf) != null)
1045 * Removes from the list of features any whose group is not shown, or that are
1046 * visible and duplicate the location of a visible feature of the same type.
1047 * Should be used only for features of the same, simple, feature colour (which
1048 * normally implies the same feature type). No filtering is done if
1049 * transparency, or any feature filters, are in force.
1053 public void filterFeaturesForDisplay(List<SequenceFeature> features)
1056 * fudge: JalviewJS's IntervalStore lacks the sort method called :-(
1058 if (Platform.isJS())
1064 * don't remove 'redundant' features if
1065 * - transparency is applied (feature count affects depth of feature colour)
1066 * - filters are applied (not all features may be displayable)
1068 if (features.isEmpty() || transparency != 1f
1069 || !featureFilters.isEmpty())
1074 SequenceFeatures.sortFeatures(features, true);
1075 SequenceFeature lastFeature = null;
1077 Iterator<SequenceFeature> it = features.iterator();
1078 while (it.hasNext())
1080 SequenceFeature sf = it.next();
1081 if (featureGroupNotShown(sf))
1088 * a feature is redundant for rendering purposes if it has the
1089 * same extent as another (so would just redraw the same colour);
1090 * (checking type and isContactFeature as a fail-safe here, although
1091 * currently they are guaranteed to match in this context)
1093 if (lastFeature != null
1094 && sf.getBegin() == lastFeature.getBegin()
1095 && sf.getEnd() == lastFeature.getEnd()
1096 && sf.isContactFeature() == lastFeature.isContactFeature()
1097 && sf.getType().equals(lastFeature.getType()))
1106 public Map<String, FeatureMatcherSetI> getFeatureFilters()
1108 return featureFilters;
1112 public void setFeatureFilters(Map<String, FeatureMatcherSetI> filters)
1114 featureFilters = filters;
1118 public FeatureMatcherSetI getFeatureFilter(String featureType)
1120 return featureFilters.get(featureType);
1124 public void setFeatureFilter(String featureType, FeatureMatcherSetI filter)
1126 if (filter == null || filter.isEmpty())
1128 featureFilters.remove(featureType);
1132 featureFilters.put(featureType, filter);
1137 * Answers the colour for the feature, or null if the feature is excluded by
1138 * feature group visibility, by filters, or by colour threshold settings. This
1139 * method does not take feature type visibility into account.
1145 public Color getColor(SequenceFeature sf, FeatureColourI fc)
1148 * is the feature group displayed?
1150 if (featureGroupNotShown(sf))
1156 * does the feature pass filters?
1158 if (!featureMatchesFilters(sf))
1163 return fc.getColor(sf);
1167 * Answers true if there no are filters defined for the feature type, or this
1168 * feature matches the filters. Answers false if the feature fails to match
1174 protected boolean featureMatchesFilters(SequenceFeature sf)
1176 FeatureMatcherSetI filter = featureFilters.get(sf.getType());
1177 return filter == null ? true : filter.matches(sf);
1181 * Answers true unless the specified group is set to hidden. Defaults to true
1182 * if group visibility is not set.
1187 public boolean isGroupVisible(String group)
1189 if (!featureGroups.containsKey(group))
1193 return featureGroups.get(group);
1197 * Orders features in render precedence (last in order is last to render, so
1198 * displayed on top of other features)
1202 public void orderFeatures(Comparator<String> order)
1204 Arrays.sort(renderOrder, order);
1208 public MappedFeatures findComplementFeaturesAtResidue(
1209 final SequenceI sequence, final int pos)
1211 SequenceI ds = sequence.getDatasetSequence();
1216 final char residue = ds.getCharAt(pos - ds.getStart());
1218 List<SequenceFeature> found = new ArrayList<>();
1219 List<AlignedCodonFrame> mappings = this.av.getAlignment()
1220 .getCodonFrame(sequence);
1223 * fudge: if no mapping found, check the complementary alignment
1224 * todo: only store in one place? StructureSelectionManager?
1226 if (mappings.isEmpty())
1228 mappings = this.av.getCodingComplement().getAlignment()
1229 .getCodonFrame(sequence);
1233 * todo: direct lookup of CDS for peptide and vice-versa; for now,
1234 * have to search through an unordered list of mappings for a candidate
1236 SequenceToSequenceMapping mapping = null;
1237 SequenceI mapFrom = null;
1239 for (AlignedCodonFrame acf : mappings)
1241 mapping = acf.getCoveringCodonMapping(ds);
1242 if (mapping == null)
1246 SearchResultsI sr = new SearchResults();
1247 mapping.markMappedRegion(ds, pos, sr);
1248 for (SearchResultMatchI match : sr.getResults())
1250 int fromRes = match.getStart();
1251 int toRes = match.getEnd();
1252 mapFrom = match.getSequence();
1253 List<SequenceFeature> fs = findFeaturesAtResidue(
1254 mapFrom, fromRes, toRes);
1255 for (SequenceFeature sf : fs)
1257 if (!found.contains(sf))
1265 * just take the first mapped features we find
1267 if (!found.isEmpty())
1272 if (found.isEmpty())
1278 * sort by renderorder (inefficiently but ok for small scale);
1279 * NB this sorts 'on top' feature to end, for rendering
1281 List<SequenceFeature> result = new ArrayList<>();
1282 final int toAdd = found.size();
1284 for (String type : renderOrder)
1286 for (SequenceFeature sf : found)
1288 if (type.equals(sf.getType()))
1300 return new MappedFeatures(mapping.getMapping(), mapFrom, pos, residue, result);
1304 public boolean isVisible(SequenceFeature feature)
1306 if (feature == null)
1310 if (getFeaturesDisplayed() == null
1311 || !getFeaturesDisplayed().isVisible(feature.getType()))
1315 if (featureGroupNotShown(feature))
1319 FeatureColourI fc = featureColours.get(feature.getType());
1320 if (fc != null && fc.isOutwithThreshold(feature))
1324 if (!featureMatchesFilters(feature))