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.GraduatedColor;
31 import jalview.viewmodel.AlignmentViewport;
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, Object> featureColours = new ConcurrentHashMap<String, Object>();
57 protected Map<String, Boolean> featureGroups = new ConcurrentHashMap<String, Boolean>();
59 protected Object currentColour;
62 * feature types in ordering of rendering, where last means on top
64 protected String[] renderOrder;
66 protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
69 protected AlignmentViewport av;
72 * map holds per feature type, {{min, max}, {min, max}} feature score
73 * values for positional and non-positional features respectively
75 private Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
78 public AlignViewportI getViewport()
83 public FeatureRendererSettings getSettings()
85 return new FeatureRendererSettings(this);
88 public void transferSettings(FeatureRendererSettings fr)
90 this.renderOrder = fr.renderOrder;
91 this.featureGroups = fr.featureGroups;
92 this.featureColours = fr.featureColours;
93 this.transparency = fr.transparency;
94 this.featureOrder = fr.featureOrder;
98 * update from another feature renderer
103 public void transferSettings(jalview.api.FeatureRenderer _fr)
105 FeatureRenderer fr = (FeatureRenderer) _fr;
106 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
107 this.renderOrder = frs.renderOrder;
108 this.featureGroups = frs.featureGroups;
109 this.featureColours = frs.featureColours;
110 this.transparency = frs.transparency;
111 this.featureOrder = frs.featureOrder;
112 if (av != null && av != fr.getViewport())
114 // copy over the displayed feature settings
115 if (_fr.getFeaturesDisplayed() != null)
117 FeaturesDisplayedI fd = getFeaturesDisplayed();
120 setFeaturesDisplayedFrom(_fr.getFeaturesDisplayed());
127 java.util.Iterator<String> fdisp = _fr.getFeaturesDisplayed()
128 .getVisibleFeatures();
129 while (fdisp.hasNext())
131 fd.setVisible(fdisp.next());
139 public void setFeaturesDisplayedFrom(FeaturesDisplayedI featuresDisplayed)
141 av.setFeaturesDisplayed(new FeaturesDisplayed(featuresDisplayed));
145 public void setVisible(String featureType)
147 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
150 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
152 if (!fdi.isRegistered(featureType))
154 pushFeatureType(Arrays.asList(new String[] { featureType }));
156 fdi.setVisible(featureType);
160 public void setAllVisible(List<String> featureTypes)
162 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
165 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
167 List<String> nft = new ArrayList<String>();
168 for (String featureType : featureTypes)
170 if (!fdi.isRegistered(featureType))
172 nft.add(featureType);
177 pushFeatureType(nft);
179 fdi.setAllVisible(featureTypes);
183 * push a set of new types onto the render order stack. Note - this is a
184 * direct mechanism rather than the one employed in updateRenderOrder
188 private void pushFeatureType(List<String> types)
191 int ts = types.size();
192 String neworder[] = new String[(renderOrder == null ? 0
193 : renderOrder.length) + ts];
194 types.toArray(neworder);
195 if (renderOrder != null)
197 System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
198 System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
200 renderOrder = neworder;
203 public Map<String, float[][]> getMinMax()
209 * normalise a score against the max/min bounds for the feature type.
211 * @param sequenceFeature
212 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
215 protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
217 float[] mm = minmax.get(sequenceFeature.type)[0];
218 final byte[] r = new byte[] { 0, (byte) 255 };
221 if (r[0] != 0 || mm[0] < 0.0)
224 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
228 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
234 boolean newFeatureAdded = false;
236 boolean findingFeatures = false;
238 protected boolean updateFeatures()
240 if (av.getFeaturesDisplayed() == null || renderOrder == null
244 if (av.getFeaturesDisplayed().getVisibleFeatureCount() < 1)
249 // TODO: decide if we should check for the visible feature count first
254 * search the alignment for all new features, give them a colour and display
255 * them. Then fires a PropertyChangeEvent on the changeSupport object.
258 protected void findAllFeatures()
260 synchronized (firing)
262 if (firing.equals(Boolean.FALSE))
264 firing = Boolean.TRUE;
265 findAllFeatures(true); // add all new features as visible
266 changeSupport.firePropertyChange("changeSupport", null, null);
267 firing = Boolean.FALSE;
273 public List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
275 ArrayList<SequenceFeature> tmp = new ArrayList<SequenceFeature>();
276 SequenceFeature[] features = sequence.getSequenceFeatures();
278 if (features != null)
280 for (int i = 0; i < features.length; i++)
282 if (!av.areFeaturesDisplayed()
283 || !av.getFeaturesDisplayed().isVisible(
284 features[i].getType()))
289 if (features[i].featureGroup != null
290 && featureGroups != null
291 && featureGroups.containsKey(features[i].featureGroup)
292 && !featureGroups.get(features[i].featureGroup)
298 if ((features[i].getBegin() <= res)
299 && (features[i].getEnd() >= res))
301 tmp.add(features[i]);
309 * Searches alignment for all features and updates colours
311 * @param newMadeVisible
312 * if true newly added feature types will be rendered immediatly
313 * TODO: check to see if this method should actually be proxied so
314 * repaint events can be propagated by the renderer code
317 public synchronized void findAllFeatures(boolean newMadeVisible)
319 newFeatureAdded = false;
323 newFeatureAdded = true;
327 findingFeatures = true;
328 if (av.getFeaturesDisplayed() == null)
330 av.setFeaturesDisplayed(new FeaturesDisplayed());
332 FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
334 ArrayList<String> allfeatures = new ArrayList<String>();
335 ArrayList<String> oldfeatures = new ArrayList<String>();
336 if (renderOrder != null)
338 for (int i = 0; i < renderOrder.length; i++)
340 if (renderOrder[i] != null)
342 oldfeatures.add(renderOrder[i]);
348 minmax = new Hashtable<String, float[][]>();
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 if (!featuresDisplayed.isRegistered(features[index].getType()))
366 String fgrp = features[index].getFeatureGroup();
369 Boolean groupDisplayed = featureGroups.get(fgrp);
370 if (groupDisplayed == null)
372 groupDisplayed = Boolean.valueOf(newMadeVisible);
373 featureGroups.put(fgrp, groupDisplayed);
375 if (!groupDisplayed.booleanValue())
381 if (!(features[index].begin == 0 && features[index].end == 0))
383 // If beginning and end are 0, the feature is for the whole sequence
384 // and we don't want to render the feature in the normal way
387 && !oldfeatures.contains(features[index].getType()))
389 // this is a new feature type on the alignment. Mark it for
391 featuresDisplayed.setVisible(features[index].getType());
392 setOrder(features[index].getType(), 0);
396 if (!allfeatures.contains(features[index].getType()))
398 allfeatures.add(features[index].getType());
400 if (!Float.isNaN(features[index].score))
402 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
403 float[][] mm = minmax.get(features[index].getType());
406 mm = new float[][] { null, null };
407 minmax.put(features[index].getType(), mm);
409 if (mm[nonpos] == null)
411 mm[nonpos] = new float[] { features[index].score,
412 features[index].score };
417 if (mm[nonpos][0] > features[index].score)
419 mm[nonpos][0] = features[index].score;
421 if (mm[nonpos][1] < features[index].score)
423 mm[nonpos][1] = features[index].score;
430 updateRenderOrder(allfeatures);
431 findingFeatures = false;
434 protected Boolean firing = Boolean.FALSE;
437 * replaces the current renderOrder with the unordered features in
438 * allfeatures. The ordering of any types in both renderOrder and allfeatures
439 * is preserved, and all new feature types are rendered on top of the existing
440 * types, in the order given by getOrder or the order given in allFeatures.
441 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
442 * eliminate the float storage for computing/recalling the persistent ordering
443 * New Cability: updates min/max for colourscheme range if its dynamic
447 private void updateRenderOrder(List<String> allFeatures)
449 List<String> allfeatures = new ArrayList<String>(allFeatures);
450 String[] oldRender = renderOrder;
451 renderOrder = new String[allfeatures.size()];
452 Object mmrange, fc = null;
453 boolean initOrders = (featureOrder == null);
455 if (oldRender != null && oldRender.length > 0)
457 for (int j = 0; j < oldRender.length; j++)
459 if (oldRender[j] != null)
463 setOrder(oldRender[j], (1 - (1 + (float) j) / oldRender.length));
465 if (allfeatures.contains(oldRender[j]))
467 renderOrder[opos++] = oldRender[j]; // existing features always
468 // appear below new features
469 allfeatures.remove(oldRender[j]);
472 mmrange = minmax.get(oldRender[j]);
475 fc = featureColours.get(oldRender[j]);
476 if (fc != null && fc instanceof GraduatedColor
477 && ((GraduatedColor) fc).isAutoScale())
479 ((GraduatedColor) fc).updateBounds(
480 ((float[][]) mmrange)[0][0],
481 ((float[][]) mmrange)[0][1]);
489 if (allfeatures.size() == 0)
491 // no new features - leave order unchanged.
494 int i = allfeatures.size() - 1;
496 boolean sort = false;
497 String[] newf = new String[allfeatures.size()];
498 float[] sortOrder = new float[allfeatures.size()];
499 for (String newfeat : allfeatures)
504 // update from new features minmax if necessary
505 mmrange = minmax.get(newf[i]);
508 fc = featureColours.get(newf[i]);
509 if (fc != null && fc instanceof GraduatedColor
510 && ((GraduatedColor) fc).isAutoScale())
512 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
513 ((float[][]) mmrange)[0][1]);
517 if (initOrders || !featureOrder.containsKey(newf[i]))
519 int denom = initOrders ? allfeatures.size() : featureOrder.size();
520 // new unordered feature - compute persistent ordering at head of
521 // existing features.
522 setOrder(newf[i], i / (float) denom);
524 // set order from newly found feature from persisted ordering.
525 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
528 // only sort if we need to
529 sort = sort || sortOrder[i] > sortOrder[i + 1];
533 if (iSize > 1 && sort)
535 jalview.util.QuickSort.sort(sortOrder, newf);
538 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
542 * get a feature style object for the given type string. Creates a
543 * java.awt.Color for a featureType with no existing colourscheme. TODO:
544 * replace return type with object implementing standard abstract colour/style
548 * @return java.awt.Color or GraduatedColor
551 public Object getFeatureStyle(String featureType)
553 Object fc = featureColours.get(featureType);
556 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
557 Color col = ucs.createColourFromName(featureType);
558 featureColours.put(featureType, fc = col);
564 * calculate the render colour for a specific feature using current feature
568 * @return render colour for the given feature
570 public Color getColour(SequenceFeature feature)
572 Object fc = getFeatureStyle(feature.getType());
573 if (fc instanceof Color)
579 if (fc instanceof GraduatedColor)
581 return ((GraduatedColor) fc).findColor(feature);
584 throw new Error("Implementation Error: Unrecognised render object "
585 + fc.getClass() + " for features of type " + feature.getType());
588 protected boolean showFeature(SequenceFeature sequenceFeature)
590 Object fc = getFeatureStyle(sequenceFeature.type);
591 if (fc instanceof GraduatedColor)
593 return ((GraduatedColor) fc).isColored(sequenceFeature);
601 protected boolean showFeatureOfType(String type)
603 return av.getFeaturesDisplayed().isVisible(type);
607 public void setColour(String featureType, Object col)
610 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
611 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
612 // Object c = featureColours.get(featureType);
613 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
614 // !((GraduatedColor)c).getMaxColor().equals(_col)))
615 if (col instanceof FeatureColourI)
617 if (((FeatureColourI) col).isGraduatedColour())
619 col = new GraduatedColor((FeatureColourI) col);
623 col = ((FeatureColourI) col).getColour();
626 featureColours.put(featureType, col);
629 public void setTransparency(float value)
631 transparency = value;
634 public float getTransparency()
639 Map featureOrder = null;
642 * analogous to colour - store a normalized ordering for all feature types in
643 * this rendering context.
646 * Feature type string
648 * normalized priority - 0 means always appears on top, 1 means
651 public float setOrder(String type, float position)
653 if (featureOrder == null)
655 featureOrder = new Hashtable();
657 featureOrder.put(type, new Float(position));
662 * get the global priority (0 (top) to 1 (bottom))
665 * @return [0,1] or -1 for a type without a priority
667 public float getOrder(String type)
669 if (featureOrder != null)
671 if (featureOrder.containsKey(type))
673 return ((Float) featureOrder.get(type)).floatValue();
680 public Map<String, Object> getFeatureColours()
682 return featureColours;
686 * Replace current ordering with new ordering
689 * { String(Type), Colour(Type), Boolean(Displayed) }
690 * @return true if any visible features have been reordered, else false
692 public boolean setFeaturePriority(Object[][] data)
694 return setFeaturePriority(data, true);
698 * Sets the priority order for features
701 * { String(Type), Colour(Type), Boolean(Displayed) }
703 * when true current featureDisplay list will be cleared
704 * @return true if any visible features have been reordered or recoloured,
705 * else false (i.e. no need to repaint)
707 public boolean setFeaturePriority(Object[][] data, boolean visibleNew)
710 * note visible feature ordering and colours before update
712 List<String> visibleFeatures = getDisplayedFeatureTypes();
713 Map<String, Object> visibleColours = new HashMap<String, Object>(
714 getFeatureColours());
716 FeaturesDisplayedI av_featuresdisplayed = null;
719 if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
721 av.getFeaturesDisplayed().clear();
725 av.setFeaturesDisplayed(av_featuresdisplayed = new FeaturesDisplayed());
730 av_featuresdisplayed = av.getFeaturesDisplayed();
736 // The feature table will display high priority
737 // features at the top, but these are the ones
738 // we need to render last, so invert the data
739 renderOrder = new String[data.length];
743 for (int i = 0; i < data.length; i++)
745 String type = data[i][0].toString();
746 setColour(type, data[i][1]); // todo : typesafety - feature color
748 if (((Boolean) data[i][2]).booleanValue())
750 av_featuresdisplayed.setVisible(type);
753 renderOrder[data.length - i - 1] = type;
758 * get the new visible ordering and return true if it has changed
759 * order or any colour has changed
761 List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
762 if (!visibleFeatures.equals(reorderedVisibleFeatures))
765 * the list of ordered visible features has changed
771 * return true if any feature colour has changed
773 for (String feature : visibleFeatures)
775 if (visibleColours.get(feature) != getFeatureStyle(feature))
785 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
787 public void addPropertyChangeListener(PropertyChangeListener listener)
789 changeSupport.addPropertyChangeListener(listener);
794 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
796 public void removePropertyChangeListener(PropertyChangeListener listener)
798 changeSupport.removePropertyChangeListener(listener);
801 public Set<String> getAllFeatureColours()
803 return featureColours.keySet();
806 public void clearRenderOrder()
811 public boolean hasRenderOrder()
813 return renderOrder != null;
817 * Returns feature types in ordering of rendering, where last means on top
819 public List<String> getRenderOrder()
821 if (renderOrder == null)
823 return Arrays.asList(new String[] {});
825 return Arrays.asList(renderOrder);
828 public int getFeatureGroupsSize()
830 return featureGroups != null ? 0 : featureGroups.size();
834 public List<String> getFeatureGroups()
836 // conflict between applet and desktop - featureGroups returns the map in
837 // the desktop featureRenderer
838 return (featureGroups == null) ? Arrays.asList(new String[0]) : Arrays
839 .asList(featureGroups.keySet().toArray(new String[0]));
842 public boolean checkGroupVisibility(String group, boolean newGroupsVisible)
844 if (featureGroups == null)
846 // then an exception happens next..
848 if (featureGroups.containsKey(group))
850 return featureGroups.get(group).booleanValue();
852 if (newGroupsVisible)
854 featureGroups.put(group, new Boolean(true));
861 * get visible or invisible groups
864 * true to return visible groups, false to return hidden ones.
865 * @return list of groups
868 public List getGroups(boolean visible)
870 if (featureGroups != null)
872 ArrayList gp = new ArrayList();
874 for (Object grp : featureGroups.keySet())
876 Boolean state = featureGroups.get(grp);
877 if (state.booleanValue() == visible)
888 public void setGroupVisibility(String group, boolean visible)
890 featureGroups.put(group, new Boolean(visible));
894 public void setGroupVisibility(List<String> toset, boolean visible)
896 if (toset != null && toset.size() > 0 && featureGroups != null)
898 boolean rdrw = false;
899 for (String gst : toset)
901 Boolean st = featureGroups.get(gst);
902 featureGroups.put(gst, new Boolean(visible));
905 rdrw = rdrw || (visible != st.booleanValue());
910 // set local flag indicating redraw needed ?
916 public Hashtable getDisplayedFeatureCols()
918 Hashtable fcols = new Hashtable();
919 if (getViewport().getFeaturesDisplayed() == null)
923 Iterator<String> en = getViewport().getFeaturesDisplayed()
924 .getVisibleFeatures();
927 String col = en.next();
928 fcols.put(col, featureColours.get(col));
934 public FeaturesDisplayedI getFeaturesDisplayed()
936 return av.getFeaturesDisplayed();
940 * Returns a (possibly empty) list of visible feature types, in render order
944 public List<String> getDisplayedFeatureTypes()
946 List<String> typ = getRenderOrder();
947 List<String> displayed = new ArrayList<String>();
948 FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
949 if (feature_disp != null)
951 synchronized (feature_disp)
953 for (String type : typ)
955 if (feature_disp.isVisible(type))
966 public List<String> getDisplayedFeatureGroups()
968 List<String> _gps = new ArrayList<String>();
969 boolean valid = false;
970 for (String gp : getFeatureGroups())
972 if (checkGroupVisibility(gp, false))
983 // gps = new String[_gps.size()];
984 // _gps.toArray(gps);