Use font.getName, not font.getFontName
[jalview.git] / src / jalview / appletgui / AlignFrame.java
1 /*\r
2  * Jalview - A Sequence Alignment Editor and Viewer\r
3  * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4  *\r
5  * This program is free software; you can redistribute it and/or\r
6  * modify it under the terms of the GNU General Public License\r
7  * as published by the Free Software Foundation; either version 2\r
8  * of the License, or (at your option) any later version.\r
9  *\r
10  * This program is distributed in the hope that it will be useful,\r
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.\r
14  *\r
15  * You should have received a copy of the GNU General Public License\r
16  * along with this program; if not, write to the Free Software\r
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18  */\r
19 \r
20 package jalview.appletgui;\r
21 \r
22 import jalview.jbappletgui.GAlignFrame;\r
23 import jalview.schemes.*;\r
24 import jalview.datamodel.*;\r
25 import jalview.analysis.*;\r
26 import jalview.io.*;\r
27 import java.awt.*;\r
28 import java.awt.event.*;\r
29 import java.util.*;\r
30 \r
31 public class AlignFrame\r
32     extends GAlignFrame\r
33 {\r
34   final AlignmentPanel alignPanel;\r
35   final AlignViewport viewport;\r
36   public static final int NEW_WINDOW_WIDTH = 700;\r
37   public static final int NEW_WINDOW_HEIGHT = 500;\r
38   jalview.bin.JalviewLite applet;\r
39 \r
40   public AlignFrame(AlignmentI al)\r
41   {\r
42     this.applet = null;\r
43     viewport = new AlignViewport(al, null);\r
44     alignPanel = new AlignmentPanel(this, viewport);\r
45     add(alignPanel);\r
46     alignPanel.validate();\r
47   }\r
48 \r
49   public AlignFrame(AlignmentI al, jalview.bin.JalviewLite applet)\r
50   {\r
51     this.applet = applet;\r
52     viewport = new AlignViewport(al, applet);\r
53     alignPanel = new AlignmentPanel(this, viewport);\r
54     add(alignPanel);\r
55     alignPanel.validate();\r
56 \r
57     String treeFile = applet.getParameter("treeFile");\r
58     TreePanel treepanel = null;\r
59     if (treeFile != null)\r
60     {\r
61       try\r
62       {\r
63         treeFile = treeFile;\r
64         jalview.io.NewickFile fin = new jalview.io.NewickFile(applet.\r
65             getCodeBase() + treeFile, "URL");\r
66         fin.parse();\r
67 \r
68         if (fin.getTree() != null)\r
69         {\r
70           TreePanel tp = null;\r
71           tp = new TreePanel(viewport, viewport.getAlignment().getSequences(),\r
72                              fin, "FromFile", applet.getCodeBase() + treeFile);\r
73           jalview.bin.JalviewLite.addFrame(tp, treeFile, 600, 500);\r
74           addTreeMenuItem(tp, treeFile);\r
75         }\r
76       }\r
77       catch (Exception ex)\r
78       {\r
79         ex.printStackTrace();\r
80       }\r
81     }\r
82 \r
83 \r
84   }\r
85 \r
86   public void inputText_actionPerformed(ActionEvent e)\r
87   {\r
88     CutAndPasteTransfer cap = new CutAndPasteTransfer(true);\r
89     Frame frame = new Frame();\r
90     frame.add(cap);\r
91     jalview.bin.JalviewLite.addFrame(frame, "Cut & Paste Input", 500, 500);\r
92   }\r
93 \r
94   protected void outputText_actionPerformed(ActionEvent e)\r
95   {\r
96     CutAndPasteTransfer cap = new CutAndPasteTransfer(false);\r
97     Frame frame = new Frame();\r
98     frame.add(cap);\r
99     jalview.bin.JalviewLite.addFrame(frame,\r
100                                      "Alignment output - " + e.getActionCommand(),\r
101                                      600, 500);\r
102     cap.setText(FormatAdapter.formatSequences(e.getActionCommand(),\r
103                                               viewport.getAlignment().\r
104                                               getSequences()));\r
105   }\r
106 \r
107   public void closeMenuItem_actionPerformed(ActionEvent e)\r
108   {\r
109     dispose();\r
110   }\r
111 \r
112   Stack historyList = new Stack();\r
113   Stack redoList = new Stack();\r
114 \r
115   void updateEditMenuBar()\r
116   {\r
117     if (historyList.size() > 0)\r
118     {\r
119       undoMenuItem.setEnabled(true);\r
120       HistoryItem hi = (HistoryItem) historyList.peek();\r
121       undoMenuItem.setLabel("Undo " + hi.getDescription());\r
122     }\r
123     else\r
124     {\r
125       undoMenuItem.setEnabled(false);\r
126       undoMenuItem.setLabel("Undo");\r
127     }\r
128 \r
129     if (redoList.size() > 0)\r
130     {\r
131       redoMenuItem.setEnabled(true);\r
132       HistoryItem hi = (HistoryItem) redoList.peek();\r
133       redoMenuItem.setLabel("Redo " + hi.getDescription());\r
134     }\r
135     else\r
136     {\r
137       redoMenuItem.setEnabled(false);\r
138       redoMenuItem.setLabel("Redo");\r
139     }\r
140   }\r
141 \r
142   public void addHistoryItem(HistoryItem hi)\r
143   {\r
144     historyList.push(hi);\r
145     updateEditMenuBar();\r
146   }\r
147 \r
148   protected void undoMenuItem_actionPerformed(ActionEvent e)\r
149   {\r
150     HistoryItem hi = (HistoryItem) historyList.pop();\r
151     redoList.push(new HistoryItem(hi.getDescription(), viewport.alignment,\r
152                                   HistoryItem.HIDE));\r
153     restoreHistoryItem(hi);\r
154   }\r
155 \r
156   protected void redoMenuItem_actionPerformed(ActionEvent e)\r
157   {\r
158     HistoryItem hi = (HistoryItem) redoList.pop();\r
159     restoreHistoryItem(hi);\r
160     updateEditMenuBar();\r
161     viewport.updateConsensus();\r
162     alignPanel.repaint();\r
163     alignPanel.repaint();\r
164   }\r
165 \r
166   // used by undo and redo\r
167   void restoreHistoryItem(HistoryItem hi)\r
168   {\r
169     if (hi.getType() == HistoryItem.SORT)\r
170     {\r
171       for (int i = 0; i < hi.getSequences().size(); i++)\r
172       {\r
173         viewport.alignment.getSequences().setElementAt(hi.getSequences().\r
174             elementAt(i), i);\r
175       }\r
176     }\r
177     else\r
178     {\r
179       for (int i = 0; i < hi.getSequences().size(); i++)\r
180       {\r
181         SequenceI restore = (SequenceI) hi.getSequences().elementAt(i);\r
182         if (restore.getLength() == 0)\r
183         {\r
184           // log.System.out.println(hi.getHidden().elementAt(i));\r
185           restore.setSequence(hi.getHidden().elementAt(i).toString());\r
186           viewport.alignment.getSequences().insertElementAt(\r
187               restore,\r
188               hi.getAlignIndex(i));\r
189         }\r
190         else\r
191         {\r
192           restore.setSequence(hi.getHidden().elementAt(i).toString());\r
193         }\r
194       }\r
195       if (hi.getType() == HistoryItem.PASTE)\r
196       {\r
197         for (int i = viewport.alignment.getHeight() - 1;\r
198              i > hi.getSequences().size() - 1; i--)\r
199         {\r
200           viewport.alignment.deleteSequence(i);\r
201         }\r
202       }\r
203     }\r
204 \r
205     updateEditMenuBar();\r
206 \r
207     viewport.updateConsensus();\r
208     viewport.updateConservation();\r
209     alignPanel.repaint();\r
210   }\r
211 \r
212   public void moveSelectedSequences(boolean up)\r
213   {\r
214     SequenceGroup sg = viewport.getSelectionGroup();\r
215     if (sg == null)\r
216     {\r
217       return;\r
218     }\r
219 \r
220     if (up)\r
221     {\r
222       for (int i = 1; i < viewport.alignment.getHeight(); i++)\r
223       {\r
224         SequenceI seq = viewport.alignment.getSequenceAt(i);\r
225         if (!sg.sequences.contains(seq))\r
226         {\r
227           continue;\r
228         }\r
229 \r
230         SequenceI temp = viewport.alignment.getSequenceAt(i - 1);\r
231         if (sg.sequences.contains(temp))\r
232         {\r
233           continue;\r
234         }\r
235 \r
236         viewport.alignment.getSequences().setElementAt(temp, i);\r
237         viewport.alignment.getSequences().setElementAt(seq, i - 1);\r
238       }\r
239     }\r
240     else\r
241     {\r
242       for (int i = viewport.alignment.getHeight() - 2; i > -1; i--)\r
243       {\r
244         SequenceI seq = viewport.alignment.getSequenceAt(i);\r
245         if (!sg.sequences.contains(seq))\r
246         {\r
247           continue;\r
248         }\r
249 \r
250         SequenceI temp = viewport.alignment.getSequenceAt(i + 1);\r
251         if (sg.sequences.contains(temp))\r
252         {\r
253           continue;\r
254         }\r
255 \r
256         viewport.alignment.getSequences().setElementAt(temp, i);\r
257         viewport.alignment.getSequences().setElementAt(seq, i + 1);\r
258       }\r
259     }\r
260 \r
261     alignPanel.repaint();\r
262   }\r
263 \r
264   StringBuffer copiedSequences;\r
265   protected void copy_actionPerformed(ActionEvent e)\r
266   {\r
267     if (viewport.getSelectionGroup() == null)\r
268     {\r
269       return;\r
270     }\r
271 \r
272     SequenceGroup sg = viewport.getSelectionGroup();\r
273     copiedSequences = new StringBuffer();\r
274     Hashtable orderedSeqs = new Hashtable();\r
275     for (int i = 0; i < sg.getSize(); i++)\r
276     {\r
277       SequenceI seq = sg.getSequenceAt(i);\r
278       int index = viewport.alignment.findIndex(seq);\r
279       orderedSeqs.put(index + "", seq);\r
280     }\r
281 \r
282     int index = 0, startRes, endRes;\r
283     char ch;\r
284 \r
285     for (int i = 0; i < sg.getSize(); i++)\r
286     {\r
287         SequenceI seq = null;\r
288 \r
289         while (seq == null)\r
290         {\r
291             if (orderedSeqs.containsKey(index + ""))\r
292             {\r
293                 seq = (SequenceI) orderedSeqs.get(index + "");\r
294                 index++;\r
295 \r
296                 break;\r
297             }\r
298             else\r
299             {\r
300                 index++;\r
301             }\r
302         }\r
303 \r
304         //FIND START RES\r
305         //Returns residue following index if gap\r
306         startRes = seq.findPosition(sg.getStartRes());\r
307 \r
308         //FIND END RES\r
309         //Need to find the residue preceeding index if gap\r
310         endRes = 0;\r
311 \r
312         for (int j = 0; j < sg.getEndRes()+1 && j < seq.getLength(); j++)\r
313         {\r
314           ch = seq.getCharAt(j);\r
315           if (!jalview.util.Comparison.isGap( (ch)))\r
316           {\r
317             endRes++;\r
318           }\r
319         }\r
320 \r
321         if(endRes>0)\r
322         {\r
323           endRes += seq.getStart() -1;\r
324         }\r
325 \r
326         copiedSequences.append(seq.getName() + "\t" +\r
327             startRes + "\t" +\r
328             endRes + "\t" +\r
329             seq.getSequence(sg.getStartRes(),\r
330                 sg.getEndRes() + 1) + "\n");\r
331     }\r
332 \r
333   }\r
334 \r
335   protected void pasteNew_actionPerformed(ActionEvent e)\r
336   {\r
337     paste(true);\r
338   }\r
339 \r
340   protected void pasteThis_actionPerformed(ActionEvent e)\r
341   {\r
342     addHistoryItem(new HistoryItem("Paste Sequences", viewport.alignment,\r
343                                    HistoryItem.PASTE));\r
344     paste(false);\r
345   }\r
346 \r
347   void paste(boolean newAlignment)\r
348   {\r
349     try\r
350     {\r
351       if (copiedSequences == null)\r
352       {\r
353         return;\r
354       }\r
355 \r
356       StringTokenizer st = new StringTokenizer(copiedSequences.toString());\r
357       Vector seqs = new Vector();\r
358       while (st.hasMoreElements())\r
359       {\r
360         String name = st.nextToken();\r
361         int start = Integer.parseInt(st.nextToken());\r
362         int end = Integer.parseInt(st.nextToken());\r
363         Sequence sequence = new Sequence(name, st.nextToken(), start, end);\r
364 \r
365         if (!newAlignment)\r
366         {\r
367           viewport.alignment.addSequence(sequence);\r
368         }\r
369         else\r
370         {\r
371           seqs.addElement(sequence);\r
372         }\r
373       }\r
374 \r
375       if (newAlignment)\r
376       {\r
377         SequenceI[] newSeqs = new SequenceI[seqs.size()];\r
378         for (int i = 0; i < seqs.size(); i++)\r
379         {\r
380           newSeqs[i] = (SequenceI) seqs.elementAt(i);\r
381         }\r
382 \r
383         AlignFrame af = new AlignFrame(new Alignment(newSeqs), applet);\r
384         String newtitle = new String("Copied sequences");\r
385         if (getTitle().startsWith("Copied sequences"))\r
386         {\r
387           newtitle = getTitle();\r
388         }\r
389         else\r
390         {\r
391           newtitle = newtitle.concat("- from " + getTitle());\r
392         }\r
393         jalview.bin.JalviewLite.addFrame(af, newtitle, NEW_WINDOW_WIDTH,\r
394                                          NEW_WINDOW_HEIGHT);\r
395       }\r
396       else\r
397       {\r
398         viewport.setEndSeq(viewport.alignment.getHeight());\r
399         viewport.alignment.getWidth();\r
400         viewport.updateConservation();\r
401         viewport.updateConsensus();\r
402         alignPanel.repaint();\r
403       }\r
404 \r
405     }\r
406     catch (Exception ex)\r
407     {} // could be anything being pasted in here\r
408 \r
409   }\r
410 \r
411   protected void cut_actionPerformed(ActionEvent e)\r
412   {\r
413     copy_actionPerformed(null);\r
414     delete_actionPerformed(null);\r
415   }\r
416 \r
417   protected void delete_actionPerformed(ActionEvent e)\r
418   {\r
419     addHistoryItem(new HistoryItem("Delete Sequences", viewport.alignment,\r
420                                    HistoryItem.HIDE));\r
421     if (viewport.getSelectionGroup() == null)\r
422     {\r
423       return;\r
424     }\r
425 \r
426 \r
427     SequenceGroup sg = viewport.getSelectionGroup();\r
428     boolean allSequences = false;\r
429     if(sg.sequences.size()==viewport.alignment.getHeight())\r
430           allSequences = true;\r
431 \r
432     for (int i = 0; i < sg.sequences.size(); i++)\r
433     {\r
434       SequenceI seq = sg.getSequenceAt(i);\r
435       int index = viewport.getAlignment().findIndex(seq);\r
436       seq.deleteChars(sg.getStartRes(), sg.getEndRes() + 1);\r
437 \r
438       // If the cut affects all sequences, remove highlighted columns\r
439       if (allSequences)\r
440       {\r
441         viewport.getColumnSelection().removeElements(sg.getStartRes(),\r
442                                                      sg.getEndRes() + 1);\r
443       }\r
444 \r
445 \r
446       if (seq.getSequence().length() < 1)\r
447       {\r
448         viewport.getAlignment().deleteSequence(seq);\r
449       }\r
450       else\r
451       {\r
452         viewport.getAlignment().getSequences().setElementAt(seq, index);\r
453       }\r
454     }\r
455 \r
456     viewport.setSelectionGroup(null);\r
457     viewport.alignment.deleteGroup(sg);\r
458     viewport.resetSeqLimits(alignPanel.seqPanel.seqCanvas.getSize().height);\r
459     if (viewport.getAlignment().getHeight() < 1)\r
460     {\r
461       try\r
462       {\r
463         this.setVisible(false);\r
464       }\r
465       catch (Exception ex)\r
466       {}\r
467     }\r
468     viewport.updateConservation();\r
469     viewport.updateConsensus();\r
470     alignPanel.repaint();\r
471 \r
472   }\r
473 \r
474   protected void deleteGroups_actionPerformed(ActionEvent e)\r
475   {\r
476     viewport.alignment.deleteAllGroups();\r
477     viewport.setSelectionGroup(null);\r
478 \r
479     alignPanel.repaint();\r
480   }\r
481 \r
482   public void selectAllSequenceMenuItem_actionPerformed(ActionEvent e)\r
483   {\r
484     SequenceGroup sg = new SequenceGroup();\r
485     for (int i = 0; i < viewport.getAlignment().getSequences().size(); i++)\r
486     {\r
487       sg.addSequence(viewport.getAlignment().getSequenceAt(i), false);\r
488     }\r
489     sg.setEndRes(viewport.alignment.getWidth());\r
490     viewport.setSelectionGroup(sg);\r
491     alignPanel.repaint();\r
492     PaintRefresher.Refresh(null);\r
493   }\r
494 \r
495   public void deselectAllSequenceMenuItem_actionPerformed(ActionEvent e)\r
496   {\r
497     viewport.setSelectionGroup(null);\r
498     viewport.getColumnSelection().clear();\r
499     viewport.setSelectionGroup(null);\r
500     PaintRefresher.Refresh(null);\r
501   }\r
502 \r
503   public void invertSequenceMenuItem_actionPerformed(ActionEvent e)\r
504   {\r
505     SequenceGroup sg = viewport.getSelectionGroup();\r
506     for (int i = 0; i < viewport.getAlignment().getSequences().size(); i++)\r
507     {\r
508       sg.addOrRemove(viewport.getAlignment().getSequenceAt(i), false);\r
509     }\r
510 \r
511     PaintRefresher.Refresh(null);\r
512   }\r
513 \r
514   public void remove2LeftMenuItem_actionPerformed(ActionEvent e)\r
515   {\r
516     ColumnSelection colSel = viewport.getColumnSelection();\r
517     if (colSel.size() > 0)\r
518     {\r
519       addHistoryItem(new HistoryItem("Remove Left", viewport.alignment,\r
520                                      HistoryItem.HIDE));\r
521       int min = colSel.getMin();\r
522       viewport.getAlignment().trimLeft(min);\r
523       colSel.compensateForEdit(0, min);\r
524 \r
525       if (viewport.getSelectionGroup() != null)\r
526       {\r
527         viewport.getSelectionGroup().adjustForRemoveLeft(min);\r
528       }\r
529 \r
530       Vector groups = viewport.alignment.getGroups();\r
531       for (int i = 0; i < groups.size(); i++)\r
532       {\r
533         SequenceGroup sg = (SequenceGroup) groups.elementAt(i);\r
534         if (!sg.adjustForRemoveLeft(min))\r
535         {\r
536           viewport.alignment.deleteGroup(sg);\r
537         }\r
538       }\r
539 \r
540       alignPanel.repaint();\r
541     }\r
542   }\r
543 \r
544   public void remove2RightMenuItem_actionPerformed(ActionEvent e)\r
545   {\r
546     ColumnSelection colSel = viewport.getColumnSelection();\r
547     if (colSel.size() > 0)\r
548     {\r
549       addHistoryItem(new HistoryItem("Remove Right", viewport.alignment,\r
550                                      HistoryItem.HIDE));\r
551       int max = colSel.getMax();\r
552       viewport.getAlignment().trimRight(max);\r
553       if (viewport.getSelectionGroup() != null)\r
554       {\r
555         viewport.getSelectionGroup().adjustForRemoveRight(max);\r
556       }\r
557 \r
558       Vector groups = viewport.alignment.getGroups();\r
559       for (int i = 0; i < groups.size(); i++)\r
560       {\r
561         SequenceGroup sg = (SequenceGroup) groups.elementAt(i);\r
562         if (!sg.adjustForRemoveRight(max))\r
563         {\r
564           viewport.alignment.deleteGroup(sg);\r
565         }\r
566       }\r
567 \r
568       alignPanel.repaint();\r
569     }\r
570 \r
571   }\r
572 \r
573   public void removeGappedColumnMenuItem_actionPerformed(ActionEvent e)\r
574   {\r
575     addHistoryItem(new HistoryItem("Remove Gapped Columns",\r
576                                    viewport.alignment,\r
577                                    HistoryItem.HIDE));\r
578     viewport.getAlignment().removeGaps();\r
579     viewport.updateConservation();\r
580     viewport.updateConsensus();\r
581     alignPanel.repaint();\r
582   }\r
583 \r
584   public void removeAllGapsMenuItem_actionPerformed(ActionEvent e)\r
585   {\r
586     addHistoryItem(new HistoryItem("Remove Gaps", viewport.alignment,\r
587                                    HistoryItem.HIDE));\r
588 \r
589     SequenceI current;\r
590     int jSize;\r
591 \r
592     Vector seqs=null;\r
593 \r
594     int start=0, end = viewport.alignment.getWidth();\r
595 \r
596     if (viewport.getSelectionGroup() != null\r
597         && viewport.getSelectionGroup().sequences != null\r
598         && viewport.getSelectionGroup().sequences.size()>0)\r
599     {\r
600       seqs = viewport.getSelectionGroup().sequences;\r
601       start = viewport.getSelectionGroup().getStartRes();\r
602       end = viewport.getSelectionGroup().getEndRes();\r
603     }\r
604     else\r
605       seqs = viewport.alignment.getSequences();\r
606 \r
607     for (int i = 0; i <seqs.size(); i++)\r
608     {\r
609       current = (SequenceI)seqs.elementAt(i);\r
610       jSize = current.getLength();\r
611 \r
612       int j = start;\r
613       do\r
614       {\r
615         if (jalview.util.Comparison.isGap(current.getCharAt(j)))\r
616         {\r
617           current.deleteCharAt(j);\r
618           j--;\r
619           jSize--;\r
620         }\r
621         j++;\r
622       }\r
623       while(j < end && j < jSize) ;\r
624     }\r
625 \r
626     viewport.updateConservation();\r
627     viewport.updateConsensus();\r
628     alignPanel.repaint();\r
629   }\r
630 \r
631 \r
632   public void findMenuItem_actionPerformed(ActionEvent e)\r
633   {\r
634     Finder finder = new Finder(alignPanel);\r
635   }\r
636 \r
637   public void font_actionPerformed(ActionEvent e)\r
638   {\r
639     Frame frame = new Frame();\r
640     FontChooser fc = new FontChooser(alignPanel, frame);\r
641     frame.add(fc);\r
642     jalview.bin.JalviewLite.addFrame(frame, "Change Font", 440, 100);\r
643 \r
644   }\r
645 \r
646   protected void fullSeqId_actionPerformed(ActionEvent e)\r
647   {\r
648     viewport.setShowFullId(fullSeqId.getState());\r
649     alignPanel.fontChanged();\r
650     alignPanel.repaint();\r
651   }\r
652 \r
653   protected void colourTextMenuItem_actionPerformed(ActionEvent e)\r
654   {\r
655     viewport.setColourText(colourTextMenuItem.getState());\r
656     alignPanel.repaint();\r
657   }\r
658 \r
659   protected void wrapMenuItem_actionPerformed(ActionEvent e)\r
660   {\r
661     viewport.setWrapAlignment(wrapMenuItem.getState());\r
662     alignPanel.setWrapAlignment(wrapMenuItem.getState());\r
663     scaleAbove.setEnabled(wrapMenuItem.getState());\r
664     scaleLeft.setEnabled(wrapMenuItem.getState());\r
665     scaleRight.setEnabled(wrapMenuItem.getState());\r
666     alignPanel.repaint();\r
667   }\r
668 \r
669   protected void scaleAbove_actionPerformed(ActionEvent e)\r
670   {\r
671     viewport.setScaleAboveWrapped(scaleAbove.getState());\r
672     alignPanel.repaint();\r
673   }\r
674 \r
675   protected void scaleLeft_actionPerformed(ActionEvent e)\r
676   {\r
677     viewport.setScaleLeftWrapped(scaleLeft.getState());\r
678     alignPanel.repaint();\r
679   }\r
680 \r
681   protected void scaleRight_actionPerformed(ActionEvent e)\r
682   {\r
683     viewport.setScaleRightWrapped(scaleRight.getState());\r
684     alignPanel.repaint();\r
685   }\r
686 \r
687   public void viewBoxesMenuItem_actionPerformed(ActionEvent e)\r
688   {\r
689     viewport.setShowBoxes(viewBoxesMenuItem.getState());\r
690     alignPanel.repaint();\r
691   }\r
692 \r
693   public void viewTextMenuItem_actionPerformed(ActionEvent e)\r
694   {\r
695     viewport.setShowText(viewTextMenuItem.getState());\r
696     alignPanel.repaint();\r
697   }\r
698 \r
699   protected void renderGapsMenuItem_actionPerformed(ActionEvent e)\r
700   {\r
701     viewport.setRenderGaps(renderGapsMenuItem.getState());\r
702     alignPanel.repaint();\r
703   }\r
704 \r
705   public void annotationPanelMenuItem_actionPerformed(ActionEvent e)\r
706   {\r
707     if (annotationPanelMenuItem.getState() && viewport.getWrapAlignment())\r
708     {\r
709       annotationPanelMenuItem.setState(false);\r
710       return;\r
711     }\r
712     viewport.setShowAnnotation(annotationPanelMenuItem.getState());\r
713     alignPanel.setAnnotationVisible(annotationPanelMenuItem.getState());\r
714   }\r
715 \r
716   public void overviewMenuItem_actionPerformed(ActionEvent e)\r
717   {\r
718     if (alignPanel.overviewPanel != null)\r
719     {\r
720       return;\r
721     }\r
722 \r
723     Frame frame = new Frame();\r
724     OverviewPanel overview = new OverviewPanel(alignPanel);\r
725     frame.add(overview);\r
726     // +50 must allow for applet frame window\r
727     jalview.bin.JalviewLite.addFrame(frame, "Overview " + this.getTitle(),\r
728                                      overview.preferredSize().width,\r
729                                      overview.preferredSize().height + 50);\r
730 \r
731     frame.pack();\r
732     frame.addWindowListener(new WindowAdapter()\r
733     {\r
734       public void windowClosing(WindowEvent e)\r
735       {\r
736         alignPanel.setOverviewPanel(null);\r
737       };\r
738     });\r
739 \r
740     alignPanel.setOverviewPanel(overview);\r
741 \r
742   }\r
743 \r
744   protected void noColourmenuItem_actionPerformed(ActionEvent e)\r
745   {\r
746     changeColour(null);\r
747   }\r
748 \r
749   public void clustalColour_actionPerformed(ActionEvent e)\r
750   {\r
751     abovePIDThreshold.setState(false);\r
752     changeColour(new ClustalxColourScheme(viewport.alignment.getSequences(),\r
753                                           viewport.alignment.getWidth()));\r
754   }\r
755 \r
756   public void zappoColour_actionPerformed(ActionEvent e)\r
757   {\r
758     changeColour(new ZappoColourScheme());\r
759   }\r
760 \r
761   public void taylorColour_actionPerformed(ActionEvent e)\r
762   {\r
763     changeColour(new TaylorColourScheme());\r
764   }\r
765 \r
766   public void hydrophobicityColour_actionPerformed(ActionEvent e)\r
767   {\r
768     changeColour(new HydrophobicColourScheme());\r
769   }\r
770 \r
771   public void helixColour_actionPerformed(ActionEvent e)\r
772   {\r
773     changeColour(new HelixColourScheme());\r
774   }\r
775 \r
776   public void strandColour_actionPerformed(ActionEvent e)\r
777   {\r
778     changeColour(new StrandColourScheme());\r
779   }\r
780 \r
781   public void turnColour_actionPerformed(ActionEvent e)\r
782   {\r
783     changeColour(new TurnColourScheme());\r
784   }\r
785 \r
786   public void buriedColour_actionPerformed(ActionEvent e)\r
787   {\r
788     changeColour(new BuriedColourScheme());\r
789   }\r
790 \r
791   public void nucleotideColour_actionPerformed(ActionEvent e)\r
792   {\r
793     changeColour(new NucleotideColourScheme());\r
794   }\r
795 \r
796   protected void applyToAllGroups_actionPerformed(ActionEvent e)\r
797   {\r
798     viewport.setColourAppliesToAllGroups(applyToAllGroups.getState());\r
799   }\r
800 \r
801   void changeColour(ColourSchemeI cs)\r
802   {\r
803     int threshold = 0;\r
804 \r
805     if (viewport.getAbovePIDThreshold())\r
806     {\r
807       threshold = SliderPanel.setPIDSliderSource(alignPanel, cs, "Background");\r
808 \r
809       if (cs instanceof ResidueColourScheme)\r
810       {\r
811         ( (ResidueColourScheme) cs).setThreshold(threshold);\r
812       }\r
813 \r
814       viewport.setGlobalColourScheme(cs);\r
815     }\r
816     else if (cs instanceof ResidueColourScheme)\r
817     {\r
818       ( (ResidueColourScheme) cs).setThreshold(0);\r
819     }\r
820 \r
821     if (viewport.getConservationSelected())\r
822     {\r
823       ConservationColourScheme ccs = null;\r
824 \r
825       Alignment al = (Alignment) viewport.alignment;\r
826       Conservation c = new Conservation("All",\r
827                                         ResidueProperties.propHash, 3,\r
828                                         al.getSequences(), 0,\r
829                                         al.getWidth() - 1);\r
830 \r
831       c.calculate();\r
832       c.verdict(false, viewport.ConsPercGaps);\r
833 \r
834       ccs = new ConservationColourScheme(c, cs);\r
835 \r
836       // MUST NOTIFY THE COLOURSCHEME OF CONSENSUS!\r
837       ccs.setConsensus(viewport.vconsensus);\r
838       viewport.setGlobalColourScheme(ccs);\r
839 \r
840       SliderPanel.setConservationSlider(alignPanel, ccs, "Background");\r
841 \r
842     }\r
843     else\r
844     {\r
845       // MUST NOTIFY THE COLOURSCHEME OF CONSENSUS!\r
846       if (cs != null)\r
847       {\r
848         cs.setConsensus(viewport.vconsensus);\r
849       }\r
850       viewport.setGlobalColourScheme(cs);\r
851     }\r
852 \r
853     if (viewport.getColourAppliesToAllGroups())\r
854     {\r
855       Vector groups = viewport.alignment.getGroups();\r
856       for (int i = 0; i < groups.size(); i++)\r
857       {\r
858         SequenceGroup sg = (SequenceGroup) groups.elementAt(i);\r
859 \r
860         if(cs==null)\r
861         {\r
862           sg.cs = null;\r
863           continue;\r
864         }\r
865         if (cs instanceof ClustalxColourScheme)\r
866         {\r
867           ClustalxColourScheme cxs = (ClustalxColourScheme) cs;\r
868           cxs.resetClustalX(viewport.alignment.getSequences(), viewport.alignment.getWidth());\r
869           sg.cs = cxs;\r
870         }\r
871         else\r
872         {\r
873           try\r
874           {\r
875             sg.cs = (ColourSchemeI) cs.getClass().newInstance();\r
876           }\r
877           catch (Exception ex)\r
878           {\r
879             ex.printStackTrace();\r
880             sg.cs = cs;\r
881           }\r
882         }\r
883 \r
884         if (viewport.getAbovePIDThreshold())\r
885         {\r
886           if (sg.cs instanceof ResidueColourScheme)\r
887           {\r
888             ( (ResidueColourScheme) sg.cs).setThreshold(threshold);\r
889           }\r
890 \r
891           sg.cs.setConsensus(AAFrequency.calculate(sg.sequences, 0, sg.getWidth()));\r
892         }\r
893 \r
894         if (viewport.getConservationSelected())\r
895         {\r
896           Conservation c = new Conservation("Group",\r
897                                             ResidueProperties.propHash, 3,\r
898                                             sg.sequences, 0,\r
899                                             viewport.alignment.getWidth() - 1);\r
900           c.calculate();\r
901           c.verdict(false, viewport.ConsPercGaps);\r
902           ConservationColourScheme ccs = new ConservationColourScheme(c, sg.cs);\r
903 \r
904           // MUST NOTIFY THE COLOURSCHEME OF CONSENSUS!\r
905           ccs.setConsensus(AAFrequency.calculate(sg.sequences, 0, sg.getWidth()));\r
906           sg.cs = ccs;\r
907         }\r
908         else\r
909         {\r
910           // MUST NOTIFY THE COLOURSCHEME OF CONSENSUS!\r
911           sg.cs.setConsensus(AAFrequency.calculate(sg.sequences, 0, sg.getWidth()));\r
912         }\r
913 \r
914       }\r
915     }\r
916 \r
917     if (alignPanel.getOverviewPanel() != null)\r
918     {\r
919       alignPanel.getOverviewPanel().updateOverviewImage();\r
920     }\r
921 \r
922     alignPanel.repaint();\r
923   }\r
924 \r
925   protected void modifyPID_actionPerformed(ActionEvent e)\r
926   {\r
927     if (viewport.getAbovePIDThreshold())\r
928     {\r
929       SliderPanel.setPIDSliderSource(alignPanel, viewport.getGlobalColourScheme(),\r
930                                      "Background");\r
931       SliderPanel.showPIDSlider();\r
932     }\r
933   }\r
934 \r
935   protected void modifyConservation_actionPerformed(ActionEvent e)\r
936   {\r
937     if (viewport.getConservationSelected())\r
938     {\r
939       SliderPanel.setConservationSlider(alignPanel, viewport.globalColourScheme,\r
940                                         "Background");\r
941       SliderPanel.showConservationSlider();\r
942     }\r
943   }\r
944 \r
945   protected void conservationMenuItem_actionPerformed(ActionEvent e)\r
946   {\r
947     viewport.setConservationSelected(conservationMenuItem.getState());\r
948 \r
949     viewport.setAbovePIDThreshold(false);\r
950     abovePIDThreshold.setState(false);\r
951 \r
952     ColourSchemeI cs = viewport.getGlobalColourScheme();\r
953     if (cs instanceof ConservationColourScheme)\r
954     {\r
955       changeColour( ( (ConservationColourScheme) cs).cs);\r
956     }\r
957     else\r
958     {\r
959       changeColour(cs);\r
960     }\r
961 \r
962     modifyConservation_actionPerformed(null);\r
963   }\r
964 \r
965   public void abovePIDThreshold_actionPerformed(ActionEvent e)\r
966   {\r
967     viewport.setAbovePIDThreshold(abovePIDThreshold.getState());\r
968 \r
969     conservationMenuItem.setState(false);\r
970     viewport.setConservationSelected(false);\r
971 \r
972     ColourSchemeI cs = viewport.getGlobalColourScheme();\r
973 \r
974     if (cs instanceof ConservationColourScheme)\r
975     {\r
976       changeColour( ( (ConservationColourScheme) cs).cs);\r
977     }\r
978     else\r
979     {\r
980       changeColour(cs);\r
981     }\r
982 \r
983     modifyPID_actionPerformed(null);\r
984   }\r
985 \r
986   public void userDefinedColour_actionPerformed(ActionEvent e)\r
987   {\r
988     UserDefinedColours chooser = new UserDefinedColours(alignPanel, null);\r
989   }\r
990 \r
991   public void PIDColour_actionPerformed(ActionEvent e)\r
992   {\r
993     changeColour(new PIDColourScheme());\r
994   }\r
995 \r
996   public void BLOSUM62Colour_actionPerformed(ActionEvent e)\r
997   {\r
998     changeColour(new Blosum62ColourScheme());\r
999   }\r
1000 \r
1001   public void sortPairwiseMenuItem_actionPerformed(ActionEvent e)\r
1002   {\r
1003     addHistoryItem(new HistoryItem("Pairwise Sort", viewport.alignment,\r
1004                                    HistoryItem.SORT));\r
1005     AlignmentSorter.sortByPID(viewport.getAlignment(),\r
1006                               viewport.getAlignment().getSequenceAt(0));\r
1007     alignPanel.repaint();\r
1008   }\r
1009 \r
1010   public void sortIDMenuItem_actionPerformed(ActionEvent e)\r
1011   {\r
1012     addHistoryItem(new HistoryItem("ID Sort", viewport.alignment,\r
1013                                    HistoryItem.SORT));\r
1014     AlignmentSorter.sortByID(viewport.getAlignment());\r
1015     alignPanel.repaint();\r
1016   }\r
1017 \r
1018   public void sortGroupMenuItem_actionPerformed(ActionEvent e)\r
1019   {\r
1020     addHistoryItem(new HistoryItem("Group Sort", viewport.alignment,\r
1021                                    HistoryItem.SORT));\r
1022     AlignmentSorter.sortByGroup(viewport.getAlignment());\r
1023     alignPanel.repaint();\r
1024 \r
1025   }\r
1026 \r
1027   public void removeRedundancyMenuItem_actionPerformed(ActionEvent e)\r
1028   {\r
1029     RedundancyPanel sp = new RedundancyPanel(alignPanel);\r
1030     Frame frame = new Frame();\r
1031     frame.add(sp);\r
1032     jalview.bin.JalviewLite.addFrame(frame, "Redundancy threshold selection",\r
1033                                      400, 120);\r
1034 \r
1035   }\r
1036 \r
1037   public void pairwiseAlignmentMenuItem_actionPerformed(ActionEvent e)\r
1038   {\r
1039     if (viewport.getSelectionGroup().getSize() > 1)\r
1040     {\r
1041       Frame frame = new Frame();\r
1042       frame.add(new PairwiseAlignPanel(viewport));\r
1043       jalview.bin.JalviewLite.addFrame(frame, "Pairwise Alignment", 600, 500);\r
1044     }\r
1045   }\r
1046 \r
1047   public void PCAMenuItem_actionPerformed(ActionEvent e)\r
1048   {\r
1049 \r
1050     if ( (viewport.getSelectionGroup() != null &&\r
1051           viewport.getSelectionGroup().getSize() < 4 &&\r
1052           viewport.getSelectionGroup().getSize() > 0)\r
1053         || viewport.getAlignment().getHeight() < 4)\r
1054     {\r
1055       return;\r
1056     }\r
1057 \r
1058     try\r
1059     {\r
1060       PCAPanel pcaPanel = new PCAPanel(viewport, null);\r
1061       Frame frame = new Frame();\r
1062       frame.add(pcaPanel);\r
1063       jalview.bin.JalviewLite.addFrame(frame, "Principal component analysis",\r
1064                                        400, 400);\r
1065     }\r
1066     catch (java.lang.OutOfMemoryError ex)\r
1067     {\r
1068     }\r
1069 \r
1070   }\r
1071 \r
1072   public void averageDistanceTreeMenuItem_actionPerformed(ActionEvent e)\r
1073   {\r
1074     NewTreePanel("AV", "PID", "Average distance tree using PID");\r
1075   }\r
1076 \r
1077   public void neighbourTreeMenuItem_actionPerformed(ActionEvent e)\r
1078   {\r
1079     NewTreePanel("NJ", "PID", "Neighbour joining tree using PID");\r
1080   }\r
1081 \r
1082   protected void njTreeBlosumMenuItem_actionPerformed(ActionEvent e)\r
1083   {\r
1084     NewTreePanel("NJ", "BL", "Neighbour joining tree using BLOSUM62");\r
1085   }\r
1086 \r
1087   protected void avTreeBlosumMenuItem_actionPerformed(ActionEvent e)\r
1088   {\r
1089     NewTreePanel("AV", "BL", "Average distance tree using BLOSUM62PID");\r
1090   }\r
1091 \r
1092   void NewTreePanel(String type, String pwType, String title)\r
1093   {\r
1094     //are the sequences aligned?\r
1095     if (!viewport.alignment.isAligned())\r
1096     {\r
1097       SequenceI current;\r
1098       int Width = viewport.getAlignment().getWidth();\r
1099 \r
1100       for (int i = 0; i < viewport.getAlignment().getSequences().size();\r
1101            i++)\r
1102       {\r
1103         current = viewport.getAlignment().getSequenceAt(i);\r
1104 \r
1105         if (current.getLength() < Width)\r
1106         {\r
1107           current.insertCharAt(Width - 1, viewport.getGapCharacter());\r
1108         }\r
1109       }\r
1110       alignPanel.repaint();\r
1111 \r
1112     }\r
1113 \r
1114     final TreePanel tp;\r
1115     if (viewport.getSelectionGroup() != null &&\r
1116         viewport.getSelectionGroup().getSize() > 3)\r
1117     {\r
1118       tp = new TreePanel(viewport, viewport.getSelectionGroup().sequences, type,\r
1119                          pwType,\r
1120                          0, viewport.alignment.getWidth());\r
1121     }\r
1122     else\r
1123     {\r
1124       tp = new TreePanel(viewport, viewport.getAlignment().getSequences(),\r
1125                          type, pwType, 0, viewport.alignment.getWidth());\r
1126     }\r
1127 \r
1128     addTreeMenuItem(tp, title);\r
1129 \r
1130     jalview.bin.JalviewLite.addFrame(tp, title, 600, 500);\r
1131   }\r
1132 \r
1133   void addTreeMenuItem(final TreePanel treePanel, String title)\r
1134   {\r
1135     final MenuItem item = new MenuItem(title);\r
1136     sortByTreeMenu.add(item);\r
1137     item.addActionListener(new java.awt.event.ActionListener()\r
1138     {\r
1139       public void actionPerformed(ActionEvent e)\r
1140       {\r
1141         addHistoryItem(new HistoryItem("Sort", viewport.alignment,\r
1142                                        HistoryItem.SORT));\r
1143         AlignmentSorter.sortByTree(viewport.getAlignment(), treePanel.getTree());\r
1144         alignPanel.repaint();\r
1145       }\r
1146     });\r
1147 \r
1148     treePanel.addWindowListener(new WindowAdapter()\r
1149     {\r
1150       public void windowClosing(WindowEvent e)\r
1151       {\r
1152         sortByTreeMenu.remove(item);\r
1153       };\r
1154     });\r
1155   }\r
1156 \r
1157   protected void documentation_actionPerformed(ActionEvent e)\r
1158   {\r
1159     jalview.bin.JalviewLite.showURL("http://www.jalview.org/help.html");\r
1160   }\r
1161 \r
1162   protected void about_actionPerformed(ActionEvent e)\r
1163   {\r
1164     class AboutPanel\r
1165         extends Canvas\r
1166     {\r
1167       public void paint(Graphics g)\r
1168       {\r
1169         g.setColor(Color.white);\r
1170         g.fillRect(0, 0, getSize().width, getSize().height);\r
1171         g.setFont(new Font("Helvetica", Font.PLAIN, 12));\r
1172         FontMetrics fm = g.getFontMetrics();\r
1173         int fh = fm.getHeight();\r
1174         int y = 5, x = 7;\r
1175         g.setColor(Color.black);\r
1176         g.setFont(new Font("Helvetica", Font.BOLD, 14));\r
1177         g.drawString("JalView - Release 2.01", 200, y += fh);\r
1178         g.setFont(new Font("Helvetica", Font.PLAIN, 12));\r
1179         g.drawString("Authors:  Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton.",\r
1180                      x, y += fh * 2);\r
1181         g.drawString("Current development managed by Andrew Waterhouse; Barton Group, University of Dundee.",\r
1182                      x, y += fh);\r
1183         g.drawString(\r
1184             "For any issues relating to Jalview, email help@jalview.org", x,\r
1185             y += fh);\r
1186         g.drawString("If  you use JalView, please cite:", x, y += fh + 8);\r
1187         g.drawString("\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"",\r
1188                      x, y += fh);\r
1189         g.drawString("Bioinformatics,  2004 12;426-7.", x, y += fh);\r
1190       }\r
1191     }\r
1192 \r
1193     Frame frame = new Frame();\r
1194     frame.add(new AboutPanel());\r
1195     jalview.bin.JalviewLite.addFrame(frame, "Jalview", 580, 200);\r
1196 \r
1197   }\r
1198 }\r