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.datamodel.SequenceGroup;
24 import jalview.datamodel.SequenceI;
25 import jalview.renderer.ResidueShaderI;
26 import jalview.renderer.seqfeatures.FeatureColourFinder;
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 public SequenceRenderer(AlignViewport av)
62 public void prepare(Graphics g, boolean renderGaps)
65 fm = g.getFontMetrics();
67 this.renderGaps = renderGaps;
70 protected Color getResidueBoxColour(SequenceI seq, int i)
72 allGroups = av.getAlignment().findAllGroups(seq);
74 if (inCurrentSequenceGroup(i))
76 if (currentSequenceGroup.getDisplayBoxes())
78 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
81 else if (av.getShowBoxes())
83 getBoxColour(av.getResidueShading(), seq, i);
90 * Get the residue colour at the given sequence position - as determined by
91 * the sequence group colour (if any), else the colour scheme, possibly
92 * overridden by a feature colour.
100 public Color getResidueColour(final SequenceI seq, int position,
101 FeatureColourFinder finder)
103 // TODO replace 8 or so code duplications with calls to this method
104 // (refactored as needed)
105 Color col = getResidueBoxColour(seq, position);
109 col = finder.findFeatureColour(col, seq, position);
114 void getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
116 if (shader.getColourScheme() != null)
118 resBoxColour = shader.findColour(seq.getCharAt(i), i, seq);
122 resBoxColour = Color.white;
127 public Color findSequenceColour(SequenceI seq, int i)
129 allGroups = av.getAlignment().findAllGroups(seq);
130 drawBoxes(seq, i, i, 0);
134 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
144 drawBoxes(seq, start, end, y1);
146 if (av.validCharWidth)
148 drawText(seq, start, end, y1);
152 public void drawBoxes(SequenceI seq, int start, int end, int y1)
155 int length = seq.getLength();
158 int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
161 Color tempColour = null;
164 resBoxColour = Color.white;
167 if (inCurrentSequenceGroup(i))
169 if (currentSequenceGroup.getDisplayBoxes())
171 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
175 else if (av.getShowBoxes())
177 getBoxColour(av.getResidueShading(), seq, i);
181 if (resBoxColour != tempColour)
183 if (tempColour != null)
185 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
188 graphics.setColor(resBoxColour);
191 curWidth = avCharWidth;
192 tempColour = resBoxColour;
197 curWidth += avCharWidth;
203 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
207 public void drawText(SequenceI seq, int start, int end, int y1)
209 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
210 Font boldFont = null;
211 boolean bold = false;
212 if (av.isUpperCasebold())
214 boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
216 graphics.setFont(av.getFont());
219 y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
223 // Need to find the sequence position here.
224 if (end + 1 >= seq.getLength())
226 end = seq.getLength() - 1;
230 boolean srep = av.isDisplayReferenceSeq();
231 for (int i = start; i <= end; i++)
233 graphics.setColor(Color.black);
235 s = seq.getCharAt(i);
236 if (!renderGaps && jalview.util.Comparison.isGap(s))
241 if (inCurrentSequenceGroup(i))
243 if (!currentSequenceGroup.getDisplayText())
248 if (currentSequenceGroup.getColourText())
250 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
251 graphics.setColor(resBoxColour.darker());
253 if (currentSequenceGroup.getShowNonconserved())
255 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
260 if (!av.getShowText())
265 if (av.getColourText())
267 getBoxColour(av.getResidueShading(), seq, i);
268 if (av.getShowBoxes())
270 graphics.setColor(resBoxColour.darker());
274 graphics.setColor(resBoxColour);
277 if (av.getShowUnconserved())
279 s = getDisplayChar(srep, i, s, '.', null);
284 if (av.isUpperCasebold())
286 fm = graphics.getFontMetrics();
287 if ('A' <= s && s <= 'Z')
292 graphics.setFont(boldFont);
298 graphics.setFont(av.font);
304 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
305 graphics.drawString(String.valueOf(s), charOffset + avCharWidth
312 * Returns 'conservedChar' to represent the given position if the sequence
313 * character at that position is equal to the consensus (ignoring case), else
314 * returns the sequence character
318 * @param sequenceChar
319 * @param conservedChar
322 private char getDisplayChar(final boolean usesrep, int position,
323 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
325 // TODO - use currentSequenceGroup rather than alignment
326 // currentSequenceGroup.getConsensus()
327 char conschar = (usesrep) ? (currentGroup == null
328 || position < currentGroup.getStartRes()
329 || position > currentGroup.getEndRes() ? av.getAlignment()
330 .getSeqrep().getCharAt(position)
331 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
332 .getCharAt(position) : av.getAlignment().getSeqrep()
333 .getCharAt(position)))
334 : (currentGroup != null && currentGroup.getConsensus() != null
335 && position >= currentGroup.getStartRes()
336 && position <= currentGroup.getEndRes() && currentGroup
337 .getConsensus().annotations.length > position) ? currentGroup
338 .getConsensus().annotations[position].displayCharacter
340 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
342 if (!jalview.util.Comparison.isGap(conschar)
343 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
345 sequenceChar = conservedChar;
350 boolean inCurrentSequenceGroup(int res)
352 if (allGroups == null)
357 for (int i = 0; i < allGroups.length; i++)
359 if (allGroups[i].getStartRes() <= res
360 && allGroups[i].getEndRes() >= res)
362 currentSequenceGroup = allGroups[i];
370 public void drawHighlightedText(SequenceI seq, int start, int end,
373 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
374 int pady = avCharHeight / 5;
376 graphics.setColor(Color.black);
377 graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
378 graphics.setColor(Color.white);
381 // Need to find the sequence position here.
382 if (av.validCharWidth)
384 for (int i = start; i <= end; i++)
386 if (i < seq.getLength())
388 s = seq.getCharAt(i);
391 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
392 graphics.drawString(String.valueOf(s), charOffset + x1
393 + avCharWidth * (i - start), y1 + avCharHeight - pady);
398 public void drawCursor(SequenceI seq, int res, int x1, int y1)
400 int pady = av.getCharHeight() / 5;
402 graphics.setColor(Color.black);
403 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
404 graphics.setColor(Color.white);
406 graphics.setColor(Color.white);
408 char s = seq.getCharAt(res);
409 if (av.validCharWidth)
412 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
413 graphics.drawString(String.valueOf(s), charOffset + x1,
414 (y1 + av.getCharHeight()) - pady);