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.schemes.CollectionColourSchemeI;
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.getViewportColourScheme(), 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(CollectionColourSchemeI collectionColourSchemeI,
118 SequenceI seq, int i)
120 if (collectionColourSchemeI != null)
122 resBoxColour = collectionColourSchemeI.findColour(seq.getCharAt(i),
126 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
128 resBoxColour = Color.lightGray;
132 resBoxColour = Color.white;
137 public Color findSequenceColour(SequenceI seq, int i)
139 allGroups = av.getAlignment().findAllGroups(seq);
140 drawBoxes(seq, i, i, 0);
144 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
154 drawBoxes(seq, start, end, y1);
156 if (av.validCharWidth)
158 drawText(seq, start, end, y1);
162 public void drawBoxes(SequenceI seq, int start, int end, int y1)
165 int length = seq.getLength();
168 int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
171 Color tempColour = null;
174 resBoxColour = Color.white;
177 if (inCurrentSequenceGroup(i))
179 if (currentSequenceGroup.getDisplayBoxes())
181 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
185 else if (av.getShowBoxes())
187 getBoxColour(av.getViewportColourScheme(), seq, i);
191 if (resBoxColour != tempColour)
193 if (tempColour != null)
195 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
198 graphics.setColor(resBoxColour);
201 curWidth = avCharWidth;
202 tempColour = resBoxColour;
207 curWidth += avCharWidth;
213 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
217 public void drawText(SequenceI seq, int start, int end, int y1)
219 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
220 Font boldFont = null;
221 boolean bold = false;
222 if (av.isUpperCasebold())
224 boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
226 graphics.setFont(av.getFont());
229 y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
233 // Need to find the sequence position here.
234 if (end + 1 >= seq.getLength())
236 end = seq.getLength() - 1;
240 boolean srep = av.isDisplayReferenceSeq();
241 for (int i = start; i <= end; i++)
243 graphics.setColor(Color.black);
245 s = seq.getCharAt(i);
246 if (!renderGaps && jalview.util.Comparison.isGap(s))
251 if (inCurrentSequenceGroup(i))
253 if (!currentSequenceGroup.getDisplayText())
258 if (currentSequenceGroup.getColourText())
260 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
261 graphics.setColor(resBoxColour.darker());
263 if (currentSequenceGroup.getShowNonconserved())
265 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
270 if (!av.getShowText())
275 if (av.getColourText())
277 getBoxColour(av.getViewportColourScheme(), seq, i);
278 if (av.getShowBoxes())
280 graphics.setColor(resBoxColour.darker());
284 graphics.setColor(resBoxColour);
287 if (av.getShowUnconserved())
289 s = getDisplayChar(srep, i, s, '.', null);
294 if (av.isUpperCasebold())
296 fm = graphics.getFontMetrics();
297 if ('A' <= s && s <= 'Z')
302 graphics.setFont(boldFont);
308 graphics.setFont(av.font);
314 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
315 graphics.drawString(String.valueOf(s), charOffset + avCharWidth
322 * Returns 'conservedChar' to represent the given position if the sequence
323 * character at that position is equal to the consensus (ignoring case), else
324 * returns the sequence character
328 * @param sequenceChar
329 * @param conservedChar
332 private char getDisplayChar(final boolean usesrep, int position,
333 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
335 // TODO - use currentSequenceGroup rather than alignment
336 // currentSequenceGroup.getConsensus()
337 char conschar = (usesrep) ? (currentGroup == null
338 || position < currentGroup.getStartRes()
339 || position > currentGroup.getEndRes() ? av.getAlignment()
340 .getSeqrep().getCharAt(position)
341 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
342 .getCharAt(position) : av.getAlignment().getSeqrep()
343 .getCharAt(position)))
344 : (currentGroup != null && currentGroup.getConsensus() != null
345 && position >= currentGroup.getStartRes()
346 && position <= currentGroup.getEndRes() && currentGroup
347 .getConsensus().annotations.length > position) ? currentGroup
348 .getConsensus().annotations[position].displayCharacter
350 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
352 if (!jalview.util.Comparison.isGap(conschar)
353 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
355 sequenceChar = conservedChar;
360 boolean inCurrentSequenceGroup(int res)
362 if (allGroups == null)
367 for (int i = 0; i < allGroups.length; i++)
369 if (allGroups[i].getStartRes() <= res
370 && allGroups[i].getEndRes() >= res)
372 currentSequenceGroup = allGroups[i];
380 public void drawHighlightedText(SequenceI seq, int start, int end,
383 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
384 int pady = avCharHeight / 5;
386 graphics.setColor(Color.black);
387 graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
388 graphics.setColor(Color.white);
391 // Need to find the sequence position here.
392 if (av.validCharWidth)
394 for (int i = start; i <= end; i++)
396 if (i < seq.getLength())
398 s = seq.getCharAt(i);
401 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
402 graphics.drawString(String.valueOf(s), charOffset + x1
403 + avCharWidth * (i - start), y1 + avCharHeight - pady);
408 public void drawCursor(SequenceI seq, int res, int x1, int y1)
410 int pady = av.getCharHeight() / 5;
412 graphics.setColor(Color.black);
413 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
414 graphics.setColor(Color.white);
416 graphics.setColor(Color.white);
418 char s = seq.getCharAt(res);
419 if (av.validCharWidth)
422 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
423 graphics.drawString(String.valueOf(s), charOffset + x1,
424 (y1 + av.getCharHeight()) - pady);