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.
23 import jalview.analysis.AnnotationSorter;
24 import jalview.api.AlignViewportI;
25 import jalview.api.AlignmentViewPanel;
26 import jalview.bin.Cache;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.SearchResults;
29 import jalview.datamodel.SequenceFeature;
30 import jalview.datamodel.SequenceGroup;
31 import jalview.datamodel.SequenceI;
32 import jalview.jbgui.GAlignmentPanel;
33 import jalview.math.AlignmentDimension;
34 import jalview.schemes.ResidueProperties;
35 import jalview.structure.StructureSelectionManager;
36 import jalview.util.MessageManager;
37 import jalview.util.Platform;
39 import java.awt.BorderLayout;
40 import java.awt.Color;
41 import java.awt.Container;
42 import java.awt.Dimension;
44 import java.awt.FontMetrics;
45 import java.awt.Graphics;
46 import java.awt.Insets;
47 import java.awt.event.AdjustmentEvent;
48 import java.awt.event.AdjustmentListener;
49 import java.awt.print.PageFormat;
50 import java.awt.print.Printable;
51 import java.awt.print.PrinterException;
52 import java.beans.PropertyChangeEvent;
53 import java.beans.PropertyChangeListener;
55 import java.io.FileWriter;
56 import java.io.PrintWriter;
57 import java.util.List;
59 import javax.swing.SwingUtilities;
65 * @version $Revision: 1.161 $
67 public class AlignmentPanel extends GAlignmentPanel implements
68 AdjustmentListener, Printable, AlignmentViewPanel
70 public AlignViewport av;
72 OverviewPanel overviewPanel;
74 private SeqPanel seqPanel;
76 private IdPanel idPanel;
78 private boolean headless;
79 IdwidthAdjuster idwidthAdjuster;
82 public AlignFrame alignFrame;
84 private ScalePanel scalePanel;
86 private AnnotationPanel annotationPanel;
88 private AnnotationLabels alabels;
90 // this value is set false when selection area being dragged
91 boolean fastPaint = true;
98 * Flag set while scrolling to follow complementary cDNA/protein scroll. When
99 * true, suppresses invoking the same method recursively.
101 private boolean followingComplementScroll;
104 * Creates a new AlignmentPanel object.
109 public AlignmentPanel(AlignFrame af, final AlignViewport av)
113 setSeqPanel(new SeqPanel(av, this));
114 setIdPanel(new IdPanel(av, this));
116 setScalePanel(new ScalePanel(av, this));
118 idPanelHolder.add(getIdPanel(), BorderLayout.CENTER);
119 idwidthAdjuster = new IdwidthAdjuster(this);
120 idSpaceFillerPanel1.add(idwidthAdjuster, BorderLayout.CENTER);
122 setAnnotationPanel(new AnnotationPanel(this));
123 setAlabels(new AnnotationLabels(this));
125 annotationScroller.setViewportView(getAnnotationPanel());
126 annotationSpaceFillerHolder.add(getAlabels(), BorderLayout.CENTER);
128 scalePanelHolder.add(getScalePanel(), BorderLayout.CENTER);
129 seqPanelHolder.add(getSeqPanel(), BorderLayout.CENTER);
131 setScrollValues(0, 0);
133 hscroll.addAdjustmentListener(this);
134 vscroll.addAdjustmentListener(this);
136 final AlignmentPanel ap = this;
137 av.addPropertyChangeListener(new PropertyChangeListener()
139 public void propertyChange(PropertyChangeEvent evt)
141 if (evt.getPropertyName().equals("alignment"))
143 PaintRefresher.Refresh(ap, av.getSequenceSetId(), true, true);
149 adjustAnnotationHeight();
154 public AlignViewportI getAlignViewport()
158 public void alignmentChanged()
160 av.alignmentChanged(this);
162 alignFrame.updateEditMenuBar();
164 paintAlignment(true);
171 public void fontChanged()
173 // set idCanvas bufferedImage to null
174 // to prevent drawing old image
175 FontMetrics fm = getFontMetrics(av.getFont());
177 scalePanelHolder.setPreferredSize(new Dimension(10, av.getCharHeight()
179 idSpaceFillerPanel1.setPreferredSize(new Dimension(10, av
183 getIdPanel().getIdCanvas().gg = null;
184 getSeqPanel().seqCanvas.img = null;
185 getAnnotationPanel().adjustPanelHeight();
187 Dimension d = calculateIdWidth();
189 d.setSize(d.width + 4, d.height);
190 getIdPanel().getIdCanvas().setPreferredSize(d);
191 hscrollFillerPanel.setPreferredSize(d);
193 if (overviewPanel != null)
195 overviewPanel.setBoxPosition();
197 if (this.alignFrame.getSplitViewContainer() != null)
199 ((SplitFrame) this.alignFrame.getSplitViewContainer()).adjustLayout();
206 * Calculate the width of the alignment labels based on the displayed names
207 * and any bounds on label width set in preferences.
209 * @return Dimension giving the maximum width of the alignment label panel
210 * that should be used.
212 public Dimension calculateIdWidth()
214 // calculate sensible default width when no preference is available
216 if (av.getIdWidth() < 0)
218 int afwidth = (alignFrame != null ? alignFrame.getWidth() : 300);
219 int maxwidth = Math.max(20, Math.min(afwidth - 200, 2 * afwidth / 3));
220 r = calculateIdWidth(maxwidth);
221 av.setIdWidth(r.width);
226 r.width = av.getIdWidth();
233 * Calculate the width of the alignment labels based on the displayed names
234 * and any bounds on label width set in preferences.
237 * -1 or maximum width allowed for IdWidth
238 * @return Dimension giving the maximum width of the alignment label panel
239 * that should be used.
241 public Dimension calculateIdWidth(int maxwidth)
243 Container c = new Container();
245 FontMetrics fm = c.getFontMetrics(new Font(av.font.getName(),
246 Font.ITALIC, av.font.getSize()));
248 AlignmentI al = av.getAlignment();
253 while ((i < al.getHeight()) && (al.getSequenceAt(i) != null))
255 SequenceI s = al.getSequenceAt(i);
257 id = s.getDisplayId(av.getShowJVSuffix());
259 if (fm.stringWidth(id) > idWidth)
261 idWidth = fm.stringWidth(id);
267 // Also check annotation label widths
270 if (al.getAlignmentAnnotation() != null)
272 fm = c.getFontMetrics(getAlabels().getFont());
274 while (i < al.getAlignmentAnnotation().length)
276 String label = al.getAlignmentAnnotation()[i].label;
278 if (fm.stringWidth(label) > idWidth)
280 idWidth = fm.stringWidth(label);
287 return new Dimension(maxwidth < 0 ? idWidth : Math.min(maxwidth,
292 * Highlight the given results on the alignment.
295 public void highlightSearchResults(SearchResults results)
297 scrollToPosition(results);
298 getSeqPanel().seqCanvas.highlightSearchResults(results);
302 * Scroll the view to show the position of the highlighted region in results
303 * (if any) and redraw the overview
307 public boolean scrollToPosition(SearchResults results)
309 return scrollToPosition(results, 0, true, false);
313 * Scroll the view to show the position of the highlighted region in results
316 * @param searchResults
317 * @param redrawOverview
320 public boolean scrollToPosition(SearchResults searchResults, boolean redrawOverview)
322 return scrollToPosition(searchResults, 0, redrawOverview, false);
326 * Scroll the view to show the position of the highlighted region in results
330 * @param verticalOffset
331 * if greater than zero, allows scrolling to a position below the
332 * first displayed sequence
333 * @param redrawOverview
334 * - when set, the overview will be recalculated (takes longer)
336 * if true, try to centre the search results horizontally in the view
337 * @return false if results were not found
339 public boolean scrollToPosition(SearchResults results,
341 boolean redrawOverview, boolean centre)
343 int startv, endv, starts, ends;
344 // TODO: properly locate search results in view when large numbers of hidden
345 // columns exist before highlighted region
346 // do we need to scroll the panel?
347 // TODO: tons of nullpointerexceptions raised here.
348 if (results != null && results.getSize() > 0 && av != null
349 && av.getAlignment() != null)
351 int seqIndex = av.getAlignment().findIndex(results);
356 SequenceI seq = av.getAlignment().getSequenceAt(seqIndex);
358 int[] r = results.getResults(seq, 0, av.getAlignment().getWidth());
366 // System.err.println(this.av.viewName + " Seq : " + seqIndex
367 // + " Scroll to " + start + "," + end);
370 * To centre results, scroll to positions half the visible width
371 * left/right of the start/end positions
375 int offset = (av.getEndRes() - av.getStartRes() + 1) / 2 - 1;
376 start = Math.max(start - offset, 0);
377 end = end + offset - 1;
383 if (end == seq.getEnd())
387 if (av.hasHiddenColumns())
389 start = av.getColumnSelection().findColumnPosition(start);
390 end = av.getColumnSelection().findColumnPosition(end);
393 if (!av.getColumnSelection().isVisible(r[0]))
395 // don't scroll - position isn't visible
402 * allow for offset of target sequence (actually scroll to one above it)
404 seqIndex = Math.max(0, seqIndex - verticalOffset);
406 // System.out.println("start=" + start + ", end=" + end + ", startv="
407 // + av.getStartRes() + ", endv=" + av.getEndRes() + ", starts="
408 // + av.getStartSeq() + ", ends=" + av.getEndSeq());
409 if (!av.getWrapAlignment())
411 if ((startv = av.getStartRes()) >= start)
414 * Scroll left to make start of search results visible
416 // setScrollValues(start - 1, seqIndex); // plus one residue
417 setScrollValues(start, seqIndex);
419 else if ((endv = av.getEndRes()) <= end)
422 * Scroll right to make end of search results visible
424 // setScrollValues(startv + 1 + end - endv, seqIndex); // plus one
425 setScrollValues(startv + end - endv, seqIndex);
427 else if ((starts = av.getStartSeq()) > seqIndex)
430 * Scroll up to make start of search results visible
432 setScrollValues(av.getStartRes(), seqIndex);
434 else if ((ends = av.getEndSeq()) <= seqIndex)
437 * Scroll down to make end of search results visible
439 setScrollValues(av.getStartRes(), starts + seqIndex - ends + 1);
442 * Else results are already visible - no need to scroll
447 scrollToWrappedVisible(start);
450 if (redrawOverview && overviewPanel != null)
452 overviewPanel.setBoxPosition();
454 paintAlignment(redrawOverview);
458 void scrollToWrappedVisible(int res)
460 int cwidth = getSeqPanel().seqCanvas
461 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
462 if (res < av.getStartRes() || res >= (av.getStartRes() + cwidth))
464 vscroll.setValue((res / cwidth));
465 av.startRes = vscroll.getValue() * cwidth;
473 * @return DOCUMENT ME!
475 public OverviewPanel getOverviewPanel()
477 return overviewPanel;
486 public void setOverviewPanel(OverviewPanel op)
494 * Hide or show annotation panel
497 public void setAnnotationVisible(boolean b)
499 if (!av.getWrapAlignment())
501 annotationSpaceFillerHolder.setVisible(b);
502 annotationScroller.setVisible(b);
508 * automatically adjust annotation panel height for new annotation whilst
509 * ensuring the alignment is still visible.
511 public void adjustAnnotationHeight()
513 // TODO: display vertical annotation scrollbar if necessary
514 // this is called after loading new annotation onto alignment
515 if (alignFrame.getHeight() == 0)
517 System.out.println("NEEDS FIXING");
519 validateAnnotationDimensions(true);
521 paintAlignment(true);
525 * calculate the annotation dimensions and refresh slider values accordingly.
526 * need to do repaints/notifys afterwards.
528 protected void validateAnnotationDimensions(boolean adjustPanelHeight)
530 int annotationHeight = getAnnotationPanel().adjustPanelHeight();
532 if (adjustPanelHeight)
534 int rowHeight = av.getCharHeight();
535 int alignmentHeight = rowHeight * av.getAlignment().getHeight();
538 * Estimate available height in the AlignFrame for alignment +
539 * annotations. Deduct an estimate for title bar, menu bar, scale panel,
540 * hscroll, status bar (as these are not laid out we can't inspect their
541 * actual heights). Insets gives frame borders.
543 int stuff = Platform.isAMac() ? 80 : 100;
544 Insets insets = alignFrame.getInsets();
545 int availableHeight = alignFrame.getHeight() - stuff - insets.top
549 * If not enough vertical space, maximize annotation height while keeping
550 * at least two rows of alignment visible
552 if (annotationHeight + alignmentHeight > availableHeight)
554 annotationHeight = Math.min(annotationHeight, availableHeight - 2
560 // maintain same window layout whilst updating sliders
561 annotationHeight = annotationScroller.getSize().height;
565 annotationScroller.setPreferredSize(new Dimension(annotationScroller
566 .getWidth(), annotationHeight));
568 annotationSpaceFillerHolder.setPreferredSize(new Dimension(
569 annotationSpaceFillerHolder.getWidth(), annotationHeight));
570 annotationScroller.validate();
571 annotationScroller.addNotify();
575 * update alignment layout for viewport settings
580 public void updateLayout()
583 setAnnotationVisible(av.isShowAnnotation());
584 boolean wrap = av.getWrapAlignment();
586 scalePanelHolder.setVisible(!wrap);
587 hscroll.setVisible(!wrap);
588 idwidthAdjuster.setVisible(!wrap);
592 annotationScroller.setVisible(false);
593 annotationSpaceFillerHolder.setVisible(false);
595 else if (av.isShowAnnotation())
597 annotationScroller.setVisible(true);
598 annotationSpaceFillerHolder.setVisible(true);
601 idSpaceFillerPanel1.setVisible(!wrap);
606 // return value is true if the scroll is valid
607 public boolean scrollUp(boolean up)
611 if (vscroll.getValue() < 1)
617 vscroll.setValue(vscroll.getValue() - 1);
621 if ((vextent + vscroll.getValue()) >= av.getAlignment().getHeight())
627 vscroll.setValue(vscroll.getValue() + 1);
641 * @return DOCUMENT ME!
643 public boolean scrollRight(boolean right)
647 if (hscroll.getValue() < 1)
653 hscroll.setValue(hscroll.getValue() - 1);
657 if ((hextent + hscroll.getValue()) >= av.getAlignment().getWidth())
663 hscroll.setValue(hscroll.getValue() + 1);
672 * Adjust row/column scrollers to show a visible position in the alignment.
675 * visible column to scroll to
677 * visible row to scroll to
680 public void setScrollValues(int x, int y)
682 // System.err.println("Scroll " + this.av.viewName + " to " + x + "," + y);
683 if (av == null || av.getAlignment() == null)
687 int width = av.getAlignment().getWidth();
688 int height = av.getAlignment().getHeight();
690 if (av.hasHiddenColumns())
692 width = av.getColumnSelection().findColumnPosition(width);
695 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av
696 .getCharWidth())) - 1);
698 hextent = getSeqPanel().seqCanvas.getWidth() / av.getCharWidth();
699 vextent = getSeqPanel().seqCanvas.getHeight() / av.getCharHeight();
706 if (vextent > height)
711 if ((hextent + x) > width)
716 if ((vextent + y) > height)
718 y = height - vextent;
731 hscroll.setValues(x, hextent, 0, width);
732 vscroll.setValues(y, vextent, 0, height);
741 public void adjustmentValueChanged(AdjustmentEvent evt)
743 int oldX = av.getStartRes();
744 int oldY = av.getStartSeq();
746 if (evt.getSource() == hscroll)
748 int x = hscroll.getValue();
750 av.setEndRes((x + (getSeqPanel().seqCanvas.getWidth() / av.getCharWidth())) - 1);
753 if (evt.getSource() == vscroll)
755 int offy = vscroll.getValue();
757 if (av.getWrapAlignment())
761 int rowSize = getSeqPanel().seqCanvas
762 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
763 av.setStartRes(offy * rowSize);
764 av.setEndRes((offy + 1) * rowSize);
768 // This is only called if file loaded is a jar file that
769 // was wrapped when saved and user has wrap alignment true
770 // as preference setting
771 SwingUtilities.invokeLater(new Runnable()
775 setScrollValues(av.getStartRes(), av.getStartSeq());
782 av.setStartSeq(offy);
784 + (getSeqPanel().seqCanvas.getHeight() / av.getCharHeight()));
788 if (overviewPanel != null)
790 overviewPanel.setBoxPosition();
793 int scrollX = av.startRes - oldX;
794 int scrollY = av.startSeq - oldY;
796 if (av.getWrapAlignment() || !fastPaint)
802 // Make sure we're not trying to draw a panel
803 // larger than the visible window
804 if (scrollX > av.endRes - av.startRes)
806 scrollX = av.endRes - av.startRes;
808 else if (scrollX < av.startRes - av.endRes)
810 scrollX = av.startRes - av.endRes;
813 if (scrollX != 0 || scrollY != 0)
815 getIdPanel().getIdCanvas().fastPaint(scrollY);
816 getSeqPanel().seqCanvas.fastPaint(scrollX, scrollY);
817 getScalePanel().repaint();
819 if (av.isShowAnnotation() && scrollX != 0)
821 getAnnotationPanel().fastPaint(scrollX);
826 * If there is one, scroll the (Protein/cDNA) complementary alignment to
827 * match, unless we are ourselves doing that.
829 if (isFollowingComplementScroll())
831 setFollowingComplementScroll(false);
835 av.scrollComplementaryAlignment();
840 * Repaint the alignment including the annotations and overview panels (if
843 public void paintAlignment(boolean updateOverview)
845 final AnnotationSorter sorter = new AnnotationSorter(getAlignment(),
846 av.isShowAutocalculatedAbove());
847 sorter.sort(getAlignment()
848 .getAlignmentAnnotation(),
849 av.getSortAnnotationsBy());
854 av.getStructureSelectionManager().sequenceColoursChanged(this);
856 if (overviewPanel != null)
858 overviewPanel.updateOverviewImage();
869 public void paintComponent(Graphics g)
873 Dimension d = getIdPanel().getIdCanvas().getPreferredSize();
874 idPanelHolder.setPreferredSize(d);
875 hscrollFillerPanel.setPreferredSize(new Dimension(d.width, 12));
878 if (av.getWrapAlignment())
880 int maxwidth = av.getAlignment().getWidth();
882 if (av.hasHiddenColumns())
884 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
887 int canvasWidth = getSeqPanel().seqCanvas
888 .getWrappedCanvasWidth(getSeqPanel().seqCanvas.getWidth());
892 / getSeqPanel().seqCanvas
893 .getWrappedCanvasWidth(getSeqPanel().seqCanvas
895 vscroll.setMaximum(max);
896 vscroll.setUnitIncrement(1);
897 vscroll.setVisibleAmount(1);
902 setScrollValues(av.getStartRes(), av.getStartSeq());
916 * @return DOCUMENT ME!
918 * @throws PrinterException
921 public int print(Graphics pg, PageFormat pf, int pi)
922 throws PrinterException
924 pg.translate((int) pf.getImageableX(), (int) pf.getImageableY());
926 int pwidth = (int) pf.getImageableWidth();
927 int pheight = (int) pf.getImageableHeight();
929 if (av.getWrapAlignment())
931 return printWrappedAlignment(pg, pwidth, pheight, pi);
935 return printUnwrapped(pg, pwidth, pheight, pi);
951 * @return DOCUMENT ME!
953 * @throws PrinterException
956 public int printUnwrapped(Graphics pg, int pwidth, int pheight, int pi)
957 throws PrinterException
959 int idWidth = getVisibleIdWidth(false);
960 FontMetrics fm = getFontMetrics(av.getFont());
961 int scaleHeight = av.getCharHeight() + fm.getDescent();
963 pg.setColor(Color.white);
964 pg.fillRect(0, 0, pwidth, pheight);
965 pg.setFont(av.getFont());
967 // //////////////////////////////////
968 // / How many sequences and residues can we fit on a printable page?
969 int totalRes = (pwidth - idWidth) / av.getCharWidth();
971 int totalSeq = (pheight - scaleHeight) / av.getCharHeight() - 1;
973 int pagesWide = (av.getAlignment().getWidth() / totalRes) + 1;
975 // ///////////////////////////
976 // / Only print these sequences and residues on this page
979 // ///////////////////////////
980 // / Only print these sequences and residues on this page
983 // ///////////////////////////
984 // / Only print these sequences and residues on this page
987 // ///////////////////////////
988 // / Only print these sequences and residues on this page
990 startRes = (pi % pagesWide) * totalRes;
991 endRes = (startRes + totalRes) - 1;
993 if (endRes > (av.getAlignment().getWidth() - 1))
995 endRes = av.getAlignment().getWidth() - 1;
998 startSeq = (pi / pagesWide) * totalSeq;
999 endSeq = startSeq + totalSeq;
1001 if (endSeq > av.getAlignment().getHeight())
1003 endSeq = av.getAlignment().getHeight();
1006 int pagesHigh = ((av.getAlignment().getHeight() / totalSeq) + 1)
1009 if (av.isShowAnnotation())
1011 pagesHigh += getAnnotationPanel().adjustPanelHeight() + 3;
1014 pagesHigh /= pheight;
1016 if (pi >= (pagesWide * pagesHigh))
1018 return Printable.NO_SUCH_PAGE;
1022 pg.translate(idWidth, 0);
1023 getScalePanel().drawScale(pg, startRes, endRes, pwidth - idWidth,
1025 pg.translate(-idWidth, scaleHeight);
1029 Color currentColor = null;
1030 Color currentTextColor = null;
1032 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1035 for (int i = startSeq; i < endSeq; i++)
1037 seq = av.getAlignment().getSequenceAt(i);
1038 if ((av.getSelectionGroup() != null)
1039 && av.getSelectionGroup().getSequences(null).contains(seq))
1041 currentColor = Color.gray;
1042 currentTextColor = Color.black;
1046 currentColor = av.getSequenceColour(seq);
1047 currentTextColor = Color.black;
1050 pg.setColor(currentColor);
1051 pg.fillRect(0, (i - startSeq) * av.getCharHeight(), idWidth,
1052 av.getCharHeight());
1054 pg.setColor(currentTextColor);
1057 if (av.isRightAlignIds())
1059 fm = pg.getFontMetrics();
1061 - fm.stringWidth(seq.getDisplayId(av.getShowJVSuffix()))
1066 seq.getDisplayId(av.getShowJVSuffix()),
1068 (((i - startSeq) * av.getCharHeight()) + av.getCharHeight())
1069 - (av.getCharHeight() / 5));
1072 pg.setFont(av.getFont());
1074 // draw main sequence panel
1075 pg.translate(idWidth, 0);
1076 getSeqPanel().seqCanvas.drawPanel(pg, startRes, endRes, startSeq, endSeq, 0);
1078 if (av.isShowAnnotation() && (endSeq == av.getAlignment().getHeight()))
1080 // draw annotation - need to offset for current scroll position
1081 int offset = -getAlabels().getScrollOffset();
1082 pg.translate(0, offset);
1083 pg.translate(-idWidth - 3, (endSeq - startSeq) * av.getCharHeight()
1085 getAlabels().drawComponent(pg, idWidth);
1086 pg.translate(idWidth + 3, 0);
1087 getAnnotationPanel().renderer.drawComponent(getAnnotationPanel(), av,
1088 pg, -1, startRes, endRes + 1);
1089 pg.translate(0, -offset);
1092 return Printable.PAGE_EXISTS;
1107 * @return DOCUMENT ME!
1109 * @throws PrinterException
1112 public int printWrappedAlignment(Graphics pg, int pwidth, int pheight,
1113 int pi) throws PrinterException
1115 int annotationHeight = 0;
1116 AnnotationLabels labels = null;
1117 if (av.isShowAnnotation())
1119 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1120 labels = new AnnotationLabels(av);
1123 int hgap = av.getCharHeight();
1124 if (av.getScaleAboveWrapped())
1126 hgap += av.getCharHeight();
1129 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1132 int idWidth = getVisibleIdWidth(false);
1134 int maxwidth = av.getAlignment().getWidth();
1135 if (av.hasHiddenColumns())
1137 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1140 int resWidth = getSeqPanel().seqCanvas.getWrappedCanvasWidth(pwidth
1143 int totalHeight = cHeight * (maxwidth / resWidth + 1);
1145 pg.setColor(Color.white);
1146 pg.fillRect(0, 0, pwidth, pheight);
1147 pg.setFont(av.getFont());
1151 pg.setColor(Color.black);
1153 pg.translate(0, -pi * pheight);
1155 pg.setClip(0, pi * pheight, pwidth, pheight);
1161 for (int i = 0; i < av.getAlignment().getHeight(); i++)
1163 pg.setFont(getIdPanel().getIdCanvas().getIdfont());
1164 SequenceI s = av.getAlignment().getSequenceAt(i);
1165 String string = s.getDisplayId(av.getShowJVSuffix());
1167 if (av.isRightAlignIds())
1169 FontMetrics fm = pg.getFontMetrics();
1170 xPos = idWidth - fm.stringWidth(string) - 4;
1172 pg.drawString(string, xPos,
1173 ((i * av.getCharHeight()) + ypos + av.getCharHeight())
1174 - (av.getCharHeight() / 5));
1178 pg.translate(-3, ypos
1179 + (av.getAlignment().getHeight() * av.getCharHeight()));
1181 pg.setFont(av.getFont());
1182 labels.drawComponent(pg, idWidth);
1183 pg.translate(+3, -ypos
1184 - (av.getAlignment().getHeight() * av
1189 } while (ypos < totalHeight);
1191 pg.translate(idWidth, 0);
1193 getSeqPanel().seqCanvas.drawWrappedPanel(pg, pwidth - idWidth, totalHeight,
1196 if ((pi * pheight) < totalHeight)
1198 return Printable.PAGE_EXISTS;
1203 return Printable.NO_SUCH_PAGE;
1208 * get current sequence ID panel width, or nominal value if panel were to be
1209 * displayed using default settings
1213 public int getVisibleIdWidth()
1215 return getVisibleIdWidth(true);
1219 * get current sequence ID panel width, or nominal value if panel were to be
1220 * displayed using default settings
1223 * indicate if the Id width for onscreen or offscreen display should
1227 public int getVisibleIdWidth(boolean onscreen)
1229 // see if rendering offscreen - check preferences and calc width accordingly
1230 if (!onscreen && Cache.getDefault("FIGURE_AUTOIDWIDTH", false))
1232 return calculateIdWidth(-1).width + 4;
1234 Integer idwidth = null;
1236 || (idwidth = Cache.getIntegerProperty("FIGURE_FIXEDIDWIDTH")) == null)
1238 return (getIdPanel().getWidth() > 0 ? getIdPanel().getWidth()
1239 : calculateIdWidth().width + 4);
1241 return idwidth.intValue() + 4;
1244 void makeAlignmentImage(jalview.util.ImageMaker.TYPE type, File file)
1246 long progress = System.currentTimeMillis();
1247 headless = (System.getProperty("java.awt.headless") != null && System
1248 .getProperty("java.awt.headless").equals("true"));
1249 if (alignFrame != null && !headless)
1251 alignFrame.setProgressBar(MessageManager.formatMessage(
1252 "status.saving_file", new Object[]
1253 { type.getLabel() }), progress);
1257 AlignmentDimension aDimension = getAlignmentDimension();
1260 jalview.util.ImageMaker im;
1261 final String imageAction, imageTitle;
1262 if (type == jalview.util.ImageMaker.TYPE.PNG)
1264 imageAction = "Create PNG image from alignment";
1267 else if (type == jalview.util.ImageMaker.TYPE.EPS)
1269 imageAction = "Create EPS file from alignment";
1270 imageTitle = alignFrame.getTitle();
1274 imageAction = "Create SVG file from alignment";
1275 imageTitle = alignFrame.getTitle();
1278 im = new jalview.util.ImageMaker(this, type, imageAction,
1279 aDimension.getWidth(), aDimension.getHeight(), file,
1281 if (av.getWrapAlignment())
1283 if (im.getGraphics() != null)
1285 printWrappedAlignment(im.getGraphics(), aDimension.getWidth(),
1286 aDimension.getHeight(), 0);
1292 if (im.getGraphics() != null)
1294 printUnwrapped(im.getGraphics(), aDimension.getWidth(),
1295 aDimension.getHeight(), 0);
1299 } catch (OutOfMemoryError err)
1302 System.out.println("########################\n" + "OUT OF MEMORY "
1303 + file + "\n" + "########################");
1304 new OOMWarning("Creating Image for " + file, err);
1305 // System.out.println("Create IMAGE: " + err);
1306 } catch (Exception ex)
1308 ex.printStackTrace();
1312 if (alignFrame != null && !headless)
1314 alignFrame.setProgressBar(MessageManager.getString("status.export_complete"), progress);
1319 public AlignmentDimension getAlignmentDimension()
1321 int maxwidth = av.getAlignment().getWidth();
1322 if (av.hasHiddenColumns())
1324 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth);
1327 int height = ((av.getAlignment().getHeight() + 1) * av.getCharHeight())
1328 + getScalePanel().getHeight();
1329 int width = getVisibleIdWidth(false) + (maxwidth * av.getCharWidth());
1331 if (av.getWrapAlignment())
1333 height = getWrappedHeight();
1336 // need to obtain default alignment width and then add in any
1337 // additional allowance for id margin
1338 // this duplicates the calculation in getWrappedHeight but adjusts for
1340 width = alignFrame.getWidth() - vscroll.getPreferredSize().width
1341 - alignFrame.getInsets().left
1342 - alignFrame.getInsets().right - getVisibleIdWidth()
1343 + getVisibleIdWidth(false);
1347 width = getSeqPanel().getWidth() + getVisibleIdWidth(false);
1351 else if (av.isShowAnnotation())
1353 height += getAnnotationPanel().adjustPanelHeight() + 3;
1355 return new AlignmentDimension(width, height);
1362 public void makeEPS(File epsFile)
1364 makeAlignmentImage(jalview.util.ImageMaker.TYPE.EPS, epsFile);
1370 public void makePNG(File pngFile)
1372 makeAlignmentImage(jalview.util.ImageMaker.TYPE.PNG, pngFile);
1375 public void makeSVG(File svgFile)
1377 makeAlignmentImage(jalview.util.ImageMaker.TYPE.SVG, svgFile);
1379 public void makePNGImageMap(File imgMapFile, String imageName)
1381 // /////ONLY WORKS WITH NONE WRAPPED ALIGNMENTS
1382 // ////////////////////////////////////////////
1383 int idWidth = getVisibleIdWidth(false);
1384 FontMetrics fm = getFontMetrics(av.getFont());
1385 int scaleHeight = av.getCharHeight() + fm.getDescent();
1388 // ////////////////////////////////
1389 if (imgMapFile != null)
1393 int s, sSize = av.getAlignment().getHeight(), res, alwidth = av
1394 .getAlignment().getWidth(), g, gSize, f, fSize, sy;
1395 StringBuffer text = new StringBuffer();
1396 PrintWriter out = new PrintWriter(new FileWriter(imgMapFile));
1397 out.println(jalview.io.HTMLOutput.getImageMapHTML());
1398 out.println("<img src=\"" + imageName
1399 + "\" border=\"0\" usemap=\"#Map\" >"
1400 + "<map name=\"Map\">");
1402 for (s = 0; s < sSize; s++)
1404 sy = s * av.getCharHeight() + scaleHeight;
1406 SequenceI seq = av.getAlignment().getSequenceAt(s);
1407 SequenceFeature[] features = seq.getSequenceFeatures();
1408 SequenceGroup[] groups = av.getAlignment().findAllGroups(seq);
1409 for (res = 0; res < alwidth; res++)
1411 text = new StringBuffer();
1412 String triplet = null;
1413 if (av.getAlignment().isNucleotide())
1415 triplet = ResidueProperties.nucleotideName.get(seq
1421 triplet = ResidueProperties.aa2Triplet.get(seq.getCharAt(res)
1425 if (triplet == null)
1430 int alIndex = seq.findPosition(res);
1431 gSize = groups.length;
1432 for (g = 0; g < gSize; g++)
1434 if (text.length() < 1)
1436 text.append("<area shape=\"rect\" coords=\""
1437 + (idWidth + res * av.getCharWidth()) + "," + sy
1438 + "," + (idWidth + (res + 1) * av.getCharWidth())
1440 + (av.getCharHeight() + sy) + "\""
1441 + " onMouseOver=\"toolTip('" + alIndex + " "
1445 if (groups[g].getStartRes() < res
1446 && groups[g].getEndRes() > res)
1448 text.append("<br><em>" + groups[g].getName() + "</em>");
1452 if (features != null)
1454 if (text.length() < 1)
1456 text.append("<area shape=\"rect\" coords=\""
1457 + (idWidth + res * av.getCharWidth()) + "," + sy
1458 + "," + (idWidth + (res + 1) * av.getCharWidth())
1460 + (av.getCharHeight() + sy) + "\""
1461 + " onMouseOver=\"toolTip('" + alIndex + " "
1464 fSize = features.length;
1465 for (f = 0; f < fSize; f++)
1468 if ((features[f].getBegin() <= seq.findPosition(res))
1469 && (features[f].getEnd() >= seq.findPosition(res)))
1471 if (features[f].getType().equals("disulfide bond"))
1473 if (features[f].getBegin() == seq.findPosition(res)
1474 || features[f].getEnd() == seq
1477 text.append("<br>disulfide bond "
1478 + features[f].getBegin() + ":"
1479 + features[f].getEnd());
1484 text.append("<br>");
1485 text.append(features[f].getType());
1486 if (features[f].getDescription() != null
1487 && !features[f].getType().equals(
1488 features[f].getDescription()))
1490 text.append(" " + features[f].getDescription());
1493 if (features[f].getValue("status") != null)
1495 text.append(" (" + features[f].getValue("status")
1503 if (text.length() > 1)
1505 text.append("')\"; onMouseOut=\"toolTip()\"; href=\"#\">");
1506 out.println(text.toString());
1510 out.println("</map></body></html>");
1513 } catch (Exception ex)
1515 ex.printStackTrace();
1517 } // /////////END OF IMAGE MAP
1521 int getWrappedHeight()
1523 int seqPanelWidth = getSeqPanel().seqCanvas.getWidth();
1525 if (System.getProperty("java.awt.headless") != null
1526 && System.getProperty("java.awt.headless").equals("true"))
1528 seqPanelWidth = alignFrame.getWidth() - getVisibleIdWidth()
1529 - vscroll.getPreferredSize().width
1530 - alignFrame.getInsets().left - alignFrame.getInsets().right;
1533 int chunkWidth = getSeqPanel().seqCanvas
1534 .getWrappedCanvasWidth(seqPanelWidth);
1536 int hgap = av.getCharHeight();
1537 if (av.getScaleAboveWrapped())
1539 hgap += av.getCharHeight();
1542 int annotationHeight = 0;
1543 if (av.isShowAnnotation())
1545 annotationHeight = getAnnotationPanel().adjustPanelHeight();
1548 int cHeight = av.getAlignment().getHeight() * av.getCharHeight() + hgap
1551 int maxwidth = av.getAlignment().getWidth();
1552 if (av.hasHiddenColumns())
1554 maxwidth = av.getColumnSelection().findColumnPosition(maxwidth) - 1;
1557 int height = ((maxwidth / chunkWidth) + 1) * cHeight;
1563 * close the panel - deregisters all listeners and nulls any references to
1566 public void closePanel()
1568 PaintRefresher.RemoveComponent(getSeqPanel().seqCanvas);
1569 PaintRefresher.RemoveComponent(getIdPanel().getIdCanvas());
1570 PaintRefresher.RemoveComponent(this);
1573 jalview.structure.StructureSelectionManager ssm = av
1574 .getStructureSelectionManager();
1575 ssm.removeStructureViewerListener(getSeqPanel(), null);
1576 ssm.removeSelectionListener(getSeqPanel());
1577 ssm.removeCommandListener(av);
1578 ssm.removeStructureViewerListener(getSeqPanel(), null);
1579 ssm.removeSelectionListener(getSeqPanel());
1580 av.setAlignment(null);
1585 if (Cache.log.isDebugEnabled())
1587 Cache.log.warn("Closing alignment panel which is already closed.");
1593 * hides or shows dynamic annotation rows based on groups and av state flags
1595 public void updateAnnotation()
1597 updateAnnotation(false, false);
1600 public void updateAnnotation(boolean applyGlobalSettings)
1602 updateAnnotation(applyGlobalSettings, false);
1605 public void updateAnnotation(boolean applyGlobalSettings,
1606 boolean preserveNewGroupSettings)
1608 av.updateGroupAnnotationSettings(applyGlobalSettings,
1609 preserveNewGroupSettings);
1610 adjustAnnotationHeight();
1614 public AlignmentI getAlignment()
1616 return av.getAlignment();
1621 public String getViewName()
1627 * Make/Unmake this alignment panel the current input focus
1631 public void setSelected(boolean b)
1635 if (alignFrame.getSplitViewContainer() != null)
1638 * bring enclosing SplitFrame to front first if there is one
1640 ((SplitFrame) alignFrame.getSplitViewContainer()).setSelected(b);
1642 alignFrame.setSelected(b);
1643 } catch (Exception ex)
1649 alignFrame.setDisplayedView(this);
1654 public StructureSelectionManager getStructureSelectionManager()
1656 return av.getStructureSelectionManager();
1660 public void raiseOOMWarning(String string, OutOfMemoryError error)
1662 new OOMWarning(string, error, this);
1666 public jalview.api.FeatureRenderer cloneFeatureRenderer()
1669 return new FeatureRenderer(this);
1672 public jalview.api.FeatureRenderer getFeatureRenderer()
1674 return seqPanel.seqCanvas.getFeatureRenderer();
1676 public void updateFeatureRenderer(jalview.renderer.seqfeatures.FeatureRenderer fr)
1678 fr.transferSettings(getSeqPanel().seqCanvas.getFeatureRenderer());
1681 public void updateFeatureRendererFrom(jalview.api.FeatureRenderer fr)
1683 if (getSeqPanel().seqCanvas.getFeatureRenderer() != null)
1685 getSeqPanel().seqCanvas.getFeatureRenderer().transferSettings(fr);
1689 public ScalePanel getScalePanel()
1694 public void setScalePanel(ScalePanel scalePanel)
1696 this.scalePanel = scalePanel;
1699 public SeqPanel getSeqPanel()
1704 public void setSeqPanel(SeqPanel seqPanel)
1706 this.seqPanel = seqPanel;
1709 public AnnotationPanel getAnnotationPanel()
1711 return annotationPanel;
1714 public void setAnnotationPanel(AnnotationPanel annotationPanel)
1716 this.annotationPanel = annotationPanel;
1719 public AnnotationLabels getAlabels()
1724 public void setAlabels(AnnotationLabels alabels)
1726 this.alabels = alabels;
1729 public IdPanel getIdPanel()
1734 public void setIdPanel(IdPanel idPanel)
1736 this.idPanel = idPanel;
1740 * Follow a scrolling change in the (cDNA/Protein) complementary alignment.
1741 * The aim is to keep the two alignments 'lined up' on their centre columns.
1744 * holds mapped region(s) of this alignment that we are scrolling
1745 * 'to'; may be modified for sequence offset by this method
1746 * @param verticalOffset
1747 * the number of visible sequences to show above the mapped region
1749 public void scrollToCentre(SearchResults sr, int verticalOffset)
1752 * To avoid jumpy vertical scrolling (if some sequences are gapped or not
1753 * mapped), we can make the scroll-to location a sequence above the one
1756 SequenceI mappedTo = sr.getResultSequence(0);
1757 List<SequenceI> seqs = av.getAlignment().getSequences();
1760 * This is like AlignmentI.findIndex(seq) but here we are matching the
1761 * dataset sequence not the aligned sequence
1763 boolean matched = false;
1764 for (SequenceI seq : seqs)
1766 if (mappedTo == seq.getDatasetSequence())
1774 return; // failsafe, shouldn't happen
1778 * Scroll to position but centring the target residue.
1780 scrollToPosition(sr, verticalOffset, true, true);
1784 * Set a flag to say we are scrolling to follow a (cDNA/protein) complement.
1788 protected void setFollowingComplementScroll(boolean b)
1790 this.followingComplementScroll = b;
1793 protected boolean isFollowingComplementScroll()
1795 return this.followingComplementScroll;