X-Git-Url: http://source.jalview.org/gitweb/?a=blobdiff_plain;f=src%2Fjalview%2Fgui%2FTreePanel.java;h=e77134ecd9c32bade9b049cb0d7ae5e3ef2b3992;hb=153dd62dc91da13ae732600e6ea55ddbe15eab39;hp=72bdbf113fde5343559919ac6be2b3f82aedfab8;hpb=7bc226b58110fa26d9dbd3f0c78095d06909ffc3;p=jalview.git diff --git a/src/jalview/gui/TreePanel.java b/src/jalview/gui/TreePanel.java index 72bdbf1..e77134e 100755 --- a/src/jalview/gui/TreePanel.java +++ b/src/jalview/gui/TreePanel.java @@ -1,20 +1,19 @@ /* - * Jalview - A Sequence Alignment Editor and Viewer - * Copyright (C) 2007 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 + * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6) + * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle + * + * This file is part of Jalview. + * + * Jalview 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 3 of the License, or (at your option) any later version. + * + * Jalview 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 Jalview. If not, see . */ package jalview.gui; @@ -30,66 +29,78 @@ import javax.swing.*; import org.jibble.epsgraphics.*; import jalview.analysis.*; +import jalview.commands.OrderCommand; import jalview.datamodel.*; import jalview.io.*; import jalview.jbgui.*; /** * DOCUMENT ME! - * + * * @author $author$ * @version $Revision$ */ -public class TreePanel - extends GTreePanel +public class TreePanel extends GTreePanel { String type; + String pwtype; + TreeCanvas treeCanvas; + NJTree tree; + AlignViewport av; /** * Creates a new TreePanel object. - * - * @param av DOCUMENT ME! - * @param seqVector DOCUMENT ME! - * @param type DOCUMENT ME! - * @param pwtype DOCUMENT ME! - * @param s DOCUMENT ME! - * @param e DOCUMENT ME! + * + * @param av + * DOCUMENT ME! + * @param seqVector + * DOCUMENT ME! + * @param type + * DOCUMENT ME! + * @param pwtype + * DOCUMENT ME! + * @param s + * DOCUMENT ME! + * @param e + * DOCUMENT ME! */ public TreePanel(AlignmentPanel ap, String type, String pwtype) { super(); initTreePanel(ap, type, pwtype, null, null); - // We know this tree has distances. JBPNote TODO: prolly should add this as a userdefined default + // We know this tree has distances. JBPNote TODO: prolly should add this as + // a userdefined default // showDistances(true); } /** * Creates a new TreePanel object. - * - * @param av DOCUMENT ME! - * @param seqVector DOCUMENT ME! - * @param newtree DOCUMENT ME! - * @param type DOCUMENT ME! - * @param pwtype DOCUMENT ME! + * + * @param av + * DOCUMENT ME! + * @param seqVector + * DOCUMENT ME! + * @param newtree + * DOCUMENT ME! + * @param type + * DOCUMENT ME! + * @param pwtype + * DOCUMENT ME! */ - public TreePanel(AlignmentPanel ap, - String type, - String pwtype, - NewickFile newtree) + public TreePanel(AlignmentPanel ap, String type, String pwtype, + NewickFile newtree) { super(); initTreePanel(ap, type, pwtype, newtree, null); } - public TreePanel(AlignmentPanel av, - String type, - String pwtype, - NewickFile newtree, AlignmentView inputData) + public TreePanel(AlignmentPanel av, String type, String pwtype, + NewickFile newtree, AlignmentView inputData) { super(); initTreePanel(av, type, pwtype, newtree, inputData); @@ -106,7 +117,7 @@ public class TreePanel } void initTreePanel(AlignmentPanel ap, String type, String pwtype, - NewickFile newTree, AlignmentView inputData) + NewickFile newTree, AlignmentView inputData) { av = ap.av; @@ -129,14 +140,20 @@ public class TreePanel if (tree == null) { System.out.println("tree is null"); + // TODO: deal with case when a change event is received whilst a + // tree is still being calculated - should save reference for + // processing message later. + return; } if (evt.getNewValue() == null) { - System.out.println("new alignment sequences vector value is null"); + System.out + .println("new alignment sequences vector value is null"); } - tree.UpdatePlaceHolders( (Vector) evt.getNewValue()); - treeCanvas.nameHash.clear(); // reset the mapping between canvas rectangles and leafnodes + tree.UpdatePlaceHolders((Vector) evt.getNewValue()); + treeCanvas.nameHash.clear(); // reset the mapping between canvas + // rectangles and leafnodes repaint(); } } @@ -158,8 +175,8 @@ public class TreePanel void buildAssociatedViewMenu() { - AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av. - getSequenceSetId()); + AlignmentPanel[] aps = PaintRefresher.getAssociatedPanels(av + .getSequenceSetId()); if (aps.length == 1 && treeCanvas.ap == aps[0]) { associateLeavesMenu.setVisible(false); @@ -168,7 +185,7 @@ public class TreePanel associateLeavesMenu.setVisible(true); - if ( (viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem)) + if ((viewMenu.getItem(viewMenu.getItemCount() - 2) instanceof JMenuItem)) { viewMenu.insertSeparator(viewMenu.getItemCount() - 1); } @@ -212,11 +229,12 @@ public class TreePanel } - class TreeLoader - extends Thread + class TreeLoader extends Thread { NewickFile newtree; + jalview.datamodel.AlignmentView odata = null; + public TreeLoader(NewickFile newtree) { this.newtree = newtree; @@ -236,12 +254,12 @@ public class TreePanel { if (odata == null) { - tree = new NJTree(av.alignment.getSequencesArray(), - newtree); + tree = new NJTree(av.alignment.getSequencesArray(), newtree); } else { - tree = new NJTree(av.alignment.getSequencesArray(), odata, newtree); + tree = new NJTree(av.alignment.getSequencesArray(), odata, + newtree); } if (!tree.hasOriginalSequenceData()) { @@ -252,7 +270,8 @@ public class TreePanel { int start, end; SequenceI[] seqs; - AlignmentView seqStrings = av.getAlignmentView(av.getSelectionGroup() != null); + AlignmentView seqStrings = av.getAlignmentView(av + .getSelectionGroup() != null); if (av.getSelectionGroup() == null) { start = 0; @@ -275,7 +294,10 @@ public class TreePanel treeCanvas.setTree(tree); treeCanvas.repaint(); av.setCurrentTree(tree); - + if (av.getSortByTree()) + { + sortByTree_actionPerformed(null); + } } } @@ -304,7 +326,7 @@ public class TreePanel /** * DOCUMENT ME! - * + * * @return DOCUMENT ME! */ public NJTree getTree() @@ -314,8 +336,9 @@ public class TreePanel /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ public void textbox_actionPerformed(ActionEvent e) { @@ -341,23 +364,31 @@ public class TreePanel buffer.append("PID"); } - Desktop.addInternalFrame(cap, buffer.toString(), 500, 100); - jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode()); - cap.setText(fout.print(tree.isHasBootstrap(), tree.isHasDistances(), - tree.isHasRootDistance())); + jalview.io.NewickFile fout = new jalview.io.NewickFile(tree + .getTopNode()); + try { + cap.setText(fout.print(tree.isHasBootstrap(), tree.isHasDistances(), + tree.isHasRootDistance())); + Desktop.addInternalFrame(cap, buffer.toString(), 500, 100); + } catch (OutOfMemoryError oom) + { + new OOMWarning("generating newick tree file",oom); + cap.dispose(); + } + } /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ public void saveAsNewick_actionPerformed(ActionEvent e) { - JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache. - getProperty( - "LAST_DIRECTORY")); + JalviewFileChooser chooser = new JalviewFileChooser(jalview.bin.Cache + .getProperty("LAST_DIRECTORY")); chooser.setFileView(new JalviewFileView()); chooser.setDialogTitle("Save tree as newick file"); chooser.setToolTipText("Save"); @@ -367,21 +398,20 @@ public class TreePanel if (value == JalviewFileChooser.APPROVE_OPTION) { String choice = chooser.getSelectedFile().getPath(); - jalview.bin.Cache.setProperty("LAST_DIRECTORY", - chooser.getSelectedFile().getParent()); + jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser + .getSelectedFile().getParent()); try { - jalview.io.NewickFile fout = new jalview.io.NewickFile(tree.getTopNode()); - String output = fout.print(tree.isHasBootstrap(), tree.isHasDistances(), - tree.isHasRootDistance()); - java.io.PrintWriter out = new java.io.PrintWriter(new java.io. - FileWriter( - choice)); + jalview.io.NewickFile fout = new jalview.io.NewickFile(tree + .getTopNode()); + String output = fout.print(tree.isHasBootstrap(), tree + .isHasDistances(), tree.isHasRootDistance()); + java.io.PrintWriter out = new java.io.PrintWriter( + new java.io.FileWriter(choice)); out.println(output); out.close(); - } - catch (Exception ex) + } catch (Exception ex) { ex.printStackTrace(); } @@ -390,12 +420,13 @@ public class TreePanel /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ public void printMenu_actionPerformed(ActionEvent e) { - //Putting in a thread avoids Swing painting problems + // Putting in a thread avoids Swing painting problems treeCanvas.startPrinting(); } @@ -403,22 +434,36 @@ public class TreePanel { if (!tree.hasOriginalSequenceData()) { - jalview.bin.Cache.log.info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action."); + jalview.bin.Cache.log + .info("Unexpected call to originalSeqData_actionPerformed - should have hidden this menu action."); return; } - // decide if av alignment is sufficiently different to original data to warrant a new window to be created - // create new alignmnt window with hidden regions (unhiding hidden regions yields unaligned seqs) + // decide if av alignment is sufficiently different to original data to + // warrant a new window to be created + // create new alignmnt window with hidden regions (unhiding hidden regions + // yields unaligned seqs) // or create a selection box around columns in alignment view // test Alignment(SeqCigar[]) - Object[] alAndColsel = tree.seqData.getAlignmentAndColumnSelection(av. - getGapCharacter()); + char gc = '-'; + try + { + // we try to get the associated view's gap character + // but this may fail if the view was closed... + gc = av.getGapCharacter(); + + } catch (Exception ex) + { + } + ; + Object[] alAndColsel = tree.seqData.getAlignmentAndColumnSelection(gc); if (alAndColsel != null && alAndColsel[0] != null) { // AlignmentOrder origorder = new AlignmentOrder(alAndColsel[0]); - Alignment al = new Alignment( (SequenceI[]) alAndColsel[0]); - Alignment dataset = av.getAlignment().getDataset(); + Alignment al = new Alignment((SequenceI[]) alAndColsel[0]); + Alignment dataset = (av != null && av.getAlignment() != null) ? av + .getAlignment().getDataset() : null; if (dataset != null) { al.setDataset(dataset); @@ -427,28 +472,28 @@ public class TreePanel if (true) { // make a new frame! - AlignFrame af = new AlignFrame(al, (ColumnSelection) alAndColsel[1], - AlignFrame.DEFAULT_WIDTH, - AlignFrame.DEFAULT_HEIGHT - ); + AlignFrame af = new AlignFrame(al, + (ColumnSelection) alAndColsel[1], AlignFrame.DEFAULT_WIDTH, + AlignFrame.DEFAULT_HEIGHT); - //>>>This is a fix for the moment, until a better solution is found!!<<< + // >>>This is a fix for the moment, until a better solution is + // found!!<<< // af.getFeatureRenderer().transferSettings(alignFrame.getFeatureRenderer()); - // af.addSortByOrderMenuItem(ServiceName + " Ordering", - // msaorder); + // af.addSortByOrderMenuItem(ServiceName + " Ordering", + // msaorder); Desktop.addInternalFrame(af, "Original Data for " + this.title, - AlignFrame.DEFAULT_WIDTH, - AlignFrame.DEFAULT_HEIGHT); + AlignFrame.DEFAULT_WIDTH, AlignFrame.DEFAULT_HEIGHT); } } } /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ public void fitToWindow_actionPerformed(ActionEvent e) { @@ -457,9 +502,28 @@ public class TreePanel } /** + * sort the associated alignment view by the current tree. + * + * @param e + */ + public void sortByTree_actionPerformed(ActionEvent e) + { + AlignmentPanel ap = av.getAlignPanel(); + SequenceI[] oldOrder = av.getAlignment().getSequencesArray(); + AlignmentSorter.sortByTree(av.getAlignment(), tree); + + ap.alignFrame.addHistoryItem(new OrderCommand("Tree Sort", oldOrder, + av.alignment)); + + ap.paintAlignment(true); + + } + + /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ public void font_actionPerformed(ActionEvent e) { @@ -486,8 +550,9 @@ public class TreePanel /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ public void distanceMenu_actionPerformed(ActionEvent e) { @@ -496,8 +561,9 @@ public class TreePanel /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ public void bootstrapMenu_actionPerformed(ActionEvent e) { @@ -506,8 +572,9 @@ public class TreePanel /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ public void placeholdersMenu_actionPerformed(ActionEvent e) { @@ -516,22 +583,22 @@ public class TreePanel /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ public void epsTree_actionPerformed(ActionEvent e) { boolean accurateText = true; String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING", - "Prompt each time"); + "Prompt each time"); // If we need to prompt, and if the GUI is visible then // Prompt for EPS rendering style if (renderStyle.equalsIgnoreCase("Prompt each time") - && ! - (System.getProperty("java.awt.headless") != null - && System.getProperty("java.awt.headless").equals("true"))) + && !(System.getProperty("java.awt.headless") != null && System + .getProperty("java.awt.headless").equals("true"))) { EPSOptions eps = new EPSOptions(); renderStyle = eps.getValue(); @@ -554,12 +621,9 @@ public class TreePanel try { jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser( - jalview.bin.Cache.getProperty( - "LAST_DIRECTORY"), new String[] - {"eps"}, - new String[] - {"Encapsulated Postscript"}, - "Encapsulated Postscript"); + jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[] + { "eps" }, new String[] + { "Encapsulated Postscript" }, "Encapsulated Postscript"); chooser.setFileView(new jalview.io.JalviewFileView()); chooser.setDialogTitle("Create EPS file from tree"); chooser.setToolTipText("Save"); @@ -571,12 +635,11 @@ public class TreePanel return; } - jalview.bin.Cache.setProperty("LAST_DIRECTORY", - chooser.getSelectedFile().getParent()); + jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser + .getSelectedFile().getParent()); FileOutputStream out = new FileOutputStream(chooser.getSelectedFile()); - EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width, - height); + EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width, height); pg.setAccurateTextMode(accurateText); @@ -584,8 +647,7 @@ public class TreePanel pg.flush(); pg.close(); - } - catch (Exception ex) + } catch (Exception ex) { ex.printStackTrace(); } @@ -593,8 +655,9 @@ public class TreePanel /** * DOCUMENT ME! - * - * @param e DOCUMENT ME! + * + * @param e + * DOCUMENT ME! */ public void pngTree_actionPerformed(ActionEvent e) { @@ -604,12 +667,9 @@ public class TreePanel try { jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser( - jalview.bin.Cache.getProperty( - "LAST_DIRECTORY"), new String[] - {"png"}, - new String[] - {"Portable network graphics"}, - "Portable network graphics"); + jalview.bin.Cache.getProperty("LAST_DIRECTORY"), new String[] + { "png" }, new String[] + { "Portable network graphics" }, "Portable network graphics"); chooser.setFileView(new jalview.io.JalviewFileView()); chooser.setDialogTitle("Create PNG image from tree"); @@ -622,23 +682,93 @@ public class TreePanel return; } - jalview.bin.Cache.setProperty("LAST_DIRECTORY", - chooser.getSelectedFile().getParent()); + jalview.bin.Cache.setProperty("LAST_DIRECTORY", chooser + .getSelectedFile().getParent()); FileOutputStream out = new FileOutputStream(chooser.getSelectedFile()); BufferedImage bi = new BufferedImage(width, height, - BufferedImage.TYPE_INT_RGB); + BufferedImage.TYPE_INT_RGB); Graphics png = bi.getGraphics(); treeCanvas.draw(png, width, height); ImageIO.write(bi, "png", out); out.close(); - } - catch (Exception ex) + } catch (Exception ex) { ex.printStackTrace(); } } + + /** + * change node labels to the annotation referred to by labelClass TODO: + * promote to a datamodel modification that can be undone TODO: make argument + * one case of a generic transformation function ie { undoStep = apply(Tree, + * TransformFunction)}; + * + * @param labelClass + */ + public void changeNames(final String labelClass) + { + tree.applyToNodes(new NodeTransformI() + { + + public void transform(BinaryNode node) + { + if (node instanceof SequenceNode + && !((SequenceNode) node).isPlaceholder() + && !((SequenceNode) node).isDummy()) + { + String newname = null; + SequenceI sq = (SequenceI) ((SequenceNode) node).element(); + if (sq != null) + { + // search dbrefs, features and annotation + DBRefEntry[] refs = jalview.util.DBRefUtils.selectRefs(sq + .getDBRef(), new String[] + { labelClass.toUpperCase() }); + if (refs != null) + { + for (int i = 0; i < refs.length; i++) + { + if (newname == null) + { + newname = new String(refs[i].getAccessionId()); + } + else + { + newname = newname + "; " + refs[i].getAccessionId(); + } + } + } + if (newname == null) + { + SequenceFeature sf[] = sq.getSequenceFeatures(); + for (int i = 0; sf != null && i < sf.length; i++) + { + if (sf[i].getType().equals(labelClass)) + { + if (newname == null) + { + newname = new String(sf[i].getDescription()); + } + else + { + newname = newname + "; " + sf[i].getDescription(); + } + } + } + } + } + if (newname != null) + { + String oldname = ((SequenceNode) node).getName(); + // TODO : save in the undo object for this modification. + ((SequenceNode) node).setName(newname); + } + } + } + }); + } }