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;
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, FeatureColourI> featureColours = new ConcurrentHashMap<String, FeatureColourI>();
57 protected Map<String, Boolean> featureGroups = new ConcurrentHashMap<String, Boolean>();
59 protected String[] renderOrder;
61 Map<String, Float> featureOrder = null;
63 protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
66 protected AlignViewportI av;
69 public AlignViewportI getViewport()
74 public FeatureRendererSettings getSettings()
76 return new FeatureRendererSettings(this);
79 public void transferSettings(FeatureRendererSettings fr)
81 this.renderOrder = fr.renderOrder;
82 this.featureGroups = fr.featureGroups;
83 this.featureColours = fr.featureColours;
84 this.transparency = fr.transparency;
85 this.featureOrder = fr.featureOrder;
89 * update from another feature renderer
94 public void transferSettings(jalview.api.FeatureRenderer _fr)
96 FeatureRenderer fr = (FeatureRenderer) _fr;
97 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
98 this.renderOrder = frs.renderOrder;
99 this.featureGroups = frs.featureGroups;
100 this.featureColours = frs.featureColours;
101 this.transparency = frs.transparency;
102 this.featureOrder = frs.featureOrder;
103 if (av != null && av != fr.getViewport())
105 // copy over the displayed feature settings
106 if (_fr.getFeaturesDisplayed() != null)
108 FeaturesDisplayedI fd = getFeaturesDisplayed();
111 setFeaturesDisplayedFrom(_fr.getFeaturesDisplayed());
118 java.util.Iterator<String> fdisp = _fr.getFeaturesDisplayed()
119 .getVisibleFeatures();
120 while (fdisp.hasNext())
122 fd.setVisible(fdisp.next());
130 public void setFeaturesDisplayedFrom(FeaturesDisplayedI featuresDisplayed)
132 av.setFeaturesDisplayed(new FeaturesDisplayed(featuresDisplayed));
136 public void setVisible(String featureType)
138 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
141 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
143 if (!fdi.isRegistered(featureType))
145 pushFeatureType(Arrays.asList(new String[] { featureType }));
147 fdi.setVisible(featureType);
151 public void setAllVisible(List<String> featureTypes)
153 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
156 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
158 List<String> nft = new ArrayList<String>();
159 for (String featureType : featureTypes)
161 if (!fdi.isRegistered(featureType))
163 nft.add(featureType);
168 pushFeatureType(nft);
170 fdi.setAllVisible(featureTypes);
174 * push a set of new types onto the render order stack. Note - this is a
175 * direct mechanism rather than the one employed in updateRenderOrder
179 private void pushFeatureType(List<String> types)
182 int ts = types.size();
183 String neworder[] = new String[(renderOrder == null ? 0
184 : renderOrder.length) + ts];
185 types.toArray(neworder);
186 if (renderOrder != null)
188 System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
189 System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
191 renderOrder = neworder;
194 protected Map<String, float[][]> minmax = new Hashtable<String, float[][]>();
196 public Map<String, float[][]> getMinMax()
202 * normalise a score against the max/min bounds for the feature type.
204 * @param sequenceFeature
205 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
208 protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
210 float[] mm = minmax.get(sequenceFeature.type)[0];
211 final byte[] r = new byte[] { 0, (byte) 255 };
214 if (r[0] != 0 || mm[0] < 0.0)
217 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
221 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
227 boolean newFeatureAdded = false;
229 boolean findingFeatures = false;
231 protected boolean updateFeatures()
233 if (av.getFeaturesDisplayed() == null || renderOrder == null
237 if (av.getFeaturesDisplayed().getVisibleFeatureCount() < 1)
242 // TODO: decide if we should check for the visible feature count first
247 * search the alignment for all new features, give them a colour and display
248 * them. Then fires a PropertyChangeEvent on the changeSupport object.
251 protected void findAllFeatures()
253 synchronized (firing)
255 if (firing.equals(Boolean.FALSE))
257 firing = Boolean.TRUE;
258 findAllFeatures(true); // add all new features as visible
259 changeSupport.firePropertyChange("changeSupport", null, null);
260 firing = Boolean.FALSE;
266 public List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
268 ArrayList<SequenceFeature> tmp = new ArrayList<SequenceFeature>();
269 SequenceFeature[] features = sequence.getSequenceFeatures();
271 if (features != null)
273 for (int i = 0; i < features.length; i++)
275 if (!av.areFeaturesDisplayed()
276 || !av.getFeaturesDisplayed().isVisible(
277 features[i].getType()))
282 if (features[i].featureGroup != null
283 && featureGroups != null
284 && featureGroups.containsKey(features[i].featureGroup)
285 && !featureGroups.get(features[i].featureGroup)
291 if ((features[i].getBegin() <= res)
292 && (features[i].getEnd() >= res))
294 tmp.add(features[i]);
302 * Searches alignment for all features and updates colours
304 * @param newMadeVisible
305 * if true newly added feature types will be rendered immediatly
306 * TODO: check to see if this method should actually be proxied so
307 * repaint events can be propagated by the renderer code
310 public synchronized void findAllFeatures(boolean newMadeVisible)
312 newFeatureAdded = false;
316 newFeatureAdded = true;
320 findingFeatures = true;
321 if (av.getFeaturesDisplayed() == null)
323 av.setFeaturesDisplayed(new FeaturesDisplayed());
325 FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
327 ArrayList<String> allfeatures = new ArrayList<String>();
328 ArrayList<String> oldfeatures = new ArrayList<String>();
329 if (renderOrder != null)
331 for (int i = 0; i < renderOrder.length; i++)
333 if (renderOrder[i] != null)
335 oldfeatures.add(renderOrder[i]);
341 minmax = new Hashtable<String, float[][]>();
343 AlignmentI alignment = av.getAlignment();
344 for (int i = 0; i < alignment.getHeight(); i++)
346 SequenceI asq = alignment.getSequenceAt(i);
347 SequenceFeature[] features = asq.getSequenceFeatures();
349 if (features == null)
355 while (index < features.length)
357 if (!featuresDisplayed.isRegistered(features[index].getType()))
359 String fgrp = features[index].getFeatureGroup();
362 Boolean groupDisplayed = featureGroups.get(fgrp);
363 if (groupDisplayed == null)
365 groupDisplayed = Boolean.valueOf(newMadeVisible);
366 featureGroups.put(fgrp, groupDisplayed);
368 if (!groupDisplayed.booleanValue())
374 if (!(features[index].begin == 0 && features[index].end == 0))
376 // If beginning and end are 0, the feature is for the whole sequence
377 // and we don't want to render the feature in the normal way
380 && !oldfeatures.contains(features[index].getType()))
382 // this is a new feature type on the alignment. Mark it for
384 featuresDisplayed.setVisible(features[index].getType());
385 setOrder(features[index].getType(), 0);
389 if (!allfeatures.contains(features[index].getType()))
391 allfeatures.add(features[index].getType());
393 if (!Float.isNaN(features[index].score))
395 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
396 float[][] mm = minmax.get(features[index].getType());
399 mm = new float[][] { null, null };
400 minmax.put(features[index].getType(), mm);
402 if (mm[nonpos] == null)
404 mm[nonpos] = new float[] { features[index].score,
405 features[index].score };
410 if (mm[nonpos][0] > features[index].score)
412 mm[nonpos][0] = features[index].score;
414 if (mm[nonpos][1] < features[index].score)
416 mm[nonpos][1] = features[index].score;
423 updateRenderOrder(allfeatures);
424 findingFeatures = false;
427 protected Boolean firing = Boolean.FALSE;
430 * replaces the current renderOrder with the unordered features in
431 * allfeatures. The ordering of any types in both renderOrder and allfeatures
432 * is preserved, and all new feature types are rendered on top of the existing
433 * types, in the order given by getOrder or the order given in allFeatures.
434 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
435 * eliminate the float storage for computing/recalling the persistent ordering
436 * New Cability: updates min/max for colourscheme range if its dynamic
440 private void updateRenderOrder(List<String> allFeatures)
442 List<String> allfeatures = new ArrayList<String>(allFeatures);
443 String[] oldRender = renderOrder;
444 renderOrder = new String[allfeatures.size()];
445 boolean initOrders = (featureOrder == null);
447 if (oldRender != null && oldRender.length > 0)
449 for (int j = 0; j < oldRender.length; j++)
451 if (oldRender[j] != null)
455 setOrder(oldRender[j], (1 - (1 + (float) j) / oldRender.length));
457 if (allfeatures.contains(oldRender[j]))
459 renderOrder[opos++] = oldRender[j]; // existing features always
460 // appear below new features
461 allfeatures.remove(oldRender[j]);
464 float[][] mmrange = minmax.get(oldRender[j]);
467 FeatureColourI fc = featureColours.get(oldRender[j]);
468 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
470 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
478 if (allfeatures.size() == 0)
480 // no new features - leave order unchanged.
483 int i = allfeatures.size() - 1;
485 boolean sort = false;
486 String[] newf = new String[allfeatures.size()];
487 float[] sortOrder = new float[allfeatures.size()];
488 for (String newfeat : allfeatures)
493 // update from new features minmax if necessary
494 float[][] mmrange = minmax.get(newf[i]);
497 FeatureColourI fc = featureColours.get(newf[i]);
498 if (fc != null && !fc.isSimpleColour() && fc.isAutoScaled())
500 fc.updateBounds(mmrange[0][0], mmrange[0][1]);
504 if (initOrders || !featureOrder.containsKey(newf[i]))
506 int denom = initOrders ? allfeatures.size() : featureOrder.size();
507 // new unordered feature - compute persistent ordering at head of
508 // existing features.
509 setOrder(newf[i], i / (float) denom);
511 // set order from newly found feature from persisted ordering.
512 sortOrder[i] = 2 - featureOrder.get(newf[i]).floatValue();
515 // only sort if we need to
516 sort = sort || sortOrder[i] > sortOrder[i + 1];
520 if (iSize > 1 && sort)
522 jalview.util.QuickSort.sort(sortOrder, newf);
525 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
529 * get a feature style object for the given type string. Creates a
530 * java.awt.Color for a featureType with no existing colourscheme.
536 public FeatureColourI getFeatureStyle(String featureType)
538 FeatureColourI fc = featureColours.get(featureType);
541 Color col = UserColourScheme.createColourFromName(featureType);
542 fc = new FeatureColour(col);
543 featureColours.put(featureType, fc);
549 * calculate the render colour for a specific feature using current feature
553 * @return render colour for the given feature
555 public Color getColour(SequenceFeature feature)
557 FeatureColourI fc = getFeatureStyle(feature.getType());
558 return fc.getColor(feature);
561 protected boolean showFeature(SequenceFeature sequenceFeature)
563 FeatureColourI fc = getFeatureStyle(sequenceFeature.type);
564 return fc.isColored(sequenceFeature);
567 protected boolean showFeatureOfType(String type)
569 return av.getFeaturesDisplayed().isVisible(type);
573 public void setColour(String featureType, FeatureColourI col)
575 featureColours.put(featureType, col);
578 public void setTransparency(float value)
580 transparency = value;
583 public float getTransparency()
589 * analogous to colour - store a normalized ordering for all feature types in
590 * this rendering context.
593 * Feature type string
595 * normalized priority - 0 means always appears on top, 1 means
598 public float setOrder(String type, float position)
600 if (featureOrder == null)
602 featureOrder = new Hashtable<String, Float>();
604 featureOrder.put(type, new Float(position));
609 * get the global priority (0 (top) to 1 (bottom))
612 * @return [0,1] or -1 for a type without a priority
614 public float getOrder(String type)
616 if (featureOrder != null)
618 if (featureOrder.containsKey(type))
620 return featureOrder.get(type).floatValue();
627 public Map<String, FeatureColourI> getFeatureColours()
629 return featureColours;
633 * Replace current ordering with new ordering
636 * { String(Type), Colour(Type), Boolean(Displayed) }
637 * @return true if any visible features have been reordered, else false
639 public boolean setFeaturePriority(Object[][] data)
641 return setFeaturePriority(data, true);
645 * Sets the priority order for features
648 * { String(Type), Colour(Type), Boolean(Displayed) }
650 * when true current featureDisplay list will be cleared
651 * @return true if any visible features have been reordered or recoloured,
652 * else false (i.e. no need to repaint)
654 public boolean setFeaturePriority(Object[][] data, boolean visibleNew)
657 * note visible feature ordering and colours before update
659 List<String> visibleFeatures = getDisplayedFeatureTypes();
660 Map<String, FeatureColourI> visibleColours = new HashMap<String, FeatureColourI>(
661 getFeatureColours());
663 FeaturesDisplayedI av_featuresdisplayed = null;
666 if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
668 av.getFeaturesDisplayed().clear();
672 av.setFeaturesDisplayed(av_featuresdisplayed = new FeaturesDisplayed());
677 av_featuresdisplayed = av.getFeaturesDisplayed();
683 // The feature table will display high priority
684 // features at the top, but these are the ones
685 // we need to render last, so invert the data
686 renderOrder = new String[data.length];
690 for (int i = 0; i < data.length; i++)
692 String type = data[i][0].toString();
693 setColour(type, (FeatureColourI) data[i][1]);
694 if (((Boolean) data[i][2]).booleanValue())
696 av_featuresdisplayed.setVisible(type);
699 renderOrder[data.length - i - 1] = type;
704 * get the new visible ordering and return true if it has changed
705 * order or any colour has changed
707 List<String> reorderedVisibleFeatures = getDisplayedFeatureTypes();
708 if (!visibleFeatures.equals(reorderedVisibleFeatures))
711 * the list of ordered visible features has changed
717 * return true if any feature colour has changed
719 for (String feature : visibleFeatures)
721 if (visibleColours.get(feature) != getFeatureStyle(feature))
731 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
733 public void addPropertyChangeListener(PropertyChangeListener listener)
735 changeSupport.addPropertyChangeListener(listener);
740 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
742 public void removePropertyChangeListener(PropertyChangeListener listener)
744 changeSupport.removePropertyChangeListener(listener);
747 public Set<String> getAllFeatureColours()
749 return featureColours.keySet();
752 public void clearRenderOrder()
757 public boolean hasRenderOrder()
759 return renderOrder != null;
763 * Returns feature types in ordering of rendering, where last means on top
765 public List<String> getRenderOrder()
767 if (renderOrder == null)
769 return Arrays.asList(new String[] {});
771 return Arrays.asList(renderOrder);
774 public int getFeatureGroupsSize()
776 return featureGroups != null ? 0 : featureGroups.size();
780 public List<String> getFeatureGroups()
782 // conflict between applet and desktop - featureGroups returns the map in
783 // the desktop featureRenderer
784 return (featureGroups == null) ? Arrays.asList(new String[0]) : Arrays
785 .asList(featureGroups.keySet().toArray(new String[0]));
788 public boolean checkGroupVisibility(String group, boolean newGroupsVisible)
790 if (featureGroups == null)
792 // then an exception happens next..
794 if (featureGroups.containsKey(group))
796 return featureGroups.get(group).booleanValue();
798 if (newGroupsVisible)
800 featureGroups.put(group, new Boolean(true));
807 * get visible or invisible groups
810 * true to return visible groups, false to return hidden ones.
811 * @return list of groups
814 public List getGroups(boolean visible)
816 if (featureGroups != null)
818 List<String> gp = new ArrayList<String>();
820 for (String grp : featureGroups.keySet())
822 Boolean state = featureGroups.get(grp);
823 if (state.booleanValue() == visible)
834 public void setGroupVisibility(String group, boolean visible)
836 featureGroups.put(group, new Boolean(visible));
840 public void setGroupVisibility(List<String> toset, boolean visible)
842 if (toset != null && toset.size() > 0 && featureGroups != null)
844 boolean rdrw = false;
845 for (String gst : toset)
847 Boolean st = featureGroups.get(gst);
848 featureGroups.put(gst, new Boolean(visible));
851 rdrw = rdrw || (visible != st.booleanValue());
856 // set local flag indicating redraw needed ?
862 public Map<String, FeatureColourI> getDisplayedFeatureCols()
864 Map<String, FeatureColourI> fcols = new Hashtable<String, FeatureColourI>();
865 if (getViewport().getFeaturesDisplayed() == null)
869 Iterator<String> features = getViewport().getFeaturesDisplayed()
870 .getVisibleFeatures();
871 while (features.hasNext())
873 String feature = features.next();
874 fcols.put(feature, getFeatureStyle(feature));
880 public FeaturesDisplayedI getFeaturesDisplayed()
882 return av.getFeaturesDisplayed();
886 * Returns a (possibly empty) list of visible feature types, in render order
890 public List<String> getDisplayedFeatureTypes()
892 List<String> typ = getRenderOrder();
893 List<String> displayed = new ArrayList<String>();
894 FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
895 if (feature_disp != null)
897 synchronized (feature_disp)
899 for (String type : typ)
901 if (feature_disp.isVisible(type))
912 public List<String> getDisplayedFeatureGroups()
914 List<String> _gps = new ArrayList<String>();
915 boolean valid = false;
916 for (String gp : getFeatureGroups())
918 if (checkGroupVisibility(gp, false))
929 // gps = new String[_gps.size()];
930 // _gps.toArray(gps);