2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
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
10 * of the License, or (at your option) any later version.
12 * Jalview is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
21 package jalview.appletgui;
23 import jalview.api.FeatureRenderer;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.renderer.ResidueShaderI;
28 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
33 public class SequenceRenderer implements jalview.api.SequenceRenderer
35 final static int CHAR_TO_UPPER = 'A' - 'a';
41 boolean renderGaps = true;
43 SequenceGroup currentSequenceGroup = null;
45 SequenceGroup[] allGroups = null;
51 boolean forOverview = false;
53 public SequenceRenderer(AlignViewport av)
64 public void prepare(Graphics g, boolean renderGaps)
67 fm = g.getFontMetrics();
69 this.renderGaps = renderGaps;
73 public Color getResidueBoxColour(SequenceI seq, int i)
75 allGroups = av.getAlignment().findAllGroups(seq);
77 if (inCurrentSequenceGroup(i))
79 if (currentSequenceGroup.getDisplayBoxes())
81 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
84 else if (av.getShowBoxes())
86 getBoxColour(av.getResidueShading(), seq, i);
93 * Get the residue colour at the given sequence position - as determined by
94 * the sequence group colour (if any), else the colour scheme, possibly
95 * overridden by a feature colour.
103 public Color getResidueColour(final SequenceI seq, int position,
106 // TODO replace 8 or so code duplications with calls to this method
107 // (refactored as needed)
108 Color col = getResidueBoxColour(seq, position);
112 col = fr.findFeatureColour(col, seq, position);
117 void getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
121 resBoxColour = shader.findColour(seq.getCharAt(i), i, seq);
124 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
126 resBoxColour = Color.lightGray;
130 resBoxColour = Color.white;
135 public Color findSequenceColour(SequenceI seq, int i)
137 allGroups = av.getAlignment().findAllGroups(seq);
138 drawBoxes(seq, i, i, 0);
142 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
152 drawBoxes(seq, start, end, y1);
154 if (av.validCharWidth)
156 drawText(seq, start, end, y1);
160 public void drawBoxes(SequenceI seq, int start, int end, int y1)
163 int length = seq.getLength();
166 int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
169 Color tempColour = null;
172 resBoxColour = Color.white;
175 if (inCurrentSequenceGroup(i))
177 if (currentSequenceGroup.getDisplayBoxes())
179 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
183 else if (av.getShowBoxes())
185 getBoxColour(av.getResidueShading(), seq, i);
189 if (resBoxColour != tempColour)
191 if (tempColour != null)
193 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
196 graphics.setColor(resBoxColour);
199 curWidth = avCharWidth;
200 tempColour = resBoxColour;
205 curWidth += avCharWidth;
211 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
215 public void drawText(SequenceI seq, int start, int end, int y1)
217 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
218 Font boldFont = null;
219 boolean bold = false;
220 if (av.isUpperCasebold())
222 boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
224 graphics.setFont(av.getFont());
227 y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
231 // Need to find the sequence position here.
232 if (end + 1 >= seq.getLength())
234 end = seq.getLength() - 1;
238 boolean srep = av.isDisplayReferenceSeq();
239 for (int i = start; i <= end; i++)
241 graphics.setColor(Color.black);
243 s = seq.getCharAt(i);
244 if (!renderGaps && jalview.util.Comparison.isGap(s))
249 if (inCurrentSequenceGroup(i))
251 if (!currentSequenceGroup.getDisplayText())
256 if (currentSequenceGroup.getColourText())
258 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
259 graphics.setColor(resBoxColour.darker());
261 if (currentSequenceGroup.getShowNonconserved())
263 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
268 if (!av.getShowText())
273 if (av.getColourText())
275 getBoxColour(av.getResidueShading(), seq, i);
276 if (av.getShowBoxes())
278 graphics.setColor(resBoxColour.darker());
282 graphics.setColor(resBoxColour);
285 if (av.getShowUnconserved())
287 s = getDisplayChar(srep, i, s, '.', null);
292 if (av.isUpperCasebold())
294 fm = graphics.getFontMetrics();
295 if ('A' <= s && s <= 'Z')
300 graphics.setFont(boldFont);
306 graphics.setFont(av.font);
312 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
313 graphics.drawString(String.valueOf(s), charOffset + avCharWidth
320 * Returns 'conservedChar' to represent the given position if the sequence
321 * character at that position is equal to the consensus (ignoring case), else
322 * returns the sequence character
326 * @param sequenceChar
327 * @param conservedChar
330 private char getDisplayChar(final boolean usesrep, int position,
331 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
333 // TODO - use currentSequenceGroup rather than alignment
334 // currentSequenceGroup.getConsensus()
335 char conschar = (usesrep) ? (currentGroup == null
336 || position < currentGroup.getStartRes()
337 || position > currentGroup.getEndRes() ? av.getAlignment()
338 .getSeqrep().getCharAt(position)
339 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
340 .getCharAt(position) : av.getAlignment().getSeqrep()
341 .getCharAt(position)))
342 : (currentGroup != null && currentGroup.getConsensus() != null
343 && position >= currentGroup.getStartRes()
344 && position <= currentGroup.getEndRes() && currentGroup
345 .getConsensus().annotations.length > position) ? currentGroup
346 .getConsensus().annotations[position].displayCharacter
348 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
350 if (!jalview.util.Comparison.isGap(conschar)
351 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
353 sequenceChar = conservedChar;
358 boolean inCurrentSequenceGroup(int res)
360 if (allGroups == null)
365 for (int i = 0; i < allGroups.length; i++)
367 if (allGroups[i].getStartRes() <= res
368 && allGroups[i].getEndRes() >= res)
370 currentSequenceGroup = allGroups[i];
378 public void drawHighlightedText(SequenceI seq, int start, int end,
381 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
382 int pady = avCharHeight / 5;
384 graphics.setColor(Color.black);
385 graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
386 graphics.setColor(Color.white);
389 // Need to find the sequence position here.
390 if (av.validCharWidth)
392 for (int i = start; i <= end; i++)
394 if (i < seq.getLength())
396 s = seq.getCharAt(i);
399 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
400 graphics.drawString(String.valueOf(s), charOffset + x1
401 + avCharWidth * (i - start), y1 + avCharHeight - pady);
406 public void drawCursor(SequenceI seq, int res, int x1, int y1)
408 int pady = av.getCharHeight() / 5;
410 graphics.setColor(Color.black);
411 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
412 graphics.setColor(Color.white);
414 graphics.setColor(Color.white);
416 char s = seq.getCharAt(res);
417 if (av.validCharWidth)
420 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
421 graphics.drawString(String.valueOf(s), charOffset + x1,
422 (y1 + av.getCharHeight()) - pady);