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 PropertyChangeSupport changeSupport = new PropertyChangeSupport(
110 protected AlignViewportI av;
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 changeSupport.firePropertyChange("changeSupport", null, null);
305 firing = Boolean.FALSE;
311 public List<SequenceFeature> findFeaturesAtColumn(SequenceI sequence, int column)
314 * include features at the position provided their feature type is
315 * displayed, and feature group is null or marked for display
317 List<SequenceFeature> result = new ArrayList<>();
318 if (!av.areFeaturesDisplayed() || getFeaturesDisplayed() == null)
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,
331 * include features unless they are hidden (have no colour), based on
332 * feature group visibility, or a filter or colour threshold
334 for (SequenceFeature sf : features)
336 if (getColour(sf) != null)
345 * Searches alignment for all features and updates colours
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
353 public synchronized void findAllFeatures(boolean newMadeVisible)
355 newFeatureAdded = false;
359 newFeatureAdded = true;
363 findingFeatures = true;
364 if (av.getFeaturesDisplayed() == null)
366 av.setFeaturesDisplayed(new FeaturesDisplayed());
368 FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
370 Set<String> oldfeatures = new HashSet<>();
371 if (renderOrder != null)
373 for (int i = 0; i < renderOrder.length; i++)
375 if (renderOrder[i] != null)
377 oldfeatures.add(renderOrder[i]);
382 AlignmentI alignment = av.getAlignment();
383 List<String> allfeatures = new ArrayList<>();
385 for (int i = 0; i < alignment.getHeight(); i++)
387 SequenceI asq = alignment.getSequenceAt(i);
388 for (String group : asq.getFeatures().getFeatureGroups(true))
390 boolean groupDisplayed = true;
393 if (featureGroups.containsKey(group))
395 groupDisplayed = featureGroups.get(group);
399 groupDisplayed = newMadeVisible;
400 featureGroups.put(group, groupDisplayed);
405 Set<String> types = asq.getFeatures().getFeatureTypesForGroups(
407 for (String type : types)
409 if (!allfeatures.contains(type)) // or use HashSet and no test?
411 allfeatures.add(type);
413 updateMinMax(asq, type, true); // todo: for all features?
419 // uncomment to add new features in alphebetical order (but JAL-2575)
420 // Collections.sort(allfeatures, String.CASE_INSENSITIVE_ORDER);
423 for (String type : allfeatures)
425 if (!oldfeatures.contains(type))
427 featuresDisplayed.setVisible(type);
433 updateRenderOrder(allfeatures);
434 findingFeatures = false;
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.
446 protected void updateMinMax(SequenceI seq, String featureType,
449 float min = seq.getFeatures().getMinimumScore(featureType, positional);
450 if (Float.isNaN(min))
455 float max = seq.getFeatures().getMaximumScore(featureType, positional);
459 * { {positionalMin, positionalMax}, {nonPositionalMin, nonPositionalMax} }
463 minmax = new Hashtable<>();
465 synchronized (minmax)
467 float[][] mm = minmax.get(featureType);
468 int index = positional ? 0 : 1;
471 mm = new float[][] { null, null };
472 minmax.put(featureType, mm);
474 if (mm[index] == null)
476 mm[index] = new float[] { min, max };
480 mm[index][0] = Math.min(mm[index][0], min);
481 mm[index][1] = Math.max(mm[index][1], max);
485 protected Boolean firing = Boolean.FALSE;
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
498 private void updateRenderOrder(List<String> allFeatures)
500 List<String> allfeatures = new ArrayList<>(allFeatures);
501 String[] oldRender = renderOrder;
502 renderOrder = new String[allfeatures.size()];
503 boolean initOrders = (featureOrder == null);
505 if (oldRender != null && oldRender.length > 0)
507 for (int j = 0; j < oldRender.length; j++)
509 if (oldRender[j] != null)
513 setOrder(oldRender[j],
514 (1 - (1 + (float) j) / oldRender.length));
516 if (allfeatures.contains(oldRender[j]))
518 renderOrder[opos++] = oldRender[j]; // existing features always
519 // appear below new features
520 allfeatures.remove(oldRender[j]);
523 float[][] mmrange = minmax.get(oldRender[j]);
526 FeatureColourI fc = featureColours.get(oldRender[j]);
527 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled()
528 && !fc.isColourByAttribute())
530 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
538 if (allfeatures.size() == 0)
540 // no new features - leave order unchanged.
543 int i = allfeatures.size() - 1;
545 boolean sort = false;
546 String[] newf = new String[allfeatures.size()];
547 float[] sortOrder = new float[allfeatures.size()];
548 for (String newfeat : allfeatures)
553 // update from new features minmax if necessary
554 float[][] mmrange = minmax.get(newf[i]);
557 FeatureColourI fc = featureColours.get(newf[i]);
558 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled()
559 && !fc.isColourByAttribute())
561 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
565 if (initOrders || !featureOrder.containsKey(newf[i]))
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);
572 // set order from newly found feature from persisted ordering.
573 sortOrder[i] = 2 - featureOrder.get(newf[i]).floatValue();
576 // only sort if we need to
577 sort = sort || sortOrder[i] > sortOrder[i + 1];
581 if (iSize > 1 && sort)
583 jalview.util.QuickSort.sort(sortOrder, newf);
586 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
590 * get a feature style object for the given type string. Creates a
591 * java.awt.Color for a featureType with no existing colourscheme.
597 public FeatureColourI getFeatureStyle(String featureType)
599 FeatureColourI fc = featureColours.get(featureType);
602 Color col = ColorUtils.createColourFromName(featureType);
603 fc = new FeatureColour(col);
604 featureColours.put(featureType, fc);
610 public Color getColour(SequenceFeature feature)
612 FeatureColourI fc = getFeatureStyle(feature.getType());
613 return getColor(feature, fc);
617 * Answers true if the feature type is currently selected to be displayed,
623 public boolean showFeatureOfType(String type)
625 return type == null ? false : (av.getFeaturesDisplayed() == null ? true
626 : av.getFeaturesDisplayed().isVisible(type));
630 public void setColour(String featureType, FeatureColourI col)
632 featureColours.put(featureType, col);
636 public void setTransparency(float value)
638 transparency = value;
642 public float getTransparency()
648 * analogous to colour - store a normalized ordering for all feature types in
649 * this rendering context.
652 * Feature type string
654 * normalized priority - 0 means always appears on top, 1 means
657 public float setOrder(String type, float position)
659 if (featureOrder == null)
661 featureOrder = new Hashtable<>();
663 featureOrder.put(type, Float.valueOf(position));
668 * get the global priority (0 (top) to 1 (bottom))
671 * @return [0,1] or -1 for a type without a priority
673 public float getOrder(String type)
675 if (featureOrder != null)
677 if (featureOrder.containsKey(type))
679 return featureOrder.get(type).floatValue();
686 public Map<String, FeatureColourI> getFeatureColours()
688 return featureColours;
692 * Replace current ordering with new ordering
695 * an array of { Type, Colour, Filter, Boolean }
696 * @return true if any visible features have been reordered, else false
698 public boolean setFeaturePriority(FeatureSettingsBean[] data)
700 return setFeaturePriority(data, true);
704 * Sets the priority order for features, with the highest priority (displayed on
705 * top) at the start of the data array
708 * an array of { Type, Colour, Filter, Boolean }
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)
714 public boolean setFeaturePriority(FeatureSettingsBean[] data,
718 * note visible feature ordering and colours before update
720 List<String> visibleFeatures = getDisplayedFeatureTypes();
721 Map<String, FeatureColourI> visibleColours = new HashMap<>(
722 getFeatureColours());
724 FeaturesDisplayedI av_featuresdisplayed = null;
727 if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
729 av.getFeaturesDisplayed().clear();
733 av.setFeaturesDisplayed(
734 av_featuresdisplayed = new FeaturesDisplayed());
739 av_featuresdisplayed = av.getFeaturesDisplayed();
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];
752 for (int i = 0; i < data.length; i++)
754 String type = data[i].featureType;
755 setColour(type, data[i].featureColour);
758 av_featuresdisplayed.setVisible(type);
761 renderOrder[data.length - i - 1] = type;
766 * get the new visible ordering and return true if it has changed
767 * order or any colour has changed
769 List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
770 if (!visibleFeatures.equals(reorderedVisibleFeatures))
773 * the list of ordered visible features has changed
779 * return true if any feature colour has changed
781 for (String feature : visibleFeatures)
783 if (visibleColours.get(feature) != getFeatureStyle(feature))
793 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
795 public void addPropertyChangeListener(PropertyChangeListener listener)
797 changeSupport.addPropertyChangeListener(listener);
802 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
804 public void removePropertyChangeListener(PropertyChangeListener listener)
806 changeSupport.removePropertyChangeListener(listener);
809 public Set<String> getAllFeatureColours()
811 return featureColours.keySet();
814 public void clearRenderOrder()
819 public boolean hasRenderOrder()
821 return renderOrder != null;
825 * Returns feature types in ordering of rendering, where last means on top
827 public List<String> getRenderOrder()
829 if (renderOrder == null)
831 return Arrays.asList(new String[] {});
833 return Arrays.asList(renderOrder);
836 public int getFeatureGroupsSize()
838 return featureGroups != null ? 0 : featureGroups.size();
842 public List<String> getFeatureGroups()
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]));
850 public boolean checkGroupVisibility(String group,
851 boolean newGroupsVisible)
853 if (featureGroups == null)
855 // then an exception happens next..
857 if (featureGroups.containsKey(group))
859 return featureGroups.get(group).booleanValue();
861 if (newGroupsVisible)
863 featureGroups.put(group, Boolean.valueOf(true));
870 * get visible or invisible groups
873 * true to return visible groups, false to return hidden ones.
874 * @return list of groups
877 public List<String> getGroups(boolean visible)
879 if (featureGroups != null)
881 List<String> gp = new ArrayList<>();
883 for (String grp : featureGroups.keySet())
885 Boolean state = featureGroups.get(grp);
886 if (state.booleanValue() == visible)
897 public void setGroupVisibility(String group, boolean visible)
899 featureGroups.put(group, Boolean.valueOf(visible));
903 public void setGroupVisibility(List<String> toset, boolean visible)
905 if (toset != null && toset.size() > 0 && featureGroups != null)
907 boolean rdrw = false;
908 for (String gst : toset)
910 Boolean st = featureGroups.get(gst);
911 featureGroups.put(gst, Boolean.valueOf(visible));
914 rdrw = rdrw || (visible != st.booleanValue());
919 // set local flag indicating redraw needed ?
925 public Map<String, FeatureColourI> getDisplayedFeatureCols()
927 Map<String, FeatureColourI> fcols = new Hashtable<>();
928 if (getViewport().getFeaturesDisplayed() == null)
932 Set<String> features = getViewport().getFeaturesDisplayed()
933 .getVisibleFeatures();
934 for (String feature : features)
936 fcols.put(feature, getFeatureStyle(feature));
942 public FeaturesDisplayedI getFeaturesDisplayed()
944 return av.getFeaturesDisplayed();
948 * Returns a (possibly empty) list of visible feature types, in render order
952 public List<String> getDisplayedFeatureTypes()
954 List<String> typ = getRenderOrder();
955 List<String> displayed = new ArrayList<>();
956 FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
957 if (feature_disp != null)
959 synchronized (feature_disp)
961 for (String type : typ)
963 if (feature_disp.isVisible(type))
974 public List<String> getDisplayedFeatureGroups()
976 List<String> _gps = new ArrayList<>();
977 for (String gp : getFeatureGroups())
979 if (checkGroupVisibility(gp, false))
988 * Answers true if the feature belongs to a feature group which is not
989 * currently displayed, else false
991 * @param sequenceFeature
994 public boolean featureGroupNotShown(final SequenceFeature sequenceFeature)
996 return featureGroups != null
997 && sequenceFeature.featureGroup != null
998 && sequenceFeature.featureGroup.length() != 0
999 && featureGroups.containsKey(sequenceFeature.featureGroup)
1000 && !featureGroups.get(sequenceFeature.featureGroup)
1008 public List<SequenceFeature> findFeaturesAtResidue(SequenceI sequence,
1009 int fromResNo, int toResNo)
1011 List<SequenceFeature> result = new ArrayList<>();
1012 if (!av.areFeaturesDisplayed() || getFeaturesDisplayed() == null)
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
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);
1028 for (SequenceFeature sf : features)
1030 if (!featureGroupNotShown(sf) && getColour(sf) != null)
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.
1047 public void filterFeaturesForDisplay(List<SequenceFeature> features)
1050 * fudge: JalviewJS's IntervalStore lacks the sort method called :-(
1052 if (Platform.isJS())
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)
1062 if (features.isEmpty() || transparency != 1f
1063 || !featureFilters.isEmpty())
1068 SequenceFeatures.sortFeatures(features, true);
1069 SequenceFeature lastFeature = null;
1071 Iterator<SequenceFeature> it = features.iterator();
1072 while (it.hasNext())
1074 SequenceFeature sf = it.next();
1075 if (featureGroupNotShown(sf))
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)
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()))
1100 public Map<String, FeatureMatcherSetI> getFeatureFilters()
1102 return featureFilters;
1106 public void setFeatureFilters(Map<String, FeatureMatcherSetI> filters)
1108 featureFilters = filters;
1112 public FeatureMatcherSetI getFeatureFilter(String featureType)
1114 return featureFilters.get(featureType);
1118 public void setFeatureFilter(String featureType, FeatureMatcherSetI filter)
1120 if (filter == null || filter.isEmpty())
1122 featureFilters.remove(featureType);
1126 featureFilters.put(featureType, filter);
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.
1139 public Color getColor(SequenceFeature sf, FeatureColourI fc)
1142 * is the feature group displayed?
1144 if (featureGroupNotShown(sf))
1150 * does the feature pass filters?
1152 if (!featureMatchesFilters(sf))
1157 return fc.getColor(sf);
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
1168 protected boolean featureMatchesFilters(SequenceFeature sf)
1170 FeatureMatcherSetI filter = featureFilters.get(sf.getType());
1171 return filter == null ? true : filter.matches(sf);
1175 * Answers true unless the specified group is set to hidden. Defaults to true
1176 * if group visibility is not set.
1181 public boolean isGroupVisible(String group)
1183 if (!featureGroups.containsKey(group))
1187 return featureGroups.get(group);
1191 * Orders features in render precedence (last in order is last to render, so
1192 * displayed on top of other features)
1196 public void orderFeatures(Comparator<String> order)
1198 Arrays.sort(renderOrder, order);
1202 public MappedFeatures findComplementFeaturesAtResidue(
1203 final SequenceI sequence, final int pos)
1205 SequenceI ds = sequence.getDatasetSequence();
1210 final char residue = ds.getCharAt(pos - ds.getStart());
1212 List<SequenceFeature> found = new ArrayList<>();
1213 List<AlignedCodonFrame> mappings = this.av.getAlignment()
1214 .getCodonFrame(sequence);
1217 * fudge: if no mapping found, check the complementary alignment
1218 * todo: only store in one place? StructureSelectionManager?
1220 if (mappings.isEmpty())
1222 mappings = this.av.getCodingComplement().getAlignment()
1223 .getCodonFrame(sequence);
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
1230 SequenceToSequenceMapping mapping = null;
1231 SequenceI mapFrom = null;
1233 for (AlignedCodonFrame acf : mappings)
1235 mapping = acf.getCoveringCodonMapping(ds);
1236 if (mapping == null)
1240 SearchResultsI sr = new SearchResults();
1241 mapping.markMappedRegion(ds, pos, sr);
1242 for (SearchResultMatchI match : sr.getResults())
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)
1251 if (!found.contains(sf))
1259 * just take the first mapped features we find
1261 if (!found.isEmpty())
1266 if (found.isEmpty())
1272 * sort by renderorder (inefficiently but ok for small scale);
1273 * NB this sorts 'on top' feature to end, for rendering
1275 List<SequenceFeature> result = new ArrayList<>();
1276 final int toAdd = found.size();
1278 for (String type : renderOrder)
1280 for (SequenceFeature sf : found)
1282 if (type.equals(sf.getType()))
1294 return new MappedFeatures(mapping.getMapping(), mapFrom, pos, residue, result);
1298 public boolean isVisible(SequenceFeature feature)
1300 if (feature == null)
1304 if (getFeaturesDisplayed() == null
1305 || !getFeaturesDisplayed().isVisible(feature.getType()))
1309 if (featureGroupNotShown(feature))
1313 FeatureColourI fc = featureColours.get(feature.getType());
1314 if (fc != null && fc.isOutwithThreshold(feature))
1318 if (!featureMatchesFilters(feature))