2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import java.awt.event.*;
25 import java.awt.image.*;
26 import java.beans.PropertyChangeListener;
27 import java.beans.PropertyChangeSupport;
31 import jalview.datamodel.*;
39 public class FeatureRenderer
44 float transparency = 1.0f;
48 Hashtable featureColours = new Hashtable();
50 // A higher level for grouping features of a
52 Hashtable featureGroups = new Hashtable();
54 // This is actually an Integer held in the hashtable,
55 // Retrieved using the key feature type
59 PropertyChangeSupport changeSupport=new PropertyChangeSupport(this);
64 * Creates a new FeatureRenderer object.
69 public FeatureRenderer(AlignmentPanel ap)
75 public class FeatureRendererSettings implements Cloneable {
77 Hashtable featureGroups;
78 Hashtable featureColours;
80 Hashtable featureOrder;
81 public FeatureRendererSettings(String[] renderOrder,
82 Hashtable featureGroups, Hashtable featureColours,
83 float transparency, Hashtable featureOrder)
86 this.renderOrder = renderOrder;
87 this.featureGroups = featureGroups;
88 this.featureColours = featureColours;
89 this.transparency = transparency;
90 this.featureOrder = featureOrder;
92 public FeatureRendererSettings(FeatureRenderer fr)
94 this.renderOrder = fr.renderOrder;
95 this.featureGroups = fr.featureGroups;
96 this.featureColours = fr.featureColours;
97 this.transparency = fr.transparency;
98 this.featureOrder = fr.featureOrder;
101 public FeatureRendererSettings getSettings()
103 return new FeatureRendererSettings(this);
105 public void transferSettings(FeatureRendererSettings fr)
107 this.renderOrder = fr.renderOrder;
108 this.featureGroups = fr.featureGroups;
109 this.featureColours = fr.featureColours;
110 this.transparency = fr.transparency;
111 this.featureOrder = fr.featureOrder;
113 public void transferSettings(FeatureRenderer fr)
115 this.renderOrder = fr.renderOrder;
116 this.featureGroups = fr.featureGroups;
117 this.featureColours = fr.featureColours;
118 this.transparency = fr.transparency;
119 this.featureOrder = fr.featureOrder;
122 BufferedImage offscreenImage;
123 boolean offscreenRender = false;
124 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
126 return new Color(findFeatureColour(initialCol.getRGB(),
131 * This is used by the Molecule Viewer and Overview to get the accurate
132 * colourof the rendered sequence
134 public int findFeatureColour(int initialCol, SequenceI seq, int column)
136 if (!av.showSequenceFeatures)
144 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
145 if (sequenceFeatures!=null)
147 sfSize = sequenceFeatures.length;
151 if (sequenceFeatures!=lastSeq.getDatasetSequence().getSequenceFeatures()) {
152 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
153 if (sequenceFeatures != null)
155 sfSize = sequenceFeatures.length;
159 if (sequenceFeatures == null || sfSize==0)
165 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
167 return Color.white.getRGB();
170 // Only bother making an offscreen image if transparency is applied
171 if (transparency != 1.0f && offscreenImage == null)
173 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
176 currentColour = null;
178 offscreenRender = true;
180 if (offscreenImage != null)
182 offscreenImage.setRGB(0, 0, initialCol);
183 drawSequence(offscreenImage.getGraphics(),
187 return offscreenImage.getRGB(0, 0);
193 lastSeq.findPosition(column),
196 if (currentColour == null)
202 return ( (Integer) currentColour).intValue();
231 // SequenceFeature sf;
233 SequenceFeature[] sequenceFeatures;
234 int sfSize, sfindex, spos, epos;
236 synchronized public void drawSequence(Graphics g, SequenceI seq,
237 int start, int end, int y1)
240 if (seq.getDatasetSequence().getSequenceFeatures() == null
241 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
248 fm = g.getFontMetrics();
251 if (av.featuresDisplayed == null
252 || renderOrder == null
256 if (av.featuresDisplayed.size() < 1)
261 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
264 if (lastSeq == null || seq != lastSeq
265 || seq.getDatasetSequence().getSequenceFeatures()!=sequenceFeatures)
268 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
271 if (transparency != 1 && g != null)
273 Graphics2D g2 = (Graphics2D) g;
275 AlphaComposite.getInstance(
276 AlphaComposite.SRC_OVER, transparency));
279 if (!offscreenRender)
281 spos = lastSeq.findPosition(start);
282 epos = lastSeq.findPosition(end);
285 sfSize = sequenceFeatures.length;
287 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
289 type = renderOrder[renderIndex];
291 if (type == null || !av.featuresDisplayed.containsKey(type))
296 // loop through all features in sequence to find
297 // current feature to render
298 for (sfindex = 0; sfindex < sfSize; sfindex++)
300 if (!sequenceFeatures[sfindex].type.equals(type))
305 if (featureGroups != null
306 && sequenceFeatures[sfindex].featureGroup != null
308 sequenceFeatures[sfindex].featureGroup.length()!=0
309 && featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
311 ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].
318 if (!offscreenRender && (sequenceFeatures[sfindex].getBegin() > epos
319 || sequenceFeatures[sfindex].getEnd() < spos))
324 if (offscreenRender && offscreenImage == null)
326 if (sequenceFeatures[sfindex].begin <= start &&
327 sequenceFeatures[sfindex].end >= start)
329 currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
333 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
336 renderFeature(g, seq,
337 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
338 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
339 new Color( ( (Integer) av.featuresDisplayed.get(
340 sequenceFeatures[sfindex].type)).intValue()),
342 renderFeature(g, seq,
343 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
344 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
345 new Color( ( (Integer) av.featuresDisplayed.get(
346 sequenceFeatures[sfindex].type)).intValue()),
352 renderFeature(g, seq,
353 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
354 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
355 getColour(sequenceFeatures[sfindex].type),
363 if (transparency != 1.0f && g != null)
365 Graphics2D g2 = (Graphics2D) g;
367 AlphaComposite.getInstance(
368 AlphaComposite.SRC_OVER, 1.0f));
374 void renderFeature(Graphics g, SequenceI seq,
375 int fstart, int fend, Color featureColour, int start,
379 if ( ( (fstart <= end) && (fend >= start)))
382 { // fix for if the feature we have starts before the sequence start,
383 fstart = start; // but the feature end is still valid!!
390 int pady = (y1 + av.charHeight) - av.charHeight / 5;
391 for (i = fstart; i <= fend; i++)
393 s = seq.getCharAt(i);
395 if (jalview.util.Comparison.isGap(s))
400 g.setColor(featureColour);
402 g.fillRect( (i - start) * av.charWidth, y1, av.charWidth, av.charHeight);
404 if (offscreenRender || !av.validCharWidth)
409 g.setColor(Color.white);
410 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
411 g.drawString(String.valueOf(s),
412 charOffset + (av.charWidth * (i - start)),
419 boolean newFeatureAdded = false;
421 * Called when alignment in associated view has new/modified features
422 * to discover and display.
425 public void featuresAdded()
431 boolean findingFeatures = false;
433 * search the alignment for all new features, give them a colour and display
434 * them. Then fires a PropertyChangeEvent on the changeSupport object.
437 void findAllFeatures()
439 synchronized (firing)
441 if (firing.equals(Boolean.FALSE)) {
443 findAllFeatures(true); // add all new features as visible
444 changeSupport.firePropertyChange("changeSupport",null,null);
445 firing=Boolean.FALSE;
450 * Searches alignment for all features and updates colours
452 * @param newMadeVisible
453 * if true newly added feature types will be rendered immediatly
455 synchronized void findAllFeatures(boolean newMadeVisible) {
456 newFeatureAdded = false;
460 newFeatureAdded = true;
464 findingFeatures = true;
466 if (av.featuresDisplayed == null)
468 av.featuresDisplayed = new Hashtable();
471 allfeatures = new Vector();
472 Vector oldfeatures = new Vector();
473 if (renderOrder!=null)
475 for (int i=0; i<renderOrder.length; i++) {
476 if (renderOrder[i]!=null)
478 oldfeatures.addElement(renderOrder[i]);
482 for (int i = 0; i < av.alignment.getHeight(); i++)
484 SequenceFeature[] features
485 = av.alignment.getSequenceAt(i).getDatasetSequence().
486 getSequenceFeatures();
488 if (features == null)
494 while (index < features.length)
496 if (!av.featuresDisplayed.containsKey(features[index].getType()))
499 if(featureGroups.containsKey(features[index].getType()))
501 boolean visible = ( (Boolean) featureGroups.get(
502 features[index].featureGroup)).booleanValue();
512 if (! (features[index].begin == 0 && features[index].end == 0))
514 // If beginning and end are 0, the feature is for the whole sequence
515 // and we don't want to render the feature in the normal way
517 if (newMadeVisible && !oldfeatures.contains(features[index].getType())) {
518 // this is a new feature type on the alignment. Mark it for
520 av.featuresDisplayed.put(features[index].getType(),
521 new Integer(getColour(features[index].
522 getType()).getRGB()));
523 setOrder(features[index].getType(),0);
527 if (!allfeatures.contains(features[index].getType()))
529 allfeatures.addElement(features[index].getType());
534 updateRenderOrder(allfeatures);
535 findingFeatures = false;
537 protected Boolean firing=Boolean.FALSE;
539 * replaces the current renderOrder with the unordered features in allfeatures.
540 * The ordering of any types in both renderOrder and allfeatures is preserved,
541 * and all new feature types are rendered on top of the existing types, in
542 * the order given by getOrder or the order given in allFeatures.
543 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
544 * eliminate the float storage for computing/recalling the persistent ordering
548 private void updateRenderOrder(Vector allFeatures) {
549 Vector allfeatures = new Vector(allFeatures);
550 String[] oldRender = renderOrder;
551 renderOrder = new String[allfeatures.size()];
552 boolean initOrders=(featureOrder==null);
554 if (oldRender!=null && oldRender.length>0)
556 for (int j=0; j<oldRender.length; j++)
558 if (oldRender[j]!=null)
562 setOrder(oldRender[j], (1-(1+(float)j)/(float) oldRender.length));
564 if (allfeatures.contains(oldRender[j])) {
565 renderOrder[opos++] = oldRender[j]; // existing features always
566 // appear below new features
567 allfeatures.removeElement(oldRender[j]);
572 if (allfeatures.size()==0) {
573 // no new features - leave order unchanged.
576 int i=allfeatures.size()-1;
579 String[] newf = new String[allfeatures.size()];
580 float[] sortOrder = new float[allfeatures.size()];
581 Enumeration en = allfeatures.elements();
582 // sort remaining elements
583 while (en.hasMoreElements())
585 newf[i] = en.nextElement().toString();
586 if (initOrders || !featureOrder.containsKey(newf[i]))
588 int denom = initOrders ? allfeatures.size() : featureOrder.size();
589 // new unordered feature - compute persistent ordering at head of
590 // existing features.
591 setOrder(newf[i], i/(float) denom);
593 // set order from newly found feature from persisted ordering.
594 sortOrder[i] = 2-((Float) featureOrder.get(newf[i])).floatValue();
597 // only sort if we need to
598 sort = sort || sortOrder[i]>sortOrder[i+1];
603 jalview.util.QuickSort.sort(sortOrder, newf);
605 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
607 public Color getColour(String featureType)
609 if (!featureColours.containsKey(featureType))
611 jalview.schemes.UserColourScheme ucs = new
612 jalview.schemes.UserColourScheme();
613 Color col = ucs.createColourFromName(featureType);
614 featureColours.put(featureType, col);
618 return (Color) featureColours.get(featureType);
621 static String lastFeatureAdded;
622 static String lastFeatureGroupAdded;
623 static String lastDescriptionAdded;
625 int featureIndex = 0;
626 boolean amendFeatures(final SequenceI[] sequences,
627 final SequenceFeature[] features,
629 final AlignmentPanel ap)
634 JPanel bigPanel = new JPanel(new BorderLayout());
635 final JComboBox overlaps;
636 final JTextField name = new JTextField(25);
637 final JTextField source = new JTextField(25);
638 final JTextArea description = new JTextArea(3, 25);
639 final JSpinner start = new JSpinner();
640 final JSpinner end = new JSpinner();
641 start.setPreferredSize(new Dimension(80, 20));
642 end.setPreferredSize(new Dimension(80, 20));
644 final JPanel colour = new JPanel();
645 colour.setBorder(BorderFactory.createEtchedBorder());
646 colour.setMaximumSize(new Dimension(40, 10));
647 colour.addMouseListener(new MouseAdapter()
649 public void mousePressed(MouseEvent evt)
651 Color col = JColorChooser.showDialog(Desktop.desktop,
652 "Select Feature Colour",
653 colour.getBackground());
655 colour.setBackground(col);
660 JPanel tmp = new JPanel();
661 JPanel panel = new JPanel(new GridLayout(3, 1));
663 ///////////////////////////////////////
664 ///MULTIPLE FEATURES AT SELECTED RESIDUE
665 if(!newFeatures && features.length>1)
667 panel = new JPanel(new GridLayout(4, 1));
669 tmp.add(new JLabel("Select Feature: "));
670 overlaps = new JComboBox();
671 for(int i=0; i<features.length; i++)
673 overlaps.addItem(features[i].getType()
674 +"/"+features[i].getBegin()+"-"+features[i].getEnd()
675 +" ("+features[i].getFeatureGroup()+")");
680 overlaps.addItemListener(new ItemListener()
682 public void itemStateChanged(ItemEvent e)
684 int index = overlaps.getSelectedIndex();
687 featureIndex = index;
688 name.setText(features[index].getType());
689 description.setText(features[index].getDescription());
690 source.setText(features[index].getFeatureGroup());
691 start.setValue(new Integer(features[index].getBegin()));
692 end.setValue(new Integer(features[index].getEnd()));
694 SearchResults highlight = new SearchResults();
695 highlight.addResult(sequences[0],
696 features[index].getBegin(),
697 features[index].getEnd());
699 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
702 Color col = getColour(name.getText());
706 jalview.schemes.UserColourScheme()
707 .createColourFromName(name.getText());
710 colour.setBackground(col);
718 //////////////////////////////////////
722 tmp.add(new JLabel("Name: ", JLabel.RIGHT));
727 tmp.add(new JLabel("Group: ", JLabel.RIGHT));
732 tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
734 colour.setPreferredSize(new Dimension(150, 15));
736 bigPanel.add(panel, BorderLayout.NORTH);
738 panel = new JPanel();
739 panel.add(new JLabel("Description: ", JLabel.RIGHT));
740 description.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
741 description.setLineWrap(true);
742 panel.add(new JScrollPane(description));
746 bigPanel.add(panel, BorderLayout.SOUTH);
748 panel = new JPanel();
749 panel.add(new JLabel(" Start:", JLabel.RIGHT));
751 panel.add(new JLabel(" End:", JLabel.RIGHT));
753 bigPanel.add(panel, BorderLayout.CENTER);
757 bigPanel.add(panel, BorderLayout.CENTER);
760 if (lastFeatureAdded == null)
762 if (features[0].type != null)
764 lastFeatureAdded = features[0].type;
768 lastFeatureAdded = "feature_1";
772 if (lastFeatureGroupAdded == null)
774 if (features[0].featureGroup != null)
776 lastFeatureGroupAdded = features[0].featureGroup;
780 lastFeatureGroupAdded = "Jalview";
786 name.setText(lastFeatureAdded);
787 source.setText(lastFeatureGroupAdded);
791 name.setText(features[0].getType());
792 source.setText(features[0].getFeatureGroup());
795 start.setValue(new Integer(features[0].getBegin()));
796 end.setValue(new Integer(features[0].getEnd()));
797 description.setText(features[0].getDescription());
798 colour.setBackground(getColour(name.getText()));
804 options = new Object[]
806 "Amend", "Delete", "Cancel"};
810 options = new Object[]
815 String title = newFeatures ? "Create New Sequence Feature(s)" :
816 "Amend/Delete Features for "
817 + sequences[0].getName();
819 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
822 JOptionPane.YES_NO_CANCEL_OPTION,
823 JOptionPane.QUESTION_MESSAGE,
827 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
829 if (reply == JOptionPane.OK_OPTION && name.getText().length()>0)
831 // This ensures that the last sequence
832 // is refreshed and new features are rendered
834 lastFeatureAdded = name.getText().trim();
835 lastFeatureGroupAdded = source.getText().trim();
836 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
838 if(lastFeatureGroupAdded.length()<1)
839 lastFeatureGroupAdded = null;
844 SequenceFeature sf = features[featureIndex];
846 if (reply == JOptionPane.NO_OPTION)
848 sequences[0].getDatasetSequence().deleteFeature(sf);
850 else if (reply == JOptionPane.YES_OPTION)
852 sf.type = lastFeatureAdded;
853 sf.featureGroup = lastFeatureGroupAdded;
854 sf.description = lastDescriptionAdded;
856 setColour(sf.type, colour.getBackground());
857 av.featuresDisplayed.put(sf.type,
858 new Integer(colour.getBackground().getRGB()));
862 sf.begin = ( (Integer) start.getValue()).intValue();
863 sf.end = ( (Integer) end.getValue()).intValue();
865 catch (NumberFormatException ex)
868 ffile.parseDescriptionHTML(sf, false);
871 else //NEW FEATURES ADDED
873 if (reply == JOptionPane.OK_OPTION
874 && lastFeatureAdded.length()>0)
876 for (int i = 0; i < sequences.length; i++)
878 features[i].type = lastFeatureAdded;
879 if (lastFeatureGroupAdded!=null)
880 features[i].featureGroup = lastFeatureGroupAdded;
881 features[i].description = lastDescriptionAdded;
882 sequences[i].addSequenceFeature(features[i]);
883 ffile.parseDescriptionHTML(features[i], false);
886 if (av.featuresDisplayed == null)
888 av.featuresDisplayed = new Hashtable();
891 if (lastFeatureGroupAdded != null)
893 if (featureGroups == null)
894 featureGroups = new Hashtable();
895 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
898 Color col = colour.getBackground();
899 setColour(lastFeatureAdded, colour.getBackground());
900 av.featuresDisplayed.put(lastFeatureAdded,
901 new Integer(col.getRGB()));
903 findAllFeatures(false);
905 ap.paintAlignment(true);
916 ap.paintAlignment(true);
921 public void setColour(String featureType, Color col)
923 featureColours.put(featureType, col);
926 public void setTransparency(float value)
928 transparency = value;
931 public float getTransparency()
936 * Replace current ordering with new ordering
937 * @param data { String(Type), Colour(Type), Boolean(Displayed) }
939 public void setFeaturePriority(Object[][] data)
941 setFeaturePriority(data, true);
945 * @param data { String(Type), Colour(Type), Boolean(Displayed) }
946 * @param visibleNew when true current featureDisplay list will be cleared
948 public void setFeaturePriority(Object[][] data, boolean visibleNew)
952 if (av.featuresDisplayed != null)
954 av.featuresDisplayed.clear();
958 av.featuresDisplayed = new Hashtable();
966 // The feature table will display high priority
967 // features at the top, but theses are the ones
968 // we need to render last, so invert the data
969 renderOrder = new String[data.length];
973 for (int i = 0; i < data.length; i++)
975 String type = data[i][0].toString();
976 setColour(type, (Color) data[i][1]);
977 if ( ( (Boolean) data[i][2]).booleanValue())
979 av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
982 renderOrder[data.length - i - 1] = type;
987 Hashtable featureOrder=null;
989 * analogous to colour - store a normalized ordering for all feature types in
990 * this rendering context.
993 * Feature type string
995 * normalized priority - 0 means always appears on top, 1 means
998 public float setOrder(String type, float position)
1000 if (featureOrder==null)
1002 featureOrder = new Hashtable();
1004 featureOrder.put(type, new Float(position));
1008 * get the global priority (0 (top) to 1 (bottom))
1011 * @return [0,1] or -1 for a type without a priority
1013 public float getOrder(String type) {
1014 if (featureOrder!=null)
1016 if (featureOrder.containsKey(type))
1018 return ((Float)featureOrder.get(type)).floatValue();
1026 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
1028 public void addPropertyChangeListener(PropertyChangeListener listener)
1030 changeSupport.addPropertyChangeListener(listener);
1035 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1037 public void removePropertyChangeListener(PropertyChangeListener listener)
1039 changeSupport.removePropertyChangeListener(listener);