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 || sfSize==0)
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 synchronized public void drawSequence(Graphics g, SequenceI seq,
200 int start, int end, int y1)
203 if (seq.getDatasetSequence().getSequenceFeatures() == null
204 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
211 fm = g.getFontMetrics();
214 if (av.featuresDisplayed == null
215 || renderOrder == null
219 if (av.featuresDisplayed.size() < 1)
224 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
227 if (lastSeq == null || seq != lastSeq
228 || seq.getDatasetSequence().getSequenceFeatures()!=sequenceFeatures)
231 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
234 if (transparency != 1 && g != null)
236 Graphics2D g2 = (Graphics2D) g;
238 AlphaComposite.getInstance(
239 AlphaComposite.SRC_OVER, transparency));
242 if (!offscreenRender)
244 spos = lastSeq.findPosition(start);
245 epos = lastSeq.findPosition(end);
248 sfSize = sequenceFeatures.length;
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[sfindex].type.equals(type))
268 if (featureGroups != null
269 && sequenceFeatures[sfindex].featureGroup != null
271 sequenceFeatures[sfindex].featureGroup.length()!=0
272 && featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
274 ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].
281 if (!offscreenRender && (sequenceFeatures[sfindex].getBegin() > epos
282 || sequenceFeatures[sfindex].getEnd() < spos))
287 if (offscreenRender && offscreenImage == null)
289 if (sequenceFeatures[sfindex].begin <= start &&
290 sequenceFeatures[sfindex].end >= start)
292 currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
296 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
299 renderFeature(g, seq,
300 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
301 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
302 new Color( ( (Integer) av.featuresDisplayed.get(
303 sequenceFeatures[sfindex].type)).intValue()),
305 renderFeature(g, seq,
306 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
307 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
308 new Color( ( (Integer) av.featuresDisplayed.get(
309 sequenceFeatures[sfindex].type)).intValue()),
315 renderFeature(g, seq,
316 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
317 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
318 getColour(sequenceFeatures[sfindex].type),
326 if (transparency != 1.0f && g != null)
328 Graphics2D g2 = (Graphics2D) g;
330 AlphaComposite.getInstance(
331 AlphaComposite.SRC_OVER, 1.0f));
337 void renderFeature(Graphics g, SequenceI seq,
338 int fstart, int fend, Color featureColour, int start,
342 if ( ( (fstart <= end) && (fend >= start)))
345 { // fix for if the feature we have starts before the sequence start,
346 fstart = start; // but the feature end is still valid!!
353 int pady = (y1 + av.charHeight) - av.charHeight / 5;
354 for (i = fstart; i <= fend; i++)
356 s = seq.getCharAt(i);
358 if (jalview.util.Comparison.isGap(s))
363 g.setColor(featureColour);
365 g.fillRect( (i - start) * av.charWidth, y1, av.charWidth, av.charHeight);
367 if (offscreenRender || !av.validCharWidth)
372 g.setColor(Color.white);
373 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
374 g.drawString(String.valueOf(s),
375 charOffset + (av.charWidth * (i - start)),
382 boolean newFeatureAdded = false;
384 * Called when alignment in associated view has new/modified features
385 * to discover and display.
388 public void featuresAdded()
394 boolean findingFeatures = false;
396 * search the alignment for all new features, give them a colour and display
397 * them. Then fires a PropertyChangeEvent on the changeSupport object.
400 synchronized void findAllFeatures()
402 findAllFeatures(true); // add all new features as visible
405 changeSupport.firePropertyChange("changeSupport",null,null);
410 * Searches alignment for all features and updates colours
412 * @param newMadeVisible
413 * if true newly added feature types will be rendered immediatly
415 synchronized void findAllFeatures(boolean newMadeVisible) {
416 newFeatureAdded = false;
420 newFeatureAdded = true;
424 findingFeatures = true;
426 if (av.featuresDisplayed == null)
428 av.featuresDisplayed = new Hashtable();
431 allfeatures = new Vector();
432 Vector oldfeatures = new Vector();
433 if (renderOrder!=null)
435 for (int i=0; i<renderOrder.length; i++) {
436 if (renderOrder[i]!=null)
438 oldfeatures.addElement(renderOrder[i]);
442 for (int i = 0; i < av.alignment.getHeight(); i++)
444 SequenceFeature[] features
445 = av.alignment.getSequenceAt(i).getDatasetSequence().
446 getSequenceFeatures();
448 if (features == null)
454 while (index < features.length)
456 if (!av.featuresDisplayed.containsKey(features[index].getType()))
458 if(featureGroups.containsKey(features[index].getType()))
460 boolean visible = ( (Boolean) featureGroups.get(
461 features[index].featureGroup)).booleanValue();
465 System.out.println(features[index].featureGroup
473 if (! (features[index].begin == 0 && features[index].end == 0))
475 // If beginning and end are 0, the feature is for the whole sequence
476 // and we don't want to render the feature in the normal way
478 if (newMadeVisible && !oldfeatures.contains(features[index].getType())) {
479 // this is a new feature type on the alignment. Mark it for
481 av.featuresDisplayed.put(features[index].getType(),
482 new Integer(getColour(features[index].
483 getType()).getRGB()));
484 setOrder(features[index].getType(),0);
488 if (!allfeatures.contains(features[index].getType()))
490 allfeatures.addElement(features[index].getType());
495 updateRenderOrder(allfeatures);
496 findingFeatures = false;
498 protected boolean firing=false;
500 * replaces the current renderOrder with the unordered features in allfeatures.
501 * The ordering of any types in both renderOrder and allfeatures is preserved,
502 * and all new feature types are rendered on top of the existing types, in
503 * the order given by getOrder or the order given in allFeatures.
504 * Note. this operates directly on the featureOrder hash for efficiency. TODO:
505 * eliminate the float storage for computing/recalling the persistent ordering
509 private void updateRenderOrder(Vector allFeatures) {
510 Vector allfeatures = new Vector(allFeatures);
511 String[] oldRender = renderOrder;
512 renderOrder = new String[allfeatures.size()];
513 boolean initOrders=(featureOrder==null);
515 if (oldRender!=null && oldRender.length>0)
517 for (int j=0; j<oldRender.length; j++)
519 if (oldRender[j]!=null)
523 setOrder(oldRender[j], (1-(1+(float)j)/(float) oldRender.length));
525 if (allfeatures.contains(oldRender[j])) {
526 renderOrder[opos++] = oldRender[j]; // existing features always
527 // appear below new features
528 allfeatures.removeElement(oldRender[j]);
533 if (allfeatures.size()==0) {
534 // no new features - leave order unchanged.
537 int i=allfeatures.size()-1;
540 String[] newf = new String[allfeatures.size()];
541 float[] sortOrder = new float[allfeatures.size()];
542 Enumeration en = allfeatures.elements();
543 // sort remaining elements
544 while (en.hasMoreElements())
546 newf[i] = en.nextElement().toString();
547 if (initOrders || !featureOrder.containsKey(newf[i]))
549 int denom = initOrders ? allfeatures.size() : featureOrder.size();
550 // new unordered feature - compute persistent ordering at head of
551 // existing features.
552 setOrder(newf[i], i/(float) denom);
554 // set order from newly found feature from persisted ordering.
555 sortOrder[i] = 2-((Float) featureOrder.get(newf[i])).floatValue();
558 // only sort if we need to
559 sort = sort || sortOrder[i]>sortOrder[i+1];
564 jalview.util.QuickSort.sort(sortOrder, newf);
566 System.arraycopy(newf, 0, renderOrder, opos, newf.length);
568 public Color getColour(String featureType)
570 if (!featureColours.containsKey(featureType))
572 jalview.schemes.UserColourScheme ucs = new
573 jalview.schemes.UserColourScheme();
574 Color col = ucs.createColourFromName(featureType);
575 featureColours.put(featureType, col);
579 return (Color) featureColours.get(featureType);
582 static String lastFeatureAdded;
583 static String lastFeatureGroupAdded;
584 static String lastDescriptionAdded;
586 int featureIndex = 0;
587 boolean amendFeatures(final SequenceI[] sequences,
588 final SequenceFeature[] features,
590 final AlignmentPanel ap)
595 JPanel bigPanel = new JPanel(new BorderLayout());
596 final JComboBox overlaps;
597 final JTextField name = new JTextField(25);
598 final JTextField source = new JTextField(25);
599 final JTextArea description = new JTextArea(3, 25);
600 final JSpinner start = new JSpinner();
601 final JSpinner end = new JSpinner();
602 start.setPreferredSize(new Dimension(80, 20));
603 end.setPreferredSize(new Dimension(80, 20));
605 final JPanel colour = new JPanel();
606 colour.setBorder(BorderFactory.createEtchedBorder());
607 colour.setMaximumSize(new Dimension(40, 10));
608 colour.addMouseListener(new MouseAdapter()
610 public void mousePressed(MouseEvent evt)
612 Color col = JColorChooser.showDialog(Desktop.desktop,
613 "Select Feature Colour",
614 colour.getBackground());
616 colour.setBackground(col);
621 JPanel tmp = new JPanel();
622 JPanel panel = new JPanel(new GridLayout(3, 1));
624 ///////////////////////////////////////
625 ///MULTIPLE FEATURES AT SELECTED RESIDUE
626 if(!newFeatures && features.length>1)
628 panel = new JPanel(new GridLayout(4, 1));
630 tmp.add(new JLabel("Select Feature: "));
631 overlaps = new JComboBox();
632 for(int i=0; i<features.length; i++)
634 overlaps.addItem(features[i].getType()
635 +"/"+features[i].getBegin()+"-"+features[i].getEnd()
636 +" ("+features[i].getFeatureGroup()+")");
641 overlaps.addItemListener(new ItemListener()
643 public void itemStateChanged(ItemEvent e)
645 int index = overlaps.getSelectedIndex();
648 featureIndex = index;
649 name.setText(features[index].getType());
650 description.setText(features[index].getDescription());
651 source.setText(features[index].getFeatureGroup());
652 start.setValue(new Integer(features[index].getBegin()));
653 end.setValue(new Integer(features[index].getEnd()));
655 SearchResults highlight = new SearchResults();
656 highlight.addResult(sequences[0],
657 features[index].getBegin(),
658 features[index].getEnd());
660 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
663 Color col = getColour(name.getText());
667 jalview.schemes.UserColourScheme()
668 .createColourFromName(name.getText());
671 colour.setBackground(col);
679 //////////////////////////////////////
683 tmp.add(new JLabel("Name: ", JLabel.RIGHT));
688 tmp.add(new JLabel("Group: ", JLabel.RIGHT));
693 tmp.add(new JLabel("Colour: ", JLabel.RIGHT));
695 colour.setPreferredSize(new Dimension(150, 15));
697 bigPanel.add(panel, BorderLayout.NORTH);
699 panel = new JPanel();
700 panel.add(new JLabel("Description: ", JLabel.RIGHT));
701 description.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
702 description.setLineWrap(true);
703 panel.add(new JScrollPane(description));
707 bigPanel.add(panel, BorderLayout.SOUTH);
709 panel = new JPanel();
710 panel.add(new JLabel(" Start:", JLabel.RIGHT));
712 panel.add(new JLabel(" End:", JLabel.RIGHT));
714 bigPanel.add(panel, BorderLayout.CENTER);
718 bigPanel.add(panel, BorderLayout.CENTER);
721 if (lastFeatureAdded == null)
723 if (features[0].type != null)
725 lastFeatureAdded = features[0].type;
729 lastFeatureAdded = "feature_1";
733 if (lastFeatureGroupAdded == null)
735 if (features[0].featureGroup != null)
737 lastFeatureGroupAdded = features[0].featureGroup;
741 lastFeatureGroupAdded = "Jalview";
747 name.setText(lastFeatureAdded);
748 source.setText(lastFeatureGroupAdded);
752 name.setText(features[0].getType());
753 source.setText(features[0].getFeatureGroup());
756 start.setValue(new Integer(features[0].getBegin()));
757 end.setValue(new Integer(features[0].getEnd()));
758 description.setText(features[0].getDescription());
759 colour.setBackground(getColour(name.getText()));
765 options = new Object[]
767 "Amend", "Delete", "Cancel"};
771 options = new Object[]
776 String title = newFeatures ? "Create New Sequence Feature(s)" :
777 "Amend/Delete Features for "
778 + sequences[0].getName();
780 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
783 JOptionPane.YES_NO_CANCEL_OPTION,
784 JOptionPane.QUESTION_MESSAGE,
788 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
790 if (reply == JOptionPane.OK_OPTION && name.getText().length()>0)
792 // This ensures that the last sequence
793 // is refreshed and new features are rendered
795 lastFeatureAdded = name.getText().trim();
796 lastFeatureGroupAdded = source.getText().trim();
797 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
799 if(lastFeatureGroupAdded.length()<1)
800 lastFeatureGroupAdded = null;
805 SequenceFeature sf = features[featureIndex];
807 if (reply == JOptionPane.NO_OPTION)
809 sequences[0].getDatasetSequence().deleteFeature(sf);
811 else if (reply == JOptionPane.YES_OPTION)
813 sf.type = lastFeatureAdded;
814 sf.featureGroup = lastFeatureGroupAdded;
815 sf.description = lastDescriptionAdded;
817 setColour(sf.type, colour.getBackground());
818 av.featuresDisplayed.put(sf.type,
819 new Integer(colour.getBackground().getRGB()));
823 sf.begin = ( (Integer) start.getValue()).intValue();
824 sf.end = ( (Integer) end.getValue()).intValue();
826 catch (NumberFormatException ex)
829 ffile.parseDescriptionHTML(sf, false);
832 else //NEW FEATURES ADDED
834 if (reply == JOptionPane.OK_OPTION
835 && lastFeatureAdded.length()>0)
837 for (int i = 0; i < sequences.length; i++)
839 features[i].type = lastFeatureAdded;
840 if (lastFeatureGroupAdded!=null)
841 features[i].featureGroup = lastFeatureGroupAdded;
842 features[i].description = lastDescriptionAdded;
843 sequences[i].addSequenceFeature(features[i]);
844 ffile.parseDescriptionHTML(features[i], false);
847 if (av.featuresDisplayed == null)
849 av.featuresDisplayed = new Hashtable();
852 if (lastFeatureGroupAdded != null)
854 if (featureGroups == null)
855 featureGroups = new Hashtable();
856 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
859 Color col = colour.getBackground();
860 setColour(lastFeatureAdded, colour.getBackground());
861 av.featuresDisplayed.put(lastFeatureAdded,
862 new Integer(col.getRGB()));
864 findAllFeatures(false);
866 ap.paintAlignment(true);
877 ap.paintAlignment(true);
882 public void setColour(String featureType, Color col)
884 featureColours.put(featureType, col);
887 public void setTransparency(float value)
889 transparency = value;
892 public float getTransparency()
897 * Replace current ordering with new ordering
898 * @param data { String(Type), Colour(Type), Boolean(Displayed) }
900 public void setFeaturePriority(Object[][] data)
902 setFeaturePriority(data, true);
906 * @param data { String(Type), Colour(Type), Boolean(Displayed) }
907 * @param visibleNew when true current featureDisplay list will be cleared
909 public void setFeaturePriority(Object[][] data, boolean visibleNew)
913 if (av.featuresDisplayed != null)
915 av.featuresDisplayed.clear();
919 av.featuresDisplayed = new Hashtable();
927 // The feature table will display high priority
928 // features at the top, but theses are the ones
929 // we need to render last, so invert the data
930 renderOrder = new String[data.length];
934 for (int i = 0; i < data.length; i++)
936 String type = data[i][0].toString();
937 setColour(type, (Color) data[i][1]);
938 if ( ( (Boolean) data[i][2]).booleanValue())
940 av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
943 renderOrder[data.length - i - 1] = type;
948 Hashtable featureOrder=null;
950 * analogous to colour - store a normalized ordering for all feature types in
951 * this rendering context.
954 * Feature type string
956 * normalized priority - 0 means always appears on top, 1 means
959 public float setOrder(String type, float position)
961 if (featureOrder==null)
963 featureOrder = new Hashtable();
965 featureOrder.put(type, new Float(position));
969 * get the global priority (0 (top) to 1 (bottom))
972 * @return [0,1] or -1 for a type without a priority
974 public float getOrder(String type) {
975 if (featureOrder!=null)
977 if (featureOrder.containsKey(type))
979 return ((Float)featureOrder.get(type)).floatValue();
987 * @see java.beans.PropertyChangeSupport#addPropertyChangeListener(java.beans.PropertyChangeListener)
989 public void addPropertyChangeListener(PropertyChangeListener listener)
991 changeSupport.addPropertyChangeListener(listener);
996 * @see java.beans.PropertyChangeSupport#removePropertyChangeListener(java.beans.PropertyChangeListener)
998 public void removePropertyChangeListener(PropertyChangeListener listener)
1000 changeSupport.removePropertyChangeListener(listener);