1ee83930309ac5fef6711f9a428c803a9c6828b6
[jalview.git] / src / jalview / appletgui / CutAndPasteTransfer.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.appletgui;
22
23 import jalview.analysis.AlignmentUtils;
24 import jalview.api.ComplexAlignFile;
25 import jalview.bin.JalviewLite;
26 import jalview.datamodel.AlignmentI;
27 import jalview.datamodel.ColumnSelection;
28 import jalview.datamodel.PDBEntry;
29 import jalview.datamodel.SequenceI;
30 import jalview.io.AnnotationFile;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.FileParse;
33 import jalview.io.IdentifyFile;
34 import jalview.io.NewickFile;
35 import jalview.io.TCoffeeScoreFile;
36 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
37 import jalview.schemes.ColourSchemeI;
38 import jalview.schemes.TCoffeeColourScheme;
39 import jalview.util.MessageManager;
40
41 import java.awt.BorderLayout;
42 import java.awt.Button;
43 import java.awt.Dialog;
44 import java.awt.Font;
45 import java.awt.Frame;
46 import java.awt.Label;
47 import java.awt.Panel;
48 import java.awt.TextArea;
49 import java.awt.event.ActionEvent;
50 import java.awt.event.ActionListener;
51 import java.awt.event.MouseEvent;
52 import java.awt.event.MouseListener;
53
54 public class CutAndPasteTransfer extends Panel implements ActionListener,
55         MouseListener
56 {
57   boolean pdbImport = false;
58
59   boolean treeImport = false;
60
61   boolean annotationImport = false;
62
63   SequenceI seq;
64
65   AlignFrame alignFrame;
66
67   FileParse source = null;
68
69   public CutAndPasteTransfer(boolean forImport, AlignFrame alignFrame)
70   {
71     try
72     {
73       jbInit();
74     } catch (Exception e)
75     {
76       e.printStackTrace();
77     }
78
79     this.alignFrame = alignFrame;
80
81     if (!forImport)
82     {
83       buttonPanel.setVisible(false);
84     }
85   }
86
87   public String getText()
88   {
89     return textarea.getText();
90   }
91
92   public void setText(String text)
93   {
94     textarea.setText(text);
95   }
96
97   public void setPDBImport(SequenceI seq)
98   {
99     this.seq = seq;
100     accept.setLabel(MessageManager.getString("action.accept"));
101     addSequences.setVisible(false);
102     pdbImport = true;
103   }
104
105   public void setTreeImport()
106   {
107     treeImport = true;
108     accept.setLabel(MessageManager.getString("action.accept"));
109     addSequences.setVisible(false);
110   }
111
112   public void setAnnotationImport()
113   {
114     annotationImport = true;
115     accept.setLabel(MessageManager.getString("action.accept"));
116     addSequences.setVisible(false);
117   }
118
119   public void actionPerformed(ActionEvent evt)
120   {
121     if (evt.getSource() == accept)
122     {
123       ok(true);
124     }
125     else if (evt.getSource() == addSequences)
126     {
127       ok(false);
128     }
129     else if (evt.getSource() == cancel)
130     {
131       cancel();
132     }
133   }
134
135   protected void ok(boolean newWindow)
136   {
137     String text = getText();
138     int length = text.length();
139     textarea.append("\n");
140     if (textarea.getText().length() == length)
141     {
142       String warning = "\n\n#################################################\n"
143               + "WARNING!! THIS IS THE MAXIMUM SIZE OF TEXTAREA!!\n"
144               + "\nCAN'T INPUT FULL ALIGNMENT"
145               + "\n\nYOU MUST DELETE THIS WARNING TO CONTINUE"
146               + "\n\nMAKE SURE LAST SEQUENCE PASTED IS COMPLETE"
147               + "\n#################################################\n";
148       textarea.setText(text.substring(0, text.length() - warning.length())
149               + warning);
150
151       textarea.setCaretPosition(text.length());
152     }
153
154     if (pdbImport)
155     {
156       openPdbViewer(text);
157
158     }
159     else if (treeImport)
160     {
161       if (!loadTree())
162       {
163         return;
164       }
165     }
166     else if (annotationImport)
167     {
168       loadAnnotations();
169     }
170     else if (alignFrame != null)
171     {
172       loadAlignment(text, newWindow, alignFrame.getAlignViewport());
173     }
174
175     // TODO: dialog should indicate if data was parsed correctly or not - see
176     // JAL-1102
177     if (this.getParent() instanceof Frame)
178     {
179       ((Frame) this.getParent()).setVisible(false);
180     }
181     else
182     {
183       ((Dialog) this.getParent()).setVisible(false);
184     }
185   }
186
187   /**
188    * Parses text as Newick Tree format, and loads on to the alignment. Returns
189    * true if successful, else false.
190    */
191   protected boolean loadTree()
192   {
193     try
194     {
195       NewickFile fin = new NewickFile(textarea.getText(), "Paste");
196
197       fin.parse();
198       if (fin.getTree() != null)
199       {
200         alignFrame.loadTree(fin, "Pasted tree file");
201         return true;
202       }
203     } catch (Exception ex)
204     {
205       // TODO: JAL-1102 - should have a warning message in dialog, not simply
206       // overwrite the broken input data with the exception
207       textarea.setText(MessageManager.formatMessage(
208               "label.could_not_parse_newick_file",
209               new Object[] { ex.getMessage() }));
210       return false;
211     }
212     return false;
213   }
214
215   /**
216    * Parse text as an alignment file and add to the current or a new window.
217    * 
218    * @param text
219    * @param newWindow
220    */
221   protected void loadAlignment(String text, boolean newWindow,
222           AlignViewport viewport)
223   {
224     AlignmentI al = null;
225
226     String format = new IdentifyFile().Identify(text,
227             AppletFormatAdapter.PASTE);
228     AppletFormatAdapter afa = new AppletFormatAdapter(alignFrame.alignPanel);
229     try
230     {
231       al = afa.readFile(text, AppletFormatAdapter.PASTE, format);
232       source = afa.getAlignFile();
233     } catch (java.io.IOException ex)
234     {
235       ex.printStackTrace();
236     }
237
238     if (al != null)
239     {
240       al.setDataset(null); // set dataset on alignment/sequences
241
242       /*
243        * SplitFrame option dependent on applet parameter for now.
244        */
245       boolean allowSplitFrame = alignFrame.viewport.applet
246               .getDefaultParameter("enableSplitFrame", false);
247       if (allowSplitFrame && openSplitFrame(al, format))
248       {
249         return;
250       }
251       if (newWindow)
252       {
253         AlignFrame af;
254
255         if (source instanceof ComplexAlignFile)
256         {
257           ColumnSelection colSel = ((ComplexAlignFile) source)
258                   .getColumnSelection();
259           SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
260                   .getHiddenSequences();
261           boolean showSeqFeatures = ((ComplexAlignFile) source)
262                   .isShowSeqFeatures();
263           String colourSchemeName = ((ComplexAlignFile) source)
264                   .getGlobalColourScheme();
265           af = new AlignFrame(al, hiddenSeqs, colSel,
266                   alignFrame.viewport.applet, "Cut & Paste input - "
267                           + format, false);
268           af.getAlignViewport().setShowSequenceFeatures(showSeqFeatures);
269           ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
270                   colourSchemeName, al);
271           af.changeColour(cs);
272         }
273         else
274         {
275           af = new AlignFrame(al, alignFrame.viewport.applet,
276                   "Cut & Paste input - " + format, false);
277         }
278
279         af.statusBar
280                 .setText(MessageManager
281                         .getString("label.successfully_pasted_annotation_to_alignment"));
282       }
283       else
284       {
285         alignFrame.addSequences(al.getSequencesArray());
286         alignFrame.statusBar.setText(MessageManager
287                 .getString("label.successfully_pasted_alignment_file"));
288       }
289     }
290   }
291
292   /**
293    * Check whether the new alignment could be mapped to the current one as
294    * cDNA/protein, if so offer the option to open as split frame view. Returns
295    * true if a split frame view is opened, false if not.
296    * 
297    * @param al
298    * @return
299    */
300   protected boolean openSplitFrame(AlignmentI al, String format)
301   {
302     final AlignmentI thisAlignment = this.alignFrame.getAlignViewport()
303             .getAlignment();
304     if (thisAlignment.isNucleotide() == al.isNucleotide())
305     {
306       // both nucleotide or both protein
307       return false;
308     }
309     AlignmentI protein = thisAlignment.isNucleotide() ? al : thisAlignment;
310     AlignmentI dna = thisAlignment.isNucleotide() ? thisAlignment : al;
311     boolean mapped = AlignmentUtils.mapProteinAlignmentToCdna(protein, dna);
312     if (!mapped)
313     {
314       return false;
315     }
316
317     /*
318      * A mapping is possible; ask user if they want a split frame.
319      */
320     String title = MessageManager.getString("label.open_split_window");
321     final JVDialog dialog = new JVDialog((Frame) this.getParent(), title,
322             true, 100, 400);
323     dialog.ok.setLabel(MessageManager.getString("action.yes"));
324     dialog.cancel.setLabel(MessageManager.getString("action.no"));
325     Panel question = new Panel(new BorderLayout());
326     final String text = MessageManager
327             .getString("label.open_split_window?");
328     question.add(new Label(text, Label.CENTER), BorderLayout.CENTER);
329     dialog.setMainPanel(question);
330     dialog.setVisible(true);
331     dialog.toFront();
332
333     if (!dialog.accept)
334     {
335       return false;
336     }
337
338     /*
339      * 'align' the added alignment to match the current one
340      */
341     al.alignAs(thisAlignment);
342
343     /*
344      * Open SplitFrame with DNA above and protein below, including the alignment
345      * from textbox and a copy of the original.
346      */
347     final JalviewLite applet = this.alignFrame.viewport.applet;
348     AlignFrame copyFrame = new AlignFrame(
349             this.alignFrame.viewport.getAlignment(), applet,
350             alignFrame.getTitle(), false, false);
351     AlignFrame newFrame = new AlignFrame(al, alignFrame.viewport.applet,
352             "Cut & Paste input - " + format, false, false);
353     AlignFrame dnaFrame = al.isNucleotide() ? newFrame : copyFrame;
354     AlignFrame proteinFrame = al.isNucleotide() ? copyFrame : newFrame;
355     SplitFrame sf = new SplitFrame(dnaFrame, proteinFrame);
356     sf.addToDisplay(false, applet);
357     return true;
358   }
359
360   /**
361    * Parse the text as a TCoffee score file, if successful add scores as
362    * alignment annotations.
363    */
364   protected void loadAnnotations()
365   {
366     TCoffeeScoreFile tcf = null;
367     try
368     {
369       tcf = new TCoffeeScoreFile(textarea.getText(),
370               jalview.io.AppletFormatAdapter.PASTE);
371       if (tcf.isValid())
372       {
373         if (tcf.annotateAlignment(alignFrame.viewport.getAlignment(), true))
374         {
375           alignFrame.tcoffeeColour.setEnabled(true);
376           alignFrame.alignPanel.fontChanged();
377           alignFrame.changeColour(new TCoffeeColourScheme(
378                   alignFrame.viewport.getAlignment()));
379           alignFrame.statusBar
380                   .setText(MessageManager
381                           .getString("label.successfully_pasted_tcoffee_scores_to_alignment"));
382         }
383         else
384         {
385           // file valid but didn't get added to alignment for some reason
386           alignFrame.statusBar.setText(MessageManager.formatMessage(
387                   "label.failed_add_tcoffee_scores",
388                   new Object[] { (tcf.getWarningMessage() != null ? tcf
389                           .getWarningMessage() : "") }));
390         }
391       }
392       else
393       {
394         tcf = null;
395       }
396     } catch (Exception x)
397     {
398       tcf = null;
399     }
400     if (tcf == null)
401     {
402       if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
403               textarea.getText(), jalview.io.AppletFormatAdapter.PASTE))
404       {
405         alignFrame.alignPanel.fontChanged();
406         alignFrame.alignPanel.setScrollValues(0, 0);
407         alignFrame.statusBar
408                 .setText(MessageManager
409                         .getString("label.successfully_pasted_annotation_to_alignment"));
410
411       }
412       else
413       {
414         if (!alignFrame.parseFeaturesFile(textarea.getText(),
415                 jalview.io.AppletFormatAdapter.PASTE))
416         {
417           alignFrame.statusBar
418                   .setText(MessageManager
419                           .getString("label.couldnt_parse_pasted_text_as_valid_annotation_feature_GFF_tcoffee_file"));
420         }
421       }
422     }
423   }
424
425   /**
426    * Open a Jmol viewer (if available), failing that the built-in PDB viewer,
427    * passing the input text as the PDB file data.
428    * 
429    * @param text
430    */
431   protected void openPdbViewer(String text)
432   {
433     PDBEntry pdb = new PDBEntry();
434     pdb.setFile(text);
435
436     if (alignFrame.alignPanel.av.applet.jmolAvailable)
437     {
438       new jalview.appletgui.AppletJmol(pdb, new SequenceI[] { seq }, null,
439               alignFrame.alignPanel, AppletFormatAdapter.PASTE);
440     }
441     else
442     {
443       new MCview.AppletPDBViewer(pdb, new SequenceI[] { seq }, null,
444               alignFrame.alignPanel, AppletFormatAdapter.PASTE);
445     }
446   }
447
448   protected void cancel()
449   {
450     textarea.setText("");
451     if (this.getParent() instanceof Frame)
452     {
453       ((Frame) this.getParent()).setVisible(false);
454     }
455     else
456     {
457       ((Dialog) this.getParent()).setVisible(false);
458     }
459   }
460
461   protected TextArea textarea = new TextArea();
462
463   Button accept = new Button("New Window");
464
465   Button addSequences = new Button("Add to Current Alignment");
466
467   Button cancel = new Button("Close");
468
469   protected Panel buttonPanel = new Panel();
470
471   BorderLayout borderLayout1 = new BorderLayout();
472
473   private void jbInit() throws Exception
474   {
475     textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
476     textarea.setText(MessageManager
477             .getString("label.paste_your_alignment_file"));
478     textarea.addMouseListener(this);
479     this.setLayout(borderLayout1);
480     accept.addActionListener(this);
481     addSequences.addActionListener(this);
482     cancel.addActionListener(this);
483     this.add(buttonPanel, BorderLayout.SOUTH);
484     buttonPanel.add(accept, null);
485     buttonPanel.add(addSequences);
486     buttonPanel.add(cancel, null);
487     this.add(textarea, java.awt.BorderLayout.CENTER);
488   }
489
490   public void mousePressed(MouseEvent evt)
491   {
492     if (textarea.getText().startsWith(
493             MessageManager.getString("label.paste_your")))
494     {
495       textarea.setText("");
496     }
497   }
498
499   public void mouseReleased(MouseEvent evt)
500   {
501   }
502
503   public void mouseClicked(MouseEvent evt)
504   {
505   }
506
507   public void mouseEntered(MouseEvent evt)
508   {
509   }
510
511   public void mouseExited(MouseEvent evt)
512   {
513   }
514 }