2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 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.api.FeatureRenderer;
24 import jalview.datamodel.AlignmentAnnotation;
25 import jalview.datamodel.SequenceGroup;
26 import jalview.datamodel.SequenceI;
27 import jalview.schemes.ColourSchemeI;
29 import java.awt.Color;
31 import java.awt.FontMetrics;
32 import java.awt.Graphics;
34 public class SequenceRenderer implements jalview.api.SequenceRenderer
40 boolean renderGaps = true;
42 SequenceGroup currentSequenceGroup = null;
44 SequenceGroup[] allGroups = null;
50 boolean forOverview = false;
52 public SequenceRenderer(AlignViewport av)
63 public void prepare(Graphics g, boolean renderGaps)
66 fm = g.getFontMetrics();
68 this.renderGaps = renderGaps;
71 public Color getResidueBoxColour(SequenceI seq, int i)
73 allGroups = av.getAlignment().findAllGroups(seq);
75 if (inCurrentSequenceGroup(i))
77 if (currentSequenceGroup.getDisplayBoxes())
79 getBoxColour(currentSequenceGroup.cs, seq, i);
82 else if (av.getShowBoxes())
84 getBoxColour(av.getGlobalColourScheme(), seq, i);
91 * Get the residue colour at the given sequence position - as determined by
92 * the sequence group colour (if any), else the colour scheme, possibly
93 * overridden by a feature colour.
101 public Color getResidueColour(final SequenceI seq, int position,
104 // TODO replace 8 or so code duplications with calls to this method
105 // (refactored as needed)
106 Color col = getResidueBoxColour(seq, position);
110 col = fr.findFeatureColour(col, seq, position);
115 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
119 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
122 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
124 resBoxColour = Color.lightGray;
128 resBoxColour = Color.white;
133 public Color findSequenceColour(SequenceI seq, int i)
135 allGroups = av.getAlignment().findAllGroups(seq);
136 drawBoxes(seq, i, i, 0);
140 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
150 drawBoxes(seq, start, end, y1);
152 if (av.validCharWidth)
154 drawText(seq, start, end, y1);
158 public void drawBoxes(SequenceI seq, int start, int end, int y1)
161 int length = seq.getLength();
164 int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
167 Color tempColour = null;
170 resBoxColour = Color.white;
173 if (inCurrentSequenceGroup(i))
175 if (currentSequenceGroup.getDisplayBoxes())
177 getBoxColour(currentSequenceGroup.cs, seq, i);
180 else if (av.getShowBoxes())
182 getBoxColour(av.getGlobalColourScheme(), seq, i);
186 if (resBoxColour != tempColour)
188 if (tempColour != null)
190 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
193 graphics.setColor(resBoxColour);
196 curWidth = avCharWidth;
197 tempColour = resBoxColour;
202 curWidth += avCharWidth;
208 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
212 public void drawText(SequenceI seq, int start, int end, int y1)
214 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
215 Font boldFont = null;
216 boolean bold = false;
217 if (av.isUpperCasebold())
219 boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
221 graphics.setFont(av.getFont());
224 y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
228 // Need to find the sequence position here.
229 if (end + 1 >= seq.getLength())
231 end = seq.getLength() - 1;
235 boolean srep = av.isDisplayReferenceSeq();
236 for (int i = start; i <= end; i++)
238 graphics.setColor(Color.black);
240 s = seq.getCharAt(i);
241 if (!renderGaps && jalview.util.Comparison.isGap(s))
246 if (inCurrentSequenceGroup(i))
248 if (!currentSequenceGroup.getDisplayText())
253 if (currentSequenceGroup.getColourText())
255 getBoxColour(currentSequenceGroup.cs, seq, i);
256 graphics.setColor(resBoxColour.darker());
258 if (currentSequenceGroup.getShowNonconserved())
260 s = getDisplayChar(srep, i, s,
266 if (!av.getShowText())
271 if (av.getColourText())
273 getBoxColour(av.getGlobalColourScheme(), seq, i);
274 if (av.getShowBoxes())
276 graphics.setColor(resBoxColour.darker());
280 graphics.setColor(resBoxColour);
283 if (av.getShowUnconserved())
285 s = getDisplayChar(srep, i, s,
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), charOffset + avCharWidth
318 private char getDisplayChar(final boolean usesrep, int position,
321 // TODO - use currentSequenceGroup rather than alignemnt
322 // currentSequenceGroup.getConsensus()
323 char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
325 if (conschar != '-' && s == conschar)
332 boolean inCurrentSequenceGroup(int res)
334 if (allGroups == null)
339 for (int i = 0; i < allGroups.length; i++)
341 if (allGroups[i].getStartRes() <= res
342 && allGroups[i].getEndRes() >= res)
344 currentSequenceGroup = allGroups[i];
352 public void drawHighlightedText(SequenceI seq, int start, int end,
355 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
356 int pady = avCharHeight / 5;
358 graphics.setColor(Color.black);
359 graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
360 graphics.setColor(Color.white);
363 // Need to find the sequence position here.
364 if (av.validCharWidth)
366 for (int i = start; i <= end; i++)
368 if (i < seq.getLength())
370 s = seq.getCharAt(i);
373 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
374 graphics.drawString(String.valueOf(s), charOffset + x1
375 + avCharWidth * (i - start), y1 + avCharHeight - pady);
380 public void drawCursor(SequenceI seq, int res, int x1, int y1)
382 int pady = av.getCharHeight() / 5;
384 graphics.setColor(Color.black);
385 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
386 graphics.setColor(Color.white);
388 graphics.setColor(Color.white);
390 char s = seq.getCharAt(res);
391 if (av.validCharWidth)
394 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
395 graphics.drawString(String.valueOf(s), charOffset + x1,
396 (y1 + av.getCharHeight()) - pady);