2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.4)
3 * Copyright (C) 2008 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 package jalview.appletgui;
24 import java.awt.event.*;
26 import jalview.analysis.*;
27 import jalview.commands.*;
28 import jalview.datamodel.*;
29 import jalview.schemes.*;
30 import jalview.util.UrlLink;
31 import jalview.io.AppletFormatAdapter;
33 public class APopupMenu extends java.awt.PopupMenu implements
34 ActionListener, ItemListener
36 Menu groupMenu = new Menu();
38 MenuItem editGroupName = new MenuItem();
40 protected MenuItem clustalColour = new MenuItem();
42 protected MenuItem zappoColour = new MenuItem();
44 protected MenuItem taylorColour = new MenuItem();
46 protected MenuItem hydrophobicityColour = new MenuItem();
48 protected MenuItem helixColour = new MenuItem();
50 protected MenuItem strandColour = new MenuItem();
52 protected MenuItem turnColour = new MenuItem();
54 protected MenuItem buriedColour = new MenuItem();
56 protected CheckboxMenuItem abovePIDColour = new CheckboxMenuItem();
58 protected MenuItem userDefinedColour = new MenuItem();
60 protected MenuItem PIDColour = new MenuItem();
62 protected MenuItem BLOSUM62Colour = new MenuItem();
64 MenuItem noColourmenuItem = new MenuItem();
66 protected CheckboxMenuItem conservationMenuItem = new CheckboxMenuItem();
68 final AlignmentPanel ap;
70 MenuItem unGroupMenuItem = new MenuItem();
72 MenuItem nucleotideMenuItem = new MenuItem();
74 Menu colourMenu = new Menu();
76 CheckboxMenuItem showBoxes = new CheckboxMenuItem();
78 CheckboxMenuItem showText = new CheckboxMenuItem();
80 CheckboxMenuItem showColourText = new CheckboxMenuItem();
82 Menu editMenu = new Menu("Edit");
84 MenuItem copy = new MenuItem("Copy (Jalview Only)");
86 MenuItem cut = new MenuItem("Cut (Jalview Only)");
88 MenuItem toUpper = new MenuItem("To Upper Case");
90 MenuItem toLower = new MenuItem("To Lower Case");
92 MenuItem toggleCase = new MenuItem("Toggle Case");
94 Menu outputmenu = new Menu();
96 Menu seqMenu = new Menu();
98 MenuItem pdb = new MenuItem();
100 MenuItem hideSeqs = new MenuItem();
102 MenuItem repGroup = new MenuItem();
104 MenuItem sequenceName = new MenuItem("Edit Name/Description");
106 MenuItem sequenceFeature = new MenuItem("Create Sequence Feature");
108 MenuItem editSequence = new MenuItem("Edit Sequence");
112 MenuItem revealAll = new MenuItem();
114 Menu menu1 = new Menu();
116 public APopupMenu(AlignmentPanel apanel, final Sequence seq, Vector links)
118 // /////////////////////////////////////////////////////////
119 // If this is activated from the sequence panel, the user may want to
120 // edit or annotate a particular residue. Therefore display the residue menu
122 // If from the IDPanel, we must display the sequence menu
123 // ////////////////////////////////////////////////////////
131 } catch (Exception e)
136 for (int i = 0; i < jalview.io.AppletFormatAdapter.WRITEABLE_FORMATS.length; i++)
138 MenuItem item = new MenuItem(
139 jalview.io.AppletFormatAdapter.WRITEABLE_FORMATS[i]);
141 item.addActionListener(this);
142 outputmenu.add(item);
145 SequenceGroup sg = ap.av.getSelectionGroup();
147 if (sg != null && sg.getSize() > 0)
149 editGroupName.setLabel(sg.getName());
150 showText.setState(sg.getDisplayText());
151 showColourText.setState(sg.getColourText());
152 showBoxes.setState(sg.getDisplayBoxes());
153 if (!ap.av.alignment.getGroups().contains(sg))
155 groupMenu.remove(unGroupMenuItem);
165 if (links != null && links.size() > 0)
167 Menu linkMenu = new Menu("Link");
169 for (int i = 0; i < links.size(); i++)
171 link = links.elementAt(i).toString();
172 UrlLink urlLink = new UrlLink(link);
173 if (!urlLink.isValid())
175 System.err.println(urlLink.getInvalidMessage());
178 final String target = urlLink.getTarget(); // link.substring(0,
179 // link.indexOf("|"));
180 final String label = urlLink.getLabel();
181 if (urlLink.isDynamic())
184 // collect matching db-refs
185 DBRefEntry[] dbr = jalview.util.DBRefUtils.selectRefs(seq
186 .getDBRef(), new String[]
188 // collect id string too
189 String id = seq.getName();
190 String descr = seq.getDescription();
191 if (descr!=null && descr.length()<1)
197 for (int r = 0; r < dbr.length; r++)
199 if (id != null && dbr[r].getAccessionId().equals(id))
201 // suppress duplicate link creation for the bare sequence ID
202 // string with this link
205 // create Bare ID link for this RUL
206 String[] urls = urlLink.makeUrls(dbr[r].getAccessionId(),
210 for (int u = 0; u < urls.length; u += 2)
212 addshowLink(linkMenu, label + "|" + urls[u], urls[u + 1]);
219 // create Bare ID link for this RUL
220 String[] urls = urlLink.makeUrls(id, true);
223 for (int u = 0; u < urls.length; u += 2)
225 addshowLink(linkMenu, label, urls[u + 1]);
228 // addshowLink(linkMenu, target, url_pref + id + url_suff);
230 // Now construct URLs from description but only try to do it for regex URL links
231 if (descr != null && urlLink.getRegexReplace()!=null)
233 // create link for this URL from description only if regex matches
234 String[] urls = urlLink.makeUrls(descr, true);
237 for (int u = 0; u < urls.length; u += 2)
239 addshowLink(linkMenu, label, urls[u + 1]);
246 addshowLink(linkMenu, target, urlLink.getUrl_prefix()); // link.substring(link.lastIndexOf("|")+1));
251 * if (link.indexOf("$SEQUENCE_ID$") > -1) { // Substitute SEQUENCE_ID
252 * string and any matching database reference accessions String url_pref =
253 * link.substring(link.indexOf("|") + 1, link.indexOf("$SEQUENCE_ID$"));
255 * String url_suff = link.substring(link.indexOf("$SEQUENCE_ID$") + 13);
256 * // collect matching db-refs DBRefEntry[] dbr =
257 * jalview.util.DBRefUtils.selectRefs(seq.getDBRef(), new
258 * String[]{target}); // collect id string too String id =
259 * seq.getName(); if (id.indexOf("|") > -1) { id =
260 * id.substring(id.lastIndexOf("|") + 1); } if (dbr!=null) { for (int
261 * r=0;r<dbr.length; r++) { if (dbr[r].getAccessionId().equals(id)) { //
262 * suppress duplicate link creation for the bare sequence ID string with
263 * this link id = null; } addshowLink(linkMenu,
264 * dbr[r].getSource()+"|"+dbr[r].getAccessionId(), target,
265 * url_pref+dbr[r].getAccessionId()+url_suff); } } if (id!=null) { //
266 * create Bare ID link for this RUL addshowLink(linkMenu, target,
267 * url_pref + id + url_suff); } } else { addshowLink(linkMenu, target,
268 * link.substring(link.lastIndexOf("|")+1)); }
274 seqMenu.add(linkMenu);
283 seqMenu.setLabel(seq.getName());
284 repGroup.setLabel("Represent Group with " + seq.getName());
291 if (!ap.av.hasHiddenRows)
298 * add a show URL menu item to the given linkMenu
306 private void addshowLink(Menu linkMenu, final String target,
309 addshowLink(linkMenu, target, target, url);
313 * add a show URL menu item to the given linkMenu
323 private void addshowLink(Menu linkMenu, final String target,
324 final String label, final String url)
326 MenuItem item = new MenuItem(label);
327 item.addActionListener(new java.awt.event.ActionListener()
329 public void actionPerformed(ActionEvent e)
331 ap.alignFrame.showURL(url, target);
337 public void itemStateChanged(ItemEvent evt)
339 if (evt.getSource() == abovePIDColour)
341 abovePIDColour_itemStateChanged();
343 else if (evt.getSource() == showColourText)
345 showColourText_itemStateChanged();
347 else if (evt.getSource() == showText)
349 showText_itemStateChanged();
351 else if (evt.getSource() == showBoxes)
353 showBoxes_itemStateChanged();
357 public void actionPerformed(ActionEvent evt)
359 Object source = evt.getSource();
360 if (source == clustalColour)
362 clustalColour_actionPerformed();
364 else if (source == zappoColour)
366 zappoColour_actionPerformed();
368 else if (source == taylorColour)
370 taylorColour_actionPerformed();
372 else if (source == hydrophobicityColour)
374 hydrophobicityColour_actionPerformed();
376 else if (source == helixColour)
378 helixColour_actionPerformed();
380 else if (source == strandColour)
382 strandColour_actionPerformed();
384 else if (source == turnColour)
386 turnColour_actionPerformed();
388 else if (source == buriedColour)
390 buriedColour_actionPerformed();
392 else if (source == nucleotideMenuItem)
394 nucleotideMenuItem_actionPerformed();
397 else if (source == userDefinedColour)
399 userDefinedColour_actionPerformed();
401 else if (source == PIDColour)
403 PIDColour_actionPerformed();
405 else if (source == BLOSUM62Colour)
407 BLOSUM62Colour_actionPerformed();
409 else if (source == noColourmenuItem)
411 noColourmenuItem_actionPerformed();
413 else if (source == conservationMenuItem)
415 conservationMenuItem_itemStateChanged();
417 else if (source == unGroupMenuItem)
419 unGroupMenuItem_actionPerformed();
422 else if (source == sequenceName)
426 else if (source == pdb)
430 else if (source == hideSeqs)
432 hideSequences(false);
434 else if (source == repGroup)
438 else if (source == revealAll)
440 ap.av.showAllHiddenSeqs();
443 else if (source == editGroupName)
445 EditNameDialog dialog = new EditNameDialog(getGroup().getName(),
446 getGroup().getDescription(), " Group Name",
447 "Group Description", ap.alignFrame,
448 "Edit Group Name / Description", 500, 100, true);
452 getGroup().setName(dialog.getName().replace(' ', '_'));
453 getGroup().setDescription(dialog.getDescription());
457 else if (source == copy)
459 ap.alignFrame.copy_actionPerformed();
461 else if (source == cut)
463 ap.alignFrame.cut_actionPerformed();
465 else if (source == editSequence)
467 SequenceGroup sg = ap.av.getSelectionGroup();
472 seq = (Sequence) sg.getSequenceAt(0);
474 EditNameDialog dialog = new EditNameDialog(seq.getSequenceAsString(
475 sg.getStartRes(), sg.getEndRes() + 1), null,
476 "Edit Sequence ", null,
478 ap.alignFrame, "Edit Sequence", 500, 100, true);
482 EditCommand editCommand = new EditCommand("Edit Sequences",
483 EditCommand.REPLACE, dialog.getName().replace(' ',
484 ap.av.getGapCharacter()), sg
485 .getSequencesAsArray(ap.av.hiddenRepSequences),
486 sg.getStartRes(), sg.getEndRes() + 1, ap.av.alignment);
488 ap.alignFrame.addHistoryItem(editCommand);
490 ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
495 else if (source == toUpper || source == toLower || source == toggleCase)
497 SequenceGroup sg = ap.av.getSelectionGroup();
498 Vector regions = new Vector();
501 int start = sg.getStartRes();
502 int end = sg.getEndRes() + 1;
506 if (ap.av.hasHiddenColumns)
510 start = ap.av.colSel.adjustForHiddenColumns(start);
513 end = ap.av.colSel.getHiddenBoundaryRight(start);
516 end = sg.getEndRes() + 1;
518 if (end > sg.getEndRes())
520 end = sg.getEndRes() + 1;
524 regions.addElement(new int[]
527 if (ap.av.hasHiddenColumns)
529 start = ap.av.colSel.adjustForHiddenColumns(end);
530 start = ap.av.colSel.getHiddenBoundaryLeft(start) + 1;
532 } while (end < sg.getEndRes());
534 int[][] startEnd = new int[regions.size()][2];
535 for (int i = 0; i < regions.size(); i++)
537 startEnd[i] = (int[]) regions.elementAt(i);
543 if (source == toggleCase)
545 description = "Toggle Case";
546 caseChange = ChangeCaseCommand.TOGGLE_CASE;
548 else if (source == toUpper)
550 description = "To Upper Case";
551 caseChange = ChangeCaseCommand.TO_UPPER;
555 description = "To Lower Case";
556 caseChange = ChangeCaseCommand.TO_LOWER;
559 ChangeCaseCommand caseCommand = new ChangeCaseCommand(description,
560 sg.getSequencesAsArray(ap.av.hiddenRepSequences), startEnd,
563 ap.alignFrame.addHistoryItem(caseCommand);
565 ap.av.firePropertyChange("alignment", null, ap.av.getAlignment()
570 else if (source == sequenceFeature)
572 SequenceGroup sg = ap.av.getSelectionGroup();
578 int gSize = sg.getSize();
579 SequenceI[] seqs = new SequenceI[gSize];
580 SequenceFeature[] features = new SequenceFeature[gSize];
582 for (int i = 0; i < gSize; i++)
584 seqs[i] = sg.getSequenceAt(i);
585 int start = sg.getSequenceAt(i).findPosition(sg.getStartRes());
586 int end = sg.findEndRes(sg.getSequenceAt(i));
587 features[i] = new SequenceFeature(null, null, null, start, end,
591 if (ap.seqPanel.seqCanvas.getFeatureRenderer().amendFeatures(seqs,
594 ap.alignFrame.sequenceFeatures.setState(true);
595 ap.av.showSequenceFeatures(true);
596 ap.highlightSearchResults(null);
606 void outputText(ActionEvent e)
608 CutAndPasteTransfer cap = new CutAndPasteTransfer(true, ap.alignFrame);
610 Frame frame = new Frame();
612 jalview.bin.JalviewLite.addFrame(frame, "Selection output - "
613 + e.getActionCommand(), 600, 500);
615 cap.setText(new jalview.io.AppletFormatAdapter().formatSequences(e
616 .getActionCommand(), new Alignment(ap.av
617 .getSelectionAsNewSequence()), ap.av.showJVSuffix));
623 EditNameDialog dialog = new EditNameDialog(seq.getName(), seq
624 .getDescription(), " Sequence Name",
625 "Sequence Description", ap.alignFrame,
626 "Edit Sequence Name / Description", 500, 100, true);
630 seq.setName(dialog.getName());
631 seq.setDescription(dialog.getDescription());
632 ap.paintAlignment(false);
638 if (seq.getPDBId() != null)
640 PDBEntry entry = (PDBEntry) seq.getPDBId().firstElement();
642 if (ap.av.applet.jmolAvailable)
643 new jalview.appletgui.AppletJmol(entry, new Sequence[]
644 { seq }, null, ap, AppletFormatAdapter.URL);
646 new MCview.AppletPDBViewer(entry, new Sequence[]
647 { seq }, null, ap, AppletFormatAdapter.URL);
652 CutAndPasteTransfer cap = new CutAndPasteTransfer(true, ap.alignFrame);
653 cap.setText("Paste your PDB file here.");
654 cap.setPDBImport(seq);
655 Frame frame = new Frame();
657 jalview.bin.JalviewLite.addFrame(frame, "Paste PDB file ", 400, 300);
661 private void jbInit() throws Exception
663 groupMenu.setLabel("Group");
664 groupMenu.setLabel("Selection");
665 sequenceFeature.addActionListener(this);
667 editGroupName.addActionListener(this);
668 unGroupMenuItem.setLabel("Remove Group");
669 unGroupMenuItem.addActionListener(this);
671 nucleotideMenuItem.setLabel("Nucleotide");
672 nucleotideMenuItem.addActionListener(this);
673 conservationMenuItem.addItemListener(this);
674 abovePIDColour.addItemListener(this);
675 colourMenu.setLabel("Group Colour");
676 showBoxes.setLabel("Boxes");
677 showBoxes.setState(true);
678 showBoxes.addItemListener(this);
679 sequenceName.addActionListener(this);
681 showText.setLabel("Text");
682 showText.addItemListener(this);
683 showColourText.setLabel("Colour Text");
684 showColourText.addItemListener(this);
685 outputmenu.setLabel("Output to Textbox...");
686 seqMenu.setLabel("Sequence");
687 pdb.setLabel("View PDB Structure");
688 hideSeqs.setLabel("Hide Sequences");
689 repGroup.setLabel("Represent Group with");
690 revealAll.setLabel("Reveal All");
691 menu1.setLabel("Group");
696 groupMenu.add(editGroupName);
697 groupMenu.add(editMenu);
698 groupMenu.add(outputmenu);
699 groupMenu.add(sequenceFeature);
700 groupMenu.add(menu1);
702 colourMenu.add(noColourmenuItem);
703 colourMenu.add(clustalColour);
704 colourMenu.add(BLOSUM62Colour);
705 colourMenu.add(PIDColour);
706 colourMenu.add(zappoColour);
707 colourMenu.add(taylorColour);
708 colourMenu.add(hydrophobicityColour);
709 colourMenu.add(helixColour);
710 colourMenu.add(strandColour);
711 colourMenu.add(turnColour);
712 colourMenu.add(buriedColour);
713 colourMenu.add(nucleotideMenuItem);
714 colourMenu.add(userDefinedColour);
715 colourMenu.addSeparator();
716 colourMenu.add(abovePIDColour);
717 colourMenu.add(conservationMenuItem);
719 noColourmenuItem.setLabel("None");
720 noColourmenuItem.addActionListener(this);
722 clustalColour.setLabel("Clustalx colours");
723 clustalColour.addActionListener(this);
724 zappoColour.setLabel("Zappo");
725 zappoColour.addActionListener(this);
726 taylorColour.setLabel("Taylor");
727 taylorColour.addActionListener(this);
728 hydrophobicityColour.setLabel("Hydrophobicity");
729 hydrophobicityColour.addActionListener(this);
730 helixColour.setLabel("Helix propensity");
731 helixColour.addActionListener(this);
732 strandColour.setLabel("Strand propensity");
733 strandColour.addActionListener(this);
734 turnColour.setLabel("Turn propensity");
735 turnColour.addActionListener(this);
736 buriedColour.setLabel("Buried Index");
737 buriedColour.addActionListener(this);
738 abovePIDColour.setLabel("Above % Identity");
740 userDefinedColour.setLabel("User Defined");
741 userDefinedColour.addActionListener(this);
742 PIDColour.setLabel("Percentage Identity");
743 PIDColour.addActionListener(this);
744 BLOSUM62Colour.setLabel("BLOSUM62");
745 BLOSUM62Colour.addActionListener(this);
746 conservationMenuItem.setLabel("Conservation");
749 copy.addActionListener(this);
751 cut.addActionListener(this);
753 editMenu.add(editSequence);
754 editSequence.addActionListener(this);
756 editMenu.add(toUpper);
757 toUpper.addActionListener(this);
758 editMenu.add(toLower);
759 toLower.addActionListener(this);
760 editMenu.add(toggleCase);
761 seqMenu.add(sequenceName);
763 seqMenu.add(repGroup);
764 menu1.add(unGroupMenuItem);
765 menu1.add(colourMenu);
766 menu1.add(showBoxes);
768 menu1.add(showColourText);
769 toggleCase.addActionListener(this);
770 pdb.addActionListener(this);
771 hideSeqs.addActionListener(this);
772 repGroup.addActionListener(this);
773 revealAll.addActionListener(this);
778 ap.paintAlignment(true);
781 protected void clustalColour_actionPerformed()
783 SequenceGroup sg = getGroup();
784 sg.cs = new ClustalxColourScheme(sg
785 .getSequences(ap.av.hiddenRepSequences), ap.av.alignment
790 protected void zappoColour_actionPerformed()
792 getGroup().cs = new ZappoColourScheme();
796 protected void taylorColour_actionPerformed()
798 getGroup().cs = new TaylorColourScheme();
802 protected void hydrophobicityColour_actionPerformed()
804 getGroup().cs = new HydrophobicColourScheme();
808 protected void helixColour_actionPerformed()
810 getGroup().cs = new HelixColourScheme();
814 protected void strandColour_actionPerformed()
816 getGroup().cs = new StrandColourScheme();
820 protected void turnColour_actionPerformed()
822 getGroup().cs = new TurnColourScheme();
826 protected void buriedColour_actionPerformed()
828 getGroup().cs = new BuriedColourScheme();
832 public void nucleotideMenuItem_actionPerformed()
834 getGroup().cs = new NucleotideColourScheme();
838 protected void abovePIDColour_itemStateChanged()
840 SequenceGroup sg = getGroup();
846 if (abovePIDColour.getState())
848 sg.cs.setConsensus(AAFrequency.calculate(sg
849 .getSequences(ap.av.hiddenRepSequences), 0, ap.av.alignment
851 int threshold = SliderPanel.setPIDSliderSource(ap, sg.cs, getGroup()
854 sg.cs.setThreshold(threshold, ap.av.getIgnoreGapsConsensus());
856 SliderPanel.showPIDSlider();
860 // remove PIDColouring
862 sg.cs.setThreshold(0, ap.av.getIgnoreGapsConsensus());
869 protected void userDefinedColour_actionPerformed()
871 new UserDefinedColours(ap, getGroup());
874 protected void PIDColour_actionPerformed()
876 SequenceGroup sg = getGroup();
877 sg.cs = new PIDColourScheme();
878 sg.cs.setConsensus(AAFrequency.calculate(sg
879 .getSequences(ap.av.hiddenRepSequences), 0, ap.av.alignment
884 protected void BLOSUM62Colour_actionPerformed()
886 SequenceGroup sg = getGroup();
888 sg.cs = new Blosum62ColourScheme();
890 sg.cs.setConsensus(AAFrequency.calculate(sg
891 .getSequences(ap.av.hiddenRepSequences), 0, ap.av.alignment
897 protected void noColourmenuItem_actionPerformed()
899 getGroup().cs = null;
903 protected void conservationMenuItem_itemStateChanged()
905 SequenceGroup sg = getGroup();
911 if (conservationMenuItem.getState())
914 Conservation c = new Conservation("Group",
915 ResidueProperties.propHash, 3, sg
916 .getSequences(ap.av.hiddenRepSequences), 0,
917 ap.av.alignment.getWidth());
920 c.verdict(false, ap.av.ConsPercGaps);
922 sg.cs.setConservation(c);
924 SliderPanel.setConservationSlider(ap, sg.cs, sg.getName());
925 SliderPanel.showConservationSlider();
928 // remove ConservationColouring
930 sg.cs.setConservation(null);
936 SequenceGroup getGroup()
938 SequenceGroup sg = ap.av.getSelectionGroup();
940 // this method won't add a new group if it already exists
943 ap.av.alignment.addGroup(sg);
949 void unGroupMenuItem_actionPerformed()
951 SequenceGroup sg = ap.av.getSelectionGroup();
952 ap.av.alignment.deleteGroup(sg);
953 ap.av.setSelectionGroup(null);
954 ap.paintAlignment(true);
957 public void showColourText_itemStateChanged()
959 getGroup().setColourText(showColourText.getState());
963 public void showText_itemStateChanged()
965 getGroup().setDisplayText(showText.getState());
969 public void showBoxes_itemStateChanged()
971 getGroup().setDisplayBoxes(showBoxes.getState());
975 void hideSequences(boolean representGroup)
977 SequenceGroup sg = ap.av.getSelectionGroup();
978 if (sg == null || sg.getSize() < 1)
980 ap.av.hideSequence(new SequenceI[]
985 ap.av.setSelectionGroup(null);
989 ap.av.hideRepSequences(seq, sg);
994 int gsize = sg.getSize();
997 hseqs = new SequenceI[gsize];
1000 for (int i = 0; i < gsize; i++)
1002 hseqs[index++] = sg.getSequenceAt(i);
1005 ap.av.hideSequence(hseqs);