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
19 package jalview.appletgui;
25 import jalview.datamodel.*;
33 public class FeatureRenderer
37 Hashtable featureColours = new Hashtable();
39 // A higher level for grouping features of a
41 Hashtable featureGroups = null;
43 // Holds web links for feature groups and feature types
44 // in the form label|link
45 Hashtable featureLinks = null;
47 // This is actually an Integer held in the hashtable,
48 // Retrieved using the key feature type
56 float transparency = 1f;
58 TransparencySetter transparencySetter = null;
61 * Creates a new FeatureRenderer object.
63 * @param av DOCUMENT ME!
65 public FeatureRenderer(AlignViewport av)
69 if (!System.getProperty("java.version").startsWith("1.1"))
71 transparencySetter = new TransparencySetter();
75 public void transferSettings(FeatureRenderer fr)
77 renderOrder = fr.renderOrder;
78 featureGroups = fr.featureGroups;
79 featureColours = fr.featureColours;
80 transparency = fr.transparency;
83 public Color findFeatureColour(Color initialCol, SequenceI seq, int i)
86 if (!av.showSequenceFeatures)
92 sequenceFeatures = lastSequence.getSequenceFeatures();
93 if (sequenceFeatures == null)
98 sfSize = sequenceFeatures.length;
100 if (jalview.util.Comparison.isGap(lastSequence.getCharAt(i)))
105 currentColour = null;
107 drawSequence(null, lastSequence, lastSequence.findPosition(i), -1, -1);
109 if (currentColour == null)
114 return new Color( ( (Integer) currentColour).intValue());
118 * This is used by the Molecule Viewer to get the accurate colour
119 * of the rendered sequence
121 boolean overview = false;
123 int white = Color.white.getRGB();
124 public int findFeatureColour(int initialCol, int seqIndex, int column)
126 if (!av.showSequenceFeatures)
131 if (seqIndex != lastSequenceIndex)
133 lastSequence = av.alignment.getSequenceAt(seqIndex);
134 lastSequenceIndex = seqIndex;
135 sequenceFeatures = lastSequence.getSequenceFeatures();
136 if (sequenceFeatures == null)
141 sfSize = sequenceFeatures.length;
144 if (jalview.util.Comparison.isGap(lastSequence.getCharAt(column)))
146 return Color.white.getRGB();
149 currentColour = null;
151 drawSequence(null, lastSequence, lastSequence.findPosition(column), -1, -1);
153 if (currentColour == null)
158 return ( (Integer) currentColour).intValue();
164 * @param g DOCUMENT ME!
165 * @param seq DOCUMENT ME!
166 * @param sg DOCUMENT ME!
167 * @param start DOCUMENT ME!
168 * @param end DOCUMENT ME!
169 * @param x1 DOCUMENT ME!
170 * @param y1 DOCUMENT ME!
171 * @param width DOCUMENT ME!
172 * @param height DOCUMENT ME!
175 // SequenceFeature sf;
176 int lastSequenceIndex = -1;
177 SequenceI lastSequence;
178 SequenceFeature[] sequenceFeatures;
179 int sfSize, sfindex, spos, epos;
181 public void drawSequence(Graphics g, SequenceI seq,
182 int start, int end, int y1)
184 if (seq.getSequenceFeatures() == null
185 || seq.getSequenceFeatures().length == 0)
190 if (transparencySetter != null && g != null)
192 transparencySetter.setTransparency(g, transparency);
195 if (lastSequence == null || seq != lastSequence || sequenceFeatures!=seq.getSequenceFeatures())
198 sequenceFeatures = seq.getSequenceFeatures();
199 sfSize = sequenceFeatures.length;
202 if (av.featuresDisplayed == null || renderOrder == null)
205 if (av.featuresDisplayed.size() < 1)
210 sequenceFeatures = seq.getSequenceFeatures();
211 sfSize = sequenceFeatures.length;
215 spos = lastSequence.findPosition(start);
216 epos = lastSequence.findPosition(end);
219 fm = g.getFontMetrics();
223 for (int renderIndex = 0; renderIndex < renderOrder.length; renderIndex++)
225 type = renderOrder[renderIndex];
226 if (!av.featuresDisplayed.containsKey(type))
231 // loop through all features in sequence to find
232 // current feature to render
233 for (sfindex = 0; sfindex < sfSize; sfindex++)
235 if (!sequenceFeatures[sfindex].type.equals(type))
240 if (featureGroups != null
241 && sequenceFeatures[sfindex].featureGroup != null
243 featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
245 ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].
252 if (!overview && (sequenceFeatures[sfindex].getBegin() > epos
253 || sequenceFeatures[sfindex].getEnd() < spos))
260 if (sequenceFeatures[sfindex].begin <= start &&
261 sequenceFeatures[sfindex].end >= start)
263 currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
268 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
271 renderFeature(g, seq,
272 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
273 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
274 new Color( ( (Integer) av.featuresDisplayed.get(
275 sequenceFeatures[sfindex].type)).intValue()),
277 renderFeature(g, seq,
278 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
279 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
280 new Color( ( (Integer) av.featuresDisplayed.get(
281 sequenceFeatures[sfindex].type)).intValue()),
287 renderFeature(g, seq,
288 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
289 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
290 getColour(sequenceFeatures[sfindex].type),
297 if (transparencySetter != null && g != null)
299 transparencySetter.setTransparency(g, 1.0f);
305 void renderFeature(Graphics g, SequenceI seq,
306 int fstart, int fend, Color featureColour, int start,
310 if ( ( (fstart <= end) && (fend >= start)))
313 { // fix for if the feature we have starts before the sequence start,
314 fstart = start; // but the feature end is still valid!!
322 for (i = fstart; i <= fend; i++)
324 s = seq.getCharAt(i);
326 if (jalview.util.Comparison.isGap(s))
331 g.setColor(featureColour);
333 g.fillRect( (i - start) * av.charWidth, y1, av.charWidth, av.charHeight);
335 if (!av.validCharWidth)
340 g.setColor(Color.white);
341 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
342 g.drawString(String.valueOf(s),
343 charOffset + (av.charWidth * (i - start)),
344 (y1 + av.charHeight) - av.charHeight / 5); //pady = height / 5;
350 void findAllFeatures()
352 jalview.schemes.UserColourScheme ucs = new
353 jalview.schemes.UserColourScheme();
355 av.featuresDisplayed = new Hashtable();
356 Vector allfeatures = new Vector();
357 for (int i = 0; i < av.alignment.getHeight(); i++)
359 SequenceFeature[] features = av.alignment.getSequenceAt(i).
360 getSequenceFeatures();
362 if (features == null)
368 while (index < features.length)
370 if (!av.featuresDisplayed.containsKey(features[index].getType()))
372 if (getColour(features[index].getType()) == null)
374 featureColours.put(features[index].getType(),
375 ucs.createColourFromName(features[index].
379 av.featuresDisplayed.put(features[index].getType(),
380 new Integer(getColour(features[index].
381 getType()).getRGB()));
382 allfeatures.addElement(features[index].getType());
388 renderOrder = new String[allfeatures.size()];
389 Enumeration en = allfeatures.elements();
390 int i = allfeatures.size() - 1;
391 while (en.hasMoreElements())
393 renderOrder[i] = en.nextElement().toString();
398 public Color getColour(String featureType)
400 return (Color) featureColours.get(featureType);
403 public void addNewFeature(String name, Color col)
406 setColour(name, col);
407 if (av.featuresDisplayed == null)
409 av.featuresDisplayed = new Hashtable();
412 av.featuresDisplayed.put(name, "NOGROUP");
415 public void setColour(String featureType, Color col)
417 featureColours.put(featureType, col);
420 public void setFeaturePriority(Object[][] data)
422 // The feature table will display high priority
423 // features at the top, but theses are the ones
424 // we need to render last, so invert the data
425 if (av.featuresDisplayed != null)
427 av.featuresDisplayed.clear();
430 renderOrder = new String[data.length];
434 for (int i = 0; i < data.length; i++)
436 String type = data[i][0].toString();
437 setColour(type, (Color) data[i][1]);
438 if ( ( (Boolean) data[i][2]).booleanValue())
440 av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
443 renderOrder[data.length - i - 1] = type;
449 class TransparencySetter
451 void setTransparency(Graphics g, float value)
453 Graphics2D g2 = (Graphics2D) g;
455 AlphaComposite.getInstance(
456 AlphaComposite.SRC_OVER, value));