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.schemes.UserColourScheme;
32 import jalview.viewmodel.AlignmentViewport;
34 import java.awt.Color;
35 import java.beans.PropertyChangeListener;
36 import java.beans.PropertyChangeSupport;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.HashMap;
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 AlignmentViewport 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 if ((features[i].getBegin() <= res)
293 && (features[i].getEnd() >= res))
295 tmp.add(features[i]);
303 * Searches alignment for all features and updates colours
305 * @param newMadeVisible
306 * if true newly added feature types will be rendered immediatly
307 * TODO: check to see if this method should actually be proxied so
308 * repaint events can be propagated by the renderer code
311 public synchronized void findAllFeatures(boolean newMadeVisible)
313 newFeatureAdded = false;
317 newFeatureAdded = true;
321 findingFeatures = true;
322 if (av.getFeaturesDisplayed() == null)
324 av.setFeaturesDisplayed(new FeaturesDisplayed());
326 FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
328 ArrayList<String> allfeatures = new ArrayList<String>();
329 ArrayList<String> oldfeatures = new ArrayList<String>();
330 if (renderOrder != null)
332 for (int i = 0; i < renderOrder.length; i++)
334 if (renderOrder[i] != null)
336 oldfeatures.add(renderOrder[i]);
342 minmax = new Hashtable<String, float[][]>();
344 AlignmentI alignment = av.getAlignment();
345 for (int i = 0; i < alignment.getHeight(); i++)
347 SequenceI asq = alignment.getSequenceAt(i);
348 SequenceFeature[] features = asq.getSequenceFeatures();
350 if (features == null)
356 while (index < features.length)
358 if (!featuresDisplayed.isRegistered(features[index].getType()))
360 String fgrp = features[index].getFeatureGroup();
363 Boolean groupDisplayed = featureGroups.get(fgrp);
364 if (groupDisplayed == null)
366 groupDisplayed = Boolean.valueOf(newMadeVisible);
367 featureGroups.put(fgrp, groupDisplayed);
369 if (!groupDisplayed.booleanValue())
375 if (!(features[index].begin == 0 && features[index].end == 0))
377 // If beginning and end are 0, the feature is for the whole sequence
378 // and we don't want to render the feature in the normal way
381 && !oldfeatures.contains(features[index].getType()))
383 // this is a new feature type on the alignment. Mark it for
385 featuresDisplayed.setVisible(features[index].getType());
386 setOrder(features[index].getType(), 0);
390 if (!allfeatures.contains(features[index].getType()))
392 allfeatures.add(features[index].getType());
394 if (!Float.isNaN(features[index].score))
396 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
397 float[][] mm = minmax.get(features[index].getType());
400 mm = new float[][] { null, null };
401 minmax.put(features[index].getType(), mm);
403 if (mm[nonpos] == null)
405 mm[nonpos] = new float[] { features[index].score,
406 features[index].score };
411 if (mm[nonpos][0] > features[index].score)
413 mm[nonpos][0] = features[index].score;
415 if (mm[nonpos][1] < features[index].score)
417 mm[nonpos][1] = features[index].score;
424 updateRenderOrder(allfeatures);
425 findingFeatures = false;
428 protected Boolean firing = Boolean.FALSE;
431 * replaces the current renderOrder with the unordered features in
432 * allfeatures. The ordering of any types in both renderOrder and allfeatures
433 * is preserved, and all new feature types are rendered on top of the existing
434 * types, in the order given by getOrder or the order given in allFeatures.
435 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
436 * eliminate the float storage for computing/recalling the persistent ordering
437 * New Cability: updates min/max for colourscheme range if its dynamic
441 private void updateRenderOrder(List<String> allFeatures)
443 List<String> allfeatures = new ArrayList<String>(allFeatures);
444 String[] oldRender = renderOrder;
445 renderOrder = new String[allfeatures.size()];
446 boolean initOrders = (featureOrder == null);
448 if (oldRender != null && oldRender.length > 0)
450 for (int j = 0; j < oldRender.length; j++)
452 if (oldRender[j] != null)
456 setOrder(oldRender[j], (1 - (1 + (float) j) / oldRender.length));
458 if (allfeatures.contains(oldRender[j]))
460 renderOrder[opos++] = oldRender[j]; // existing features always
461 // appear below new features
462 allfeatures.remove(oldRender[j]);
465 float[][] mmrange = minmax.get(oldRender[j]);
468 FeatureColourI fc = featureColours.get(oldRender[j]);
469 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
471 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
479 if (allfeatures.size() == 0)
481 // no new features - leave order unchanged.
484 int i = allfeatures.size() - 1;
486 boolean sort = false;
487 String[] newf = new String[allfeatures.size()];
488 float[] sortOrder = new float[allfeatures.size()];
489 for (String newfeat : allfeatures)
494 // update from new features minmax if necessary
495 float[][] mmrange = minmax.get(newf[i]);
498 FeatureColourI fc = featureColours.get(newf[i]);
499 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
501 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
505 if (initOrders || !featureOrder.containsKey(newf[i]))
507 int denom = initOrders ? allfeatures.size() : featureOrder.size();
508 // new unordered feature - compute persistent ordering at head of
509 // existing features.
510 setOrder(newf[i], i / (float) denom);
512 // set order from newly found feature from persisted ordering.
513 sortOrder[i] = 2 - featureOrder.get(newf[i]).floatValue();
516 // only sort if we need to
517 sort = sort || sortOrder[i] > sortOrder[i + 1];
521 if (iSize > 1 && sort)
523 jalview.util.QuickSort.sort(sortOrder, newf);
526 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
530 * get a feature style object for the given type string. Creates a
531 * java.awt.Color for a featureType with no existing colourscheme.
537 public FeatureColourI getFeatureStyle(String featureType)
539 FeatureColourI fc = featureColours.get(featureType);
542 Color col = UserColourScheme.createColourFromName(featureType);
543 fc = new FeatureColour(col);
544 featureColours.put(featureType, fc);
550 * calculate the render colour for a specific feature using current feature
554 * @return render colour for the given feature
556 public Color getColour(SequenceFeature feature)
558 FeatureColourI fc = getFeatureStyle(feature.getType());
559 return fc.getColor(feature);
562 protected boolean showFeature(SequenceFeature sequenceFeature)
564 FeatureColourI fc = getFeatureStyle(sequenceFeature.type);
565 return fc.isColored(sequenceFeature);
568 protected boolean showFeatureOfType(String type)
570 return av.getFeaturesDisplayed().isVisible(type);
574 public void setColour(String featureType, FeatureColourI col)
576 featureColours.put(featureType, col);
579 public void setTransparency(float value)
581 transparency = value;
584 public float getTransparency()
590 * analogous to colour - store a normalized ordering for all feature types in
591 * this rendering context.
594 * Feature type string
596 * normalized priority - 0 means always appears on top, 1 means
599 public float setOrder(String type, float position)
601 if (featureOrder == null)
603 featureOrder = new Hashtable<String, Float>();
605 featureOrder.put(type, new Float(position));
610 * get the global priority (0 (top) to 1 (bottom))
613 * @return [0,1] or -1 for a type without a priority
615 public float getOrder(String type)
617 if (featureOrder != null)
619 if (featureOrder.containsKey(type))
621 return featureOrder.get(type).floatValue();
628 public Map<String, FeatureColourI> getFeatureColours()
630 return featureColours;
634 * Replace current ordering with new ordering
637 * { String(Type), Colour(Type), Boolean(Displayed) }
638 * @return true if any visible features have been reordered, else false
640 public boolean setFeaturePriority(Object[][] data)
642 return setFeaturePriority(data, true);
646 * Sets the priority order for features
649 * { String(Type), Colour(Type), Boolean(Displayed) }
651 * when true current featureDisplay list will be cleared
652 * @return true if any visible features have been reordered or recoloured,
653 * else false (i.e. no need to repaint)
655 public boolean setFeaturePriority(Object[][] data, boolean visibleNew)
658 * note visible feature ordering and colours before update
660 List<String> visibleFeatures = getDisplayedFeatureTypes();
661 Map<String, FeatureColourI> visibleColours = new HashMap<String, FeatureColourI>(
662 getFeatureColours());
664 FeaturesDisplayedI av_featuresdisplayed = null;
667 if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
669 av.getFeaturesDisplayed().clear();
673 av.setFeaturesDisplayed(av_featuresdisplayed = new FeaturesDisplayed());
678 av_featuresdisplayed = av.getFeaturesDisplayed();
684 // The feature table will display high priority
685 // features at the top, but these are the ones
686 // we need to render last, so invert the data
687 renderOrder = new String[data.length];
691 for (int i = 0; i < data.length; i++)
693 String type = data[i][0].toString();
694 setColour(type, (FeatureColourI) data[i][1]);
695 if (((Boolean) data[i][2]).booleanValue())
697 av_featuresdisplayed.setVisible(type);
700 renderOrder[data.length - i - 1] = type;
705 * get the new visible ordering and return true if it has changed
706 * order or any colour has changed
708 List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
709 if (!visibleFeatures.equals(reorderedVisibleFeatures))
712 * the list of ordered visible features has changed
718 * return true if any feature colour has changed
720 for (String feature : visibleFeatures)
722 if (visibleColours.get(feature) != getFeatureStyle(feature))
732 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
734 public void addPropertyChangeListener(PropertyChangeListener listener)
736 changeSupport.addPropertyChangeListener(listener);
741 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
743 public void removePropertyChangeListener(PropertyChangeListener listener)
745 changeSupport.removePropertyChangeListener(listener);
748 public Set<String> getAllFeatureColours()
750 return featureColours.keySet();
753 public void clearRenderOrder()
758 public boolean hasRenderOrder()
760 return renderOrder != null;
764 * Returns feature types in ordering of rendering, where last means on top
766 public List<String> getRenderOrder()
768 if (renderOrder == null)
770 return Arrays.asList(new String[] {});
772 return Arrays.asList(renderOrder);
775 public int getFeatureGroupsSize()
777 return featureGroups != null ? 0 : featureGroups.size();
781 public List<String> getFeatureGroups()
783 // conflict between applet and desktop - featureGroups returns the map in
784 // the desktop featureRenderer
785 return (featureGroups == null) ? Arrays.asList(new String[0]) : Arrays
786 .asList(featureGroups.keySet().toArray(new String[0]));
789 public boolean checkGroupVisibility(String group, boolean newGroupsVisible)
791 if (featureGroups == null)
793 // then an exception happens next..
795 if (featureGroups.containsKey(group))
797 return featureGroups.get(group).booleanValue();
799 if (newGroupsVisible)
801 featureGroups.put(group, new Boolean(true));
808 * get visible or invisible groups
811 * true to return visible groups, false to return hidden ones.
812 * @return list of groups
815 public List getGroups(boolean visible)
817 if (featureGroups != null)
819 List<String> gp = new ArrayList<String>();
821 for (String grp : featureGroups.keySet())
823 Boolean state = featureGroups.get(grp);
824 if (state.booleanValue() == visible)
835 public void setGroupVisibility(String group, boolean visible)
837 featureGroups.put(group, new Boolean(visible));
841 public void setGroupVisibility(List<String> toset, boolean visible)
843 if (toset != null && toset.size() > 0 && featureGroups != null)
845 boolean rdrw = false;
846 for (String gst : toset)
848 Boolean st = featureGroups.get(gst);
849 featureGroups.put(gst, new Boolean(visible));
852 rdrw = rdrw || (visible != st.booleanValue());
857 // set local flag indicating redraw needed ?
863 public Map<String, FeatureColourI> getDisplayedFeatureCols()
865 Map<String, FeatureColourI> fcols = new Hashtable<String, FeatureColourI>();
866 if (getViewport().getFeaturesDisplayed() == null)
870 Iterator<String> features = getViewport().getFeaturesDisplayed()
871 .getVisibleFeatures();
872 while (features.hasNext())
874 String feature = features.next();
875 fcols.put(feature, getFeatureStyle(feature));
881 public FeaturesDisplayedI getFeaturesDisplayed()
883 return av.getFeaturesDisplayed();
887 * Returns a (possibly empty) list of visible feature types, in render order
891 public List<String> getDisplayedFeatureTypes()
893 List<String> typ = getRenderOrder();
894 List<String> displayed = new ArrayList<String>();
895 FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
896 if (feature_disp != null)
898 synchronized (feature_disp)
900 for (String type : typ)
902 if (feature_disp.isVisible(type))
913 public List<String> getDisplayedFeatureGroups()
915 List<String> _gps = new ArrayList<String>();
916 boolean valid = false;
917 for (String gp : getFeatureGroups())
919 if (checkGroupVisibility(gp, false))
930 // gps = new String[_gps.size()];
931 // _gps.toArray(gps);