/******************** * 2004 Jalview Reengineered * Barton Group * Dundee University * * AM Waterhouse *******************/ package jalview.gui; import jalview.jbgui.GAlignFrame; import jalview.schemes.*; import jalview.datamodel.*; import jalview.analysis.*; import jalview.io.*; import jalview.ws.*; import java.awt.*; 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 { final AlignmentPanel alignPanel; final AlignViewport viewport; public AlignFrame(AlignmentI al) { super(); AlignmentUtil.addUniprotFeatures(al); 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()+""); } 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){} } } 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) { HTMLOutput htmlOutput = new HTMLOutput(viewport); htmlOutput = null; } public void printMenuItem_actionPerformed(ActionEvent e) { //Putting in a thread avoids Swing painting problems PrintThread thread = new PrintThread(); thread.start(); } 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(); } } } } 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 { undoMenuItem.setEnabled(false); undoMenuItem.setText("Undo"); } if(redoList.size()>0) { redoMenuItem.setEnabled(true); Object [] history = (Object[])redoList.get(0); redoMenuItem.setText("Redo "+history[0]); } else { redoMenuItem.setEnabled(false); redoMenuItem.setText("Redo"); } } public void addHistoryItem(String type) { // 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 0) { int min = colSel.getMin(); viewport.getAlignment().trimLeft(min); colSel.compensateForEdit(0,min); alignPanel.RefreshPanels(); } } public void remove2RightMenuItem_actionPerformed(ActionEvent e) { addHistoryItem("delete columns"); ColumnSelection colSel = viewport.getColumnSelection(); if (colSel.size() > 0) { int max = colSel.getMax(); if(max>1) viewport.getAlignment().trimRight(max); alignPanel.RefreshPanels(); } } public void removeGappedColumnMenuItem_actionPerformed(ActionEvent e) { addHistoryItem("delete gapped columns"); viewport.getAlignment().removeGaps(); alignPanel.RefreshPanels(); } public void removeAllGapsMenuItem_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(); } public void setGapCharMenuItem_actionPerformed(ActionEvent e) { char thisChar = '-'; char nextChar = '.'; if(viewport.getGapCharacter()=='-') { thisChar = '.'; nextChar = '-'; } setGapCharMenuItem.setText("Set gap character to \""+nextChar+"\""); viewport.setGapCharacter(thisChar); alignPanel.RefreshPanels(); } public void findMenuItem_actionPerformed(ActionEvent e) { 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); } public void fontNameMenuItem_actionPerformed(ActionEvent e) { String fonts[] = java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); String selection = JOptionPane.showInternalInputDialog(this, "Select font", "Font selection", JOptionPane.QUESTION_MESSAGE, null,fonts ,fontNameMenuItem.getText()).toString(); if(selection!=null) { fontNameMenuItem.setText(selection); SetFont(); } } public void fontSizeMenuItem_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(); } } public void fontStyleMenuItem_actionPerformed(ActionEvent e) { 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) { fontStyleMenuItem.setText(selection); SetFont(); } } protected void colourTextMenuItem_actionPerformed(ActionEvent e) { viewport.setColourText( colourTextMenuItem.isSelected() ); alignPanel.RefreshPanels(); } void SetFont() { 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(); } protected void wrapMenuItem_actionPerformed(ActionEvent e) { viewport.setWrapAlignment( wrapMenuItem.isSelected() ); alignPanel.setWrapAlignment( wrapMenuItem.isSelected() ); } public void viewBoxesMenuItem_actionPerformed(ActionEvent e) { viewport.setShowBoxes( viewBoxesMenuItem.isSelected() ); alignPanel.RefreshPanels(); } public void viewTextMenuItem_actionPerformed(ActionEvent e) { viewport.setShowText( viewTextMenuItem.isSelected() ); alignPanel.RefreshPanels(); } protected void renderGapsMenuItem_actionPerformed(ActionEvent e) { viewport.setRenderGaps(renderGapsMenuItem.isSelected()); alignPanel.RefreshPanels(); } public void sequenceFeatures_actionPerformed(ActionEvent evt) { viewport.showSequenceFeatures(sequenceFeatures.isSelected()); alignPanel.RefreshPanels(); } public void consensusGraphMenuItem_actionPerformed(ActionEvent e) { alignPanel.setGraphPanelVisible( consensusGraphMenuItem.isSelected() ); } public void overviewMenuItem_actionPerformed(ActionEvent e) { if (alignPanel.overviewPanel != 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) { alignPanel.setOverviewPanel(null); }; }); alignPanel.setOverviewPanel( overview ); }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); } } protected void noColourmenuItem_actionPerformed(ActionEvent e) { viewport.setGlobalColourScheme( null ); changeColour(); } public void clustalColour_actionPerformed(ActionEvent e) { viewport.setGlobalColourScheme( new ClustalxColourScheme(viewport.alignment.getSequences(), viewport.alignment.getWidth())); 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(); } public void hydrophobicityColour_actionPerformed(ActionEvent e) { viewport.setGlobalColourScheme( new HydrophobicColourScheme() ); changeColour(); } public void helixColour_actionPerformed(ActionEvent e) { viewport.setGlobalColourScheme( new HelixColourScheme() ); changeColour(); } public void strandColour_actionPerformed(ActionEvent e) { viewport.setGlobalColourScheme(new StrandColourScheme() ); changeColour(); } public void turnColour_actionPerformed(ActionEvent e) { viewport.setGlobalColourScheme(new TurnColourScheme() ); changeColour(); } public void buriedColour_actionPerformed(ActionEvent e) { viewport.setGlobalColourScheme( new BuriedColourScheme() ); changeColour(); } public void nucleotideColour_actionPerformed(ActionEvent e) { viewport.setGlobalColourScheme( new NucleotideColourScheme() ); changeColour(); } void changeColour() { if(abovePIDThreshold.isSelected()) abovePIDThreshold_actionPerformed(null); else if(conservationMenuItem.isSelected()) conservationMenuItem_actionPerformed(null); alignPanel.RefreshPanels(); } protected void conservationMenuItem_actionPerformed(ActionEvent e) { if(abovePIDThreshold.isSelected()) { abovePIDThreshold.setSelected(false); Desktop.hidePIDSlider(); } viewport.setConservationSelected(conservationMenuItem.isSelected()); ColourSchemeI oldCs = viewport.getGlobalColourScheme(); ConservationColourScheme ccs = null; if (oldCs instanceof ConservationColourScheme) ccs = (ConservationColourScheme) oldCs; if (conservationMenuItem.isSelected()) { Alignment al = (Alignment) viewport.alignment; Conservation c = new Conservation("All", ResidueProperties.propHash, 3, al.getSequences(), 0, al.getWidth()); c.calculate(); c.verdict(false, 100); if (ccs != null) ccs = new ConservationColourScheme(c, ccs.cs); else ccs = new ConservationColourScheme(c, oldCs); int threshold = Desktop.setConservationSliderSource(alignPanel, ccs, "Background"); ccs.inc = threshold; if (ccs.cs instanceof ResidueColourScheme) ((ResidueColourScheme) ccs.cs).setThreshold(0); else if (ccs.cs instanceof ScoreColourScheme) ((ScoreColourScheme)ccs.cs).setThreshold(0); viewport.setGlobalColourScheme(ccs); } else if (oldCs instanceof ConservationColourScheme) { /// ie, if we remove ConservationColourScheme from backgroundColour oldCs = ccs.cs; viewport.setGlobalColourScheme(oldCs); Desktop.hideConservationSlider(); } if(e!=null) alignPanel.RefreshPanels(); } public void abovePIDThreshold_actionPerformed(ActionEvent e) { if(conservationMenuItem.isSelected()) conservationMenuItem.setSelected(false); ColourSchemeI oldCs = viewport.getGlobalColourScheme(); int threshold = 0; if (oldCs instanceof ConservationColourScheme) { ConservationColourScheme ccs = (ConservationColourScheme) oldCs; oldCs = ccs.cs; } if(abovePIDThreshold.isSelected()) { threshold = Desktop.setPIDSliderSource(alignPanel, oldCs, "Background"); Desktop.hideConservationSlider(); } else Desktop.hidePIDSlider(); if (oldCs instanceof ResidueColourScheme) ((ResidueColourScheme) oldCs).setThreshold(threshold); else if (oldCs instanceof ScoreColourScheme) ((ScoreColourScheme)oldCs).setThreshold(threshold); viewport.setGlobalColourScheme(oldCs); if(e!=null) alignPanel.RefreshPanels(); } public void userDefinedColour_actionPerformed(ActionEvent e) { JInternalFrame frame = new JInternalFrame(); UserDefinedColours chooser = new UserDefinedColours( frame, alignPanel, null); frame.setContentPane(chooser); Desktop.addInternalFrame(frame,"User defined colours", 450,540 ); frame.setResizable(false); frame.setIconifiable(false); frame.setMaximizable(false); } public void PIDColour_actionPerformed(ActionEvent e) { viewport.setGlobalColourScheme( new PIDColourScheme() ); alignPanel.setColourScheme( ); } public void BLOSUM62Colour_actionPerformed(ActionEvent e) { viewport.setGlobalColourScheme( new Blosum62ColourScheme(viewport) ); changeColour(); } public void sortPairwiseMenuItem_actionPerformed(ActionEvent e) { addHistoryItem("sort"); AlignmentSorter.sortByPID(viewport.getAlignment(), viewport.getAlignment().getSequenceAt(0)); alignPanel.RefreshPanels(); } public void sortIDMenuItem_actionPerformed(ActionEvent e) { addHistoryItem("sort"); AlignmentSorter.sortByID( viewport.getAlignment() ); alignPanel.RefreshPanels(); } public void sortGroupMenuItem_actionPerformed(ActionEvent e) { addHistoryItem("sort"); AlignmentSorter.sortByGroup(viewport.getAlignment()); AlignmentSorter.sortGroups(viewport.getAlignment()); alignPanel.RefreshPanels(); } public void sortTreeOrderMenuItem_actionPerformed(ActionEvent e) { addHistoryItem("sort"); if(viewport.getCurrentTree()==null) return; AlignmentSorter.sortByTree(viewport.getAlignment(), viewport.getCurrentTree()); alignPanel.RefreshPanels(); } public void removeRedundancyMenuItem_actionPerformed(ActionEvent e) { RedundancyPanel sp = new RedundancyPanel(alignPanel); JInternalFrame frame = new JInternalFrame(); frame.setContentPane(sp); Desktop.addInternalFrame(frame, "Redundancy threshold selection", 400, 100); frame.setMaximizable(false); frame.setResizable(false); } public void pairwiseAlignmentMenuItem_actionPerformed(ActionEvent e) { if(viewport.getSelection().size()<2) JOptionPane.showInternalMessageDialog(this, "You must select at least 2 sequences.", "Invalid Selection", JOptionPane.WARNING_MESSAGE); else { JInternalFrame frame = new JInternalFrame(); frame.setContentPane(new PairwiseAlignPanel(viewport)); Desktop.addInternalFrame(frame, "Pairwise Alignment", 600, 500); } } public void PCAMenuItem_actionPerformed(ActionEvent e) { if( (viewport.getSelection().size()<4 && viewport.getSelection().size()>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; } 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); } } public void averageDistanceTreeMenuItem_actionPerformed(ActionEvent e) { NewTreePanel("AV", "PID", "Average distance tree using PID"); } public void neighbourTreeMenuItem_actionPerformed(ActionEvent e) { NewTreePanel("NJ", "PID", "Neighbour joining tree using PID"); } protected void njTreeBlosumMenuItem_actionPerformed(ActionEvent e) { NewTreePanel("NJ", "BL", "Neighbour joining tree using BLOSUM62"); } protected void avTreeBlosumMenuItem_actionPerformed(ActionEvent e) { NewTreePanel("AV", "BL", "Average distance tree using BLOSUM62PID"); } void NewTreePanel(String type, String pwType, String title) { 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); } 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); } class ClustalThread extends JPanel implements Runnable { Image [] image; int imageIndex = 0; boolean webServiceRunning = false; JInternalFrame frame; public ClustalThread(JInternalFrame frame) { this.frame = frame; image = new Image[9]; for(int i=0; i<9; i++) { java.net.URL url = getClass().getResource("/dna" + (i+1) + ".gif"); if (url != null) { 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){} } } DNATwirler twirler = new DNATwirler(); twirler.start(); webServiceRunning = true; } class DNATwirler extends Thread { public void run() { while(webServiceRunning) { try{ Thread.sleep(100); imageIndex++; imageIndex %=9; repaint(); } catch(Exception ex){} } } } // 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)); af.clustalColour.setSelected(true); af.clustalColour_actionPerformed(null); 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){} } 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); } } public void doKeyPressed(KeyEvent evt) { if(evt.isControlDown() && evt.getKeyChar()=='f') findMenuItem_actionPerformed(null); } }