3 * Jalview - A Sequence Alignment Editor and Viewer
\r
4 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
\r
6 * This program is free software; you can redistribute it and/or
\r
7 * modify it under the terms of the GNU General Public License
\r
8 * as published by the Free Software Foundation; either version 2
\r
9 * of the License, or (at your option) any later version.
\r
11 * This program is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with this program; if not, write to the Free Software
\r
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
\r
20 package jalview.appletgui;
\r
22 import jalview.datamodel.*;
\r
32 * @version $Revision$
\r
34 public class FeatureRenderer
\r
38 Hashtable featureColours = new Hashtable();
\r
40 // A higher level for grouping features of a
\r
42 Hashtable featureGroups = null;
\r
44 // Holds web links for feature groups and feature types
\r
45 // in the form label|link
\r
46 Hashtable featureLinks = null;
\r
49 // This is actually an Integer held in the hashtable,
\r
50 // Retrieved using the key feature type
\r
51 Object currentColour;
\r
53 String [] renderOrder;
\r
58 float transparency = 1f;
\r
60 TransparencySetter transparencySetter = null;
\r
63 * Creates a new FeatureRenderer object.
\r
65 * @param av DOCUMENT ME!
\r
67 public FeatureRenderer(AlignViewport av)
\r
71 if(!System.getProperty("java.version").startsWith("1.1"))
\r
72 transparencySetter = new TransparencySetter();
\r
76 public void transferSettings(FeatureRenderer fr)
\r
78 renderOrder = fr.renderOrder;
\r
79 featureGroups = fr.featureGroups;
\r
80 featureColours = fr.featureColours;
\r
81 transparency = fr.transparency;
\r
85 public Color findFeatureColour(Color initialCol, SequenceI seq, int i)
\r
88 if(!av.showSequenceFeatures)
\r
92 sequenceFeatures = lastSequence.getSequenceFeatures();
\r
93 if(sequenceFeatures==null)
\r
96 sfSize = sequenceFeatures.length;
\r
98 if(jalview.util.Comparison.isGap(lastSequence.getCharAt(i)))
\r
101 currentColour = null;
\r
103 drawSequence(null, lastSequence, lastSequence.findPosition(i), -1,-1);
\r
105 if(currentColour==null)
\r
108 return new Color( ((Integer)currentColour).intValue() );
\r
112 * This is used by the Molecule Viewer to get the accurate colour
\r
113 * of the rendered sequence
\r
115 boolean overview = false;
\r
117 int white = Color.white.getRGB();
\r
118 public int findFeatureColour(int initialCol, int seqIndex, int column)
\r
120 if(!av.showSequenceFeatures)
\r
123 if(seqIndex!=lastSequenceIndex)
\r
125 lastSequence = av.alignment.getSequenceAt(seqIndex);
\r
126 lastSequenceIndex = seqIndex;
\r
127 sequenceFeatures = lastSequence.getSequenceFeatures();
\r
128 if(sequenceFeatures==null)
\r
131 sfSize = sequenceFeatures.length;
\r
135 if(jalview.util.Comparison.isGap(lastSequence.getCharAt(column)))
\r
136 return Color.white.getRGB();
\r
138 currentColour = null;
\r
140 drawSequence(null, lastSequence, lastSequence.findPosition(column), -1,-1);
\r
142 if(currentColour==null)
\r
145 return ((Integer)currentColour).intValue();
\r
152 * @param g DOCUMENT ME!
\r
153 * @param seq DOCUMENT ME!
\r
154 * @param sg DOCUMENT ME!
\r
155 * @param start DOCUMENT ME!
\r
156 * @param end DOCUMENT ME!
\r
157 * @param x1 DOCUMENT ME!
\r
158 * @param y1 DOCUMENT ME!
\r
159 * @param width DOCUMENT ME!
\r
160 * @param height DOCUMENT ME!
\r
163 // SequenceFeature sf;
\r
164 int lastSequenceIndex=-1;
\r
165 SequenceI lastSequence;
\r
166 SequenceFeature [] sequenceFeatures;
\r
167 int sfSize, sfindex, spos, epos;
\r
169 public void drawSequence(Graphics g, SequenceI seq,
\r
170 int start, int end, int y1)
\r
172 if ( seq.getSequenceFeatures() == null
\r
173 || seq.getSequenceFeatures().length==0)
\r
176 if(transparencySetter!=null && g!=null)
\r
178 transparencySetter.setTransparency(g, transparency);
\r
181 if (av.featuresDisplayed == null || renderOrder==null)
\r
184 if(av.featuresDisplayed.size()<1)
\r
187 sequenceFeatures = seq.getSequenceFeatures();
\r
188 sfSize = sequenceFeatures.length;
\r
190 if(lastSequence==null || seq!=lastSequence)
\r
192 lastSequence = seq;
\r
193 sequenceFeatures = seq.getSequenceFeatures();
\r
194 sfSize = sequenceFeatures.length;
\r
198 spos = lastSequence.findPosition(start);
\r
199 epos = lastSequence.findPosition(end);
\r
201 fm = g.getFontMetrics();
\r
204 for(int renderIndex=0; renderIndex<renderOrder.length; renderIndex++)
\r
206 type = renderOrder[renderIndex];
\r
207 if(!av.featuresDisplayed.containsKey(type))
\r
210 // loop through all features in sequence to find
\r
211 // current feature to render
\r
212 for (sfindex = 0; sfindex < sfSize; sfindex++)
\r
214 if (!sequenceFeatures[sfindex].type.equals(type))
\r
217 if (featureGroups != null
\r
218 && sequenceFeatures[sfindex].featureGroup != null
\r
220 featureGroups.containsKey(sequenceFeatures[sfindex].featureGroup)
\r
222 ! ( (Boolean) featureGroups.get(sequenceFeatures[sfindex].featureGroup)).
\r
228 if (!overview && (sequenceFeatures[sfindex].getBegin() > epos
\r
229 || sequenceFeatures[sfindex].getEnd() < spos))
\r
234 if (sequenceFeatures[sfindex].begin <= start &&
\r
235 sequenceFeatures[sfindex].end >= start)
\r
237 currentColour = av.featuresDisplayed.get(sequenceFeatures[sfindex].
\r
242 else if (sequenceFeatures[sfindex].type.equals("disulfide bond"))
\r
245 renderFeature(g, seq,
\r
246 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
247 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
248 new Color( ( (Integer) av.featuresDisplayed.get(
\r
249 sequenceFeatures[sfindex].type)).intValue()),
\r
251 renderFeature(g, seq,
\r
252 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
253 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
254 new Color( ( (Integer) av.featuresDisplayed.get(
\r
255 sequenceFeatures[sfindex].type)).intValue()),
\r
260 renderFeature(g, seq,
\r
261 seq.findIndex(sequenceFeatures[sfindex].begin) - 1,
\r
262 seq.findIndex(sequenceFeatures[sfindex].end) - 1,
\r
263 getColour(sequenceFeatures[sfindex].type),
\r
269 if(transparencySetter!=null && g!=null)
\r
271 transparencySetter.setTransparency(g, 1.0f);
\r
278 void renderFeature(Graphics g, SequenceI seq,
\r
279 int fstart, int fend, Color featureColour, int start, int end, int y1)
\r
282 if (((fstart <= end) && (fend >= start)))
\r
284 if (fstart < start)
\r
285 { // fix for if the feature we have starts before the sequence start,
\r
286 fstart = start; // but the feature end is still valid!!
\r
294 for (i = fstart; i <= fend; i++)
\r
296 s = seq.getSequence().charAt(i);
\r
298 if (jalview.util.Comparison.isGap(s))
\r
303 g.setColor(featureColour);
\r
305 g.fillRect( (i - start) * av.charWidth, y1, av.charWidth, av.charHeight);
\r
307 if(!av.validCharWidth)
\r
310 g.setColor(Color.white);
\r
311 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
\r
312 g.drawString(String.valueOf(s),
\r
313 charOffset + (av.charWidth * (i - start)),
\r
314 (y1 + av.charHeight) - av.charHeight / 5); //pady = height / 5;
\r
320 void findAllFeatures()
\r
322 jalview.schemes.UserColourScheme ucs = new
\r
323 jalview.schemes.UserColourScheme();
\r
325 av.featuresDisplayed = new Hashtable();
\r
326 Vector allfeatures = new Vector();
\r
327 for (int i = 0; i < av.alignment.getHeight(); i++)
\r
329 SequenceFeature [] features = av.alignment.getSequenceAt(i).getSequenceFeatures();
\r
331 if (features == null)
\r
335 while (index < features.length)
\r
337 if (!av.featuresDisplayed.containsKey(features[index].getType()))
\r
339 if (getColour(features[index].getType()) == null)
\r
341 featureColours.put(features[index].getType(),
\r
342 ucs.createColourFromName(features[index].
\r
347 av.featuresDisplayed.put(features[index].getType(),
\r
348 new Integer( getColour(features[index].getType()).getRGB()) );
\r
349 allfeatures.addElement(features[index].getType());
\r
355 renderOrder = new String[allfeatures.size()];
\r
356 Enumeration en = allfeatures.elements();
\r
357 int i = allfeatures.size()-1;
\r
358 while(en.hasMoreElements())
\r
360 renderOrder[i] = en.nextElement().toString();
\r
365 public Color getColour(String featureType)
\r
367 return (Color)featureColours.get(featureType);
\r
370 public void addNewFeature(String name, Color col)
\r
373 setColour(name, col);
\r
374 if(av.featuresDisplayed==null)
\r
375 av.featuresDisplayed = new Hashtable();
\r
378 av.featuresDisplayed.put(name, "NOGROUP");
\r
381 public void setColour(String featureType, Color col)
\r
383 featureColours.put(featureType, col);
\r
386 public void setFeaturePriority(Object [][] data)
\r
388 // The feature table will display high priority
\r
389 // features at the top, but theses are the ones
\r
390 // we need to render last, so invert the data
\r
391 if(av.featuresDisplayed!=null)
\r
392 av.featuresDisplayed.clear();
\r
394 renderOrder = new String[data.length];
\r
396 if (data.length > 0)
\r
397 for (int i = 0; i < data.length; i++)
\r
399 String type = data[i][0].toString();
\r
400 setColour(type, (Color) data[i][1]);
\r
401 if ( ( (Boolean) data[i][2]).booleanValue())
\r
403 av.featuresDisplayed.put(type, new Integer(getColour(type).getRGB()));
\r
406 renderOrder[data.length - i - 1] = type;
\r
411 class TransparencySetter
\r
413 void setTransparency(Graphics g, float value)
\r
415 Graphics2D g2 = (Graphics2D) g;
\r
417 AlphaComposite.getInstance(
\r
418 AlphaComposite.SRC_OVER, value));
\r