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.ColumnSelection;
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 implements ActionListener,
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",
215 new Object[] { 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 ColumnSelection colSel = ((ComplexAlignFile) source)
261 .getColumnSelection();
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, "Cut & Paste input - "
271 af.getAlignViewport().setShowSequenceFeatures(showSeqFeatures);
272 ColourSchemeI cs = ColourSchemeMapper.getJalviewColourScheme(
273 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);
290 .setText(MessageManager
291 .getString("label.successfully_pasted_annotation_to_alignment"));
295 alignFrame.addSequences(al.getSequencesArray());
296 alignFrame.statusBar.setText(MessageManager
297 .getString("label.successfully_pasted_alignment_file"));
300 } catch (IOException ex)
302 ex.printStackTrace();
307 * Check whether the new alignment could be mapped to the current one as
308 * cDNA/protein, if so offer the option to open as split frame view. Returns
309 * true if a split frame view is opened, false if not.
314 protected boolean openSplitFrame(AlignmentI al, FileFormatI format)
316 final AlignmentI thisAlignment = this.alignFrame.getAlignViewport()
318 if (thisAlignment.isNucleotide() == al.isNucleotide())
320 // both nucleotide or both protein
323 AlignmentI protein = thisAlignment.isNucleotide() ? al : thisAlignment;
324 AlignmentI dna = thisAlignment.isNucleotide() ? thisAlignment : al;
325 boolean mapped = AlignmentUtils.mapProteinAlignmentToCdna(protein, dna);
332 * A mapping is possible; ask user if they want a split frame.
334 String title = MessageManager.getString("label.open_split_window");
335 final JVDialog dialog = new JVDialog((Frame) this.getParent(), title,
337 dialog.ok.setLabel(MessageManager.getString("action.yes"));
338 dialog.cancel.setLabel(MessageManager.getString("action.no"));
339 Panel question = new Panel(new BorderLayout());
340 final String text = MessageManager
341 .getString("label.open_split_window?");
342 question.add(new Label(text, Label.CENTER), BorderLayout.CENTER);
343 dialog.setMainPanel(question);
344 dialog.setVisible(true);
353 * 'align' the added alignment to match the current one
355 al.alignAs(thisAlignment);
358 * Open SplitFrame with DNA above and protein below, including the alignment
359 * from textbox and a copy of the original.
361 final JalviewLite applet = this.alignFrame.viewport.applet;
362 AlignFrame copyFrame = new AlignFrame(
363 this.alignFrame.viewport.getAlignment(), applet,
364 alignFrame.getTitle(), false, false);
365 AlignFrame newFrame = new AlignFrame(al, alignFrame.viewport.applet,
366 "Cut & Paste input - " + format, false, false);
367 AlignFrame dnaFrame = al.isNucleotide() ? newFrame : copyFrame;
368 AlignFrame proteinFrame = al.isNucleotide() ? copyFrame : newFrame;
369 SplitFrame sf = new SplitFrame(dnaFrame, proteinFrame);
370 sf.addToDisplay(false, applet);
375 * Parse the text as a TCoffee score file, if successful add scores as
376 * alignment annotations.
378 protected void loadAnnotations()
380 TCoffeeScoreFile tcf = null;
383 tcf = new TCoffeeScoreFile(textarea.getText(),
384 jalview.io.DataSourceType.PASTE);
387 if (tcf.annotateAlignment(alignFrame.viewport.getAlignment(), true))
389 alignFrame.tcoffeeColour.setEnabled(true);
390 alignFrame.alignPanel.fontChanged();
391 alignFrame.changeColour(new TCoffeeColourScheme(
392 alignFrame.viewport.getAlignment()));
394 .setText(MessageManager
395 .getString("label.successfully_pasted_tcoffee_scores_to_alignment"));
399 // file valid but didn't get added to alignment for some reason
400 alignFrame.statusBar.setText(MessageManager.formatMessage(
401 "label.failed_add_tcoffee_scores",
402 new Object[] { (tcf.getWarningMessage() != null ? tcf
403 .getWarningMessage() : "") }));
410 } catch (Exception x)
416 if (new AnnotationFile().annotateAlignmentView(alignFrame.viewport,
417 textarea.getText(), jalview.io.DataSourceType.PASTE))
419 alignFrame.alignPanel.fontChanged();
420 alignFrame.alignPanel.setScrollValues(0, 0);
422 .setText(MessageManager
423 .getString("label.successfully_pasted_annotation_to_alignment"));
428 if (!alignFrame.parseFeaturesFile(textarea.getText(),
429 jalview.io.DataSourceType.PASTE))
432 .setText(MessageManager
433 .getString("label.couldnt_parse_pasted_text_as_valid_annotation_feature_GFF_tcoffee_file"));
440 * Open a Jmol viewer (if available), failing that the built-in PDB viewer,
441 * passing the input text as the PDB file data.
445 protected void openPdbViewer(String text)
447 PDBEntry pdb = new PDBEntry();
450 if (alignFrame.alignPanel.av.applet.jmolAvailable)
452 new jalview.appletgui.AppletJmol(pdb, new SequenceI[] { seq }, null,
453 alignFrame.alignPanel, DataSourceType.PASTE);
457 new MCview.AppletPDBViewer(pdb, new SequenceI[] { seq }, null,
458 alignFrame.alignPanel, DataSourceType.PASTE);
462 protected void cancel()
464 textarea.setText("");
465 if (this.getParent() instanceof Frame)
467 ((Frame) this.getParent()).setVisible(false);
471 ((Dialog) this.getParent()).setVisible(false);
475 protected TextArea textarea = new TextArea();
477 Button accept = new Button("New Window");
479 Button addSequences = new Button("Add to Current Alignment");
481 Button cancel = new Button("Close");
483 protected Panel buttonPanel = new Panel();
485 BorderLayout borderLayout1 = new BorderLayout();
487 private void jbInit() throws Exception
489 textarea.setFont(new java.awt.Font("Monospaced", Font.PLAIN, 10));
490 textarea.setText(MessageManager
491 .getString("label.paste_your_alignment_file"));
492 textarea.addMouseListener(this);
493 this.setLayout(borderLayout1);
494 accept.addActionListener(this);
495 addSequences.addActionListener(this);
496 cancel.addActionListener(this);
497 this.add(buttonPanel, BorderLayout.SOUTH);
498 buttonPanel.add(accept, null);
499 buttonPanel.add(addSequences);
500 buttonPanel.add(cancel, null);
501 this.add(textarea, java.awt.BorderLayout.CENTER);
505 public void mousePressed(MouseEvent evt)
507 if (textarea.getText().startsWith(
508 MessageManager.getString("label.paste_your")))
510 textarea.setText("");
515 public void mouseReleased(MouseEvent evt)
520 public void mouseClicked(MouseEvent evt)
525 public void mouseEntered(MouseEvent evt)
530 public void mouseExited(MouseEvent evt)