2 * Jalview - A Sequence Alignment Editor and Viewer
\r
3 * Copyright (C) 2005 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
32 * @version $Revision$
\r
34 public class FeatureRenderer
\r
38 float transparency = 1.0f;
\r
42 // A higher level for grouping features of a
\r
44 Hashtable featureGroups = null;
\r
47 // This is actually an Integer held in the hashtable,
\r
48 // Retrieved using the key feature type
\r
49 Object currentColour;
\r
51 String [] renderOrder;
\r
55 * Creates a new FeatureRenderer object.
\r
57 * @param av DOCUMENT ME!
\r
59 public FeatureRenderer(AlignViewport av)
\r
66 public Color findFeatureColour(Color initialCol, SequenceI seq, int i)
\r
68 if(!av.showSequenceFeatures)
\r
72 sequenceFeatures = lastSequence.getDatasetSequence().getSequenceFeatures();
\r
73 if(sequenceFeatures==null)
\r
76 sfSize = sequenceFeatures.length;
\r
78 if(jalview.util.Comparison.isGap(lastSequence.getCharAt(i)))
\r
81 currentColour = null;
\r
83 drawSequence(null, lastSequence, lastSequence.findPosition(i), -1,-1, -1, -1);
\r
85 if(currentColour==null)
\r
88 return new Color( ((Integer)currentColour).intValue() );
\r
91 * This is used by the Molecule Viewer to get the accurate colour
\r
92 * of the rendered sequence
\r
95 boolean overview = false;
\r
97 int white = Color.white.getRGB();
\r
98 public int findFeatureColour(int initialCol, int seqIndex, int column)
\r
100 if(!av.showSequenceFeatures)
\r
103 if(seqIndex!=lastSequenceIndex)
\r
105 lastSequence = av.alignment.getSequenceAt(seqIndex);
\r
106 lastSequenceIndex = seqIndex;
\r
107 sequenceFeatures = lastSequence.getDatasetSequence().getSequenceFeatures();
\r
108 if(sequenceFeatures==null)
\r
111 sfSize = sequenceFeatures.length;
\r
115 if(jalview.util.Comparison.isGap(lastSequence.getCharAt(column)))
\r
116 return Color.white.getRGB();
\r
118 currentColour = null;
\r
120 drawSequence(null, lastSequence, lastSequence.findPosition(column), -1,-1, -1, -1);
\r
122 if(currentColour==null)
\r
125 return ((Integer)currentColour).intValue();
\r
132 * @param g DOCUMENT ME!
\r
133 * @param seq DOCUMENT ME!
\r
134 * @param sg DOCUMENT ME!
\r
135 * @param start DOCUMENT ME!
\r
136 * @param end DOCUMENT ME!
\r
137 * @param x1 DOCUMENT ME!
\r
138 * @param y1 DOCUMENT ME!
\r
139 * @param width DOCUMENT ME!
\r
140 * @param height DOCUMENT ME!
\r
143 // SequenceFeature sf;
\r
144 int lastSequenceIndex=-1;
\r
145 SequenceI lastSequence;
\r
146 SequenceFeature [] sequenceFeatures;
\r
147 int sfSize, sfindex, spos, epos;
\r
149 public void drawSequence(Graphics g, SequenceI seq,
\r
150 int start, int end, int y1, int width, int height)
\r
152 if ( seq.getDatasetSequence().getSequenceFeatures() == null
\r
153 || seq.getDatasetSequence().getSequenceFeatures().length==0)
\r
158 fm = g.getFontMetrics();
\r
161 if (av.featuresDisplayed == null || renderOrder==null)
\r
164 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
\r
165 sfSize = sequenceFeatures.length;
\r
168 if(lastSequence==null || seq!=lastSequence)
\r
170 lastSequence = seq;
\r
171 sequenceFeatures = seq.getDatasetSequence().getSequenceFeatures();
\r
172 sfSize = sequenceFeatures.length;
\r
176 if (transparency != 1 && g!=null)
\r
178 Graphics2D g2 = (Graphics2D) g;
\r
180 AlphaComposite.getInstance(
\r
181 AlphaComposite.SRC_OVER, transparency));
\r
186 spos = lastSequence.findPosition(start);
\r
187 epos = lastSequence.findPosition(end);
\r
191 for(int renderIndex=0; renderIndex<renderOrder.length; renderIndex++)
\r
193 type = renderOrder[renderIndex];
\r
195 // loop through all features in sequence to find
\r
196 // current feature to render
\r
197 for (sfindex = 0; sfindex < sfSize; sfindex++)
\r
199 if (!sequenceFeatures[sfindex].type.equals(type))
\r
202 if (featureGroups != null
\r
203 && sequenceFeatures[sfindex].featureGroup != null
\r
205 featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
\r
207 ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].featureGroup)).
\r
213 if (!overview && (sequenceFeatures[sfindex].getBegin() > epos
\r
214 || sequenceFeatures[sfindex].getEnd() < spos))
\r
220 if (sequenceFeatures[sfindex].begin - 1 <= start &&
\r
221 sequenceFeatures[sfindex].end + 1 >= start)
\r
223 currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
\r
228 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
\r
231 renderFeature(g, seq,
\r
232 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
233 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
234 new Color( ( (Integer) av.featuresDisplayed.get(
\r
235 sequenceFeatures[sfindex].type)).intValue()),
\r
236 start, end, y1, width, height);
\r
237 renderFeature(g, seq,
\r
238 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
239 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
240 new Color( ( (Integer) av.featuresDisplayed.get(
\r
241 sequenceFeatures[sfindex].type)).intValue()),
\r
242 start, end, y1, width, height);
\r
246 renderFeature(g, seq,
\r
247 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
248 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
249 getColour(sequenceFeatures[sfindex].type),
\r
250 start, end, y1, width, height);
\r
257 if(transparency!=1.0f && g!=null)
\r
259 Graphics2D g2 = (Graphics2D) g;
\r
261 AlphaComposite.getInstance(
\r
262 AlphaComposite.SRC_OVER, 1.0f));
\r
269 void renderFeature(Graphics g, SequenceI seq,
\r
270 int fstart, int fend, Color featureColour, int start, int end, int y1, int width, int height)
\r
273 if (((fstart <= end) && (fend >= start)))
\r
275 if (fstart < start)
\r
276 { // fix for if the feature we have starts before the sequence start,
\r
277 fstart = start; // but the feature end is still valid!!
\r
285 for (i = fstart; i <= fend; i++)
\r
287 s = seq.getSequence().charAt(i);
\r
289 if (jalview.util.Comparison.isGap(s))
\r
294 g.setColor(featureColour);
\r
296 g.fillRect( (i - start) * width, y1, width, height);
\r
298 g.setColor(Color.white);
\r
299 charOffset = (width - fm.charWidth(s)) / 2;
\r
300 g.drawString(String.valueOf(s),
\r
301 charOffset + (width * (i - start)),
\r
302 (y1 + height) - height / 5); //pady = height / 5;
\r
308 void findAllFeatures()
\r
310 av.featuresDisplayed = new Hashtable();
\r
311 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
313 SequenceFeature [] features = av.alignment.getSequenceAt(i).getDatasetSequence().
\r
314 getSequenceFeatures();
\r
316 if (features == null)
\r
320 while (index < features.length)
\r
322 if (!av.featuresDisplayed.containsKey(features[index].getType()))
\r
324 av.featuresDisplayed.put(features[index].getType(),
\r
325 new Integer( getColour(features[index].getType()).getRGB()) );
\r
331 renderOrder = new String[av.featuresDisplayed.size()];
\r
332 Enumeration en = av.featuresDisplayed.keys();
\r
334 while(en.hasMoreElements())
\r
336 renderOrder[i] = en.nextElement().toString();
\r
341 public Color getColour(String featureType)
\r
343 return (Color)featureColours.get(featureType);
\r
346 public void addNewFeature(String name, Color col)
\r
349 setColour(name, col);
\r
350 if(av.featuresDisplayed==null)
\r
351 av.featuresDisplayed = new Hashtable();
\r
354 av.featuresDisplayed.put(name, "NOGROUP");
\r
357 public void setColour(String featureType, Color col)
\r
359 featureColours.put(featureType, col);
\r
362 public void setTransparency(float value)
\r
364 transparency = value;
\r
367 public float getTransparency()
\r
369 return transparency;
\r
372 public void setFeaturePriority(Object [][] data)
\r
374 // The feature table will display high priority
\r
375 // features at the top, but theses are the ones
\r
376 // we need to render last, so invert the data
\r
377 av.featuresDisplayed.clear();
\r
379 renderOrder = new String[data.length];
\r
382 for(int i=data.length-1; i>-1; i--)
\r
384 String type = data[i][0].toString();
\r
385 setColour(type, (Color)data[i][1]);
\r
386 if( ((Boolean)data[i][2]).booleanValue() )
\r
388 av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
\r
390 renderOrder[i] = type;
\r
395 Hashtable featureColours = new Hashtable();
\r
398 featureColours.put("active site", new Color(255, 75, 0));
\r
399 featureColours.put("binding site", new Color(245, 85, 0));
\r
400 featureColours.put("calcium-binding region", new Color(235, 95, 0));
\r
401 featureColours.put("chain", new Color(225, 105, 0));
\r
402 featureColours.put("coiled-coil region", new Color(215, 115, 0));
\r
403 featureColours.put("compositionally biased region", new Color(205, 125, 0));
\r
404 featureColours.put("cross-link", new Color(195, 135, 0));
\r
405 featureColours.put("disulfide bond", new Color(230,230,0));
\r
406 featureColours.put("DNA-binding region", new Color(175, 155, 0));
\r
407 featureColours.put("domain", new Color(165, 165, 0));
\r
408 featureColours.put("glycosylation site", new Color(155, 175, 0));
\r
409 featureColours.put("helix", new Color(145, 185, 0));
\r
410 featureColours.put("initiator methionine", new Color(135, 195, 5));
\r
411 featureColours.put("lipid moiety-binding region", new Color(125, 205, 15));
\r
412 featureColours.put("metal ion-binding site", new Color(115, 215, 25));
\r
413 featureColours.put("modified residue", new Color(105, 225, 35));
\r
414 featureColours.put("mutagenesis site", new Color(95, 235, 45));
\r
415 featureColours.put("non-consecutive residues", new Color(85, 245, 55));
\r
416 featureColours.put("non-terminal residue", new Color(75, 255, 65));
\r
417 featureColours.put("nucleotide phosphate-binding region",new Color(65, 245, 75));
\r
418 featureColours.put("peptide", new Color(55, 235, 85));
\r
419 featureColours.put("propeptide", new Color(45, 225, 95));
\r
420 featureColours.put("region of interest", new Color(35, 215, 105));
\r
421 featureColours.put("repeat", new Color(25, 205, 115));
\r
422 featureColours.put("selenocysteine", new Color(15, 195, 125));
\r
423 featureColours.put("sequence conflict", new Color(5, 185, 135));
\r
424 featureColours.put("sequence variant", new Color(0, 175, 145));
\r
425 featureColours.put("short sequence motif", new Color(0, 165, 155));
\r
426 featureColours.put("signal peptide", new Color(0, 155, 165));
\r
427 featureColours.put("site", new Color(0, 145, 175));
\r
428 featureColours.put("splice variant", new Color(0, 135, 185));
\r
429 featureColours.put("strand", new Color(0, 125, 195));
\r
430 featureColours.put("topological domain", new Color(0, 115, 205));
\r
431 featureColours.put("transit peptide", new Color(0, 105, 215));
\r
432 featureColours.put("transmembrane region", new Color(0, 95, 225));
\r
433 featureColours.put("turn", new Color(0, 85, 235));
\r
434 featureColours.put("unsure residue", new Color(0, 75, 245));
\r
435 featureColours.put("zinc finger region", new Color(0, 65, 255));
\r