2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $$Year-Rel$$ 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.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;
41 import java.awt.BorderLayout;
42 import java.awt.Button;
43 import java.awt.Dialog;
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;
54 public class CutAndPasteTransfer extends Panel implements ActionListener,
57 boolean pdbImport = false;
59 boolean treeImport = false;
61 boolean annotationImport = false;
65 AlignFrame alignFrame;
67 FileParse source = null;
69 public CutAndPasteTransfer(boolean forImport, AlignFrame alignFrame)
79 this.alignFrame = alignFrame;
83 buttonPanel.setVisible(false);
87 public String getText()
89 return textarea.getText();
92 public void setText(String text)
94 textarea.setText(text);
97 public void setPDBImport(SequenceI seq)
100 accept.setLabel(MessageManager.getString("action.accept"));
101 addSequences.setVisible(false);
105 public void setTreeImport()
108 accept.setLabel(MessageManager.getString("action.accept"));
109 addSequences.setVisible(false);
112 public void setAnnotationImport()
114 annotationImport = true;
115 accept.setLabel(MessageManager.getString("action.accept"));
116 addSequences.setVisible(false);
119 public void actionPerformed(ActionEvent evt)
121 if (evt.getSource() == accept)
125 else if (evt.getSource() == addSequences)
129 else if (evt.getSource() == cancel)
135 protected void ok(boolean newWindow)
137 String text = getText();
138 int length = text.length();
139 textarea.append("\n");
140 if (textarea.getText().length() == length)
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())
151 textarea.setCaretPosition(text.length());
166 else if (annotationImport)
170 else if (alignFrame != null)
172 loadAlignment(text, newWindow, alignFrame.getAlignViewport());
175 // TODO: dialog should indicate if data was parsed correctly or not - see
177 if (this.getParent() instanceof Frame)
179 ((Frame) this.getParent()).setVisible(false);
183 ((Dialog) this.getParent()).setVisible(false);
188 * Parses text as Newick Tree format, and loads on to the alignment. Returns
189 * true if successful, else false.
191 protected boolean loadTree()
195 NewickFile fin = new NewickFile(textarea.getText(), "Paste");
198 if (fin.getTree() != null)
200 alignFrame.loadTree(fin, "Pasted tree file");
203 } catch (Exception ex)
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() }));
216 * Parse text as an alignment file and add to the current or a new window.
221 protected void loadAlignment(String text, boolean newWindow,
222 AlignViewport viewport)
224 AlignmentI al = null;
226 String format = new IdentifyFile().Identify(text,
227 AppletFormatAdapter.PASTE);
228 AppletFormatAdapter afa = new AppletFormatAdapter(alignFrame.alignPanel);
231 al = afa.readFile(text, AppletFormatAdapter.PASTE, format);
232 source = afa.getAlignFile();
233 } catch (java.io.IOException ex)
235 ex.printStackTrace();
240 al.setDataset(null); // set dataset on alignment/sequences
243 * SplitFrame option dependent on applet parameter for now.
245 boolean allowSplitFrame = alignFrame.viewport.applet
246 .getDefaultParameter("enableSplitFrame", false);
247 if (allowSplitFrame && openSplitFrame(al, format))
255 if (source instanceof ComplexAlignFile)
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 - "
268 af.getAlignViewport().setShowSequenceFeatures(showSeqFeatures);
269 ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
270 colourSchemeName, al);
278 af = new AlignFrame(al, alignFrame.viewport.applet,
279 "Cut & Paste input - " + format, false);
283 .setText(MessageManager
284 .getString("label.successfully_pasted_annotation_to_alignment"));
288 alignFrame.addSequences(al.getSequencesArray());
289 alignFrame.statusBar.setText(MessageManager
290 .getString("label.successfully_pasted_alignment_file"));
296 * Check whether the new alignment could be mapped to the current one as
297 * cDNA/protein, if so offer the option to open as split frame view. Returns
298 * true if a split frame view is opened, false if not.
303 protected boolean openSplitFrame(AlignmentI al, String format)
305 final AlignmentI thisAlignment = this.alignFrame.getAlignViewport()
307 if (thisAlignment.isNucleotide() == al.isNucleotide())
309 // both nucleotide or both protein
312 AlignmentI protein = thisAlignment.isNucleotide() ? al : thisAlignment;
313 AlignmentI dna = thisAlignment.isNucleotide() ? thisAlignment : al;
314 boolean mapped = AlignmentUtils.mapProteinAlignmentToCdna(protein, dna);
321 * A mapping is possible; ask user if they want a split frame.
323 String title = MessageManager.getString("label.open_split_window");
324 final JVDialog dialog = new JVDialog((Frame) this.getParent(), title,
326 dialog.ok.setLabel(MessageManager.getString("action.yes"));
327 dialog.cancel.setLabel(MessageManager.getString("action.no"));
328 Panel question = new Panel(new BorderLayout());
329 final String text = MessageManager
330 .getString("label.open_split_window?");
331 question.add(new Label(text, Label.CENTER), BorderLayout.CENTER);
332 dialog.setMainPanel(question);
333 dialog.setVisible(true);
342 * 'align' the added alignment to match the current one
344 al.alignAs(thisAlignment);
347 * Open SplitFrame with DNA above and protein below, including the alignment
348 * from textbox and a copy of the original.
350 final JalviewLite applet = this.alignFrame.viewport.applet;
351 AlignFrame copyFrame = new AlignFrame(
352 this.alignFrame.viewport.getAlignment(), applet,
353 alignFrame.getTitle(), false, false);
354 AlignFrame newFrame = new AlignFrame(al, alignFrame.viewport.applet,
355 "Cut & Paste input - " + format, false, false);
356 AlignFrame dnaFrame = al.isNucleotide() ? newFrame : copyFrame;
357 AlignFrame proteinFrame = al.isNucleotide() ? copyFrame : newFrame;
358 SplitFrame sf = new SplitFrame(dnaFrame, proteinFrame);
359 sf.addToDisplay(false, applet);
364 * Parse the text as a TCoffee score file, if successful add scores as
365 * alignment annotations.
367 protected void loadAnnotations()
369 TCoffeeScoreFile tcf = null;
372 tcf = new TCoffeeScoreFile(textarea.getText(),
373 jalview.io.AppletFormatAdapter.PASTE);
376 if (tcf.annotateAlignment(alignFrame.viewport.getAlignment(), true))
378 alignFrame.tcoffeeColour.setEnabled(true);
379 alignFrame.alignPanel.fontChanged();
380 alignFrame.changeColour(new TCoffeeColourScheme(
381 alignFrame.viewport.getAlignment()));
383 .setText(MessageManager
384 .getString("label.successfully_pasted_tcoffee_scores_to_alignment"));
388 // file valid but didn't get added to alignment for some reason
389 alignFrame.statusBar.setText(MessageManager.formatMessage(
390 "label.failed_add_tcoffee_scores",
391 new Object[] { (tcf.getWarningMessage() != null ? tcf
392 .getWarningMessage() : "") }));
399 } catch (Exception x)
405 if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
406 textarea.getText(), jalview.io.AppletFormatAdapter.PASTE))
408 alignFrame.alignPanel.fontChanged();
409 alignFrame.alignPanel.setScrollValues(0, 0);
411 .setText(MessageManager
412 .getString("label.successfully_pasted_annotation_to_alignment"));
417 if (!alignFrame.parseFeaturesFile(textarea.getText(),
418 jalview.io.AppletFormatAdapter.PASTE))
421 .setText(MessageManager
422 .getString("label.couldnt_parse_pasted_text_as_valid_annotation_feature_GFF_tcoffee_file"));
429 * Open a Jmol viewer (if available), failing that the built-in PDB viewer,
430 * passing the input text as the PDB file data.
434 protected void openPdbViewer(String text)
436 PDBEntry pdb = new PDBEntry();
439 if (alignFrame.alignPanel.av.applet.jmolAvailable)
441 new jalview.appletgui.AppletJmol(pdb, new SequenceI[] { seq }, null,
442 alignFrame.alignPanel, AppletFormatAdapter.PASTE);
446 new MCview.AppletPDBViewer(pdb, new SequenceI[] { seq }, null,
447 alignFrame.alignPanel, AppletFormatAdapter.PASTE);
451 protected void cancel()
453 textarea.setText("");
454 if (this.getParent() instanceof Frame)
456 ((Frame) this.getParent()).setVisible(false);
460 ((Dialog) this.getParent()).setVisible(false);
464 protected TextArea textarea = new TextArea();
466 Button accept = new Button("New Window");
468 Button addSequences = new Button("Add to Current Alignment");
470 Button cancel = new Button("Close");
472 protected Panel buttonPanel = new Panel();
474 BorderLayout borderLayout1 = new BorderLayout();
476 private void jbInit() throws Exception
478 textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
479 textarea.setText(MessageManager
480 .getString("label.paste_your_alignment_file"));
481 textarea.addMouseListener(this);
482 this.setLayout(borderLayout1);
483 accept.addActionListener(this);
484 addSequences.addActionListener(this);
485 cancel.addActionListener(this);
486 this.add(buttonPanel, BorderLayout.SOUTH);
487 buttonPanel.add(accept, null);
488 buttonPanel.add(addSequences);
489 buttonPanel.add(cancel, null);
490 this.add(textarea, java.awt.BorderLayout.CENTER);
493 public void mousePressed(MouseEvent evt)
495 if (textarea.getText().startsWith(
496 MessageManager.getString("label.paste_your")))
498 textarea.setText("");
502 public void mouseReleased(MouseEvent evt)
506 public void mouseClicked(MouseEvent evt)
510 public void mouseEntered(MouseEvent evt)
514 public void mouseExited(MouseEvent evt)