/* * 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 Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ package jalview.appletgui; import jalview.jbappletgui.GAlignFrame; import jalview.schemes.*; import jalview.datamodel.*; import jalview.analysis.*; import jalview.io.*; import java.awt.*; import java.awt.event.*; import java.util.*; import java.io.InputStreamReader; import java.io.BufferedReader; public class AlignFrame extends GAlignFrame { final AlignmentPanel alignPanel; final AlignViewport viewport; public static final int NEW_WINDOW_WIDTH = 700; public static final int NEW_WINDOW_HEIGHT = 500; jalview.bin.JalviewLite applet; public AlignFrame(AlignmentI al) { this.applet = null; viewport = new AlignViewport(al, null); alignPanel = new AlignmentPanel(this, viewport); add(alignPanel); alignPanel.validate(); this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { closeMenuItem_actionPerformed(null); } }); } public AlignFrame(AlignmentI al, jalview.bin.JalviewLite applet) { this.applet = applet; viewport = new AlignViewport(al, applet); alignPanel = new AlignmentPanel(this, viewport); add(alignPanel); alignPanel.validate(); String treeFile = applet.getParameter("treeFile"); if (treeFile != null) { try { treeFile = treeFile; jalview.io.NewickFile fin = new jalview.io.NewickFile(applet. getCodeBase() + treeFile, "URL"); fin.parse(); if (fin.getTree() != null) { TreePanel tp = null; tp = new TreePanel(viewport, viewport.getAlignment().getSequences(), fin, "FromFile", applet.getCodeBase() + treeFile); jalview.bin.JalviewLite.addFrame(tp, treeFile, 600, 500); addTreeMenuItem(tp, treeFile); } } catch (Exception ex) { ex.printStackTrace(); } } this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { closeMenuItem_actionPerformed(null); } }); } public void inputText_actionPerformed(ActionEvent e) { CutAndPasteTransfer cap = new CutAndPasteTransfer(true); Frame frame = new Frame(); frame.add(cap); jalview.bin.JalviewLite.addFrame(frame, "Cut & Paste Input", 500, 500); } protected void outputText_actionPerformed(ActionEvent e) { CutAndPasteTransfer cap = new CutAndPasteTransfer(false); Frame frame = new Frame(); frame.add(cap); jalview.bin.JalviewLite.addFrame(frame, "Alignment output - " + e.getActionCommand(), 600, 500); cap.setText(FormatAdapter.formatSequences(e.getActionCommand(), viewport.getAlignment(). getSequences())); } public void closeMenuItem_actionPerformed(ActionEvent e) { PaintRefresher.components.remove(viewport.alignment); dispose(); } Stack historyList = new Stack(); Stack redoList = new Stack(); void updateEditMenuBar() { if (historyList.size() > 0) { undoMenuItem.setEnabled(true); HistoryItem hi = (HistoryItem) historyList.peek(); undoMenuItem.setLabel("Undo " + hi.getDescription()); } else { undoMenuItem.setEnabled(false); undoMenuItem.setLabel("Undo"); } if (redoList.size() > 0) { redoMenuItem.setEnabled(true); HistoryItem hi = (HistoryItem) redoList.peek(); redoMenuItem.setLabel("Redo " + hi.getDescription()); } else { redoMenuItem.setEnabled(false); redoMenuItem.setLabel("Redo"); } } public void addHistoryItem(HistoryItem hi) { historyList.push(hi); updateEditMenuBar(); } protected void undoMenuItem_actionPerformed(ActionEvent e) { HistoryItem hi = (HistoryItem) historyList.pop(); redoList.push(new HistoryItem(hi.getDescription(), viewport.alignment, HistoryItem.HIDE)); restoreHistoryItem(hi); } protected void redoMenuItem_actionPerformed(ActionEvent e) { HistoryItem hi = (HistoryItem) redoList.pop(); restoreHistoryItem(hi); updateEditMenuBar(); viewport.updateConsensus(); alignPanel.repaint(); alignPanel.repaint(); } // used by undo and redo void restoreHistoryItem(HistoryItem hi) { if (hi.getType() == HistoryItem.SORT) { for (int i = 0; i < hi.getSequences().size(); i++) { viewport.alignment.getSequences().setElementAt(hi.getSequences(). elementAt(i), i); } } else { for (int i = 0; i < hi.getSequences().size(); i++) { SequenceI restore = (SequenceI) hi.getSequences().elementAt(i); if (restore.getLength() == 0) { // log.System.out.println(hi.getHidden().elementAt(i)); restore.setSequence(hi.getHidden().elementAt(i).toString()); viewport.alignment.getSequences().insertElementAt( restore, hi.getAlignIndex(i)); } else { restore.setSequence(hi.getHidden().elementAt(i).toString()); } } if (hi.getType() == HistoryItem.PASTE) { for (int i = viewport.alignment.getHeight() - 1; i > hi.getSequences().size() - 1; i--) { viewport.alignment.deleteSequence(i); } } } updateEditMenuBar(); viewport.updateConsensus(); viewport.updateConservation(); alignPanel.repaint(); } 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.sequences.contains(seq)) { continue; } SequenceI temp = viewport.alignment.getSequenceAt(i - 1); if (sg.sequences.contains(temp)) { continue; } viewport.alignment.getSequences().setElementAt(temp, i); viewport.alignment.getSequences().setElementAt(seq, i - 1); } } else { for (int i = viewport.alignment.getHeight() - 2; i > -1; i--) { SequenceI seq = viewport.alignment.getSequenceAt(i); if (!sg.sequences.contains(seq)) { continue; } SequenceI temp = viewport.alignment.getSequenceAt(i + 1); if (sg.sequences.contains(temp)) { continue; } viewport.alignment.getSequences().setElementAt(temp, i); viewport.alignment.getSequences().setElementAt(seq, i + 1); } } alignPanel.repaint(); } StringBuffer copiedSequences; protected void copy_actionPerformed(ActionEvent e) { if (viewport.getSelectionGroup() == null) { return; } SequenceGroup sg = viewport.getSelectionGroup(); copiedSequences = new StringBuffer(); Hashtable orderedSeqs = new Hashtable(); for (int i = 0; i < sg.getSize(); i++) { SequenceI seq = sg.getSequenceAt(i); int index = viewport.alignment.findIndex(seq); orderedSeqs.put(index + "", seq); } int index = 0, startRes, endRes; char ch; for (int i = 0; i < sg.getSize(); i++) { SequenceI seq = null; while (seq == null) { if (orderedSeqs.containsKey(index + "")) { seq = (SequenceI) orderedSeqs.get(index + ""); index++; break; } else { index++; } } //FIND START RES //Returns residue following index if gap startRes = seq.findPosition(sg.getStartRes()); //FIND END RES //Need to find the residue preceeding index if gap endRes = 0; for (int j = 0; j < sg.getEndRes()+1 && j < seq.getLength(); j++) { ch = seq.getCharAt(j); if (!jalview.util.Comparison.isGap( (ch))) { endRes++; } } if(endRes>0) { endRes += seq.getStart() -1; } copiedSequences.append(seq.getName() + "\t" + startRes + "\t" + endRes + "\t" + seq.getSequence(sg.getStartRes(), sg.getEndRes() + 1) + "\n"); } } protected void pasteNew_actionPerformed(ActionEvent e) { paste(true); } protected void pasteThis_actionPerformed(ActionEvent e) { addHistoryItem(new HistoryItem("Paste Sequences", viewport.alignment, HistoryItem.PASTE)); paste(false); } void paste(boolean newAlignment) { try { if (copiedSequences == null) { return; } StringTokenizer st = new StringTokenizer(copiedSequences.toString()); Vector seqs = new Vector(); while (st.hasMoreElements()) { String name = st.nextToken(); int start = Integer.parseInt(st.nextToken()); int end = Integer.parseInt(st.nextToken()); Sequence sequence = new Sequence(name, st.nextToken(), start, end); if (!newAlignment) { viewport.alignment.addSequence(sequence); } else { seqs.addElement(sequence); } } if (newAlignment) { SequenceI[] newSeqs = new SequenceI[seqs.size()]; for (int i = 0; i < seqs.size(); i++) { newSeqs[i] = (SequenceI) seqs.elementAt(i); } AlignFrame af = new AlignFrame(new Alignment(newSeqs), applet); String newtitle = new String("Copied sequences"); if (getTitle().startsWith("Copied sequences")) { newtitle = getTitle(); } else { newtitle = newtitle.concat("- from " + getTitle()); } jalview.bin.JalviewLite.addFrame(af, newtitle, NEW_WINDOW_WIDTH, NEW_WINDOW_HEIGHT); } else { viewport.setEndSeq(viewport.alignment.getHeight()); viewport.alignment.getWidth(); viewport.updateConservation(); viewport.updateConsensus(); alignPanel.repaint(); } } catch (Exception ex) {} // could be anything being pasted in here } protected void cut_actionPerformed(ActionEvent e) { copy_actionPerformed(null); delete_actionPerformed(null); } protected void delete_actionPerformed(ActionEvent e) { addHistoryItem(new HistoryItem("Delete Sequences", viewport.alignment, HistoryItem.HIDE)); if (viewport.getSelectionGroup() == null) { return; } SequenceGroup sg = viewport.getSelectionGroup(); boolean allSequences = false; if(sg.sequences.size()==viewport.alignment.getHeight()) allSequences = true; for (int i = 0; i < sg.sequences.size(); i++) { SequenceI seq = sg.getSequenceAt(i); int index = viewport.getAlignment().findIndex(seq); seq.deleteChars(sg.getStartRes(), sg.getEndRes() + 1); // If the cut affects all sequences, remove highlighted columns if (allSequences) { viewport.getColumnSelection().removeElements(sg.getStartRes(), sg.getEndRes() + 1); } if (seq.getSequence().length() < 1) { viewport.getAlignment().deleteSequence(seq); } else { viewport.getAlignment().getSequences().setElementAt(seq, index); } } viewport.setSelectionGroup(null); viewport.alignment.deleteGroup(sg); viewport.resetSeqLimits(alignPanel.seqPanel.seqCanvas.getSize().height); if (viewport.getAlignment().getHeight() < 1) { try { this.setVisible(false); } catch (Exception ex) {} } viewport.updateConservation(); viewport.updateConsensus(); alignPanel.repaint(); } protected void deleteGroups_actionPerformed(ActionEvent e) { viewport.alignment.deleteAllGroups(); viewport.setSelectionGroup(null); alignPanel.repaint(); } 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); alignPanel.repaint(); PaintRefresher.Refresh(null, viewport.alignment); } public void deselectAllSequenceMenuItem_actionPerformed(ActionEvent e) { viewport.setSelectionGroup(null); viewport.getColumnSelection().clear(); viewport.setSelectionGroup(null); PaintRefresher.Refresh(null, viewport.alignment); } public void invertSequenceMenuItem_actionPerformed(ActionEvent e) { SequenceGroup sg = viewport.getSelectionGroup(); for (int i = 0; i < viewport.getAlignment().getSequences().size(); i++) { sg.addOrRemove(viewport.getAlignment().getSequenceAt(i), false); } PaintRefresher.Refresh(null, viewport.alignment); } 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.elementAt(i); if (!sg.adjustForRemoveLeft(min)) { viewport.alignment.deleteGroup(sg); } } alignPanel.repaint(); } } 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.elementAt(i); if (!sg.adjustForRemoveRight(max)) { viewport.alignment.deleteGroup(sg); } } alignPanel.repaint(); } } public void removeGappedColumnMenuItem_actionPerformed(ActionEvent e) { addHistoryItem(new HistoryItem("Remove Gapped Columns", viewport.alignment, HistoryItem.HIDE)); viewport.getAlignment().removeGaps(); viewport.updateConservation(); viewport.updateConsensus(); resetAllClustalSchemes(); alignPanel.repaint(); } public void removeAllGapsMenuItem_actionPerformed(ActionEvent e) { addHistoryItem(new HistoryItem("Remove Gaps", viewport.alignment, HistoryItem.HIDE)); SequenceI current; int jSize; Vector seqs=null; int start=0, 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(); } else seqs = viewport.alignment.getSequences(); for (int i = 0; i 1) { Frame frame = new Frame(); frame.add(new PairwiseAlignPanel(viewport)); jalview.bin.JalviewLite.addFrame(frame, "Pairwise Alignment", 600, 500); } } public void PCAMenuItem_actionPerformed(ActionEvent e) { if ( (viewport.getSelectionGroup() != null && viewport.getSelectionGroup().getSize() < 4 && viewport.getSelectionGroup().getSize() > 0) || viewport.getAlignment().getHeight() < 4) { return; } try { PCAPanel pcaPanel = new PCAPanel(viewport, null); Frame frame = new Frame(); frame.add(pcaPanel); jalview.bin.JalviewLite.addFrame(frame, "Principal component analysis", 400, 400); } catch (java.lang.OutOfMemoryError ex) { } } 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) { //are the sequences aligned? if (!viewport.alignment.isAligned()) { SequenceI current; int Width = viewport.getAlignment().getWidth(); for (int i = 0; i < viewport.getAlignment().getSequences().size(); i++) { current = viewport.getAlignment().getSequenceAt(i); if (current.getLength() < Width) { current.insertCharAt(Width - 1, viewport.getGapCharacter()); } } alignPanel.repaint(); } final TreePanel tp; if (viewport.getSelectionGroup() != null && viewport.getSelectionGroup().getSize() > 3) { tp = new TreePanel(viewport, viewport.getSelectionGroup().sequences, type, pwType, 0, viewport.alignment.getWidth()); } else { tp = new TreePanel(viewport, viewport.getAlignment().getSequences(), type, pwType, 0, viewport.alignment.getWidth()); } addTreeMenuItem(tp, title); jalview.bin.JalviewLite.addFrame(tp, title, 600, 500); } void addTreeMenuItem(final TreePanel treePanel, String title) { final MenuItem item = new MenuItem(title); sortByTreeMenu.add(item); item.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(ActionEvent e) { addHistoryItem(new HistoryItem("Sort", viewport.alignment, HistoryItem.SORT)); AlignmentSorter.sortByTree(viewport.getAlignment(), treePanel.getTree()); alignPanel.repaint(); } }); treePanel.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { sortByTreeMenu.remove(item); }; }); } protected void documentation_actionPerformed(ActionEvent e) { jalview.bin.JalviewLite.showURL("http://www.jalview.org/help.html"); } protected void about_actionPerformed(ActionEvent e) { class AboutPanel extends Canvas { String version; public AboutPanel(String version) { this.version = version; } public void paint(Graphics g) { g.setColor(Color.white); g.fillRect(0, 0, getSize().width, getSize().height); g.setFont(new Font("Helvetica", Font.PLAIN, 12)); FontMetrics fm = g.getFontMetrics(); int fh = fm.getHeight(); int y = 5, x = 7; g.setColor(Color.black); g.setFont(new Font("Helvetica", Font.BOLD, 14)); g.drawString("Jalview - Release "+version, 200, y += fh); g.setFont(new Font("Helvetica", Font.PLAIN, 12)); g.drawString("Authors: Michele Clamp, James Cuff, Steve Searle, Andrew Waterhouse, Jim Procter & Geoff Barton.", x, y += fh * 2); g.drawString("Current development managed by Andrew Waterhouse; Barton Group, University of Dundee.", x, y += fh); g.drawString( "For any issues relating to Jalview, email help@jalview.org", x, y += fh); g.drawString("If you use JalView, please cite:", x, y += fh + 8); g.drawString("\"Clamp, M., Cuff, J., Searle, S. M. and Barton, G. J. (2004), The Jalview Java Alignment Editor\"", x, y += fh); g.drawString("Bioinformatics, 2004 12;426-7.", x, y += fh); } } String version = "test"; java.net.URL url = getClass().getResource("/.build_properties"); if (url != null) { try { BufferedReader reader = new BufferedReader(new InputStreamReader( url.openStream())); String line; while ( (line = reader.readLine()) != null) { if (line.indexOf("VERSION") > -1) { version = line.substring(line.indexOf("=") + 1); } } } catch (Exception ex) { ex.printStackTrace(); } } Frame frame = new Frame(); frame.add(new AboutPanel(version)); jalview.bin.JalviewLite.addFrame(frame, "Jalview", 580, 200); } }