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(), allGroups, seq, position, finder);
91 public Color findSequenceColour(SequenceI seq, int i)
93 allGroups = av.getAlignment().findAllGroups(seq);
94 drawBoxes(seq, i, i, 0);
98 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
108 drawBoxes(seq, start, end, y1);
110 if (av.validCharWidth)
112 drawText(seq, start, end, y1);
116 public void drawBoxes(SequenceI seq, int start, int end, int y1)
119 int length = seq.getLength();
122 int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(),
123 avCharHeight = av.getCharHeight();
125 Color resBoxColour = Color.white;
126 Color tempColour = null;
129 resBoxColour = Color.white;
132 SequenceGroup currentSequenceGroup = resColourFinder
133 .getCurrentSequenceGroup(allGroups, i);
134 if (currentSequenceGroup != null)
136 if (currentSequenceGroup.getDisplayBoxes())
138 resBoxColour = resColourFinder.getBoxColour(
139 currentSequenceGroup.getGroupColourScheme(), seq, i);
142 else if (av.getShowBoxes())
144 resBoxColour = resColourFinder
145 .getBoxColour(av.getResidueShading(), seq, i);
149 if (resBoxColour != tempColour)
151 if (tempColour != null)
153 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
156 graphics.setColor(resBoxColour);
159 curWidth = avCharWidth;
160 tempColour = resBoxColour;
165 curWidth += avCharWidth;
171 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
175 public void drawText(SequenceI seq, int start, int end, int y1)
177 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
178 Font boldFont = null;
179 boolean bold = false;
180 if (av.isUpperCasebold())
182 boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
184 graphics.setFont(av.getFont());
187 y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
191 // Need to find the sequence position here.
192 if (end + 1 >= seq.getLength())
194 end = seq.getLength() - 1;
198 boolean srep = av.isDisplayReferenceSeq();
199 for (int i = start; i <= end; i++)
201 graphics.setColor(Color.black);
203 s = seq.getCharAt(i);
204 if (!renderGaps && jalview.util.Comparison.isGap(s))
209 SequenceGroup currentSequenceGroup = resColourFinder
210 .getCurrentSequenceGroup(allGroups, i);
211 if (currentSequenceGroup != null)
213 if (!currentSequenceGroup.getDisplayText())
218 if (currentSequenceGroup.getColourText())
220 resBoxColour = resColourFinder.getBoxColour(
221 currentSequenceGroup.getGroupColourScheme(), seq, i);
222 graphics.setColor(resBoxColour.darker());
224 if (currentSequenceGroup.getShowNonconserved())
226 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
231 if (!av.getShowText())
236 if (av.getColourText())
238 resBoxColour = resColourFinder
239 .getBoxColour(av.getResidueShading(), seq, i);
240 if (av.getShowBoxes())
242 graphics.setColor(resBoxColour.darker());
246 graphics.setColor(resBoxColour);
249 if (av.getShowUnconserved())
251 s = getDisplayChar(srep, i, s, '.', null);
256 if (av.isUpperCasebold())
258 fm = graphics.getFontMetrics();
259 if ('A' <= s && s <= 'Z')
264 graphics.setFont(boldFont);
270 graphics.setFont(av.font);
276 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
277 graphics.drawString(String.valueOf(s),
278 charOffset + avCharWidth * (i - start), y1);
284 * Returns 'conservedChar' to represent the given position if the sequence
285 * character at that position is equal to the consensus (ignoring case), else
286 * returns the sequence character
290 * @param sequenceChar
291 * @param conservedChar
294 private char getDisplayChar(final boolean usesrep, int position,
295 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
297 // TODO - use currentSequenceGroup rather than alignment
298 // currentSequenceGroup.getConsensus()
299 char conschar = (usesrep) ? (currentGroup == null
300 || position < currentGroup.getStartRes()
301 || position > currentGroup.getEndRes()
302 ? av.getAlignment().getSeqrep().getCharAt(position)
303 : (currentGroup.getSeqrep() != null
304 ? currentGroup.getSeqrep().getCharAt(position)
305 : av.getAlignment().getSeqrep()
306 .getCharAt(position)))
307 : (currentGroup != null && currentGroup.getConsensus() != null
308 && position >= currentGroup.getStartRes()
309 && position <= currentGroup.getEndRes()
311 .getConsensus().annotations.length > position)
313 .getConsensus().annotations[position].displayCharacter
315 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
317 if (!jalview.util.Comparison.isGap(conschar)
318 && (sequenceChar == conschar
319 || sequenceChar + CHAR_TO_UPPER == conschar))
321 sequenceChar = conservedChar;
326 public void drawHighlightedText(SequenceI seq, int start, int end, int x1,
329 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
330 int pady = avCharHeight / 5;
332 graphics.setColor(Color.black);
333 graphics.fillRect(x1, y1, avCharWidth * (end - start + 1),
335 graphics.setColor(Color.white);
338 // Need to find the sequence position here.
339 if (av.validCharWidth)
341 for (int i = start; i <= end; i++)
343 if (i < seq.getLength())
345 s = seq.getCharAt(i);
348 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
349 graphics.drawString(String.valueOf(s),
350 charOffset + x1 + avCharWidth * (i - start),
351 y1 + avCharHeight - pady);
356 public void drawCursor(SequenceI seq, int res, int x1, int y1)
358 int pady = av.getCharHeight() / 5;
360 graphics.setColor(Color.black);
361 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
362 graphics.setColor(Color.white);
364 graphics.setColor(Color.white);
366 char s = seq.getCharAt(res);
367 if (av.validCharWidth)
370 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
371 graphics.drawString(String.valueOf(s), charOffset + x1,
372 (y1 + av.getCharHeight()) - pady);