X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FAlignFrame.java;h=5685e42a13d28e2fa1db3a7df5b52ee5bdf2aa73;hb=c24071f7b9e6b70c94743214677871ae2dc88499;hp=b504538d2cb0e45363a4d6fa1f2c656b4b75ec2a;hpb=14767e3e26b0c18a90566e02ab945426de6b847d;p=jalview.git diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index b504538..5685e42 100755 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -1,674 +1,2631 @@ -/******************** - * 2004 Jalview Reengineered - * Barton Group - * Dundee University +/* + * Jalview - A Sequence Alignment Editor and Viewer + * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle * - * AM Waterhouse - *******************/ - - + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Softwarechang + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA + */ +package jalview.gui; +import java.beans.*; +import java.io.*; +import java.util.*; -package jalview.gui; +import java.awt.*; +import java.awt.datatransfer.*; +import java.awt.event.*; +import java.awt.print.*; +import javax.swing.*; -import jalview.jbgui.GAlignFrame; -import jalview.schemes.*; -import jalview.datamodel.*; import jalview.analysis.*; +import jalview.datamodel.*; import jalview.io.*; -import java.awt.event.*; -import java.awt.*; -import javax.swing.*; -import java.util.*; +import jalview.jbgui.*; +import jalview.schemes.*; +import jalview.ws.*; +import java.awt.dnd.*; -public class AlignFrame extends GAlignFrame + +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +public class AlignFrame + extends GAlignFrame implements ClipboardOwner, DropTargetListener { - final AlignmentPanel alignPanel; - final AlignViewport viewport; + /** DOCUMENT ME!! */ + public static final int NEW_WINDOW_WIDTH = 700; + + /** DOCUMENT ME!! */ + public static final int NEW_WINDOW_HEIGHT = 500; + AlignmentPanel alignPanel; + AlignViewport viewport; + + Vector viewports = new Vector(); + Vector alignPanels = new Vector(); + + /** DOCUMENT ME!! */ + public String currentFileFormat = null; + Stack historyList = new Stack(); + Stack redoList = new Stack(); + private int treeCount = 0; + + + /** + * Creates a new AlignFrame object. + * + * @param al DOCUMENT ME! + */ public AlignFrame(AlignmentI al) { - super(); - viewport = new AlignViewport(al,true,true,true,false); - alignPanel = new AlignmentPanel(this, viewport); - getContentPane().add(alignPanel, java.awt.BorderLayout.CENTER); - fontNameMenuItem.setText(viewport.getFont().getName()); - fontSizeMenuItem.setText(viewport.getFont().getSize()+""); - } + viewport = new AlignViewport(al); + viewports.add(viewport); - protected void saveAs_actionPerformed(ActionEvent e) - { - JFileChooser chooser = new JFileChooser(jalview.bin.Cache.LAST_DIRECTORY); - chooser.setDialogTitle("Save Alignment to file - "+e.getActionCommand() +" format."); - chooser.setToolTipText("Save"); - int value = chooser.showSaveDialog(this); - if(value == JFileChooser.APPROVE_OPTION) - { - String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.LAST_DIRECTORY = choice; - String output = FormatAdapter.get(e.getActionCommand(), viewport.getAlignment().getSequences()); - try{ - java.io.PrintWriter out = new java.io.PrintWriter( new java.io.FileWriter( choice ) ); - out.println(output); - out.close(); - } - catch(Exception ex){} + this.setDropTarget(new java.awt.dnd.DropTarget(this, this)); + + if(viewport.vconsensus==null) + { + //Out of memory calculating consensus. + BLOSUM62Colour.setEnabled(false); + PIDColour.setEnabled(false); + conservationMenuItem.setEnabled(false); + modifyConservation.setEnabled(false); + abovePIDThreshold.setEnabled(false); + modifyPID.setEnabled(false); } - } + alignPanel = new AlignmentPanel(this, viewport); + alignPanels.add(alignPanel); - protected void outputText_actionPerformed(ActionEvent e) - { - CutAndPasteTransfer cap = new CutAndPasteTransfer(false); - JInternalFrame frame = new JInternalFrame(); - cap.formatForOutput(); - frame.setContentPane(cap); - Desktop.addInternalFrame(frame, "Alignment output - "+e.getActionCommand(), 600, 500); - cap.setText( FormatAdapter.get(e.getActionCommand(), viewport.getAlignment().getSequences())); - } + String sortby = jalview.bin.Cache.getDefault("SORT_ALIGNMENT", "No sort"); - protected void htmlMenuItem_actionPerformed(ActionEvent e) - { - HTMLOutput htmlOutput = new HTMLOutput(viewport.getAlignment(), alignPanel.seqPanel.getColourScheme()); - htmlOutput = null; - } + if(sortby.equals("Id")) + sortIDMenuItem_actionPerformed(null); + else if(sortby.equals("Pairwise Identity")) + sortPairwiseMenuItem_actionPerformed(null); - public void saveAsPostscriptMenuItem_actionPerformed(ActionEvent e) - { + // remove(tabbedPane); + getContentPane().add(alignPanel, BorderLayout.CENTER); - } - public void closeMenuItem_actionPerformed(ActionEvent e) - { - try{ - this.setClosed(true); - }catch(Exception ex){} - } - ArrayList historyList = new ArrayList(); - ArrayList redoList = new ArrayList(); + // tabbedPane.add(al.isNucleotide() ? "DNA":"Protein", alignPanel); - void updateEditMenuBar() - { - if(historyList.size()>0) - { - undoMenuItem.setEnabled(true); - Object [] history = (Object[])historyList.get(0); - undoMenuItem.setText("Undo "+history[0]); - } - else + ///Dataset tab + ///////////////////////// + if(al.getDataset()==null) { - undoMenuItem.setEnabled(false); - undoMenuItem.setText("Undo"); + al.setDataset(null); } + // AlignViewport ds = new AlignViewport(al.getDataset(), true); + // AlignmentPanel dap = new AlignmentPanel(this, ds); + // tabbedPane.add("Dataset", dap); + // viewports.add(ds); + // alignPanels.add(dap); + ///////////////////////// + - if(redoList.size()>0) + viewport.addPropertyChangeListener(new PropertyChangeListener() + { + public void propertyChange(PropertyChangeEvent evt) { - redoMenuItem.setEnabled(true); - Object [] history = (Object[])redoList.get(0); - redoMenuItem.setText("Redo "+history[0]); + if (evt.getPropertyName().equals("alignment")) + { + alignmentChanged(); + } } - else - { - redoMenuItem.setEnabled(false); - redoMenuItem.setText("Redo"); - } + }); + + + if (Desktop.desktop != null) + { + addServiceListeners(); + setGUINucleotide(al.isNucleotide()); + } } - public void addHistoryItem(String type) + /* Set up intrinsic listeners for dynamically generated GUI bits. */ + private void addServiceListeners() { - // must make sure we add new sequence objects her, not refs to the existing sequences - redoList.clear(); - SequenceI[] seq = new SequenceI[viewport.getAlignment().getHeight()]; - for(int i=0; i -1) + { + shortName = shortName.substring(shortName.lastIndexOf( + java.io.File.separatorChar) + 1); + } - viewport.getSelection().clear(); - viewport.resetSeqLimits(alignPanel.seqPanel.seqCanvas.getHeight()); - alignPanel.RefreshPanels(); - } + Jalview2XML.SaveAlignment(this, file, shortName); - public void moveSelectedMenuItem_actionPerformed(ActionEvent e) - { - copySelectedMenuItem_actionPerformed(null); - deleteSelectedMenuItem_actionPerformed(null); - alignPanel.repaint(); - } + // USE Jalview2XML to save this file + return true; + } + else + { + String output = new FormatAdapter().formatSequences(format, + viewport.getAlignment(). + getSequences()); + if (output == null) + { + return false; + } - public void copySelectedMenuItem_actionPerformed(ActionEvent e) - { - if(viewport.getSelection().size()==0) - return; + try + { + java.io.PrintWriter out = new java.io.PrintWriter( + new java.io.FileWriter(file)); - SequenceI[] s = new Sequence[viewport.getSelection().size()]; - for (int i=0; i < viewport.getSelection().size(); i++) { - s[i] = new Sequence(viewport.getSelection().sequenceAt(i)); + out.print(output); + out.close(); + return true; + } + catch (Exception ex) + { + ex.printStackTrace(); + } } - AlignFrame af = new AlignFrame(new Alignment(s)); - int newHeight = s.length * af.viewport.getCharHeight() + 200; - if(newHeight>500) - newHeight=500; - Desktop.addInternalFrame(af, "Copied sequences", 700,newHeight); + return false; } - public void deselectAllColumnsMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void outputText_actionPerformed(ActionEvent e) { - viewport.getColumnSelection().clear(); - repaint(); + CutAndPasteTransfer cap = new CutAndPasteTransfer(); + Desktop.addInternalFrame(cap, + "Alignment output - " + e.getActionCommand(), 600, + 500); + cap.setText(new FormatAdapter().formatSequences(e.getActionCommand(), + viewport.getAlignment(). + getSequences())); } - public void remove2LeftMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void htmlMenuItem_actionPerformed(ActionEvent e) { - addHistoryItem("delete columns"); - ColumnSelection colSel = viewport.getColumnSelection(); - if (colSel.size() > 0) - { - int min = colSel.getMin(); - viewport.getAlignment().trimLeft(min); - colSel.compensateForEdit(0,min); - alignPanel.RefreshPanels(); - } + new HTMLOutput(viewport, + alignPanel.seqPanel.seqCanvas.getSequenceRenderer(), + alignPanel.seqPanel.seqCanvas.getFeatureRenderer()); } - public void remove2RightMenuItem_actionPerformed(ActionEvent e) + public void createImageMap(File file, String image) { - addHistoryItem("delete columns"); - ColumnSelection colSel = viewport.getColumnSelection(); - if (colSel.size() > 0) - { - int max = colSel.getMax(); - if(max>1) - viewport.getAlignment().trimRight(max); - - alignPanel.RefreshPanels(); - } - + alignPanel.makePNGImageMap(file, image); } - public void removeGappedColumnMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void createPNG(File f) { - addHistoryItem("delete gapped columns"); - viewport.getAlignment().removeGaps(); - alignPanel.RefreshPanels(); + alignPanel.makePNG(f); } - public void removeAllGapsMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void createEPS(File f) { - addHistoryItem("delete all gaps"); - SequenceI current; - int jSize; - for (int i=0; i < viewport.getAlignment().getSequences().size();i++) - { - current = viewport.getAlignment().getSequenceAt(i); - jSize = current.getLength(); - for (int j=0; j < jSize; j++) - if (current.getCharAt(j)=='-' || current.getCharAt(j)=='.' || current.getCharAt(j)==' ') - { - current.deleteCharAt(j); - j--; - jSize--; - } - } + alignPanel.makeEPS(f); + } - alignPanel.RefreshPanels(); + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void printMenuItem_actionPerformed(ActionEvent e) + { + //Putting in a thread avoids Swing painting problems + PrintThread thread = new PrintThread(); + thread.start(); } - public void setGapCharMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void closeMenuItem_actionPerformed(ActionEvent e) { - String thisChar = "-"; - String nextChar = "."; - if(viewport.getGapCharacter().equals("-")) + try + { + PaintRefresher.components.remove(viewport.alignment); + this.setClosed(true); + } + catch (Exception ex) { - thisChar = "."; - nextChar = "-"; } - setGapCharMenuItem.setText("Set gap character to \""+nextChar+"\""); - viewport.setGapCharacter(thisChar); } - - public void fontNameMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + */ + void updateEditMenuBar() { - String fonts[] = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); + if (historyList.size() > 0) + { + undoMenuItem.setEnabled(true); + HistoryItem hi = (HistoryItem) historyList.peek(); + undoMenuItem.setText("Undo " + hi.getDescription()); + } + else + { + undoMenuItem.setEnabled(false); + undoMenuItem.setText("Undo"); + } - String selection = JOptionPane.showInputDialog(this, - "Select font", - "Font selection", - JOptionPane.QUESTION_MESSAGE, - null,fonts - ,fontNameMenuItem.getText()).toString(); - if(selection!=null) + if (redoList.size() > 0) { - fontNameMenuItem.setText(selection); - SetFont(); - } + redoMenuItem.setEnabled(true); + HistoryItem hi = (HistoryItem) redoList.peek(); + redoMenuItem.setText("Redo " + hi.getDescription()); + } + else + { + redoMenuItem.setEnabled(false); + redoMenuItem.setText("Redo"); + } } - public void fontSizeMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param hi DOCUMENT ME! + */ + public void addHistoryItem(HistoryItem hi) { - String selection = JOptionPane.showInputDialog(this, - "Select font size", - "Font size", - JOptionPane.QUESTION_MESSAGE, - null, new String[]{"1","2","4","6","8","10","12","14","16","18","20"} - ,fontSizeMenuItem.getText()).toString(); - if(selection!=null) - { - fontSizeMenuItem.setText(selection); - SetFont(); - } + historyList.push(hi); + updateEditMenuBar(); } - public void fontStyleMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void undoMenuItem_actionPerformed(ActionEvent e) { - String selection = JOptionPane.showInputDialog(this, - "Select font style", - "Font style", - JOptionPane.QUESTION_MESSAGE, - null, new String[]{"plain", "bold", "italic"} - ,fontStyleMenuItem.getText()).toString(); - if(selection!=null) - { - fontStyleMenuItem.setText(selection); - SetFont(); - } - + HistoryItem hi = (HistoryItem) historyList.pop(); + redoList.push(new HistoryItem(hi.getDescription(), viewport.alignment, + HistoryItem.HIDE)); + restoreHistoryItem(hi); + viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); } - protected void colourTextMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void redoMenuItem_actionPerformed(ActionEvent e) { - viewport.setColourText( colourTextMenuItem.isSelected() ); - alignPanel.RefreshPanels(); + HistoryItem hi = (HistoryItem) redoList.pop(); + restoreHistoryItem(hi); + updateEditMenuBar(); + viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); } - void SetFont() + // used by undo and redo + void restoreHistoryItem(HistoryItem hi) { - int style = java.awt.Font.PLAIN; - if(fontStyleMenuItem.getText().equals("bold")) - style = java.awt.Font.BOLD; - else if(fontStyleMenuItem.getText().equals("italic")) - style = java.awt.Font.ITALIC; - - viewport.setFont(new java.awt.Font(fontNameMenuItem.getText(), - style, - Integer.parseInt(fontSizeMenuItem.getText()))); - alignPanel.RefreshPanels(); + if (hi.getType() == HistoryItem.SORT) + { + for (int i = 0; i < hi.getSequences().size(); i++) + { + viewport.alignment.getSequences().setElementAt(hi.getSequences() + .elementAt(i), + i); + } + } + else + { + for (int i = 0; i < hi.getSequences().size(); i++) + { + SequenceI restore = (SequenceI) hi.getSequences().elementAt(i); - } + if (restore.getLength() == 0) + { + restore.setSequence(hi.getHidden().elementAt(i).toString()); + viewport.alignment.getSequences().insertElementAt(restore, + hi.getAlignIndex(i)); + } + else + { + restore.setSequence(hi.getHidden().elementAt(i).toString()); + } + } - protected void wrapMenuItem_actionPerformed(ActionEvent e) - { - viewport.setWrapAlignment( wrapMenuItem.isSelected() ); - alignPanel.setWrapAlignment( wrapMenuItem.isSelected() ); - } + if (hi.getType() == HistoryItem.PASTE) + { + for (int i = viewport.alignment.getHeight() - 1; + i > (hi.getSequences().size() - 1); i--) + { + viewport.alignment.deleteSequence(i); + } + } + } + updateEditMenuBar(); - public void viewBoxesMenuItem_actionPerformed(ActionEvent e) - { - viewport.setShowBoxes( viewBoxesMenuItem.isSelected() ); - alignPanel.RefreshPanels(); + viewport.firePropertyChange("alignment", null, + viewport.getAlignment().getSequences()); } - public void viewTextMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param up DOCUMENT ME! + */ + public void moveSelectedSequences(boolean up) { - viewport.setShowText( viewTextMenuItem.isSelected() ); - alignPanel.RefreshPanels(); - } + SequenceGroup sg = viewport.getSelectionGroup(); + if (sg == null) + { + return; + } - public void consensusGraphMenuItem_actionPerformed(ActionEvent e) - { - alignPanel.setGraphPanelVisible( consensusGraphMenuItem.isSelected() ); - } + if (up) + { + for (int i = 1; i < viewport.alignment.getHeight(); i++) + { + SequenceI seq = viewport.alignment.getSequenceAt(i); - public void overviewMenuItem_actionPerformed(ActionEvent e) - { + if (!sg.sequences.contains(seq)) + { + continue; + } - JInternalFrame frame = new JInternalFrame(); - OverviewPanel overview = alignPanel.getOverviewPanel(); - try{ - overview = new OverviewPanel(alignPanel, viewport); - frame.setContentPane(overview); - Desktop.addInternalFrame(frame, "Overview " + this.getTitle(), - frame.getWidth(), frame.getHeight()); - frame.pack(); - frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter() - { public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) + SequenceI temp = viewport.alignment.getSequenceAt(i - 1); + + if (sg.sequences.contains(temp)) + { + continue; + } + + viewport.alignment.getSequences().setElementAt(temp, i); + viewport.alignment.getSequences().setElementAt(seq, i - 1); + } + } + else + { + for (int i = viewport.alignment.getHeight() - 2; i > -1; i--) { - alignPanel.setOverviewPanel(null); - }; - }); - alignPanel.setOverviewPanel( overview ); + SequenceI seq = viewport.alignment.getSequenceAt(i); - }catch(java.lang.OutOfMemoryError ex) - { - JOptionPane.showMessageDialog(this, "Sequence alignment too large to\nproduce overview image!!" - +"\nTry reducing the font size.", - "Out of memory", JOptionPane.WARNING_MESSAGE); - } + if (!sg.sequences.contains(seq)) + { + continue; + } + + SequenceI temp = viewport.alignment.getSequenceAt(i + 1); + + if (sg.sequences.contains(temp)) + { + continue; + } + viewport.alignment.getSequences().setElementAt(temp, i); + viewport.alignment.getSequences().setElementAt(seq, i + 1); + } + } + alignPanel.repaint(); } - protected void noColourmenuItem_actionPerformed(ActionEvent e) + public void lostOwnership(Clipboard clipboard, Transferable contents) { - alignPanel.setColourScheme(null, false); + Desktop.jalviewClipboard = null; } - public void clustalColour_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void copy_actionPerformed(ActionEvent e) { - // alignPanel.setColourScheme( new ClustalxColourScheme() ); - } + if (viewport.getSelectionGroup() == null) + { + return; + } - public void zappoColour_actionPerformed(ActionEvent e) - { - alignPanel.setColourScheme( new ZappoColourScheme(), conservationMenuItem.isSelected() ); + SequenceGroup sg = viewport.getSelectionGroup(); + + Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard(); + + Hashtable orderedSeqs = new Hashtable(); + SequenceI[] seqs = new SequenceI[sg.getSize()]; + + for (int i = 0; i < sg.getSize(); i++) + { + SequenceI seq = sg.getSequenceAt(i); + int index = viewport.alignment.findIndex(seq); + orderedSeqs.put(index + "", seq); + } + + int index = 0, startRes, endRes; + char ch; + + for (int i = 0; i < sg.getSize(); i++) + { + SequenceI seq = null; + + while (seq == null) + { + if (orderedSeqs.containsKey(index + "")) + { + seq = (SequenceI) orderedSeqs.get(index + ""); + index++; + + break; + } + else + { + index++; + } + } + + //FIND START RES + //Returns residue following index if gap + startRes = seq.findPosition(sg.getStartRes()); + + //FIND END RES + //Need to find the residue preceeding index if gap + endRes = 0; + + for (int j = 0; j < sg.getEndRes() + 1 && j < seq.getLength(); j++) + { + ch = seq.getCharAt(j); + if (!jalview.util.Comparison.isGap( (ch))) + { + endRes++; + } + } + + if (endRes > 0) + { + endRes += seq.getStart() - 1; + } + + seqs[i] = new Sequence(seq.getName(), + seq.getSequence(sg.getStartRes(), sg.getEndRes() + 1), + startRes, + endRes); + seqs[i].setDescription(seq.getDescription()); + seqs[i].setDBRef(seq.getDBRef()); + seqs[i].setSequenceFeatures(seq.getSequenceFeatures()); + seqs[i].setDatasetSequence(seq.getDatasetSequence()); + + } + + FastaFile ff = new FastaFile(); + ff.addJVSuffix( viewport.showJVSuffix ); + c.setContents(new StringSelection( ff.print(seqs)), this); + Desktop.jalviewClipboard = new Object[]{seqs, viewport.alignment.getDataset()}; } - public void taylorColour_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void pasteNew_actionPerformed(ActionEvent e) { - alignPanel.setColourScheme( new TaylorColourScheme(),conservationMenuItem.isSelected() ); + paste(true); } - - public void hydrophobicityColour_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void pasteThis_actionPerformed(ActionEvent e) { - alignPanel.setColourScheme( new HydrophobicColourScheme(),conservationMenuItem.isSelected() ); + addHistoryItem(new HistoryItem("Paste Sequences", viewport.alignment, + HistoryItem.PASTE)); + paste(false); } - public void helixColour_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param newAlignment DOCUMENT ME! + */ + void paste(boolean newAlignment) { - alignPanel.setColourScheme( new HelixColourScheme(),conservationMenuItem.isSelected() ); - } + try + { + Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard(); + Transferable contents = c.getContents(this); + if (contents == null) + { + return; + } - public void strandColour_actionPerformed(ActionEvent e) - { - alignPanel.setColourScheme( new StrandColourScheme() ,conservationMenuItem.isSelected() ); - } + String str = (String) contents.getTransferData(DataFlavor.stringFlavor); + if(str.length()<1) + return; + String format = IdentifyFile.Identify(str, "Paste"); + SequenceI[] sequences; - public void turnColour_actionPerformed(ActionEvent e) - { - alignPanel.setColourScheme( new TurnColourScheme() ,conservationMenuItem.isSelected() ); - } + if(Desktop.jalviewClipboard!=null) + { + // The clipboard was filled from within Jalview, we must use the sequences + // And dataset from the copied alignment + sequences = (SequenceI[])Desktop.jalviewClipboard[0]; + } + else + { + sequences = new FormatAdapter().readFile(str, "Paste", format); + } + if (newAlignment) + { - public void buriedColour_actionPerformed(ActionEvent e) - { - alignPanel.setColourScheme( new BuriedColourScheme() ,conservationMenuItem.isSelected() ); - } + Alignment alignment = new Alignment(sequences); + if(Desktop.jalviewClipboard!=null) + alignment.setDataset( (Alignment)Desktop.jalviewClipboard[1] ); + else + alignment.setDataset( null ); - protected void conservationMenuItem_actionPerformed(ActionEvent e) - { - alignPanel.setColourScheme ( alignPanel.seqPanel.getColourScheme(),conservationMenuItem.isSelected() ); - conservationColourIncMenuItem.setEnabled( conservationMenuItem.isSelected() ); - } + AlignFrame af = new AlignFrame(alignment); + String newtitle = new String("Copied sequences"); + if (title.startsWith("Copied sequences")) + { + newtitle = title; + } + else + { + newtitle = newtitle.concat("- from " + title); + } - public void conservationColourIncMenuItem_actionPerformed(ActionEvent e) - { - ConservationIncrementPanel cip = new ConservationIncrementPanel(viewport, alignPanel); - JInternalFrame frame = new JInternalFrame(); - frame.setContentPane(cip); - Desktop.addInternalFrame(frame, "Conservation Colour Increment", 400,90); - frame.setMaximizable(false); - } + Desktop.addInternalFrame(af, newtitle, NEW_WINDOW_WIDTH, + NEW_WINDOW_HEIGHT); + } + else + { + //!newAlignment + for (int i = 0; i < sequences.length; i++) + { + Sequence newseq = new Sequence(sequences[i].getName(), + sequences[i].getSequence(), sequences[i].getStart(), + sequences[i].getEnd()); + viewport.alignment.addSequence(newseq); + if(sequences[i].getDatasetSequence()==null) + { + //////////////////////////// + //Datset needs extension; + ///////////////////////////// + Sequence ds = new Sequence(sequences[i].getName(), + AlignSeq.extractGaps("-. ", sequences[i].getSequence()), + sequences[i].getStart(), + sequences[i].getEnd()); + newseq.setDatasetSequence(ds); + viewport.alignment.getDataset().addSequence(ds); + } + else + newseq.setDatasetSequence(sequences[i].getDatasetSequence()); + } + viewport.setEndSeq(viewport.alignment.getHeight()); + viewport.alignment.getWidth(); + viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); + } + } + catch (Exception ex) + { + // could be anything being pasted in here + } - public void abovePIDColour_actionPerformed(ActionEvent e) - { - alignPanel.setColourScheme( new PIDColourScheme() ,conservationMenuItem.isSelected() ); - } + } - public void userDefinedColour_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void cut_actionPerformed(ActionEvent e) { - JInternalFrame frame = new JInternalFrame(); - UserDefinedColours chooser = new UserDefinedColours( frame, alignPanel.seqPanel ); - frame.setContentPane(chooser); - Desktop.addInternalFrame(frame,"User defined colours", 450,540 ); - frame.setResizable(false); - frame.setIconifiable(false); - frame.setMaximizable(false); + copy_actionPerformed(null); + delete_actionPerformed(null); } - public void PIDColour_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void delete_actionPerformed(ActionEvent e) { - alignPanel.setColourScheme( new PIDColourScheme(),conservationMenuItem.isSelected() ); - } + if (viewport.getSelectionGroup() == null) + { + return; + } + + addHistoryItem(new HistoryItem("Delete Sequences", viewport.alignment, + HistoryItem.HIDE)); - public void BLOSUM62Colour_actionPerformed(ActionEvent e) - { - alignPanel.setColourScheme( new Blosum62ColourScheme(viewport) ,conservationMenuItem.isSelected() ); - } + SequenceGroup sg = viewport.getSelectionGroup(); + boolean allSequences = false; + if (sg.sequences.size() == viewport.alignment.getHeight()) + { + allSequences = true; + } + for (int i = 0; i < sg.sequences.size(); i++) + { + SequenceI seq = sg.getSequenceAt(i); + int index = viewport.getAlignment().findIndex(seq); + seq.deleteChars(sg.getStartRes(), sg.getEndRes() + 1); + // If the cut affects all sequences, remove highlighted columns + if (allSequences) + { + viewport.getColumnSelection().removeElements(sg.getStartRes(), + sg.getEndRes() + 1); + } - protected void schemeKeyMenuItem_actionPerformed(ActionEvent e) - { - ColourKey colourKey = new ColourKey( alignPanel.seqPanel.getColourScheme() ); - Desktop.addInternalFrame(colourKey, "Colour scheme key", 400, 320); - } + if (seq.getSequence().length() < 1) + { + viewport.getAlignment().deleteSequence(seq); + } + else + { + viewport.getAlignment().getSequences().setElementAt(seq, index); + } + } + viewport.setSelectionGroup(null); + viewport.alignment.deleteGroup(sg); - public void sortPairwiseMenuItem_actionPerformed(ActionEvent e) + viewport.firePropertyChange("alignment", null, + viewport.getAlignment().getSequences()); + + + + if (viewport.getAlignment().getHeight() < 1) + { + try + { + this.setClosed(true); + } + catch (Exception ex) + { + } + } + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void deleteGroups_actionPerformed(ActionEvent e) { - addHistoryItem("sort"); - AlignmentSorter.sortByPID(viewport.getAlignment(), viewport.getAlignment().getSequenceAt(0)); - alignPanel.RefreshPanels(); + viewport.alignment.deleteAllGroups(); + viewport.setSelectionGroup(null); + alignPanel.repaint(); } - public void sortIDMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void selectAllSequenceMenuItem_actionPerformed(ActionEvent e) { - addHistoryItem("sort"); - AlignmentSorter.sortByID( viewport.getAlignment() ); - alignPanel.RefreshPanels(); + SequenceGroup sg = new SequenceGroup(); + + for (int i = 0; i < viewport.getAlignment().getSequences().size(); + i++) + { + sg.addSequence(viewport.getAlignment().getSequenceAt(i), false); + } + + sg.setEndRes(viewport.alignment.getWidth() - 1); + viewport.setSelectionGroup(sg); + PaintRefresher.Refresh(null, viewport.alignment); } - public void sortGroupMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void deselectAllSequenceMenuItem_actionPerformed(ActionEvent e) { - addHistoryItem("sort"); - AlignmentSorter.sortGroups(viewport.getAlignment()); - AlignmentSorter.sortGroups(viewport.getAlignment()); - alignPanel.RefreshPanels(); + viewport.setSelectionGroup(null); + viewport.getColumnSelection().clear(); + viewport.setSelectionGroup(null); + alignPanel.seqPanel.seqCanvas.highlightSearchResults(null); + alignPanel.annotationPanel.activeRes = null; + PaintRefresher.Refresh(null, viewport.alignment); } - public void sortTreeOrderMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void invertSequenceMenuItem_actionPerformed(ActionEvent e) { - addHistoryItem("sort"); - if(viewport.getCurrentTree()==null) + SequenceGroup sg = viewport.getSelectionGroup(); + + if (sg == null) + { + selectAllSequenceMenuItem_actionPerformed(null); + return; + } - AlignmentSorter.sortByTree(viewport.getAlignment(), viewport.getCurrentTree()); - alignPanel.RefreshPanels(); + for (int i = 0; i < viewport.getAlignment().getSequences().size(); + i++) + { + sg.addOrRemove(viewport.getAlignment().getSequenceAt(i), false); + } + + PaintRefresher.Refresh(null, viewport.alignment); } - public void removeRedundancyMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void remove2LeftMenuItem_actionPerformed(ActionEvent e) { + ColumnSelection colSel = viewport.getColumnSelection(); + + if (colSel.size() > 0) + { + addHistoryItem(new HistoryItem("Remove Left", viewport.alignment, + HistoryItem.HIDE)); + + int min = colSel.getMin(); + viewport.getAlignment().trimLeft(min); + colSel.compensateForEdit(0, min); + + if (viewport.getSelectionGroup() != null) + { + viewport.getSelectionGroup().adjustForRemoveLeft(min); + } + + Vector groups = viewport.alignment.getGroups(); + + for (int i = 0; i < groups.size(); i++) + { + SequenceGroup sg = (SequenceGroup) groups.get(i); + + if (!sg.adjustForRemoveLeft(min)) + { + viewport.alignment.deleteGroup(sg); + } + } + viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); + } } - public void pairwiseAlignmentMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void remove2RightMenuItem_actionPerformed(ActionEvent e) { - if(viewport.getSelection().size()<2) - JOptionPane.showMessageDialog(this, "You must select at least 2 sequences.", "Invalid Selection", JOptionPane.WARNING_MESSAGE); - else + ColumnSelection colSel = viewport.getColumnSelection(); + + if (colSel.size() > 0) { - JInternalFrame frame = new JInternalFrame(); - frame.setContentPane(new PairwiseAlignPanel(viewport)); - Desktop.addInternalFrame(frame, "Pairwise Alignment", 600, 500); + addHistoryItem(new HistoryItem("Remove Right", viewport.alignment, + HistoryItem.HIDE)); + + int max = colSel.getMax(); + viewport.getAlignment().trimRight(max); + + if (viewport.getSelectionGroup() != null) + { + viewport.getSelectionGroup().adjustForRemoveRight(max); + } + + Vector groups = viewport.alignment.getGroups(); + + for (int i = 0; i < groups.size(); i++) + { + SequenceGroup sg = (SequenceGroup) groups.get(i); + + if (!sg.adjustForRemoveRight(max)) + { + viewport.alignment.deleteGroup(sg); + } + } + + viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); } } - public void PCAMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void removeGappedColumnMenuItem_actionPerformed(ActionEvent e) + { + addHistoryItem(new HistoryItem("Remove Gapped Columns", + viewport.alignment, HistoryItem.HIDE)); + + //This is to maintain viewport position on first residue + //of first sequence + SequenceI seq = viewport.alignment.getSequenceAt(0); + int startRes = seq.findPosition(viewport.startRes); + + viewport.getAlignment().removeGaps(); + + viewport.setStartRes(seq.findIndex(startRes)-1); + + viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void removeAllGapsMenuItem_actionPerformed(ActionEvent e) { - if( (viewport.getSelection().size()<4 && viewport.getSelection().size()>0) - || viewport.getAlignment().getHeight()<4) + addHistoryItem(new HistoryItem("Remove Gaps", viewport.alignment, + HistoryItem.HIDE)); + + //This is to maintain viewport position on first residue + //of first sequence + SequenceI seq = viewport.alignment.getSequenceAt(0); + int startRes = seq.findPosition(viewport.startRes); + + + SequenceI current; + int jSize; + + Vector seqs = null; + + int start = 0; + int end = viewport.alignment.getWidth(); + + if (viewport.getSelectionGroup() != null + && viewport.getSelectionGroup().sequences != null + && viewport.getSelectionGroup().sequences.size() > 0) { - JOptionPane.showMessageDialog(this, "Principal component analysis must take\n" - +"at least 4 input sequences.", - "Sequence selection insufficient", - JOptionPane.WARNING_MESSAGE); - return; + seqs = viewport.getSelectionGroup().sequences; + start = viewport.getSelectionGroup().getStartRes(); + end = viewport.getSelectionGroup().getEndRes()+1; + } + else + { + seqs = viewport.alignment.getSequences(); } - try{ - PCAPanel pcaPanel = new PCAPanel(viewport, null); - JInternalFrame frame = new JInternalFrame(); - frame.setContentPane(pcaPanel); - Desktop.addInternalFrame(frame, "Principal component analysis", 400, 400); - }catch(java.lang.OutOfMemoryError ex) + for (int i = 0; i < seqs.size(); i++) + { + current = (SequenceI) seqs.elementAt(i); + jSize = current.getLength(); + + // Removing a range is much quicker than removing gaps + // one by one for long sequences + int j = start; + int rangeStart=-1, rangeEnd=-1; + + do + { + if (jalview.util.Comparison.isGap(current.getCharAt(j))) + { + if(rangeStart==-1) + { + rangeStart = j; + rangeEnd = j+1; + } + else + { + rangeEnd++; + } + j++; + } + else + { + if(rangeStart>-1) + { + current.deleteChars(rangeStart, rangeEnd); + j-=rangeEnd-rangeStart; + jSize-=rangeEnd-rangeStart; + rangeStart = -1; + rangeEnd = -1; + } + else + j++; + } + } + while (j < end && j < jSize); + if(rangeStart>-1) + { + current.deleteChars(rangeStart, rangeEnd); + } + } + + viewport.setStartRes(seq.findIndex(startRes)-1); + + viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); + } + + public void alignmentChanged() + { + if(viewport.vconsensus!=null) { - JOptionPane.showMessageDialog(this, "Too many sequences selected\nfor Principal Component Analysis!!", - "Out of memory", JOptionPane.WARNING_MESSAGE); + viewport.updateConsensus(); + viewport.updateConservation(); } + resetAllColourSchemes(); + if(alignPanel.overviewPanel!=null) + alignPanel.overviewPanel.updateOverviewImage(); + + alignPanel.repaint(); + } + + void resetAllColourSchemes() + { + ColourSchemeI cs = viewport.globalColourScheme; + if(cs!=null) + { + if (cs instanceof ClustalxColourScheme) + { + ( (ClustalxColourScheme) viewport.getGlobalColourScheme()). + resetClustalX(viewport.alignment.getSequences(), + viewport.alignment.getWidth()); + } + cs.setConsensus(viewport.vconsensus); + if (cs.conservationApplied()) + { + Alignment al = (Alignment) viewport.alignment; + Conservation c = new Conservation("All", + ResidueProperties.propHash, 3, + al.getSequences(), 0, + al.getWidth() - 1); + c.calculate(); + c.verdict(false, viewport.ConsPercGaps); + + cs.setConservation(c); + } + } + int s, sSize = viewport.alignment.getGroups().size(); + for(s=0; s 3) - { - tp = new TreePanel(viewport, viewport.getSelection().asVector(),type, pwType, - viewport.getStartRes(), viewport.getEndRes()); - } - else - { - tp = new TreePanel(viewport, viewport.getAlignment().getSequences(), - type, pwType, viewport.getStartRes(), - viewport.getEndRes()); - } + viewport.setShowJVSuffix(seqLimits.isSelected()); + + alignPanel.idPanel.idCanvas.setPreferredSize(alignPanel.calculateIdWidth()); + alignPanel.repaint(); + } - frame.setContentPane(tp); - Desktop.addInternalFrame(frame, title, 600, 500); + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void colourTextMenuItem_actionPerformed(ActionEvent e) + { + viewport.setColourText(colourTextMenuItem.isSelected()); + alignPanel.repaint(); } + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void wrapMenuItem_actionPerformed(ActionEvent e) + { + viewport.setWrapAlignment(wrapMenuItem.isSelected()); + alignPanel.setWrapAlignment(wrapMenuItem.isSelected()); + scaleAbove.setVisible(wrapMenuItem.isSelected()); + scaleLeft.setVisible(wrapMenuItem.isSelected()); + scaleRight.setVisible(wrapMenuItem.isSelected()); + alignPanel.repaint(); + } + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void scaleAbove_actionPerformed(ActionEvent e) + { + viewport.setScaleAboveWrapped(scaleAbove.isSelected()); + alignPanel.repaint(); + } - public void clustalAlignMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void scaleLeft_actionPerformed(ActionEvent e) { - JOptionPane.showMessageDialog(this, "Jalview is currently being reengineered" - +"\nwithin the Barton Group, Dundee University." - +"\nThis will be available as a web service 2005", - "Web service", JOptionPane.INFORMATION_MESSAGE); + viewport.setScaleLeftWrapped(scaleLeft.isSelected()); + alignPanel.repaint(); } + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void scaleRight_actionPerformed(ActionEvent e) + { + viewport.setScaleRightWrapped(scaleRight.isSelected()); + alignPanel.repaint(); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void viewBoxesMenuItem_actionPerformed(ActionEvent e) + { + viewport.setShowBoxes(viewBoxesMenuItem.isSelected()); + alignPanel.repaint(); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void viewTextMenuItem_actionPerformed(ActionEvent e) + { + viewport.setShowText(viewTextMenuItem.isSelected()); + alignPanel.repaint(); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void renderGapsMenuItem_actionPerformed(ActionEvent e) + { + viewport.setRenderGaps(renderGapsMenuItem.isSelected()); + alignPanel.repaint(); + } + + /** + * DOCUMENT ME! + * + * @param evt DOCUMENT ME! + */ + public void sequenceFeatures_actionPerformed(ActionEvent evt) + { + viewport.showSequenceFeatures(sequenceFeatures.isSelected()); + + if (viewport.showSequenceFeatures) + { + new SequenceFeatureFetcher(viewport. + alignment, + alignPanel); + } + + featureSettings.setEnabled(true); + + alignPanel.repaint(); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void annotationPanelMenuItem_actionPerformed(ActionEvent e) + { + viewport.setShowAnnotation(annotationPanelMenuItem.isSelected()); + alignPanel.setAnnotationVisible(annotationPanelMenuItem.isSelected()); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void overviewMenuItem_actionPerformed(ActionEvent e) + { + if (alignPanel.overviewPanel != null) + { + return; + } + + JInternalFrame frame = new JInternalFrame(); + OverviewPanel overview = new OverviewPanel(alignPanel); + frame.setContentPane(overview); + Desktop.addInternalFrame(frame, "Overview " + this.getTitle(), + frame.getWidth(), frame.getHeight()); + frame.pack(); + frame.setLayer(JLayeredPane.PALETTE_LAYER); + frame.addInternalFrameListener(new javax.swing.event.InternalFrameAdapter() + { + public void internalFrameClosed( + javax.swing.event.InternalFrameEvent evt) + { + alignPanel.setOverviewPanel(null); + } + ; + }); + + alignPanel.setOverviewPanel(overview); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void noColourmenuItem_actionPerformed(ActionEvent e) + { + changeColour(null); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void clustalColour_actionPerformed(ActionEvent e) + { + changeColour(new ClustalxColourScheme( + viewport.alignment.getSequences(), viewport.alignment.getWidth())); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void zappoColour_actionPerformed(ActionEvent e) + { + changeColour(new ZappoColourScheme()); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void taylorColour_actionPerformed(ActionEvent e) + { + changeColour(new TaylorColourScheme()); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void hydrophobicityColour_actionPerformed(ActionEvent e) + { + changeColour(new HydrophobicColourScheme()); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void helixColour_actionPerformed(ActionEvent e) + { + changeColour(new HelixColourScheme()); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void strandColour_actionPerformed(ActionEvent e) + { + changeColour(new StrandColourScheme()); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void turnColour_actionPerformed(ActionEvent e) + { + changeColour(new TurnColourScheme()); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void buriedColour_actionPerformed(ActionEvent e) + { + changeColour(new BuriedColourScheme()); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void nucleotideColour_actionPerformed(ActionEvent e) + { + changeColour(new NucleotideColourScheme()); + } + + public void annotationColour_actionPerformed(ActionEvent e) + { + new AnnotationColourChooser(viewport, alignPanel); + } + + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void applyToAllGroups_actionPerformed(ActionEvent e) + { + viewport.setColourAppliesToAllGroups(applyToAllGroups.isSelected()); + } + + /** + * DOCUMENT ME! + * + * @param cs DOCUMENT ME! + */ + void changeColour(ColourSchemeI cs) + { + int threshold = 0; + + if(cs!=null) + { + if (viewport.getAbovePIDThreshold()) + { + threshold = SliderPanel.setPIDSliderSource(alignPanel, cs, + "Background"); + + cs.setThreshold(threshold, + viewport.getIgnoreGapsConsensus()); + + viewport.setGlobalColourScheme(cs); + } + else + { + cs.setThreshold(0, viewport.getIgnoreGapsConsensus()); + } + + if (viewport.getConservationSelected()) + { + + Alignment al = (Alignment) viewport.alignment; + Conservation c = new Conservation("All", + ResidueProperties.propHash, 3, + al.getSequences(), 0, + al.getWidth() - 1); + + c.calculate(); + c.verdict(false, viewport.ConsPercGaps); + + cs.setConservation(c); + + cs.setConservationInc(SliderPanel.setConservationSlider(alignPanel, cs, + "Background")); + } + else + { + cs.setConservation(null); + } + + cs.setConsensus(viewport.vconsensus); + } + + viewport.setGlobalColourScheme(cs); + + if (viewport.getColourAppliesToAllGroups()) + { + Vector groups = viewport.alignment.getGroups(); + + for (int i = 0; i < groups.size(); i++) + { + SequenceGroup sg = (SequenceGroup) groups.elementAt(i); + + if (cs == null) + { + sg.cs = null; + continue; + } + + if (cs instanceof ClustalxColourScheme) + { + sg.cs = new ClustalxColourScheme(sg.sequences, sg.getWidth()); + } + else if (cs instanceof UserColourScheme) + { + sg.cs = new UserColourScheme( ( (UserColourScheme) cs).getColours()); + } + else + { + try + { + sg.cs = (ColourSchemeI) cs.getClass().newInstance(); + } + catch (Exception ex) + { + } + } + + if (viewport.getAbovePIDThreshold() + || cs instanceof PIDColourScheme + || cs instanceof Blosum62ColourScheme) + { + sg.cs.setThreshold(threshold, + viewport.getIgnoreGapsConsensus()); + + sg.cs.setConsensus(AAFrequency.calculate(sg.sequences, 0, + sg.getWidth())); + } + else + sg.cs.setThreshold(0, viewport.getIgnoreGapsConsensus()); + + + if (viewport.getConservationSelected()) + { + Conservation c = new Conservation("Group", + ResidueProperties.propHash, 3, + sg.sequences, 0, + viewport.alignment.getWidth() - 1); + c.calculate(); + c.verdict(false, viewport.ConsPercGaps); + sg.cs.setConservation(c); + } + else + sg.cs.setConservation(null); + } + } + + if (alignPanel.getOverviewPanel() != null) + { + alignPanel.getOverviewPanel().updateOverviewImage(); + } + + alignPanel.repaint(); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void modifyPID_actionPerformed(ActionEvent e) + { + if (viewport.getAbovePIDThreshold() && viewport.globalColourScheme!=null) + { + SliderPanel.setPIDSliderSource(alignPanel, + viewport.getGlobalColourScheme(), + "Background"); + SliderPanel.showPIDSlider(); + } + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void modifyConservation_actionPerformed(ActionEvent e) + { + if (viewport.getConservationSelected() && viewport.globalColourScheme!=null) + { + SliderPanel.setConservationSlider(alignPanel, + viewport.globalColourScheme, + "Background"); + SliderPanel.showConservationSlider(); + } + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void conservationMenuItem_actionPerformed(ActionEvent e) + { + viewport.setConservationSelected(conservationMenuItem.isSelected()); + + viewport.setAbovePIDThreshold(false); + abovePIDThreshold.setSelected(false); + + changeColour(viewport.getGlobalColourScheme()); + + modifyConservation_actionPerformed(null); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void abovePIDThreshold_actionPerformed(ActionEvent e) + { + viewport.setAbovePIDThreshold(abovePIDThreshold.isSelected()); + + conservationMenuItem.setSelected(false); + viewport.setConservationSelected(false); + + changeColour(viewport.getGlobalColourScheme()); + + modifyPID_actionPerformed(null); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void userDefinedColour_actionPerformed(ActionEvent e) + { + if (e.getActionCommand().equals("User Defined...")) + { + new UserDefinedColours(alignPanel, null); + } + else + { + UserColourScheme udc = (UserColourScheme) UserDefinedColours. + getUserColourSchemes().get(e.getActionCommand()); + + changeColour(udc); + } + } + + public void updateUserColourMenu() + { + + Component[] menuItems = colourMenu.getMenuComponents(); + int i, iSize = menuItems.length; + for (i = 0; i < iSize; i++) + { + if (menuItems[i].getName() != null && + menuItems[i].getName().equals("USER_DEFINED")) + { + colourMenu.remove(menuItems[i]); + iSize--; + } + } + if (jalview.gui.UserDefinedColours.getUserColourSchemes() != null) + { + java.util.Enumeration userColours = jalview.gui.UserDefinedColours. + getUserColourSchemes().keys(); + + while (userColours.hasMoreElements()) + { + final JRadioButtonMenuItem radioItem = new JRadioButtonMenuItem(userColours. + nextElement().toString()); + radioItem.setName("USER_DEFINED"); + radioItem.addMouseListener(new MouseAdapter() + { + public void mousePressed(MouseEvent evt) + { + if(evt.isControlDown() || SwingUtilities.isRightMouseButton(evt)) + { + radioItem.removeActionListener(radioItem.getActionListeners()[0]); + + int option = JOptionPane.showInternalConfirmDialog(jalview.gui.Desktop.desktop, + "Remove from default list?", + "Remove user defined colour", + JOptionPane.YES_NO_OPTION); + if(option == JOptionPane.YES_OPTION) + { + jalview.gui.UserDefinedColours.removeColourFromDefaults(radioItem.getText()); + colourMenu.remove(radioItem); + } + else + radioItem.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent evt) + { + userDefinedColour_actionPerformed(evt); + } + }); + } + } + }); + radioItem.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent evt) + { + userDefinedColour_actionPerformed(evt); + } + }); + + colourMenu.insert(radioItem, 15); + colours.add(radioItem); + } + } + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void PIDColour_actionPerformed(ActionEvent e) + { + changeColour(new PIDColourScheme()); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void BLOSUM62Colour_actionPerformed(ActionEvent e) + { + changeColour(new Blosum62ColourScheme()); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void sortPairwiseMenuItem_actionPerformed(ActionEvent e) + { + addHistoryItem(new HistoryItem("Pairwise Sort", viewport.alignment, + HistoryItem.SORT)); + AlignmentSorter.sortByPID(viewport.getAlignment(), + viewport.getAlignment().getSequenceAt(0)); + alignPanel.repaint(); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void sortIDMenuItem_actionPerformed(ActionEvent e) + { + addHistoryItem(new HistoryItem("ID Sort", viewport.alignment, + HistoryItem.SORT)); + AlignmentSorter.sortByID(viewport.getAlignment()); + alignPanel.repaint(); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void sortGroupMenuItem_actionPerformed(ActionEvent e) + { + addHistoryItem(new HistoryItem("Group Sort", viewport.alignment, + HistoryItem.SORT)); + + AlignmentSorter.sortByGroup(viewport.getAlignment()); + alignPanel.repaint(); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void removeRedundancyMenuItem_actionPerformed(ActionEvent e) + { + RedundancyPanel sp = new RedundancyPanel(alignPanel, this); + JInternalFrame frame = new JInternalFrame(); + frame.setContentPane(sp); + Desktop.addInternalFrame(frame, "Redundancy threshold selection", 400, + 100, false); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void pairwiseAlignmentMenuItem_actionPerformed(ActionEvent e) + { + if ( (viewport.getSelectionGroup() == null) || + (viewport.getSelectionGroup().getSize() < 2)) + { + JOptionPane.showInternalMessageDialog(this, + "You must select at least 2 sequences.", + "Invalid Selection", + JOptionPane.WARNING_MESSAGE); + } + else + { + JInternalFrame frame = new JInternalFrame(); + frame.setContentPane(new PairwiseAlignPanel(viewport)); + Desktop.addInternalFrame(frame, "Pairwise Alignment", 600, 500); + } + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void PCAMenuItem_actionPerformed(ActionEvent e) + { + if ( ( (viewport.getSelectionGroup() != null) && + (viewport.getSelectionGroup().getSize() < 4) && + (viewport.getSelectionGroup().getSize() > 0)) || + (viewport.getAlignment().getHeight() < 4)) + { + JOptionPane.showInternalMessageDialog(this, + "Principal component analysis must take\n" + + "at least 4 input sequences.", + "Sequence selection insufficient", + JOptionPane.WARNING_MESSAGE); + + return; + } + + new PCAPanel(viewport); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void averageDistanceTreeMenuItem_actionPerformed(ActionEvent e) + { + NewTreePanel("AV", "PID", "Average distance tree using PID"); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void neighbourTreeMenuItem_actionPerformed(ActionEvent e) + { + NewTreePanel("NJ", "PID", "Neighbour joining tree using PID"); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void njTreeBlosumMenuItem_actionPerformed(ActionEvent e) + { + NewTreePanel("NJ", "BL", "Neighbour joining tree using BLOSUM62"); + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void avTreeBlosumMenuItem_actionPerformed(ActionEvent e) + { + NewTreePanel("AV", "BL", "Average distance tree using BLOSUM62"); + } + + /** + * DOCUMENT ME! + * + * @param type DOCUMENT ME! + * @param pwType DOCUMENT ME! + * @param title DOCUMENT ME! + */ + void NewTreePanel(String type, String pwType, String title) + { + final TreePanel tp; + + if ( (viewport.getSelectionGroup() != null) && + (viewport.getSelectionGroup().getSize() > 3)) + { + int s = 0; + SequenceGroup sg = viewport.getSelectionGroup(); + + /* Decide if the selection is a column region */ + while (s < sg.sequences.size()) + { + if ( ( (SequenceI) sg.sequences.elementAt(s++)).getLength() < + sg.getEndRes()) + { + JOptionPane.showMessageDialog(Desktop.desktop, + "The selected region to create a tree may\nonly contain residues or gaps.\n" + + "Try using the Pad function in the edit menu,\n" + + "or one of the multiple sequence alignment web services.", + "Sequences in selection are not aligned", + JOptionPane.WARNING_MESSAGE); + + return; + } + } + + title = title + " on region"; + tp = new TreePanel(viewport, + viewport.getSelectionGroup().sequences, type, pwType, + sg.getStartRes(), sg.getEndRes()); + } + else + { + //are the sequences aligned? + if (!viewport.alignment.isAligned()) + { + JOptionPane.showMessageDialog(Desktop.desktop, + "The sequences must be aligned before creating a tree.\n" + + "Try using the Pad function in the edit menu,\n" + + "or one of the multiple sequence alignment web services.", + "Sequences not aligned", + JOptionPane.WARNING_MESSAGE); + + return; + } + + tp = new TreePanel(viewport, + viewport.getAlignment().getSequences(), type, pwType, + 0, + viewport.alignment.getWidth()); + } + + addTreeMenuItem(tp, title); + viewport.setCurrentTree(tp.getTree()); + + Desktop.addInternalFrame(tp, title + " from " + this.title, 600, 500); + } + + /** + * DOCUMENT ME! + * + * @param title DOCUMENT ME! + * @param order DOCUMENT ME! + */ + public void addSortByOrderMenuItem(String title, final AlignmentOrder order) + { + final JMenuItem item = new JMenuItem("by " + title); + sort.add(item); + item.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(ActionEvent e) + { + addHistoryItem(new HistoryItem("Sort", viewport.alignment, + HistoryItem.SORT)); + + // TODO: JBPNote - have to map order entries to curent SequenceI pointers + AlignmentSorter.sortBy(viewport.getAlignment(), order); + alignPanel.repaint(); + } + }); + } + + /** + * Maintain the Order by->Displayed Tree menu. + * Creates a new menu item for a TreePanel with an appropriate + * jalview.analysis.AlignmentSorter call. Listeners are added + * to remove the menu item when the treePanel is closed, and adjust + * the tree leaf to sequence mapping when the alignment is modified. + * @param treePanel Displayed tree window. + * @param title SortBy menu item title. + */ + void addTreeMenuItem(final TreePanel treePanel, String title) + { + final JMenuItem item = new JMenuItem(title); + + treeCount++; + + if (treeCount == 1) + { + sort.add(sortByTreeMenu); + } + + sortByTreeMenu.add(item); + item.addActionListener(new java.awt.event.ActionListener() + { + public void actionPerformed(ActionEvent e) + { + addHistoryItem(new HistoryItem("Tree Sort", + viewport.alignment, HistoryItem.SORT)); + AlignmentSorter.sortByTree(viewport.getAlignment(), + treePanel.getTree()); + alignPanel.repaint(); + } + }); + + treePanel.addInternalFrameListener(new javax.swing.event. + InternalFrameAdapter() + { + public void internalFrameClosed( + javax.swing.event.InternalFrameEvent evt) + { + treeCount--; + sortByTreeMenu.remove(item); + + if (treeCount == 0) + { + sort.remove(sortByTreeMenu); + } + } + ; + }); + } + + /** + * Work out whether the whole set of sequences + * or just the selected set will be submitted for multiple alignment. + * + */ + private SequenceI[] gatherSequencesForAlignment() + { + // Now, check we have enough sequences + SequenceI[] msa = null; + + if ( (viewport.getSelectionGroup() != null) && + (viewport.getSelectionGroup().getSize() > 1)) + { + // JBPNote UGLY! To prettify, make SequenceGroup and Alignment conform to some common interface! + SequenceGroup seqs = viewport.getSelectionGroup(); + int sz; + msa = new SequenceI[sz = seqs.getSize()]; + + for (int i = 0; i < sz; i++) + { + msa[i] = (SequenceI) seqs.getSequenceAt(i); + } + } + else + { + Vector seqs = viewport.getAlignment().getSequences(); + + if (seqs.size() > 1) + { + msa = new SequenceI[seqs.size()]; + + for (int i = 0; i < seqs.size(); i++) + { + msa[i] = (SequenceI) seqs.elementAt(i); + } + } + } + return msa; + } + + /** + * Decides what is submitted to a secondary structure prediction service, + * the currently selected sequence, or the currently selected alignment + * (where the first sequence in the set is the one that the prediction + * will be for). + */ + SequenceI[] gatherSeqOrMsaForSecStrPrediction() + { + SequenceI seq = null; + SequenceI[] msa = null; + + if ( (viewport.getSelectionGroup() != null) && + (viewport.getSelectionGroup().getSize() > 0)) + { + // JBPNote UGLY! To prettify, make SequenceGroup and Alignment conform to some common interface! + SequenceGroup seqs = viewport.getSelectionGroup(); + + if ( (seqs.getSize() == 1) || !viewport.alignment.isAligned()) + { + seq = (SequenceI) seqs.getSequenceAt(0); + } + else + { + int sz; + msa = new SequenceI[sz = seqs.getSize()]; + + for (int i = 0; i < sz; i++) + { + msa[i] = (SequenceI) seqs.getSequenceAt(i); + } + } + } + else + { + Vector seqs = viewport.getAlignment().getSequences(); + + if ( (seqs.size() == 1) || !viewport.alignment.isAligned()) + { + seq = (SequenceI) seqs.elementAt(0); + } + else + { + msa = new SequenceI[seqs.size()]; + + for (int i = 0; i < seqs.size(); i++) + { + msa[i] = (SequenceI) seqs.elementAt(i); + } + } + } + if (msa != null) + { + return msa; + } + else + { + if (seq != null) + { + return new SequenceI[] + { + seq}; + } + } + return null; + } + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void LoadtreeMenuItem_actionPerformed(ActionEvent e) + { + // Pick the tree file + JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache. + getProperty( + "LAST_DIRECTORY")); + chooser.setFileView(new JalviewFileView()); + chooser.setDialogTitle("Select a newick-like tree file"); + chooser.setToolTipText("Load a tree file"); + + int value = chooser.showOpenDialog(null); + + if (value == JalviewFileChooser.APPROVE_OPTION) + { + String choice = chooser.getSelectedFile().getPath(); + jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice); + + try + { + jalview.io.NewickFile fin = new jalview.io.NewickFile(choice, + "File"); + viewport.setCurrentTree(ShowNewickTree(fin, choice).getTree()); + } + catch (Exception ex) + { + JOptionPane.showMessageDialog(Desktop.desktop, + "Problem reading tree file", + ex.getMessage(), + JOptionPane.WARNING_MESSAGE); + ex.printStackTrace(); + } + } + } + + + public TreePanel ShowNewickTree(NewickFile nf, String title) + { + return ShowNewickTree(nf,title,600,500,4,5); + } + /** + * DOCUMENT ME! + * + * @param nf DOCUMENT ME! + * @param title DOCUMENT ME! + * + * @return DOCUMENT ME! + */ + public TreePanel ShowNewickTree(NewickFile nf, String title, int w,int h,int x, int y) + { + TreePanel tp = null; + + try + { + nf.parse(); + + if (nf.getTree() != null) + { + tp = new TreePanel(viewport, + viewport.getAlignment().getSequences(), nf, + "FromFile", + title); + + tp.setSize(w,h); + + if(x>0 && y>0) + tp.setLocation(x,y); + + + Desktop.addInternalFrame(tp, title, w, h); + addTreeMenuItem(tp, title); + } + } + catch (Exception ex) + { + ex.printStackTrace(); + } + + return tp; + } + + class PrintThread + extends Thread + { + public void run() + { + PrinterJob printJob = PrinterJob.getPrinterJob(); + PageFormat pf = printJob.pageDialog(printJob.defaultPage()); + printJob.setPrintable(alignPanel, pf); + + if (printJob.printDialog()) + { + try + { + printJob.print(); + } + catch (Exception PrintException) + { + PrintException.printStackTrace(); + } + } + } + } + + /** + * Generates menu items and listener event actions for web service clients + * + */ + public void BuildWebServiceMenu() + { + if ( (Discoverer.services != null) + && (Discoverer.services.size() > 0)) + { + Vector msaws = (Vector) Discoverer.services.get("MsaWS"); + Vector secstrpr = (Vector) Discoverer.services.get("SecStrPred"); + Vector wsmenu = new Vector(); + if (msaws != null) + { + // Add any Multiple Sequence Alignment Services + final JMenu msawsmenu = new JMenu("Alignment"); + for (int i = 0, j = msaws.size(); i < j; i++) + { + final ext.vamsas.ServiceHandle sh = (ext.vamsas.ServiceHandle) msaws. + get(i); + final JMenuItem method = new JMenuItem(sh.getName()); + method.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + SequenceI[] msa = gatherSequencesForAlignment(); + new jalview.ws.MsaWSClient(sh, title, msa, + false, true, viewport.getAlignment().getDataset()); + + } + + }); + msawsmenu.add(method); + // Deal with services that we know accept partial alignments. + if (sh.getName().indexOf("lustal") > -1) + { + // We know that ClustalWS can accept partial alignments for refinement. + final JMenuItem methodR = new JMenuItem(sh.getName()+" Realign"); + methodR.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + SequenceI[] msa = gatherSequencesForAlignment(); + new jalview.ws.MsaWSClient(sh, title, msa, + true, true, viewport.getAlignment().getDataset()); + + } + + }); + msawsmenu.add(methodR); + + } + } + wsmenu.add(msawsmenu); + } + if (secstrpr != null) + { + // Add any secondary structure prediction services + final JMenu secstrmenu = new JMenu("Secondary Structure Prediction"); + for (int i = 0, j = secstrpr.size(); i < j; i++) + { + final ext.vamsas.ServiceHandle sh = (ext.vamsas.ServiceHandle) + secstrpr.get(i); + final JMenuItem method = new JMenuItem(sh.getName()); + method.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + SequenceI[] msa = gatherSeqOrMsaForSecStrPrediction(); + if (msa.length == 1) + { + // Single Sequence prediction + new jalview.ws.JPredClient(sh,title, msa[0]); + } + else + { + if (msa.length > 1) + { + // Single Sequence prediction + jalview.ws.JPredClient ct = new jalview.ws.JPredClient(sh, + title, msa); + } + } + } + }); + secstrmenu.add(method); + } + wsmenu.add(secstrmenu); + } + this.webService.removeAll(); + for (int i = 0, j = wsmenu.size(); i < j; i++) + { + webService.add( (JMenu) wsmenu.get(i)); + } + } + else + { + this.webService.removeAll(); + this.webService.add(this.webServiceNoServices); + } + // TODO: add in rediscovery function + // TODO: reduce code redundancy. + // TODO: group services by location as well as function. + } + + /* public void vamsasStore_actionPerformed(ActionEvent e) + { + JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache. + getProperty("LAST_DIRECTORY")); + + chooser.setFileView(new JalviewFileView()); + chooser.setDialogTitle("Export to Vamsas file"); + chooser.setToolTipText("Export"); + + int value = chooser.showSaveDialog(this); + + if (value == JalviewFileChooser.APPROVE_OPTION) + { + jalview.io.VamsasDatastore vs = new jalview.io.VamsasDatastore(viewport); + //vs.store(chooser.getSelectedFile().getAbsolutePath() ); + vs.storeJalview( chooser.getSelectedFile().getAbsolutePath(), this); + } + }*/ + + public void featureSettings_actionPerformed(ActionEvent e) + { + new FeatureSettings(viewport, alignPanel); + } + + + +public void showTranslation_actionPerformed(ActionEvent e) +{ + int s, sSize = viewport.alignment.getHeight(); + SequenceI [] newSeq = new SequenceI[sSize]; + + int res, resSize; + StringBuffer protein; + String seq; + for(s=0; s