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.FeaturesDisplayedI;
25 import jalview.datamodel.AlignmentI;
26 import jalview.datamodel.SequenceFeature;
27 import jalview.datamodel.SequenceI;
28 import jalview.renderer.seqfeatures.FeatureRenderer;
29 import jalview.schemes.GraduatedColor;
30 import jalview.viewmodel.AlignmentViewport;
32 import java.awt.Color;
33 import java.beans.PropertyChangeListener;
34 import java.beans.PropertyChangeSupport;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Hashtable;
38 import java.util.Iterator;
39 import java.util.List;
42 import java.util.concurrent.ConcurrentHashMap;
44 public abstract class FeatureRendererModel implements
45 jalview.api.FeatureRenderer
49 * global transparency for feature
51 protected float transparency = 1.0f;
53 protected Map<String, Object> featureColours = new ConcurrentHashMap<String, Object>();
55 protected Map<String, Boolean> featureGroups = new ConcurrentHashMap<String, Boolean>();
57 protected Object currentColour;
59 protected String[] renderOrder;
61 protected PropertyChangeSupport changeSupport = new PropertyChangeSupport(
64 protected AlignmentViewport av;
66 public AlignViewportI getViewport()
71 public FeatureRendererSettings getSettings()
73 return new FeatureRendererSettings(this);
76 public void transferSettings(FeatureRendererSettings fr)
78 this.renderOrder = fr.renderOrder;
79 this.featureGroups = fr.featureGroups;
80 this.featureColours = fr.featureColours;
81 this.transparency = fr.transparency;
82 this.featureOrder = fr.featureOrder;
86 * update from another feature renderer
91 public void transferSettings(jalview.api.FeatureRenderer _fr)
93 FeatureRenderer fr = (FeatureRenderer) _fr;
94 FeatureRendererSettings frs = new FeatureRendererSettings(fr);
95 this.renderOrder = frs.renderOrder;
96 this.featureGroups = frs.featureGroups;
97 this.featureColours = frs.featureColours;
98 this.transparency = frs.transparency;
99 this.featureOrder = frs.featureOrder;
100 if (av != null && av != fr.getViewport())
102 // copy over the displayed feature settings
103 if (_fr.getFeaturesDisplayed() != null)
105 FeaturesDisplayedI fd = getFeaturesDisplayed();
108 setFeaturesDisplayedFrom(_fr.getFeaturesDisplayed());
115 java.util.Iterator<String> fdisp = _fr.getFeaturesDisplayed()
116 .getVisibleFeatures();
117 while (fdisp.hasNext())
119 fd.setVisible(fdisp.next());
127 public void setFeaturesDisplayedFrom(FeaturesDisplayedI featuresDisplayed)
129 av.setFeaturesDisplayed(new FeaturesDisplayed(featuresDisplayed));
133 public void setVisible(String featureType)
135 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
138 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
140 if (!fdi.isRegistered(featureType))
142 pushFeatureType(Arrays.asList(new String[] { featureType }));
144 fdi.setVisible(featureType);
148 public void setAllVisible(List<String> featureTypes)
150 FeaturesDisplayedI fdi = av.getFeaturesDisplayed();
153 av.setFeaturesDisplayed(fdi = new FeaturesDisplayed());
155 List<String> nft = new ArrayList<String>();
156 for (String featureType : featureTypes)
158 if (!fdi.isRegistered(featureType))
160 nft.add(featureType);
165 pushFeatureType(nft);
167 fdi.setAllVisible(featureTypes);
171 * push a set of new types onto the render order stack. Note - this is a
172 * direct mechanism rather than the one employed in updateRenderOrder
176 private void pushFeatureType(List<String> types)
179 int ts = types.size();
180 String neworder[] = new String[(renderOrder == null ? 0
181 : renderOrder.length) + ts];
182 types.toArray(neworder);
183 if (renderOrder != null)
185 System.arraycopy(neworder, 0, neworder, renderOrder.length, ts);
186 System.arraycopy(renderOrder, 0, neworder, 0, renderOrder.length);
188 renderOrder = neworder;
191 protected Hashtable minmax = new Hashtable();
193 public Hashtable getMinMax()
199 * normalise a score against the max/min bounds for the feature type.
201 * @param sequenceFeature
202 * @return byte[] { signed, normalised signed (-127 to 127) or unsigned
205 protected final byte[] normaliseScore(SequenceFeature sequenceFeature)
207 float[] mm = ((float[][]) minmax.get(sequenceFeature.type))[0];
208 final byte[] r = new byte[] { 0, (byte) 255 };
211 if (r[0] != 0 || mm[0] < 0.0)
214 r[1] = (byte) ((int) 128.0 + 127.0 * (sequenceFeature.score / mm[1]));
218 r[1] = (byte) ((int) 255.0 * (sequenceFeature.score / mm[1]));
224 boolean newFeatureAdded = false;
226 boolean findingFeatures = false;
228 protected boolean updateFeatures()
230 if (av.getFeaturesDisplayed() == null || renderOrder == null
234 if (av.getFeaturesDisplayed().getVisibleFeatureCount() < 1)
239 // TODO: decide if we should check for the visible feature count first
244 * search the alignment for all new features, give them a colour and display
245 * them. Then fires a PropertyChangeEvent on the changeSupport object.
248 protected void findAllFeatures()
250 synchronized (firing)
252 if (firing.equals(Boolean.FALSE))
254 firing = Boolean.TRUE;
255 findAllFeatures(true); // add all new features as visible
256 changeSupport.firePropertyChange("changeSupport", null, null);
257 firing = Boolean.FALSE;
263 public List<SequenceFeature> findFeaturesAtRes(SequenceI sequence, int res)
265 ArrayList<SequenceFeature> tmp = new ArrayList<SequenceFeature>();
266 SequenceFeature[] features = sequence.getSequenceFeatures();
268 if (features != null)
270 for (int i = 0; i < features.length; i++)
272 if (!av.areFeaturesDisplayed()
273 || !av.getFeaturesDisplayed().isVisible(
274 features[i].getType()))
279 if (features[i].featureGroup != null
280 && featureGroups != null
281 && featureGroups.containsKey(features[i].featureGroup)
282 && !featureGroups.get(features[i].featureGroup)
288 if ((features[i].getBegin() <= res)
289 && (features[i].getEnd() >= res))
291 tmp.add(features[i]);
299 * Searches alignment for all features and updates colours
301 * @param newMadeVisible
302 * if true newly added feature types will be rendered immediatly
303 * TODO: check to see if this method should actually be proxied so
304 * repaint events can be propagated by the renderer code
307 public synchronized void findAllFeatures(boolean newMadeVisible)
309 newFeatureAdded = false;
313 newFeatureAdded = true;
317 findingFeatures = true;
318 if (av.getFeaturesDisplayed() == null)
320 av.setFeaturesDisplayed(new FeaturesDisplayed());
322 FeaturesDisplayedI featuresDisplayed = av.getFeaturesDisplayed();
324 ArrayList<String> allfeatures = new ArrayList<String>();
325 ArrayList<String> oldfeatures = new ArrayList<String>();
326 if (renderOrder != null)
328 for (int i = 0; i < renderOrder.length; i++)
330 if (renderOrder[i] != null)
332 oldfeatures.add(renderOrder[i]);
338 minmax = new Hashtable();
340 AlignmentI alignment = av.getAlignment();
341 for (int i = 0; i < alignment.getHeight(); i++)
343 SequenceI asq = alignment.getSequenceAt(i);
344 SequenceFeature[] features = asq.getSequenceFeatures();
346 if (features == null)
352 while (index < features.length)
354 if (!featuresDisplayed.isRegistered(features[index].getType()))
356 String fgrp = features[index].getFeatureGroup();
359 Boolean groupDisplayed = featureGroups.get(fgrp);
360 if (groupDisplayed == null)
362 groupDisplayed = Boolean.valueOf(newMadeVisible);
363 featureGroups.put(fgrp, groupDisplayed);
365 if (!groupDisplayed.booleanValue())
371 if (!(features[index].begin == 0 && features[index].end == 0))
373 // If beginning and end are 0, the feature is for the whole sequence
374 // and we don't want to render the feature in the normal way
377 && !oldfeatures.contains(features[index].getType()))
379 // this is a new feature type on the alignment. Mark it for
381 featuresDisplayed.setVisible(features[index].getType());
382 setOrder(features[index].getType(), 0);
386 if (!allfeatures.contains(features[index].getType()))
388 allfeatures.add(features[index].getType());
390 if (!Float.isNaN(features[index].score))
392 int nonpos = features[index].getBegin() >= 1 ? 0 : 1;
393 float[][] mm = (float[][]) minmax.get(features[index].getType());
396 mm = new float[][] { null, null };
397 minmax.put(features[index].getType(), mm);
399 if (mm[nonpos] == null)
401 mm[nonpos] = new float[] { features[index].score,
402 features[index].score };
407 if (mm[nonpos][0] > features[index].score)
409 mm[nonpos][0] = features[index].score;
411 if (mm[nonpos][1] < features[index].score)
413 mm[nonpos][1] = features[index].score;
420 updateRenderOrder(allfeatures);
421 findingFeatures = false;
424 protected Boolean firing = Boolean.FALSE;
427 * replaces the current renderOrder with the unordered features in
428 * allfeatures. The ordering of any types in both renderOrder and allfeatures
429 * is preserved, and all new feature types are rendered on top of the existing
430 * types, in the order given by getOrder or the order given in allFeatures.
431 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
432 * eliminate the float storage for computing/recalling the persistent ordering
433 * New Cability: updates min/max for colourscheme range if its dynamic
437 private void updateRenderOrder(List<String> allFeatures)
439 List<String> allfeatures = new ArrayList<String>(allFeatures);
440 String[] oldRender = renderOrder;
441 renderOrder = new String[allfeatures.size()];
442 Object mmrange, fc = null;
443 boolean initOrders = (featureOrder == null);
445 if (oldRender != null && oldRender.length > 0)
447 for (int j = 0; j < oldRender.length; j++)
449 if (oldRender[j] != null)
453 setOrder(oldRender[j], (1 - (1 + (float) j) / oldRender.length));
455 if (allfeatures.contains(oldRender[j]))
457 renderOrder[opos++] = oldRender[j]; // existing features always
458 // appear below new features
459 allfeatures.remove(oldRender[j]);
462 mmrange = minmax.get(oldRender[j]);
465 fc = featureColours.get(oldRender[j]);
466 if (fc != null && fc instanceof GraduatedColor
467 && ((GraduatedColor) fc).isAutoScale())
469 ((GraduatedColor) fc).updateBounds(
470 ((float[][]) mmrange)[0][0],
471 ((float[][]) 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 mmrange = minmax.get(newf[i]);
498 fc = featureColours.get(newf[i]);
499 if (fc != null && fc instanceof GraduatedColor
500 && ((GraduatedColor) fc).isAutoScale())
502 ((GraduatedColor) fc).updateBounds(((float[][]) mmrange)[0][0],
503 ((float[][]) mmrange)[0][1]);
507 if (initOrders || !featureOrder.containsKey(newf[i]))
509 int denom = initOrders ? allfeatures.size() : featureOrder.size();
510 // new unordered feature - compute persistent ordering at head of
511 // existing features.
512 setOrder(newf[i], i / (float) denom);
514 // set order from newly found feature from persisted ordering.
515 sortOrder[i] = 2 - ((Float) featureOrder.get(newf[i])).floatValue();
518 // only sort if we need to
519 sort = sort || sortOrder[i] > sortOrder[i + 1];
523 if (iSize > 1 && sort)
525 jalview.util.QuickSort.sort(sortOrder, newf);
528 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
532 * get a feature style object for the given type string. Creates a
533 * java.awt.Color for a featureType with no existing colourscheme. TODO:
534 * replace return type with object implementing standard abstract colour/style
538 * @return java.awt.Color or GraduatedColor
540 public Object getFeatureStyle(String featureType)
542 Object fc = featureColours.get(featureType);
545 jalview.schemes.UserColourScheme ucs = new jalview.schemes.UserColourScheme();
546 Color col = ucs.createColourFromName(featureType);
547 featureColours.put(featureType, fc = col);
553 * return a nominal colour for this feature
556 * @return standard color, or maximum colour for graduated colourscheme
558 public Color getColour(String featureType)
560 Object fc = getFeatureStyle(featureType);
562 if (fc instanceof Color)
568 if (fc instanceof GraduatedColor)
570 return ((GraduatedColor) fc).getMaxColor();
573 throw new Error("Implementation Error: Unrecognised render object "
574 + fc.getClass() + " for features of type " + featureType);
578 * calculate the render colour for a specific feature using current feature
582 * @return render colour for the given feature
584 public Color getColour(SequenceFeature feature)
586 Object fc = getFeatureStyle(feature.getType());
587 if (fc instanceof Color)
593 if (fc instanceof GraduatedColor)
595 return ((GraduatedColor) fc).findColor(feature);
598 throw new Error("Implementation Error: Unrecognised render object "
599 + fc.getClass() + " for features of type " + feature.getType());
602 protected boolean showFeature(SequenceFeature sequenceFeature)
604 Object fc = getFeatureStyle(sequenceFeature.type);
605 if (fc instanceof GraduatedColor)
607 return ((GraduatedColor) fc).isColored(sequenceFeature);
615 protected boolean showFeatureOfType(String type)
617 return av.getFeaturesDisplayed().isVisible(type);
620 public void setColour(String featureType, Object col)
623 // Color _col = (col instanceof Color) ? ((Color) col) : (col instanceof
624 // GraduatedColor) ? ((GraduatedColor) col).getMaxColor() : null;
625 // Object c = featureColours.get(featureType);
626 // if (c == null || c instanceof Color || (c instanceof GraduatedColor &&
627 // !((GraduatedColor)c).getMaxColor().equals(_col)))
629 featureColours.put(featureType, col);
633 public void setTransparency(float value)
635 transparency = value;
638 public float getTransparency()
643 Map featureOrder = null;
646 * analogous to colour - store a normalized ordering for all feature types in
647 * this rendering context.
650 * Feature type string
652 * normalized priority - 0 means always appears on top, 1 means
655 public float setOrder(String type, float position)
657 if (featureOrder == null)
659 featureOrder = new Hashtable();
661 featureOrder.put(type, new Float(position));
666 * get the global priority (0 (top) to 1 (bottom))
669 * @return [0,1] or -1 for a type without a priority
671 public float getOrder(String type)
673 if (featureOrder != null)
675 if (featureOrder.containsKey(type))
677 return ((Float) featureOrder.get(type)).floatValue();
684 public Map<String, Object> getFeatureColours()
686 return featureColours;
690 * Replace current ordering with new ordering
693 * { String(Type), Colour(Type), Boolean(Displayed) }
695 public void setFeaturePriority(Object[][] data)
697 setFeaturePriority(data, true);
703 * { String(Type), Colour(Type), Boolean(Displayed) }
705 * when true current featureDisplay list will be cleared
707 public void setFeaturePriority(Object[][] data, boolean visibleNew)
709 FeaturesDisplayedI av_featuresdisplayed = null;
712 if ((av_featuresdisplayed = av.getFeaturesDisplayed()) != null)
714 av.getFeaturesDisplayed().clear();
718 av.setFeaturesDisplayed(av_featuresdisplayed = new FeaturesDisplayed());
723 av_featuresdisplayed = av.getFeaturesDisplayed();
729 // The feature table will display high priority
730 // features at the top, but theses are the ones
731 // we need to render last, so invert the data
732 renderOrder = new String[data.length];
736 for (int i = 0; i < data.length; i++)
738 String type = data[i][0].toString();
739 setColour(type, data[i][1]); // todo : typesafety - feature color
741 if (((Boolean) data[i][2]).booleanValue())
743 av_featuresdisplayed.setVisible(type);
746 renderOrder[data.length - i - 1] = type;
754 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
756 public void addPropertyChangeListener(PropertyChangeListener listener)
758 changeSupport.addPropertyChangeListener(listener);
763 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
765 public void removePropertyChangeListener(PropertyChangeListener listener)
767 changeSupport.removePropertyChangeListener(listener);
770 public Set getAllFeatureColours()
772 return featureColours.keySet();
775 public void clearRenderOrder()
780 public boolean hasRenderOrder()
782 return renderOrder != null;
785 public List<String> getRenderOrder()
787 if (renderOrder == null)
789 return Arrays.asList(new String[] {});
791 return Arrays.asList(renderOrder);
794 public int getFeatureGroupsSize()
796 return featureGroups != null ? 0 : featureGroups.size();
800 public List<String> getFeatureGroups()
802 // conflict between applet and desktop - featureGroups returns the map in
803 // the desktop featureRenderer
804 return (featureGroups == null) ? Arrays.asList(new String[0]) : Arrays
805 .asList(featureGroups.keySet().toArray(new String[0]));
808 public boolean checkGroupVisibility(String group, boolean newGroupsVisible)
810 if (featureGroups == null)
812 // then an exception happens next..
814 if (featureGroups.containsKey(group))
816 return featureGroups.get(group).booleanValue();
818 if (newGroupsVisible)
820 featureGroups.put(group, new Boolean(true));
827 * get visible or invisible groups
830 * true to return visible groups, false to return hidden ones.
831 * @return list of groups
834 public List getGroups(boolean visible)
836 if (featureGroups != null)
838 ArrayList gp = new ArrayList();
840 for (Object grp : featureGroups.keySet())
842 Boolean state = featureGroups.get(grp);
843 if (state.booleanValue() == visible)
854 public void setGroupVisibility(String group, boolean visible)
856 featureGroups.put(group, new Boolean(visible));
860 public void setGroupVisibility(List<String> toset, boolean visible)
862 if (toset != null && toset.size() > 0 && featureGroups != null)
864 boolean rdrw = false;
865 for (String gst : toset)
867 Boolean st = featureGroups.get(gst);
868 featureGroups.put(gst, new Boolean(visible));
871 rdrw = rdrw || (visible != st.booleanValue());
876 // set local flag indicating redraw needed ?
882 public Hashtable getDisplayedFeatureCols()
884 Hashtable fcols = new Hashtable();
885 if (getViewport().getFeaturesDisplayed() == null)
889 Iterator<String> en = getViewport().getFeaturesDisplayed()
890 .getVisibleFeatures();
893 String col = en.next();
894 fcols.put(col, getColour(col));
900 public FeaturesDisplayedI getFeaturesDisplayed()
902 return av.getFeaturesDisplayed();
906 public String[] getDisplayedFeatureTypes()
909 typ = getRenderOrder().toArray(new String[0]);
910 FeaturesDisplayedI feature_disp = av.getFeaturesDisplayed();
911 if (feature_disp != null)
913 synchronized (feature_disp)
915 for (int i = 0; i < typ.length; i++)
917 if (!feature_disp.isVisible(typ[i]))
928 public String[] getDisplayedFeatureGroups()
931 ArrayList<String> _gps = new ArrayList<String>();
932 Iterator en = getFeatureGroups().iterator();
934 boolean valid = false;
937 String gp = (String) en.next();
938 if (checkGroupVisibility(gp, false))
949 gps = new String[_gps.size()];