package jalview.gui; import jalview.datamodel.*; import jalview.analysis.*; import jalview.jbgui.GTreePanel; import jalview.io.*; import java.awt.event.*; import java.util.*; import java.io.*; import jalview.util.*; import javax.swing.*; public class TreePanel extends GTreePanel { SequenceI[] seq; String type; String pwtype; AlignViewport av; int start; int end; TreeCanvas treeCanvas; NJTree tree; FileProperties fp; PostscriptProperties pp; PrintWriter bw; PrintStream ps; boolean makeString = false; StringBuffer out; public TreePanel(AlignViewport av, Vector seqVector, String type, String pwtype, int s, int e) { super(); this.type = type; this.pwtype = pwtype; start = s; end = e; String longestName = ""; seq = new Sequence [seqVector.size()]; for (int i=0;i < seqVector.size();i++) { seq[i] = (Sequence) seqVector.elementAt(i); if(seq[i].getName().length()>longestName.length()) longestName = seq[i].getName(); } tree = new NJTree(seq, type, pwtype, start, end); av.setCurrentTree(tree); propertiesInit(); treeCanvas = new TreeCanvas(av, tree, scrollPane, longestName); tree.reCount(tree.getTopNode()); tree.findHeight(tree.getTopNode()); scrollPane.setViewportView(treeCanvas); } public void drawPostscript(PostscriptProperties pp) { try { int width = 0; int height = 0; printout("%!\n"); printout("/" + pp.font + " findfont\n"); printout(pp.fsize + " scalefont setfont\n"); int offx = pp.xoffset; int offy = pp.yoffset; if (pp.orientation == PostscriptProperties.PORTRAIT) { width = PostscriptProperties.SHORTSIDE; height = PostscriptProperties.LONGSIDE; } else { height = PostscriptProperties.SHORTSIDE; width = PostscriptProperties.LONGSIDE; printout(height + " 0 translate\n90 rotate\n"); } float wscale = (float)(width*.8-offx*2)/tree.getMaxHeight(); SequenceNode top = tree.getTopNode(); if (top.count == 0) { top.count = ((SequenceNode)top.left()).count + ((SequenceNode)top.right()).count ; } float chunk = (float)(height-offy*2)/(((SequenceNode)top).count+1); drawPostscriptNode(top,chunk,wscale,width,offx,offy); printout("showpage\n"); } catch (java.io.IOException e) { System.out.println("Exception " + e); } } public void drawPostscriptNode(SequenceNode node, float chunk, float scale, int width, int offx, int offy) throws java.io.IOException { if (node == null) { return; } if (node.left() == null && node.right() == null) { // Drawing leaf node float height = node.height; float dist = node.dist; int xstart = (int)((height-dist)*scale) + offx; int xend = (int)(height*scale) + offx; int ypos = (int)(node.ycount * chunk) + offy; // g.setColor(Color.black); printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " + new Format("%5.3f").form((double)node.color.getGreen()/255) + " " + new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n"); // Draw horizontal line // g.drawLine(xstart,ypos,xend,ypos); printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke \n"); if (treeCanvas.showDistances && node.dist > 0) { // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5); printout("(" + new Format("%5.2f").form(node.dist) + ") " + xstart + " " + (ypos+5) + " moveto show\n"); } //g.drawString((String)node.element(),xend+20,ypos); printout("(" + (((SequenceI)node.element()).getName()) + ") " + (xend+20) + " " + (ypos) + " moveto show\n"); } else { drawPostscriptNode((SequenceNode)node.left(),chunk,scale,width,offx,offy); drawPostscriptNode((SequenceNode)node.right(),chunk,scale,width,offx,offy); float height = node.height; float dist = node.dist; int xstart = (int)((height-dist)*scale) + offx; int xend = (int)(height*scale) + offx; int ypos = (int)(node.ycount * chunk) + offy; printout("\n" + new Format("%5.3f").form((double)node.color.getRed()/255) + " " + new Format("%5.3f").form((double)node.color.getGreen()/255) + " " + new Format("%5.3f").form((double)node.color.getBlue()/255) + " setrgbcolor\n"); // g.setColor(Color.black); // bw.append("\nblack setrgbcolor\n"); // Draw horizontal line // g.drawLine(xstart,ypos,xend,ypos); printout(xstart + " " + ypos + " moveto " + xend + " " + ypos + " lineto stroke\n"); int ystart = (int)(((SequenceNode)node.left()).ycount * chunk) + offy; int yend = (int)(((SequenceNode)node.right()).ycount * chunk) + offy; // g.drawLine((int)(height*scale) + offx, ystart, // (int)(height*scale) + offx, yend); printout (((int)(height*scale) + offx) + " " + ystart + " moveto " + ((int)(height*scale) + offx) + " " + yend + " lineto stroke\n"); if (treeCanvas.showDistances && node.dist > 0) { // g.drawString(new Format("%5.2f").form(node.dist),xstart,ypos - 5); printout("(" +new Format("%5.2f").form(node.dist) + ") " + (xstart) + " " + (ypos+5) + " moveto show\n"); } } } public void printout(String s) throws IOException { if (bw != null) { bw.write(s); } if (ps != null) { ps.print(s); } if (makeString == true) { out.append(s); } } public PostscriptProperties getPostscriptProperties() { return pp; } public FileProperties getFileProperties() { return fp; } public void setPostscriptProperties(PostscriptProperties pp) { this.pp = pp; } public void setFileProperties(FileProperties fp) { this.fp = fp; } public String getText(String format) { return null; } public void getPostscript(PrintWriter bw) { this.bw = bw; drawPostscript(pp); } public void getPostscript(PrintStream bw) { this.ps = bw; drawPostscript(pp); bw.flush(); } public StringBuffer getPostscript() { makeString = true; out = new StringBuffer(); drawPostscript(pp); return out; } public void propertiesInit() { this.pp = new PostscriptProperties(); this.fp = new FileProperties(); } public void saveButton_actionPerformed(ActionEvent e) { try{ JFileChooser chooser = new JFileChooser(jalview.bin.Cache.LAST_DIRECTORY); chooser.setDialogTitle("Save Tree as postscript file"); chooser.setToolTipText("Save"); int value = chooser.showSaveDialog(this); if (value == JFileChooser.APPROVE_OPTION) { String choice = chooser.getSelectedFile().getPath(); jalview.bin.Cache.LAST_DIRECTORY = choice; bw = new PrintWriter(new FileWriter(choice)); getPostscript(bw); bw.close(); } }catch(Exception ex){ex.printStackTrace();} } public void showDistanceCheckbox_actionPerformed(ActionEvent e) { treeCanvas.setShowDistances(showDistanceCheckbox.isSelected()); } public void fontSizeCombobox_actionPerformed(ActionEvent e) { if( treeCanvas==null ) return; int size = Integer.parseInt( fontSizeCombobox.getSelectedItem().toString() ); treeCanvas.setFontSize( size ); scrollPane.setViewportView(treeCanvas); } public void bootStrapCheckBox_actionPerformed(ActionEvent e) { treeCanvas.setShowBootstrap(bootStrapCheckBox.isSelected()); } }