1 package jalview.viewmodel.seqfeatures;
3 import jalview.api.AlignViewportI;
4 import jalview.api.FeaturesDisplayedI;
5 import jalview.datamodel.AlignmentI;
6 import jalview.datamodel.SequenceFeature;
7 import jalview.datamodel.SequenceI;
8 import jalview.renderer.seqfeatures.FeatureRenderer;
9 import jalview.schemes.GraduatedColor;
10 import jalview.viewmodel.AlignmentViewport;
12 import java.awt.Color;
13 import java.beans.PropertyChangeListener;
14 import java.beans.PropertyChangeSupport;
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.Hashtable;
18 import java.util.Iterator;
19 import java.util.List;
22 import java.util.concurrent.ConcurrentHashMap;
24 public abstract class FeatureRendererModel implements
25 jalview.api.FeatureRenderer
29 * global transparency for feature
31 protected float transparency = 1.0f;
33 protected Map<String, Object> featureColours = new ConcurrentHashMap<String, Object>();
35 protected Map<String, Boolean> featureGroups = new ConcurrentHashMap<String, Boolean>();
37 protected Object currentColour;
39 protected String[] renderOrder;
41 protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
44 protected AlignmentViewport av;
46 public AlignViewportI getViewport()
51 public FeatureRendererSettings getSettings()
53 return new FeatureRendererSettings(this);
56 public void transferSettings(FeatureRendererSettings fr)
58 this.renderOrder = fr.renderOrder;
59 this.featureGroups = fr.featureGroups;
60 this.featureColours = fr.featureColours;
61 this.transparency = fr.transparency;
62 this.featureOrder = fr.featureOrder;
66 * update from another feature renderer
71 public void transferSettings(jalview.api.FeatureRenderer _fr)
73 FeatureRenderer fr = (FeatureRenderer) _fr;
74 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
75 this.renderOrder = frs.renderOrder;
76 this.featureGroups = frs.featureGroups;
77 this.featureColours = frs.featureColours;
78 this.transparency = frs.transparency;
79 this.featureOrder = frs.featureOrder;
80 if (av != null && av != fr.getViewport())
82 // copy over the displayed feature settings
83 if (_fr.getFeaturesDisplayed() != null)
85 FeaturesDisplayedI fd = getFeaturesDisplayed();
88 setFeaturesDisplayedFrom(_fr.getFeaturesDisplayed());
95 java.util.Iterator<String> fdisp = _fr.getFeaturesDisplayed()
96 .getVisibleFeatures();
97 while (fdisp.hasNext())
99 fd.setVisible(fdisp.next());
107 public void setFeaturesDisplayedFrom(FeaturesDisplayedI featuresDisplayed)
109 av.setFeaturesDisplayed(new FeaturesDisplayed(featuresDisplayed));
113 public void setVisible(String featureType)
115 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
118 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
120 if (!fdi.isRegistered(featureType))
122 pushFeatureType(Arrays.asList(new String[]
125 fdi.setVisible(featureType);
129 public void setAllVisible(List<String> featureTypes)
131 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
134 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
136 List<String> nft = new ArrayList<String>();
137 for (String featureType : featureTypes)
139 if (!fdi.isRegistered(featureType))
141 nft.add(featureType);
146 pushFeatureType(nft);
148 fdi.setAllVisible(featureTypes);
152 * push a set of new types onto the render order stack. Note - this is a
153 * direct mechanism rather than the one employed in updateRenderOrder
157 private void pushFeatureType(List<String> types)
160 int ts = types.size();
161 String neworder[] = new String[(renderOrder == null ? 0
162 : renderOrder.length) + ts];
163 types.toArray(neworder);
164 if (renderOrder != null)
166 System.arraycopy(neworder,0,neworder,renderOrder.length,ts);
167 System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
169 renderOrder = neworder;
172 protected Hashtable minmax = new Hashtable();
174 public Hashtable getMinMax()
180 * normalise a score against the max/min bounds for the feature type.
182 * @param sequenceFeature
183 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
186 protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
188 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
189 final byte[] r = new byte[]
193 if (r[0] != 0 || mm[0] < 0.0)
196 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
200 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
206 boolean newFeatureAdded = false;
208 boolean findingFeatures = false;
210 protected boolean updateFeatures()
212 if (av.getFeaturesDisplayed() == null || renderOrder == null
216 if (av.getFeaturesDisplayed().getVisibleFeatureCount() < 1)
221 // TODO: decide if we should check for the visible feature count first
226 * search the alignment for all new features, give them a colour and display
227 * them. Then fires a PropertyChangeEvent on the changeSupport object.
230 protected void findAllFeatures()
232 synchronized (firing)
234 if (firing.equals(Boolean.FALSE))
236 firing = Boolean.TRUE;
237 findAllFeatures(true); // add all new features as visible
238 changeSupport.firePropertyChange("changeSupport", null, null);
239 firing = Boolean.FALSE;
245 public List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
247 ArrayList<SequenceFeature> tmp = new ArrayList<SequenceFeature>();
248 SequenceFeature[] features = sequence.getSequenceFeatures();
250 if (features != null)
252 for (int i = 0; i < features.length; i++)
254 if (!av.areFeaturesDisplayed()
255 || !av.getFeaturesDisplayed().isVisible(
256 features[i].getType()))
261 if (features[i].featureGroup != null
262 && featureGroups != null
263 && featureGroups.containsKey(features[i].featureGroup)
264 && !featureGroups.get(features[i].featureGroup)
270 if ((features[i].getBegin() <= res)
271 && (features[i].getEnd() >= res))
273 tmp.add(features[i]);
281 * Searches alignment for all features and updates colours
283 * @param newMadeVisible
284 * if true newly added feature types will be rendered immediatly
285 * TODO: check to see if this method should actually be proxied so
286 * repaint events can be propagated by the renderer code
289 public synchronized void findAllFeatures(boolean newMadeVisible)
291 newFeatureAdded = false;
295 newFeatureAdded = true;
299 findingFeatures = true;
300 if (av.getFeaturesDisplayed() == null)
302 av.setFeaturesDisplayed(new FeaturesDisplayed());
304 FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
306 ArrayList<String> allfeatures = new ArrayList<String>();
307 ArrayList<String> oldfeatures = new ArrayList<String>();
308 if (renderOrder != null)
310 for (int i = 0; i < renderOrder.length; i++)
312 if (renderOrder[i] != null)
314 oldfeatures.add(renderOrder[i]);
320 minmax = new Hashtable();
322 AlignmentI alignment = av.getAlignment();
323 for (int i = 0; i < alignment.getHeight(); i++)
325 SequenceI asq = alignment.getSequenceAt(i);
326 SequenceFeature[] features = asq.getSequenceFeatures();
328 if (features == null)
334 while (index < features.length)
336 if (!featuresDisplayed.isRegistered(features[index].getType()))
338 String fgrp = features[index].getFeatureGroup();
341 Boolean groupDisplayed = featureGroups.get(fgrp);
342 if (groupDisplayed == null)
344 groupDisplayed = Boolean.valueOf(newMadeVisible);
345 featureGroups.put(fgrp, groupDisplayed);
347 if (!groupDisplayed.booleanValue())
353 if (!(features[index].begin == 0 && features[index].end == 0))
355 // If beginning and end are 0, the feature is for the whole sequence
356 // and we don't want to render the feature in the normal way
359 && !oldfeatures.contains(features[index].getType()))
361 // this is a new feature type on the alignment. Mark it for
363 featuresDisplayed.setVisible(features[index].getType());
364 setOrder(features[index].getType(), 0);
368 if (!allfeatures.contains(features[index].getType()))
370 allfeatures.add(features[index].getType());
372 if (features[index].score != Float.NaN)
374 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
375 float[][] mm = (float[][]) minmax.get(features[index].getType());
380 minmax.put(features[index].getType(), mm);
382 if (mm[nonpos] == null)
384 mm[nonpos] = new float[]
385 { features[index].score, features[index].score };
390 if (mm[nonpos][0] > features[index].score)
392 mm[nonpos][0] = features[index].score;
394 if (mm[nonpos][1] < features[index].score)
396 mm[nonpos][1] = features[index].score;
403 updateRenderOrder(allfeatures);
404 findingFeatures = false;
407 protected Boolean firing = Boolean.FALSE;
410 * replaces the current renderOrder with the unordered features in
411 * allfeatures. The ordering of any types in both renderOrder and allfeatures
412 * is preserved, and all new feature types are rendered on top of the existing
413 * types, in the order given by getOrder or the order given in allFeatures.
414 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
415 * eliminate the float storage for computing/recalling the persistent ordering
416 * New Cability: updates min/max for colourscheme range if its dynamic
420 private void updateRenderOrder(List<String> allFeatures)
422 List<String> allfeatures = new ArrayList<String>(allFeatures);
423 String[] oldRender = renderOrder;
424 renderOrder = new String[allfeatures.size()];
425 Object mmrange, fc = null;
426 boolean initOrders = (featureOrder == null);
428 if (oldRender != null && oldRender.length > 0)
430 for (int j = 0; j < oldRender.length; j++)
432 if (oldRender[j] != null)
436 setOrder(oldRender[j], (1 - (1 + (float) j)
437 / oldRender.length));
439 if (allfeatures.contains(oldRender[j]))
441 renderOrder[opos++] = oldRender[j]; // existing features always
442 // appear below new features
443 allfeatures.remove(oldRender[j]);
446 mmrange = minmax.get(oldRender[j]);
449 fc = featureColours.get(oldRender[j]);
450 if (fc != null && fc instanceof GraduatedColor
451 && ((GraduatedColor) fc).isAutoScale())
453 ((GraduatedColor) fc).updateBounds(
454 ((float[][]) mmrange)[0][0],
455 ((float[][]) mmrange)[0][1]);
463 if (allfeatures.size() == 0)
465 // no new features - leave order unchanged.
468 int i = allfeatures.size() - 1;
470 boolean sort = false;
471 String[] newf = new String[allfeatures.size()];
472 float[] sortOrder = new float[allfeatures.size()];
473 for (String newfeat : allfeatures)
478 // update from new features minmax if necessary
479 mmrange = minmax.get(newf[i]);
482 fc = featureColours.get(newf[i]);
483 if (fc != null && fc instanceof GraduatedColor
484 && ((GraduatedColor) fc).isAutoScale())
486 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
487 ((float[][]) mmrange)[0][1]);
491 if (initOrders || !featureOrder.containsKey(newf[i]))
493 int denom = initOrders ? allfeatures.size() : featureOrder.size();
494 // new unordered feature - compute persistent ordering at head of
495 // existing features.
496 setOrder(newf[i], i / (float) denom);
498 // set order from newly found feature from persisted ordering.
499 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
502 // only sort if we need to
503 sort = sort || sortOrder[i] > sortOrder[i + 1];
507 if (iSize > 1 && sort)
509 jalview.util.QuickSort.sort(sortOrder, newf);
512 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
516 * get a feature style object for the given type string. Creates a
517 * java.awt.Color for a featureType with no existing colourscheme. TODO:
518 * replace return type with object implementing standard abstract colour/style
522 * @return java.awt.Color or GraduatedColor
524 public Object getFeatureStyle(String featureType)
526 Object fc = featureColours.get(featureType);
529 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
530 Color col = ucs.createColourFromName(featureType);
531 featureColours.put(featureType, fc = col);
537 * return a nominal colour for this feature
540 * @return standard color, or maximum colour for graduated colourscheme
542 public Color getColour(String featureType)
544 Object fc = getFeatureStyle(featureType);
546 if (fc instanceof Color)
552 if (fc instanceof GraduatedColor)
554 return ((GraduatedColor) fc).getMaxColor();
557 throw new Error("Implementation Error: Unrecognised render object "
558 + fc.getClass() + " for features of type " + featureType);
562 * calculate the render colour for a specific feature using current feature
566 * @return render colour for the given feature
568 public Color getColour(SequenceFeature feature)
570 Object fc = getFeatureStyle(feature.getType());
571 if (fc instanceof Color)
577 if (fc instanceof GraduatedColor)
579 return ((GraduatedColor) fc).findColor(feature);
582 throw new Error("Implementation Error: Unrecognised render object "
583 + fc.getClass() + " for features of type " + feature.getType());
586 protected boolean showFeature(SequenceFeature sequenceFeature)
588 Object fc = getFeatureStyle(sequenceFeature.type);
589 if (fc instanceof GraduatedColor)
591 return ((GraduatedColor) fc).isColored(sequenceFeature);
599 protected boolean showFeatureOfType(String type)
601 return av.getFeaturesDisplayed().isVisible(type);
604 public void setColour(String featureType, Object col)
607 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
608 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
609 // Object c = featureColours.get(featureType);
610 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
611 // !((GraduatedColor)c).getMaxColor().equals(_col)))
613 featureColours.put(featureType, col);
617 public void setTransparency(float value)
619 transparency = value;
622 public float getTransparency()
627 Map featureOrder = null;
630 * analogous to colour - store a normalized ordering for all feature types in
631 * this rendering context.
634 * Feature type string
636 * normalized priority - 0 means always appears on top, 1 means
639 public float setOrder(String type, float position)
641 if (featureOrder == null)
643 featureOrder = new Hashtable();
645 featureOrder.put(type, new Float(position));
650 * get the global priority (0 (top) to 1 (bottom))
653 * @return [0,1] or -1 for a type without a priority
655 public float getOrder(String type)
657 if (featureOrder != null)
659 if (featureOrder.containsKey(type))
661 return ((Float) featureOrder.get(type)).floatValue();
668 public Map<String, Object> getFeatureColours()
670 return new ConcurrentHashMap<String, Object>(featureColours);
674 * Replace current ordering with new ordering
677 * { String(Type), Colour(Type), Boolean(Displayed) }
679 public void setFeaturePriority(Object[][] data)
681 setFeaturePriority(data, true);
687 * { String(Type), Colour(Type), Boolean(Displayed) }
689 * when true current featureDisplay list will be cleared
691 public void setFeaturePriority(Object[][] data, boolean visibleNew)
693 FeaturesDisplayedI av_featuresdisplayed = null;
696 if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
698 av.getFeaturesDisplayed().clear();
702 av.setFeaturesDisplayed(av_featuresdisplayed = new FeaturesDisplayed());
707 av_featuresdisplayed = av.getFeaturesDisplayed();
713 // The feature table will display high priority
714 // features at the top, but theses are the ones
715 // we need to render last, so invert the data
716 renderOrder = new String[data.length];
720 for (int i = 0; i < data.length; i++)
722 String type = data[i][0].toString();
723 setColour(type, data[i][1]); // todo : typesafety - feature color
725 if (((Boolean) data[i][2]).booleanValue())
727 av_featuresdisplayed.setVisible(type);
730 renderOrder[data.length - i - 1] = type;
738 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
740 public void addPropertyChangeListener(PropertyChangeListener listener)
742 changeSupport.addPropertyChangeListener(listener);
747 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
749 public void removePropertyChangeListener(PropertyChangeListener listener)
751 changeSupport.removePropertyChangeListener(listener);
754 public Set getAllFeatureColours()
756 return featureColours.keySet();
759 public void clearRenderOrder()
764 public boolean hasRenderOrder()
766 return renderOrder != null;
769 public List<String> getRenderOrder()
771 if (renderOrder == null)
773 return Arrays.asList(new String[]
776 return Arrays.asList(renderOrder);
779 public int getFeatureGroupsSize()
781 return featureGroups != null ? 0 : featureGroups.size();
785 public List<String> getFeatureGroups()
787 // conflict between applet and desktop - featureGroups returns the map in
788 // the desktop featureRenderer
789 return (featureGroups == null) ? Arrays.asList(new String[0]) : Arrays
790 .asList(featureGroups.keySet().toArray(new String[0]));
793 public boolean checkGroupVisibility(String group, boolean newGroupsVisible)
795 if (featureGroups == null)
797 // then an exception happens next..
799 if (featureGroups.containsKey(group))
801 return featureGroups.get(group).booleanValue();
803 if (newGroupsVisible)
805 featureGroups.put(group, new Boolean(true));
812 * get visible or invisible groups
815 * true to return visible groups, false to return hidden ones.
816 * @return list of groups
819 public List getGroups(boolean visible)
821 if (featureGroups != null)
823 ArrayList gp = new ArrayList();
825 for (Object grp : featureGroups.keySet())
827 Boolean state = featureGroups.get(grp);
828 if (state.booleanValue() == visible)
839 public void setGroupVisibility(String group, boolean visible)
841 featureGroups.put(group, new Boolean(visible));
845 public void setGroupVisibility(List<String> toset, boolean visible)
847 if (toset != null && toset.size() > 0 && featureGroups != null)
849 boolean rdrw = false;
850 for (String gst : toset)
852 Boolean st = featureGroups.get(gst);
853 featureGroups.put(gst, new Boolean(visible));
856 rdrw = rdrw || (visible != st.booleanValue());
861 // set local flag indicating redraw needed ?
867 public Hashtable getDisplayedFeatureCols()
869 Hashtable fcols = new Hashtable();
870 if (getViewport().getFeaturesDisplayed() == null)
874 Iterator<String> en = getViewport().getFeaturesDisplayed()
875 .getVisibleFeatures();
878 String col = en.next();
879 fcols.put(col, getColour(col));
885 public FeaturesDisplayedI getFeaturesDisplayed()
887 return av.getFeaturesDisplayed();
891 public String[] getDisplayedFeatureTypes()
894 typ = getRenderOrder().toArray(new String[0]);
895 FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
896 if (feature_disp != null)
898 synchronized (feature_disp)
900 for (int i = 0; i < typ.length; i++)
902 if (!feature_disp.isVisible(typ[i]))
913 public String[] getDisplayedFeatureGroups()
916 ArrayList<String> _gps = new ArrayList<String>();
917 Iterator en = getFeatureGroups().iterator();
919 boolean valid = false;
922 String gp = (String) en.next();
923 if (checkGroupVisibility(gp, false))
934 gps = new String[_gps.size()];