2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, G Barton, M Clamp, S Searle
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 of the License, or (at your option) any later version.
11 * Jalview is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with Jalview. If not, see <http://www.gnu.org/licenses/>.
21 import java.util.regex.Pattern;
24 import java.awt.datatransfer.*;
25 import java.awt.event.*;
26 import java.awt.geom.AffineTransform;
27 import java.awt.image.*;
30 import jalview.datamodel.*;
39 public class AnnotationLabels extends JPanel implements MouseListener,
40 MouseMotionListener, ActionListener
42 static String TOGGLE_LABELSCALE = "Scale Label to Column";
44 static String ADDNEW = "Add New Row";
46 static String EDITNAME = "Edit Label/Description";
48 static String HIDE = "Hide This Row";
50 static String DELETE = "Delete This Row";
52 static String SHOWALL = "Show All Hidden Rows";
54 static String OUTPUT_TEXT = "Export Annotation";
56 static String COPYCONS_SEQ = "Copy Consensus Sequence";
58 boolean resizePanel = false;
66 boolean resizing = false;
76 Font font = new Font("Arial", Font.PLAIN, 11);
78 private boolean hasHiddenRows;
81 * Creates a new AnnotationLabels object.
86 public AnnotationLabels(AlignmentPanel ap)
90 ToolTipManager.sharedInstance().registerComponent(this);
92 java.net.URL url = getClass().getResource("/images/idwidth.gif");
97 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
102 MediaTracker mt = new MediaTracker(this);
103 mt.addImage(temp, 0);
105 } catch (Exception ex)
109 BufferedImage bi = new BufferedImage(temp.getHeight(this),
110 temp.getWidth(this), BufferedImage.TYPE_INT_RGB);
111 Graphics2D g = (Graphics2D) bi.getGraphics();
112 g.rotate(Math.toRadians(90));
113 g.drawImage(temp, 0, -bi.getWidth(this), this);
116 addMouseListener(this);
117 addMouseMotionListener(this);
120 public AnnotationLabels(AlignViewport av)
131 public void setScrollOffset(int y)
138 * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
142 * coordinate position to search for a row
144 void getSelectedRow(int y)
147 AlignmentAnnotation[] aa = ap.av.getAlignment().getAlignmentAnnotation();
151 for (int i = 0; i < aa.length; i++)
159 height += aa[i].height;
177 public void actionPerformed(ActionEvent evt)
179 AlignmentAnnotation[] aa = ap.av.getAlignment().getAlignmentAnnotation();
181 if (evt.getActionCommand().equals(ADDNEW))
183 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
184 null, new Annotation[ap.av.getAlignment().getWidth()]);
186 if (!editLabelDescription(newAnnotation))
191 ap.av.getAlignment().addAnnotation(newAnnotation);
192 ap.av.getAlignment().setAnnotationIndex(newAnnotation, 0);
194 else if (evt.getActionCommand().equals(EDITNAME))
196 editLabelDescription(aa[selectedRow]);
199 else if (evt.getActionCommand().equals(HIDE))
201 aa[selectedRow].visible = false;
203 else if (evt.getActionCommand().equals(DELETE))
205 ap.av.getAlignment().deleteAnnotation(aa[selectedRow]);
207 else if (evt.getActionCommand().equals(SHOWALL))
209 for (int i = 0; i < aa.length; i++)
211 if (!aa[i].visible && aa[i].annotations != null)
213 aa[i].visible = true;
217 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
219 new AnnotationExporter().exportAnnotations(ap,
220 new AlignmentAnnotation[]
221 { aa[selectedRow] }, null, null);
223 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
225 SequenceI cons = null;
226 if (aa[selectedRow].groupRef != null)
228 cons = aa[selectedRow].groupRef.getConsensusSeq();
232 cons = av.getConsensusSeq();
236 copy_annotseqtoclipboard(cons);
240 else if (evt.getActionCommand().equals(TOGGLE_LABELSCALE))
242 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
245 ap.validateAnnotationDimensions(false);
249 //ap.paintAlignment(true);
258 boolean editLabelDescription(AlignmentAnnotation annotation)
260 EditNameDialog dialog = new EditNameDialog(annotation.label,
261 annotation.description, " Annotation Name ",
262 "Annotation Description ", "Edit Annotation Name/Description",
270 annotation.label = dialog.getName();
272 String text = dialog.getDescription();
273 if (text != null && text.length() == 0)
277 annotation.description = text;
288 public void mousePressed(MouseEvent evt)
290 getSelectedRow(evt.getY() - scrollOffset);
300 public void mouseReleased(MouseEvent evt)
302 int start = selectedRow;
303 getSelectedRow(evt.getY() - scrollOffset);
304 int end = selectedRow;
308 // Swap these annotations
309 AlignmentAnnotation startAA = ap.av.getAlignment()
310 .getAlignmentAnnotation()[start];
313 end = ap.av.getAlignment().getAlignmentAnnotation().length - 1;
315 AlignmentAnnotation endAA = ap.av.getAlignment().getAlignmentAnnotation()[end];
317 ap.av.getAlignment().getAlignmentAnnotation()[end] = startAA;
318 ap.av.getAlignment().getAlignmentAnnotation()[start] = endAA;
324 ap.annotationPanel.repaint();
333 public void mouseEntered(MouseEvent evt)
348 public void mouseExited(MouseEvent evt)
350 if (dragEvent == null)
363 public void mouseDragged(MouseEvent evt)
369 Dimension d = ap.annotationScroller.getPreferredSize();
370 int dif = evt.getY() - oldY;
372 dif /= ap.av.charHeight;
373 dif *= ap.av.charHeight;
375 if ((d.height - dif) > 20)
377 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
379 d = ap.annotationSpaceFillerHolder.getPreferredSize();
380 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
381 d.width, d.height - dif));
382 ap.paintAlignment(true);
399 public void mouseMoved(MouseEvent evt)
401 resizePanel = evt.getY() < 10;
403 getSelectedRow(evt.getY() - scrollOffset);
406 && ap.av.getAlignment().getAlignmentAnnotation().length > selectedRow)
408 AlignmentAnnotation aa = ap.av.getAlignment().getAlignmentAnnotation()[selectedRow];
410 StringBuffer desc = new StringBuffer();
411 if (aa.description != null
412 && !aa.description.equals("New description"))
414 // TODO: we could refactor and merge this code with the code in jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature tooltips
415 desc.append(aa.getDescription(true).trim());
416 // check to see if the description is an html fragment.
417 if (desc.length()<6 || (desc.substring(0,6).toLowerCase().indexOf("<html>")<0))
419 // clean the description ready for embedding in html
420 desc = new StringBuffer(Pattern.compile("<").matcher(desc).replaceAll("<"));
421 desc.insert(0, "<html>");
423 // remove terminating html if any
424 int i=desc.substring(desc.length()-7).toLowerCase().lastIndexOf("</html>");
426 desc.setLength(desc.length()-7+i);
431 desc.append("<br/>");
436 // begin the tooltip's html fragment
437 desc.append("<html>");
441 // TODO: limit precision of score to avoid noise from imprecise doubles (64.7 becomes 64.7+/some tiny value).
442 desc.append(" Score: " + aa.score);
445 if (desc.length() > 6)
447 desc.append("</html>");
448 this.setToolTipText(desc.toString());
451 this.setToolTipText(null);
462 public void mouseClicked(MouseEvent evt)
464 AlignmentAnnotation[] aa = ap.av.getAlignment().getAlignmentAnnotation();
465 if (SwingUtilities.isLeftMouseButton(evt))
467 if (selectedRow > -1 && selectedRow < aa.length)
469 if (aa[selectedRow].groupRef != null)
471 if (evt.getClickCount() >= 2)
473 // todo: make the ap scroll to the selection - not necessary, first click highlights/scrolls, second selects
474 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
475 ap.av.setSelectionGroup(// new SequenceGroup(
476 aa[selectedRow].groupRef); // );
477 ap.paintAlignment(false);
478 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
479 ap.av.sendSelection();
483 ap.seqPanel.ap.idPanel
484 .highlightSearchResults(aa[selectedRow].groupRef
485 .getSequences(null));
489 else if (aa[selectedRow].sequenceRef != null)
491 Vector sr = new Vector();
492 sr.addElement(aa[selectedRow].sequenceRef);
493 if (evt.getClickCount() == 1)
495 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
497 else if (evt.getClickCount() >= 2)
499 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
500 SequenceGroup sg = new SequenceGroup();
501 sg.addSequence(aa[selectedRow].sequenceRef, false);
502 ap.av.setSelectionGroup(sg);
503 ap.av.sendSelection();
504 ap.paintAlignment(false);
505 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
511 if (!SwingUtilities.isRightMouseButton(evt))
516 JPopupMenu pop = new JPopupMenu("Annotations");
517 JMenuItem item = new JMenuItem(ADDNEW);
518 item.addActionListener(this);
523 { // let the user make everything visible again
524 item = new JMenuItem(SHOWALL);
525 item.addActionListener(this);
528 pop.show(this, evt.getX(), evt.getY());
531 item = new JMenuItem(EDITNAME);
532 item.addActionListener(this);
534 item = new JMenuItem(HIDE);
535 item.addActionListener(this);
537 item = new JMenuItem(DELETE);
538 item.addActionListener(this);
542 item = new JMenuItem(SHOWALL);
543 item.addActionListener(this);
546 item = new JMenuItem(OUTPUT_TEXT);
547 item.addActionListener(this);
549 // TODO: annotation object should be typed for autocalculated/derived
551 if (selectedRow < aa.length)
553 if (!aa[selectedRow].autoCalculated)
555 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
557 // display formatting settings for this row.
559 // av and sequencegroup need to implement same interface for
560 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
561 aa[selectedRow].scaleColLabel);
562 item.addActionListener(this);
566 else if (aa[selectedRow].label.indexOf("Consensus") > -1)
569 // av and sequencegroup need to implement same interface for
570 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
571 "Ignore Gaps In Consensus",
572 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
573 .getIgnoreGapsConsensus() : ap.av
574 .getIgnoreGapsConsensus());
575 final AlignmentAnnotation aaa = aa[selectedRow];
576 cbmi.addActionListener(new ActionListener()
578 public void actionPerformed(ActionEvent e)
580 if (aaa.groupRef != null)
582 // TODO: pass on reference to ap so the view can be updated.
583 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
584 ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
588 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
593 // av and sequencegroup need to implement same interface for
594 if (aaa.groupRef != null)
596 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
597 "Show Group Histogram",
598 aa[selectedRow].groupRef.isShowConsensusHistogram());
599 chist.addActionListener(new ActionListener()
601 public void actionPerformed(ActionEvent e)
603 // TODO: pass on reference
609 aaa.groupRef.setShowConsensusHistogram(chist.getState());
611 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
615 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
617 aa[selectedRow].groupRef.isShowSequenceLogo());
618 cprofl.addActionListener(new ActionListener()
620 public void actionPerformed(ActionEvent e)
622 // TODO: pass on reference
628 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
630 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
634 final JCheckBoxMenuItem cproflnorm = new JCheckBoxMenuItem(
635 "Normalise Group Logo",
636 aa[selectedRow].groupRef.isNormaliseSequenceLogo());
637 cproflnorm.addActionListener(new ActionListener()
639 public void actionPerformed(ActionEvent e)
642 // TODO: pass on reference
648 aaa.groupRef.setNormaliseSequenceLogo(cproflnorm.getState());
649 // automatically enable logo display if we're clicked
650 aaa.groupRef.setshowSequenceLogo(true);
652 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
659 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
660 "Show Histogram", av.isShowConsensusHistogram());
661 chist.addActionListener(new ActionListener()
663 public void actionPerformed(ActionEvent e)
665 // TODO: pass on reference
671 av.setShowConsensusHistogram(chist.getState());
672 ap.alignFrame.setMenusForViewport();
674 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
678 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
679 "Show Logo", av.isShowSequenceLogo());
680 cprof.addActionListener(new ActionListener()
682 public void actionPerformed(ActionEvent e)
684 // TODO: pass on reference
690 av.setShowSequenceLogo(cprof.getState());
691 ap.alignFrame.setMenusForViewport();
693 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
697 final JCheckBoxMenuItem cprofnorm = new JCheckBoxMenuItem(
698 "Normalise Logo", av.isNormaliseSequenceLogo());
699 cprofnorm.addActionListener(new ActionListener()
701 public void actionPerformed(ActionEvent e)
703 // TODO: pass on reference
709 av.setShowSequenceLogo(true);
710 av.setNormaliseSequenceLogo(cprofnorm.getState());
711 ap.alignFrame.setMenusForViewport();
713 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
718 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
719 consclipbrd.addActionListener(this);
720 pop.add(consclipbrd);
723 pop.show(this, evt.getX(), evt.getY());
727 * do a single sequence copy to jalview and the system clipboard
730 * sequence to be copied to clipboard
732 protected void copy_annotseqtoclipboard(SequenceI sq)
734 SequenceI[] seqs = new SequenceI[]
736 String[] omitHidden = null;
737 SequenceI[] dseqs = new SequenceI[]
738 { sq.getDatasetSequence() };
739 if (dseqs[0] == null)
741 dseqs[0] = new Sequence(sq);
742 dseqs[0].setSequence(jalview.analysis.AlignSeq.extractGaps(
743 jalview.util.Comparison.GapChars, sq.getSequenceAsString()));
745 sq.setDatasetSequence(dseqs[0]);
747 Alignment ds = new Alignment(dseqs);
748 if (av.hasHiddenColumns())
750 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
751 sq.getLength(), seqs);
754 String output = new FormatAdapter().formatSequences("Fasta", seqs,
757 Toolkit.getDefaultToolkit().getSystemClipboard()
758 .setContents(new StringSelection(output), Desktop.instance);
760 Vector hiddenColumns = null;
761 if (av.hasHiddenColumns())
763 hiddenColumns = new Vector();
764 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
766 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
769 hiddenColumns.addElement(new int[]
770 { region[0], region[1] });
774 Desktop.jalviewClipboard = new Object[]
775 { seqs, ds, // what is the dataset of a consensus sequence ? need to flag
776 // sequence as special.
786 public void paintComponent(Graphics g)
789 int width = getWidth();
792 width = ap.calculateIdWidth().width + 4;
795 Graphics2D g2 = (Graphics2D) g;
798 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
799 RenderingHints.VALUE_ANTIALIAS_ON);
802 drawComponent(g2, width);
812 public void drawComponent(Graphics g, int width)
814 if (av.getFont().getSize() < 10)
820 g.setFont(av.getFont());
823 FontMetrics fm = g.getFontMetrics(g.getFont());
824 g.setColor(Color.white);
825 g.fillRect(0, 0, getWidth(), getHeight());
827 g.translate(0, scrollOffset);
828 g.setColor(Color.black);
830 AlignmentAnnotation[] aa = av.getAlignment().getAlignmentAnnotation();
831 int fontHeight = g.getFont().getSize();
836 Font baseFont = g.getFont();
837 FontMetrics baseMetrics = fm;
838 int ofontH = fontHeight;
839 boolean visible = true;
842 hasHiddenRows = false;
843 for (int i = 0; i < aa.length; i++)
846 g.setColor(Color.black);
850 hasHiddenRows = true;
856 offset = -aa[i].height / 2;
860 offset += fm.getHeight() / 2;
861 offset -= fm.getDescent();
864 offset += fm.getDescent();
866 x = width - fm.stringWidth(aa[i].label) - 3;
868 if (aa[i].graphGroup > -1)
871 for (int gg = 0; gg < aa.length; gg++)
873 if (aa[gg].graphGroup == aa[i].graphGroup)
878 if (groupSize * (fontHeight + 8) < aa[i].height)
880 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
885 float h = aa[i].height / (float) groupSize, s;
892 fontHeight = -8 + (int) h;
893 s = ((float) fontHeight) / (float) ofontH;
894 Font f = baseFont.deriveFont(AffineTransform
895 .getScaleInstance(s, s));
897 fm = g.getFontMetrics();
898 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
903 for (int gg = 0; gg < aa.length; gg++)
905 if (aa[gg].graphGroup == aa[i].graphGroup)
907 x = width - fm.stringWidth(aa[gg].label) - 3;
908 g.drawString(aa[gg].label, x, y - graphExtras);
910 if (aa[gg]._linecolour != null)
913 g.setColor(aa[gg]._linecolour);
914 g.drawLine(x, y - graphExtras + 3,
915 x + fm.stringWidth(aa[gg].label), y - graphExtras
919 g.setColor(Color.black);
920 graphExtras += fontHeight + 8;
930 g.drawString(aa[i].label, x, y + offset);
937 g.drawImage(image, 2, 0 - scrollOffset, this);
939 else if (dragEvent != null && aa != null)
941 g.setColor(Color.lightGray);
942 g.drawString(aa[selectedRow].label, dragEvent.getX(),
943 dragEvent.getY() - scrollOffset);
946 if ((aa == null) || (aa.length < 1))
948 g.drawString("Right click", 2, 8);
949 g.drawString("to add annotation", 2, 18);