X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FAlignFrame.java;h=ccf7303f35160d152b5f9854d58296cc4b07c7fb;hb=b01c4edc00dbeedd6b53777310d4232a4104a9ec;hp=b0d1b3250fe9556dee6384b24e035d578fe4a578;hpb=a83810ff98dde4f414fa348af95f41ee852b3576;p=jalview.git diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index b0d1b32..ccf7303 100755 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -1,971 +1,2791 @@ -/******************** - * 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 jalview.jbgui.GAlignFrame; -import jalview.schemes.*; -import jalview.datamodel.*; -import jalview.analysis.*; -import jalview.io.*; -import jalview.ws.*; -import MCview.*; +import java.beans.*; +import java.io.*; +import java.util.*; + import java.awt.*; +import java.awt.datatransfer.*; import java.awt.event.*; import java.awt.print.*; import javax.swing.*; -import javax.swing.event.*; -import java.util.*; -import jalview.datamodel.SequenceI; -public class AlignFrame extends GAlignFrame +import jalview.analysis.*; +import jalview.datamodel.*; +import jalview.io.*; +import jalview.jbgui.*; +import jalview.schemes.*; +import jalview.ws.*; +import java.awt.dnd.*; + + +/** + * DOCUMENT ME! + * + * @author $author$ + * @version $Revision$ + */ +public class AlignFrame + extends GAlignFrame implements ClipboardOwner, DropTargetListener { - final AlignmentPanel alignPanel; - final AlignViewport viewport; - public AlignFrame(AlignmentI al) - { - super(); + /** DOCUMENT ME!! */ + public static final int NEW_WINDOW_WIDTH = 700; - 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()+""); - } + /** DOCUMENT ME!! */ + public static final int NEW_WINDOW_HEIGHT = 500; + AlignmentPanel alignPanel; + AlignViewport 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){} - } + 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; - 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())); - } - protected void htmlMenuItem_actionPerformed(ActionEvent e) + /** + * Creates a new AlignFrame object. + * + * @param al DOCUMENT ME! + */ + public AlignFrame(AlignmentI al) { - HTMLOutput htmlOutput = new HTMLOutput(viewport); - htmlOutput = null; - } + viewport = new AlignViewport(al); + viewports.add(viewport); - public void printMenuItem_actionPerformed(ActionEvent e) - { - //Putting in a thread avoids Swing painting problems - PrintThread thread = new PrintThread(); - thread.start(); - } + this.setDropTarget(new java.awt.dnd.DropTarget(this, this)); - class PrintThread extends Thread - { - public void run() + if(viewport.vconsensus==null) { - 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(); - } - } + //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); + String sortby = jalview.bin.Cache.getDefault("SORT_ALIGNMENT", "No sort"); + if(sortby.equals("Id")) + sortIDMenuItem_actionPerformed(null); + else if(sortby.equals("Pairwise Identity")) + sortPairwiseMenuItem_actionPerformed(null); + // 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(); - void updateEditMenuBar() - { - if(historyList.size()>0) - { - undoMenuItem.setEnabled(true); - Object [] history = (Object[])historyList.get(0); - undoMenuItem.setText("Undo "+history[0]); - } - else + // tabbedPane.add(al.isNucleotide() ? "DNA":"Protein", alignPanel); + + ///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); + } - public void deselectAllColumnsMenuItem_actionPerformed(ActionEvent e) - { - viewport.getColumnSelection().clear(); - repaint(); - } + new Jalview2XML().SaveAlignment(this, file, shortName); - public void remove2LeftMenuItem_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(); + // USE Jalview2XML to save this file + return true; } - } - - public void remove2RightMenuItem_actionPerformed(ActionEvent e) - { - addHistoryItem("delete columns"); - ColumnSelection colSel = viewport.getColumnSelection(); - if (colSel.size() > 0) + else { - int max = colSel.getMax(); - if(max>1) - viewport.getAlignment().trimRight(max); + String output = new FormatAdapter().formatSequences(format, + viewport.getAlignment(). + getSequences()); + if (output == null) + { + return false; + } - alignPanel.RefreshPanels(); - } + try + { + java.io.PrintWriter out = new java.io.PrintWriter( + new java.io.FileWriter(file)); + out.print(output); + out.close(); + return true; + } + catch (Exception ex) + { + ex.printStackTrace(); + } + } + return false; } - public void removeGappedColumnMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void outputText_actionPerformed(ActionEvent e) { - addHistoryItem("delete gapped columns"); - viewport.getAlignment().removeGaps(); - alignPanel.RefreshPanels(); + 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 removeAllGapsMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void htmlMenuItem_actionPerformed(ActionEvent e) { - 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(jalview.util.Comparison.isGap(current.getCharAt(j))) - { - current.deleteCharAt(j); - j--; - jSize--; - } - } - - alignPanel.RefreshPanels(); + new HTMLOutput(viewport, + alignPanel.seqPanel.seqCanvas.getSequenceRenderer(), + alignPanel.seqPanel.seqCanvas.getFeatureRenderer()); } - public void setGapCharMenuItem_actionPerformed(ActionEvent e) + public void createImageMap(File file, String image) { - char thisChar = '-'; - char nextChar = '.'; - if(viewport.getGapCharacter()=='-') - { - thisChar = '.'; - nextChar = '-'; - } - setGapCharMenuItem.setText("Set gap character to \""+nextChar+"\""); - viewport.setGapCharacter(thisChar); - alignPanel.RefreshPanels(); + alignPanel.makePNGImageMap(file, image); } - public void findMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void createPNG(File f) { - JOptionPane op = new JOptionPane(); - JInternalFrame frame = op.createInternalFrame(this, "Find"); - Finder finder = new Finder(viewport, alignPanel, frame); - frame.setClosable(true); - frame.setContentPane(finder); - frame.setSize(340,110); - frame.setVisible(true); + alignPanel.makePNG(f); } + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void createEPS(File f) + { + alignPanel.makeEPS(f); + } + /** + * 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 fontNameMenuItem_actionPerformed(ActionEvent e) + public void associatedData_actionPerformed(ActionEvent e) { - String fonts[] = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); + // Pick the tree file + JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache. + getProperty( + "LAST_DIRECTORY")); + chooser.setFileView(new JalviewFileView()); + chooser.setDialogTitle("Load Jalview Annotations or Features File"); + chooser.setToolTipText("Load Jalview Annotations / Features file"); + int value = chooser.showOpenDialog(null); - String selection = JOptionPane.showInternalInputDialog(this, - "Select font", - "Font selection", - JOptionPane.QUESTION_MESSAGE, - null,fonts - ,fontNameMenuItem.getText()).toString(); - if(selection!=null) + if (value == JalviewFileChooser.APPROVE_OPTION) { - fontNameMenuItem.setText(selection); - SetFont(); - } + String choice = chooser.getSelectedFile().getPath(); + jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice); + loadJalviewDataFile(choice); + } } - public void fontSizeMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void closeMenuItem_actionPerformed(ActionEvent e) { - String selection = JOptionPane.showInternalInputDialog(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(); - } + try + { + PaintRefresher.components.remove(viewport.alignment); + this.setClosed(true); + } + catch (Exception ex) + { + } } - public void fontStyleMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + */ + void updateEditMenuBar() { - String selection = JOptionPane.showInternalInputDialog(this, - "Select font style", - "Font style", - JOptionPane.QUESTION_MESSAGE, - null, new String[]{"plain", "bold", "italic"} - ,fontStyleMenuItem.getText()).toString(); - if(selection!=null) + if (historyList.size() > 0) + { + undoMenuItem.setEnabled(true); + + HistoryItem hi = (HistoryItem) historyList.peek(); + undoMenuItem.setText("Undo " + hi.getDescription()); + } + else { - fontStyleMenuItem.setText(selection); - SetFont(); + undoMenuItem.setEnabled(false); + undoMenuItem.setText("Undo"); } - } + if (redoList.size() > 0) + { + redoMenuItem.setEnabled(true); - protected void colourTextMenuItem_actionPerformed(ActionEvent e) - { - viewport.setColourText( colourTextMenuItem.isSelected() ); - alignPanel.RefreshPanels(); + HistoryItem hi = (HistoryItem) redoList.peek(); + redoMenuItem.setText("Redo " + hi.getDescription()); + } + else + { + redoMenuItem.setEnabled(false); + redoMenuItem.setText("Redo"); + } } - void SetFont() + /** + * DOCUMENT ME! + * + * @param hi DOCUMENT ME! + */ + public void addHistoryItem(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(); - + historyList.push(hi); + updateEditMenuBar(); } - protected void wrapMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void undoMenuItem_actionPerformed(ActionEvent e) { - viewport.setWrapAlignment( wrapMenuItem.isSelected() ); - alignPanel.RefreshPanels(); + HistoryItem hi = (HistoryItem) historyList.pop(); + redoList.push(new HistoryItem(hi.getDescription(), viewport.alignment, + HistoryItem.HIDE)); + restoreHistoryItem(hi); + viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); } - - public void viewBoxesMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void redoMenuItem_actionPerformed(ActionEvent e) { - viewport.setShowBoxes( viewBoxesMenuItem.isSelected() ); - alignPanel.RefreshPanels(); + HistoryItem hi = (HistoryItem) redoList.pop(); + restoreHistoryItem(hi); + updateEditMenuBar(); + viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); } - public void viewTextMenuItem_actionPerformed(ActionEvent e) + // used by undo and redo + void restoreHistoryItem(HistoryItem hi) { - viewport.setShowText( viewTextMenuItem.isSelected() ); - alignPanel.RefreshPanels(); - } + hi.restore(); - protected void renderGapsMenuItem_actionPerformed(ActionEvent e) - { - viewport.setRenderGaps(renderGapsMenuItem.isSelected()); - alignPanel.RefreshPanels(); - } + updateEditMenuBar(); - public void secondaryStructure_actionPerformed(ActionEvent evt) - { - alignPanel.setSecondaryStructureVisible(secondaryStructure.isSelected()); + viewport.firePropertyChange("alignment", null, + viewport.getAlignment().getSequences()); } - public void consensusGraphMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param up DOCUMENT ME! + */ + public void moveSelectedSequences(boolean up) { - alignPanel.setGraphPanelVisible( consensusGraphMenuItem.isSelected() ); - } + SequenceGroup sg = viewport.getSelectionGroup(); - public void overviewMenuItem_actionPerformed(ActionEvent e) - { - if (alignPanel.overviewPanel != null) + if (sg == null) + { return; + } - 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) + if (up) + { + for (int i = 1; i < viewport.alignment.getHeight(); i++) { - alignPanel.setOverviewPanel(null); - }; - }); - alignPanel.setOverviewPanel( overview ); + SequenceI seq = viewport.alignment.getSequenceAt(i); - }catch(java.lang.OutOfMemoryError ex) - { - JOptionPane.showInternalMessageDialog(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; + } - protected void noColourmenuItem_actionPerformed(ActionEvent e) - { - viewport.setGlobalColourScheme( null ); - changeColour(); - } + viewport.alignment.getSequences().setElementAt(temp, i); + viewport.alignment.getSequences().setElementAt(seq, i - 1); + } + } + else + { + for (int i = viewport.alignment.getHeight() - 2; i > -1; i--) + { + SequenceI seq = viewport.alignment.getSequenceAt(i); + if (!sg.sequences.contains(seq)) + { + continue; + } - public void clustalColour_actionPerformed(ActionEvent e) - { - viewport.setGlobalColourScheme(new ClustalxColourScheme( )); - conservationMenuItem.setSelected(false); - abovePIDThreshold.setSelected(false); - changeColour(); - } - - public void zappoColour_actionPerformed(ActionEvent e) - { - viewport.setGlobalColourScheme(new ZappoColourScheme() ); - changeColour(); - } - - public void taylorColour_actionPerformed(ActionEvent e) - { - viewport.setGlobalColourScheme(new TaylorColourScheme() ); - changeColour(); - } + SequenceI temp = viewport.alignment.getSequenceAt(i + 1); + if (sg.sequences.contains(temp)) + { + continue; + } - public void hydrophobicityColour_actionPerformed(ActionEvent e) - { - viewport.setGlobalColourScheme( new HydrophobicColourScheme() ); - changeColour(); - } + viewport.alignment.getSequences().setElementAt(temp, i); + viewport.alignment.getSequences().setElementAt(seq, i + 1); + } + } - public void helixColour_actionPerformed(ActionEvent e) - { - viewport.setGlobalColourScheme( new HelixColourScheme() ); - changeColour(); + alignPanel.repaint(); } - - public void strandColour_actionPerformed(ActionEvent e) + public void lostOwnership(Clipboard clipboard, Transferable contents) { - viewport.setGlobalColourScheme(new StrandColourScheme() ); - changeColour(); + Desktop.jalviewClipboard = null; } - public void turnColour_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void copy_actionPerformed(ActionEvent e) { - viewport.setGlobalColourScheme(new TurnColourScheme() ); - changeColour(); - } - + if (viewport.getSelectionGroup() == null) + { + return; + } - public void buriedColour_actionPerformed(ActionEvent e) - { - viewport.setGlobalColourScheme( new BuriedColourScheme() ); - changeColour(); - } + SequenceGroup sg = viewport.getSelectionGroup(); - public void nucleotideColour_actionPerformed(ActionEvent e) - { - viewport.setGlobalColourScheme( new NucleotideColourScheme() ); - changeColour(); - } + Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard(); + Hashtable orderedSeqs = new Hashtable(); + SequenceI[] seqs = new SequenceI[sg.getSize()]; - void changeColour() - { - if(abovePIDThreshold.isSelected()) - abovePIDThreshold_actionPerformed(null); - else if(conservationMenuItem.isSelected()) - conservationMenuItem_actionPerformed(null); + 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; - alignPanel.RefreshPanels(); - } + for (int i = 0; i < sg.getSize(); i++) + { + SequenceI seq = null; - void addConservationOrPIDColour() - { - int x=0, y=0, threshold = 70; + while (seq == null) + { + if (orderedSeqs.containsKey(index + "")) + { + seq = (SequenceI) orderedSeqs.get(index + ""); + index++; - if(sliderFrame!=null) - try - { - x = sliderFrame.getX(); - y = sliderFrame.getY(); - SliderPanel sp = (SliderPanel) sliderFrame.getContentPane(); - threshold = sp.getValue(); - sliderFrame.setClosed(true); - } - catch (Exception ex) - { ex.printStackTrace(); } + break; + } + else + { + index++; + } + } - ColourSchemeI oldCs = viewport.getGlobalColourScheme(); + //FIND START RES + //Returns residue following index if gap + startRes = seq.findPosition(sg.getStartRes()); - ConservationColourScheme ccs = null; - if (oldCs instanceof ConservationColourScheme) - ccs = (ConservationColourScheme) oldCs; + //FIND END RES + //Need to find the residue preceeding index if gap + endRes = 0; - if(conservationMenuItem.isSelected()) - { - Alignment al = (Alignment)viewport.alignment; - Conservation c = new Conservation("All", - ResidueProperties.propHash, 3, al.getSequences(), 0, - al.getWidth() ); + for (int j = 0; j < sg.getEndRes() + 1 && j < seq.getLength(); j++) + { + ch = seq.getCharAt(j); + if (!jalview.util.Comparison.isGap( (ch))) + { + endRes++; + } + } - c.calculate(); - c.verdict(false, 100); + if (endRes > 0) + { + endRes += seq.getStart() - 1; + } - if(ccs!=null) - ccs = new ConservationColourScheme(c, ccs.cs); - else - ccs = new ConservationColourScheme(c, oldCs); + 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()); - viewport.setGlobalColourScheme( ccs ); + } - } - else if(oldCs instanceof ConservationColourScheme) - { - oldCs = ccs.cs; - viewport.setGlobalColourScheme( oldCs ); - } + FastaFile ff = new FastaFile(); + ff.addJVSuffix( viewport.showJVSuffix ); + c.setContents(new StringSelection( ff.print(seqs)), this); + Desktop.jalviewClipboard = new Object[]{seqs, viewport.alignment.getDataset()}; + } + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void pasteNew_actionPerformed(ActionEvent e) + { + paste(true); + } - if( conservationMenuItem.isSelected() || abovePIDThreshold.isSelected() ) - { - sliderFrame = new JInternalFrame(); - sliderFrame.setMaximizable(false); - sliderFrame.setResizable(false); + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void pasteThis_actionPerformed(ActionEvent e) + { + addHistoryItem(new HistoryItem("Paste Sequences", viewport.alignment, + HistoryItem.PASTE)); + paste(false); + } - SliderPanel sp = null; - if (conservationMenuItem.isSelected()) - { - if(threshold > 50) - threshold = 30; - sp = new SliderPanel(alignPanel, threshold, true,ccs); - Desktop.addInternalFrame(sliderFrame, "Conservation Colour Increment (Global colour)", 400, 90); - threshold = 0; - } - else if (abovePIDThreshold.isSelected()) - { - sp = new SliderPanel(alignPanel, threshold, false, oldCs); - sp.cs = oldCs; - Desktop.addInternalFrame(sliderFrame, "Percentage Identity Threshold (Global colour)", 400, 90); - } + /** + * DOCUMENT ME! + * + * @param newAlignment DOCUMENT ME! + */ + void paste(boolean newAlignment) + { + try + { + Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard(); + Transferable contents = c.getContents(this); - sliderFrame.setContentPane(sp); - if(x!=0 && y!=0) - sliderFrame.setLocation(x,y); + if (contents == null) + { + return; + } - } - else - threshold = 0; + String str = (String) contents.getTransferData(DataFlavor.stringFlavor); + if(str.length()<1) + return; + String format = new IdentifyFile().Identify(str, "Paste"); + SequenceI[] sequences; - if (oldCs instanceof ResidueColourScheme) + if(Desktop.jalviewClipboard!=null) { - ResidueColourScheme rcs = (ResidueColourScheme) oldCs; - rcs.setThreshold(threshold); + // The clipboard was filled from within Jalview, we must use the sequences + // And dataset from the copied alignment + sequences = (SequenceI[])Desktop.jalviewClipboard[0]; } - else if (oldCs instanceof ScoreColourScheme) + else { - ScoreColourScheme scs = (ScoreColourScheme) oldCs; - scs.setThreshold(threshold); + sequences = new FormatAdapter().readFile(str, "Paste", format); } + if (newAlignment) + { - } + Alignment alignment = new Alignment(sequences); - protected void conservationMenuItem_actionPerformed(ActionEvent e) - { - if(abovePIDThreshold.isSelected()) - abovePIDThreshold.setSelected(false); + if(Desktop.jalviewClipboard!=null) + alignment.setDataset( (Alignment)Desktop.jalviewClipboard[1] ); + else + alignment.setDataset( null ); - viewport.setConservationSelected(conservationMenuItem.isSelected()); - addConservationOrPIDColour(); - } + AlignFrame af = new AlignFrame(alignment); + String newtitle = new String("Copied sequences"); - JInternalFrame sliderFrame; - public void abovePIDThreshold_actionPerformed(ActionEvent e) - { - if(conservationMenuItem.isSelected()) - conservationMenuItem.setSelected(false); + //>>>This is a fix for the moment, until a better solution is found!!<<< + FeatureRenderer fr = af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer(); + fr.featureColours = alignPanel.seqPanel.seqCanvas.getFeatureRenderer().featureColours; - addConservationOrPIDColour(); - } + if (title.startsWith("Copied sequences")) + { + newtitle = title; + } + else + { + newtitle = newtitle.concat("- from " + title); + } + 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()); + if(sequences[i].getDatasetSequence().getAnnotation()!=null) + { + for(int aa=0; aa0) - || 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; + if (seq.getSequence().length() < 1) + { + viewport.getAlignment().deleteSequence(seq); + } + else + { + viewport.getAlignment().getSequences().setElementAt(seq, index); + } } - 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) - { - JOptionPane.showInternalMessageDialog(this, "Too many sequences selected\nfor Principal Component Analysis!!", - "Out of memory", JOptionPane.WARNING_MESSAGE); - } + viewport.setSelectionGroup(null); + viewport.alignment.deleteGroup(sg); + viewport.firePropertyChange("alignment", null, + viewport.getAlignment().getSequences()); + + + if (viewport.getAlignment().getHeight() < 1) + { + try + { + this.setClosed(true); + } + catch (Exception ex) + { + } + } } - public void averageDistanceTreeMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + protected void deleteGroups_actionPerformed(ActionEvent e) { - NewTreePanel("AV", "PID", "Average distance tree using PID"); + viewport.alignment.deleteAllGroups(); + viewport.setSelectionGroup(null); + alignPanel.repaint(); } - public void neighbourTreeMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void selectAllSequenceMenuItem_actionPerformed(ActionEvent e) { - NewTreePanel("NJ", "PID", "Neighbour joining tree using PID"); - } + SequenceGroup sg = new SequenceGroup(); + for (int i = 0; i < viewport.getAlignment().getSequences().size(); + i++) + { + sg.addSequence(viewport.getAlignment().getSequenceAt(i), false); + } - protected void njTreeBlosumMenuItem_actionPerformed(ActionEvent e) - { - NewTreePanel("NJ", "BL", "Neighbour joining tree using BLOSUM62"); + sg.setEndRes(viewport.alignment.getWidth() - 1); + viewport.setSelectionGroup(sg); + PaintRefresher.Refresh(null, viewport.alignment); } - protected void avTreeBlosumMenuItem_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void deselectAllSequenceMenuItem_actionPerformed(ActionEvent e) { - NewTreePanel("AV", "BL", "Average distance tree using BLOSUM62PID"); + if(viewport.cursorMode) + { + alignPanel.seqPanel.keyboardNo1 = null; + alignPanel.seqPanel.keyboardNo2 = null; + } + viewport.setSelectionGroup(null); + viewport.getColumnSelection().clear(); + viewport.setSelectionGroup(null); + alignPanel.seqPanel.seqCanvas.highlightSearchResults(null); + alignPanel.idPanel.idCanvas.searchResults = null; + PaintRefresher.Refresh(null, viewport.alignment); } - void NewTreePanel(String type, String pwType, String title) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void invertSequenceMenuItem_actionPerformed(ActionEvent e) { - JInternalFrame frame = new javax.swing.JInternalFrame(); - TreePanel tp=null; - if (viewport.getSelection() != null && viewport.getSelection().size() > 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()); - } - - frame.setContentPane(tp); - Desktop.addInternalFrame(frame, title, 600, 500); - } - + SequenceGroup sg = viewport.getSelectionGroup(); + if (sg == null) + { + selectAllSequenceMenuItem_actionPerformed(null); + return; + } - public void clustalAlignMenuItem_actionPerformed(ActionEvent e) - { - JInternalFrame frame = new JInternalFrame(); - ClustalThread ct = new ClustalThread(frame); - Thread t = new Thread(ct); - t.start(); - frame.setContentPane(ct); - Desktop.addInternalFrame(frame, title, 300, 80); + for (int i = 0; i < viewport.getAlignment().getSequences().size(); + i++) + { + sg.addOrRemove(viewport.getAlignment().getSequenceAt(i), false); + } + PaintRefresher.Refresh(null, viewport.alignment); } - class ClustalThread extends JPanel implements Runnable + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void remove2LeftMenuItem_actionPerformed(ActionEvent e) { - Image [] image; - int imageIndex = 0; - boolean webServiceRunning = false; - JInternalFrame frame; - public ClustalThread(JInternalFrame frame) + ColumnSelection colSel = viewport.getColumnSelection(); + + if (colSel.size() > 0) { - this.frame = frame; - image = new Image[9]; - for(int i=0; i<9; i++) + 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++) { - java.net.URL url = getClass().getResource("/dna" + (i+1) + ".gif"); - if (url != null) + SequenceGroup sg = (SequenceGroup) groups.get(i); + + if (!sg.adjustForRemoveLeft(min)) { - image[i] = java.awt.Toolkit.getDefaultToolkit().createImage(url); - MediaTracker mt = new MediaTracker(this); - mt.addImage(image[i], i); - try{mt.waitForID(i);} - catch(Exception ex){} + viewport.alignment.deleteGroup(sg); } } - DNATwirler twirler = new DNATwirler(); - twirler.start(); - webServiceRunning = true; + + viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); } + } + + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void remove2RightMenuItem_actionPerformed(ActionEvent e) + { + ColumnSelection colSel = viewport.getColumnSelection(); - class DNATwirler extends Thread + if (colSel.size() > 0) { - public void run() + addHistoryItem(new HistoryItem("Remove Right", viewport.alignment, + HistoryItem.HIDE)); + + int max = colSel.getMax(); + viewport.getAlignment().trimRight(max); + + if (viewport.getSelectionGroup() != null) { - while(webServiceRunning) - { - try{ - Thread.sleep(100); - imageIndex++; - imageIndex %=9; - repaint(); - } - catch(Exception ex){} - } + viewport.getSelectionGroup().adjustForRemoveRight(max); } - } - // JBPNote - // Should check to see if the server settings are valid - // Need visual-delay indication here. - public void run() - { - jalview.ws.Jemboss jemboss = new jalview.ws.Jemboss(); - Vector sv = viewport.getAlignment().getSequences(); - SequenceI[] seqs = new SequenceI[sv.size()]; - - int i = 0; - do - { - seqs[i] = (SequenceI) sv.elementAt(i); - } - while (++i < sv.size()); - - SequenceI[] alignment = jemboss.clustalW(seqs); // gaps removed within method - - if (alignment != null) - { - AlignFrame af = new AlignFrame(new Alignment(alignment)); - Desktop.addInternalFrame(af, getTitle().concat(" - ClustalW Alignment"), - 700, 500); // JBPNote - is there a new window geom. property ? - } - else - JOptionPane.showMessageDialog(Desktop.desktop, "Problem obtaining clustal alignment", "Web service error", - JOptionPane.WARNING_MESSAGE); - - webServiceRunning = false; - try{ - frame.setClosed(true); - }catch(Exception ex){} - } + Vector groups = viewport.alignment.getGroups(); - public void paintComponent(Graphics g) - { - g.setColor(Color.white); - g.fillRect(0,0,getWidth(), getHeight()); - if(image!=null) - { - g.drawImage(image[imageIndex],10,10,this); - } - g.setFont(new Font("Arial", Font.BOLD, 12)); - g.setColor(Color.black); - g.drawString("Clustal Alignment Web Service running", 30,30); - } + 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 pdbTest_actionPerformed(ActionEvent e) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void removeGappedColumnMenuItem_actionPerformed(ActionEvent e) { - String reply = - JOptionPane.showInternalInputDialog(this, "Enter pdb code", - "PDB test viewer", JOptionPane.QUESTION_MESSAGE); + addHistoryItem(new HistoryItem("Remove Gapped Columns", + viewport.alignment, HistoryItem.HIDE)); - String url = "http://www.ebi.ac.uk/cgi-bin/pdbfetch?id=1a4u"; - if (reply.length()>1) - url = "http://www.ebi.ac.uk/cgi-bin/pdbfetch?id="+reply; + //This is to maintain viewport position on first residue + //of first sequence + SequenceI seq = viewport.alignment.getSequenceAt(0); + int startRes = seq.findPosition(viewport.startRes); - try - { - PDBfile pdb = new PDBfile(url, - "URL"); - Sequence seq = (Sequence)viewport.getAlignment().getSequenceAt(0); - seq.setPDBfile(pdb); - ( (PDBChain) pdb.chains.elementAt(seq.maxchain)).isVisible = true; - ( (PDBChain) pdb.chains.elementAt(seq.maxchain)).sequence = seq; - // ( (PDBChain) pdb.chains.elementAt(seq.maxchain)).colourBySequence(); - - rotCanvas rc = new rotCanvas(pdb); - JInternalFrame frame = new JInternalFrame(); - frame.setContentPane(rc); - Desktop.addInternalFrame(frame, url, 400,400); - } - catch (Exception ex) - { - ex.printStackTrace(); - } + viewport.getAlignment().removeGaps(); - } + viewport.setStartRes(seq.findIndex(startRes)-1); + viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); + } - public void doKeyPressed(KeyEvent evt) + /** + * DOCUMENT ME! + * + * @param e DOCUMENT ME! + */ + public void removeAllGapsMenuItem_actionPerformed(ActionEvent e) { + addHistoryItem(new HistoryItem("Remove Gaps", viewport.alignment, + HistoryItem.HIDE)); - System.out.println(evt.getKeyChar()); - if(evt.isControlDown() && evt.getKeyChar()=='f') - findMenuItem_actionPerformed(null); + //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) + { + seqs = viewport.getSelectionGroup().sequences; + start = viewport.getSelectionGroup().getStartRes(); + end = viewport.getSelectionGroup().getEndRes()+1; + } + else + { + seqs = viewport.alignment.getSequences(); + } + + 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.padGaps) + viewport.getAlignment().padGaps(); + + if(viewport.vconsensus!=null && viewport.autoCalculateConsensus) + { + viewport.updateConsensus(); + viewport.updateConservation(); + } + resetAllColourSchemes(); + if(alignPanel.overviewPanel!=null) + alignPanel.overviewPanel.updateOverviewImage(); + + viewport.alignment.adjustSequenceAnnotations(); + + 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 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); + } + + + public void autoCalculate_actionPerformed(ActionEvent e) + { + viewport.autoCalculateConsensus = autoCalculate.isSelected(); + } + + + /** + * 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) + { + 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); + + 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 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