2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2006 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
21 import jalview.datamodel.*;
\r
27 import java.awt.image.*;
\r
28 import javax.swing.*;
\r
29 import javax.swing.JOptionPane;
\r
36 * @version $Revision$
\r
38 public class FeatureRenderer
\r
42 float transparency = 1.0f;
\r
46 Hashtable featureColours = new Hashtable();
\r
49 // A higher level for grouping features of a
\r
51 Hashtable featureGroups = null;
\r
53 // This is actually an Integer held in the hashtable,
\r
54 // Retrieved using the key feature type
\r
55 Object currentColour;
\r
57 String [] renderOrder;
\r
60 * Creates a new FeatureRenderer object.
\r
62 * @param av DOCUMENT ME!
\r
64 public FeatureRenderer(AlignViewport av)
\r
69 public void transferSettings(FeatureRenderer fr)
\r
71 renderOrder = fr.renderOrder;
\r
72 featureGroups = fr.featureGroups;
\r
73 featureColours = fr.featureColours;
\r
74 transparency = fr.transparency;
\r
77 BufferedImage offscreenImage;
\r
78 boolean offscreenRender = false;
\r
79 public Color findFeatureColour(Color initialCol, SequenceI seq, int res)
\r
81 return new Color( findFeatureColour (initialCol.getRGB(),
\r
86 * This is used by the Molecule Viewer and Overview to
\r
87 * get the accurate colourof the rendered sequence
\r
89 public int findFeatureColour(int initialCol, SequenceI seq, int column)
\r
91 if(!av.showSequenceFeatures)
\r
97 sequenceFeatures = lastSeq.getDatasetSequence().getSequenceFeatures();
\r
98 if(sequenceFeatures==null)
\r
101 sfSize = sequenceFeatures.length;
\r
104 if(jalview.util.Comparison.isGap(lastSeq.getCharAt(column)))
\r
105 return Color.white.getRGB();
\r
108 //Only bother making an offscreen image if transparency is applied
\r
109 if(transparency!=1.0f && offscreenImage==null)
\r
111 offscreenImage = new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB);
\r
114 currentColour = null;
\r
116 offscreenRender = true;
\r
118 if(offscreenImage!=null)
\r
120 offscreenImage.setRGB(0,0,initialCol);
\r
121 drawSequence(offscreenImage.getGraphics(),
\r
125 return offscreenImage.getRGB(0,0);
\r
131 lastSeq.findPosition(column),
\r
134 if (currentColour == null)
\r
137 return ((Integer)currentColour).intValue();
\r
147 * @param g DOCUMENT ME!
\r
148 * @param seq DOCUMENT ME!
\r
149 * @param sg DOCUMENT ME!
\r
150 * @param start DOCUMENT ME!
\r
151 * @param end DOCUMENT ME!
\r
152 * @param x1 DOCUMENT ME!
\r
153 * @param y1 DOCUMENT ME!
\r
154 * @param width DOCUMENT ME!
\r
155 * @param height DOCUMENT ME!
\r
158 // SequenceFeature sf;
\r
160 SequenceFeature [] sequenceFeatures;
\r
161 int sfSize, sfindex, spos, epos;
\r
163 public void drawSequence(Graphics g, SequenceI seq,
\r
164 int start, int end, int y1)
\r
166 if ( seq.getDatasetSequence().getSequenceFeatures() == null
\r
167 || seq.getDatasetSequence().getSequenceFeatures().length==0)
\r
171 fm = g.getFontMetrics();
\r
174 if (av.featuresDisplayed == null
\r
175 || renderOrder==null
\r
176 || newFeatureAdded)
\r
179 if(av.featuresDisplayed.size()<1)
\r
182 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
\r
183 sfSize = sequenceFeatures.length;
\r
186 if(lastSeq==null || seq!=lastSeq)
\r
189 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
\r
190 sfSize = sequenceFeatures.length;
\r
194 if (transparency != 1 && g!=null)
\r
196 Graphics2D g2 = (Graphics2D) g;
\r
198 AlphaComposite.getInstance(
\r
199 AlphaComposite.SRC_OVER, transparency));
\r
202 if(!offscreenRender)
\r
204 spos = lastSeq.findPosition(start);
\r
205 epos = lastSeq.findPosition(end);
\r
210 for(int renderIndex=0; renderIndex<renderOrder.length; renderIndex++)
\r
212 type = renderOrder[renderIndex];
\r
214 if(!av.featuresDisplayed.containsKey(type))
\r
217 // loop through all features in sequence to find
\r
218 // current feature to render
\r
219 for (sfindex = 0; sfindex < sfSize; sfindex++)
\r
221 if(sequenceFeatures.length<=sfindex)
\r
225 if (!sequenceFeatures[sfindex].type.equals(type))
\r
228 if (featureGroups != null
\r
229 && sequenceFeatures[sfindex].featureGroup != null
\r
231 featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
\r
233 ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].featureGroup)).
\r
239 if (!offscreenRender && (sequenceFeatures[sfindex].getBegin() > epos
\r
240 || sequenceFeatures[sfindex].getEnd() < spos))
\r
243 if (offscreenRender && offscreenImage==null)
\r
245 if (sequenceFeatures[sfindex].begin <= start &&
\r
246 sequenceFeatures[sfindex].end >= start)
\r
248 currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
\r
252 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
\r
255 renderFeature(g, seq,
\r
256 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
257 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
258 new Color( ( (Integer) av.featuresDisplayed.get(
\r
259 sequenceFeatures[sfindex].type)).intValue()),
\r
261 renderFeature(g, seq,
\r
262 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
263 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
264 new Color( ( (Integer) av.featuresDisplayed.get(
\r
265 sequenceFeatures[sfindex].type)).intValue()),
\r
270 renderFeature(g, seq,
\r
271 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
272 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
273 getColour(sequenceFeatures[sfindex].type),
\r
281 if(transparency!=1.0f && g!=null)
\r
283 Graphics2D g2 = (Graphics2D) g;
\r
285 AlphaComposite.getInstance(
\r
286 AlphaComposite.SRC_OVER, 1.0f));
\r
293 void renderFeature(Graphics g, SequenceI seq,
\r
294 int fstart, int fend, Color featureColour, int start, int end, int y1)
\r
297 if (((fstart <= end) && (fend >= start)))
\r
299 if (fstart < start)
\r
300 { // fix for if the feature we have starts before the sequence start,
\r
301 fstart = start; // but the feature end is still valid!!
\r
308 int pady = (y1 + av.charHeight) - av.charHeight / 5;
\r
309 for (i = fstart; i <= fend; i++)
\r
311 s = seq.getSequence().charAt(i);
\r
313 if (jalview.util.Comparison.isGap(s))
\r
318 g.setColor(featureColour);
\r
320 g.fillRect( (i - start) * av.charWidth, y1, av.charWidth,av.charHeight);
\r
322 if(offscreenRender || !av.validCharWidth)
\r
325 g.setColor(Color.white);
\r
326 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
\r
327 g.drawString(String.valueOf(s),
\r
328 charOffset + (av.charWidth * (i - start)),
\r
336 boolean newFeatureAdded = false;
\r
338 public void featuresAdded()
\r
343 boolean findingFeatures = false;
\r
344 synchronized void findAllFeatures()
\r
346 newFeatureAdded = false;
\r
348 if(findingFeatures)
\r
350 newFeatureAdded = true;
\r
354 findingFeatures = true;
\r
355 jalview.schemes.UserColourScheme ucs = new
\r
356 jalview.schemes.UserColourScheme();
\r
358 if(av.featuresDisplayed==null)
\r
359 av.featuresDisplayed = new Hashtable();
\r
361 av.featuresDisplayed.clear();
\r
363 Vector allfeatures = new Vector();
\r
364 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
366 SequenceFeature [] features
\r
367 = av.alignment.getSequenceAt(i).getDatasetSequence().getSequenceFeatures();
\r
369 if (features == null)
\r
373 while (index < features.length)
\r
375 if (!av.featuresDisplayed.containsKey(features[index].getType()))
\r
377 if(!(features[index].begin == 0 && features[index].end ==0))
\r
379 // If beginning and end are 0, the feature is for the whole sequence
\r
380 // and we don't want to render the feature in the normal way
\r
382 if (getColour(features[index].getType()) == null)
\r
384 featureColours.put(features[index].getType(),
\r
385 ucs.createColourFromName(features[index].
\r
389 av.featuresDisplayed.put(features[index].getType(),
\r
390 new Integer(getColour(features[index].
\r
391 getType()).getRGB()));
\r
392 allfeatures.addElement(features[index].getType());
\r
399 renderOrder = new String[allfeatures.size()];
\r
400 Enumeration en = allfeatures.elements();
\r
401 int i = allfeatures.size()-1;
\r
402 while(en.hasMoreElements())
\r
404 renderOrder[i] = en.nextElement().toString();
\r
408 findingFeatures = false;
\r
411 public Color getColour(String featureType)
\r
413 Color colour = (Color)featureColours.get(featureType);
\r
418 static String lastFeatureAdded;
\r
419 static String lastFeatureGroupAdded;
\r
420 static String lastDescriptionAdded;
\r
422 public boolean createNewFeatures(SequenceI[] sequences,
\r
423 SequenceFeature [] features)
\r
426 JPanel bigPanel = new JPanel(new BorderLayout());
\r
427 JComboBox name = new JComboBox();
\r
428 JComboBox source = new JComboBox();
\r
429 JTextArea description = new JTextArea(3,25);
\r
431 JPanel panel = new JPanel(new GridLayout(2, 2));
\r
432 panel.add(new JLabel("New Sequence Feature Name: ",JLabel.RIGHT));
\r
434 panel.add(new JLabel("Feature Group: ", JLabel.RIGHT));
\r
436 name.setEditable(true);
\r
437 source.setEditable(true);
\r
439 bigPanel.add(panel, BorderLayout.NORTH);
\r
440 panel = new JPanel();
\r
441 panel.add(new JLabel("Description: ", JLabel.RIGHT));
\r
442 description.setFont(new java.awt.Font("Verdana", Font.PLAIN, 11));
\r
443 description.setLineWrap(true);
\r
444 panel.add(new JScrollPane(description));
\r
445 bigPanel.add(panel, BorderLayout.CENTER);
\r
449 if (av.featuresDisplayed != null)
\r
451 en = av.featuresDisplayed.keys();
\r
452 while (en.hasMoreElements())
\r
454 name.addItem(en.nextElement().toString());
\r
458 if (featureGroups != null)
\r
460 en = featureGroups.keys();
\r
461 while (en.hasMoreElements())
\r
463 source.addItem(en.nextElement().toString());
\r
467 if (lastFeatureAdded == null)
\r
468 if (features[0].type != null)
\r
469 lastFeatureAdded = features[0].type;
\r
471 lastFeatureAdded = "feature_1";
\r
473 if (lastFeatureGroupAdded == null)
\r
474 if (features[0].featureGroup != null)
\r
475 lastFeatureGroupAdded = features[0].featureGroup;
\r
477 lastFeatureAdded = "Jalview";
\r
480 name.setSelectedItem(lastFeatureAdded);
\r
481 source.setSelectedItem(lastFeatureGroupAdded);
\r
482 description.setText(
\r
483 lastDescriptionAdded == null ?
\r
484 features[0].description : lastDescriptionAdded);
\r
487 int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop,
\r
488 bigPanel, "Create New Sequence Feature(s)",
\r
489 JOptionPane.OK_CANCEL_OPTION);
\r
492 if(reply==JOptionPane.OK_OPTION
\r
493 && name.getSelectedItem()!=null
\r
494 && source.getSelectedItem()!=null)
\r
496 lastFeatureAdded = name.getSelectedItem().toString();
\r
497 lastFeatureGroupAdded = source.getSelectedItem().toString();
\r
498 lastDescriptionAdded = description.getText().replaceAll("\n"," ");
\r
500 jalview.io.FeaturesFile ffile = new jalview.io.FeaturesFile();
\r
503 for(int i=0; i<sequences.length; i++)
\r
505 features[i].type = lastFeatureAdded;
\r
506 features[i].featureGroup = lastFeatureGroupAdded;
\r
507 features[i].description = lastDescriptionAdded;
\r
508 sequences[i].addSequenceFeature(features[i]);
\r
509 ffile.parseDescriptionHTML(features[i], false);
\r
512 if (av.featuresDisplayed == null)
\r
513 av.featuresDisplayed = new Hashtable();
\r
515 if(featureGroups==null)
\r
516 featureGroups = new Hashtable();
\r
518 featureGroups.put(lastFeatureGroupAdded, new Boolean(true));
\r
520 Color col = getColour(lastFeatureAdded);
\r
523 col = new Color(60, 160, 115);
\r
524 setColour(lastFeatureAdded, col);
\r
527 av.featuresDisplayed.put(lastFeatureGroupAdded, new Integer(col.getRGB()));
\r
536 public void setColour(String featureType, Color col)
\r
538 featureColours.put(featureType, col);
\r
541 public void setTransparency(float value)
\r
543 transparency = value;
\r
546 public float getTransparency()
\r
548 return transparency;
\r
551 public void setFeaturePriority(Object [][] data)
\r
553 // The feature table will display high priority
\r
554 // features at the top, but theses are the ones
\r
555 // we need to render last, so invert the data
\r
556 if(av.featuresDisplayed!=null)
\r
557 av.featuresDisplayed.clear();
\r
559 av.featuresDisplayed = new Hashtable();
\r
561 renderOrder = new String[data.length];
\r
563 if (data.length > 0)
\r
564 for (int i = 0; i < data.length; i++)
\r
566 String type = data[i][0].toString();
\r
567 setColour(type, (Color) data[i][1]);
\r
568 if ( ( (Boolean) data[i][2]).booleanValue())
\r
570 av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
\r
573 renderOrder[data.length - i - 1] = type;
\r