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.appletgui;
\r
21 import jalview.datamodel.*;
\r
31 * @version $Revision$
\r
33 public class FeatureRenderer
\r
38 // A higher level for grouping features of a
\r
40 Hashtable featureGroups = null;
\r
42 // This is actually an Integer held in the hashtable,
\r
43 // Retrieved using the key feature type
\r
44 Object currentColour;
\r
46 String [] renderOrder;
\r
52 * Creates a new FeatureRenderer object.
\r
54 * @param av DOCUMENT ME!
\r
56 public FeatureRenderer(AlignViewport av)
\r
63 public Color findFeatureColour(Color initialCol, SequenceI seq, int i)
\r
65 if(!av.showSequenceFeatures)
\r
69 sequenceFeatures = lastSequence.getSequenceFeatures();
\r
70 if(sequenceFeatures==null)
\r
73 sfSize = sequenceFeatures.length;
\r
75 if(jalview.util.Comparison.isGap(lastSequence.getCharAt(i)))
\r
78 currentColour = null;
\r
80 drawSequence(null, lastSequence, lastSequence.findPosition(i), -1,-1, -1, -1);
\r
82 if(currentColour==null)
\r
85 return new Color( ((Integer)currentColour).intValue() );
\r
89 * This is used by the Molecule Viewer to get the accurate colour
\r
90 * of the rendered sequence
\r
92 boolean overview = false;
\r
94 int white = Color.white.getRGB();
\r
95 public int findFeatureColour(int initialCol, int seqIndex, int column)
\r
97 if(!av.showSequenceFeatures)
\r
100 if(seqIndex!=lastSequenceIndex)
\r
102 lastSequence = av.alignment.getSequenceAt(seqIndex);
\r
103 lastSequenceIndex = seqIndex;
\r
104 sequenceFeatures = lastSequence.getSequenceFeatures();
\r
105 if(sequenceFeatures==null)
\r
108 sfSize = sequenceFeatures.length;
\r
112 if(jalview.util.Comparison.isGap(lastSequence.getCharAt(column)))
\r
113 return Color.white.getRGB();
\r
115 currentColour = null;
\r
117 drawSequence(null, lastSequence, lastSequence.findPosition(column), -1,-1, -1, -1);
\r
119 if(currentColour==null)
\r
122 return ((Integer)currentColour).intValue();
\r
129 * @param g DOCUMENT ME!
\r
130 * @param seq DOCUMENT ME!
\r
131 * @param sg DOCUMENT ME!
\r
132 * @param start DOCUMENT ME!
\r
133 * @param end DOCUMENT ME!
\r
134 * @param x1 DOCUMENT ME!
\r
135 * @param y1 DOCUMENT ME!
\r
136 * @param width DOCUMENT ME!
\r
137 * @param height DOCUMENT ME!
\r
140 // SequenceFeature sf;
\r
141 int lastSequenceIndex=-1;
\r
142 SequenceI lastSequence;
\r
143 SequenceFeature [] sequenceFeatures;
\r
144 int sfSize, sfindex, spos, epos;
\r
146 public void drawSequence(Graphics g, SequenceI seq,
\r
147 int start, int end, int y1, int width, int height)
\r
149 if ( seq.getSequenceFeatures() == null
\r
150 || seq.getSequenceFeatures().length==0)
\r
154 if (av.featuresDisplayed == null || renderOrder==null)
\r
157 if(av.featuresDisplayed.size()<1)
\r
160 sequenceFeatures = seq.getSequenceFeatures();
\r
161 sfSize = sequenceFeatures.length;
\r
163 if(lastSequence==null || seq!=lastSequence)
\r
165 lastSequence = seq;
\r
166 sequenceFeatures = seq.getSequenceFeatures();
\r
167 sfSize = sequenceFeatures.length;
\r
171 spos = lastSequence.findPosition(start);
\r
172 epos = lastSequence.findPosition(end);
\r
174 fm = g.getFontMetrics();
\r
177 for(int renderIndex=0; renderIndex<renderOrder.length; renderIndex++)
\r
179 type = renderOrder[renderIndex];
\r
180 if(!av.featuresDisplayed.containsKey(type))
\r
183 // loop through all features in sequence to find
\r
184 // current feature to render
\r
185 for (sfindex = 0; sfindex < sfSize; sfindex++)
\r
187 if (!sequenceFeatures[sfindex].type.equals(type))
\r
190 if (featureGroups != null
\r
191 && sequenceFeatures[sfindex].featureGroup != null
\r
193 featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
\r
195 ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].featureGroup)).
\r
201 if (!overview && (sequenceFeatures[sfindex].getBegin() > epos
\r
202 || sequenceFeatures[sfindex].getEnd() < spos))
\r
207 if (sequenceFeatures[sfindex].begin <= start &&
\r
208 sequenceFeatures[sfindex].end >= start)
\r
210 currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
\r
215 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
\r
218 renderFeature(g, seq,
\r
219 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
220 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
221 new Color( ( (Integer) av.featuresDisplayed.get(
\r
222 sequenceFeatures[sfindex].type)).intValue()),
\r
223 start, end, y1, width, height);
\r
224 renderFeature(g, seq,
\r
225 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
226 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
227 new Color( ( (Integer) av.featuresDisplayed.get(
\r
228 sequenceFeatures[sfindex].type)).intValue()),
\r
229 start, end, y1, width, height);
\r
233 renderFeature(g, seq,
\r
234 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
235 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
236 getColour(sequenceFeatures[sfindex].type),
\r
237 start, end, y1, width, height);
\r
246 void renderFeature(Graphics g, SequenceI seq,
\r
247 int fstart, int fend, Color featureColour, int start, int end, int y1, int width, int height)
\r
250 if (((fstart <= end) && (fend >= start)))
\r
252 if (fstart < start)
\r
253 { // fix for if the feature we have starts before the sequence start,
\r
254 fstart = start; // but the feature end is still valid!!
\r
262 for (i = fstart; i <= fend; i++)
\r
264 s = seq.getSequence().charAt(i);
\r
266 if (jalview.util.Comparison.isGap(s))
\r
271 g.setColor(featureColour);
\r
273 g.fillRect( (i - start) * width, y1, width, height);
\r
275 g.setColor(Color.white);
\r
276 charOffset = (width - fm.charWidth(s)) / 2;
\r
277 g.drawString(String.valueOf(s),
\r
278 charOffset + (width * (i - start)),
\r
279 (y1 + height) - height / 5); //pady = height / 5;
\r
285 void findAllFeatures()
\r
287 av.featuresDisplayed = new Hashtable();
\r
288 Vector allfeatures = new Vector();
\r
289 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
291 SequenceFeature [] features = av.alignment.getSequenceAt(i).getSequenceFeatures();
\r
293 if (features == null)
\r
297 while (index < features.length)
\r
299 if (!av.featuresDisplayed.containsKey(features[index].getType()))
\r
301 av.featuresDisplayed.put(features[index].getType(),
\r
302 new Integer( getColour(features[index].getType()).getRGB()) );
\r
303 allfeatures.addElement(features[index].getType());
\r
309 renderOrder = new String[allfeatures.size()];
\r
310 Enumeration en = allfeatures.elements();
\r
311 int i = allfeatures.size()-1;
\r
312 while(en.hasMoreElements())
\r
314 renderOrder[i] = en.nextElement().toString();
\r
319 public Color getColour(String featureType)
\r
321 return (Color)featureColours.get(featureType);
\r
324 public void addNewFeature(String name, Color col)
\r
327 setColour(name, col);
\r
328 if(av.featuresDisplayed==null)
\r
329 av.featuresDisplayed = new Hashtable();
\r
332 av.featuresDisplayed.put(name, "NOGROUP");
\r
335 public void setColour(String featureType, Color col)
\r
337 featureColours.put(featureType, col);
\r
340 public void setFeaturePriority(Object [][] data)
\r
342 // The feature table will display high priority
\r
343 // features at the top, but theses are the ones
\r
344 // we need to render last, so invert the data
\r
345 if(av.featuresDisplayed!=null)
\r
346 av.featuresDisplayed.clear();
\r
348 renderOrder = new String[data.length];
\r
350 if (data.length > 0)
\r
351 for (int i = 0; i < data.length; i++)
\r
353 String type = data[i][0].toString();
\r
354 setColour(type, (Color) data[i][1]);
\r
355 if ( ( (Boolean) data[i][2]).booleanValue())
\r
357 av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
\r
360 renderOrder[data.length - i - 1] = type;
\r
364 Hashtable featureColours = new Hashtable();
\r
367 featureColours.put("active site", new Color(255, 75, 0));
\r
368 featureColours.put("binding site", new Color(245, 85, 0));
\r
369 featureColours.put("calcium-binding region", new Color(235, 95, 0));
\r
370 featureColours.put("chain", new Color(225, 105, 0));
\r
371 featureColours.put("coiled-coil region", new Color(215, 115, 0));
\r
372 featureColours.put("compositionally biased region", new Color(205, 125, 0));
\r
373 featureColours.put("cross-link", new Color(195, 135, 0));
\r
374 featureColours.put("disulfide bond", new Color(185, 145, 0));
\r
375 featureColours.put("DNA-binding region", new Color(175, 155, 0));
\r
376 featureColours.put("domain", new Color(165, 165, 0));
\r
377 featureColours.put("glycosylation site", new Color(155, 175, 0));
\r
378 featureColours.put("helix", new Color(145, 185, 0));
\r
379 featureColours.put("initiator methionine", new Color(135, 195, 5));
\r
380 featureColours.put("lipid moiety-binding region", new Color(125, 205, 15));
\r
381 featureColours.put("metal ion-binding site", new Color(115, 215, 25));
\r
382 featureColours.put("modified residue", new Color(105, 225, 35));
\r
383 featureColours.put("mutagenesis site", new Color(95, 235, 45));
\r
384 featureColours.put("non-consecutive residues", new Color(85, 245, 55));
\r
385 featureColours.put("non-terminal residue", new Color(75, 255, 65));
\r
386 featureColours.put("nucleotide phosphate-binding region",
\r
387 new Color(65, 245, 75));
\r
388 featureColours.put("peptide", new Color(55, 235, 85));
\r
389 featureColours.put("propeptide", new Color(45, 225, 95));
\r
390 featureColours.put("region of interest", new Color(35, 215, 105));
\r
391 featureColours.put("repeat", new Color(25, 205, 115));
\r
392 featureColours.put("selenocysteine", new Color(15, 195, 125));
\r
393 featureColours.put("sequence conflict", new Color(5, 185, 135));
\r
394 featureColours.put("sequence variant", new Color(0, 175, 145));
\r
395 featureColours.put("short sequence motif", new Color(0, 165, 155));
\r
396 featureColours.put("signal peptide", new Color(0, 155, 165));
\r
397 featureColours.put("site", new Color(0, 145, 175));
\r
398 featureColours.put("splice variant", new Color(0, 135, 185));
\r
399 featureColours.put("strand", new Color(0, 125, 195));
\r
400 featureColours.put("topological domain", new Color(0, 115, 205));
\r
401 featureColours.put("transit peptide", new Color(0, 105, 215));
\r
402 featureColours.put("transmembrane region", new Color(0, 95, 225));
\r
403 featureColours.put("turn", new Color(0, 85, 235));
\r
404 featureColours.put("unsure residue", new Color(0, 75, 245));
\r
405 featureColours.put("zinc finger region", new Color(0, 65, 255));
\r