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.api.FeatureRenderer;
24 import jalview.datamodel.SequenceGroup;
25 import jalview.datamodel.SequenceI;
26 import jalview.schemes.ColourSchemeI;
28 import java.awt.Color;
30 import java.awt.FontMetrics;
31 import java.awt.Graphics;
33 public class SequenceRenderer implements jalview.api.SequenceRenderer
39 boolean renderGaps = true;
41 SequenceGroup currentSequenceGroup = null;
43 SequenceGroup[] allGroups = null;
49 boolean forOverview = false;
51 public SequenceRenderer(AlignViewport av)
62 public void prepare(Graphics g, boolean renderGaps)
65 fm = g.getFontMetrics();
67 this.renderGaps = renderGaps;
70 public Color getResidueBoxColour(SequenceI seq, int i)
72 allGroups = av.getAlignment().findAllGroups(seq);
74 if (inCurrentSequenceGroup(i))
76 if (currentSequenceGroup.getDisplayBoxes())
78 getBoxColour(currentSequenceGroup.cs, seq, i);
81 else if (av.getShowBoxes())
83 getBoxColour(av.getGlobalColourScheme(), seq, i);
90 * Get the residue colour at the given sequence position - as determined by
91 * the sequence group colour (if any), else the colour scheme, possibly
92 * overridden by a feature colour.
100 public Color getResidueColour(final SequenceI seq, int position,
103 // TODO replace 8 or so code duplications with calls to this method
104 // (refactored as needed)
105 Color col = getResidueBoxColour(seq, position);
109 col = fr.findFeatureColour(col, seq, position);
114 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
118 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
121 && !jalview.util.Comparison.isGap(seq.getCharAt(i)))
123 resBoxColour = Color.lightGray;
127 resBoxColour = Color.white;
132 public Color findSequenceColour(SequenceI seq, int i)
134 allGroups = av.getAlignment().findAllGroups(seq);
135 drawBoxes(seq, i, i, 0);
139 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
149 drawBoxes(seq, start, end, y1);
151 if (av.validCharWidth)
153 drawText(seq, start, end, y1);
157 public void drawBoxes(SequenceI seq, int start, int end, int y1)
160 int length = seq.getLength();
163 int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
166 Color tempColour = null;
169 resBoxColour = Color.white;
172 if (inCurrentSequenceGroup(i))
174 if (currentSequenceGroup.getDisplayBoxes())
176 getBoxColour(currentSequenceGroup.cs, seq, i);
179 else if (av.getShowBoxes())
181 getBoxColour(av.getGlobalColourScheme(), seq, i);
185 if (resBoxColour != tempColour)
187 if (tempColour != null)
189 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
192 graphics.setColor(resBoxColour);
195 curWidth = avCharWidth;
196 tempColour = resBoxColour;
201 curWidth += avCharWidth;
207 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
211 public void drawText(SequenceI seq, int start, int end, int y1)
213 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
214 Font boldFont = null;
215 boolean bold = false;
216 if (av.isUpperCasebold())
218 boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
220 graphics.setFont(av.getFont());
223 y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
227 // Need to find the sequence position here.
228 if (end + 1 >= seq.getLength())
230 end = seq.getLength() - 1;
234 boolean srep = av.isDisplayReferenceSeq();
235 for (int i = start; i <= end; i++)
237 graphics.setColor(Color.black);
239 s = seq.getCharAt(i);
240 if (!renderGaps && jalview.util.Comparison.isGap(s))
245 if (inCurrentSequenceGroup(i))
247 if (!currentSequenceGroup.getDisplayText())
252 if (currentSequenceGroup.getColourText())
254 getBoxColour(currentSequenceGroup.cs, seq, i);
255 graphics.setColor(resBoxColour.darker());
257 if (currentSequenceGroup.getShowNonconserved())
259 s = getDisplayChar(srep, i, s,
265 if (!av.getShowText())
270 if (av.getColourText())
272 getBoxColour(av.getGlobalColourScheme(), seq, i);
273 if (av.getShowBoxes())
275 graphics.setColor(resBoxColour.darker());
279 graphics.setColor(resBoxColour);
282 if (av.getShowUnconserved())
284 s = getDisplayChar(srep, i, s,
290 if (av.isUpperCasebold())
292 fm = graphics.getFontMetrics();
293 if ('A' <= s && s <= 'Z')
298 graphics.setFont(boldFont);
304 graphics.setFont(av.font);
310 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
311 graphics.drawString(String.valueOf(s), charOffset + avCharWidth
317 private char getDisplayChar(final boolean usesrep, int position,
320 // TODO - use currentSequenceGroup rather than alignemnt
321 // currentSequenceGroup.getConsensus()
322 char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
324 if (conschar != '-' && s == conschar)
331 boolean inCurrentSequenceGroup(int res)
333 if (allGroups == null)
338 for (int i = 0; i < allGroups.length; i++)
340 if (allGroups[i].getStartRes() <= res
341 && allGroups[i].getEndRes() >= res)
343 currentSequenceGroup = allGroups[i];
351 public void drawHighlightedText(SequenceI seq, int start, int end,
354 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
355 int pady = avCharHeight / 5;
357 graphics.setColor(Color.black);
358 graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
359 graphics.setColor(Color.white);
362 // Need to find the sequence position here.
363 if (av.validCharWidth)
365 for (int i = start; i <= end; i++)
367 if (i < seq.getLength())
369 s = seq.getCharAt(i);
372 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
373 graphics.drawString(String.valueOf(s), charOffset + x1
374 + avCharWidth * (i - start), y1 + avCharHeight - pady);
379 public void drawCursor(SequenceI seq, int res, int x1, int y1)
381 int pady = av.getCharHeight() / 5;
383 graphics.setColor(Color.black);
384 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
385 graphics.setColor(Color.white);
387 graphics.setColor(Color.white);
389 char s = seq.getCharAt(res);
390 if (av.validCharWidth)
393 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
394 graphics.drawString(String.valueOf(s), charOffset + x1,
395 (y1 + av.getCharHeight()) - pady);