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.workers.InformationThread;
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.geom.AffineTransform;
49 import java.awt.image.ImageObserver;
50 import java.util.BitSet;
51 import java.util.Hashtable;
53 public class AnnotationRenderer
55 private static final int UPPER_TO_LOWER = 'a' - 'A'; // 32
57 private static final int CHAR_A = 'A'; // 65
59 private static final int CHAR_Z = 'Z'; // 90
62 * flag indicating if timing and redraw parameter info should be output
64 private final boolean debugRedraw;
66 private int charWidth, endRes, charHeight;
68 private boolean validCharWidth, hasHiddenColumns;
70 private FontMetrics fm;
72 private final boolean USE_FILL_ROUND_RECT = Platform.isAMacAndNotJS();
74 // todo remove these flags, read from group/viewport where needed
75 boolean av_renderHistogram = true;
77 boolean av_renderProfile = true;
79 boolean av_normaliseProfile = false;
81 boolean av_infoHeight = false;
83 ResidueShaderI profcolour = null;
85 private ColumnSelection columnSelection;
87 private HiddenColumns hiddenColumns;
89 private ProfilesI hconsensus;
91 private Hashtable<String, Object>[] complementConsensus;
93 private Hashtable<String, Object>[] hStrucConsensus;
95 private boolean av_ignoreGapsConsensus;
97 private boolean av_ignoreBelowBackground;
100 * attributes set from AwtRenderPanelI
103 * old image used when data is currently being calculated and cannot be
106 private Image fadedImage;
109 * panel being rendered into
111 private ImageObserver annotationPanel;
114 * width of image to render in panel
116 private int imgWidth;
119 * offset to beginning of visible area
124 * offset to end of visible area
126 private int visHeight;
129 * indicate if the renderer should only render the visible portion of the
130 * annotation given the current view settings
132 private boolean useClip = true;
135 * master flag indicating if renderer should ever try to clip. not enabled for
138 private boolean canClip = false;
140 public AnnotationRenderer()
146 * Create a new annotation Renderer
149 * flag indicating if timing and redraw parameter info should be
152 public AnnotationRenderer(boolean debugRedraw)
154 this.debugRedraw = debugRedraw;
158 * Remove any references and resources when this object is no longer required
160 public void dispose()
162 hiddenColumns = null;
164 complementConsensus = null;
165 hStrucConsensus = null;
167 annotationPanel = null;
170 void drawStemAnnot(Graphics g, Annotation[] row_annotations, int lastSSX,
171 int x, int y, int iconOffset, int startRes, int column,
172 boolean validRes, boolean validEnd)
174 g.setColor(STEM_COLOUR);
175 int sCol = (lastSSX / charWidth)
176 + hiddenColumns.visibleToAbsoluteColumn(startRes);
178 int x2 = (x * charWidth);
180 char dc = (column == 0 || row_annotations[column - 1] == null) ? ' '
181 : row_annotations[column - 1].secondaryStructure;
183 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
184 || dc != row_annotations[sCol - 1].secondaryStructure;
185 boolean diffdownstream = !validRes || !validEnd
186 || row_annotations[column] == null
187 || dc != row_annotations[column].secondaryStructure;
189 if (column > 0 && Rna.isClosingParenthesis(dc))
192 // if (validRes && column>1 && row_annotations[column-2]!=null &&
193 // dc.equals(row_annotations[column-2].displayCharacter))
196 * if new annotation with a closing base pair half of the stem,
197 * display a backward arrow
199 g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
201 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
212 // display a forward arrow
216 * if annotation ending with an opeing base pair half of the stem,
217 * display a forward arrow
219 g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 },
221 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
231 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
234 void drawNotCanonicalAnnot(Graphics g, Color nonCanColor,
235 Annotation[] row_annotations, int lastSSX, int x, int y,
236 int iconOffset, int startRes, int column, boolean validRes,
239 // System.out.println(nonCanColor);
241 g.setColor(nonCanColor);
242 int sCol = (lastSSX / charWidth)
243 + hiddenColumns.visibleToAbsoluteColumn(startRes);
245 int x2 = (x * charWidth);
247 String dc = (column == 0 || row_annotations[column - 1] == null) ? ""
248 : row_annotations[column - 1].displayCharacter;
250 boolean diffupstream = sCol == 0 || row_annotations[sCol - 1] == null
251 || !dc.equals(row_annotations[sCol - 1].displayCharacter);
252 boolean diffdownstream = !validRes || !validEnd
253 || row_annotations[column] == null
254 || !dc.equals(row_annotations[column].displayCharacter);
255 // System.out.println("Column "+column+" diff up: "+diffupstream+"
256 // down:"+diffdownstream);
257 // If a closing base pair half of the stem, display a backward arrow
258 if (column > 0 && Rna.isClosingParenthesis(dc))
262 // if (validRes && column>1 && row_annotations[column-2]!=null &&
263 // dc.equals(row_annotations[column-2].displayCharacter))
265 g.fillPolygon(new int[] { lastSSX + 5, lastSSX + 5, lastSSX },
267 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
279 // display a forward arrow
282 g.fillPolygon(new int[] { x2 - 5, x2 - 5, x2 },
284 { y + iconOffset, y + 14 + iconOffset, y + 8 + iconOffset },
294 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 7);
297 // public void updateFromAnnotationPanel(FontMetrics annotFM, AlignViewportI
299 public void updateFromAwtRenderPanel(AwtRenderPanelI annotPanel,
302 fm = annotPanel.getFontMetrics();
303 annotationPanel = annotPanel;
304 fadedImage = annotPanel.getFadedImage();
305 imgWidth = annotPanel.getFadedImageWidth();
306 // visible area for rendering
307 int[] bounds = annotPanel.getVisibleVRange();
311 visHeight = bounds[1];
326 updateFromAlignViewport(av);
329 public void updateFromAlignViewport(AlignViewportI av)
331 charWidth = av.getCharWidth();
332 endRes = av.getRanges().getEndRes();
333 charHeight = av.getCharHeight();
334 hasHiddenColumns = av.hasHiddenColumns();
335 validCharWidth = av.isValidCharWidth();
336 av_renderHistogram = av.isShowConsensusHistogram();
337 av_renderProfile = av.isShowSequenceLogo();
338 av_normaliseProfile = av.isNormaliseSequenceLogo();
339 profcolour = av.getResidueShading();
340 if (profcolour == null || profcolour.getColourScheme() == null)
343 * Use default colour for sequence logo if
344 * the alignment has no colourscheme set
345 * (would like to use user preference but n/a for applet)
347 ColourSchemeI col = av.getAlignment().isNucleotide()
348 ? new NucleotideColourScheme()
349 : new ZappoColourScheme();
350 profcolour = new ResidueShader(col);
352 columnSelection = av.getColumnSelection();
353 hiddenColumns = av.getAlignment().getHiddenColumns();
354 hconsensus = av.getSequenceConsensusHash();
355 complementConsensus = av.getComplementConsensusHash();
356 hStrucConsensus = av.getRnaStructureConsensusHash();
357 av_ignoreGapsConsensus = av.isIgnoreGapsConsensus();
358 av_ignoreBelowBackground = av.isIgnoreBelowBackground();
359 av_infoHeight = av.isInfoLetterHeight();
365 * Returns profile data; the first element is the profile type, the second is
366 * the number of distinct values, the third the total count, and the remainder
367 * depend on the profile type.
373 int[] getProfileFor(AlignmentAnnotation aa, int column)
375 // TODO : consider refactoring the global alignment calculation
376 // properties/rendering attributes as a global 'alignment group' which holds
377 // all vis settings for the alignment as a whole rather than a subset
379 if (InformationThread.HMM_CALC_ID.equals(aa.getCalcId()))
381 HiddenMarkovModel hmm = aa.sequenceRef.getHMM();
382 return AAFrequency.extractHMMProfile(hmm, column,
383 av_ignoreBelowBackground, av_infoHeight); // TODO check if this follows standard
386 if (aa.autoCalculated
387 && (aa.label.startsWith("Consensus") || aa.label
388 .startsWith("cDNA Consensus")))
390 boolean forComplement = aa.label.startsWith("cDNA Consensus");
391 if (aa.groupRef != null && aa.groupRef.getConsensusData() != null
392 && aa.groupRef.isShowSequenceLogo())
394 // TODO? group consensus for cDNA complement
395 return AAFrequency.extractProfile(
396 aa.groupRef.getConsensusData().get(column),
397 aa.groupRef.getIgnoreGapsConsensus());
399 // TODO extend annotation row to enable dynamic and static profile data to
401 if (aa.groupRef == null && aa.sequenceRef == null)
405 return AAFrequency.extractCdnaProfile(complementConsensus[column],
406 av_ignoreGapsConsensus);
410 return AAFrequency.extractProfile(hconsensus.get(column),
411 av_ignoreGapsConsensus);
417 if (aa.autoCalculated && aa.label.startsWith("StrucConsensus"))
419 // TODO implement group structure consensus
421 * if (aa.groupRef != null && aa.groupRef.consensusData != null &&
422 * aa.groupRef.isShowSequenceLogo()) { //TODO check what happens for
423 * group selections return StructureFrequency.extractProfile(
424 * aa.groupRef.consensusData[column], aa.groupRef
425 * .getIgnoreGapsConsensus()); }
427 // TODO extend annotation row to enable dynamic and static profile data
430 if (aa.groupRef == null && aa.sequenceRef == null
431 && hStrucConsensus != null
432 && hStrucConsensus.length > column)
434 return StructureFrequency.extractProfile(hStrucConsensus[column],
435 av_ignoreGapsConsensus);
443 * Render the annotation rows associated with an alignment.
448 * data and view settings to render
450 * destination for graphics
452 * row where a mouse event occured (or -1)
454 * first column that will be drawn
456 * last column that will be drawn
457 * @return true if the fadedImage was used for any alignment annotation rows
458 * currently being calculated
460 public boolean drawComponent(AwtRenderPanelI annotPanel,
461 AlignViewportI av, Graphics g, int activeRow, int startRes,
464 long stime = System.currentTimeMillis();
465 boolean usedFaded = false;
467 // AnnotationPanel needs to implement: ImageObserver, access to
469 updateFromAwtRenderPanel(annotPanel, av);
470 fm = g.getFontMetrics();
471 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
482 boolean validRes = false;
483 boolean validEnd = false;
484 boolean labelAllCols = false;
485 // boolean centreColLabels;
486 // boolean centreColLabelsDef = av.isCentreColumnLabels();
487 boolean scaleColLabel = false;
488 final AlignmentAnnotation consensusAnnot = av
489 .getAlignmentConsensusAnnotation();
490 final AlignmentAnnotation structConsensusAnnot = av
491 .getAlignmentStrucConsensusAnnotation();
492 final AlignmentAnnotation complementConsensusAnnot = av
493 .getComplementConsensusAnnotation();
495 BitSet graphGroupDrawn = new BitSet();
496 int charOffset = 0; // offset for a label
499 int yfrom = 0, f_i = 0, yto = 0, f_to = 0;
500 boolean clipst = false, clipend = false;
501 for (int i = 0; i < aa.length; i++)
503 AlignmentAnnotation row = aa[i];
504 boolean renderHistogram = true;
505 boolean renderProfile = false;
506 boolean normaliseProfile = false;
507 boolean isRNA = row.isRNA();
509 // check if this is a consensus annotation row and set the display
510 // settings appropriately
511 // TODO: generalise this to have render styles for consensus/profile
513 if (row.groupRef != null && row == row.groupRef.getConsensus())
515 renderHistogram = row.groupRef.isShowConsensusHistogram();
516 renderProfile = row.groupRef.isShowSequenceLogo();
517 normaliseProfile = row.groupRef.isNormaliseSequenceLogo();
519 else if (row == consensusAnnot || row == structConsensusAnnot
520 || row == complementConsensusAnnot)
522 renderHistogram = av_renderHistogram;
523 renderProfile = av_renderProfile;
524 normaliseProfile = av_normaliseProfile;
526 else if (InformationThread.HMM_CALC_ID.equals(row.getCalcId()))
528 if (row.groupRef != null)
530 renderHistogram = row.groupRef.isShowInformationHistogram();
531 renderProfile = row.groupRef.isShowHMMSequenceLogo();
532 normaliseProfile = row.groupRef.isNormaliseHMMSequenceLogo();
536 renderHistogram = av.isShowInformationHistogram();
537 renderProfile = av.isShowHMMSequenceLogo();
538 normaliseProfile = av.isNormaliseHMMSequenceLogo();
541 else if (row == consensusAnnot || row == structConsensusAnnot
542 || row == complementConsensusAnnot)
544 renderHistogram = av_renderHistogram;
545 renderProfile = av_renderProfile;
546 normaliseProfile = av_normaliseProfile;
549 Annotation[] row_annotations = row.annotations;
554 // centreColLabels = row.centreColLabels || centreColLabelsDef;
555 labelAllCols = row.showAllColLabels;
556 scaleColLabel = row.scaleColLabel;
560 if (!useClip || ((y - charHeight) < visHeight
561 && (y + row.height + charHeight * 2) >= sOffset))
562 {// if_in_visible_region
573 if (row.graphGroup > -1 && graphGroupDrawn.get(row.graphGroup))
578 // this is so that we draw the characters below the graph
583 iconOffset = charHeight - fm.getDescent();
587 else if (row.hasText)
589 iconOffset = charHeight - fm.getDescent();
597 if (row.autoCalculated && av.isCalculationInProgress(row))
601 g.drawImage(fadedImage, 0, y - row.height, imgWidth, y, 0,
602 y - row.height, imgWidth, y, annotationPanel);
603 g.setColor(Color.black);
604 // g.drawString("Calculating "+aa[i].label+"....",20, y-row.height/2);
610 * else if (annotationPanel.av.updatingConservation &&
611 * aa[i].label.equals("Conservation")) {
613 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
614 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
615 * annotationPanel.imgWidth, y, annotationPanel);
617 * g.setColor(Color.black); //
618 * g.drawString("Calculating Conservation.....",20, y-row.height/2);
620 * continue; } else if (annotationPanel.av.updatingConservation &&
621 * aa[i].label.equals("Quality")) {
623 * y += charHeight; g.drawImage(annotationPanel.fadedImage, 0, y -
624 * row.height, annotationPanel.imgWidth, y, 0, y - row.height,
625 * annotationPanel.imgWidth, y, annotationPanel);
626 * g.setColor(Color.black); // /
627 * g.drawString("Calculating Quality....",20, y-row.height/2);
631 // first pass sets up state for drawing continuation from left-hand
634 x = (startRes == 0) ? 0 : -1;
635 while (x < endRes - startRes)
637 if (hasHiddenColumns)
639 column = hiddenColumns.visibleToAbsoluteColumn(startRes + x);
640 if (column > row_annotations.length - 1)
647 column = startRes + x;
650 if ((row_annotations == null)
651 || (row_annotations.length <= column)
652 || (row_annotations[column] == null))
660 final String displayChar = validRes
661 ? row_annotations[column].displayCharacter
667 g.setColor(Color.red);
669 if (columnSelection != null)
671 if (columnSelection.contains(column))
673 g.fillRect(x * charWidth, y, charWidth, charHeight);
677 if (row.getInvalidStrucPos() > x)
679 g.setColor(Color.orange);
680 g.fillRect(x * charWidth, y, charWidth, charHeight);
682 else if (row.getInvalidStrucPos() == x)
684 g.setColor(Color.orange.darker());
685 g.fillRect(x * charWidth, y, charWidth, charHeight);
687 if (validCharWidth && validRes && displayChar != null
688 && (displayChar.length() > 0))
690 Graphics2D gg = ((Graphics2D) g);
691 float fmWidth = fm.charsWidth(displayChar.toCharArray(), 0,
692 displayChar.length());
695 * shrink label width to fit in column, if that is
696 * both configured and necessary
698 boolean scaledToFit = false;
699 float fmScaling = 1f;
700 if (scaleColLabel && fmWidth > charWidth)
703 fmScaling = charWidth;
704 fmScaling /= fmWidth;
705 // and update the label's width to reflect the scaling.
709 charOffset = (int) ((charWidth - fmWidth) / 2f);
711 if (row_annotations[column].colour == null)
713 gg.setColor(Color.black);
717 gg.setColor(row_annotations[column].colour);
721 * draw the label, unless it is the same secondary structure
722 * symbol (excluding RNA Helix) as the previous column
724 final int xPos = (x * charWidth) + charOffset;
725 final int yPos = y + iconOffset;
728 * translate to drawing position _before_ applying any scaling
730 gg.translate(xPos, yPos);
734 * use a scaling transform to make the label narrower
735 * (JalviewJS doesn't have Font.deriveFont(AffineTransform))
738 AffineTransform.getScaleInstance(fmScaling, 1.0));
740 if (column == 0 || row.graph > 0)
742 gg.drawString(displayChar, 0, 0);
744 else if (row_annotations[column - 1] == null || (labelAllCols
745 || !displayChar.equals(
746 row_annotations[column - 1].displayCharacter)
747 || (displayChar.length() < 2
748 && row_annotations[column].secondaryStructure == ' ')))
750 gg.drawString(displayChar, 0, 0);
755 * undo scaling before translating back
756 * (restoring saved transform does NOT work in JS PDFGraphics!)
758 gg.transform(AffineTransform
759 .getScaleInstance(1D / fmScaling, 1.0));
761 gg.translate(-xPos, -yPos);
766 char ss = validRes ? row_annotations[column].secondaryStructure
771 // distinguish between forward/backward base-pairing
772 if (displayChar.indexOf(')') > -1)
781 if ((displayChar.indexOf(']') > -1))
789 // distinguish between forward/backward base-pairing
790 if (displayChar.indexOf('}') > -1)
798 // distinguish between forward/backward base-pairing
799 if (displayChar.indexOf('<') > -1)
805 if (isRNA && (ss >= CHAR_A) && (ss <= CHAR_Z))
807 // distinguish between forward/backward base-pairing
808 int ssLowerCase = ss + UPPER_TO_LOWER;
809 // TODO would .equals() be safer here? or charAt(0)?
810 if (displayChar.indexOf(ssLowerCase) > -1)
812 ss = (char) ssLowerCase;
816 if (!validRes || (ss != lastSS))
822 // int nb_annot = x - temp;
823 // System.out.println("\t type :"+lastSS+"\t x :"+x+"\t nbre
824 // annot :"+nb_annot);
827 case '(': // Stem case for RNA secondary structure
828 case ')': // and opposite direction
829 drawStemAnnot(g, row_annotations, lastSSX, x, y,
830 iconOffset, startRes, column, validRes, validEnd);
837 drawHelixAnnot(g, row_annotations, lastSSX, x, y,
838 iconOffset, startRes, column, validRes,
842 // no break if isRNA - falls through to drawNotCanonicalAnnot!
846 drawSheetAnnot(g, row_annotations, lastSSX, x, y,
847 iconOffset, startRes, column, validRes,
851 // no break if isRNA - fall through to drawNotCanonicalAnnot!
910 Color nonCanColor = getNotCanonicalColor(lastSS);
911 drawNotCanonicalAnnot(g, nonCanColor, row_annotations,
912 lastSSX, x, y, iconOffset, startRes, column,
917 g.setColor(Color.gray);
918 g.fillRect(lastSSX, y + 6 + iconOffset,
919 (x * charWidth) - lastSSX, 2);
934 lastSSX = (x * charWidth);
941 if (column >= row_annotations.length)
943 column = row_annotations.length - 1;
950 if ((row_annotations == null) || (row_annotations.length <= column)
951 || (row_annotations[column] == null))
969 drawHelixAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
970 startRes, column, validRes, validEnd);
973 // no break if isRNA - fall through to drawNotCanonicalAnnot!
978 drawSheetAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
979 startRes, column, validRes, validEnd);
982 // no break if isRNA - fall through to drawNotCanonicalAnnot!
985 case ')': // Stem case for RNA secondary structure
987 drawStemAnnot(g, row_annotations, lastSSX, x, y, iconOffset,
988 startRes, column, validRes, validEnd);
1045 // System.out.println(lastSS);
1046 Color nonCanColor = getNotCanonicalColor(lastSS);
1047 drawNotCanonicalAnnot(g, nonCanColor, row_annotations, lastSSX,
1048 x, y, iconOffset, startRes, column, validRes, validEnd);
1051 drawGlyphLine(g, row_annotations, lastSSX, x, y, iconOffset,
1052 startRes, column, validRes, validEnd);
1057 if (row.graph > 0 && row.graphHeight > 0)
1059 if (row.graph == AlignmentAnnotation.LINE_GRAPH)
1061 if (row.graphGroup > -1 && !graphGroupDrawn.get(row.graphGroup))
1063 // TODO: JAL-1291 revise rendering model so the graphGroup map is
1064 // computed efficiently for all visible labels
1065 float groupmax = -999999, groupmin = 9999999;
1066 for (int gg = 0; gg < aa.length; gg++)
1068 if (aa[gg].graphGroup != row.graphGroup)
1075 aa[gg].visible = false;
1077 if (aa[gg].graphMax > groupmax)
1079 groupmax = aa[gg].graphMax;
1081 if (aa[gg].graphMin < groupmin)
1083 groupmin = aa[gg].graphMin;
1087 for (int gg = 0; gg < aa.length; gg++)
1089 if (aa[gg].graphGroup == row.graphGroup)
1091 drawLineGraph(g, aa[gg], aa[gg].annotations, startRes,
1092 endRes, y, groupmin, groupmax, row.graphHeight);
1096 graphGroupDrawn.set(row.graphGroup);
1100 drawLineGraph(g, row, row_annotations, startRes, endRes, y,
1101 row.graphMin, row.graphMax, row.graphHeight);
1104 else if (row.graph == AlignmentAnnotation.BAR_GRAPH)
1106 drawBarGraph(g, row, row_annotations, startRes, endRes,
1107 row.graphMin, row.graphMax, y, renderHistogram,
1108 renderProfile, normaliseProfile);
1114 if (clipst && !clipend)
1118 } // end if_in_visible_region
1119 if (row.graph > 0 && row.hasText)
1136 "Start clip at : " + yfrom + " (index " + f_i + ")");
1141 "End clip at : " + yto + " (index " + f_to + ")");
1145 System.err.println("Annotation Rendering time:"
1146 + (System.currentTimeMillis() - stime));
1153 public static final Color GLYPHLINE_COLOR = Color.gray;
1155 public static final Color SHEET_COLOUR = Color.green;
1157 public static final Color HELIX_COLOUR = Color.red;
1159 public static final Color STEM_COLOUR = Color.blue;
1161 // private Color sdNOTCANONICAL_COLOUR;
1163 void drawGlyphLine(Graphics g, Annotation[] row, int lastSSX, int x,
1164 int y, int iconOffset, int startRes, int column, boolean validRes,
1167 g.setColor(GLYPHLINE_COLOR);
1168 g.fillRect(lastSSX, y + 6 + iconOffset, (x * charWidth) - lastSSX, 2);
1171 void drawSheetAnnot(Graphics g, Annotation[] row,
1173 int lastSSX, int x, int y, int iconOffset, int startRes,
1174 int column, boolean validRes, boolean validEnd)
1176 g.setColor(SHEET_COLOUR);
1178 if (!validEnd || !validRes || row == null || row[column] == null
1179 || row[column].secondaryStructure != 'E')
1181 g.fillRect(lastSSX, y + 4 + iconOffset, (x * charWidth) - lastSSX - 4,
1185 { (x * charWidth) - 4, (x * charWidth) - 4, (x * charWidth) },
1187 { y + iconOffset, y + 14 + iconOffset, y + 7 + iconOffset },
1192 g.fillRect(lastSSX, y + 4 + iconOffset, (x + 1) * charWidth - lastSSX,
1198 void drawHelixAnnot(Graphics g, Annotation[] row, int lastSSX, int x,
1199 int y, int iconOffset, int startRes, int column, boolean validRes,
1202 g.setColor(HELIX_COLOUR);
1204 int sCol = (lastSSX / charWidth)
1205 + hiddenColumns.visibleToAbsoluteColumn(startRes);
1207 int x2 = (x * charWidth);
1209 if (USE_FILL_ROUND_RECT)
1211 int ofs = charWidth / 2;
1212 // Off by 1 offset when drawing rects and ovals
1213 // to offscreen image on the MAC
1214 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1, 8, 8, 8);
1215 if (sCol == 0 || row[sCol - 1] == null
1216 || row[sCol - 1].secondaryStructure != 'H')
1221 // g.setColor(Color.orange);
1222 g.fillRoundRect(lastSSX, y + 4 + iconOffset, x2 - x1 - ofs + 1, 8,
1225 if (!validRes || row[column] == null
1226 || row[column].secondaryStructure != 'H')
1232 // g.setColor(Color.magenta);
1233 g.fillRoundRect(lastSSX + ofs, y + 4 + iconOffset,
1234 x2 - x1 - ofs + 1, 8, 0, 0);
1241 if (sCol == 0 || row[sCol - 1] == null
1242 || row[sCol - 1].secondaryStructure != 'H')
1244 g.fillArc(lastSSX, y + 4 + iconOffset, charWidth, 8, 90, 180);
1245 x1 += charWidth / 2;
1248 if (!validRes || row[column] == null
1249 || row[column].secondaryStructure != 'H')
1251 g.fillArc((x * charWidth) - charWidth, y + 4 + iconOffset, charWidth,
1253 x2 -= charWidth / 2;
1256 g.fillRect(x1, y + 4 + iconOffset, x2 - x1, 8);
1259 void drawLineGraph(Graphics g, AlignmentAnnotation _aa,
1260 Annotation[] aa_annotations, int sRes, int eRes, int y, float min,
1261 float max, int graphHeight)
1263 if (sRes > aa_annotations.length)
1270 // Adjustment for fastpaint to left
1276 eRes = Math.min(eRes, aa_annotations.length);
1284 float range = max - min;
1289 y2 = y - (int) ((0 - min / range) * graphHeight);
1292 g.setColor(Color.gray);
1293 g.drawLine(x - charWidth, y2, (eRes - sRes + 1) * charWidth, y2);
1295 eRes = Math.min(eRes, aa_annotations.length);
1298 int aaMax = aa_annotations.length - 1;
1300 while (x < eRes - sRes)
1303 if (hasHiddenColumns)
1305 column = hiddenColumns.visibleToAbsoluteColumn(column);
1313 if (aa_annotations[column] == null
1314 || aa_annotations[column - 1] == null)
1320 if (aa_annotations[column].colour == null)
1322 g.setColor(Color.black);
1326 g.setColor(aa_annotations[column].colour);
1329 y1 = y - (int) (((aa_annotations[column - 1].value - min) / range)
1331 y2 = y - (int) (((aa_annotations[column].value - min) / range)
1334 g.drawLine(x * charWidth - charWidth / 2, y1,
1335 x * charWidth + charWidth / 2, y2);
1339 if (_aa.threshold != null)
1341 g.setColor(_aa.threshold.colour);
1342 Graphics2D g2 = (Graphics2D) g;
1343 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1344 BasicStroke.JOIN_ROUND, 3f, new float[]
1347 y2 = (int) (y - ((_aa.threshold.value - min) / range) * graphHeight);
1348 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1349 g2.setStroke(new BasicStroke());
1353 @SuppressWarnings("unused")
1354 void drawBarGraph(Graphics g, AlignmentAnnotation _aa,
1355 Annotation[] aa_annotations, int sRes, int eRes, float min,
1356 float max, int y, boolean renderHistogram, boolean renderProfile,
1357 boolean normaliseProfile)
1359 if (sRes > aa_annotations.length)
1363 Font ofont = g.getFont();
1364 eRes = Math.min(eRes, aa_annotations.length);
1366 int x = 0, y1 = y, y2 = y;
1368 float range = max - min;
1372 y2 = y - (int) ((0 - min / (range)) * _aa.graphHeight);
1375 g.setColor(Color.gray);
1377 g.drawLine(x, y2, (eRes - sRes) * charWidth, y2);
1380 int aaMax = aa_annotations.length - 1;
1381 while (x < eRes - sRes)
1384 if (hasHiddenColumns)
1386 column = hiddenColumns.visibleToAbsoluteColumn(column);
1394 if (aa_annotations[column] == null)
1399 if (aa_annotations[column].colour == null)
1401 g.setColor(Color.black);
1405 g.setColor(aa_annotations[column].colour);
1408 y1 = y - (int) (((aa_annotations[column].value - min) / (range))
1411 if (renderHistogram)
1415 g.fillRect(x * charWidth, y2, charWidth, y1 - y2);
1419 g.fillRect(x * charWidth, y1, charWidth, y2 - y1);
1422 // draw profile if available
1427 * {profile type, #values, total count, char1, pct1, char2, pct2...}
1429 int profl[] = getProfileFor(_aa, column);
1431 // just try to draw the logo if profl is not null
1432 if (profl != null && profl[2] != 0)
1434 boolean isStructureProfile = profl[0] == AlignmentAnnotation.STRUCTURE_PROFILE;
1435 boolean isCdnaProfile = profl[0] == AlignmentAnnotation.CDNA_PROFILE;
1436 float ht = normaliseProfile ? y - _aa.graphHeight : y1;
1437 final double normaliseFactor = normaliseProfile ? _aa.graphHeight
1441 * Render a single base for a sequence profile, a base pair for
1442 * structure profile, and a triplet for a cdna profile
1444 char[] dc = new char[isStructureProfile ? 2
1445 : (isCdnaProfile ? 3 : 1)];
1447 // lm is not necessary - we can just use fm - could be off by no more
1449 // LineMetrics lm = g.getFontMetrics(ofont).getLineMetrics("Q", g);
1450 // System.out.println(asc + " " + dec + " " + (asc - lm.getAscent())
1451 // + " " + (dec - lm.getDescent()));
1453 double asc = fm.getAscent();
1454 double dec = fm.getDescent();
1455 double fht = fm.getHeight();
1457 double scale = 1f / (normaliseProfile ? profl[2] : 100f);
1458 // float ofontHeight = 1f / fm.getAscent();// magnify to fill box
1461 * Traverse the character(s)/percentage data in the array
1466 // profl[1] is the number of values in the profile
1467 for (int i = 0, c = 3, last = profl[1]; i < last; i++)
1471 if (isStructureProfile)
1473 // todo can we encode a structure pair as an int, like codons?
1474 dc[0] = (char) profl[c++];
1475 dc[1] = (char) profl[c++];
1478 else if (isCdnaProfile)
1480 CodingUtils.decodeCodon2(profl[c++], dc);
1485 dc[0] = (char) profl[c++];
1488 // next profl[] position is profile % for the character(s)
1490 int percent = profl[c++];
1493 // failsafe in case a count rounds down to 0%
1496 double newHeight = normaliseFactor * scale * percent;
1499 * Set character colour as per alignment colour scheme; use the
1500 * codon translation if a cDNA profile
1502 Color colour = null;
1505 final String codonTranslation = ResidueProperties
1507 colour = profcolour.findColour(codonTranslation.charAt(0),
1512 colour = profcolour.findColour(dc[0], column, null);
1514 g.setColor(colour == Color.white ? Color.lightGray : colour);
1516 // Debug - render boxes around characters
1517 // g.setColor(Color.red);
1518 // g.drawRect(x*av.charWidth, (int)ht, av.charWidth,
1520 // g.setColor(profcolour.findColour(dc[0]).darker());
1522 double sx = 1f * charWidth / fm.charsWidth(dc, 0, dc.length);
1523 double sy = newHeight / asc;
1524 double newAsc = asc * sy;
1525 double newDec = dec * sy;
1526 // it is not necessary to recalculate lm for the new font.
1527 // note: lm.getBaselineOffsets()[lm.getBaselineIndex()]) must be 0
1528 // by definition. Was:
1529 // int hght = (int) (ht + (newAsc - newDec);
1530 // - lm.getBaselineOffsets()[lm.getBaselineIndex()]));
1532 if (Platform.isJS())
1535 * SwingJS does not implement font.deriveFont()
1536 * so use a scaling transform to draw instead,
1537 * this is off by a very small amount
1539 final int hght = (int) (ht2 + (newAsc - newDec));
1540 Graphics2D gg = (Graphics2D) g;
1541 int xShift = (int) Math.round(x * charWidth / sx);
1542 int yShift = (int) Math.round(hght / sy);
1543 gg.transform(AffineTransform.getScaleInstance(sx, sy));
1544 gg.drawString(s, xShift, yShift);
1546 AffineTransform.getScaleInstance(1D / sx, 1D / sy));
1556 // Java ('normal') method is to scale the font to fit
1558 final int hght = (int) (ht + (newAsc - newDec));
1560 .deriveFont(AffineTransform.getScaleInstance(sx, sy));
1562 g.drawChars(dc, 0, dc.length, x * charWidth, hght);
1572 if (_aa.threshold != null)
1574 g.setColor(_aa.threshold.colour);
1575 Graphics2D g2 = (Graphics2D) g;
1576 g2.setStroke(new BasicStroke(1, BasicStroke.CAP_SQUARE,
1577 BasicStroke.JOIN_ROUND, 3f, new float[]
1581 - ((_aa.threshold.value - min) / range) * _aa.graphHeight);
1582 g.drawLine(0, y2, (eRes - sRes) * charWidth, y2);
1583 g2.setStroke(new BasicStroke());
1587 // used by overview window
1588 public void drawGraph(Graphics g, AlignmentAnnotation _aa,
1589 Annotation[] aa_annotations, int width, int y, int sRes, int eRes)
1591 eRes = Math.min(eRes, aa_annotations.length);
1592 g.setColor(Color.white);
1593 g.fillRect(0, 0, width, y);
1594 g.setColor(new Color(0, 0, 180));
1598 for (int j = sRes; j < eRes; j++)
1600 if (aa_annotations[j] != null)
1602 if (aa_annotations[j].colour == null)
1604 g.setColor(Color.black);
1608 g.setColor(aa_annotations[j].colour);
1611 height = (int) ((aa_annotations[j].value / _aa.graphMax) * y);
1617 g.fillRect(x, y - height, charWidth, height);
1623 Color getNotCanonicalColor(char lastss)
1629 return new Color(255, 125, 5);
1633 return new Color(245, 115, 10);
1637 return new Color(235, 135, 15);
1641 return new Color(225, 105, 20);
1645 return new Color(215, 145, 30);
1649 return new Color(205, 95, 35);
1653 return new Color(195, 155, 45);
1657 return new Color(185, 85, 55);
1661 return new Color(175, 165, 65);
1665 return new Color(170, 75, 75);
1669 return new Color(160, 175, 85);
1673 return new Color(150, 65, 95);
1677 return new Color(140, 185, 105);
1681 return new Color(130, 55, 110);
1685 return new Color(120, 195, 120);
1689 return new Color(110, 45, 130);
1693 return new Color(100, 205, 140);
1697 return new Color(90, 35, 150);
1701 return new Color(85, 215, 160);
1705 return new Color(75, 25, 170);
1709 return new Color(65, 225, 180);
1713 return new Color(55, 15, 185);
1717 return new Color(45, 235, 195);
1721 return new Color(35, 5, 205);
1725 return new Color(25, 245, 215);
1729 return new Color(15, 0, 225);
1733 return new Color(10, 255, 235);
1737 return new Color(5, 150, 245);
1741 return new Color(0, 80, 255);
1744 System.out.println("This is not a interaction : " + lastss);