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.ResidueColourFinder;
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[] allGroups = null;
49 ResidueColourFinder resColourFinder;
51 public SequenceRenderer(AlignViewport av)
54 resColourFinder = new ResidueColourFinder();
63 public void prepare(Graphics g, boolean renderGaps)
66 fm = g.getFontMetrics();
68 this.renderGaps = renderGaps;
72 * Get the residue colour at the given sequence position - as determined by
73 * the sequence group colour (if any), else the colour scheme, possibly
74 * overridden by a feature colour.
82 public Color getResidueColour(final SequenceI seq, int position,
83 FeatureColourFinder finder)
85 // TODO replace 8 or so code duplications with calls to this method
86 // (refactored as needed)
87 return resColourFinder.getResidueColour(av.getShowBoxes(),
88 av.getResidueShading(),
89 allGroups, seq, position, finder);
92 public Color findSequenceColour(SequenceI seq, int i)
94 allGroups = av.getAlignment().findAllGroups(seq);
95 drawBoxes(seq, i, i, 0);
99 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
109 drawBoxes(seq, start, end, y1);
111 if (av.validCharWidth)
113 drawText(seq, start, end, y1);
117 public void drawBoxes(SequenceI seq, int start, int end, int y1)
120 int length = seq.getLength();
123 int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(),
124 avCharHeight = av.getCharHeight();
126 Color resBoxColour = Color.white;
127 Color tempColour = null;
130 resBoxColour = Color.white;
133 SequenceGroup currentSequenceGroup = resColourFinder
134 .getCurrentSequenceGroup(allGroups, i);
135 if (currentSequenceGroup != null)
137 if (currentSequenceGroup.getDisplayBoxes())
139 resBoxColour = resColourFinder.getBoxColour(
140 currentSequenceGroup.getGroupColourScheme(), seq,
144 else if (av.getShowBoxes())
146 resBoxColour = resColourFinder
147 .getBoxColour(av.getResidueShading(), seq, i);
151 if (resBoxColour != tempColour)
153 if (tempColour != null)
155 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
158 graphics.setColor(resBoxColour);
161 curWidth = avCharWidth;
162 tempColour = resBoxColour;
167 curWidth += avCharWidth;
173 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
177 public void drawText(SequenceI seq, int start, int end, int y1)
179 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
180 Font boldFont = null;
181 boolean bold = false;
182 if (av.isUpperCasebold())
184 boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
186 graphics.setFont(av.getFont());
189 y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
193 // Need to find the sequence position here.
194 if (end + 1 >= seq.getLength())
196 end = seq.getLength() - 1;
200 boolean srep = av.isDisplayReferenceSeq();
201 for (int i = start; i <= end; i++)
203 graphics.setColor(Color.black);
205 s = seq.getCharAt(i);
206 if (!renderGaps && jalview.util.Comparison.isGap(s))
211 SequenceGroup currentSequenceGroup = resColourFinder
212 .getCurrentSequenceGroup(allGroups, i);
213 if (currentSequenceGroup != null)
215 if (!currentSequenceGroup.getDisplayText())
220 if (currentSequenceGroup.getColourText())
222 resBoxColour = resColourFinder.getBoxColour(
223 currentSequenceGroup.getGroupColourScheme(), seq, i);
224 graphics.setColor(resBoxColour.darker());
226 if (currentSequenceGroup.getShowNonconserved())
228 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
233 if (!av.getShowText())
238 if (av.getColourText())
240 resBoxColour = resColourFinder
241 .getBoxColour(av.getResidueShading(), seq, i);
242 if (av.getShowBoxes())
244 graphics.setColor(resBoxColour.darker());
248 graphics.setColor(resBoxColour);
251 if (av.getShowUnconserved())
253 s = getDisplayChar(srep, i, s, '.', null);
258 if (av.isUpperCasebold())
260 fm = graphics.getFontMetrics();
261 if ('A' <= s && s <= 'Z')
266 graphics.setFont(boldFont);
272 graphics.setFont(av.font);
278 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
279 graphics.drawString(String.valueOf(s),
280 charOffset + avCharWidth * (i - start), y1);
286 * Returns 'conservedChar' to represent the given position if the sequence
287 * character at that position is equal to the consensus (ignoring case), else
288 * returns the sequence character
292 * @param sequenceChar
293 * @param conservedChar
296 private char getDisplayChar(final boolean usesrep, int position,
297 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
299 // TODO - use currentSequenceGroup rather than alignment
300 // currentSequenceGroup.getConsensus()
301 char conschar = (usesrep) ? (currentGroup == null
302 || position < currentGroup.getStartRes()
303 || position > currentGroup.getEndRes()
304 ? av.getAlignment().getSeqrep().getCharAt(position)
305 : (currentGroup.getSeqrep() != null
306 ? currentGroup.getSeqrep().getCharAt(position)
307 : av.getAlignment().getSeqrep()
308 .getCharAt(position)))
309 : (currentGroup != null && currentGroup.getConsensus() != null
310 && position >= currentGroup.getStartRes()
311 && position <= currentGroup.getEndRes()
313 .getConsensus().annotations.length > position)
315 .getConsensus().annotations[position].displayCharacter
317 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
319 if (!jalview.util.Comparison.isGap(conschar)
320 && (sequenceChar == conschar
321 || sequenceChar + CHAR_TO_UPPER == conschar))
323 sequenceChar = conservedChar;
328 public void drawHighlightedText(SequenceI seq, int start, int end,
331 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
332 int pady = avCharHeight / 5;
334 graphics.setColor(Color.black);
335 graphics.fillRect(x1, y1, avCharWidth * (end - start + 1),
337 graphics.setColor(Color.white);
340 // Need to find the sequence position here.
341 if (av.validCharWidth)
343 for (int i = start; i <= end; i++)
345 if (i < seq.getLength())
347 s = seq.getCharAt(i);
350 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
351 graphics.drawString(String.valueOf(s),
352 charOffset + x1 + avCharWidth * (i - start),
353 y1 + avCharHeight - pady);
358 public void drawCursor(SequenceI seq, int res, int x1, int y1)
360 int pady = av.getCharHeight() / 5;
362 graphics.setColor(Color.black);
363 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
364 graphics.setColor(Color.white);
366 graphics.setColor(Color.white);
368 char s = seq.getCharAt(res);
369 if (av.validCharWidth)
372 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
373 graphics.drawString(String.valueOf(s), charOffset + x1,
374 (y1 + av.getCharHeight()) - pady);