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.HashSet;
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
48 implements 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 AlignViewportI 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
219 + 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,
271 ArrayList<SequenceFeature> tmp = new ArrayList<SequenceFeature>();
272 SequenceFeature[] features = sequence.getSequenceFeatures();
274 if (features != null)
276 for (int i = 0; i < features.length; i++)
278 if (!av.areFeaturesDisplayed() || !av.getFeaturesDisplayed()
279 .isVisible(features[i].getType()))
284 if (features[i].featureGroup != null && featureGroups != null
285 && featureGroups.containsKey(features[i].featureGroup)
286 && !featureGroups.get(features[i].featureGroup)
292 // check if start/end are at res, and if not a contact feature, that res
293 // lies between start and end
294 if ((features[i].getBegin() == res || features[i].getEnd() == res)
295 || (!features[i].isContactFeature()
296 && (features[i].getBegin() < res)
297 && (features[i].getEnd() >= res)))
299 tmp.add(features[i]);
307 * Searches alignment for all features and updates colours
309 * @param newMadeVisible
310 * if true newly added feature types will be rendered immediatly
311 * TODO: check to see if this method should actually be proxied so
312 * repaint events can be propagated by the renderer code
315 public synchronized void findAllFeatures(boolean newMadeVisible)
317 newFeatureAdded = false;
321 newFeatureAdded = true;
325 findingFeatures = true;
326 if (av.getFeaturesDisplayed() == null)
328 av.setFeaturesDisplayed(new FeaturesDisplayed());
330 FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
332 ArrayList<String> allfeatures = new ArrayList<String>();
333 ArrayList<String> oldfeatures = new ArrayList<String>();
334 if (renderOrder != null)
336 for (int i = 0; i < renderOrder.length; i++)
338 if (renderOrder[i] != null)
340 oldfeatures.add(renderOrder[i]);
346 minmax = new Hashtable<String, float[][]>();
349 Set<String> oldGroups = new HashSet<String>(featureGroups.keySet());
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 String fgrp = features[index].getFeatureGroup();
365 oldGroups.remove(fgrp);
366 if (!featuresDisplayed.isRegistered(features[index].getType()))
370 Boolean groupDisplayed = featureGroups.get(fgrp);
371 if (groupDisplayed == null)
373 groupDisplayed = Boolean.valueOf(newMadeVisible);
374 featureGroups.put(fgrp, groupDisplayed);
376 if (!groupDisplayed.booleanValue())
382 if (!(features[index].begin == 0 && features[index].end == 0))
384 // If beginning and end are 0, the feature is for the whole sequence
385 // and we don't want to render the feature in the normal way
388 && !oldfeatures.contains(features[index].getType()))
390 // this is a new feature type on the alignment. Mark it for
392 featuresDisplayed.setVisible(features[index].getType());
393 setOrder(features[index].getType(), 0);
397 if (!allfeatures.contains(features[index].getType()))
399 allfeatures.add(features[index].getType());
401 if (!Float.isNaN(features[index].score))
403 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
404 float[][] mm = minmax.get(features[index].getType());
407 mm = new float[][] { null, null };
408 minmax.put(features[index].getType(), mm);
410 if (mm[nonpos] == null)
412 mm[nonpos] = new float[] { features[index].score,
413 features[index].score };
418 if (mm[nonpos][0] > features[index].score)
420 mm[nonpos][0] = features[index].score;
422 if (mm[nonpos][1] < features[index].score)
424 mm[nonpos][1] = features[index].score;
433 * oldGroups now consists of groups that no longer
434 * have any feature in them - remove these
436 for (String grp : oldGroups)
438 featureGroups.remove(grp);
441 updateRenderOrder(allfeatures);
442 findingFeatures = false;
445 protected Boolean firing = Boolean.FALSE;
448 * replaces the current renderOrder with the unordered features in
449 * allfeatures. The ordering of any types in both renderOrder and allfeatures
450 * is preserved, and all new feature types are rendered on top of the existing
451 * types, in the order given by getOrder or the order given in allFeatures.
452 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
453 * eliminate the float storage for computing/recalling the persistent ordering
454 * New Cability: updates min/max for colourscheme range if its dynamic
458 private void updateRenderOrder(List<String> allFeatures)
460 List<String> allfeatures = new ArrayList<String>(allFeatures);
461 String[] oldRender = renderOrder;
462 renderOrder = new String[allfeatures.size()];
463 boolean initOrders = (featureOrder == null);
465 if (oldRender != null && oldRender.length > 0)
467 for (int j = 0; j < oldRender.length; j++)
469 if (oldRender[j] != null)
473 setOrder(oldRender[j],
474 (1 - (1 + (float) j) / oldRender.length));
476 if (allfeatures.contains(oldRender[j]))
478 renderOrder[opos++] = oldRender[j]; // existing features always
479 // appear below new features
480 allfeatures.remove(oldRender[j]);
483 float[][] mmrange = minmax.get(oldRender[j]);
486 FeatureColourI fc = featureColours.get(oldRender[j]);
487 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
489 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
497 if (allfeatures.size() == 0)
499 // no new features - leave order unchanged.
502 int i = allfeatures.size() - 1;
504 boolean sort = false;
505 String[] newf = new String[allfeatures.size()];
506 float[] sortOrder = new float[allfeatures.size()];
507 for (String newfeat : allfeatures)
512 // update from new features minmax if necessary
513 float[][] mmrange = minmax.get(newf[i]);
516 FeatureColourI fc = featureColours.get(newf[i]);
517 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
519 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
523 if (initOrders || !featureOrder.containsKey(newf[i]))
525 int denom = initOrders ? allfeatures.size() : featureOrder.size();
526 // new unordered feature - compute persistent ordering at head of
527 // existing features.
528 setOrder(newf[i], i / (float) denom);
530 // set order from newly found feature from persisted ordering.
531 sortOrder[i] = 2 - featureOrder.get(newf[i]).floatValue();
534 // only sort if we need to
535 sort = sort || sortOrder[i] > sortOrder[i + 1];
539 if (iSize > 1 && sort)
541 jalview.util.QuickSort.sort(sortOrder, newf);
544 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
548 * get a feature style object for the given type string. Creates a
549 * java.awt.Color for a featureType with no existing colourscheme.
555 public FeatureColourI getFeatureStyle(String featureType)
557 FeatureColourI fc = featureColours.get(featureType);
560 Color col = ColorUtils.createColourFromName(featureType);
561 fc = new FeatureColour(col);
562 featureColours.put(featureType, fc);
568 * Returns the configured colour for a particular feature instance. This
569 * includes calculation of 'colour by label', or of a graduated score colour,
570 * if applicable. It does not take into account feature visibility or colour
576 public Color getColour(SequenceFeature feature)
578 FeatureColourI fc = getFeatureStyle(feature.getType());
579 return fc.isColored(feature) ? fc.getColor(feature) : null;
583 * Answers true unless the feature has a score value which lies outside a
584 * minimum or maximum threshold configured for colouring. This method does not
585 * check feature type or group visibility.
587 * @param sequenceFeature
590 protected boolean showFeature(SequenceFeature sequenceFeature)
592 FeatureColourI fc = getFeatureStyle(sequenceFeature.type);
593 return fc.isColored(sequenceFeature);
597 * Answers true if the feature type is currently selected to be displayed,
603 protected boolean showFeatureOfType(String type)
605 return type == null ? false : av.getFeaturesDisplayed().isVisible(type);
609 public void setColour(String featureType, FeatureColourI col)
611 featureColours.put(featureType, col);
615 public void setTransparency(float value)
617 transparency = value;
621 public float getTransparency()
627 * analogous to colour - store a normalized ordering for all feature types in
628 * this rendering context.
631 * Feature type string
633 * normalized priority - 0 means always appears on top, 1 means
636 public float setOrder(String type, float position)
638 if (featureOrder == null)
640 featureOrder = new Hashtable<String, Float>();
642 featureOrder.put(type, new Float(position));
647 * get the global priority (0 (top) to 1 (bottom))
650 * @return [0,1] or -1 for a type without a priority
652 public float getOrder(String type)
654 if (featureOrder != null)
656 if (featureOrder.containsKey(type))
658 return featureOrder.get(type).floatValue();
665 public Map<String, FeatureColourI> getFeatureColours()
667 return featureColours;
671 * Replace current ordering with new ordering
674 * { String(Type), Colour(Type), Boolean(Displayed) }
675 * @return true if any visible features have been reordered, else false
677 public boolean setFeaturePriority(Object[][] data)
679 return setFeaturePriority(data, true);
683 * Sets the priority order for features
686 * { String(Type), Colour(Type), Boolean(Displayed) }
688 * when true current featureDisplay list will be cleared
689 * @return true if any visible features have been reordered or recoloured,
690 * else false (i.e. no need to repaint)
692 public boolean setFeaturePriority(Object[][] data, boolean visibleNew)
695 * note visible feature ordering and colours before update
697 List<String> visibleFeatures = getDisplayedFeatureTypes();
698 Map<String, FeatureColourI> visibleColours = new HashMap<String, FeatureColourI>(
699 getFeatureColours());
701 FeaturesDisplayedI av_featuresdisplayed = null;
704 if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
706 av.getFeaturesDisplayed().clear();
710 av.setFeaturesDisplayed(
711 av_featuresdisplayed = new FeaturesDisplayed());
716 av_featuresdisplayed = av.getFeaturesDisplayed();
722 // The feature table will display high priority
723 // features at the top, but these are the ones
724 // we need to render last, so invert the data
725 renderOrder = new String[data.length];
729 for (int i = 0; i < data.length; i++)
731 String type = data[i][0].toString();
732 setColour(type, (FeatureColourI) data[i][1]);
733 if (((Boolean) data[i][2]).booleanValue())
735 av_featuresdisplayed.setVisible(type);
738 renderOrder[data.length - i - 1] = type;
743 * get the new visible ordering and return true if it has changed
744 * order or any colour has changed
746 List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
747 if (!visibleFeatures.equals(reorderedVisibleFeatures))
750 * the list of ordered visible features has changed
756 * return true if any feature colour has changed
758 for (String feature : visibleFeatures)
760 if (visibleColours.get(feature) != getFeatureStyle(feature))
770 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
772 public void addPropertyChangeListener(PropertyChangeListener listener)
774 changeSupport.addPropertyChangeListener(listener);
779 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
781 public void removePropertyChangeListener(PropertyChangeListener listener)
783 changeSupport.removePropertyChangeListener(listener);
786 public Set<String> getAllFeatureColours()
788 return featureColours.keySet();
791 public void clearRenderOrder()
796 public boolean hasRenderOrder()
798 return renderOrder != null;
802 * Returns feature types in ordering of rendering, where last means on top
804 public List<String> getRenderOrder()
806 if (renderOrder == null)
808 return Arrays.asList(new String[] {});
810 return Arrays.asList(renderOrder);
813 public int getFeatureGroupsSize()
815 return featureGroups != null ? 0 : featureGroups.size();
819 public List<String> getFeatureGroups()
821 // conflict between applet and desktop - featureGroups returns the map in
822 // the desktop featureRenderer
823 return (featureGroups == null) ? Arrays.asList(new String[0])
824 : Arrays.asList(featureGroups.keySet().toArray(new String[0]));
827 public boolean checkGroupVisibility(String group,
828 boolean newGroupsVisible)
830 if (featureGroups == null)
832 // then an exception happens next..
834 if (featureGroups.containsKey(group))
836 return featureGroups.get(group).booleanValue();
838 if (newGroupsVisible)
840 featureGroups.put(group, new Boolean(true));
847 * get visible or invisible groups
850 * true to return visible groups, false to return hidden ones.
851 * @return list of groups
854 public List<String> getGroups(boolean visible)
856 if (featureGroups != null)
858 List<String> gp = new ArrayList<String>();
860 for (String grp : featureGroups.keySet())
862 Boolean state = featureGroups.get(grp);
863 if (state.booleanValue() == visible)
874 public void setGroupVisibility(String group, boolean visible)
876 featureGroups.put(group, new Boolean(visible));
880 public void setGroupVisibility(List<String> toset, boolean visible)
882 if (toset != null && toset.size() > 0 && featureGroups != null)
884 boolean rdrw = false;
885 for (String gst : toset)
887 Boolean st = featureGroups.get(gst);
888 featureGroups.put(gst, new Boolean(visible));
891 rdrw = rdrw || (visible != st.booleanValue());
896 // set local flag indicating redraw needed ?
902 public Map<String, FeatureColourI> getDisplayedFeatureCols()
904 Map<String, FeatureColourI> fcols = new Hashtable<String, FeatureColourI>();
905 if (getViewport().getFeaturesDisplayed() == null)
909 Iterator<String> features = getViewport().getFeaturesDisplayed()
910 .getVisibleFeatures();
911 while (features.hasNext())
913 String feature = features.next();
914 fcols.put(feature, getFeatureStyle(feature));
920 public FeaturesDisplayedI getFeaturesDisplayed()
922 return av.getFeaturesDisplayed();
926 * Returns a (possibly empty) list of visible feature types, in render order
930 public List<String> getDisplayedFeatureTypes()
932 List<String> typ = getRenderOrder();
933 List<String> displayed = new ArrayList<String>();
934 FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
935 if (feature_disp != null)
937 synchronized (feature_disp)
939 for (String type : typ)
941 if (feature_disp.isVisible(type))
952 public List<String> getDisplayedFeatureGroups()
954 List<String> _gps = new ArrayList<String>();
955 boolean valid = false;
956 for (String gp : getFeatureGroups())
958 if (checkGroupVisibility(gp, false))
969 // gps = new String[_gps.size()];
970 // _gps.toArray(gps);