2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
\r
3 * Copyright (C) $$Year-Rel$$ The Jalview Authors
\r
5 * This file is part of Jalview.
\r
7 * Jalview is free software: you can redistribute it and/or
\r
8 * modify it under the terms of the GNU General Public License
\r
9 * as published by the Free Software Foundation, either version 3
\r
10 * of the License, or (at your option) any later version.
\r
12 * Jalview is distributed in the hope that it will be useful, but
\r
13 * WITHOUT ANY WARRANTY; without even the implied warranty
\r
14 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
\r
15 * PURPOSE. See the GNU General Public License for more details.
\r
17 * You should have received a copy of the GNU General Public License
\r
18 * along with Jalview. If not, see <http://www.gnu.org/licenses/>.
\r
19 * The Jalview Authors are detailed in the 'AUTHORS' file.
\r
21 package jalview.appletgui;
\r
23 import jalview.api.FeatureRenderer;
\r
24 import jalview.datamodel.SequenceGroup;
\r
25 import jalview.datamodel.SequenceI;
\r
26 import jalview.schemes.ColourSchemeI;
\r
27 import jalview.util.Comparison;
\r
29 import java.awt.Color;
\r
30 import java.awt.Font;
\r
31 import java.awt.FontMetrics;
\r
32 import java.awt.Graphics;
\r
34 public class SequenceRenderer implements jalview.api.SequenceRenderer
\r
40 boolean renderGaps = true;
\r
42 SequenceGroup currentSequenceGroup = null;
\r
44 SequenceGroup[] allGroups = null;
\r
50 boolean forOverview = false;
\r
52 public SequenceRenderer(AlignViewport av)
\r
63 public void prepare(Graphics g, boolean renderGaps)
\r
66 fm = g.getFontMetrics();
\r
68 this.renderGaps = renderGaps;
\r
71 public Color getResidueBoxColour(SequenceI seq, int i)
\r
73 allGroups = av.getAlignment().findAllGroups(seq);
\r
75 if (inCurrentSequenceGroup(i))
\r
77 if (currentSequenceGroup.getDisplayBoxes())
\r
79 getBoxColour(currentSequenceGroup.cs, seq, i);
\r
82 else if (av.getShowBoxes())
\r
84 getBoxColour(av.getGlobalColourScheme(), seq, i);
\r
87 return resBoxColour;
\r
91 * Get the residue colour at the given sequence position - as determined by
\r
92 * the sequence group colour (if any), else the colour scheme, possibly
\r
93 * overridden by a feature colour.
\r
101 public Color getResidueColour(final SequenceI seq, int position,
\r
102 FeatureRenderer fr)
\r
104 // TODO replace 8 or so code duplications with calls to this method
\r
105 // (refactored as needed)
\r
106 Color col = getResidueBoxColour(seq, position);
\r
110 col = fr.findFeatureColour(col, seq, position);
\r
115 void getBoxColour(ColourSchemeI cs, SequenceI seq, int i)
\r
119 resBoxColour = cs.findColour(seq.getCharAt(i), i, seq);
\r
121 else if (forOverview && !Comparison.isGap(seq.getCharAt(i)))
\r
123 resBoxColour = Color.lightGray;
\r
127 resBoxColour = Color.white;
\r
132 public Color findSequenceColour(SequenceI seq, int i)
\r
134 allGroups = av.getAlignment().findAllGroups(seq);
\r
135 drawBoxes(seq, i, i, 0);
\r
136 return resBoxColour;
\r
139 public void drawSequence(SequenceI seq, SequenceGroup[] sg, int start,
\r
149 drawBoxes(seq, start, end, y1);
\r
151 if (av.validCharWidth)
\r
153 drawText(seq, start, end, y1);
\r
157 public void drawBoxes(SequenceI seq, int start, int end, int y1)
\r
160 int length = seq.getLength();
\r
163 int curWidth = av.getCharWidth(), avCharWidth = av.getCharWidth(), avCharHeight = av
\r
166 Color tempColour = null;
\r
169 resBoxColour = Color.white;
\r
172 if (inCurrentSequenceGroup(i))
\r
174 if (currentSequenceGroup.getDisplayBoxes())
\r
176 getBoxColour(currentSequenceGroup.cs, seq, i);
\r
179 else if (av.getShowBoxes())
\r
181 getBoxColour(av.getGlobalColourScheme(), seq, i);
\r
185 if (resBoxColour != tempColour)
\r
187 if (tempColour != null)
\r
189 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
\r
192 graphics.setColor(resBoxColour);
\r
195 curWidth = avCharWidth;
\r
196 tempColour = resBoxColour;
\r
201 curWidth += avCharWidth;
\r
207 graphics.fillRect(avCharWidth * (curStart - start), y1, curWidth,
\r
211 public void drawText(SequenceI seq, int start, int end, int y1)
\r
213 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
\r
214 Font boldFont = null;
\r
215 boolean bold = false;
\r
216 if (av.isUpperCasebold())
\r
218 boldFont = new Font(av.getFont().getName(), Font.BOLD, avCharHeight);
\r
220 graphics.setFont(av.getFont());
\r
223 y1 += avCharHeight - avCharHeight / 5; // height/5 replaces pady
\r
225 int charOffset = 0;
\r
227 // Need to find the sequence position here.
\r
228 if (end + 1 >= seq.getLength())
\r
230 end = seq.getLength() - 1;
\r
234 boolean srep = av.isDisplayReferenceSeq();
\r
235 for (int i = start; i <= end; i++)
\r
237 graphics.setColor(Color.black);
\r
239 s = seq.getCharAt(i);
\r
240 if (!renderGaps && Comparison.isGap(s))
\r
245 if (inCurrentSequenceGroup(i))
\r
247 if (!currentSequenceGroup.getDisplayText())
\r
252 if (currentSequenceGroup.getColourText())
\r
254 getBoxColour(currentSequenceGroup.cs, seq, i);
\r
255 graphics.setColor(resBoxColour.darker());
\r
257 if (currentSequenceGroup.getShowNonconserved())
\r
259 s = getDisplayChar(srep, i, s,
\r
265 if (!av.getShowText())
\r
270 if (av.getColourText())
\r
272 getBoxColour(av.getGlobalColourScheme(), seq, i);
\r
273 if (av.getShowBoxes())
\r
275 graphics.setColor(resBoxColour.darker());
\r
279 graphics.setColor(resBoxColour);
\r
282 if (av.getShowUnconserved())
\r
284 s = getDisplayChar(srep, i, s,
\r
290 if (av.isUpperCasebold())
\r
292 fm = graphics.getFontMetrics();
\r
293 if ('A' <= s && s <= 'Z')
\r
298 graphics.setFont(boldFont);
\r
304 graphics.setFont(av.font);
\r
310 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
\r
311 graphics.drawString(String.valueOf(s), charOffset + avCharWidth
\r
312 * (i - start), y1);
\r
317 private char getDisplayChar(final boolean usesrep, int position,
\r
320 // TODO - use currentSequenceGroup rather than alignemnt
\r
321 // currentSequenceGroup.getConsensus()
\r
322 char conschar = (usesrep) ? av.getAlignment().getSeqrep().getCharAt(position) : av.getAlignmentConsensusAnnotation().annotations[position].displayCharacter
\r
324 if (conschar != '-' && s == conschar)
\r
331 boolean inCurrentSequenceGroup(int res)
\r
333 if (allGroups == null)
\r
338 for (int i = 0; i < allGroups.length; i++)
\r
340 if (allGroups[i].getStartRes() <= res
\r
341 && allGroups[i].getEndRes() >= res)
\r
343 currentSequenceGroup = allGroups[i];
\r
351 public void drawHighlightedText(SequenceI seq, int start, int end,
\r
354 int avCharWidth = av.getCharWidth(), avCharHeight = av.getCharHeight();
\r
355 int pady = avCharHeight / 5;
\r
356 int charOffset = 0;
\r
357 graphics.setColor(Color.black);
\r
358 graphics.fillRect(x1, y1, avCharWidth * (end - start + 1), avCharHeight);
\r
359 graphics.setColor(Color.white);
\r
362 // Need to find the sequence position here.
\r
363 if (av.validCharWidth)
\r
365 for (int i = start; i <= end; i++)
\r
367 if (i < seq.getLength())
\r
369 s = seq.getCharAt(i);
\r
372 charOffset = (avCharWidth - fm.charWidth(s)) / 2;
\r
373 graphics.drawString(String.valueOf(s), charOffset + x1
\r
374 + avCharWidth * (i - start), y1 + avCharHeight - pady);
\r
379 public void drawCursor(SequenceI seq, int res, int x1, int y1)
\r
381 int pady = av.getCharHeight() / 5;
\r
382 int charOffset = 0;
\r
383 graphics.setColor(Color.black);
\r
384 graphics.fillRect(x1, y1, av.getCharWidth(), av.getCharHeight());
\r
385 graphics.setColor(Color.white);
\r
387 graphics.setColor(Color.white);
\r
389 char s = seq.getCharAt(res);
\r
390 if (av.validCharWidth)
\r
393 charOffset = (av.getCharWidth() - fm.charWidth(s)) / 2;
\r
394 graphics.drawString(String.valueOf(s), charOffset + x1,
\r
395 (y1 + av.getCharHeight()) - pady);
\r