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.api.FeaturesSourceI;
26 import jalview.bin.JalviewLite;
27 import jalview.datamodel.AlignmentI;
28 import jalview.datamodel.HiddenColumns;
29 import jalview.datamodel.PDBEntry;
30 import jalview.datamodel.SequenceI;
31 import jalview.io.AlignmentFileReaderI;
32 import jalview.io.AnnotationFile;
33 import jalview.io.AppletFormatAdapter;
34 import jalview.io.DataSourceType;
35 import jalview.io.FileFormatI;
36 import jalview.io.IdentifyFile;
37 import jalview.io.NewickFile;
38 import jalview.io.TCoffeeScoreFile;
39 import jalview.json.binding.biojson.v1.ColourSchemeMapper;
40 import jalview.schemes.ColourSchemeI;
41 import jalview.schemes.TCoffeeColourScheme;
42 import jalview.util.MessageManager;
44 import java.awt.BorderLayout;
45 import java.awt.Button;
46 import java.awt.Dialog;
48 import java.awt.Frame;
49 import java.awt.Label;
50 import java.awt.Panel;
51 import java.awt.TextArea;
52 import java.awt.event.ActionEvent;
53 import java.awt.event.ActionListener;
54 import java.awt.event.MouseEvent;
55 import java.awt.event.MouseListener;
56 import java.io.IOException;
58 public class CutAndPasteTransfer extends Panel
59 implements ActionListener, MouseListener
61 boolean pdbImport = false;
63 boolean treeImport = false;
65 boolean annotationImport = false;
69 AlignFrame alignFrame;
71 AlignmentFileReaderI source = null;
73 public CutAndPasteTransfer(boolean forImport, AlignFrame alignFrame)
83 this.alignFrame = alignFrame;
87 buttonPanel.setVisible(false);
91 public String getText()
93 return textarea.getText();
96 public void setText(String text)
98 textarea.setText(text);
101 public void setPDBImport(SequenceI seq)
104 accept.setLabel(MessageManager.getString("action.accept"));
105 addSequences.setVisible(false);
109 public void setTreeImport()
112 accept.setLabel(MessageManager.getString("action.accept"));
113 addSequences.setVisible(false);
116 public void setAnnotationImport()
118 annotationImport = true;
119 accept.setLabel(MessageManager.getString("action.accept"));
120 addSequences.setVisible(false);
124 public void actionPerformed(ActionEvent evt)
126 if (evt.getSource() == accept)
130 else if (evt.getSource() == addSequences)
134 else if (evt.getSource() == cancel)
140 protected void ok(boolean newWindow)
142 String text = getText();
143 int length = text.length();
144 textarea.append("\n");
145 if (textarea.getText().length() == length)
147 String warning = "\n\n#################################################\n"
148 + "WARNING!! THIS IS THE MAXIMUM SIZE OF TEXTAREA!!\n"
149 + "\nCAN'T INPUT FULL ALIGNMENT"
150 + "\n\nYOU MUST DELETE THIS WARNING TO CONTINUE"
151 + "\n\nMAKE SURE LAST SEQUENCE PASTED IS COMPLETE"
152 + "\n#################################################\n";
153 textarea.setText(text.substring(0, text.length() - warning.length())
156 textarea.setCaretPosition(text.length());
171 else if (annotationImport)
175 else if (alignFrame != null)
177 loadAlignment(text, newWindow, alignFrame.getAlignViewport());
180 // TODO: dialog should indicate if data was parsed correctly or not - see
182 if (this.getParent() instanceof Frame)
184 ((Frame) this.getParent()).setVisible(false);
188 ((Dialog) this.getParent()).setVisible(false);
193 * Parses text as Newick Tree format, and loads on to the alignment. Returns
194 * true if successful, else false.
196 protected boolean loadTree()
200 NewickFile fin = new NewickFile(textarea.getText(),
201 DataSourceType.PASTE);
204 if (fin.getTree() != null)
206 alignFrame.loadTree(fin, "Pasted tree file");
209 } catch (Exception ex)
211 // TODO: JAL-1102 - should have a warning message in dialog, not simply
212 // overwrite the broken input data with the exception
213 textarea.setText(MessageManager.formatMessage(
214 "label.could_not_parse_newick_file", new Object[]
215 { ex.getMessage() }));
222 * Parse text as an alignment file and add to the current or a new window.
227 protected void loadAlignment(String text, boolean newWindow,
228 AlignViewport viewport)
230 AlignmentI al = null;
234 FileFormatI format = new IdentifyFile().identify(text,
235 DataSourceType.PASTE);
236 AppletFormatAdapter afa = new AppletFormatAdapter(
237 alignFrame.alignPanel);
238 al = afa.readFile(text, DataSourceType.PASTE, format);
239 source = afa.getAlignFile();
243 al.setDataset(null); // set dataset on alignment/sequences
246 * SplitFrame option dependent on applet parameter for now.
248 boolean allowSplitFrame = alignFrame.viewport.applet
249 .getDefaultParameter("enableSplitFrame", false);
250 if (allowSplitFrame && openSplitFrame(al, format))
258 if (source instanceof ComplexAlignFile)
260 HiddenColumns colSel = ((ComplexAlignFile) source)
262 SequenceI[] hiddenSeqs = ((ComplexAlignFile) source)
263 .getHiddenSequences();
264 boolean showSeqFeatures = ((ComplexAlignFile) source)
265 .isShowSeqFeatures();
266 String colourSchemeName = ((ComplexAlignFile) source)
267 .getGlobalColourScheme();
268 af = new AlignFrame(al, hiddenSeqs, colSel,
269 alignFrame.viewport.applet,
270 "Cut & Paste input - " + format, false);
271 af.getAlignViewport().setShowSequenceFeatures(showSeqFeatures);
272 ColourSchemeI cs = ColourSchemeMapper
273 .getJalviewColourScheme(colourSchemeName, al);
281 af = new AlignFrame(al, alignFrame.viewport.applet,
282 "Cut & Paste input - " + format, false);
283 if (source instanceof FeaturesSourceI)
285 af.getAlignViewport().setShowSequenceFeatures(true);
289 af.statusBar.setText(MessageManager.getString(
290 "label.successfully_pasted_annotation_to_alignment"));
294 alignFrame.addSequences(al.getSequencesArray());
295 alignFrame.statusBar.setText(MessageManager
296 .getString("label.successfully_pasted_alignment_file"));
299 } catch (IOException ex)
301 ex.printStackTrace();
306 * Check whether the new alignment could be mapped to the current one as
307 * cDNA/protein, if so offer the option to open as split frame view. Returns
308 * true if a split frame view is opened, false if not.
313 protected boolean openSplitFrame(AlignmentI al, FileFormatI format)
315 final AlignmentI thisAlignment = this.alignFrame.getAlignViewport()
317 if (thisAlignment.isNucleotide() == al.isNucleotide())
319 // both nucleotide or both protein
322 AlignmentI protein = thisAlignment.isNucleotide() ? al : thisAlignment;
323 AlignmentI dna = thisAlignment.isNucleotide() ? thisAlignment : al;
324 boolean mapped = AlignmentUtils.mapProteinAlignmentToCdna(protein, dna);
331 * A mapping is possible; ask user if they want a split frame.
333 String title = MessageManager.getString("label.open_split_window");
334 final JVDialog dialog = new JVDialog((Frame) this.getParent(), title,
336 dialog.ok.setLabel(MessageManager.getString("action.yes"));
337 dialog.cancel.setLabel(MessageManager.getString("action.no"));
338 Panel question = new Panel(new BorderLayout());
339 final String text = MessageManager
340 .getString("label.open_split_window?");
341 question.add(new Label(text, Label.CENTER), BorderLayout.CENTER);
342 dialog.setMainPanel(question);
343 dialog.setVisible(true);
352 * 'align' the added alignment to match the current one
354 al.alignAs(thisAlignment);
357 * Open SplitFrame with DNA above and protein below, including the alignment
358 * from textbox and a copy of the original.
360 final JalviewLite applet = this.alignFrame.viewport.applet;
361 AlignFrame copyFrame = new AlignFrame(
362 this.alignFrame.viewport.getAlignment(), applet,
363 alignFrame.getTitle(), false, false);
364 AlignFrame newFrame = new AlignFrame(al, alignFrame.viewport.applet,
365 "Cut & Paste input - " + format, false, false);
366 AlignFrame dnaFrame = al.isNucleotide() ? newFrame : copyFrame;
367 AlignFrame proteinFrame = al.isNucleotide() ? copyFrame : newFrame;
368 SplitFrame sf = new SplitFrame(dnaFrame, proteinFrame);
369 sf.addToDisplay(false, applet);
374 * Parse the text as a TCoffee score file, if successful add scores as
375 * alignment annotations.
377 protected void loadAnnotations()
379 TCoffeeScoreFile tcf = null;
382 tcf = new TCoffeeScoreFile(textarea.getText(),
383 jalview.io.DataSourceType.PASTE);
386 if (tcf.annotateAlignment(alignFrame.viewport.getAlignment(), true))
388 alignFrame.tcoffeeColour.setEnabled(true);
389 alignFrame.alignPanel.fontChanged();
390 alignFrame.changeColour(new TCoffeeColourScheme(
391 alignFrame.viewport.getAlignment()));
392 alignFrame.statusBar.setText(MessageManager.getString(
393 "label.successfully_pasted_tcoffee_scores_to_alignment"));
397 // file valid but didn't get added to alignment for some reason
398 alignFrame.statusBar.setText(MessageManager.formatMessage(
399 "label.failed_add_tcoffee_scores", new Object[]
400 { (tcf.getWarningMessage() != null
401 ? tcf.getWarningMessage()
409 } catch (Exception x)
415 if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
416 textarea.getText(), jalview.io.DataSourceType.PASTE))
418 alignFrame.alignPanel.fontChanged();
419 alignFrame.alignPanel.setScrollValues(0, 0);
420 alignFrame.statusBar.setText(MessageManager.getString(
421 "label.successfully_pasted_annotation_to_alignment"));
426 if (!alignFrame.parseFeaturesFile(textarea.getText(),
427 jalview.io.DataSourceType.PASTE))
429 alignFrame.statusBar.setText(MessageManager.getString(
430 "label.couldnt_parse_pasted_text_as_valid_annotation_feature_GFF_tcoffee_file"));
437 * Open a Jmol viewer (if available), failing that the built-in PDB viewer,
438 * passing the input text as the PDB file data.
442 protected void openPdbViewer(String text)
444 PDBEntry pdb = new PDBEntry();
447 if (alignFrame.alignPanel.av.applet.jmolAvailable)
449 new jalview.appletgui.AppletJmol(pdb, new SequenceI[] { seq }, null,
450 alignFrame.alignPanel, DataSourceType.PASTE);
454 new mc_view.AppletPDBViewer(pdb, new SequenceI[] { seq }, null,
455 alignFrame.alignPanel, DataSourceType.PASTE);
459 protected void cancel()
461 textarea.setText("");
462 if (this.getParent() instanceof Frame)
464 ((Frame) this.getParent()).setVisible(false);
468 ((Dialog) this.getParent()).setVisible(false);
472 protected TextArea textarea = new TextArea();
474 Button accept = new Button("New Window");
476 Button addSequences = new Button("Add to Current Alignment");
478 Button cancel = new Button("Close");
480 protected Panel buttonPanel = new Panel();
482 BorderLayout borderLayout1 = new BorderLayout();
484 private void jbInit() throws Exception
486 textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
488 MessageManager.getString("label.paste_your_alignment_file"));
489 textarea.addMouseListener(this);
490 this.setLayout(borderLayout1);
491 accept.addActionListener(this);
492 addSequences.addActionListener(this);
493 cancel.addActionListener(this);
494 this.add(buttonPanel, BorderLayout.SOUTH);
495 buttonPanel.add(accept, null);
496 buttonPanel.add(addSequences);
497 buttonPanel.add(cancel, null);
498 this.add(textarea, java.awt.BorderLayout.CENTER);
502 public void mousePressed(MouseEvent evt)
504 if (textarea.getText()
505 .startsWith(MessageManager.getString("label.paste_your")))
507 textarea.setText("");
512 public void mouseReleased(MouseEvent evt)
517 public void mouseClicked(MouseEvent evt)
522 public void mouseEntered(MouseEvent evt)
527 public void mouseExited(MouseEvent evt)