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.getResidueShading(),
88 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(), avCharHeight = av
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,
143 else if (av.getShowBoxes())
145 resBoxColour = resColourFinder
146 .getBoxColour(av.getResidueShading(), seq, i);
150 if (resBoxColour != tempColour)
152 if (tempColour != null)
154 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
157 graphics.setColor(resBoxColour);
160 curWidth = avCharWidth;
161 tempColour = resBoxColour;
166 curWidth += avCharWidth;
172 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
176 public void drawText(SequenceI seq, int start, int end, int y1)
178 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
179 Font boldFont = null;
180 boolean bold = false;
181 if (av.isUpperCasebold())
183 boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
185 graphics.setFont(av.getFont());
188 y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
192 // Need to find the sequence position here.
193 if (end + 1 >= seq.getLength())
195 end = seq.getLength() - 1;
199 boolean srep = av.isDisplayReferenceSeq();
200 for (int i = start; i <= end; i++)
202 graphics.setColor(Color.black);
204 s = seq.getCharAt(i);
205 if (!renderGaps && jalview.util.Comparison.isGap(s))
210 SequenceGroup currentSequenceGroup = resColourFinder
211 .getCurrentSequenceGroup(allGroups, i);
212 if (currentSequenceGroup != null)
214 if (!currentSequenceGroup.getDisplayText())
219 if (currentSequenceGroup.getColourText())
221 resBoxColour = resColourFinder.getBoxColour(
222 currentSequenceGroup.getGroupColourScheme(), seq, i);
223 graphics.setColor(resBoxColour.darker());
225 if (currentSequenceGroup.getShowNonconserved())
227 s = getDisplayChar(srep, i, s, '.', currentSequenceGroup);
232 if (!av.getShowText())
237 if (av.getColourText())
239 resBoxColour = resColourFinder
240 .getBoxColour(av.getResidueShading(), seq, i);
241 if (av.getShowBoxes())
243 graphics.setColor(resBoxColour.darker());
247 graphics.setColor(resBoxColour);
250 if (av.getShowUnconserved())
252 s = getDisplayChar(srep, i, s, '.', null);
257 if (av.isUpperCasebold())
259 fm = graphics.getFontMetrics();
260 if ('A' <= s && s <= 'Z')
265 graphics.setFont(boldFont);
271 graphics.setFont(av.font);
277 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
278 graphics.drawString(String.valueOf(s), charOffset + avCharWidth
285 * Returns 'conservedChar' to represent the given position if the sequence
286 * character at that position is equal to the consensus (ignoring case), else
287 * returns the sequence character
291 * @param sequenceChar
292 * @param conservedChar
295 private char getDisplayChar(final boolean usesrep, int position,
296 char sequenceChar, char conservedChar, SequenceGroup currentGroup)
298 // TODO - use currentSequenceGroup rather than alignment
299 // currentSequenceGroup.getConsensus()
300 char conschar = (usesrep) ? (currentGroup == null
301 || position < currentGroup.getStartRes()
302 || position > currentGroup.getEndRes() ? av.getAlignment()
303 .getSeqrep().getCharAt(position)
304 : (currentGroup.getSeqrep() != null ? currentGroup.getSeqrep()
305 .getCharAt(position) : av.getAlignment().getSeqrep()
306 .getCharAt(position)))
307 : (currentGroup != null && currentGroup.getConsensus() != null
308 && position >= currentGroup.getStartRes()
309 && position <= currentGroup.getEndRes() && currentGroup
310 .getConsensus().annotations.length > position) ? currentGroup
311 .getConsensus().annotations[position].displayCharacter
313 : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
315 if (!jalview.util.Comparison.isGap(conschar)
316 && (sequenceChar == conschar || sequenceChar + CHAR_TO_UPPER == conschar))
318 sequenceChar = conservedChar;
323 public void drawHighlightedText(SequenceI seq, int start, int end,
326 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
327 int pady = avCharHeight / 5;
329 graphics.setColor(Color.black);
330 graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
331 graphics.setColor(Color.white);
334 // Need to find the sequence position here.
335 if (av.validCharWidth)
337 for (int i = start; i <= end; i++)
339 if (i < seq.getLength())
341 s = seq.getCharAt(i);
344 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
345 graphics.drawString(String.valueOf(s), charOffset + x1
346 + avCharWidth * (i - start), y1 + avCharHeight - pady);
351 public void drawCursor(SequenceI seq, int res, int x1, int y1)
353 int pady = av.getCharHeight() / 5;
355 graphics.setColor(Color.black);
356 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
357 graphics.setColor(Color.white);
359 graphics.setColor(Color.white);
361 char s = seq.getCharAt(res);
362 if (av.validCharWidth)
365 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
366 graphics.drawString(String.valueOf(s), charOffset + x1,
367 (y1 + av.getCharHeight()) - pady);