2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3 * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
5 * This file is part of Jalview.
7 * Jalview is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
22 import jalview.datamodel.*;
23 import jalview.schemes.*;
31 public class SequenceRenderer implements jalview.api.SequenceRenderer
37 boolean renderGaps = true;
39 SequenceGroup currentSequenceGroup = null;
41 SequenceGroup[] allGroups = null;
47 boolean monospacedFont;
49 boolean forOverview = false;
52 * Creates a new SequenceRenderer object.
57 public SequenceRenderer(AlignViewport av)
68 public void prepare(Graphics g, boolean renderGaps)
71 fm = g.getFontMetrics();
73 // If EPS graphics, stringWidth will be a double, not an int
74 double dwidth = fm.getStringBounds("M", g).getWidth();
76 monospacedFont = (dwidth == fm.getStringBounds("|", g).getWidth() && (float) av.charWidth == dwidth);
78 this.renderGaps = renderGaps;
81 public Color getResidueBoxColour(SequenceI seq, int i)
83 allGroups = av.alignment.findAllGroups(seq);
85 if (inCurrentSequenceGroup(i))
87 if (currentSequenceGroup.getDisplayBoxes())
89 getBoxColour(currentSequenceGroup.cs, seq, i);
92 else if (av.getShowBoxes())
94 getBoxColour(av.globalColourScheme, seq, i);
110 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
114 resBoxColour = cs.findColour(seq.getCharAt(i), i);
117 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
119 resBoxColour = Color.lightGray;
123 resBoxColour = Color.white;
128 * draw coloured sequence to the current graphics cursor
134 * @param drawBox - draw background
135 * @param drawText - draw text (if shading style permits)
137 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
138 int end, int y1, boolean drawBox, boolean drawText)
143 drawBoxes(seq, start, end, y1);
146 if (av.validCharWidth && drawText)
148 drawText(seq, start, end, y1);
170 public synchronized void drawBoxes(SequenceI seq, int start, int end,
174 return; // fix for racecondition
176 int length = seq.getLength();
179 int curWidth = av.charWidth;
181 Color tempColour = null;
185 resBoxColour = Color.white;
189 if (inCurrentSequenceGroup(i))
191 if (currentSequenceGroup.getDisplayBoxes())
193 getBoxColour(currentSequenceGroup.cs, seq, i);
196 else if (av.getShowBoxes())
198 getBoxColour(av.globalColourScheme, seq, i);
203 if (resBoxColour != tempColour)
205 if (tempColour != null)
207 graphics.fillRect(av.charWidth * (curStart - start), y1,
208 curWidth, av.charHeight);
211 graphics.setColor(resBoxColour);
214 curWidth = av.charWidth;
215 tempColour = resBoxColour;
219 curWidth += av.charWidth;
225 graphics.fillRect(av.charWidth * (curStart - start), y1, curWidth,
248 public void drawText(SequenceI seq, int start, int end, int y1)
250 y1 += av.charHeight - av.charHeight / 5; // height/5 replaces pady
254 if (end + 1 >= seq.getLength())
256 end = seq.getLength() - 1;
258 graphics.setColor(av.textColour);
260 if (monospacedFont && av.showText && allGroups.length == 0
261 && !av.getColourText() && av.thresholdTextColour == 0)
265 graphics.drawString(seq.getSequenceAsString(start, end + 1), 0, y1);
269 char gap = av.getGapCharacter();
270 graphics.drawString(seq.getSequenceAsString(start, end + 1)
271 .replace(gap, ' '), 0, y1);
276 boolean getboxColour = false;
277 for (int i = start; i <= end; i++)
279 graphics.setColor(av.textColour);
280 getboxColour = false;
281 s = seq.getCharAt(i);
282 if (!renderGaps && jalview.util.Comparison.isGap(s))
287 if (inCurrentSequenceGroup(i))
289 if (!currentSequenceGroup.getDisplayText())
294 if (currentSequenceGroup.thresholdTextColour > 0
295 || currentSequenceGroup.getColourText())
298 getBoxColour(currentSequenceGroup.cs, seq, i);
300 if (currentSequenceGroup.getColourText())
302 graphics.setColor(resBoxColour.darker());
305 if (currentSequenceGroup.thresholdTextColour > 0)
307 if (resBoxColour.getRed() + resBoxColour.getBlue()
308 + resBoxColour.getGreen() < currentSequenceGroup.thresholdTextColour)
310 graphics.setColor(currentSequenceGroup.textColour2);
316 graphics.setColor(currentSequenceGroup.textColour);
318 if (currentSequenceGroup.getShowNonconserved()) // todo optimize
320 // todo - use sequence group consensus
321 s = getDisplayChar(av.consensus, i, s, '.');
328 if (!av.getShowText())
333 if (av.getColourText())
336 getBoxColour(av.globalColourScheme, seq, i);
338 if (av.getShowBoxes())
340 graphics.setColor(resBoxColour.darker());
344 graphics.setColor(resBoxColour);
348 if (av.thresholdTextColour > 0)
352 getBoxColour(av.globalColourScheme, seq, i);
355 if (resBoxColour.getRed() + resBoxColour.getBlue()
356 + resBoxColour.getGreen() < av.thresholdTextColour)
358 graphics.setColor(av.textColour2);
361 if (av.showUnconserved)
363 s = getDisplayChar(av.consensus, i, s, '.');
369 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
370 graphics.drawString(String.valueOf(s), charOffset + av.charWidth
377 private char getDisplayChar(AlignmentAnnotation consensus, int position,
380 char conschar = consensus.annotations[position].displayCharacter
382 if (conschar != '-' && s == conschar)
395 * @return DOCUMENT ME!
397 boolean inCurrentSequenceGroup(int res)
399 if (allGroups == null)
404 for (int i = 0; i < allGroups.length; i++)
406 if ((allGroups[i].getStartRes() <= res)
407 && (allGroups[i].getEndRes() >= res))
409 currentSequenceGroup = allGroups[i];
436 public void drawHighlightedText(SequenceI seq, int start, int end,
439 int pady = av.charHeight / 5;
441 graphics.setColor(Color.BLACK);
442 graphics.fillRect(x1, y1, av.charWidth * (end - start + 1),
444 graphics.setColor(Color.white);
448 // Need to find the sequence position here.
449 if (av.validCharWidth)
451 for (int i = start; i <= end; i++)
453 if (i < seq.getLength())
455 s = seq.getCharAt(i);
458 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
459 graphics.drawString(String.valueOf(s), charOffset + x1
460 + (av.charWidth * (i - start)), (y1 + av.charHeight) - pady);
465 public void drawCursor(SequenceI seq, int res, int x1, int y1)
467 int pady = av.charHeight / 5;
469 graphics.setColor(Color.black);
470 graphics.fillRect(x1, y1, av.charWidth, av.charHeight);
472 if (av.validCharWidth)
474 graphics.setColor(Color.white);
476 char s = seq.getCharAt(res);
478 charOffset = (av.charWidth - fm.charWidth(s)) / 2;
479 graphics.drawString(String.valueOf(s), charOffset + x1,
480 (y1 + av.charHeight) - pady);