2 * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.2)
3 * Copyright (C) 2014 The Jalview Authors
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
10 * of the License, or (at your option) any later version.
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.
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.
21 package jalview.appletgui;
23 import jalview.analysis.AlignmentUtils;
24 import jalview.analysis.AlignmentUtils.MappingResult;
25 import jalview.bin.JalviewLite;
26 import jalview.datamodel.Alignment;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.PDBEntry;
29 import jalview.datamodel.Sequence;
30 import jalview.io.AnnotationFile;
31 import jalview.io.AppletFormatAdapter;
32 import jalview.io.IdentifyFile;
33 import jalview.io.NewickFile;
34 import jalview.io.TCoffeeScoreFile;
35 import jalview.schemes.TCoffeeColourScheme;
36 import jalview.util.MessageManager;
38 import java.awt.BorderLayout;
39 import java.awt.Button;
40 import java.awt.Dialog;
42 import java.awt.Frame;
43 import java.awt.Label;
44 import java.awt.Panel;
45 import java.awt.TextArea;
46 import java.awt.event.ActionEvent;
47 import java.awt.event.ActionListener;
48 import java.awt.event.MouseEvent;
49 import java.awt.event.MouseListener;
51 public class CutAndPasteTransfer extends Panel implements ActionListener,
54 boolean pdbImport = false;
56 boolean treeImport = false;
58 boolean annotationImport = false;
62 AlignFrame alignFrame;
64 public CutAndPasteTransfer(boolean forImport, AlignFrame alignFrame)
74 this.alignFrame = alignFrame;
78 buttonPanel.setVisible(false);
82 public String getText()
84 return textarea.getText();
87 public void setText(String text)
89 textarea.setText(text);
92 public void setPDBImport(Sequence seq)
95 accept.setLabel(MessageManager.getString("action.accept"));
96 addSequences.setVisible(false);
100 public void setTreeImport()
103 accept.setLabel(MessageManager.getString("action.accept"));
104 addSequences.setVisible(false);
107 public void setAnnotationImport()
109 annotationImport = true;
110 accept.setLabel(MessageManager.getString("action.accept"));
111 addSequences.setVisible(false);
114 public void actionPerformed(ActionEvent evt)
116 if (evt.getSource() == accept)
120 else if (evt.getSource() == addSequences)
124 else if (evt.getSource() == cancel)
130 protected void ok(boolean newWindow)
132 String text = getText();
133 int length = text.length();
134 textarea.append("\n");
135 if (textarea.getText().length() == length)
137 String warning = "\n\n#################################################\n"
138 + "WARNING!! THIS IS THE MAXIMUM SIZE OF TEXTAREA!!\n"
139 + "\nCAN'T INPUT FULL ALIGNMENT"
140 + "\n\nYOU MUST DELETE THIS WARNING TO CONTINUE"
141 + "\n\nMAKE SURE LAST SEQUENCE PASTED IS COMPLETE"
142 + "\n#################################################\n";
143 textarea.setText(text.substring(0, text.length() - warning.length())
146 textarea.setCaretPosition(text.length());
161 else if (annotationImport)
165 else if (alignFrame != null)
167 loadAlignment(text, newWindow);
170 // TODO: dialog should indicate if data was parsed correctly or not - see
172 if (this.getParent() instanceof Frame)
174 ((Frame) this.getParent()).setVisible(false);
178 ((Dialog) this.getParent()).setVisible(false);
183 * Parses text as Newick Tree format, and loads on to the alignment. Returns
184 * true if successful, else false.
186 protected boolean loadTree()
190 NewickFile fin = new NewickFile(textarea.getText(), "Paste");
193 if (fin.getTree() != null)
195 alignFrame.loadTree(fin, "Pasted tree file");
198 } catch (Exception ex)
200 // TODO: JAL-1102 - should have a warning message in dialog, not simply
201 // overwrite the broken input data with the exception
202 textarea.setText(MessageManager.formatMessage(
203 "label.could_not_parse_newick_file", new Object[]
204 { ex.getMessage() }));
211 * Parse text as an alignment file and add to the current or a new window.
216 protected void loadAlignment(String text, boolean newWindow)
220 String format = new IdentifyFile().Identify(text,
221 AppletFormatAdapter.PASTE);
224 al = new AppletFormatAdapter().readFile(text,
225 AppletFormatAdapter.PASTE, format);
226 } catch (java.io.IOException ex)
228 ex.printStackTrace();
233 al.setDataset(null); // set dataset on alignment/sequences
234 if (openSplitFrame(al, format))
240 AlignFrame af = new AlignFrame(al, alignFrame.viewport.applet,
241 "Cut & Paste input - " + format, false);
243 .setText(MessageManager
244 .getString("label.successfully_pasted_annotation_to_alignment"));
248 alignFrame.addSequences(al.getSequencesArray());
249 alignFrame.statusBar.setText(MessageManager
250 .getString("label.successfully_pasted_alignment_file"));
256 * Check whether the new alignment could be mapped to the current one as
257 * cDNA/protein, if so offer the option to open as split frame view. Returns
258 * true if a split frame view is opened, false if not.
263 protected boolean openSplitFrame(Alignment al, String format)
265 final AlignmentI thisAlignment = this.alignFrame.getAlignViewport().getAlignment();
266 if (thisAlignment.isNucleotide() == al.isNucleotide())
268 // both nucleotide or both protein
271 AlignmentI protein = thisAlignment.isNucleotide() ? al : thisAlignment;
272 AlignmentI dna = thisAlignment.isNucleotide() ? thisAlignment : al;
273 MappingResult mapped = AlignmentUtils.mapProteinToCdna(protein, dna);
274 if (mapped == MappingResult.NotMapped)
280 * A mapping is possible; ask user if they want a split frame.
282 String title = MessageManager.getString("label.open_split_window");
283 final JVDialog dialog = new JVDialog((Frame) this.getParent(), title,
285 dialog.ok.setLabel(MessageManager.getString("action.yes"));
286 dialog.cancel.setLabel(MessageManager.getString("action.no"));
287 Panel question = new Panel(new BorderLayout());
288 final String text = MessageManager
289 .getString("label.open_split_window?");
290 question.add(new Label(text, Label.CENTER), BorderLayout.CENTER);
291 dialog.setMainPanel(question);
292 dialog.setVisible(true);
301 * Open SplitFrame with DNA above and protein below, including the alignment
302 * from textbox and a copy of the original.
304 final JalviewLite applet = this.alignFrame.viewport.applet;
305 AlignFrame copyFrame = new AlignFrame(
306 this.alignFrame.viewport.getAlignment(), applet,
307 alignFrame.getTitle(), false, false);
308 AlignFrame newFrame = new AlignFrame(al, alignFrame.viewport.applet,
309 "Cut & Paste input - " + format, false, false);
310 AlignFrame dnaFrame = al.isNucleotide() ? newFrame : copyFrame;
311 AlignFrame proteinFrame = al.isNucleotide() ? copyFrame
313 SplitFrame sf = new SplitFrame(dnaFrame, proteinFrame);
314 sf.addToDisplay(false, applet);
319 * Parse the text as a TCoffee score file, if successful add scores as
320 * alignment annotations.
322 protected void loadAnnotations()
324 TCoffeeScoreFile tcf = null;
327 tcf = new TCoffeeScoreFile(textarea.getText(),
328 jalview.io.AppletFormatAdapter.PASTE);
331 if (tcf.annotateAlignment(alignFrame.viewport.getAlignment(),
334 alignFrame.tcoffeeColour.setEnabled(true);
335 alignFrame.alignPanel.fontChanged();
336 alignFrame.changeColour(new TCoffeeColourScheme(
337 alignFrame.viewport.getAlignment()));
339 .setText(MessageManager
340 .getString("label.successfully_pasted_tcoffee_scores_to_alignment"));
344 // file valid but didn't get added to alignment for some reason
345 alignFrame.statusBar.setText(MessageManager.formatMessage(
346 "label.failed_add_tcoffee_scores",
348 { (tcf.getWarningMessage() != null ? tcf
349 .getWarningMessage() : "") }));
356 } catch (Exception x)
362 if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
364 jalview.io.AppletFormatAdapter.PASTE))
366 alignFrame.alignPanel.fontChanged();
367 alignFrame.alignPanel.setScrollValues(0, 0);
369 .setText(MessageManager
370 .getString("label.successfully_pasted_annotation_to_alignment"));
375 if (!alignFrame.parseFeaturesFile(textarea.getText(),
376 jalview.io.AppletFormatAdapter.PASTE))
379 .setText(MessageManager
380 .getString("label.couldnt_parse_pasted_text_as_valid_annotation_feature_GFF_tcoffee_file"));
387 * Open a Jmol viewer (if available), failing that the built-in PDB viewer,
388 * passing the input text as the PDB file data.
392 protected void openPdbViewer(String text)
394 PDBEntry pdb = new PDBEntry();
397 if (alignFrame.alignPanel.av.applet.jmolAvailable)
399 new jalview.appletgui.AppletJmol(pdb, new Sequence[]
400 { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
404 new MCview.AppletPDBViewer(pdb, new Sequence[]
405 { seq }, null, alignFrame.alignPanel, AppletFormatAdapter.PASTE);
409 protected void cancel()
411 textarea.setText("");
412 if (this.getParent() instanceof Frame)
414 ((Frame) this.getParent()).setVisible(false);
418 ((Dialog) this.getParent()).setVisible(false);
422 protected TextArea textarea = new TextArea();
424 Button accept = new Button("New Window");
426 Button addSequences = new Button("Add to Current Alignment");
428 Button cancel = new Button("Close");
430 protected Panel buttonPanel = new Panel();
432 BorderLayout borderLayout1 = new BorderLayout();
434 private void jbInit() throws Exception
436 textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
437 textarea.setText(MessageManager
438 .getString("label.paste_your_alignment_file"));
439 textarea.addMouseListener(this);
440 this.setLayout(borderLayout1);
441 accept.addActionListener(this);
442 addSequences.addActionListener(this);
443 cancel.addActionListener(this);
444 this.add(buttonPanel, BorderLayout.SOUTH);
445 buttonPanel.add(accept, null);
446 buttonPanel.add(addSequences);
447 buttonPanel.add(cancel, null);
448 this.add(textarea, java.awt.BorderLayout.CENTER);
451 public void mousePressed(MouseEvent evt)
453 if (textarea.getText().startsWith(
454 MessageManager.getString("label.paste_your")))
456 textarea.setText("");
460 public void mouseReleased(MouseEvent evt)
464 public void mouseClicked(MouseEvent evt)
468 public void mouseEntered(MouseEvent evt)
472 public void mouseExited(MouseEvent evt)