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.renderer;
23 import jalview.analysis.AAFrequency;
24 import jalview.analysis.CodingUtils;
25 import jalview.analysis.Rna;
26 import jalview.analysis.StructureFrequency;
27 import jalview.api.AlignViewportI;
28 import jalview.datamodel.AlignmentAnnotation;
29 import jalview.datamodel.Annotation;
30 import jalview.datamodel.ColumnSelection;
31 import jalview.datamodel.HiddenColumns;
32 import jalview.datamodel.HiddenMarkovModel;
33 import jalview.datamodel.ProfilesI;
34 import jalview.schemes.ColourSchemeI;
35 import jalview.schemes.NucleotideColourScheme;
36 import jalview.schemes.ResidueProperties;
37 import jalview.schemes.ZappoColourScheme;
38 import jalview.util.Platform;
39 import jalview.util.QuickSort;
41 import java.awt.BasicStroke;
42 import java.awt.Color;
44 import java.awt.FontMetrics;
45 import java.awt.Graphics;
46 import java.awt.Graphics2D;
47 import java.awt.Image;
48 import java.awt.font.LineMetrics;
49 import java.awt.geom.AffineTransform;
50 import java.awt.image.ImageObserver;
51 import java.util.BitSet;
52 import java.util.Hashtable;
53 import java.util.List;
55 public class AnnotationRenderer
57 private static final int UPPER_TO_LOWER = 'a' - 'A'; // 32
59 private static final int CHAR_A = 'A'; // 65
61 private static final int CHAR_Z = 'Z'; // 90
63 private static final int AMINO = 0;
65 private static final int DNA = 1;
68 * flag indicating if timing and redraw parameter info should be output
70 private final boolean debugRedraw;
72 private int charWidth, endRes, charHeight;
74 private boolean validCharWidth, hasHiddenColumns;
76 private FontMetrics fm;
78 private final boolean MAC = Platform.isAMac();
80 boolean av_renderHistogram = true, av_renderProfile = true,
81 av_normaliseProfile = false;
83 ResidueShaderI profcolour = null;
85 private ColumnSelection columnSelection;
87 private HiddenColumns hiddenColumns;
89 private ProfilesI hconsensus;
91 private Hashtable[] complementConsensus;
93 private Hashtable[] hStrucConsensus;
95 private boolean av_ignoreGapsConsensus;
98 * attributes set from AwtRenderPanelI
101 * old image used when data is currently being calculated and cannot be
104 private Image fadedImage;
107 * panel being rendered into
109 private ImageObserver annotationPanel;
112 * width of image to render in panel
114 private int imgWidth;
117 * offset to beginning of visible area
122 * offset to end of visible area
124 private int visHeight;
127 * indicate if the renderer should only render the visible portion of the
128 * annotation given the current view settings
130 private boolean useClip = true;
133 * master flag indicating if renderer should ever try to clip. not enabled for
136 private boolean canClip = false;
138 public AnnotationRenderer()
144 * Create a new annotation Renderer
147 * flag indicating if timing and redraw parameter info should be
150 public AnnotationRenderer(boolean debugRedraw)
152 this.debugRedraw = debugRedraw;
156 * Remove any references and resources when this object is no longer required
158 public void dispose()
161 complementConsensus = null;
162 hStrucConsensus = null;
164 annotationPanel = null;
167 void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
168 int x, int y, int iconOffset, int startRes, int column,
169 boolean validRes, boolean validEnd)
171 g.setColor(STEM_COLOUR);
172 int sCol = (lastSSX / charWidth) + startRes;
174 int x2 = (x * charWidth);
176 char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
177 : row_annotations[column - 1].secondaryStructure;
179 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
180 || dc != row_annotations[sCol - 1].secondaryStructure;
181 boolean diffdownstream = !validRes || !validEnd
182 || row_annotations[column] == null
183 || dc != row_annotations[column].secondaryStructure;
185 if (column > 0 && Rna.isClosingParenthesis(dc))
188 // if (validRes && column>1 && row_annotations[column-2]!=null &&
189 // dc.equals(row_annotations[column-2].displayCharacter))
192 * if new annotation with a closing base pair half of the stem,
193 * display a backward arrow
195 g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
196 new int[] { y + iconOffset, y + 14 + iconOffset,
197 y + 8 + iconOffset }, 3);
207 // display a forward arrow
211 * if annotation ending with an opeing base pair half of the stem,
212 * display a forward arrow
214 g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 }, new int[] {
215 y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
224 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
227 void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
228 Annotation[] row_annotations, int lastSSX, int x, int y,
229 int iconOffset, int startRes, int column, boolean validRes,
232 // System.out.println(nonCanColor);
234 g.setColor(nonCanColor);
235 int sCol = (lastSSX / charWidth) + startRes;
237 int x2 = (x * charWidth);
239 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
240 : row_annotations[column - 1].displayCharacter;
242 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
243 || !dc.equals(row_annotations[sCol - 1].displayCharacter);
244 boolean diffdownstream = !validRes || !validEnd
245 || row_annotations[column] == null
246 || !dc.equals(row_annotations[column].displayCharacter);
247 // System.out.println("Column "+column+" diff up: "+diffupstream+" down:"+diffdownstream);
248 // If a closing base pair half of the stem, display a backward arrow
249 if (column > 0 && Rna.isClosingParenthesis(dc))
253 // if (validRes && column>1 && row_annotations[column-2]!=null &&
254 // dc.equals(row_annotations[column-2].displayCharacter))
256 g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
257 new int[] { y + iconOffset, y + 14 + iconOffset,
258 y + 8 + iconOffset }, 3);
269 // display a forward arrow
272 g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 }, new int[] {
273 y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset }, 3);
282 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
285 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
287 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
290 fm = annotPanel.getFontMetrics();
291 annotationPanel = annotPanel;
292 fadedImage = annotPanel.getFadedImage();
293 imgWidth = annotPanel.getFadedImageWidth();
294 // visible area for rendering
295 int[] bounds = annotPanel.getVisibleVRange();
299 visHeight = bounds[1];
314 updateFromAlignViewport(av);
317 public void updateFromAlignViewport(AlignViewportI av)
319 charWidth = av.getCharWidth();
320 endRes = av.getRanges().getEndRes();
321 charHeight = av.getCharHeight();
322 hasHiddenColumns = av.hasHiddenColumns();
323 validCharWidth = av.isValidCharWidth();
324 av_renderHistogram = av.isShowConsensusHistogram();
325 av_renderProfile = av.isShowSequenceLogo();
326 av_normaliseProfile = av.isNormaliseSequenceLogo();
327 profcolour = av.getResidueShading();
328 if (profcolour == null || profcolour.getColourScheme() == null)
331 * Use default colour for sequence logo if
332 * the alignment has no colourscheme set
333 * (would like to use user preference but n/a for applet)
335 ColourSchemeI col = av.getAlignment().isNucleotide() ? new NucleotideColourScheme()
336 : new ZappoColourScheme();
337 profcolour = new ResidueShader(col);
339 columnSelection = av.getColumnSelection();
340 hiddenColumns = av.getAlignment().getHiddenColumns();
341 hconsensus = av.getSequenceConsensusHash();
342 complementConsensus = av.getComplementConsensusHash();
343 hStrucConsensus = av.getRnaStructureConsensusHash();
344 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
347 public int[] getHMMProfileFor(AlignmentAnnotation aa, int column,
348 boolean removeBelowBackground)
351 HiddenMarkovModel hmm;
355 String alph = hmm.getAlphabetType();
356 if (alph.equals("amino"))
361 else if (alph.equals("DNA"))
367 char symbols[] = new char[size];
368 int values[] = new int[size];
370 List<Character> charList = hmm.getSymbols();
373 for (char character : charList)
375 symbols[i] = character;
379 Integer totalCount = 0;
380 for (int j = 0; j < size; j++)
383 char symbol = symbols[j];
384 value = hmm.getMatchEmissionProbability(column, symbol);
387 if (alphabet == AMINO && removeBelowBackground)
389 freq = ResidueProperties.aminoBackgroundFrequencies.get(symbol);
395 else if (alphabet == DNA && removeBelowBackground)
397 freq = ResidueProperties.nucleotideBackgroundFrequencies
404 value = value * 10000;
405 values[j] = value.intValue();
406 totalCount += value.intValue();
409 QuickSort.sort(values, symbols);
411 int[] profile = new int[3 + size * 2];
413 profile[0] = AlignmentAnnotation.SEQUENCE_PROFILE;
415 profile[2] = totalCount;
420 for (int k = size - 1; k >= 0; k--)
423 Integer value = values[k];
424 percentage = (value.doubleValue() / totalCount.doubleValue())
426 profile[arrayPos] = symbols[k];
427 profile[arrayPos + 1] = percentage.intValue();
437 * Returns profile data; the first element is the profile type, the second is
438 * the number of distinct values, the third the total count, and the remainder
439 * depend on the profile type.
445 int[] getProfileFor(AlignmentAnnotation aa, int column)
447 // TODO : consider refactoring the global alignment calculation
448 // properties/rendering attributes as a global 'alignment group' which holds
449 // all vis settings for the alignment as a whole rather than a subset
451 if (aa.label.startsWith("Information"))
453 return getHMMProfileFor(aa, column, true);
455 if (aa.autoCalculated
456 && (aa.label.startsWith("Consensus") || aa.label
457 .startsWith("cDNA Consensus")))
459 boolean forComplement = aa.label.startsWith("cDNA Consensus");
460 if (aa.groupRef != null && aa.groupRef.consensusData != null
461 && aa.groupRef.isShowSequenceLogo())
463 // TODO? group consensus for cDNA complement
464 return AAFrequency.extractProfile(
465 aa.groupRef.consensusData.get(column),
466 aa.groupRef.getIgnoreGapsConsensus());
468 // TODO extend annotation row to enable dynamic and static profile data to
470 if (aa.groupRef == null && aa.sequenceRef == null)
474 return AAFrequency.extractCdnaProfile(
475 complementConsensus[column], av_ignoreGapsConsensus);
479 return AAFrequency.extractProfile(
480 hconsensus.get(column),
481 av_ignoreGapsConsensus);
487 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
489 // TODO implement group structure consensus
491 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
492 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
493 * group selections return StructureFrequency.extractProfile(
494 * aa.groupRef.consensusData[column], aa.groupRef
495 * .getIgnoreGapsConsensus()); }
497 // TODO extend annotation row to enable dynamic and static profile data
500 if (aa.groupRef == null && aa.sequenceRef == null
501 && hStrucConsensus != null
502 && hStrucConsensus.length > column)
504 return StructureFrequency.extractProfile(hStrucConsensus[column],
505 av_ignoreGapsConsensus);
515 * Render the annotation rows associated with an alignment.
520 * data and view settings to render
522 * destination for graphics
524 * row where a mouse event occured (or -1)
526 * first column that will be drawn
528 * last column that will be drawn
529 * @return true if the fadedImage was used for any alignment annotation rows
530 * currently being calculated
532 public boolean drawComponent(AwtRenderPanelI annotPanel,
533 AlignViewportI av, Graphics g, int activeRow, int startRes,
536 long stime = System.currentTimeMillis();
537 boolean usedFaded = false;
539 // AnnotationPanel needs to implement: ImageObserver, access to
541 updateFromAwtRenderPanel(annotPanel, av);
542 fm = g.getFontMetrics();
543 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
554 boolean validRes = false;
555 boolean validEnd = false;
556 boolean labelAllCols = false;
557 boolean centreColLabels;
558 boolean centreColLabelsDef = av.isCentreColumnLabels();
559 boolean scaleColLabel = false;
560 final AlignmentAnnotation consensusAnnot = av
561 .getAlignmentConsensusAnnotation();
562 final AlignmentAnnotation structConsensusAnnot = av
563 .getAlignmentStrucConsensusAnnotation();
564 final AlignmentAnnotation complementConsensusAnnot = av
565 .getComplementConsensusAnnotation();
566 boolean renderHistogram = true, renderProfile = true, normaliseProfile = false, isRNA = rna;
568 BitSet graphGroupDrawn = new BitSet();
569 int charOffset = 0; // offset for a label
570 float fmWidth, fmScaling = 1f; // scaling for a label to fit it into a
572 Font ofont = g.getFont();
575 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
576 boolean clipst = false, clipend = false;
577 for (int i = 0; i < aa.length; i++)
579 AlignmentAnnotation row = aa[i];
582 // check if this is a consensus annotation row and set the display
583 // settings appropriately
584 // TODO: generalise this to have render styles for consensus/profile
586 if (row.groupRef != null && row == row.groupRef.getConsensus())
588 renderHistogram = row.groupRef.isShowConsensusHistogram();
589 renderProfile = row.groupRef.isShowSequenceLogo();
590 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
592 else if (row == consensusAnnot || row == structConsensusAnnot
593 || row == complementConsensusAnnot)
595 renderHistogram = av_renderHistogram;
596 renderProfile = av_renderProfile;
597 normaliseProfile = av_normaliseProfile;
601 renderHistogram = true;
602 // don't need to set render/normaliseProfile since they are not
603 // currently used in any other annotation track renderer
606 Annotation[] row_annotations = row.annotations;
611 centreColLabels = row.centreColLabels || centreColLabelsDef;
612 labelAllCols = row.showAllColLabels;
613 scaleColLabel = row.scaleColLabel;
618 || ((y - charHeight) < visHeight && (y + row.height + charHeight * 2) >= sOffset))
619 {// if_in_visible_region
630 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
635 // this is so that we draw the characters below the graph
640 iconOffset = charHeight - fm.getDescent();
644 else if (row.hasText)
646 iconOffset = charHeight - fm.getDescent();
654 if (row.autoCalculated && av.isCalculationInProgress(row))
658 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0, y
659 - row.height, imgWidth, y, annotationPanel);
660 g.setColor(Color.black);
661 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
667 * else if (annotationPanel.av.updatingConservation &&
668 * aa[i].label.equals("Conservation")) {
670 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
671 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
672 * annotationPanel.imgWidth, y, annotationPanel);
674 * g.setColor(Color.black); //
675 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
677 * continue; } else if (annotationPanel.av.updatingConservation &&
678 * aa[i].label.equals("Quality")) {
680 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
681 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
682 * annotationPanel.imgWidth, y, annotationPanel);
683 * g.setColor(Color.black); // /
684 * g.drawString("Calculating Quality....",20, y-row.height/2);
688 // first pass sets up state for drawing continuation from left-hand
691 x = (startRes == 0) ? 0 : -1;
692 while (x < endRes - startRes)
694 if (hasHiddenColumns)
696 column = hiddenColumns.adjustForHiddenColumns(startRes + x);
697 if (column > row_annotations.length - 1)
704 column = startRes + x;
707 if ((row_annotations == null)
708 || (row_annotations.length <= column)
709 || (row_annotations[column] == null))
717 final String displayChar = validRes ? row_annotations[column].displayCharacter
723 g.setColor(Color.red);
725 if (columnSelection != null)
727 if (columnSelection.contains(column))
729 g.fillRect(x * charWidth, y, charWidth, charHeight);
733 if (row.getInvalidStrucPos() > x)
735 g.setColor(Color.orange);
736 g.fillRect(x * charWidth, y, charWidth, charHeight);
738 else if (row.getInvalidStrucPos() == x)
740 g.setColor(Color.orange.darker());
741 g.fillRect(x * charWidth, y, charWidth, charHeight);
743 if (validCharWidth && validRes && displayChar != null
744 && (displayChar.length() > 0))
747 fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
748 displayChar.length());
749 if (/* centreColLabels || */scaleColLabel)
751 // fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
752 // displayChar.length());
754 // if (scaleColLabel)
756 // justify the label and scale to fit in column
757 if (fmWidth > charWidth)
759 // scale only if the current font isn't already small enough
760 fmScaling = charWidth;
761 fmScaling /= fmWidth;
762 g.setFont(ofont.deriveFont(AffineTransform
763 .getScaleInstance(fmScaling, 1.0)));
764 // and update the label's width to reflect the scaling.
769 // TODO is it ok to use width of / show all characters here?
772 // fmWidth = fm.charWidth(displayChar.charAt(0));
774 charOffset = (int) ((charWidth - fmWidth) / 2f);
776 if (row_annotations[column].colour == null)
778 g.setColor(Color.black);
782 g.setColor(row_annotations[column].colour);
785 if (column == 0 || row.graph > 0)
787 g.drawString(displayChar, (x * charWidth) + charOffset, y
790 else if (row_annotations[column - 1] == null
793 .equals(row_annotations[column - 1].displayCharacter) || (displayChar
794 .length() < 2 && row_annotations[column].secondaryStructure == ' ')))
796 g.drawString(displayChar, x * charWidth + charOffset, y
804 char ss = validRes ? row_annotations[column].secondaryStructure
809 // distinguish between forward/backward base-pairing
810 if (displayChar.indexOf(')') > -1)
819 if ((displayChar.indexOf(']') > -1))
827 // distinguish between forward/backward base-pairing
828 if (displayChar.indexOf('}') > -1)
836 // distinguish between forward/backward base-pairing
837 if (displayChar.indexOf('<') > -1)
843 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
845 // distinguish between forward/backward base-pairing
846 int ssLowerCase = ss + UPPER_TO_LOWER;
847 // TODO would .equals() be safer here? or charAt(0)?
848 if (displayChar.indexOf(ssLowerCase) > -1)
850 ss = (char) ssLowerCase;
854 if (!validRes || (ss != lastSS))
860 int nb_annot = x - temp;
861 // System.out.println("\t type :"+lastSS+"\t x :"+x+"\t nbre annot :"+nb_annot);
864 case '(': // Stem case for RNA secondary structure
865 case ')': // and opposite direction
866 drawStemAnnot(g, row_annotations, lastSSX, x, y,
867 iconOffset, startRes, column, validRes, validEnd);
874 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
875 iconOffset, startRes, column, validRes,
879 // no break if isRNA - falls through to drawNotCanonicalAnnot!
883 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
884 iconOffset, startRes, column, validRes,
888 // no break if isRNA - fall through to drawNotCanonicalAnnot!
947 Color nonCanColor = getNotCanonicalColor(lastSS);
948 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
949 lastSSX, x, y, iconOffset, startRes, column,
954 g.setColor(Color.gray);
955 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth)
971 lastSSX = (x * charWidth);
978 if (column >= row_annotations.length)
980 column = row_annotations.length - 1;
987 if ((row_annotations == null) || (row_annotations.length <= column)
988 || (row_annotations[column] == null))
1006 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1007 startRes, column, validRes, validEnd);
1010 // no break if isRNA - fall through to drawNotCanonicalAnnot!
1015 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1016 startRes, column, validRes, validEnd);
1019 // no break if isRNA - fall through to drawNotCanonicalAnnot!
1022 case ')': // Stem case for RNA secondary structure
1024 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
1025 startRes, column, validRes, validEnd);
1082 // System.out.println(lastSS);
1083 Color nonCanColor = getNotCanonicalColor(lastSS);
1084 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1085 x, y, iconOffset, startRes, column, validRes, validEnd);
1088 drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
1089 startRes, column, validRes, validEnd);
1094 if (row.graph > 0 && row.graphHeight > 0)
1096 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1098 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1100 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1101 // computed efficiently for all visible labels
1102 float groupmax = -999999, groupmin = 9999999;
1103 for (int gg = 0; gg < aa.length; gg++)
1105 if (aa[gg].graphGroup != row.graphGroup)
1112 aa[gg].visible = false;
1114 if (aa[gg].graphMax > groupmax)
1116 groupmax = aa[gg].graphMax;
1118 if (aa[gg].graphMin < groupmin)
1120 groupmin = aa[gg].graphMin;
1124 for (int gg = 0; gg < aa.length; gg++)
1126 if (aa[gg].graphGroup == row.graphGroup)
1128 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1129 endRes, y, groupmin, groupmax, row.graphHeight);
1133 graphGroupDrawn.set(row.graphGroup);
1137 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1138 row.graphMin, row.graphMax, row.graphHeight);
1141 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1143 drawBarGraph(g, row, row_annotations, startRes, endRes,
1144 row.graphMin, row.graphMax, y, renderHistogram,
1145 renderProfile, normaliseProfile);
1151 if (clipst && !clipend)
1155 }// end if_in_visible_region
1156 if (row.graph > 0 && row.hasText)
1172 System.err.println("Start clip at : " + yfrom + " (index " + f_i
1177 System.err.println("End clip at : " + yto + " (index " + f_to
1182 System.err.println("Annotation Rendering time:"
1183 + (System.currentTimeMillis() - stime));
1190 public static final Color GLYPHLINE_COLOR = Color.gray;
1192 public static final Color SHEET_COLOUR = Color.green;
1194 public static final Color HELIX_COLOUR = Color.red;
1196 public static final Color STEM_COLOUR = Color.blue;
1198 private Color sdNOTCANONICAL_COLOUR;
1200 void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x,
1201 int y, int iconOffset, int startRes, int column,
1202 boolean validRes, boolean validEnd)
1204 g.setColor(GLYPHLINE_COLOR);
1205 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1208 void drawSheetAnnot(Graphics g, Annotation[] row,
1210 int lastSSX, int x, int y, int iconOffset, int startRes, int column,
1211 boolean validRes, boolean validEnd)
1213 g.setColor(SHEET_COLOUR);
1215 if (!validEnd || !validRes || row == null || row[column] == null
1216 || row[column].secondaryStructure != 'E')
1218 g.fillRect(lastSSX, y + 4 + iconOffset,
1219 (x * charWidth) - lastSSX - 4, 7);
1220 g.fillPolygon(new int[] { (x * charWidth) - 4, (x * charWidth) - 4,
1221 (x * charWidth) }, new int[] { y + iconOffset,
1222 y + 14 + iconOffset, y + 7 + iconOffset }, 3);
1226 g.fillRect(lastSSX, y + 4 + iconOffset,
1227 (x + 1) * charWidth - lastSSX, 7);
1232 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1233 int y, int iconOffset, int startRes, int column,
1234 boolean validRes, boolean validEnd)
1236 g.setColor(HELIX_COLOUR);
1238 int sCol = (lastSSX / charWidth) + startRes;
1240 int x2 = (x * charWidth);
1244 int ofs = charWidth / 2;
1245 // Off by 1 offset when drawing rects and ovals
1246 // to offscreen image on the MAC
1247 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
1248 if (sCol == 0 || row[sCol - 1] == null
1249 || row[sCol - 1].secondaryStructure != 'H')
1254 // g.setColor(Color.orange);
1255 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
1258 if (!validRes || row[column] == null
1259 || row[column].secondaryStructure != 'H')
1265 // g.setColor(Color.magenta);
1266 g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset, x2 - x1 - ofs
1274 if (sCol == 0 || row[sCol - 1] == null
1275 || row[sCol - 1].secondaryStructure != 'H')
1277 g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1278 x1 += charWidth / 2;
1281 if (!validRes || row[column] == null
1282 || row[column].secondaryStructure != 'H')
1284 g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
1286 x2 -= charWidth / 2;
1289 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1292 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1293 Annotation[] aa_annotations, int sRes, int eRes, int y,
1294 float min, float max, int graphHeight)
1296 if (sRes > aa_annotations.length)
1303 // Adjustment for fastpaint to left
1309 eRes = Math.min(eRes, aa_annotations.length);
1317 float range = max - min;
1322 y2 = y - (int) ((0 - min / range) * graphHeight);
1325 g.setColor(Color.gray);
1326 g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1328 eRes = Math.min(eRes, aa_annotations.length);
1331 int aaMax = aa_annotations.length - 1;
1333 while (x < eRes - sRes)
1336 if (hasHiddenColumns)
1338 column = hiddenColumns.adjustForHiddenColumns(column);
1346 if (aa_annotations[column] == null
1347 || aa_annotations[column - 1] == null)
1353 if (aa_annotations[column].colour == null)
1355 g.setColor(Color.black);
1359 g.setColor(aa_annotations[column].colour);
1363 - (int) (((aa_annotations[column - 1].value - min) / range) * graphHeight);
1365 - (int) (((aa_annotations[column].value - min) / range) * graphHeight);
1367 g.drawLine(x * charWidth - charWidth / 2, y1, x * charWidth
1368 + charWidth / 2, y2);
1372 if (_aa.threshold != null)
1374 g.setColor(_aa.threshold.colour);
1375 Graphics2D g2 = (Graphics2D) g;
1376 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1377 BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f }, 0f));
1379 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1380 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1381 g2.setStroke(new BasicStroke());
1385 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1386 Annotation[] aa_annotations, int sRes, int eRes, float min,
1387 float max, int y, boolean renderHistogram, boolean renderProfile,
1388 boolean normaliseProfile)
1390 if (sRes > aa_annotations.length)
1394 Font ofont = g.getFont();
1395 eRes = Math.min(eRes, aa_annotations.length);
1397 int x = 0, y1 = y, y2 = y;
1399 float range = max - min;
1403 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1406 g.setColor(Color.gray);
1408 g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1411 int aaMax = aa_annotations.length - 1;
1412 while (x < eRes - sRes)
1415 if (hasHiddenColumns)
1417 column = hiddenColumns.adjustForHiddenColumns(column);
1425 if (aa_annotations[column] == null)
1430 if (aa_annotations[column].colour == null)
1432 g.setColor(Color.black);
1436 g.setColor(aa_annotations[column].colour);
1440 - (int) (((aa_annotations[column].value - min) / (range)) * _aa.graphHeight);
1442 if (renderHistogram)
1446 g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
1450 g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
1453 // draw profile if available
1458 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1460 int profl[] = getProfileFor(_aa, column);
1462 // just try to draw the logo if profl is not null
1463 if (profl != null && profl[2] != 0)
1465 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1466 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1467 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1468 double htn = normaliseProfile ? _aa.graphHeight : (y2 - y1);// aa.graphHeight;
1475 * Render a single base for a sequence profile, a base pair for
1476 * structure profile, and a triplet for a cdna profile
1478 dc = new char[isStructureProfile ? 2 : (isCdnaProfile ? 3 : 1)];
1480 LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1481 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1482 float ofontHeight = 1f / lm.getAscent();// magnify to fill box
1486 * Traverse the character(s)/percentage data in the array
1489 int valuesProcessed = 0;
1490 // profl[1] is the number of values in the profile
1491 while (valuesProcessed < profl[1])
1493 if (isStructureProfile)
1495 // todo can we encode a structure pair as an int, like codons?
1496 dc[0] = (char) profl[c++];
1497 dc[1] = (char) profl[c++];
1499 else if (isCdnaProfile)
1501 dc = CodingUtils.decodeCodon(profl[c++]);
1505 dc[0] = (char) profl[c++];
1509 wdth /= fm.charsWidth(dc, 0, dc.length);
1512 // next profl[] position is profile % for the character(s)
1513 scl = htn * scale * profl[c++];
1514 lm = ofont.getLineMetrics(dc, 0, 1, g.getFontMetrics()
1515 .getFontRenderContext());
1516 Font font = ofont.deriveFont(AffineTransform.getScaleInstance(
1517 wdth, scl / lm.getAscent()));
1519 lm = g.getFontMetrics().getLineMetrics(dc, 0, 1, g);
1521 // Debug - render boxes around characters
1522 // g.setColor(Color.red);
1523 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1525 // g.setColor(profcolour.findColour(dc[0]).darker());
1528 * Set character colour as per alignment colour scheme; use the
1529 * codon translation if a cDNA profile
1531 Color colour = null;
1534 final String codonTranslation = ResidueProperties
1535 .codonTranslate(new String(dc));
1536 colour = profcolour.findColour(codonTranslation.charAt(0),
1541 colour = profcolour.findColour(dc[0], column, null);
1543 g.setColor(colour == Color.white ? Color.lightGray : colour);
1545 hght = (ht + (scl - lm.getDescent() - lm.getBaselineOffsets()[lm
1546 .getBaselineIndex()]));
1548 g.drawChars(dc, 0, dc.length, x * charWidth, (int) hght);
1556 if (_aa.threshold != null)
1558 g.setColor(_aa.threshold.colour);
1559 Graphics2D g2 = (Graphics2D) g;
1560 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1561 BasicStroke.JOIN_ROUND, 3f, new float[] { 5f, 3f }, 0f));
1563 y2 = (int) (y - ((_aa.threshold.value - min) / range)
1565 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1566 g2.setStroke(new BasicStroke());
1570 // used by overview window
1571 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1572 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1574 eRes = Math.min(eRes, aa_annotations.length);
1575 g.setColor(Color.white);
1576 g.fillRect(0, 0, width, y);
1577 g.setColor(new Color(0, 0, 180));
1581 for (int j = sRes; j < eRes; j++)
1583 if (aa_annotations[j] != null)
1585 if (aa_annotations[j].colour == null)
1587 g.setColor(Color.black);
1591 g.setColor(aa_annotations[j].colour);
1594 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1600 g.fillRect(x, y - height, charWidth, height);
1606 Color getNotCanonicalColor(char lastss)
1612 return new Color(255, 125, 5);
1616 return new Color(245, 115, 10);
1620 return new Color(235, 135, 15);
1624 return new Color(225, 105, 20);
1628 return new Color(215, 145, 30);
1632 return new Color(205, 95, 35);
1636 return new Color(195, 155, 45);
1640 return new Color(185, 85, 55);
1644 return new Color(175, 165, 65);
1648 return new Color(170, 75, 75);
1652 return new Color(160, 175, 85);
1656 return new Color(150, 65, 95);
1660 return new Color(140, 185, 105);
1664 return new Color(130, 55, 110);
1668 return new Color(120, 195, 120);
1672 return new Color(110, 45, 130);
1676 return new Color(100, 205, 140);
1680 return new Color(90, 35, 150);
1684 return new Color(85, 215, 160);
1688 return new Color(75, 25, 170);
1692 return new Color(65, 225, 180);
1696 return new Color(55, 15, 185);
1700 return new Color(45, 235, 195);
1704 return new Color(35, 5, 205);
1708 return new Color(25, 245, 215);
1712 return new Color(15, 0, 225);
1716 return new Color(10, 255, 235);
1720 return new Color(5, 150, 245);
1724 return new Color(0, 80, 255);
1727 System.out.println("This is not a interaction : " + lastss);