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 boolean forOverview = false;
53 public SequenceRenderer(AlignViewport av)
64 public void prepare(Graphics g, boolean renderGaps)
67 fm = g.getFontMetrics();
69 this.renderGaps = renderGaps;
72 protected Color getResidueBoxColour(SequenceI seq, int i)
74 allGroups = av.getAlignment().findAllGroups(seq);
76 if (inCurrentSequenceGroup(i))
78 if (currentSequenceGroup.getDisplayBoxes())
80 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
83 else if (av.getShowBoxes())
85 getBoxColour(av.getResidueShading(), seq, i);
92 * Get the residue colour at the given sequence position - as determined by
93 * the sequence group colour (if any), else the colour scheme, possibly
94 * overridden by a feature colour.
102 public Color getResidueColour(final SequenceI seq, int position,
103 FeatureColourFinder finder)
105 // TODO replace 8 or so code duplications with calls to this method
106 // (refactored as needed)
107 Color col = getResidueBoxColour(seq, position);
111 col = finder.findFeatureColour(col, seq, position);
116 void getBoxColour(ResidueShaderI shader, SequenceI seq, int i)
118 if (shader.getColourScheme() != null)
120 resBoxColour = shader.findColour(seq.getCharAt(i), i, seq);
123 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
125 resBoxColour = Color.lightGray;
129 resBoxColour = Color.white;
134 public Color findSequenceColour(SequenceI seq, int i)
136 allGroups = av.getAlignment().findAllGroups(seq);
137 drawBoxes(seq, i, i, 0);
141 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
151 drawBoxes(seq, start, end, y1);
153 if (av.validCharWidth)
155 drawText(seq, start, end, y1);
159 public void drawBoxes(SequenceI seq, int start, int end, int y1)
162 int length = seq.getLength();
165 int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(),
166 avCharHeight = av.getCharHeight();
168 Color tempColour = null;
171 resBoxColour = Color.white;
174 if (inCurrentSequenceGroup(i))
176 if (currentSequenceGroup.getDisplayBoxes())
178 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq,
182 else if (av.getShowBoxes())
184 getBoxColour(av.getResidueShading(), seq, i);
188 if (resBoxColour != tempColour)
190 if (tempColour != null)
192 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
195 graphics.setColor(resBoxColour);
198 curWidth = avCharWidth;
199 tempColour = resBoxColour;
204 curWidth += avCharWidth;
210 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
214 public void drawText(SequenceI seq, int start, int end, int y1)
216 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
217 Font boldFont = null;
218 boolean bold = false;
219 if (av.isUpperCasebold())
221 boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
223 graphics.setFont(av.getFont());
226 y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
230 // Need to find the sequence position here.
231 if (end + 1 >= seq.getLength())
233 end = seq.getLength() - 1;
237 boolean srep = av.isDisplayReferenceSeq();
238 for (int i = start; i <= end; i++)
240 graphics.setColor(Color.black);
242 s = seq.getCharAt(i);
243 if (!renderGaps && jalview.util.Comparison.isGap(s))
248 if (inCurrentSequenceGroup(i))
250 if (!currentSequenceGroup.getDisplayText())
255 if (currentSequenceGroup.getColourText())
257 getBoxColour(currentSequenceGroup.getGroupColourScheme(), seq, i);
258 graphics.setColor(resBoxColour.darker());
260 if (currentSequenceGroup.getShowNonconserved())
262 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
267 if (!av.getShowText())
272 if (av.getColourText())
274 getBoxColour(av.getResidueShading(), seq, i);
275 if (av.getShowBoxes())
277 graphics.setColor(resBoxColour.darker());
281 graphics.setColor(resBoxColour);
284 if (av.getShowUnconserved())
286 s = getDisplayChar(srep, i, s, '.', null);
291 if (av.isUpperCasebold())
293 fm = graphics.getFontMetrics();
294 if ('A' <= s && s <= 'Z')
299 graphics.setFont(boldFont);
305 graphics.setFont(av.font);
311 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
312 graphics.drawString(String.valueOf(s),
313 charOffset + avCharWidth * (i - start), y1);
319 * Returns 'conservedChar' to represent the given position if the sequence
320 * character at that position is equal to the consensus (ignoring case), else
321 * returns the sequence character
325 * @param sequenceChar
326 * @param conservedChar
329 private char getDisplayChar(final boolean usesrep, int position,
330 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
332 // TODO - use currentSequenceGroup rather than alignment
333 // currentSequenceGroup.getConsensus()
334 char conschar = (usesrep) ? (currentGroup == null
335 || position < currentGroup.getStartRes()
336 || position > currentGroup.getEndRes()
337 ? av.getAlignment().getSeqrep().getCharAt(position)
338 : (currentGroup.getSeqrep() != null
339 ? currentGroup.getSeqrep().getCharAt(position)
340 : av.getAlignment().getSeqrep()
341 .getCharAt(position)))
342 : (currentGroup != null && currentGroup.getConsensus() != null
343 && position >= currentGroup.getStartRes()
344 && position <= currentGroup.getEndRes()
346 .getConsensus().annotations.length > position)
348 .getConsensus().annotations[position].displayCharacter
350 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
352 if (!jalview.util.Comparison.isGap(conschar)
353 && (sequenceChar == conschar
354 || sequenceChar + CHAR_TO_UPPER == conschar))
356 sequenceChar = conservedChar;
361 boolean inCurrentSequenceGroup(int res)
363 if (allGroups == null)
368 for (int i = 0; i < allGroups.length; i++)
370 if (allGroups[i].getStartRes() <= res
371 && allGroups[i].getEndRes() >= res)
373 currentSequenceGroup = allGroups[i];
381 public void drawHighlightedText(SequenceI seq, int start, int end, int x1,
384 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
385 int pady = avCharHeight / 5;
387 graphics.setColor(Color.black);
388 graphics.fillRect(x1, y1, avCharWidth * (end - start + 1),
390 graphics.setColor(Color.white);
393 // Need to find the sequence position here.
394 if (av.validCharWidth)
396 for (int i = start; i <= end; i++)
398 if (i < seq.getLength())
400 s = seq.getCharAt(i);
403 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
404 graphics.drawString(String.valueOf(s),
405 charOffset + x1 + avCharWidth * (i - start),
406 y1 + avCharHeight - pady);
411 public void drawCursor(SequenceI seq, int res, int x1, int y1)
413 int pady = av.getCharHeight() / 5;
415 graphics.setColor(Color.black);
416 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
417 graphics.setColor(Color.white);
419 graphics.setColor(Color.white);
421 char s = seq.getCharAt(res);
422 if (av.validCharWidth)
425 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
426 graphics.drawString(String.valueOf(s), charOffset + x1,
427 (y1 + av.getCharHeight()) - pady);