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 jalview.api.AlignViewportI;
24 import jalview.api.FeatureColourI;
25 import jalview.api.FeaturesDisplayedI;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.SequenceFeature;
28 import jalview.datamodel.SequenceI;
29 import jalview.renderer.seqfeatures.FeatureRenderer;
30 import jalview.schemes.FeatureColour;
31 import jalview.util.ColorUtils;
33 import java.awt.Color;
34 import java.beans.PropertyChangeListener;
35 import java.beans.PropertyChangeSupport;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.Hashtable;
41 import java.util.Iterator;
42 import java.util.List;
45 import java.util.concurrent.ConcurrentHashMap;
47 public abstract class FeatureRendererModel implements
48 jalview.api.FeatureRenderer
52 * global transparency for feature
54 protected float transparency = 1.0f;
56 protected Map<String, FeatureColourI> featureColours = new ConcurrentHashMap<String, FeatureColourI>();
58 protected Map<String, Boolean> featureGroups = new ConcurrentHashMap<String, Boolean>();
60 protected String[] renderOrder;
62 Map<String, Float> featureOrder = null;
64 protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
67 protected AlignViewportI av;
70 public AlignViewportI getViewport()
75 public FeatureRendererSettings getSettings()
77 return new FeatureRendererSettings(this);
80 public void transferSettings(FeatureRendererSettings fr)
82 this.renderOrder = fr.renderOrder;
83 this.featureGroups = fr.featureGroups;
84 this.featureColours = fr.featureColours;
85 this.transparency = fr.transparency;
86 this.featureOrder = fr.featureOrder;
90 * update from another feature renderer
95 public void transferSettings(jalview.api.FeatureRenderer _fr)
97 FeatureRenderer fr = (FeatureRenderer) _fr;
98 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
99 this.renderOrder = frs.renderOrder;
100 this.featureGroups = frs.featureGroups;
101 this.featureColours = frs.featureColours;
102 this.transparency = frs.transparency;
103 this.featureOrder = frs.featureOrder;
104 if (av != null && av != fr.getViewport())
106 // copy over the displayed feature settings
107 if (_fr.getFeaturesDisplayed() != null)
109 FeaturesDisplayedI fd = getFeaturesDisplayed();
112 setFeaturesDisplayedFrom(_fr.getFeaturesDisplayed());
119 java.util.Iterator<String> fdisp = _fr.getFeaturesDisplayed()
120 .getVisibleFeatures();
121 while (fdisp.hasNext())
123 fd.setVisible(fdisp.next());
131 public void setFeaturesDisplayedFrom(FeaturesDisplayedI featuresDisplayed)
133 av.setFeaturesDisplayed(new FeaturesDisplayed(featuresDisplayed));
137 public void setVisible(String featureType)
139 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
142 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
144 if (!fdi.isRegistered(featureType))
146 pushFeatureType(Arrays.asList(new String[] { featureType }));
148 fdi.setVisible(featureType);
152 public void setAllVisible(List<String> featureTypes)
154 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
157 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
159 List<String> nft = new ArrayList<String>();
160 for (String featureType : featureTypes)
162 if (!fdi.isRegistered(featureType))
164 nft.add(featureType);
169 pushFeatureType(nft);
171 fdi.setAllVisible(featureTypes);
175 * push a set of new types onto the render order stack. Note - this is a
176 * direct mechanism rather than the one employed in updateRenderOrder
180 private void pushFeatureType(List<String> types)
183 int ts = types.size();
184 String neworder[] = new String[(renderOrder == null ? 0
185 : renderOrder.length) + ts];
186 types.toArray(neworder);
187 if (renderOrder != null)
189 System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
190 System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
192 renderOrder = neworder;
195 protected Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
197 public Map<String, float[][]> getMinMax()
203 * normalise a score against the max/min bounds for the feature type.
205 * @param sequenceFeature
206 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
209 protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
211 float[] mm = minmax.get(sequenceFeature.type)[0];
212 final byte[] r = new byte[] { 0, (byte) 255 };
215 if (r[0] != 0 || mm[0] < 0.0)
218 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
222 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
228 boolean newFeatureAdded = false;
230 boolean findingFeatures = false;
232 protected boolean updateFeatures()
234 if (av.getFeaturesDisplayed() == null || renderOrder == null
238 if (av.getFeaturesDisplayed().getVisibleFeatureCount() < 1)
243 // TODO: decide if we should check for the visible feature count first
248 * search the alignment for all new features, give them a colour and display
249 * them. Then fires a PropertyChangeEvent on the changeSupport object.
252 protected void findAllFeatures()
254 synchronized (firing)
256 if (firing.equals(Boolean.FALSE))
258 firing = Boolean.TRUE;
259 findAllFeatures(true); // add all new features as visible
260 changeSupport.firePropertyChange("changeSupport", null, null);
261 firing = Boolean.FALSE;
267 public List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
269 ArrayList<SequenceFeature> tmp = new ArrayList<SequenceFeature>();
270 SequenceFeature[] features = sequence.getSequenceFeatures();
272 if (features != null)
274 for (int i = 0; i < features.length; i++)
276 if (!av.areFeaturesDisplayed()
277 || !av.getFeaturesDisplayed().isVisible(
278 features[i].getType()))
283 if (features[i].featureGroup != null
284 && featureGroups != null
285 && featureGroups.containsKey(features[i].featureGroup)
286 && !featureGroups.get(features[i].featureGroup)
292 // check if start/end are at res, and if not a contact feature, that res
293 // lies between start and end
294 if ((features[i].getBegin() == res || features[i].getEnd() == res)
295 || (!features[i].isContactFeature()
296 && (features[i].getBegin() < res) && (features[i]
299 tmp.add(features[i]);
307 * Searches alignment for all features and updates colours
309 * @param newMadeVisible
310 * if true newly added feature types will be rendered immediatly
311 * TODO: check to see if this method should actually be proxied so
312 * repaint events can be propagated by the renderer code
315 public synchronized void findAllFeatures(boolean newMadeVisible)
317 newFeatureAdded = false;
321 newFeatureAdded = true;
325 findingFeatures = true;
326 if (av.getFeaturesDisplayed() == null)
328 av.setFeaturesDisplayed(new FeaturesDisplayed());
330 FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
332 ArrayList<String> allfeatures = new ArrayList<String>();
333 ArrayList<String> oldfeatures = new ArrayList<String>();
334 if (renderOrder != null)
336 for (int i = 0; i < renderOrder.length; i++)
338 if (renderOrder[i] != null)
340 oldfeatures.add(renderOrder[i]);
346 minmax = new Hashtable<String, float[][]>();
349 Set<String> oldGroups = new HashSet<String>(featureGroups.keySet());
350 AlignmentI alignment = av.getAlignment();
351 for (int i = 0; i < alignment.getHeight(); i++)
353 SequenceI asq = alignment.getSequenceAt(i);
354 SequenceFeature[] features = asq.getSequenceFeatures();
356 if (features == null)
362 while (index < features.length)
364 String fgrp = features[index].getFeatureGroup();
365 oldGroups.remove(fgrp);
366 if (!featuresDisplayed.isRegistered(features[index].getType()))
370 Boolean groupDisplayed = featureGroups.get(fgrp);
371 if (groupDisplayed == null)
373 groupDisplayed = Boolean.valueOf(newMadeVisible);
374 featureGroups.put(fgrp, groupDisplayed);
376 if (!groupDisplayed.booleanValue())
382 if (!(features[index].begin == 0 && features[index].end == 0))
384 // If beginning and end are 0, the feature is for the whole sequence
385 // and we don't want to render the feature in the normal way
388 && !oldfeatures.contains(features[index].getType()))
390 // this is a new feature type on the alignment. Mark it for
392 featuresDisplayed.setVisible(features[index].getType());
393 setOrder(features[index].getType(), 0);
397 if (!allfeatures.contains(features[index].getType()))
399 allfeatures.add(features[index].getType());
401 if (!Float.isNaN(features[index].score))
403 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
404 float[][] mm = minmax.get(features[index].getType());
407 mm = new float[][] { null, null };
408 minmax.put(features[index].getType(), mm);
410 if (mm[nonpos] == null)
412 mm[nonpos] = new float[] { features[index].score,
413 features[index].score };
418 if (mm[nonpos][0] > features[index].score)
420 mm[nonpos][0] = features[index].score;
422 if (mm[nonpos][1] < features[index].score)
424 mm[nonpos][1] = features[index].score;
433 * oldGroups now consists of groups that no longer
434 * have any feature in them - remove these
436 for (String grp : oldGroups)
438 featureGroups.remove(grp);
441 updateRenderOrder(allfeatures);
442 findingFeatures = false;
445 protected Boolean firing = Boolean.FALSE;
448 * replaces the current renderOrder with the unordered features in
449 * allfeatures. The ordering of any types in both renderOrder and allfeatures
450 * is preserved, and all new feature types are rendered on top of the existing
451 * types, in the order given by getOrder or the order given in allFeatures.
452 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
453 * eliminate the float storage for computing/recalling the persistent ordering
454 * New Cability: updates min/max for colourscheme range if its dynamic
458 private void updateRenderOrder(List<String> allFeatures)
460 List<String> allfeatures = new ArrayList<String>(allFeatures);
461 String[] oldRender = renderOrder;
462 renderOrder = new String[allfeatures.size()];
463 boolean initOrders = (featureOrder == null);
465 if (oldRender != null && oldRender.length > 0)
467 for (int j = 0; j < oldRender.length; j++)
469 if (oldRender[j] != null)
473 setOrder(oldRender[j], (1 - (1 + (float) j) / oldRender.length));
475 if (allfeatures.contains(oldRender[j]))
477 renderOrder[opos++] = oldRender[j]; // existing features always
478 // appear below new features
479 allfeatures.remove(oldRender[j]);
482 float[][] mmrange = minmax.get(oldRender[j]);
485 FeatureColourI fc = featureColours.get(oldRender[j]);
486 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
488 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
496 if (allfeatures.size() == 0)
498 // no new features - leave order unchanged.
501 int i = allfeatures.size() - 1;
503 boolean sort = false;
504 String[] newf = new String[allfeatures.size()];
505 float[] sortOrder = new float[allfeatures.size()];
506 for (String newfeat : allfeatures)
511 // update from new features minmax if necessary
512 float[][] mmrange = minmax.get(newf[i]);
515 FeatureColourI fc = featureColours.get(newf[i]);
516 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
518 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
522 if (initOrders || !featureOrder.containsKey(newf[i]))
524 int denom = initOrders ? allfeatures.size() : featureOrder.size();
525 // new unordered feature - compute persistent ordering at head of
526 // existing features.
527 setOrder(newf[i], i / (float) denom);
529 // set order from newly found feature from persisted ordering.
530 sortOrder[i] = 2 - featureOrder.get(newf[i]).floatValue();
533 // only sort if we need to
534 sort = sort || sortOrder[i] > sortOrder[i + 1];
538 if (iSize > 1 && sort)
540 jalview.util.QuickSort.sort(sortOrder, newf);
543 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
547 * get a feature style object for the given type string. Creates a
548 * java.awt.Color for a featureType with no existing colourscheme.
554 public FeatureColourI getFeatureStyle(String featureType)
556 FeatureColourI fc = featureColours.get(featureType);
559 Color col = ColorUtils.createColourFromName(featureType);
560 fc = new FeatureColour(col);
561 featureColours.put(featureType, fc);
567 * Returns the configured colour for a particular feature instance. This
568 * includes calculation of 'colour by label', or of a graduated score colour,
569 * if applicable. It does not take into account feature visibility or colour
575 public Color getColour(SequenceFeature feature)
577 FeatureColourI fc = getFeatureStyle(feature.getType());
578 return fc.isColored(feature) ? fc.getColor(feature) : null;
582 * Answers true unless the feature has a score value which lies outside a
583 * minimum or maximum threshold configured for colouring. This method does not
584 * check feature type or group visibility.
586 * @param sequenceFeature
589 protected boolean showFeature(SequenceFeature sequenceFeature)
591 FeatureColourI fc = getFeatureStyle(sequenceFeature.type);
592 return fc.isColored(sequenceFeature);
596 * Answers true if the feature type is currently selected to be displayed,
602 protected boolean showFeatureOfType(String type)
604 return type == null ? false : av.getFeaturesDisplayed().isVisible(type);
608 public void setColour(String featureType, FeatureColourI col)
610 featureColours.put(featureType, col);
614 public void setTransparency(float value)
616 transparency = value;
620 public float getTransparency()
626 * analogous to colour - store a normalized ordering for all feature types in
627 * this rendering context.
630 * Feature type string
632 * normalized priority - 0 means always appears on top, 1 means
635 public float setOrder(String type, float position)
637 if (featureOrder == null)
639 featureOrder = new Hashtable<String, Float>();
641 featureOrder.put(type, new Float(position));
646 * get the global priority (0 (top) to 1 (bottom))
649 * @return [0,1] or -1 for a type without a priority
651 public float getOrder(String type)
653 if (featureOrder != null)
655 if (featureOrder.containsKey(type))
657 return featureOrder.get(type).floatValue();
664 public Map<String, FeatureColourI> getFeatureColours()
666 return featureColours;
670 * Replace current ordering with new ordering
673 * { String(Type), Colour(Type), Boolean(Displayed) }
674 * @return true if any visible features have been reordered, else false
676 public boolean setFeaturePriority(Object[][] data)
678 return setFeaturePriority(data, true);
682 * Sets the priority order for features
685 * { String(Type), Colour(Type), Boolean(Displayed) }
687 * when true current featureDisplay list will be cleared
688 * @return true if any visible features have been reordered or recoloured,
689 * else false (i.e. no need to repaint)
691 public boolean setFeaturePriority(Object[][] data, boolean visibleNew)
694 * note visible feature ordering and colours before update
696 List<String> visibleFeatures = getDisplayedFeatureTypes();
697 Map<String, FeatureColourI> visibleColours = new HashMap<String, FeatureColourI>(
698 getFeatureColours());
700 FeaturesDisplayedI av_featuresdisplayed = null;
703 if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
705 av.getFeaturesDisplayed().clear();
709 av.setFeaturesDisplayed(av_featuresdisplayed = new FeaturesDisplayed());
714 av_featuresdisplayed = av.getFeaturesDisplayed();
720 // The feature table will display high priority
721 // features at the top, but these are the ones
722 // we need to render last, so invert the data
723 renderOrder = new String[data.length];
727 for (int i = 0; i < data.length; i++)
729 String type = data[i][0].toString();
730 setColour(type, (FeatureColourI) data[i][1]);
731 if (((Boolean) data[i][2]).booleanValue())
733 av_featuresdisplayed.setVisible(type);
736 renderOrder[data.length - i - 1] = type;
741 * get the new visible ordering and return true if it has changed
742 * order or any colour has changed
744 List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
745 if (!visibleFeatures.equals(reorderedVisibleFeatures))
748 * the list of ordered visible features has changed
754 * return true if any feature colour has changed
756 for (String feature : visibleFeatures)
758 if (visibleColours.get(feature) != getFeatureStyle(feature))
768 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
770 public void addPropertyChangeListener(PropertyChangeListener listener)
772 changeSupport.addPropertyChangeListener(listener);
777 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
779 public void removePropertyChangeListener(PropertyChangeListener listener)
781 changeSupport.removePropertyChangeListener(listener);
784 public Set<String> getAllFeatureColours()
786 return featureColours.keySet();
789 public void clearRenderOrder()
794 public boolean hasRenderOrder()
796 return renderOrder != null;
800 * Returns feature types in ordering of rendering, where last means on top
802 public List<String> getRenderOrder()
804 if (renderOrder == null)
806 return Arrays.asList(new String[] {});
808 return Arrays.asList(renderOrder);
811 public int getFeatureGroupsSize()
813 return featureGroups != null ? 0 : featureGroups.size();
817 public List<String> getFeatureGroups()
819 // conflict between applet and desktop - featureGroups returns the map in
820 // the desktop featureRenderer
821 return (featureGroups == null) ? Arrays.asList(new String[0]) : Arrays
822 .asList(featureGroups.keySet().toArray(new String[0]));
825 public boolean checkGroupVisibility(String group, boolean newGroupsVisible)
827 if (featureGroups == null)
829 // then an exception happens next..
831 if (featureGroups.containsKey(group))
833 return featureGroups.get(group).booleanValue();
835 if (newGroupsVisible)
837 featureGroups.put(group, new Boolean(true));
844 * get visible or invisible groups
847 * true to return visible groups, false to return hidden ones.
848 * @return list of groups
851 public List<String> getGroups(boolean visible)
853 if (featureGroups != null)
855 List<String> gp = new ArrayList<String>();
857 for (String grp : featureGroups.keySet())
859 Boolean state = featureGroups.get(grp);
860 if (state.booleanValue() == visible)
871 public void setGroupVisibility(String group, boolean visible)
873 featureGroups.put(group, new Boolean(visible));
877 public void setGroupVisibility(List<String> toset, boolean visible)
879 if (toset != null && toset.size() > 0 && featureGroups != null)
881 boolean rdrw = false;
882 for (String gst : toset)
884 Boolean st = featureGroups.get(gst);
885 featureGroups.put(gst, new Boolean(visible));
888 rdrw = rdrw || (visible != st.booleanValue());
893 // set local flag indicating redraw needed ?
899 public Map<String, FeatureColourI> getDisplayedFeatureCols()
901 Map<String, FeatureColourI> fcols = new Hashtable<String, FeatureColourI>();
902 if (getViewport().getFeaturesDisplayed() == null)
906 Iterator<String> features = getViewport().getFeaturesDisplayed()
907 .getVisibleFeatures();
908 while (features.hasNext())
910 String feature = features.next();
911 fcols.put(feature, getFeatureStyle(feature));
917 public FeaturesDisplayedI getFeaturesDisplayed()
919 return av.getFeaturesDisplayed();
923 * Returns a (possibly empty) list of visible feature types, in render order
927 public List<String> getDisplayedFeatureTypes()
929 List<String> typ = getRenderOrder();
930 List<String> displayed = new ArrayList<String>();
931 FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
932 if (feature_disp != null)
934 synchronized (feature_disp)
936 for (String type : typ)
938 if (feature_disp.isVisible(type))
949 public List<String> getDisplayedFeatureGroups()
951 List<String> _gps = new ArrayList<String>();
952 boolean valid = false;
953 for (String gp : getFeatureGroups())
955 if (checkGroupVisibility(gp, false))
966 // gps = new String[_gps.size()];
967 // _gps.toArray(gps);