2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2007 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
5 * This program is free software; you can redistribute it and/or
\r
6 * modify it under the terms of the GNU General Public License
\r
7 * as published by the Free Software Foundation; either version 2
\r
8 * of the License, or (at your option) any later version.
\r
10 * This program is distributed in the hope that it will be useful,
\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
13 * GNU General Public License for more details.
\r
15 * You should have received a copy of the GNU General Public License
\r
16 * along with this program; if not, write to the Free Software
\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
19 package jalview.gui;
\r
24 import java.awt.event.*;
\r
25 import java.awt.image.*;
\r
26 import javax.swing.*;
\r
28 import jalview.datamodel.*;
\r
34 * @version $Revision$
\r
36 public class FeatureRenderer
\r
40 float transparency = 1.0f;
\r
44 Hashtable featureColours = new Hashtable();
\r
46 // A higher level for grouping features of a
\r
48 Hashtable featureGroups = null;
\r
50 // This is actually an Integer held in the hashtable,
\r
51 // Retrieved using the key feature type
\r
52 Object currentColour;
\r
54 String[] renderOrder;
\r
57 * Creates a new FeatureRenderer object.
\r
59 * @param av DOCUMENT ME!
\r
61 public FeatureRenderer(AlignViewport av)
\r
66 public void transferSettings(FeatureRenderer fr)
\r
68 renderOrder = fr.renderOrder;
\r
69 featureGroups = fr.featureGroups;
\r
70 featureColours = fr.featureColours;
\r
71 transparency = fr.transparency;
\r
74 BufferedImage offscreenImage;
\r
75 boolean offscreenRender = false;
\r
76 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
\r
78 return new Color(findFeatureColour(initialCol.getRGB(),
\r
83 * This is used by the Molecule Viewer and Overview to
\r
84 * get the accurate colourof the rendered sequence
\r
86 public int findFeatureColour(int initialCol, SequenceI seq, int column)
\r
88 if (!av.showSequenceFeatures)
\r
96 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
\r
97 if (sequenceFeatures == null)
\r
102 sfSize = sequenceFeatures.length;
\r
105 if (jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
\r
107 return Color.white.getRGB();
\r
110 //Only bother making an offscreen image if transparency is applied
\r
111 if (transparency != 1.0f && offscreenImage == null)
\r
113 offscreenImage = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
\r
116 currentColour = null;
\r
118 offscreenRender = true;
\r
120 if (offscreenImage != null)
\r
122 offscreenImage.setRGB(0, 0, initialCol);
\r
123 drawSequence(offscreenImage.getGraphics(),
\r
125 column, column, 0);
\r
127 return offscreenImage.getRGB(0, 0);
\r
133 lastSeq.findPosition(column),
\r
136 if (currentColour == null)
\r
142 return ( (Integer) currentColour).intValue();
\r
151 * @param g DOCUMENT ME!
\r
152 * @param seq DOCUMENT ME!
\r
153 * @param sg DOCUMENT ME!
\r
154 * @param start DOCUMENT ME!
\r
155 * @param end DOCUMENT ME!
\r
156 * @param x1 DOCUMENT ME!
\r
157 * @param y1 DOCUMENT ME!
\r
158 * @param width DOCUMENT ME!
\r
159 * @param height DOCUMENT ME!
\r
162 // SequenceFeature sf;
\r
164 SequenceFeature[] sequenceFeatures;
\r
165 int sfSize, sfindex, spos, epos;
\r
167 public void drawSequence(Graphics g, SequenceI seq,
\r
168 int start, int end, int y1)
\r
170 if (seq.getDatasetSequence().getSequenceFeatures() == null
\r
171 || seq.getDatasetSequence().getSequenceFeatures().length == 0)
\r
178 fm = g.getFontMetrics();
\r
181 if (av.featuresDisplayed == null
\r
182 || renderOrder == null
\r
183 || newFeatureAdded)
\r
186 if (av.featuresDisplayed.size() < 1)
\r
191 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
\r
192 sfSize = sequenceFeatures.length;
\r
195 if (lastSeq == null || seq != lastSeq)
\r
198 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
\r
199 sfSize = sequenceFeatures.length;
\r
202 if (transparency != 1 && g != null)
\r
204 Graphics2D g2 = (Graphics2D) g;
\r
206 AlphaComposite.getInstance(
\r
207 AlphaComposite.SRC_OVER, transparency));
\r
210 if (!offscreenRender)
\r
212 spos = lastSeq.findPosition(start);
\r
213 epos = lastSeq.findPosition(end);
\r
217 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
\r
219 type = renderOrder[renderIndex];
\r
221 if (type == null || !av.featuresDisplayed.containsKey(type))
\r
226 // loop through all features in sequence to find
\r
227 // current feature to render
\r
228 for (sfindex = 0; sfindex < sfSize; sfindex++)
\r
230 if (sequenceFeatures.length <= sfindex)
\r
234 if (!sequenceFeatures[sfindex].type.equals(type))
\r
239 if (featureGroups != null
\r
240 && sequenceFeatures[sfindex].featureGroup != null
\r
242 featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
\r
244 ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].
\r
251 if (!offscreenRender && (sequenceFeatures[sfindex].getBegin() > epos
\r
252 || sequenceFeatures[sfindex].getEnd() < spos))
\r
257 if (offscreenRender && offscreenImage == null)
\r
259 if (sequenceFeatures[sfindex].begin <= start &&
\r
260 sequenceFeatures[sfindex].end >= start)
\r
262 currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
\r
266 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
\r
269 renderFeature(g, seq,
\r
270 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
271 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
272 new Color( ( (Integer) av.featuresDisplayed.get(
\r
273 sequenceFeatures[sfindex].type)).intValue()),
\r
275 renderFeature(g, seq,
\r
276 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
277 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
278 new Color( ( (Integer) av.featuresDisplayed.get(
\r
279 sequenceFeatures[sfindex].type)).intValue()),
\r
285 renderFeature(g, seq,
\r
286 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
287 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
288 getColour(sequenceFeatures[sfindex].type),
\r
296 if (transparency != 1.0f && g != null)
\r
298 Graphics2D g2 = (Graphics2D) g;
\r
300 AlphaComposite.getInstance(
\r
301 AlphaComposite.SRC_OVER, 1.0f));
\r
307 void renderFeature(Graphics g, SequenceI seq,
\r
308 int fstart, int fend, Color featureColour, int start,
\r
312 if ( ( (fstart <= end) && (fend >= start)))
\r
314 if (fstart < start)
\r
315 { // fix for if the feature we have starts before the sequence start,
\r
316 fstart = start; // but the feature end is still valid!!
\r
323 int pady = (y1 + av.charHeight) - av.charHeight / 5;
\r
324 for (i = fstart; i <= fend; i++)
\r
326 s = seq.getCharAt(i);
\r
328 if (jalview.util.Comparison.isGap(s))
\r
333 g.setColor(featureColour);
\r
335 g.fillRect( (i - start) * av.charWidth, y1, av.charWidth, av.charHeight);
\r
337 if (offscreenRender || !av.validCharWidth)
\r
342 g.setColor(Color.white);
\r
343 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
\r
344 g.drawString(String.valueOf(s),
\r
345 charOffset + (av.charWidth * (i - start)),
\r
352 boolean newFeatureAdded = false;
\r
354 public void featuresAdded()
\r
359 boolean findingFeatures = false;
\r
360 synchronized void findAllFeatures()
\r
362 newFeatureAdded = false;
\r
364 if (findingFeatures)
\r
366 newFeatureAdded = true;
\r
370 findingFeatures = true;
\r
371 jalview.schemes.UserColourScheme ucs = new
\r
372 jalview.schemes.UserColourScheme();
\r
374 if (av.featuresDisplayed == null)
\r
376 av.featuresDisplayed = new Hashtable();
\r
379 av.featuresDisplayed.clear();
\r
381 Vector allfeatures = new Vector();
\r
382 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
384 SequenceFeature[] features
\r
385 = av.alignment.getSequenceAt(i).getDatasetSequence().
\r
386 getSequenceFeatures();
\r
388 if (features == null)
\r
394 while (index < features.length)
\r
396 if (!av.featuresDisplayed.containsKey(features[index].getType()))
\r
398 if (! (features[index].begin == 0 && features[index].end == 0))
\r
400 // If beginning and end are 0, the feature is for the whole sequence
\r
401 // and we don't want to render the feature in the normal way
\r
403 if (getColour(features[index].getType()) == null)
\r
405 featureColours.put(features[index].getType(),
\r
406 ucs.createColourFromName(features[index].
\r
410 av.featuresDisplayed.put(features[index].getType(),
\r
411 new Integer(getColour(features[index].
\r
412 getType()).getRGB()));
\r
413 allfeatures.addElement(features[index].getType());
\r
420 renderOrder = new String[allfeatures.size()];
\r
421 Enumeration en = allfeatures.elements();
\r
422 int i = allfeatures.size() - 1;
\r
423 while (en.hasMoreElements())
\r
425 renderOrder[i] = en.nextElement().toString();
\r
429 findingFeatures = false;
\r
432 public Color getColour(String featureType)
\r
434 Color colour = (Color) featureColours.get(featureType);
\r
438 static String lastFeatureAdded;
\r
439 static String lastFeatureGroupAdded;
\r
440 static String lastDescriptionAdded;
\r
442 public boolean createNewFeatures(SequenceI[] sequences,
\r
443 SequenceFeature[] features)
\r
445 return amendFeatures(sequences, features, true, null);
\r
448 int featureIndex = 0;
\r
449 boolean amendFeatures(final SequenceI[] sequences,
\r
450 final SequenceFeature[] features,
\r
451 boolean newFeatures,
\r
452 final AlignmentPanel ap)
\r
454 JPanel bigPanel = new JPanel(new BorderLayout());
\r
455 final JComboBox name = new JComboBox();
\r
456 final JComboBox source = new JComboBox();
\r
457 final JTextArea description = new JTextArea(3, 25);
\r
458 final JSpinner start = new JSpinner();
\r
459 final JSpinner end = new JSpinner();
\r
460 start.setPreferredSize(new Dimension(80, 20));
\r
461 end.setPreferredSize(new Dimension(80, 20));
\r
462 final JPanel colour = new JPanel();
\r
463 colour.setBorder(BorderFactory.createEtchedBorder());
\r
464 colour.setMaximumSize(new Dimension(40, 10));
\r
465 colour.addMouseListener(new MouseAdapter()
\r
467 public void mousePressed(MouseEvent evt)
\r
469 colour.setBackground(
\r
470 JColorChooser.showDialog(Desktop.desktop,
\r
471 "Select Feature Colour",
\r
472 colour.getBackground()));
\r
476 JPanel panel = new JPanel(new GridLayout(3, 2));
\r
477 panel.add(new JLabel("Sequence Feature Name: ", JLabel.RIGHT));
\r
479 panel.add(new JLabel("Feature Group: ", JLabel.RIGHT));
\r
481 panel.add(new JLabel("Feature Colour: ", JLabel.RIGHT));
\r
482 JPanel tmp = new JPanel();
\r
484 colour.setPreferredSize(new Dimension(150, 15));
\r
486 name.setEditable(true);
\r
487 source.setEditable(true);
\r
489 bigPanel.add(panel, BorderLayout.NORTH);
\r
490 panel = new JPanel();
\r
491 panel.add(new JLabel("Description: ", JLabel.RIGHT));
\r
492 description.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
\r
493 description.setLineWrap(true);
\r
494 panel.add(new JScrollPane(description));
\r
498 bigPanel.add(panel, BorderLayout.SOUTH);
\r
500 panel = new JPanel();
\r
501 panel.add(new JLabel(" Start:", JLabel.RIGHT));
\r
503 panel.add(new JLabel(" End:", JLabel.RIGHT));
\r
505 bigPanel.add(panel, BorderLayout.CENTER);
\r
509 bigPanel.add(panel, BorderLayout.CENTER);
\r
512 if (lastFeatureAdded == null)
\r
514 if (features[0].type != null)
\r
516 lastFeatureAdded = features[0].type;
\r
520 lastFeatureAdded = "feature_1";
\r
524 if (lastFeatureGroupAdded == null)
\r
526 if (features[0].featureGroup != null)
\r
528 lastFeatureGroupAdded = features[0].featureGroup;
\r
532 lastFeatureAdded = "Jalview";
\r
537 if (featureGroups != null)
\r
539 en = featureGroups.keys();
\r
540 while (en.hasMoreElements())
\r
542 source.addItem(en.nextElement().toString());
\r
548 if (av.featuresDisplayed != null)
\r
550 en = av.featuresDisplayed.keys();
\r
551 while (en.hasMoreElements())
\r
553 name.addItem(en.nextElement().toString());
\r
557 name.setSelectedItem(lastFeatureAdded);
\r
558 source.setSelectedItem(lastFeatureGroupAdded);
\r
559 description.setText(
\r
560 lastDescriptionAdded == null ?
\r
561 features[0].description : lastDescriptionAdded);
\r
563 if (getColour(lastFeatureAdded) != null)
\r
565 colour.setBackground(getColour(lastFeatureAdded));
\r
569 colour.setBackground(new Color(60, 160, 115));
\r
573 else if (!newFeatures)
\r
576 for (int f = 0; f < features.length; f++)
\r
578 name.addItem(features[f].getType().toString());
\r
581 description.setText(features[0].getDescription());
\r
582 source.setSelectedItem(features[0].getFeatureGroup());
\r
583 start.setValue(new Integer(features[0].getBegin()));
\r
584 end.setValue(new Integer(features[0].getEnd()));
\r
585 colour.setBackground(
\r
586 getColour(name.getSelectedItem().toString()));
\r
587 name.addItemListener(new ItemListener()
\r
589 public void itemStateChanged(ItemEvent e)
\r
591 int index = name.getSelectedIndex();
\r
594 featureIndex = index;
\r
595 description.setText(features[index].getDescription());
\r
596 source.setSelectedItem(features[index].getFeatureGroup());
\r
597 start.setValue(new Integer(features[index].getBegin()));
\r
598 end.setValue(new Integer(features[index].getEnd()));
\r
599 colour.setBackground(
\r
600 getColour(name.getSelectedItem().toString()));
\r
602 SearchResults highlight = new SearchResults();
\r
603 highlight.addResult(sequences[0],
\r
604 features[index].getBegin(),
\r
605 features[index].getEnd());
\r
607 ap.seqPanel.seqCanvas.highlightSearchResults(highlight);
\r
610 Color col = getColour(name.getSelectedItem().toString());
\r
614 jalview.schemes.UserColourScheme()
\r
615 .createColourFromName(name.getSelectedItem().toString());
\r
618 colour.setBackground(col);
\r
627 options = new Object[]
\r
629 "Amend", "Delete", "Cancel"};
\r
633 options = new Object[]
\r
638 String title = newFeatures ? "Create New Sequence Feature(s)" :
\r
639 "Amend/Delete Features for "
\r
640 + sequences[0].getName();
\r
642 int reply = JOptionPane.showInternalOptionDialog(Desktop.desktop,
\r
645 JOptionPane.YES_NO_CANCEL_OPTION,
\r
646 JOptionPane.QUESTION_MESSAGE,
\r
650 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
\r
652 if (reply == JOptionPane.OK_OPTION
\r
653 && name.getSelectedItem() != null
\r
654 && source.getSelectedItem() != null)
\r
656 //This ensures that the last sequence
\r
657 //is refreshed and new features are rendered
\r
659 lastFeatureAdded = name.getSelectedItem().toString();
\r
660 lastFeatureGroupAdded = source.getSelectedItem().toString();
\r
661 lastDescriptionAdded = description.getText().replaceAll("\n", " ");
\r
666 SequenceFeature sf = features[featureIndex];
\r
668 if (reply == JOptionPane.NO_OPTION)
\r
670 sequences[0].getDatasetSequence().deleteFeature(sf);
\r
672 else if (reply == JOptionPane.YES_OPTION)
\r
674 sf.type = lastFeatureAdded;
\r
675 sf.featureGroup = lastFeatureGroupAdded;
\r
676 sf.description = lastDescriptionAdded;
\r
677 setColour(sf.type, colour.getBackground());
\r
680 sf.begin = ( (Integer) start.getValue()).intValue();
\r
681 sf.end = ( (Integer) end.getValue()).intValue();
\r
683 catch (NumberFormatException ex)
\r
686 ffile.parseDescriptionHTML(sf, false);
\r
691 if (reply == JOptionPane.OK_OPTION
\r
692 && name.getSelectedItem() != null
\r
693 && source.getSelectedItem() != null)
\r
695 for (int i = 0; i < sequences.length; i++)
\r
697 features[i].type = lastFeatureAdded;
\r
698 features[i].featureGroup = lastFeatureGroupAdded;
\r
699 features[i].description = lastDescriptionAdded;
\r
700 sequences[i].addSequenceFeature(features[i]);
\r
701 ffile.parseDescriptionHTML(features[i], false);
\r
704 if (av.featuresDisplayed == null)
\r
706 av.featuresDisplayed = new Hashtable();
\r
709 if (featureGroups == null)
\r
711 featureGroups = new Hashtable();
\r
714 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
\r
716 Color col = colour.getBackground();
\r
717 setColour(lastFeatureAdded, colour.getBackground());
\r
719 av.featuresDisplayed.put(lastFeatureGroupAdded,
\r
720 new Integer(col.getRGB()));
\r
732 if (name.getSelectedIndex() == -1)
\r
740 public void setColour(String featureType, Color col)
\r
742 featureColours.put(featureType, col);
\r
745 public void setTransparency(float value)
\r
747 transparency = value;
\r
750 public float getTransparency()
\r
752 return transparency;
\r
755 public void setFeaturePriority(Object[][] data)
\r
757 // The feature table will display high priority
\r
758 // features at the top, but theses are the ones
\r
759 // we need to render last, so invert the data
\r
760 if (av.featuresDisplayed != null)
\r
762 av.featuresDisplayed.clear();
\r
766 av.featuresDisplayed = new Hashtable();
\r
769 renderOrder = new String[data.length];
\r
771 if (data.length > 0)
\r
773 for (int i = 0; i < data.length; i++)
\r
775 String type = data[i][0].toString();
\r
776 setColour(type, (Color) data[i][1]);
\r
777 if ( ( (Boolean) data[i][2]).booleanValue())
\r
779 av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
\r
782 renderOrder[data.length - i - 1] = type;
\r