2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3 * Copyright (C) 2011 J Procter, AM Waterhouse, 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.image.*;
29 import jalview.datamodel.*;
38 public class AnnotationLabels extends JPanel implements MouseListener,
39 MouseMotionListener, ActionListener
41 static String TOGGLE_LABELSCALE = "Scale Label to Column";
43 static String ADDNEW = "Add New Row";
45 static String EDITNAME = "Edit Label/Description";
47 static String HIDE = "Hide This Row";
49 static String DELETE = "Delete This Row";
51 static String SHOWALL = "Show All Hidden Rows";
53 static String OUTPUT_TEXT = "Export Annotation";
55 static String COPYCONS_SEQ = "Copy Consensus Sequence";
57 boolean resizePanel = false;
65 boolean resizing = false;
75 Font font = new Font("Arial", Font.PLAIN, 11);
77 private boolean hasHiddenRows;
80 * Creates a new AnnotationLabels object.
85 public AnnotationLabels(AlignmentPanel ap)
89 ToolTipManager.sharedInstance().registerComponent(this);
91 java.net.URL url = getClass().getResource("/images/idwidth.gif");
96 temp = java.awt.Toolkit.getDefaultToolkit().createImage(url);
101 MediaTracker mt = new MediaTracker(this);
102 mt.addImage(temp, 0);
104 } catch (Exception ex)
108 BufferedImage bi = new BufferedImage(temp.getHeight(this),
109 temp.getWidth(this), BufferedImage.TYPE_INT_RGB);
110 Graphics2D g = (Graphics2D) bi.getGraphics();
111 g.rotate(Math.toRadians(90));
112 g.drawImage(temp, 0, -bi.getWidth(this), this);
115 addMouseListener(this);
116 addMouseMotionListener(this);
119 public AnnotationLabels(AlignViewport av)
130 public void setScrollOffset(int y)
137 * sets selectedRow to -2 if no annotation preset, -1 if no visible row is at
141 * coordinate position to search for a row
143 void getSelectedRow(int y)
146 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
150 for (int i = 0; i < aa.length; i++)
158 height += aa[i].height;
176 public void actionPerformed(ActionEvent evt)
178 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
180 if (evt.getActionCommand().equals(ADDNEW))
182 AlignmentAnnotation newAnnotation = new AlignmentAnnotation(null,
183 null, new Annotation[ap.av.alignment.getWidth()]);
185 if (!editLabelDescription(newAnnotation))
190 ap.av.alignment.addAnnotation(newAnnotation);
191 ap.av.alignment.setAnnotationIndex(newAnnotation, 0);
193 else if (evt.getActionCommand().equals(EDITNAME))
195 editLabelDescription(aa[selectedRow]);
198 else if (evt.getActionCommand().equals(HIDE))
200 aa[selectedRow].visible = false;
202 else if (evt.getActionCommand().equals(DELETE))
204 ap.av.alignment.deleteAnnotation(aa[selectedRow]);
206 else if (evt.getActionCommand().equals(SHOWALL))
208 for (int i = 0; i < aa.length; i++)
210 if (!aa[i].visible && aa[i].annotations != null)
212 aa[i].visible = true;
216 else if (evt.getActionCommand().equals(OUTPUT_TEXT))
218 new AnnotationExporter().exportAnnotations(ap,
219 new AlignmentAnnotation[]
220 { aa[selectedRow] }, null, null);
222 else if (evt.getActionCommand().equals(COPYCONS_SEQ))
224 SequenceI cons = null;
225 if (aa[selectedRow].groupRef != null)
227 cons = aa[selectedRow].groupRef.getConsensusSeq();
231 cons = av.getConsensusSeq();
235 copy_annotseqtoclipboard(cons);
239 else if (evt.getActionCommand().equals(TOGGLE_LABELSCALE))
241 aa[selectedRow].scaleColLabel = !aa[selectedRow].scaleColLabel;
244 ap.validateAnnotationDimensions(false);
248 //ap.paintAlignment(true);
257 boolean editLabelDescription(AlignmentAnnotation annotation)
259 EditNameDialog dialog = new EditNameDialog(annotation.label,
260 annotation.description, " Annotation Name ",
261 "Annotation Description ", "Edit Annotation Name/Description",
269 annotation.label = dialog.getName();
271 String text = dialog.getDescription();
272 if (text != null && text.length() == 0)
276 annotation.description = text;
287 public void mousePressed(MouseEvent evt)
289 getSelectedRow(evt.getY() - scrollOffset);
299 public void mouseReleased(MouseEvent evt)
301 int start = selectedRow;
302 getSelectedRow(evt.getY() - scrollOffset);
303 int end = selectedRow;
307 // Swap these annotations
308 AlignmentAnnotation startAA = ap.av.alignment
309 .getAlignmentAnnotation()[start];
312 end = ap.av.alignment.getAlignmentAnnotation().length - 1;
314 AlignmentAnnotation endAA = ap.av.alignment.getAlignmentAnnotation()[end];
316 ap.av.alignment.getAlignmentAnnotation()[end] = startAA;
317 ap.av.alignment.getAlignmentAnnotation()[start] = endAA;
323 ap.annotationPanel.repaint();
332 public void mouseEntered(MouseEvent evt)
347 public void mouseExited(MouseEvent evt)
349 if (dragEvent == null)
362 public void mouseDragged(MouseEvent evt)
368 Dimension d = ap.annotationScroller.getPreferredSize();
369 int dif = evt.getY() - oldY;
371 dif /= ap.av.charHeight;
372 dif *= ap.av.charHeight;
374 if ((d.height - dif) > 20)
376 ap.annotationScroller.setPreferredSize(new Dimension(d.width,
378 d = ap.annotationSpaceFillerHolder.getPreferredSize();
379 ap.annotationSpaceFillerHolder.setPreferredSize(new Dimension(
380 d.width, d.height - dif));
381 ap.paintAlignment(true);
398 public void mouseMoved(MouseEvent evt)
400 resizePanel = evt.getY() < 10;
402 getSelectedRow(evt.getY() - scrollOffset);
405 && ap.av.alignment.getAlignmentAnnotation().length > selectedRow)
407 AlignmentAnnotation aa = ap.av.alignment.getAlignmentAnnotation()[selectedRow];
409 StringBuffer desc = new StringBuffer();
410 if (aa.description != null
411 && !aa.description.equals("New description"))
413 // TODO: we could refactor and merge this code with the code in jalview.gui.SeqPanel.mouseMoved(..) that formats sequence feature tooltips
414 desc.append(aa.getDescription(true).trim());
415 // check to see if the description is an html fragment.
416 if (desc.length()<6 || (desc.substring(0,6).toLowerCase().indexOf("<html>")<0))
418 // clean the description ready for embedding in html
419 desc = new StringBuffer(Pattern.compile("<").matcher(desc).replaceAll("<"));
420 desc.insert(0, "<html>");
422 // remove terminating html if any
423 int i=desc.substring(desc.length()-7).toLowerCase().lastIndexOf("</html>");
425 desc.setLength(desc.length()-7+i);
430 desc.append("<br/>");
435 // begin the tooltip's html fragment
436 desc.append("<html>");
440 // TODO: limit precision of score to avoid noise from imprecise doubles (64.7 becomes 64.7+/some tiny value).
441 desc.append(" Score: " + aa.score);
444 if (desc.length() > 6)
446 desc.append("</html>");
447 this.setToolTipText(desc.toString());
450 this.setToolTipText(null);
461 public void mouseClicked(MouseEvent evt)
463 AlignmentAnnotation[] aa = ap.av.alignment.getAlignmentAnnotation();
464 if (SwingUtilities.isLeftMouseButton(evt))
466 if (selectedRow > -1 && selectedRow < aa.length)
468 if (aa[selectedRow].groupRef != null)
470 if (evt.getClickCount() >= 2)
472 // todo: make the ap scroll to the selection - not necessary, first click highlights/scrolls, second selects
473 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
474 ap.av.setSelectionGroup(// new SequenceGroup(
475 aa[selectedRow].groupRef); // );
476 ap.paintAlignment(false);
477 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
478 ap.av.sendSelection();
482 ap.seqPanel.ap.idPanel
483 .highlightSearchResults(aa[selectedRow].groupRef
484 .getSequences(null));
488 else if (aa[selectedRow].sequenceRef != null)
490 Vector sr = new Vector();
491 sr.addElement(aa[selectedRow].sequenceRef);
492 if (evt.getClickCount() == 1)
494 ap.seqPanel.ap.idPanel.highlightSearchResults(sr);
496 else if (evt.getClickCount() >= 2)
498 ap.seqPanel.ap.idPanel.highlightSearchResults(null);
499 SequenceGroup sg = new SequenceGroup();
500 sg.addSequence(aa[selectedRow].sequenceRef, false);
501 ap.av.setSelectionGroup(sg);
502 ap.av.sendSelection();
503 ap.paintAlignment(false);
504 PaintRefresher.Refresh(ap, ap.av.getSequenceSetId());
510 if (!SwingUtilities.isRightMouseButton(evt))
515 JPopupMenu pop = new JPopupMenu("Annotations");
516 JMenuItem item = new JMenuItem(ADDNEW);
517 item.addActionListener(this);
522 { // let the user make everything visible again
523 item = new JMenuItem(SHOWALL);
524 item.addActionListener(this);
527 pop.show(this, evt.getX(), evt.getY());
530 item = new JMenuItem(EDITNAME);
531 item.addActionListener(this);
533 item = new JMenuItem(HIDE);
534 item.addActionListener(this);
536 item = new JMenuItem(DELETE);
537 item.addActionListener(this);
541 item = new JMenuItem(SHOWALL);
542 item.addActionListener(this);
545 item = new JMenuItem(OUTPUT_TEXT);
546 item.addActionListener(this);
548 // TODO: annotation object should be typed for autocalculated/derived
550 if (selectedRow < aa.length)
552 if (!aa[selectedRow].autoCalculated)
554 if (aa[selectedRow].graph == AlignmentAnnotation.NO_GRAPH)
556 // display formatting settings for this row.
558 // av and sequencegroup need to implement same interface for
559 item = new JCheckBoxMenuItem(TOGGLE_LABELSCALE,
560 aa[selectedRow].scaleColLabel);
561 item.addActionListener(this);
565 else if (aa[selectedRow].label.indexOf("Consensus") > -1)
568 // av and sequencegroup need to implement same interface for
569 final JCheckBoxMenuItem cbmi = new JCheckBoxMenuItem(
570 "Ignore Gaps In Consensus",
571 (aa[selectedRow].groupRef != null) ? aa[selectedRow].groupRef
572 .getIgnoreGapsConsensus() : ap.av
573 .getIgnoreGapsConsensus());
574 final AlignmentAnnotation aaa = aa[selectedRow];
575 cbmi.addActionListener(new ActionListener()
577 public void actionPerformed(ActionEvent e)
579 if (aaa.groupRef != null)
581 // TODO: pass on reference to ap so the view can be updated.
582 aaa.groupRef.setIgnoreGapsConsensus(cbmi.getState());
583 ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
587 ap.av.setIgnoreGapsConsensus(cbmi.getState(), ap);
592 // av and sequencegroup need to implement same interface for
593 if (aaa.groupRef != null)
595 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
596 "Show Group Histogram",
597 aa[selectedRow].groupRef.isShowConsensusHistogram());
598 chist.addActionListener(new ActionListener()
600 public void actionPerformed(ActionEvent e)
602 // TODO: pass on reference
608 aaa.groupRef.setShowConsensusHistogram(chist.getState());
610 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
614 final JCheckBoxMenuItem cprofl = new JCheckBoxMenuItem(
616 aa[selectedRow].groupRef.isShowSequenceLogo());
617 cprofl.addActionListener(new ActionListener()
619 public void actionPerformed(ActionEvent e)
621 // TODO: pass on reference
627 aaa.groupRef.setshowSequenceLogo(cprofl.getState());
629 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
636 final JCheckBoxMenuItem chist = new JCheckBoxMenuItem(
637 "Show Histogram", av.isShowConsensusHistogram());
638 chist.addActionListener(new ActionListener()
640 public void actionPerformed(ActionEvent e)
642 // TODO: pass on reference
648 av.setShowConsensusHistogram(chist.getState());
650 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
654 final JCheckBoxMenuItem cprof = new JCheckBoxMenuItem(
655 "Show Logo", av.isShowSequenceLogo());
656 cprof.addActionListener(new ActionListener()
658 public void actionPerformed(ActionEvent e)
660 // TODO: pass on reference
666 av.setShowSequenceLogo(cprof.getState());
668 // ap.annotationPanel.paint(ap.annotationPanel.getGraphics());
673 final JMenuItem consclipbrd = new JMenuItem(COPYCONS_SEQ);
674 consclipbrd.addActionListener(this);
675 pop.add(consclipbrd);
678 pop.show(this, evt.getX(), evt.getY());
682 * do a single sequence copy to jalview and the system clipboard
685 * sequence to be copied to clipboard
687 protected void copy_annotseqtoclipboard(SequenceI sq)
689 SequenceI[] seqs = new SequenceI[]
691 String[] omitHidden = null;
692 SequenceI[] dseqs = new SequenceI[]
693 { sq.getDatasetSequence() };
694 if (dseqs[0] == null)
696 dseqs[0] = new Sequence(sq);
697 dseqs[0].setSequence(jalview.analysis.AlignSeq.extractGaps(
698 jalview.util.Comparison.GapChars, sq.getSequenceAsString()));
700 sq.setDatasetSequence(dseqs[0]);
702 Alignment ds = new Alignment(dseqs);
703 if (av.hasHiddenColumns)
705 omitHidden = av.getColumnSelection().getVisibleSequenceStrings(0,
706 sq.getLength(), seqs);
709 String output = new FormatAdapter().formatSequences("Fasta", seqs,
712 Toolkit.getDefaultToolkit().getSystemClipboard()
713 .setContents(new StringSelection(output), Desktop.instance);
715 Vector hiddenColumns = null;
716 if (av.hasHiddenColumns)
718 hiddenColumns = new Vector();
719 for (int i = 0; i < av.getColumnSelection().getHiddenColumns().size(); i++)
721 int[] region = (int[]) av.getColumnSelection().getHiddenColumns()
724 hiddenColumns.addElement(new int[]
725 { region[0], region[1] });
729 Desktop.jalviewClipboard = new Object[]
730 { seqs, ds, // what is the dataset of a consensus sequence ? need to flag
731 // sequence as special.
741 public void paintComponent(Graphics g)
744 int width = getWidth();
747 width = ap.calculateIdWidth().width + 4;
750 Graphics2D g2 = (Graphics2D) g;
753 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
754 RenderingHints.VALUE_ANTIALIAS_ON);
757 drawComponent(g2, width);
767 public void drawComponent(Graphics g, int width)
769 if (av.getFont().getSize() < 10)
775 g.setFont(av.getFont());
778 FontMetrics fm = g.getFontMetrics(g.getFont());
779 g.setColor(Color.white);
780 g.fillRect(0, 0, getWidth(), getHeight());
782 g.translate(0, scrollOffset);
783 g.setColor(Color.black);
785 AlignmentAnnotation[] aa = av.alignment.getAlignmentAnnotation();
786 int fontHeight = g.getFont().getSize();
794 hasHiddenRows = false;
795 for (int i = 0; i < aa.length; i++)
797 g.setColor(Color.black);
801 hasHiddenRows = true;
807 offset = -aa[i].height / 2;
811 offset += fm.getHeight() / 2;
812 offset -= fm.getDescent();
815 offset += fm.getDescent();
817 x = width - fm.stringWidth(aa[i].label) - 3;
819 if (aa[i].graphGroup > -1)
822 for (int gg = 0; gg < aa.length; gg++)
824 if (aa[gg].graphGroup == aa[i].graphGroup)
830 if (groupSize * (fontHeight + 8) < aa[i].height)
832 graphExtras = (aa[i].height - (groupSize * (fontHeight + 8))) / 2;
835 for (int gg = 0; gg < aa.length; gg++)
837 if (aa[gg].graphGroup == aa[i].graphGroup)
839 x = width - fm.stringWidth(aa[gg].label) - 3;
840 g.drawString(aa[gg].label, x, y - graphExtras);
841 if (aa[gg].annotations[0] != null)
843 g.setColor(aa[gg].annotations[0].colour);
846 g.drawLine(x, y - graphExtras - 3,
847 x + fm.stringWidth(aa[gg].label), y - graphExtras - 3);
849 g.setColor(Color.black);
850 graphExtras += fontHeight + 8;
856 g.drawString(aa[i].label, x, y + offset);
863 g.drawImage(image, 2, 0 - scrollOffset, this);
865 else if (dragEvent != null && aa != null)
867 g.setColor(Color.lightGray);
868 g.drawString(aa[selectedRow].label, dragEvent.getX(),
869 dragEvent.getY() - scrollOffset);
872 if ((aa == null) || (aa.length < 1))
874 g.drawString("Right click", 2, 8);
875 g.drawString("to add annotation", 2, 18);