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,
321 * include features at the position provided their feature type is
322 * displayed, and feature group is null or marked for display
324 List<SequenceFeature> result = new ArrayList<>();
325 if (!av.areFeaturesDisplayed() || getFeaturesDisplayed() == null)
330 Set<String> visibleFeatures = getFeaturesDisplayed()
331 .getVisibleFeatures();
332 String[] visibleTypes = visibleFeatures
333 .toArray(new String[visibleFeatures.size()]);
334 List<SequenceFeature> features = sequence.findFeatures(column, column,
338 * include features unless they are hidden (have no colour), based on
339 * feature group visibility, or a filter or colour threshold
341 for (SequenceFeature sf : features)
343 if (getColour(sf) != null)
352 * Searches alignment for all features and updates colours
354 * @param newMadeVisible
355 * if true newly added feature types will be rendered immediately
356 * TODO: check to see if this method should actually be proxied so
357 * repaint events can be propagated by the renderer code
360 public synchronized void findAllFeatures(boolean newMadeVisible)
362 newFeatureAdded = false;
366 newFeatureAdded = true;
370 findingFeatures = true;
371 if (av.getFeaturesDisplayed() == null)
373 av.setFeaturesDisplayed(new FeaturesDisplayed());
375 FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
377 Set<String> oldfeatures = new HashSet<>();
378 if (renderOrder != null)
380 for (int i = 0; i < renderOrder.length; i++)
382 if (renderOrder[i] != null)
384 oldfeatures.add(renderOrder[i]);
389 AlignmentI alignment = av.getAlignment();
390 List<String> allfeatures = new ArrayList<>();
392 for (int i = 0; i < alignment.getHeight(); i++)
394 SequenceI asq = alignment.getSequenceAt(i);
395 for (String group : asq.getFeatures().getFeatureGroups(true))
397 boolean groupDisplayed = true;
400 if (featureGroups.containsKey(group))
402 groupDisplayed = featureGroups.get(group);
406 groupDisplayed = newMadeVisible;
407 featureGroups.put(group, groupDisplayed);
412 Set<String> types = asq.getFeatures()
413 .getFeatureTypesForGroups(true, group);
414 for (String type : types)
416 if (!allfeatures.contains(type)) // or use HashSet and no test?
418 allfeatures.add(type);
420 updateMinMax(asq, type, true); // todo: for all features?
426 // uncomment to add new features in alphebetical order (but JAL-2575)
427 // Collections.sort(allfeatures, String.CASE_INSENSITIVE_ORDER);
430 for (String type : allfeatures)
432 if (!oldfeatures.contains(type))
434 featuresDisplayed.setVisible(type);
440 updateRenderOrder(allfeatures);
441 findingFeatures = false;
445 * Updates the global (alignment) min and max values for a feature type from
446 * the score for a sequence, if the score is not NaN. Values are stored
447 * separately for positional and non-positional features.
453 protected void updateMinMax(SequenceI seq, String featureType,
456 float min = seq.getFeatures().getMinimumScore(featureType, positional);
457 if (Float.isNaN(min))
462 float max = seq.getFeatures().getMaximumScore(featureType, positional);
466 * { {positionalMin, positionalMax}, {nonPositionalMin, nonPositionalMax} }
470 minmax = new Hashtable<>();
472 synchronized (minmax)
474 float[][] mm = minmax.get(featureType);
475 int index = positional ? 0 : 1;
478 mm = new float[][] { null, null };
479 minmax.put(featureType, mm);
481 if (mm[index] == null)
483 mm[index] = new float[] { min, max };
487 mm[index][0] = Math.min(mm[index][0], min);
488 mm[index][1] = Math.max(mm[index][1], max);
493 protected Boolean firing = Boolean.FALSE;
496 * replaces the current renderOrder with the unordered features in
497 * allfeatures. The ordering of any types in both renderOrder and allfeatures
498 * is preserved, and all new feature types are rendered on top of the existing
499 * types, in the order given by getOrder or the order given in allFeatures.
500 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
501 * eliminate the float storage for computing/recalling the persistent ordering
502 * New Cability: updates min/max for colourscheme range if its dynamic
506 private void updateRenderOrder(List<String> allFeatures)
508 List<String> allfeatures = new ArrayList<>(allFeatures);
509 String[] oldRender = renderOrder;
510 renderOrder = new String[allfeatures.size()];
511 boolean initOrders = (featureOrder == null);
513 if (oldRender != null && oldRender.length > 0)
515 for (int j = 0; j < oldRender.length; j++)
517 if (oldRender[j] != null)
521 setOrder(oldRender[j],
522 (1 - (1 + (float) j) / oldRender.length));
524 if (allfeatures.contains(oldRender[j]))
526 renderOrder[opos++] = oldRender[j]; // existing features always
527 // appear below new features
528 allfeatures.remove(oldRender[j]);
531 float[][] mmrange = minmax.get(oldRender[j]);
534 FeatureColourI fc = featureColours.get(oldRender[j]);
535 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled()
536 && !fc.isColourByAttribute())
538 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
546 if (allfeatures.size() == 0)
548 // no new features - leave order unchanged.
551 int i = allfeatures.size() - 1;
553 boolean sort = false;
554 String[] newf = new String[allfeatures.size()];
555 float[] sortOrder = new float[allfeatures.size()];
556 for (String newfeat : allfeatures)
561 // update from new features minmax if necessary
562 float[][] mmrange = minmax.get(newf[i]);
565 FeatureColourI fc = featureColours.get(newf[i]);
566 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled()
567 && !fc.isColourByAttribute())
569 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
573 if (initOrders || !featureOrder.containsKey(newf[i]))
575 int denom = initOrders ? allfeatures.size() : featureOrder.size();
576 // new unordered feature - compute persistent ordering at head of
577 // existing features.
578 setOrder(newf[i], i / (float) denom);
580 // set order from newly found feature from persisted ordering.
581 sortOrder[i] = 2 - featureOrder.get(newf[i]).floatValue();
584 // only sort if we need to
585 sort = sort || sortOrder[i] > sortOrder[i + 1];
589 if (iSize > 1 && sort)
591 jalview.util.QuickSort.sort(sortOrder, newf);
594 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
598 * get a feature style object for the given type string. Creates a
599 * java.awt.Color for a featureType with no existing colourscheme.
605 public FeatureColourI getFeatureStyle(String featureType)
607 FeatureColourI fc = featureColours.get(featureType);
610 Color col = ColorUtils.createColourFromName(featureType);
611 fc = new FeatureColour(col);
612 featureColours.put(featureType, fc);
618 public Color getColour(SequenceFeature feature)
620 FeatureColourI fc = getFeatureStyle(feature.getType());
621 return getColor(feature, fc);
625 * Answers true if the feature type is currently selected to be displayed,
631 public boolean showFeatureOfType(String type)
633 return type == null ? false
634 : (av.getFeaturesDisplayed() == null ? true
635 : av.getFeaturesDisplayed().isVisible(type));
639 public void setColour(String featureType, FeatureColourI col)
641 featureColours.put(featureType, col);
645 public void setTransparency(float value)
647 transparency = value;
651 public float getTransparency()
657 * analogous to colour - store a normalized ordering for all feature types in
658 * this rendering context.
661 * Feature type string
663 * normalized priority - 0 means always appears on top, 1 means
666 public float setOrder(String type, float position)
668 if (featureOrder == null)
670 featureOrder = new Hashtable<>();
672 featureOrder.put(type, Float.valueOf(position));
677 * get the global priority (0 (top) to 1 (bottom))
680 * @return [0,1] or -1 for a type without a priority
682 public float getOrder(String type)
684 if (featureOrder != null)
686 if (featureOrder.containsKey(type))
688 return featureOrder.get(type).floatValue();
695 public Map<String, FeatureColourI> getFeatureColours()
697 return featureColours;
701 * Replace current ordering with new ordering
704 * an array of { Type, Colour, Filter, Boolean }
705 * @return true if any visible features have been reordered, else false
707 public boolean setFeaturePriority(FeatureSettingsBean[] data)
709 return setFeaturePriority(data, true);
713 * Sets the priority order for features, with the highest priority (displayed
714 * on top) at the start of the data array
717 * an array of { Type, Colour, Filter, Boolean }
719 * when true current featureDisplay list will be cleared
720 * @return true if any visible features have been reordered or recoloured,
721 * else false (i.e. no need to repaint)
723 public boolean setFeaturePriority(FeatureSettingsBean[] data,
727 * note visible feature ordering and colours before update
729 List<String> visibleFeatures = getDisplayedFeatureTypes();
730 Map<String, FeatureColourI> visibleColours = new HashMap<>(
731 getFeatureColours());
733 FeaturesDisplayedI av_featuresdisplayed = null;
736 if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
738 av.getFeaturesDisplayed().clear();
742 av.setFeaturesDisplayed(
743 av_featuresdisplayed = new FeaturesDisplayed());
748 av_featuresdisplayed = av.getFeaturesDisplayed();
754 // The feature table will display high priority
755 // features at the top, but these are the ones
756 // we need to render last, so invert the data
757 renderOrder = new String[data.length];
761 for (int i = 0; i < data.length; i++)
763 String type = data[i].featureType;
764 setColour(type, data[i].featureColour);
767 av_featuresdisplayed.setVisible(type);
770 renderOrder[data.length - i - 1] = type;
775 * get the new visible ordering and return true if it has changed
776 * order or any colour has changed
778 List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
779 if (!visibleFeatures.equals(reorderedVisibleFeatures))
782 * the list of ordered visible features has changed
788 * return true if any feature colour has changed
790 for (String feature : visibleFeatures)
792 if (visibleColours.get(feature) != getFeatureStyle(feature))
802 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
804 public void addPropertyChangeListener(PropertyChangeListener listener)
806 changeSupport.addPropertyChangeListener(listener);
811 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
813 public void removePropertyChangeListener(PropertyChangeListener listener)
815 changeSupport.removePropertyChangeListener(listener);
818 public Set<String> getAllFeatureColours()
820 return featureColours.keySet();
823 public void clearRenderOrder()
828 public boolean hasRenderOrder()
830 return renderOrder != null;
834 * Returns feature types in ordering of rendering, where last means on top
836 public List<String> getRenderOrder()
838 if (renderOrder == null)
840 return Arrays.asList(new String[] {});
842 return Arrays.asList(renderOrder);
845 public int getFeatureGroupsSize()
847 return featureGroups != null ? 0 : featureGroups.size();
851 public List<String> getFeatureGroups()
853 // conflict between applet and desktop - featureGroups returns the map in
854 // the desktop featureRenderer
855 return (featureGroups == null) ? Arrays.asList(new String[0])
856 : Arrays.asList(featureGroups.keySet().toArray(new String[0]));
859 public boolean checkGroupVisibility(String group,
860 boolean newGroupsVisible)
862 if (featureGroups == null)
864 // then an exception happens next..
866 if (featureGroups.containsKey(group))
868 return featureGroups.get(group).booleanValue();
870 if (newGroupsVisible)
872 featureGroups.put(group, Boolean.valueOf(true));
879 * get visible or invisible groups
882 * true to return visible groups, false to return hidden ones.
883 * @return list of groups
886 public List<String> getGroups(boolean visible)
888 if (featureGroups != null)
890 List<String> gp = new ArrayList<>();
892 for (String grp : featureGroups.keySet())
894 Boolean state = featureGroups.get(grp);
895 if (state.booleanValue() == visible)
906 public void setGroupVisibility(String group, boolean visible)
908 featureGroups.put(group, Boolean.valueOf(visible));
912 public void setGroupVisibility(List<String> toset, boolean visible)
914 if (toset != null && toset.size() > 0 && featureGroups != null)
916 boolean rdrw = false;
917 for (String gst : toset)
919 Boolean st = featureGroups.get(gst);
920 featureGroups.put(gst, Boolean.valueOf(visible));
923 rdrw = rdrw || (visible != st.booleanValue());
928 // set local flag indicating redraw needed ?
934 public Map<String, FeatureColourI> getDisplayedFeatureCols()
936 Map<String, FeatureColourI> fcols = new Hashtable<>();
937 if (getViewport().getFeaturesDisplayed() == null)
941 Set<String> features = getViewport().getFeaturesDisplayed()
942 .getVisibleFeatures();
943 for (String feature : features)
945 fcols.put(feature, getFeatureStyle(feature));
951 public FeaturesDisplayedI getFeaturesDisplayed()
953 return av.getFeaturesDisplayed();
957 * Returns a (possibly empty) list of visible feature types, in render order
961 public List<String> getDisplayedFeatureTypes()
963 List<String> typ = getRenderOrder();
964 List<String> displayed = new ArrayList<>();
965 FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
966 if (feature_disp != null)
968 synchronized (feature_disp)
970 for (String type : typ)
972 if (feature_disp.isVisible(type))
983 public List<String> getDisplayedFeatureGroups()
985 List<String> _gps = new ArrayList<>();
986 for (String gp : getFeatureGroups())
988 if (checkGroupVisibility(gp, false))
997 * Answers true if the feature belongs to a feature group which is not
998 * currently displayed, else false
1000 * @param sequenceFeature
1003 public boolean featureGroupNotShown(final SequenceFeature sequenceFeature)
1005 return featureGroups != null && sequenceFeature.featureGroup != null
1006 && sequenceFeature.featureGroup.length() != 0
1007 && featureGroups.containsKey(sequenceFeature.featureGroup)
1008 && !featureGroups.get(sequenceFeature.featureGroup)
1016 public List<SequenceFeature> findFeaturesAtResidue(SequenceI sequence,
1017 int fromResNo, int toResNo)
1019 List<SequenceFeature> result = new ArrayList<>();
1020 if (!av.areFeaturesDisplayed() || getFeaturesDisplayed() == null)
1026 * include features at the position provided their feature type is
1027 * displayed, and feature group is null or the empty string
1028 * or marked for display
1030 List<String> visibleFeatures = getDisplayedFeatureTypes();
1031 String[] visibleTypes = visibleFeatures
1032 .toArray(new String[visibleFeatures.size()]);
1033 List<SequenceFeature> features = sequence.getFeatures()
1034 .findFeatures(fromResNo, toResNo, visibleTypes);
1036 for (SequenceFeature sf : features)
1038 if (!featureGroupNotShown(sf) && getColour(sf) != null)
1047 * Removes from the list of features any whose group is not shown, or that are
1048 * visible and duplicate the location of a visible feature of the same type.
1049 * Should be used only for features of the same, simple, feature colour (which
1050 * normally implies the same feature type). No filtering is done if
1051 * transparency, or any feature filters, are in force.
1055 public void filterFeaturesForDisplay(List<SequenceFeature> features)
1058 * fudge: JalviewJS's IntervalStore lacks the sort method called :-(
1060 if (Platform.isJS())
1066 * don't remove 'redundant' features if
1067 * - transparency is applied (feature count affects depth of feature colour)
1068 * - filters are applied (not all features may be displayable)
1070 if (features.isEmpty() || transparency != 1f
1071 || !featureFilters.isEmpty())
1076 SequenceFeatures.sortFeatures(features, true);
1077 SequenceFeature lastFeature = null;
1079 Iterator<SequenceFeature> it = features.iterator();
1080 while (it.hasNext())
1082 SequenceFeature sf = it.next();
1083 if (featureGroupNotShown(sf))
1090 * a feature is redundant for rendering purposes if it has the
1091 * same extent as another (so would just redraw the same colour);
1092 * (checking type and isContactFeature as a fail-safe here, although
1093 * currently they are guaranteed to match in this context)
1095 if (lastFeature != null && sf.getBegin() == lastFeature.getBegin()
1096 && sf.getEnd() == lastFeature.getEnd()
1097 && sf.isContactFeature() == lastFeature.isContactFeature()
1098 && sf.getType().equals(lastFeature.getType()))
1107 public Map<String, FeatureMatcherSetI> getFeatureFilters()
1109 return featureFilters;
1113 public void setFeatureFilters(Map<String, FeatureMatcherSetI> filters)
1115 featureFilters = filters;
1119 public FeatureMatcherSetI getFeatureFilter(String featureType)
1121 return featureFilters.get(featureType);
1125 public void setFeatureFilter(String featureType,
1126 FeatureMatcherSetI filter)
1128 if (filter == null || filter.isEmpty())
1130 featureFilters.remove(featureType);
1134 featureFilters.put(featureType, filter);
1139 * Answers the colour for the feature, or null if the feature is excluded by
1140 * feature group visibility, by filters, or by colour threshold settings. This
1141 * method does not take feature type visibility into account.
1147 public Color getColor(SequenceFeature sf, FeatureColourI fc)
1150 * is the feature group displayed?
1152 if (featureGroupNotShown(sf))
1158 * does the feature pass filters?
1160 if (!featureMatchesFilters(sf))
1165 return fc.getColor(sf);
1169 * Answers true if there no are filters defined for the feature type, or this
1170 * feature matches the filters. Answers false if the feature fails to match
1176 protected boolean featureMatchesFilters(SequenceFeature sf)
1178 FeatureMatcherSetI filter = featureFilters.get(sf.getType());
1179 return filter == null ? true : filter.matches(sf);
1183 * Answers true unless the specified group is set to hidden. Defaults to true
1184 * if group visibility is not set.
1189 public boolean isGroupVisible(String group)
1191 if (!featureGroups.containsKey(group))
1195 return featureGroups.get(group);
1199 * Orders features in render precedence (last in order is last to render, so
1200 * displayed on top of other features)
1204 public void orderFeatures(Comparator<String> order)
1206 Arrays.sort(renderOrder, order);
1210 public MappedFeatures findComplementFeaturesAtResidue(
1211 final SequenceI sequence, final int pos)
1213 SequenceI ds = sequence.getDatasetSequence();
1218 final char residue = ds.getCharAt(pos - ds.getStart());
1220 List<SequenceFeature> found = new ArrayList<>();
1221 List<AlignedCodonFrame> mappings = this.av.getAlignment()
1222 .getCodonFrame(sequence);
1225 * fudge: if no mapping found, check the complementary alignment
1226 * todo: only store in one place? StructureSelectionManager?
1228 if (mappings.isEmpty())
1230 mappings = this.av.getCodingComplement().getAlignment()
1231 .getCodonFrame(sequence);
1235 * todo: direct lookup of CDS for peptide and vice-versa; for now,
1236 * have to search through an unordered list of mappings for a candidate
1238 SequenceToSequenceMapping mapping = null;
1239 SequenceI mapFrom = null;
1241 for (AlignedCodonFrame acf : mappings)
1243 mapping = acf.getCoveringCodonMapping(ds);
1244 if (mapping == null)
1248 SearchResultsI sr = new SearchResults();
1249 mapping.markMappedRegion(ds, pos, sr);
1250 for (SearchResultMatchI match : sr.getResults())
1252 int fromRes = match.getStart();
1253 int toRes = match.getEnd();
1254 mapFrom = match.getSequence();
1255 List<SequenceFeature> fs = findFeaturesAtResidue(mapFrom, fromRes,
1257 for (SequenceFeature sf : fs)
1259 if (!found.contains(sf))
1267 * just take the first mapped features we find
1269 if (!found.isEmpty())
1274 if (found.isEmpty())
1280 * sort by renderorder (inefficiently but ok for small scale);
1281 * NB this sorts 'on top' feature to end, for rendering
1283 List<SequenceFeature> result = new ArrayList<>();
1284 final int toAdd = found.size();
1286 for (String type : renderOrder)
1288 for (SequenceFeature sf : found)
1290 if (type.equals(sf.getType()))
1302 return new MappedFeatures(mapping.getMapping(), mapFrom, pos, residue,
1307 public boolean isVisible(SequenceFeature feature)
1309 if (feature == null)
1313 if (getFeaturesDisplayed() == null
1314 || !getFeaturesDisplayed().isVisible(feature.getType()))
1318 if (featureGroupNotShown(feature))
1322 FeatureColourI fc = featureColours.get(feature.getType());
1323 if (fc != null && fc.isOutwithThreshold(feature))
1327 if (!featureMatchesFilters(feature))