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 = null;
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)
76 public void transferSettings(FeatureRenderer 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;
85 BufferedImage offscreenImage;
86 boolean offscreenRender = false;
87 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
89 return new Color(findFeatureColour(initialCol.getRGB(),
94 * This is used by the Molecule Viewer and Overview to get the accurate
95 * colourof the rendered sequence
97 public int findFeatureColour(int initialCol, SequenceI seq, int column)
99 if (!av.showSequenceFeatures)
107 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
108 if (sequenceFeatures!=null)
110 sfSize = sequenceFeatures.length;
114 if (sequenceFeatures!=lastSeq.getDatasetSequence().getSequenceFeatures()) {
115 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
116 if (sequenceFeatures != null)
118 sfSize = sequenceFeatures.length;
122 if (sequenceFeatures == null)
128 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
130 return Color.white.getRGB();
133 // Only bother making an offscreen image if transparency is applied
134 if (transparency != 1.0f && offscreenImage == null)
136 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
139 currentColour = null;
141 offscreenRender = true;
143 if (offscreenImage != null)
145 offscreenImage.setRGB(0, 0, initialCol);
146 drawSequence(offscreenImage.getGraphics(),
150 return offscreenImage.getRGB(0, 0);
156 lastSeq.findPosition(column),
159 if (currentColour == null)
165 return ( (Integer) currentColour).intValue();
194 // SequenceFeature sf;
196 SequenceFeature[] sequenceFeatures;
197 int sfSize, sfindex, spos, epos;
199 public void drawSequence(Graphics g, SequenceI seq,
200 int start, int end, int y1)
202 if (seq.getDatasetSequence().getSequenceFeatures() == null
203 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
210 fm = g.getFontMetrics();
213 if (av.featuresDisplayed == null
214 || renderOrder == null
218 if (av.featuresDisplayed.size() < 1)
223 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
224 sfSize = sequenceFeatures.length;
227 if (lastSeq == null || seq != lastSeq
228 || seq.getDatasetSequence().getSequenceFeatures()!=sequenceFeatures)
231 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
232 sfSize = sequenceFeatures.length;
235 if (transparency != 1 && g != null)
237 Graphics2D g2 = (Graphics2D) g;
239 AlphaComposite.getInstance(
240 AlphaComposite.SRC_OVER, transparency));
243 if (!offscreenRender)
245 spos = lastSeq.findPosition(start);
246 epos = lastSeq.findPosition(end);
250 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
252 type = renderOrder[renderIndex];
254 if (type == null || !av.featuresDisplayed.containsKey(type))
259 // loop through all features in sequence to find
260 // current feature to render
261 for (sfindex = 0; sfindex < sfSize; sfindex++)
263 if (sequenceFeatures.length <= sfindex)
267 if (!sequenceFeatures[sfindex].type.equals(type))
272 if (featureGroups != null
273 && sequenceFeatures[sfindex].featureGroup != null
275 sequenceFeatures[sfindex].featureGroup.length()!=0
276 && featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
278 ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].
285 if (!offscreenRender && (sequenceFeatures[sfindex].getBegin() > epos
286 || sequenceFeatures[sfindex].getEnd() < spos))
291 if (offscreenRender && offscreenImage == null)
293 if (sequenceFeatures[sfindex].begin <= start &&
294 sequenceFeatures[sfindex].end >= start)
296 currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
300 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
303 renderFeature(g, seq,
304 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
305 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
306 new Color( ( (Integer) av.featuresDisplayed.get(
307 sequenceFeatures[sfindex].type)).intValue()),
309 renderFeature(g, seq,
310 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
311 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
312 new Color( ( (Integer) av.featuresDisplayed.get(
313 sequenceFeatures[sfindex].type)).intValue()),
319 renderFeature(g, seq,
320 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
321 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
322 getColour(sequenceFeatures[sfindex].type),
330 if (transparency != 1.0f && g != null)
332 Graphics2D g2 = (Graphics2D) g;
334 AlphaComposite.getInstance(
335 AlphaComposite.SRC_OVER, 1.0f));
341 void renderFeature(Graphics g, SequenceI seq,
342 int fstart, int fend, Color featureColour, int start,
346 if ( ( (fstart <= end) && (fend >= start)))
349 { // fix for if the feature we have starts before the sequence start,
350 fstart = start; // but the feature end is still valid!!
357 int pady = (y1 + av.charHeight) - av.charHeight / 5;
358 for (i = fstart; i <= fend; i++)
360 s = seq.getCharAt(i);
362 if (jalview.util.Comparison.isGap(s))
367 g.setColor(featureColour);
369 g.fillRect( (i - start) * av.charWidth, y1, av.charWidth, av.charHeight);
371 if (offscreenRender || !av.validCharWidth)
376 g.setColor(Color.white);
377 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
378 g.drawString(String.valueOf(s),
379 charOffset + (av.charWidth * (i - start)),
386 boolean newFeatureAdded = false;
388 * Called when alignment in associated view has new/modified features
389 * to discover and display.
392 public void featuresAdded()
398 boolean findingFeatures = false;
400 * search the alignment for all new features, give them a colour and display
401 * them. Then fires a PropertyChangeEvent on the changeSupport object.
404 synchronized void findAllFeatures()
406 findAllFeatures(true); // add all new features as visible
409 changeSupport.firePropertyChange("changeSupport",null,null);
414 * Searches alignment for all features and updates colours
416 * @param newMadeVisible
417 * if true newly added feature types will be rendered immediatly
419 synchronized void findAllFeatures(boolean newMadeVisible) {
420 newFeatureAdded = false;
424 newFeatureAdded = true;
428 findingFeatures = true;
430 if (av.featuresDisplayed == null)
432 av.featuresDisplayed = new Hashtable();
435 allfeatures = new Vector();
436 Vector oldfeatures = new Vector();
437 if (renderOrder!=null)
439 for (int i=0; i<renderOrder.length; i++) {
440 if (renderOrder[i]!=null)
442 oldfeatures.addElement(renderOrder[i]);
446 for (int i = 0; i < av.alignment.getHeight(); i++)
448 SequenceFeature[] features
449 = av.alignment.getSequenceAt(i).getDatasetSequence().
450 getSequenceFeatures();
452 if (features == null)
458 while (index < features.length)
460 if (!av.featuresDisplayed.containsKey(features[index].getType()))
462 if(featureGroups.containsKey(features[index].getType()))
464 boolean visible = ( (Boolean) featureGroups.get(
465 features[index].featureGroup)).booleanValue();
469 System.out.println(features[index].featureGroup
477 if (! (features[index].begin == 0 && features[index].end == 0))
479 // If beginning and end are 0, the feature is for the whole sequence
480 // and we don't want to render the feature in the normal way
482 if (newMadeVisible && !oldfeatures.contains(features[index].getType())) {
483 // this is a new feature type on the alignment. Mark it for
485 av.featuresDisplayed.put(features[index].getType(),
486 new Integer(getColour(features[index].
487 getType()).getRGB()));
488 setOrder(features[index].getType(),0);
492 if (!allfeatures.contains(features[index].getType()))
494 allfeatures.addElement(features[index].getType());
499 updateRenderOrder(allfeatures);
500 findingFeatures = false;
502 protected boolean firing=false;
504 * replaces the current renderOrder with the unordered features in allfeatures.
505 * The ordering of any types in both renderOrder and allfeatures is preserved,
506 * and all new feature types are rendered on top of the existing types, in
507 * the order given by getOrder or the order given in allFeatures.
508 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
509 * eliminate the float storage for computing/recalling the persistent ordering
513 private void updateRenderOrder(Vector allFeatures) {
514 Vector allfeatures = new Vector(allFeatures);
515 String[] oldRender = renderOrder;
516 renderOrder = new String[allfeatures.size()];
517 boolean initOrders=(featureOrder==null);
519 if (oldRender!=null && oldRender.length>0)
521 for (int j=0; j<oldRender.length; j++)
523 if (oldRender[j]!=null)
527 setOrder(oldRender[j], (1-(1+(float)j)/(float) oldRender.length));
529 if (allfeatures.contains(oldRender[j])) {
530 renderOrder[opos++] = oldRender[j]; // existing features always
531 // appear below new features
532 allfeatures.removeElement(oldRender[j]);
537 if (allfeatures.size()==0) {
538 // no new features - leave order unchanged.
541 int i=allfeatures.size()-1;
544 String[] newf = new String[allfeatures.size()];
545 float[] sortOrder = new float[allfeatures.size()];
546 Enumeration en = allfeatures.elements();
547 // sort remaining elements
548 while (en.hasMoreElements())
550 newf[i] = en.nextElement().toString();
551 if (initOrders || !featureOrder.containsKey(newf[i]))
553 int denom = initOrders ? allfeatures.size() : featureOrder.size();
554 // new unordered feature - compute persistent ordering at head of
555 // existing features.
556 setOrder(newf[i], i/(float) denom);
558 // set order from newly found feature from persisted ordering.
559 sortOrder[i] = 2-((Float) featureOrder.get(newf[i])).floatValue();
562 // only sort if we need to
563 sort = sort || sortOrder[i]>sortOrder[i+1];
568 jalview.util.QuickSort.sort(sortOrder, newf);
570 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
572 public Color getColour(String featureType)
574 if (!featureColours.containsKey(featureType))
576 jalview.schemes.UserColourScheme ucs = new
577 jalview.schemes.UserColourScheme();
578 Color col = ucs.createColourFromName(featureType);
579 featureColours.put(featureType, col);
583 return (Color) featureColours.get(featureType);
586 static String lastFeatureAdded;
587 static String lastFeatureGroupAdded;
588 static String lastDescriptionAdded;
590 int featureIndex = 0;
591 boolean amendFeatures(final SequenceI[] sequences,
592 final SequenceFeature[] features,
594 final AlignmentPanel ap)
599 JPanel bigPanel = new JPanel(new BorderLayout());
600 final JComboBox overlaps;
601 final JTextField name = new JTextField(25);
602 final JTextField source = new JTextField(25);
603 final JTextArea description = new JTextArea(3, 25);
604 final JSpinner start = new JSpinner();
605 final JSpinner end = new JSpinner();
606 start.setPreferredSize(new Dimension(80, 20));
607 end.setPreferredSize(new Dimension(80, 20));
609 final JPanel colour = new JPanel();
610 colour.setBorder(BorderFactory.createEtchedBorder());
611 colour.setMaximumSize(new Dimension(40, 10));
612 colour.addMouseListener(new MouseAdapter()
614 public void mousePressed(MouseEvent evt)
616 Color col = JColorChooser.showDialog(Desktop.desktop,
617 "Select Feature Colour",
618 colour.getBackground());
620 colour.setBackground(col);
625 JPanel tmp = new JPanel();
626 JPanel panel = new JPanel(new GridLayout(3, 1));
628 ///////////////////////////////////////
629 ///MULTIPLE FEATURES AT SELECTED RESIDUE
630 if(!newFeatures && features.length>1)
632 panel = new JPanel(new GridLayout(4, 1));
634 tmp.add(new JLabel("Select Feature: "));
635 overlaps = new JComboBox();
636 for(int i=0; i<features.length; i++)
638 overlaps.addItem(features[i].getType()
639 +"/"+features[i].getBegin()+"-"+features[i].getEnd()
640 +" ("+features[i].getFeatureGroup()+")");
645 overlaps.addItemListener(new ItemListener()
647 public void itemStateChanged(ItemEvent e)
649 int index = overlaps.getSelectedIndex();
652 featureIndex = index;
653 name.setText(features[index].getType());
654 description.setText(features[index].getDescription());
655 source.setText(features[index].getFeatureGroup());
656 start.setValue(new Integer(features[index].getBegin()));
657 end.setValue(new Integer(features[index].getEnd()));
659 SearchResults highlight = new SearchResults();
660 highlight.addResult(sequences[0],
661 features[index].getBegin(),
662 features[index].getEnd());
664 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
667 Color col = getColour(name.getText());
671 jalview.schemes.UserColourScheme()
672 .createColourFromName(name.getText());
675 colour.setBackground(col);
683 //////////////////////////////////////
687 tmp.add(new JLabel("Name: ", JLabel.RIGHT));
692 tmp.add(new JLabel("Group: ", JLabel.RIGHT));
697 tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
699 colour.setPreferredSize(new Dimension(150, 15));
701 bigPanel.add(panel, BorderLayout.NORTH);
703 panel = new JPanel();
704 panel.add(new JLabel("Description: ", JLabel.RIGHT));
705 description.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
706 description.setLineWrap(true);
707 panel.add(new JScrollPane(description));
711 bigPanel.add(panel, BorderLayout.SOUTH);
713 panel = new JPanel();
714 panel.add(new JLabel(" Start:", JLabel.RIGHT));
716 panel.add(new JLabel(" End:", JLabel.RIGHT));
718 bigPanel.add(panel, BorderLayout.CENTER);
722 bigPanel.add(panel, BorderLayout.CENTER);
725 if (lastFeatureAdded == null)
727 if (features[0].type != null)
729 lastFeatureAdded = features[0].type;
733 lastFeatureAdded = "feature_1";
737 if (lastFeatureGroupAdded == null)
739 if (features[0].featureGroup != null)
741 lastFeatureGroupAdded = features[0].featureGroup;
745 lastFeatureGroupAdded = "Jalview";
751 name.setText(lastFeatureAdded);
752 source.setText(lastFeatureGroupAdded);
756 name.setText(features[0].getType());
757 source.setText(features[0].getFeatureGroup());
760 start.setValue(new Integer(features[0].getBegin()));
761 end.setValue(new Integer(features[0].getEnd()));
762 description.setText(features[0].getDescription());
763 colour.setBackground(getColour(name.getText()));
769 options = new Object[]
771 "Amend", "Delete", "Cancel"};
775 options = new Object[]
780 String title = newFeatures ? "Create New Sequence Feature(s)" :
781 "Amend/Delete Features for "
782 + sequences[0].getName();
784 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
787 JOptionPane.YES_NO_CANCEL_OPTION,
788 JOptionPane.QUESTION_MESSAGE,
792 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
794 if (reply == JOptionPane.OK_OPTION && name.getText().length()>0)
796 // This ensures that the last sequence
797 // is refreshed and new features are rendered
799 lastFeatureAdded = name.getText().trim();
800 lastFeatureGroupAdded = source.getText().trim();
801 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
803 if(lastFeatureGroupAdded.length()<1)
804 lastFeatureGroupAdded = null;
809 SequenceFeature sf = features[featureIndex];
811 if (reply == JOptionPane.NO_OPTION)
813 sequences[0].getDatasetSequence().deleteFeature(sf);
815 else if (reply == JOptionPane.YES_OPTION)
817 sf.type = lastFeatureAdded;
818 sf.featureGroup = lastFeatureGroupAdded;
819 sf.description = lastDescriptionAdded;
821 setColour(sf.type, colour.getBackground());
822 av.featuresDisplayed.put(sf.type,
823 new Integer(colour.getBackground().getRGB()));
827 sf.begin = ( (Integer) start.getValue()).intValue();
828 sf.end = ( (Integer) end.getValue()).intValue();
830 catch (NumberFormatException ex)
833 ffile.parseDescriptionHTML(sf, false);
836 else //NEW FEATURES ADDED
838 if (reply == JOptionPane.OK_OPTION
839 && lastFeatureAdded.length()>0)
841 for (int i = 0; i < sequences.length; i++)
843 features[i].type = lastFeatureAdded;
844 if (lastFeatureGroupAdded!=null)
845 features[i].featureGroup = lastFeatureGroupAdded;
846 features[i].description = lastDescriptionAdded;
847 sequences[i].addSequenceFeature(features[i]);
848 ffile.parseDescriptionHTML(features[i], false);
851 if (av.featuresDisplayed == null)
853 av.featuresDisplayed = new Hashtable();
856 if (lastFeatureGroupAdded != null)
858 if (featureGroups == null)
859 featureGroups = new Hashtable();
860 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
863 Color col = colour.getBackground();
864 setColour(lastFeatureAdded, colour.getBackground());
865 av.featuresDisplayed.put(lastFeatureAdded,
866 new Integer(col.getRGB()));
868 findAllFeatures(false);
870 ap.paintAlignment(true);
881 ap.paintAlignment(true);
886 public void setColour(String featureType, Color col)
888 featureColours.put(featureType, col);
891 public void setTransparency(float value)
893 transparency = value;
896 public float getTransparency()
901 * Replace current ordering with new ordering
902 * @param data { String(Type), Colour(Type), Boolean(Displayed) }
904 public void setFeaturePriority(Object[][] data)
906 setFeaturePriority(data, true);
910 * @param data { String(Type), Colour(Type), Boolean(Displayed) }
911 * @param visibleNew when true current featureDisplay list will be cleared
913 public void setFeaturePriority(Object[][] data, boolean visibleNew)
917 if (av.featuresDisplayed != null)
919 av.featuresDisplayed.clear();
923 av.featuresDisplayed = new Hashtable();
931 // The feature table will display high priority
932 // features at the top, but theses are the ones
933 // we need to render last, so invert the data
934 renderOrder = new String[data.length];
938 for (int i = 0; i < data.length; i++)
940 String type = data[i][0].toString();
941 setColour(type, (Color) data[i][1]);
942 if ( ( (Boolean) data[i][2]).booleanValue())
944 av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
947 renderOrder[data.length - i - 1] = type;
952 Hashtable featureOrder=null;
954 * analogous to colour - store a normalized ordering for all feature types in
955 * this rendering context.
958 * Feature type string
960 * normalized priority - 0 means always appears on top, 1 means
963 public float setOrder(String type, float position)
965 if (featureOrder==null)
967 featureOrder = new Hashtable();
969 featureOrder.put(type, new Float(position));
973 * get the global priority (0 (top) to 1 (bottom))
976 * @return [0,1] or -1 for a type without a priority
978 public float getOrder(String type) {
979 if (featureOrder!=null)
981 if (featureOrder.containsKey(type))
983 return ((Float)featureOrder.get(type)).floatValue();
991 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
993 public void addPropertyChangeListener(PropertyChangeListener listener)
995 changeSupport.addPropertyChangeListener(listener);
1000 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
1002 public void removePropertyChangeListener(PropertyChangeListener listener)
1004 changeSupport.removePropertyChangeListener(listener);