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.FeaturesDisplayedI;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.renderer.seqfeatures.FeatureRenderer;
29 import jalview.schemes.GraduatedColor;
30 import jalview.viewmodel.AlignmentViewport;
32 import java.awt.Color;
33 import java.beans.PropertyChangeListener;
34 import java.beans.PropertyChangeSupport;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.HashMap;
38 import java.util.Hashtable;
39 import java.util.Iterator;
40 import java.util.List;
43 import java.util.concurrent.ConcurrentHashMap;
45 public abstract class FeatureRendererModel implements
46 jalview.api.FeatureRenderer
50 * global transparency for feature
52 protected float transparency = 1.0f;
54 protected Map<String, Object> featureColours = new ConcurrentHashMap<String, Object>();
56 protected Map<String, Boolean> featureGroups = new ConcurrentHashMap<String, Boolean>();
58 protected Object currentColour;
61 * ordering of rendering, where last means on top
63 protected String[] renderOrder;
65 protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
68 protected AlignmentViewport av;
71 public AlignViewportI getViewport()
76 public FeatureRendererSettings getSettings()
78 return new FeatureRendererSettings(this);
81 public void transferSettings(FeatureRendererSettings fr)
83 this.renderOrder = fr.renderOrder;
84 this.featureGroups = fr.featureGroups;
85 this.featureColours = fr.featureColours;
86 this.transparency = fr.transparency;
87 this.featureOrder = fr.featureOrder;
91 * update from another feature renderer
96 public void transferSettings(jalview.api.FeatureRenderer _fr)
98 FeatureRenderer fr = (FeatureRenderer) _fr;
99 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
100 this.renderOrder = frs.renderOrder;
101 this.featureGroups = frs.featureGroups;
102 this.featureColours = frs.featureColours;
103 this.transparency = frs.transparency;
104 this.featureOrder = frs.featureOrder;
105 if (av != null && av != fr.getViewport())
107 // copy over the displayed feature settings
108 if (_fr.getFeaturesDisplayed() != null)
110 FeaturesDisplayedI fd = getFeaturesDisplayed();
113 setFeaturesDisplayedFrom(_fr.getFeaturesDisplayed());
120 java.util.Iterator<String> fdisp = _fr.getFeaturesDisplayed()
121 .getVisibleFeatures();
122 while (fdisp.hasNext())
124 fd.setVisible(fdisp.next());
132 public void setFeaturesDisplayedFrom(FeaturesDisplayedI featuresDisplayed)
134 av.setFeaturesDisplayed(new FeaturesDisplayed(featuresDisplayed));
138 public void setVisible(String featureType)
140 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
143 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
145 if (!fdi.isRegistered(featureType))
147 pushFeatureType(Arrays.asList(new String[] { featureType }));
149 fdi.setVisible(featureType);
153 public void setAllVisible(List<String> featureTypes)
155 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
158 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
160 List<String> nft = new ArrayList<String>();
161 for (String featureType : featureTypes)
163 if (!fdi.isRegistered(featureType))
165 nft.add(featureType);
170 pushFeatureType(nft);
172 fdi.setAllVisible(featureTypes);
176 * push a set of new types onto the render order stack. Note - this is a
177 * direct mechanism rather than the one employed in updateRenderOrder
181 private void pushFeatureType(List<String> types)
184 int ts = types.size();
185 String neworder[] = new String[(renderOrder == null ? 0
186 : renderOrder.length) + ts];
187 types.toArray(neworder);
188 if (renderOrder != null)
190 System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
191 System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
193 renderOrder = neworder;
196 protected Hashtable minmax = new Hashtable();
198 public Hashtable getMinMax()
204 * normalise a score against the max/min bounds for the feature type.
206 * @param sequenceFeature
207 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
210 protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
212 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
213 final byte[] r = new byte[] { 0, (byte) 255 };
216 if (r[0] != 0 || mm[0] < 0.0)
219 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
223 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
229 boolean newFeatureAdded = false;
231 boolean findingFeatures = false;
233 protected boolean updateFeatures()
235 if (av.getFeaturesDisplayed() == null || renderOrder == null
239 if (av.getFeaturesDisplayed().getVisibleFeatureCount() < 1)
244 // TODO: decide if we should check for the visible feature count first
249 * search the alignment for all new features, give them a colour and display
250 * them. Then fires a PropertyChangeEvent on the changeSupport object.
253 protected void findAllFeatures()
255 synchronized (firing)
257 if (firing.equals(Boolean.FALSE))
259 firing = Boolean.TRUE;
260 findAllFeatures(true); // add all new features as visible
261 changeSupport.firePropertyChange("changeSupport", null, null);
262 firing = Boolean.FALSE;
268 public List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
270 ArrayList<SequenceFeature> tmp = new ArrayList<SequenceFeature>();
271 SequenceFeature[] features = sequence.getSequenceFeatures();
273 if (features != null)
275 for (int i = 0; i < features.length; i++)
277 if (!av.areFeaturesDisplayed()
278 || !av.getFeaturesDisplayed().isVisible(
279 features[i].getType()))
284 if (features[i].featureGroup != null
285 && featureGroups != null
286 && featureGroups.containsKey(features[i].featureGroup)
287 && !featureGroups.get(features[i].featureGroup)
293 if ((features[i].getBegin() <= res)
294 && (features[i].getEnd() >= res))
296 tmp.add(features[i]);
304 * Searches alignment for all features and updates colours
306 * @param newMadeVisible
307 * if true newly added feature types will be rendered immediatly
308 * TODO: check to see if this method should actually be proxied so
309 * repaint events can be propagated by the renderer code
312 public synchronized void findAllFeatures(boolean newMadeVisible)
314 newFeatureAdded = false;
318 newFeatureAdded = true;
322 findingFeatures = true;
323 if (av.getFeaturesDisplayed() == null)
325 av.setFeaturesDisplayed(new FeaturesDisplayed());
327 FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
329 ArrayList<String> allfeatures = new ArrayList<String>();
330 ArrayList<String> oldfeatures = new ArrayList<String>();
331 if (renderOrder != null)
333 for (int i = 0; i < renderOrder.length; i++)
335 if (renderOrder[i] != null)
337 oldfeatures.add(renderOrder[i]);
343 minmax = new Hashtable();
345 AlignmentI alignment = av.getAlignment();
346 for (int i = 0; i < alignment.getHeight(); i++)
348 SequenceI asq = alignment.getSequenceAt(i);
349 SequenceFeature[] features = asq.getSequenceFeatures();
351 if (features == null)
357 while (index < features.length)
359 if (!featuresDisplayed.isRegistered(features[index].getType()))
361 String fgrp = features[index].getFeatureGroup();
364 Boolean groupDisplayed = featureGroups.get(fgrp);
365 if (groupDisplayed == null)
367 groupDisplayed = Boolean.valueOf(newMadeVisible);
368 featureGroups.put(fgrp, groupDisplayed);
370 if (!groupDisplayed.booleanValue())
376 if (!(features[index].begin == 0 && features[index].end == 0))
378 // If beginning and end are 0, the feature is for the whole sequence
379 // and we don't want to render the feature in the normal way
382 && !oldfeatures.contains(features[index].getType()))
384 // this is a new feature type on the alignment. Mark it for
386 featuresDisplayed.setVisible(features[index].getType());
387 setOrder(features[index].getType(), 0);
391 if (!allfeatures.contains(features[index].getType()))
393 allfeatures.add(features[index].getType());
395 if (!Float.isNaN(features[index].score))
397 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
398 float[][] mm = (float[][]) minmax.get(features[index].getType());
401 mm = new float[][] { null, null };
402 minmax.put(features[index].getType(), mm);
404 if (mm[nonpos] == null)
406 mm[nonpos] = new float[] { features[index].score,
407 features[index].score };
412 if (mm[nonpos][0] > features[index].score)
414 mm[nonpos][0] = features[index].score;
416 if (mm[nonpos][1] < features[index].score)
418 mm[nonpos][1] = features[index].score;
425 updateRenderOrder(allfeatures);
426 findingFeatures = false;
429 protected Boolean firing = Boolean.FALSE;
432 * replaces the current renderOrder with the unordered features in
433 * allfeatures. The ordering of any types in both renderOrder and allfeatures
434 * is preserved, and all new feature types are rendered on top of the existing
435 * types, in the order given by getOrder or the order given in allFeatures.
436 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
437 * eliminate the float storage for computing/recalling the persistent ordering
438 * New Cability: updates min/max for colourscheme range if its dynamic
442 private void updateRenderOrder(List<String> allFeatures)
444 List<String> allfeatures = new ArrayList<String>(allFeatures);
445 String[] oldRender = renderOrder;
446 renderOrder = new String[allfeatures.size()];
447 Object mmrange, fc = null;
448 boolean initOrders = (featureOrder == null);
450 if (oldRender != null && oldRender.length > 0)
452 for (int j = 0; j < oldRender.length; j++)
454 if (oldRender[j] != null)
458 setOrder(oldRender[j], (1 - (1 + (float) j) / oldRender.length));
460 if (allfeatures.contains(oldRender[j]))
462 renderOrder[opos++] = oldRender[j]; // existing features always
463 // appear below new features
464 allfeatures.remove(oldRender[j]);
467 mmrange = minmax.get(oldRender[j]);
470 fc = featureColours.get(oldRender[j]);
471 if (fc != null && fc instanceof GraduatedColor
472 && ((GraduatedColor) fc).isAutoScale())
474 ((GraduatedColor) fc).updateBounds(
475 ((float[][]) mmrange)[0][0],
476 ((float[][]) mmrange)[0][1]);
484 if (allfeatures.size() == 0)
486 // no new features - leave order unchanged.
489 int i = allfeatures.size() - 1;
491 boolean sort = false;
492 String[] newf = new String[allfeatures.size()];
493 float[] sortOrder = new float[allfeatures.size()];
494 for (String newfeat : allfeatures)
499 // update from new features minmax if necessary
500 mmrange = minmax.get(newf[i]);
503 fc = featureColours.get(newf[i]);
504 if (fc != null && fc instanceof GraduatedColor
505 && ((GraduatedColor) fc).isAutoScale())
507 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
508 ((float[][]) mmrange)[0][1]);
512 if (initOrders || !featureOrder.containsKey(newf[i]))
514 int denom = initOrders ? allfeatures.size() : featureOrder.size();
515 // new unordered feature - compute persistent ordering at head of
516 // existing features.
517 setOrder(newf[i], i / (float) denom);
519 // set order from newly found feature from persisted ordering.
520 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
523 // only sort if we need to
524 sort = sort || sortOrder[i] > sortOrder[i + 1];
528 if (iSize > 1 && sort)
530 jalview.util.QuickSort.sort(sortOrder, newf);
533 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
537 * get a feature style object for the given type string. Creates a
538 * java.awt.Color for a featureType with no existing colourscheme. TODO:
539 * replace return type with object implementing standard abstract colour/style
543 * @return java.awt.Color or GraduatedColor
546 public Object getFeatureStyle(String featureType)
548 Object fc = featureColours.get(featureType);
551 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
552 Color col = ucs.createColourFromName(featureType);
553 featureColours.put(featureType, fc = col);
559 * return a nominal colour for this feature
562 * @return standard color, or maximum colour for graduated colourscheme
564 public Color getColour(String featureType)
566 Object fc = getFeatureStyle(featureType);
568 if (fc instanceof Color)
574 if (fc instanceof GraduatedColor)
576 return ((GraduatedColor) fc).getMaxColor();
579 throw new Error("Implementation Error: Unrecognised render object "
580 + fc.getClass() + " for features of type " + featureType);
584 * calculate the render colour for a specific feature using current feature
588 * @return render colour for the given feature
590 public Color getColour(SequenceFeature feature)
592 Object fc = getFeatureStyle(feature.getType());
593 if (fc instanceof Color)
599 if (fc instanceof GraduatedColor)
601 return ((GraduatedColor) fc).findColor(feature);
604 throw new Error("Implementation Error: Unrecognised render object "
605 + fc.getClass() + " for features of type " + feature.getType());
608 protected boolean showFeature(SequenceFeature sequenceFeature)
610 Object fc = getFeatureStyle(sequenceFeature.type);
611 if (fc instanceof GraduatedColor)
613 return ((GraduatedColor) fc).isColored(sequenceFeature);
621 protected boolean showFeatureOfType(String type)
623 return av.getFeaturesDisplayed().isVisible(type);
627 public void setColour(String featureType, Object col)
630 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
631 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
632 // Object c = featureColours.get(featureType);
633 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
634 // !((GraduatedColor)c).getMaxColor().equals(_col)))
636 featureColours.put(featureType, col);
640 public void setTransparency(float value)
642 transparency = value;
645 public float getTransparency()
650 Map featureOrder = null;
653 * analogous to colour - store a normalized ordering for all feature types in
654 * this rendering context.
657 * Feature type string
659 * normalized priority - 0 means always appears on top, 1 means
662 public float setOrder(String type, float position)
664 if (featureOrder == null)
666 featureOrder = new Hashtable();
668 featureOrder.put(type, new Float(position));
673 * get the global priority (0 (top) to 1 (bottom))
676 * @return [0,1] or -1 for a type without a priority
678 public float getOrder(String type)
680 if (featureOrder != null)
682 if (featureOrder.containsKey(type))
684 return ((Float) featureOrder.get(type)).floatValue();
691 public Map<String, Object> getFeatureColours()
693 return featureColours;
697 * Replace current ordering with new ordering
700 * { String(Type), Colour(Type), Boolean(Displayed) }
701 * @return true if any visible features have been reordered, else false
703 public boolean setFeaturePriority(Object[][] data)
705 return setFeaturePriority(data, true);
709 * Sets the priority order for features
712 * { String(Type), Colour(Type), Boolean(Displayed) }
714 * when true current featureDisplay list will be cleared
715 * @return true if any visible features have been reordered or recoloured,
716 * else false (i.e. no need to repaint)
718 public boolean setFeaturePriority(Object[][] data, boolean visibleNew)
721 * note visible feature ordering and colours before update
723 List<String> visibleFeatures = getDisplayedFeatureTypes();
724 Map<String, Object> visibleColours = new HashMap<String, Object>(
725 getFeatureColours());
727 FeaturesDisplayedI av_featuresdisplayed = null;
730 if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
732 av.getFeaturesDisplayed().clear();
736 av.setFeaturesDisplayed(av_featuresdisplayed = new FeaturesDisplayed());
741 av_featuresdisplayed = av.getFeaturesDisplayed();
747 // The feature table will display high priority
748 // features at the top, but these are the ones
749 // we need to render last, so invert the data
750 renderOrder = new String[data.length];
754 for (int i = 0; i < data.length; i++)
756 String type = data[i][0].toString();
757 setColour(type, data[i][1]); // todo : typesafety - feature color
759 if (((Boolean) data[i][2]).booleanValue())
761 av_featuresdisplayed.setVisible(type);
764 renderOrder[data.length - i - 1] = type;
769 * get the new visible ordering and return true if it has changed
770 * order or any colour has changed
772 List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
773 if (!visibleFeatures.equals(reorderedVisibleFeatures))
776 * the list of ordered visible features has changed
782 * return true if any feature colour has changed
784 for (String feature : visibleFeatures)
786 if (visibleColours.get(feature) != getFeatureStyle(feature))
796 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
798 public void addPropertyChangeListener(PropertyChangeListener listener)
800 changeSupport.addPropertyChangeListener(listener);
805 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
807 public void removePropertyChangeListener(PropertyChangeListener listener)
809 changeSupport.removePropertyChangeListener(listener);
812 public Set getAllFeatureColours()
814 return featureColours.keySet();
817 public void clearRenderOrder()
822 public boolean hasRenderOrder()
824 return renderOrder != null;
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]) : Arrays
847 .asList(featureGroups.keySet().toArray(new String[0]));
850 public boolean checkGroupVisibility(String group, boolean newGroupsVisible)
852 if (featureGroups == null)
854 // then an exception happens next..
856 if (featureGroups.containsKey(group))
858 return featureGroups.get(group).booleanValue();
860 if (newGroupsVisible)
862 featureGroups.put(group, new Boolean(true));
869 * get visible or invisible groups
872 * true to return visible groups, false to return hidden ones.
873 * @return list of groups
876 public List getGroups(boolean visible)
878 if (featureGroups != null)
880 ArrayList gp = new ArrayList();
882 for (Object grp : featureGroups.keySet())
884 Boolean state = featureGroups.get(grp);
885 if (state.booleanValue() == visible)
896 public void setGroupVisibility(String group, boolean visible)
898 featureGroups.put(group, new Boolean(visible));
902 public void setGroupVisibility(List<String> toset, boolean visible)
904 if (toset != null && toset.size() > 0 && featureGroups != null)
906 boolean rdrw = false;
907 for (String gst : toset)
909 Boolean st = featureGroups.get(gst);
910 featureGroups.put(gst, new Boolean(visible));
913 rdrw = rdrw || (visible != st.booleanValue());
918 // set local flag indicating redraw needed ?
924 public Hashtable getDisplayedFeatureCols()
926 Hashtable fcols = new Hashtable();
927 if (getViewport().getFeaturesDisplayed() == null)
931 Iterator<String> en = getViewport().getFeaturesDisplayed()
932 .getVisibleFeatures();
935 String col = en.next();
936 fcols.put(col, getColour(col));
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<String>();
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<String>();
977 boolean valid = false;
978 for (String gp : getFeatureGroups())
980 if (checkGroupVisibility(gp, false))
991 // gps = new String[_gps.size()];
992 // _gps.toArray(gps);