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.Hashtable;
40 import java.util.Iterator;
41 import java.util.List;
44 import java.util.concurrent.ConcurrentHashMap;
46 public abstract class FeatureRendererModel implements
47 jalview.api.FeatureRenderer
51 * global transparency for feature
53 protected float transparency = 1.0f;
55 protected Map<String, FeatureColourI> featureColours = new ConcurrentHashMap<String, FeatureColourI>();
57 protected Map<String, Boolean> featureGroups = new ConcurrentHashMap<String, Boolean>();
59 protected String[] renderOrder;
61 Map<String, Float> featureOrder = null;
63 protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
66 protected AlignViewportI av;
69 public AlignViewportI getViewport()
74 public FeatureRendererSettings getSettings()
76 return new FeatureRendererSettings(this);
79 public void transferSettings(FeatureRendererSettings fr)
81 this.renderOrder = fr.renderOrder;
82 this.featureGroups = fr.featureGroups;
83 this.featureColours = fr.featureColours;
84 this.transparency = fr.transparency;
85 this.featureOrder = fr.featureOrder;
89 * update from another feature renderer
94 public void transferSettings(jalview.api.FeatureRenderer _fr)
96 FeatureRenderer fr = (FeatureRenderer) _fr;
97 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
98 this.renderOrder = frs.renderOrder;
99 this.featureGroups = frs.featureGroups;
100 this.featureColours = frs.featureColours;
101 this.transparency = frs.transparency;
102 this.featureOrder = frs.featureOrder;
103 if (av != null && av != fr.getViewport())
105 // copy over the displayed feature settings
106 if (_fr.getFeaturesDisplayed() != null)
108 FeaturesDisplayedI fd = getFeaturesDisplayed();
111 setFeaturesDisplayedFrom(_fr.getFeaturesDisplayed());
118 java.util.Iterator<String> fdisp = _fr.getFeaturesDisplayed()
119 .getVisibleFeatures();
120 while (fdisp.hasNext())
122 fd.setVisible(fdisp.next());
130 public void setFeaturesDisplayedFrom(FeaturesDisplayedI featuresDisplayed)
132 av.setFeaturesDisplayed(new FeaturesDisplayed(featuresDisplayed));
136 public void setVisible(String featureType)
138 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
141 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
143 if (!fdi.isRegistered(featureType))
145 pushFeatureType(Arrays.asList(new String[] { featureType }));
147 fdi.setVisible(featureType);
151 public void setAllVisible(List<String> featureTypes)
153 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
156 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
158 List<String> nft = new ArrayList<String>();
159 for (String featureType : featureTypes)
161 if (!fdi.isRegistered(featureType))
163 nft.add(featureType);
168 pushFeatureType(nft);
170 fdi.setAllVisible(featureTypes);
174 * push a set of new types onto the render order stack. Note - this is a
175 * direct mechanism rather than the one employed in updateRenderOrder
179 private void pushFeatureType(List<String> types)
182 int ts = types.size();
183 String neworder[] = new String[(renderOrder == null ? 0
184 : renderOrder.length) + ts];
185 types.toArray(neworder);
186 if (renderOrder != null)
188 System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
189 System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
191 renderOrder = neworder;
194 protected Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
196 public Map<String, float[][]> getMinMax()
202 * normalise a score against the max/min bounds for the feature type.
204 * @param sequenceFeature
205 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
208 protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
210 float[] mm = minmax.get(sequenceFeature.type)[0];
211 final byte[] r = new byte[] { 0, (byte) 255 };
214 if (r[0] != 0 || mm[0] < 0.0)
217 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
221 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
227 boolean newFeatureAdded = false;
229 boolean findingFeatures = false;
231 protected boolean updateFeatures()
233 if (av.getFeaturesDisplayed() == null || renderOrder == null
237 if (av.getFeaturesDisplayed().getVisibleFeatureCount() < 1)
242 // TODO: decide if we should check for the visible feature count first
247 * search the alignment for all new features, give them a colour and display
248 * them. Then fires a PropertyChangeEvent on the changeSupport object.
251 protected void findAllFeatures()
253 synchronized (firing)
255 if (firing.equals(Boolean.FALSE))
257 firing = Boolean.TRUE;
258 findAllFeatures(true); // add all new features as visible
259 changeSupport.firePropertyChange("changeSupport", null, null);
260 firing = Boolean.FALSE;
266 public List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
268 ArrayList<SequenceFeature> tmp = new ArrayList<SequenceFeature>();
269 SequenceFeature[] features = sequence.getSequenceFeatures();
271 if (features != null)
273 for (int i = 0; i < features.length; i++)
275 if (!av.areFeaturesDisplayed()
276 || !av.getFeaturesDisplayed().isVisible(
277 features[i].getType()))
282 if (features[i].featureGroup != null
283 && featureGroups != null
284 && featureGroups.containsKey(features[i].featureGroup)
285 && !featureGroups.get(features[i].featureGroup)
291 // check if start/end are at res, and if not a contact feature, that res
292 // lies between start and end
293 if ((features[i].getBegin() == res || features[i].getEnd() == res)
294 || (!features[i].isContactFeature()
295 && (features[i].getBegin() < res) && (features[i]
298 tmp.add(features[i]);
306 * Searches alignment for all features and updates colours
308 * @param newMadeVisible
309 * if true newly added feature types will be rendered immediatly
310 * TODO: check to see if this method should actually be proxied so
311 * repaint events can be propagated by the renderer code
314 public synchronized void findAllFeatures(boolean newMadeVisible)
316 newFeatureAdded = false;
320 newFeatureAdded = true;
324 findingFeatures = true;
325 if (av.getFeaturesDisplayed() == null)
327 av.setFeaturesDisplayed(new FeaturesDisplayed());
329 FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
331 ArrayList<String> allfeatures = new ArrayList<String>();
332 ArrayList<String> oldfeatures = new ArrayList<String>();
333 if (renderOrder != null)
335 for (int i = 0; i < renderOrder.length; i++)
337 if (renderOrder[i] != null)
339 oldfeatures.add(renderOrder[i]);
345 minmax = new Hashtable<String, float[][]>();
347 AlignmentI alignment = av.getAlignment();
348 for (int i = 0; i < alignment.getHeight(); i++)
350 SequenceI asq = alignment.getSequenceAt(i);
351 SequenceFeature[] features = asq.getSequenceFeatures();
353 if (features == null)
359 while (index < features.length)
361 if (!featuresDisplayed.isRegistered(features[index].getType()))
363 String fgrp = features[index].getFeatureGroup();
366 Boolean groupDisplayed = featureGroups.get(fgrp);
367 if (groupDisplayed == null)
369 groupDisplayed = Boolean.valueOf(newMadeVisible);
370 featureGroups.put(fgrp, groupDisplayed);
372 if (!groupDisplayed.booleanValue())
378 if (!(features[index].begin == 0 && features[index].end == 0))
380 // If beginning and end are 0, the feature is for the whole sequence
381 // and we don't want to render the feature in the normal way
384 && !oldfeatures.contains(features[index].getType()))
386 // this is a new feature type on the alignment. Mark it for
388 featuresDisplayed.setVisible(features[index].getType());
389 setOrder(features[index].getType(), 0);
393 if (!allfeatures.contains(features[index].getType()))
395 allfeatures.add(features[index].getType());
397 if (!Float.isNaN(features[index].score))
399 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
400 float[][] mm = minmax.get(features[index].getType());
403 mm = new float[][] { null, null };
404 minmax.put(features[index].getType(), mm);
406 if (mm[nonpos] == null)
408 mm[nonpos] = new float[] { features[index].score,
409 features[index].score };
414 if (mm[nonpos][0] > features[index].score)
416 mm[nonpos][0] = features[index].score;
418 if (mm[nonpos][1] < features[index].score)
420 mm[nonpos][1] = features[index].score;
427 updateRenderOrder(allfeatures);
428 findingFeatures = false;
431 protected Boolean firing = Boolean.FALSE;
434 * replaces the current renderOrder with the unordered features in
435 * allfeatures. The ordering of any types in both renderOrder and allfeatures
436 * is preserved, and all new feature types are rendered on top of the existing
437 * types, in the order given by getOrder or the order given in allFeatures.
438 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
439 * eliminate the float storage for computing/recalling the persistent ordering
440 * New Cability: updates min/max for colourscheme range if its dynamic
444 private void updateRenderOrder(List<String> allFeatures)
446 List<String> allfeatures = new ArrayList<String>(allFeatures);
447 String[] oldRender = renderOrder;
448 renderOrder = new String[allfeatures.size()];
449 boolean initOrders = (featureOrder == null);
451 if (oldRender != null && oldRender.length > 0)
453 for (int j = 0; j < oldRender.length; j++)
455 if (oldRender[j] != null)
459 setOrder(oldRender[j], (1 - (1 + (float) j) / oldRender.length));
461 if (allfeatures.contains(oldRender[j]))
463 renderOrder[opos++] = oldRender[j]; // existing features always
464 // appear below new features
465 allfeatures.remove(oldRender[j]);
468 float[][] mmrange = minmax.get(oldRender[j]);
471 FeatureColourI fc = featureColours.get(oldRender[j]);
472 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
474 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
482 if (allfeatures.size() == 0)
484 // no new features - leave order unchanged.
487 int i = allfeatures.size() - 1;
489 boolean sort = false;
490 String[] newf = new String[allfeatures.size()];
491 float[] sortOrder = new float[allfeatures.size()];
492 for (String newfeat : allfeatures)
497 // update from new features minmax if necessary
498 float[][] mmrange = minmax.get(newf[i]);
501 FeatureColourI fc = featureColours.get(newf[i]);
502 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
504 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
508 if (initOrders || !featureOrder.containsKey(newf[i]))
510 int denom = initOrders ? allfeatures.size() : featureOrder.size();
511 // new unordered feature - compute persistent ordering at head of
512 // existing features.
513 setOrder(newf[i], i / (float) denom);
515 // set order from newly found feature from persisted ordering.
516 sortOrder[i] = 2 - featureOrder.get(newf[i]).floatValue();
519 // only sort if we need to
520 sort = sort || sortOrder[i] > sortOrder[i + 1];
524 if (iSize > 1 && sort)
526 jalview.util.QuickSort.sort(sortOrder, newf);
529 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
533 * get a feature style object for the given type string. Creates a
534 * java.awt.Color for a featureType with no existing colourscheme.
540 public FeatureColourI getFeatureStyle(String featureType)
542 FeatureColourI fc = featureColours.get(featureType);
545 Color col = ColorUtils.createColourFromName(featureType);
546 fc = new FeatureColour(col);
547 featureColours.put(featureType, fc);
553 * Returns the configured colour for a particular feature instance. This
554 * includes calculation of 'colour by label', or of a graduated score colour,
555 * if applicable. It does not take into account feature visibility or colour
561 public Color getColour(SequenceFeature feature)
563 FeatureColourI fc = getFeatureStyle(feature.getType());
564 return fc.getColor(feature);
567 protected boolean showFeature(SequenceFeature sequenceFeature)
569 FeatureColourI fc = getFeatureStyle(sequenceFeature.type);
570 return fc.isColored(sequenceFeature);
574 * Answers true if the feature type is currently selected to be displayed,
580 protected boolean showFeatureOfType(String type)
582 return type == null ? false : av.getFeaturesDisplayed().isVisible(type);
586 public void setColour(String featureType, FeatureColourI col)
588 featureColours.put(featureType, col);
592 public void setTransparency(float value)
594 transparency = value;
598 public float getTransparency()
604 * analogous to colour - store a normalized ordering for all feature types in
605 * this rendering context.
608 * Feature type string
610 * normalized priority - 0 means always appears on top, 1 means
613 public float setOrder(String type, float position)
615 if (featureOrder == null)
617 featureOrder = new Hashtable<String, Float>();
619 featureOrder.put(type, new Float(position));
624 * get the global priority (0 (top) to 1 (bottom))
627 * @return [0,1] or -1 for a type without a priority
629 public float getOrder(String type)
631 if (featureOrder != null)
633 if (featureOrder.containsKey(type))
635 return featureOrder.get(type).floatValue();
642 public Map<String, FeatureColourI> getFeatureColours()
644 return featureColours;
648 * Replace current ordering with new ordering
651 * { String(Type), Colour(Type), Boolean(Displayed) }
652 * @return true if any visible features have been reordered, else false
654 public boolean setFeaturePriority(Object[][] data)
656 return setFeaturePriority(data, true);
660 * Sets the priority order for features
663 * { String(Type), Colour(Type), Boolean(Displayed) }
665 * when true current featureDisplay list will be cleared
666 * @return true if any visible features have been reordered or recoloured,
667 * else false (i.e. no need to repaint)
669 public boolean setFeaturePriority(Object[][] data, boolean visibleNew)
672 * note visible feature ordering and colours before update
674 List<String> visibleFeatures = getDisplayedFeatureTypes();
675 Map<String, FeatureColourI> visibleColours = new HashMap<String, FeatureColourI>(
676 getFeatureColours());
678 FeaturesDisplayedI av_featuresdisplayed = null;
681 if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
683 av.getFeaturesDisplayed().clear();
687 av.setFeaturesDisplayed(av_featuresdisplayed = new FeaturesDisplayed());
692 av_featuresdisplayed = av.getFeaturesDisplayed();
698 // The feature table will display high priority
699 // features at the top, but these are the ones
700 // we need to render last, so invert the data
701 renderOrder = new String[data.length];
705 for (int i = 0; i < data.length; i++)
707 String type = data[i][0].toString();
708 setColour(type, (FeatureColourI) data[i][1]);
709 if (((Boolean) data[i][2]).booleanValue())
711 av_featuresdisplayed.setVisible(type);
714 renderOrder[data.length - i - 1] = type;
719 * get the new visible ordering and return true if it has changed
720 * order or any colour has changed
722 List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
723 if (!visibleFeatures.equals(reorderedVisibleFeatures))
726 * the list of ordered visible features has changed
732 * return true if any feature colour has changed
734 for (String feature : visibleFeatures)
736 if (visibleColours.get(feature) != getFeatureStyle(feature))
746 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
748 public void addPropertyChangeListener(PropertyChangeListener listener)
750 changeSupport.addPropertyChangeListener(listener);
755 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
757 public void removePropertyChangeListener(PropertyChangeListener listener)
759 changeSupport.removePropertyChangeListener(listener);
762 public Set<String> getAllFeatureColours()
764 return featureColours.keySet();
767 public void clearRenderOrder()
772 public boolean hasRenderOrder()
774 return renderOrder != null;
778 * Returns feature types in ordering of rendering, where last means on top
780 public List<String> getRenderOrder()
782 if (renderOrder == null)
784 return Arrays.asList(new String[] {});
786 return Arrays.asList(renderOrder);
789 public int getFeatureGroupsSize()
791 return featureGroups != null ? 0 : featureGroups.size();
795 public List<String> getFeatureGroups()
797 // conflict between applet and desktop - featureGroups returns the map in
798 // the desktop featureRenderer
799 return (featureGroups == null) ? Arrays.asList(new String[0]) : Arrays
800 .asList(featureGroups.keySet().toArray(new String[0]));
803 public boolean checkGroupVisibility(String group, boolean newGroupsVisible)
805 if (featureGroups == null)
807 // then an exception happens next..
809 if (featureGroups.containsKey(group))
811 return featureGroups.get(group).booleanValue();
813 if (newGroupsVisible)
815 featureGroups.put(group, new Boolean(true));
822 * get visible or invisible groups
825 * true to return visible groups, false to return hidden ones.
826 * @return list of groups
829 public List<String> getGroups(boolean visible)
831 if (featureGroups != null)
833 List<String> gp = new ArrayList<String>();
835 for (String grp : featureGroups.keySet())
837 Boolean state = featureGroups.get(grp);
838 if (state.booleanValue() == visible)
849 public void setGroupVisibility(String group, boolean visible)
851 featureGroups.put(group, new Boolean(visible));
855 public void setGroupVisibility(List<String> toset, boolean visible)
857 if (toset != null && toset.size() > 0 && featureGroups != null)
859 boolean rdrw = false;
860 for (String gst : toset)
862 Boolean st = featureGroups.get(gst);
863 featureGroups.put(gst, new Boolean(visible));
866 rdrw = rdrw || (visible != st.booleanValue());
871 // set local flag indicating redraw needed ?
877 public Map<String, FeatureColourI> getDisplayedFeatureCols()
879 Map<String, FeatureColourI> fcols = new Hashtable<String, FeatureColourI>();
880 if (getViewport().getFeaturesDisplayed() == null)
884 Iterator<String> features = getViewport().getFeaturesDisplayed()
885 .getVisibleFeatures();
886 while (features.hasNext())
888 String feature = features.next();
889 fcols.put(feature, getFeatureStyle(feature));
895 public FeaturesDisplayedI getFeaturesDisplayed()
897 return av.getFeaturesDisplayed();
901 * Returns a (possibly empty) list of visible feature types, in render order
905 public List<String> getDisplayedFeatureTypes()
907 List<String> typ = getRenderOrder();
908 List<String> displayed = new ArrayList<String>();
909 FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
910 if (feature_disp != null)
912 synchronized (feature_disp)
914 for (String type : typ)
916 if (feature_disp.isVisible(type))
927 public List<String> getDisplayedFeatureGroups()
929 List<String> _gps = new ArrayList<String>();
930 boolean valid = false;
931 for (String gp : getFeatureGroups())
933 if (checkGroupVisibility(gp, false))
944 // gps = new String[_gps.size()];
945 // _gps.toArray(gps);