From 998f8c437ca6ea5f76e261ef544e90ebee632f78 Mon Sep 17 00:00:00 2001 From: jprocter Date: Mon, 21 Aug 2006 13:41:41 +0000 Subject: [PATCH] Refactored Jnet and added hidden columns support. fixed undo-redo bug, hidden columns are moved with gapped columns and delete columns and undo-redo. --- src/jalview/gui/AlignFrame.java | 5725 +++++++++++++++++++-------------------- src/jalview/ws/JPredClient.java | 1458 +++++----- 2 files changed, 3615 insertions(+), 3568 deletions(-) diff --git a/src/jalview/gui/AlignFrame.java b/src/jalview/gui/AlignFrame.java index 28961e5..2d39108 100755 --- a/src/jalview/gui/AlignFrame.java +++ b/src/jalview/gui/AlignFrame.java @@ -1,2880 +1,2845 @@ -/* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle - * - * 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.*; - -import java.awt.*; -import java.awt.datatransfer.*; -import java.awt.event.*; -import java.awt.print.*; -import javax.swing.*; - -import jalview.analysis.*; -import jalview.datamodel.*; -import jalview.io.*; -import jalview.jbgui.*; -import jalview.schemes.*; -import jalview.ws.*; -import java.awt.dnd.*; -import org.biojava.dasobert.eventmodel.*; - -/** - * DOCUMENT ME! - * - * @author $author$ - * @version $Revision$ - */ -public class AlignFrame - extends GAlignFrame implements DropTargetListener, FeatureListener -{ - /** DOCUMENT ME!! */ - public static final int NEW_WINDOW_WIDTH = 700; - - /** DOCUMENT ME!! */ - public static final int NEW_WINDOW_HEIGHT = 500; - AlignmentPanel alignPanel; - AlignViewport viewport; - - /** DOCUMENT ME!! */ - public String currentFileFormat = null; - Stack historyList = new Stack(); - Stack redoList = new Stack(); - private int treeCount = 0; - - /** - * new alignment window with hidden columns - * @param al AlignmentI - * @param hiddenColumns ColumnSelection or null - */ - public AlignFrame(AlignmentI al, ColumnSelection hiddenColumns) { - - viewport = new AlignViewport(al, hiddenColumns); - - 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); - - 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); - - - - // tabbedPane.add(al.isNucleotide() ? "DNA":"Protein", alignPanel); - - ///Dataset tab - ///////////////////////// - if(al.getDataset()==null) - { - 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); - ///////////////////////// - - - viewport.addPropertyChangeListener(new PropertyChangeListener() - { - public void propertyChange(PropertyChangeEvent evt) - { - if (evt.getPropertyName().equals("alignment")) - { - alignmentChanged(); - } - } - }); - - - if (Desktop.desktop != null) - { - addServiceListeners(); - setGUINucleotide(al.isNucleotide()); - } - - - if (jalview.bin.Cache.getDefault("WRAP_ALIGNMENT", false)) - { - wrapMenuItem.setSelected(true); - wrapMenuItem_actionPerformed(null); - } - - } - - - /** - * Creates a new AlignFrame object. - * - * @param al DOCUMENT ME! - */ - public AlignFrame(AlignmentI al) - { - this(al, null); - } - - public AlignViewport getViewport() - { - return viewport; - } - - /* Set up intrinsic listeners for dynamically generated GUI bits. */ - private void addServiceListeners() - { - final java.beans.PropertyChangeListener thisListener; - // Do this once to get current state - BuildWebServiceMenu(); - Desktop.discoverer.addPropertyChangeListener( - thisListener = new java.beans.PropertyChangeListener() - { - public void propertyChange(PropertyChangeEvent evt) - { - // System.out.println("Discoverer property change."); - if (evt.getPropertyName().equals("services")) - { - // System.out.println("Rebuilding web service menu"); - BuildWebServiceMenu(); - } - } - }); - addInternalFrameListener(new javax.swing.event. - InternalFrameAdapter() - { - public void internalFrameClosed( - javax.swing.event.InternalFrameEvent evt) - { - // System.out.println("deregistering discoverer listener"); - Desktop.discoverer.removePropertyChangeListener(thisListener); - closeMenuItem_actionPerformed(null); - } - ; - }); - } - - public void setGUINucleotide(boolean nucleotide) - { - showTranslation.setVisible( nucleotide ); - //sequenceFeatures.setVisible(!nucleotide ); - //featureSettings.setVisible( !nucleotide ); - conservationMenuItem.setVisible( !nucleotide ); - modifyConservation.setVisible( !nucleotide ); - - //Remember AlignFrame always starts as protein - if(!nucleotide) - { - calculateMenu.remove(calculateMenu.getItemCount()-2); - } - } - - public void comeBackLater(FeatureEvent evt) - {} - - public void newFeatures(FeatureEvent evt) - { - if (evt.getFeatures().length > 0) - { - alignPanel.seqPanel.seqCanvas.fr.featuresAdded(); - alignPanel.repaint(); - } - } - - Hashtable progressBars; - public void setProgressBar(String message, long id) - { - if(progressBars == null) - progressBars = new Hashtable(); - - JPanel progressPanel; - GridLayout layout = (GridLayout) statusPanel.getLayout(); - if(progressBars.get( new Long(id) )!=null) - { - progressPanel = (JPanel)progressBars.get( new Long(id) ); - statusPanel.remove(progressPanel); - progressBars.remove( progressPanel ); - progressPanel = null; - if(message!=null) - statusBar.setText(message); - - layout.setRows(layout.getRows() - 1); - } - else - { - progressPanel = new JPanel(new BorderLayout(10, 5)); - - JProgressBar progressBar = new JProgressBar(); - progressBar.setIndeterminate(true); - - progressPanel.add(new JLabel(message), BorderLayout.WEST); - progressPanel.add(progressBar, BorderLayout.CENTER); - - layout.setRows(layout.getRows() + 1); - statusPanel.add(progressPanel); - - progressBars.put(new Long(id), progressPanel); - } - - validate(); - } - - - /* - Added so Castor Mapping file can obtain Jalview Version - */ - public String getVersion() - { - return jalview.bin.Cache.getProperty("VERSION"); - } - - public FeatureRenderer getFeatureRenderer() - { - return alignPanel.seqPanel.seqCanvas.getFeatureRenderer(); - } - - - public void fetchSequence_actionPerformed(ActionEvent e) - { - new SequenceFetcher(this); - } - - public void addFromFile_actionPerformed(ActionEvent e) - { - Desktop.instance.inputLocalFileMenuItem_actionPerformed(viewport); - } - - public void addFromText_actionPerformed(ActionEvent e) - { - Desktop.instance.inputTextboxMenuItem_actionPerformed(viewport); - } - - public void addFromURL_actionPerformed(ActionEvent e) - { - Desktop.instance.inputURLMenuItem_actionPerformed(viewport); - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - public void saveAlignmentMenu_actionPerformed(ActionEvent e) - { - JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache. - getProperty( "LAST_DIRECTORY"), - new String[] - { "fa, fasta, fastq", "aln", "pfam", "msf", "pir", "blc","jar" }, - new String[] - { "Fasta", "Clustal", "PFAM", "MSF", "PIR", "BLC", "Jalview" }, - currentFileFormat, - false); - - - chooser.setFileView(new JalviewFileView()); - chooser.setDialogTitle("Save Alignment to file"); - chooser.setToolTipText("Save"); - - int value = chooser.showSaveDialog(this); - - if (value == JalviewFileChooser.APPROVE_OPTION) - { - currentFileFormat = chooser.getSelectedFormat(); - - if (currentFileFormat == null) - { - JOptionPane.showInternalMessageDialog(Desktop.desktop, - "You must select a file format before saving!", - "File format not specified", - JOptionPane.WARNING_MESSAGE); - value = chooser.showSaveDialog(this); - return; - } - - jalview.bin.Cache.setProperty("DEFAULT_FILE_FORMAT", - currentFileFormat); - - String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice); - - saveAlignment(choice, currentFileFormat); - } - } - - public boolean saveAlignment(String file, String format) - { - if (format.equalsIgnoreCase("Jalview")) - { - String shortName = title; - - if (shortName.indexOf(java.io.File.separatorChar) > -1) - { - shortName = shortName.substring(shortName.lastIndexOf( - java.io.File.separatorChar) + 1); - } - - new Jalview2XML().SaveAlignment(this, file, shortName); - - // USE Jalview2XML to save this file - return true; - } - else - { - - String[] omitHidden = null; - - if (viewport.hasHiddenColumns) - { - int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop, - "The Alignment contains hidden columns." - + "\nDo you want to save only the visible alignment?", - "Save / Omit Hidden Columns", - JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); - - if (reply == JOptionPane.YES_OPTION) - omitHidden = viewport.getViewAsString(false); - } - - String output = new FormatAdapter().formatSequences( - format, - viewport.alignment.getSequencesArray(), - omitHidden); - - if (output == null) - { - return false; - } - - try - { - java.io.PrintWriter out = new java.io.PrintWriter( - new java.io.FileWriter(file)); - - out.print(output); - out.close(); - this.setTitle(file); - return true; - } - catch (Exception ex) - { - ex.printStackTrace(); - } - } - return false; - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - protected void outputText_actionPerformed(ActionEvent e) - { - String [] omitHidden = null; - - if(viewport.hasHiddenColumns) - { - int reply = JOptionPane.showInternalConfirmDialog(Desktop.desktop, - "The Alignment contains hidden columns." - +"\nDo you want to output only the visible alignment?", - "Save / Omit Hidden Columns", - JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE); - - if(reply==JOptionPane.YES_OPTION) - { - omitHidden = viewport.getViewAsString(false); - } - } - - CutAndPasteTransfer cap = new CutAndPasteTransfer(); - Desktop.addInternalFrame(cap, - "Alignment output - " + e.getActionCommand(), 600, - 500); - - - cap.setText(new FormatAdapter().formatSequences( - e.getActionCommand(), - viewport.alignment.getSequencesArray(), - omitHidden)); - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - protected void htmlMenuItem_actionPerformed(ActionEvent e) - { - new HTMLOutput(viewport, - alignPanel.seqPanel.seqCanvas.getSequenceRenderer(), - alignPanel.seqPanel.seqCanvas.getFeatureRenderer()); - } - - public void createImageMap(File file, String image) - { - alignPanel.makePNGImageMap(file, image); - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - public void createPNG(File f) - { - 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 exportFeatures_actionPerformed(ActionEvent e) - { - new AnnotationExporter().exportFeatures(alignPanel); - } - - - public void exportAnnotations_actionPerformed(ActionEvent e) - { - new AnnotationExporter().exportAnnotations( - alignPanel, - viewport.alignment.getAlignmentAnnotation() - ); - } - - - public void associatedData_actionPerformed(ActionEvent e) - { - // 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); - - if (value == JalviewFileChooser.APPROVE_OPTION) - { - String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.setProperty("LAST_DIRECTORY", choice); - loadJalviewDataFile(choice); - } - - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - public void closeMenuItem_actionPerformed(ActionEvent e) - { - try - { - PaintRefresher.components.remove(viewport.alignment); - this.setClosed(true); - } - catch (Exception ex) - { - } - } - - /** - * DOCUMENT ME! - */ - void updateEditMenuBar() - { - if (historyList.size() > 0) - { - undoMenuItem.setEnabled(true); - - HistoryItem hi = (HistoryItem) historyList.peek(); - undoMenuItem.setText("Undo " + hi.getDescription()); - } - else - { - undoMenuItem.setEnabled(false); - undoMenuItem.setText("Undo"); - } - - if (redoList.size() > 0) - { - redoMenuItem.setEnabled(true); - - HistoryItem hi = (HistoryItem) redoList.peek(); - redoMenuItem.setText("Redo " + hi.getDescription()); - } - else - { - redoMenuItem.setEnabled(false); - redoMenuItem.setText("Redo"); - } - } - - /** - * DOCUMENT ME! - * - * @param hi DOCUMENT ME! - */ - public void addHistoryItem(HistoryItem hi) - { - historyList.push(hi); - updateEditMenuBar(); - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - protected void undoMenuItem_actionPerformed(ActionEvent e) - { - HistoryItem hi = (HistoryItem) historyList.pop(); - redoList.push(new HistoryItem(hi.getDescription(), viewport.alignment, - HistoryItem.HIDE)); - restoreHistoryItem(hi); - viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - protected void redoMenuItem_actionPerformed(ActionEvent e) - { - HistoryItem hi = (HistoryItem) redoList.pop(); - restoreHistoryItem(hi); - updateEditMenuBar(); - viewport.firePropertyChange("alignment", null, viewport.getAlignment().getSequences()); - } - - // used by undo and redo - void restoreHistoryItem(HistoryItem hi) - { - - hi.restore(); - - updateEditMenuBar(); - - viewport.firePropertyChange("alignment", null, - viewport.getAlignment().getSequences()); - } - - /** - * DOCUMENT ME! - * - * @param up DOCUMENT ME! - */ - public void moveSelectedSequences(boolean up) - { - SequenceGroup sg = viewport.getSelectionGroup(); - - if (sg == null) - { - return; - } - - if (up) - { - for (int i = 1; i < viewport.alignment.getHeight(); i++) - { - SequenceI seq = viewport.alignment.getSequenceAt(i); - - if (!sg.getSequences(false).contains(seq)) - { - continue; - } - - SequenceI temp = viewport.alignment.getSequenceAt(i - 1); - - if (sg.getSequences(false).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--) - { - SequenceI seq = viewport.alignment.getSequenceAt(i); - - if (!sg.getSequences(false).contains(seq)) - { - continue; - } - - SequenceI temp = viewport.alignment.getSequenceAt(i + 1); - - if (sg.getSequences(false).contains(temp)) - { - continue; - } - - viewport.alignment.getSequences().setElementAt(temp, i); - viewport.alignment.getSequences().setElementAt(seq, i + 1); - } - } - - alignPanel.repaint(); - } - - - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - protected void copy_actionPerformed(ActionEvent e) - { - if (viewport.getSelectionGroup() == null) - { - return; - } - - SequenceI [] seqs = viewport.getSelectionAsNewSequence(); - String[] omitHidden = null; - - if (viewport.hasHiddenColumns) - { - omitHidden = viewport.getViewAsString(true); - } - - String output = new FormatAdapter().formatSequences( - "Fasta", - seqs, - omitHidden); - - - Toolkit.getDefaultToolkit().getSystemClipboard() - .setContents(new StringSelection(output), Desktop.instance); - - Vector hiddenColumns = null; - if(viewport.hasHiddenColumns) - { - hiddenColumns =new Vector(); - int hiddenOffset = viewport.getSelectionGroup().getStartRes(); - for(int i=0; i>>This is a fix for the moment, until a better solution is found!!<<< - af.alignPanel.seqPanel.seqCanvas.getFeatureRenderer().transferSettings( - alignPanel.seqPanel.seqCanvas.getFeatureRenderer()); - - - if (title.startsWith("Copied sequences")) - { - newtitle = title; - } - else - { - newtitle = newtitle.concat("- from " + title); - } - - Desktop.addInternalFrame(af, newtitle, NEW_WINDOW_WIDTH, - NEW_WINDOW_HEIGHT); - - } - - - } - catch (Exception ex) - { - ex.printStackTrace(); - System.out.println("Exception whilst pasting: "+ex); - // could be anything being pasted in here - } - - - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - protected void cut_actionPerformed(ActionEvent e) - { - copy_actionPerformed(null); - delete_actionPerformed(null); - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - protected void delete_actionPerformed(ActionEvent e) - { - - if (viewport.getSelectionGroup() == null) - { - return; - } - - - SequenceGroup sg = viewport.getSelectionGroup(); - - - - //Jalview no longer allows deletion of residues. - //Check here whether any residues are in selection area - /* if( sg.getEndRes()-sg.getStartRes() < viewport.alignment.getWidth()-1) - { - for (int i = 0; i < sg.sequences.size(); i++) - { - SequenceI seq = sg.getSequenceAt(i); - int j = sg.getStartRes(); - do - { - if (!jalview.util.Comparison.isGap(seq.getCharAt(j))) - { - JOptionPane.showInternalMessageDialog( - Desktop.desktop, "Cannot delete residues from alignment!\n" - + "Try hiding columns instead.", - "Deletion of residues not permitted", - JOptionPane.WARNING_MESSAGE); - - return; - } - j++; - }while(j<=sg.getEndRes()); - } - }*/ - - - addHistoryItem(new HistoryItem("Delete Sequences", viewport.alignment, - HistoryItem.HIDE)); - - - for (int i = 0; i < sg.getSize(false); 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 (sg.getSize(false) == viewport.alignment.getHeight()) - { - viewport.getColumnSelection().removeElements(sg.getStartRes(), - sg.getEndRes() + 1); - } - - if (seq.getSequence().length() < 1) - { - viewport.getAlignment().deleteSequence(seq); - } - else - { - viewport.getAlignment().getSequences().setElementAt(seq, index); - } - } - - 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) - { - } - } - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - protected void deleteGroups_actionPerformed(ActionEvent e) - { - viewport.alignment.deleteAllGroups(); - viewport.setSelectionGroup(null); - alignPanel.repaint(); - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - public void selectAllSequenceMenuItem_actionPerformed(ActionEvent e) - { - 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); - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - public void deselectAllSequenceMenuItem_actionPerformed(ActionEvent e) - { - 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; - alignPanel.repaint(); - PaintRefresher.Refresh(null, viewport.alignment); - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - public void invertSequenceMenuItem_actionPerformed(ActionEvent e) - { - SequenceGroup sg = viewport.getSelectionGroup(); - - if (sg == null) - { - selectAllSequenceMenuItem_actionPerformed(null); - - return; - } - - for (int i = 0; i < viewport.getAlignment().getSequences().size(); - i++) - { - sg.addOrRemove(viewport.getAlignment().getSequenceAt(i), false); - } - - PaintRefresher.Refresh(null, viewport.alignment); - } - - public void invertColSel_actionPerformed(ActionEvent e) - { - viewport.invertColumnSelection(); - alignPanel.repaint(); - } - - - /** - * 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()); - } - } - - /** - * DOCUMENT ME! - * - * @param e DOCUMENT ME! - */ - public void remove2RightMenuItem_actionPerformed(ActionEvent e) - { - ColumnSelection colSel = viewport.getColumnSelection(); - - if (colSel.size() > 0) - { - 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()); - } - } - - /** - * 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) - { - 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().getSequences(true) != null - && viewport.getSelectionGroup().getSize(true) > 0) - { - seqs = viewport.getSelectionGroup().getSequences(true); - 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(); - - 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 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(); - if(viewport.autoCalculateConsensus) - { - alignmentChanged(); - } - } - - - /** - * 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(false) > 3)) - { - int s = 0; - SequenceGroup sg = viewport.getSelectionGroup(); - - /* Decide if the selection is a column region */ - while (s < sg.getSize(false)) - { - if ( ( (SequenceI) sg.getSequences(false).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, type, pwType); - } - 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; - } - - if(viewport.alignment.getHeight()<2) - return; - - tp = new TreePanel(viewport, type, pwType); - } - - 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 jalview.datamodel.AlignmentView gatherSequencesForAlignment() - { - // Now, check we have enough sequences - AlignmentView msa = null; - - if ( (viewport.getSelectionGroup() != null) && - (viewport.getSelectionGroup().getSize(false) > 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(false)]; - - for (int i = 0; i < sz; i++) - { - msa[i] = (SequenceI) seqs.getSequenceAt(i); - } */ - msa = viewport.getAlignmentView(true); - } - 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); - } - }*/ - msa = viewport.getAlignmentView(false); - } - 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(false) > 0)) - { - // JBPNote UGLY! To prettify, make SequenceGroup and Alignment conform to some common interface! - SequenceGroup seqs = viewport.getSelectionGroup(); - - if ( (seqs.getSize(false) == 1) || !viewport.alignment.isAligned()) - { - seq = (SequenceI) seqs.getSequenceAt(0); - } - else - { - int sz; - msa = new SequenceI[sz = seqs.getSize(false)]; - - 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, - "FromFile", - title, - nf); - - 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"); - final AlignFrame af = this; - 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) - { - AlignmentView msa = gatherSequencesForAlignment(); - new jalview.ws.MsaWSClient(sh, title, msa, - false, true, viewport.getAlignment().getDataset(), af); - - } - - }); - 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) - { - AlignmentView msa = gatherSequencesForAlignment(); - new jalview.ws.MsaWSClient(sh, title, msa, - true, true, viewport.getAlignment().getDataset(), af); - - } - - }); - 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], null); - } - else - { - if (msa.length > 1) - { - // Single Sequence prediction - jalview.ws.JPredClient ct = new jalview.ws.JPredClient(sh, - title, msa, null); - } - } - } - }); - 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) -{ - SequenceI [] selection = viewport.getSelectionAsNewSequence(); - String [] seqstring = viewport.getViewAsString(true); - - int s, sSize = selection.length; - SequenceI [] newSeq = new SequenceI[sSize]; - - int res, resSize; - StringBuffer protein; - String seq; - for(s=0; s