--- /dev/null
+/*\r
+* Jalview - A Sequence Alignment Editor and Viewer\r
+* Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
+*\r
+* This program is free software; you can redistribute it and/or\r
+* modify it under the terms of the GNU General Public License\r
+* as published by the Free Software Foundation; either version 2\r
+* of the License, or (at your option) any later version.\r
+*\r
+* This program is distributed in the hope that it will be useful,\r
+* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\r
+* GNU General Public License for more details.\r
+*\r
+* You should have received a copy of the GNU General Public License\r
+* along with this program; if not, write to the Free Software\r
+* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
+*/\r
+package MCview;\r
+\r
+import jalview.analysis.AlignSeq;\r
+\r
+import jalview.datamodel.*;\r
+\r
+// JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug\r
+import java.awt.*;\r
+import java.awt.event.*;\r
+\r
+import java.io.*;\r
+\r
+import java.util.*;\r
+\r
+import javax.swing.*;\r
+\r
+\r
+public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListener\r
+{\r
+ MCMatrix idmat = new MCMatrix(3, 3);\r
+ MCMatrix objmat = new MCMatrix(3, 3);\r
+ boolean redrawneeded = true;\r
+ int omx = 0;\r
+ int mx = 0;\r
+ int omy = 0;\r
+ int my = 0;\r
+ public PDBfile pdb;\r
+ int bsize;\r
+ Image img;\r
+ Graphics ig;\r
+ Dimension prefsize;\r
+ float[] centre = new float[3];\r
+ float[] width = new float[3];\r
+ float maxwidth;\r
+ float scale;\r
+ String inStr;\r
+ String inType;\r
+ boolean bysequence = true;\r
+ boolean depthcue = true;\r
+ boolean wire = false;\r
+ boolean bymolecule = false;\r
+ boolean zbuffer = true;\r
+ boolean dragging;\r
+ int xstart;\r
+ int xend;\r
+ int ystart;\r
+ int yend;\r
+ int xmid;\r
+ int ymid;\r
+ Font font = new Font("Helvetica", Font.PLAIN, 10);\r
+ jalview.gui.SequenceRenderer sr;\r
+ jalview.gui.FeatureRenderer fr;\r
+\r
+ public PDBCanvas()\r
+ {\r
+\r
+ }\r
+\r
+ public void setPDBFile(PDBfile pdb, Sequence sequence,\r
+ jalview.gui.SequenceRenderer sr,\r
+ jalview.gui.FeatureRenderer fr)\r
+ {\r
+\r
+\r
+ this.sr = sr;\r
+ this.fr = fr;\r
+ int max = -10;\r
+ int maxchain = -1;\r
+ int pdbstart = 0;\r
+ int pdbend = 0;\r
+ int seqstart = 0;\r
+ int seqend = 0;\r
+\r
+ for (int i = 0; i < pdb.chains.size(); i++) {\r
+ // Now lets compare the sequences to get\r
+ // the start and end points.\r
+ // Align the sequence to the pdb\r
+ AlignSeq as = new AlignSeq(sequence,\r
+ ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep");\r
+ as.calcScoreMatrix();\r
+ as.traceAlignment();\r
+ as.printAlignment();\r
+\r
+\r
+ if (as.maxscore > max) {\r
+ max = as.maxscore;\r
+ maxchain = i;\r
+\r
+ pdbstart = as.seq2start;\r
+ pdbend = as.seq2end;\r
+ seqstart = as.seq1start + sequence.getStart()-1;\r
+ seqend = as.seq1end + sequence.getEnd()-1;\r
+ }\r
+\r
+ System.out.println("PDB start/end " + pdbstart + " " + pdbend);\r
+ System.out.println("SEQ start/end "+ seqstart + " " + seqend);\r
+ }\r
+\r
+ ((PDBChain) pdb.chains.elementAt(maxchain)).pdbstart = pdbstart;\r
+ ((PDBChain) pdb.chains.elementAt(maxchain)).pdbend = pdbend;\r
+ ((PDBChain) pdb.chains.elementAt(maxchain)).seqstart = seqstart;\r
+ ((PDBChain) pdb.chains.elementAt(maxchain)).seqend = seqend;\r
+ ((PDBChain) pdb.chains.elementAt(maxchain)).isVisible = true;\r
+ ((PDBChain) pdb.chains.elementAt(maxchain)).sequence = sequence;\r
+\r
+ this.pdb = pdb;\r
+ this.prefsize = new Dimension(getWidth(), getHeight());\r
+\r
+ //Initialize the matrices to identity\r
+ for (int i = 0; i < 3; i++) {\r
+ for (int j = 0; j < 3; j++) {\r
+ if (i != j) {\r
+ idmat.addElement(i, j, 0);\r
+ objmat.addElement(i, j, 0);\r
+ } else {\r
+ idmat.addElement(i, j, 1);\r
+ objmat.addElement(i, j, 1);\r
+ }\r
+ }\r
+ }\r
+\r
+ addMouseMotionListener(this);\r
+ addMouseListener(this);\r
+\r
+ findCentre();\r
+ findWidth();\r
+\r
+ scale = findScale();\r
+\r
+ System.out.println("Scale factor = " + scale);\r
+\r
+ updateSeqColours();\r
+ ToolTipManager.sharedInstance().registerComponent(this);\r
+ ToolTipManager.sharedInstance().setInitialDelay(0);\r
+ ToolTipManager.sharedInstance().setDismissDelay(10000);\r
+\r
+ }\r
+\r
+ public void deleteBonds() {\r
+ scale = 0;\r
+ maxwidth = 0;\r
+\r
+ width[0] = 0;\r
+ width[1] = 0;\r
+ width[2] = 0;\r
+\r
+ centre[0] = 0;\r
+ centre[1] = 0;\r
+ centre[2] = 0;\r
+\r
+ for (int i = 0; i < pdb.chains.size(); i++) {\r
+ ((PDBChain) pdb.chains.elementAt(i)).bonds = null;\r
+ }\r
+ }\r
+\r
+ public void findWidth() {\r
+ float[] max = new float[3];\r
+ float[] min = new float[3];\r
+\r
+ max[0] = (float) -1e30;\r
+ max[1] = (float) -1e30;\r
+ max[2] = (float) -1e30;\r
+\r
+ min[0] = (float) 1e30;\r
+ min[1] = (float) 1e30;\r
+ min[2] = (float) 1e30;\r
+\r
+ for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
+ if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {\r
+ Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
+\r
+ for (int i = 0; i < bonds.size(); i++) {\r
+ Bond tmp = (Bond) bonds.elementAt(i);\r
+\r
+ if (tmp.start[0] >= max[0]) {\r
+ max[0] = tmp.start[0];\r
+ }\r
+\r
+ if (tmp.start[1] >= max[1]) {\r
+ max[1] = tmp.start[1];\r
+ }\r
+\r
+ if (tmp.start[2] >= max[2]) {\r
+ max[2] = tmp.start[2];\r
+ }\r
+\r
+ if (tmp.start[0] <= min[0]) {\r
+ min[0] = tmp.start[0];\r
+ }\r
+\r
+ if (tmp.start[1] <= min[1]) {\r
+ min[1] = tmp.start[1];\r
+ }\r
+\r
+ if (tmp.start[2] <= min[2]) {\r
+ min[2] = tmp.start[2];\r
+ }\r
+\r
+ if (tmp.end[0] >= max[0]) {\r
+ max[0] = tmp.end[0];\r
+ }\r
+\r
+ if (tmp.end[1] >= max[1]) {\r
+ max[1] = tmp.end[1];\r
+ }\r
+\r
+ if (tmp.end[2] >= max[2]) {\r
+ max[2] = tmp.end[2];\r
+ }\r
+\r
+ if (tmp.end[0] <= min[0]) {\r
+ min[0] = tmp.end[0];\r
+ }\r
+\r
+ if (tmp.end[1] <= min[1]) {\r
+ min[1] = tmp.end[1];\r
+ }\r
+\r
+ if (tmp.end[2] <= min[2]) {\r
+ min[2] = tmp.end[2];\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ System.out.println("xmax " + max[0] + " min " + min[0]);\r
+ System.out.println("ymax " + max[1] + " min " + min[1]);\r
+ System.out.println("zmax " + max[2] + " min " + min[2]);\r
+\r
+ width[0] = (float) Math.abs(max[0] - min[0]);\r
+ width[1] = (float) Math.abs(max[1] - min[1]);\r
+ width[2] = (float) Math.abs(max[2] - min[2]);\r
+\r
+ maxwidth = width[0];\r
+\r
+ if (width[1] > width[0]) {\r
+ maxwidth = width[1];\r
+ }\r
+\r
+ if (width[2] > width[1]) {\r
+ maxwidth = width[2];\r
+ }\r
+\r
+ System.out.println("Maxwidth = " + maxwidth);\r
+ }\r
+\r
+ public float findScale() {\r
+ int dim;\r
+ int width;\r
+ int height;\r
+\r
+ if (getWidth() != 0) {\r
+ width = getWidth();\r
+ height = getHeight();\r
+ } else {\r
+ width = prefsize.width;\r
+ height = prefsize.height;\r
+ }\r
+\r
+ if (width < height) {\r
+ dim = width;\r
+ } else {\r
+ dim = height;\r
+ }\r
+\r
+ return (float) (dim / (1.5d * maxwidth));\r
+ }\r
+\r
+ public void findCentre() {\r
+ float xtot = 0;\r
+ float ytot = 0;\r
+ float ztot = 0;\r
+\r
+ int bsize = 0;\r
+\r
+ //Find centre coordinate\r
+ for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
+ if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) {\r
+ Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
+\r
+ bsize += bonds.size();\r
+\r
+ for (int i = 0; i < bonds.size(); i++) {\r
+ xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] +\r
+ ((Bond) bonds.elementAt(i)).end[0];\r
+\r
+ ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] +\r
+ ((Bond) bonds.elementAt(i)).end[1];\r
+\r
+ ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] +\r
+ ((Bond) bonds.elementAt(i)).end[2];\r
+ }\r
+ }\r
+ }\r
+\r
+ centre[0] = xtot / (2 * (float) bsize);\r
+ centre[1] = ytot / (2 * (float) bsize);\r
+ centre[2] = ztot / (2 * (float) bsize);\r
+ }\r
+\r
+ public void paintComponent(Graphics g) {\r
+\r
+ if(pdb==null)\r
+ {\r
+ g.setColor(Color.black);\r
+ g.setFont(new Font("Verdana", Font.BOLD, 14));\r
+ g.drawString("Retrieving PDB data....", 20, getHeight()/2);\r
+\r
+ return;\r
+ }\r
+\r
+\r
+ //Only create the image at the beginning -\r
+ //this saves much memory usage\r
+ if ((img == null) || (prefsize.width != getWidth()) ||\r
+ (prefsize.height != getHeight())) {\r
+ prefsize.width = getWidth();\r
+ prefsize.height = getHeight();\r
+\r
+ scale = findScale();\r
+ img = createImage(prefsize.width, prefsize.height);\r
+ ig = img.getGraphics();\r
+ Graphics2D ig2 = (Graphics2D) ig;\r
+\r
+ ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
+ RenderingHints.VALUE_ANTIALIAS_ON);\r
+\r
+\r
+ redrawneeded = true;\r
+ }\r
+\r
+\r
+ if (redrawneeded)\r
+ {\r
+ drawAll(ig, prefsize.width, prefsize.height);\r
+ redrawneeded = false;\r
+ }\r
+\r
+ g.drawImage(img, 0, 0, this);\r
+ }\r
+\r
+ public void drawAll(Graphics g, int width, int height)\r
+ {\r
+ g.setColor(Color.black);\r
+ g.fillRect(0, 0, width, height);\r
+ drawScene(g);\r
+ drawLabels(g);\r
+ }\r
+\r
+\r
+ public void updateSeqColours()\r
+ {\r
+ if(bysequence)\r
+ {\r
+ for (int ii = 0; ii < pdb.chains.size(); ii++)\r
+ {\r
+ ( (PDBChain) pdb.chains.elementAt(ii)).colourBySequence(sr, fr);\r
+ }\r
+ }\r
+\r
+ redrawneeded=true;\r
+ repaint();\r
+\r
+ }\r
+\r
+ public void drawScene(Graphics g) {\r
+ // Sort the bonds by z coord\r
+ Vector bonds = new Vector();\r
+\r
+ for (int ii = 0; ii < pdb.chains.size(); ii++)\r
+ {\r
+ if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible)\r
+ {\r
+ Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds;\r
+\r
+ for (int i = 0; i < tmp.size(); i++)\r
+ {\r
+ bonds.addElement(tmp.elementAt(i));\r
+ }\r
+ }\r
+ }\r
+\r
+ if (zbuffer) {\r
+ Zsort.Zsort(bonds);\r
+ }\r
+\r
+ for (int i = 0; i < bonds.size(); i++) {\r
+ Bond tmpBond = (Bond) bonds.elementAt(i);\r
+\r
+ xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) +\r
+ (getWidth() / 2));\r
+ ystart = (int) (((tmpBond.start[1] - centre[1]) * scale) +\r
+ (getHeight() / 2));\r
+\r
+ xend = (int) (((tmpBond.end[0] - centre[0]) * scale) +\r
+ (getWidth() / 2));\r
+ yend = (int) (((tmpBond.end[1] - centre[1]) * scale) +\r
+ (getHeight() / 2));\r
+\r
+ xmid = (xend + xstart) / 2;\r
+ ymid = (yend + ystart) / 2;\r
+\r
+ if (depthcue && !bymolecule) {\r
+ if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {\r
+ g.setColor(tmpBond.startCol.darker().darker());\r
+ drawLine(g, xstart, ystart, xmid, ymid);\r
+\r
+ g.setColor(tmpBond.endCol.darker().darker());\r
+ drawLine(g, xmid, ymid, xend, yend);\r
+ } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {\r
+ g.setColor(tmpBond.startCol.darker());\r
+ drawLine(g, xstart, ystart, xmid, ymid);\r
+\r
+ g.setColor(tmpBond.endCol.darker());\r
+ drawLine(g, xmid, ymid, xend, yend);\r
+ } else {\r
+ g.setColor(tmpBond.startCol);\r
+ drawLine(g, xstart, ystart, xmid, ymid);\r
+\r
+ g.setColor(tmpBond.endCol);\r
+ drawLine(g, xmid, ymid, xend, yend);\r
+ }\r
+ } else if (depthcue && bymolecule) {\r
+ if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) {\r
+ g.setColor(Color.green.darker().darker());\r
+ drawLine(g, xstart, ystart, xend, yend);\r
+ } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) {\r
+ g.setColor(Color.green.darker());\r
+ drawLine(g, xstart, ystart, xend, yend);\r
+ } else {\r
+ g.setColor(Color.green);\r
+ drawLine(g, xstart, ystart, xend, yend);\r
+ }\r
+ } else if (!depthcue && !bymolecule) {\r
+ g.setColor(tmpBond.startCol);\r
+ drawLine(g, xstart, ystart, xmid, ymid);\r
+ g.setColor(tmpBond.endCol);\r
+ drawLine(g, xmid, ymid, xend, yend);\r
+ } else {\r
+ drawLine(g, xstart, ystart, xend, yend);\r
+ }\r
+ }\r
+ }\r
+\r
+ public void drawLine(Graphics g, int x1, int y1, int x2, int y2) {\r
+ if (!wire) {\r
+ if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) {\r
+ g.drawLine(x1, y1, x2, y2);\r
+ g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1);\r
+ g.drawLine(x1, y1 - 1, x2, y2 - 1);\r
+ } else {\r
+ g.setColor(g.getColor().brighter());\r
+ g.drawLine(x1, y1, x2, y2);\r
+ g.drawLine(x1 + 1, y1, x2 + 1, y2);\r
+ g.drawLine(x1 - 1, y1, x2 - 1, y2);\r
+ }\r
+ } else {\r
+ g.drawLine(x1, y1, x2, y2);\r
+ }\r
+ }\r
+\r
+ public Dimension minimumsize() {\r
+ return prefsize;\r
+ }\r
+\r
+ public Dimension preferredsize() {\r
+ return prefsize;\r
+ }\r
+\r
+ public void keyPressed(KeyEvent evt) {\r
+ int key = evt.getKeyChar();\r
+ System.out.println(key);\r
+ if (evt.getKeyCode() == KeyEvent.VK_UP)\r
+ {\r
+ scale = (float) (scale * 1.1);\r
+ redrawneeded = true;\r
+ repaint();\r
+ }\r
+ else if (evt.getKeyCode() == KeyEvent.VK_DOWN)\r
+ {\r
+ scale = (float) (scale * 0.9);\r
+ redrawneeded = true;\r
+ repaint();\r
+ }\r
+ }\r
+\r
+ public void mousePressed(MouseEvent e) {\r
+ myAtom fatom = findAtom(e.getX(), e.getY());\r
+ if(fatom!=null)\r
+ {\r
+ fatom.isSelected = !fatom.isSelected;\r
+ redrawneeded = true;\r
+ repaint();\r
+ }\r
+ mx = e.getX();\r
+ my = e.getY();\r
+ omx = mx;\r
+ omy = my;\r
+ dragging = false;\r
+ }\r
+\r
+ public void mouseMoved(MouseEvent e) {\r
+\r
+ myAtom fatom = findAtom(e.getX(), e.getY());\r
+\r
+ if (fatom != null) {\r
+ this.setToolTipText(fatom.resNumber+" "+ fatom.resName);\r
+ } else {\r
+ this.setToolTipText("");\r
+ }\r
+ }\r
+\r
+ public void mouseClicked(MouseEvent e) {\r
+ }\r
+\r
+ public void mouseEntered(MouseEvent e) {\r
+ }\r
+\r
+ public void mouseExited(MouseEvent e) {\r
+ }\r
+\r
+ public void mouseDragged(MouseEvent evt) {\r
+ int x = evt.getX();\r
+ int y = evt.getY();\r
+ mx = x;\r
+ my = y;\r
+\r
+ MCMatrix objmat = new MCMatrix(3, 3);\r
+ objmat.setIdentity();\r
+\r
+ if ((evt.getModifiers() & Event.META_MASK) != 0) {\r
+ objmat.rotatez((float) ((mx - omx)));\r
+ } else {\r
+ objmat.rotatex((float) ((my - omy)));\r
+ objmat.rotatey((float) ((omx - mx)));\r
+ }\r
+\r
+ //Alter the bonds\r
+ for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
+ Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
+\r
+ for (int i = 0; i < bonds.size(); i++) {\r
+ Bond tmpBond = (Bond) bonds.elementAt(i);\r
+\r
+ //Translate the bond so the centre is 0,0,0\r
+ tmpBond.translate(-centre[0], -centre[1], -centre[2]);\r
+\r
+ //Now apply the rotation matrix\r
+ tmpBond.start = objmat.vectorMultiply(tmpBond.start);\r
+ tmpBond.end = objmat.vectorMultiply(tmpBond.end);\r
+\r
+ //Now translate back again\r
+ tmpBond.translate(centre[0], centre[1], centre[2]);\r
+ }\r
+ }\r
+\r
+ objmat = null;\r
+\r
+ omx = mx;\r
+ omy = my;\r
+\r
+ dragging = true;\r
+\r
+ redrawneeded = true;\r
+\r
+ repaint();\r
+ }\r
+\r
+ public void mouseReleased(MouseEvent evt) {\r
+ dragging = false;\r
+ return;\r
+ }\r
+\r
+ void drawLabels(Graphics g) {\r
+\r
+ for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
+ PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);\r
+\r
+ if (chain.isVisible) {\r
+ Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
+\r
+ for (int i = 0; i < bonds.size(); i++) {\r
+ Bond tmpBond = (Bond) bonds.elementAt(i);\r
+\r
+ if (tmpBond.at1.isSelected) {\r
+ labelAtom(g, tmpBond, 1);\r
+ }\r
+\r
+ if (tmpBond.at2.isSelected) {\r
+ labelAtom(g, tmpBond, 2);\r
+ }\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ public void labelAtom(Graphics g, Bond b, int n) {\r
+ g.setFont(font);\r
+\r
+ if (n == 1) {\r
+ int xstart = (int) (((b.start[0] - centre[0]) * scale) +\r
+ (getWidth() / 2));\r
+ int ystart = (int) (((b.start[1] - centre[1]) * scale) +\r
+ (getHeight() / 2));\r
+\r
+ g.setColor(Color.red);\r
+ g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart);\r
+ }\r
+\r
+ if (n == 2) {\r
+ int xstart = (int) (((b.end[0] - centre[0]) * scale) +\r
+ (getWidth() / 2));\r
+ int ystart = (int) (((b.end[1] - centre[1]) * scale) +\r
+ (getHeight() / 2));\r
+\r
+ g.setColor(Color.red);\r
+ g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart);\r
+ }\r
+ }\r
+\r
+ public myAtom findAtom(int x, int y) {\r
+ myAtom fatom = null;\r
+\r
+ int foundchain = -1;\r
+\r
+ for (int ii = 0; ii < pdb.chains.size(); ii++) {\r
+ PDBChain chain = (PDBChain) pdb.chains.elementAt(ii);\r
+\r
+ if (chain.isVisible) {\r
+ Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds;\r
+\r
+ for (int i = 0; i < bonds.size(); i++) {\r
+ Bond tmpBond = (Bond) bonds.elementAt(i);\r
+\r
+ int truex = (int) (((tmpBond.start[0] - centre[0]) * scale) +\r
+ (getWidth() / 2));\r
+\r
+ if (Math.abs(truex - x) <= 2) {\r
+ int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) +\r
+ (getHeight() / 2));\r
+\r
+ if (Math.abs(truey - y) <= 2)\r
+ {\r
+ fatom = tmpBond.at1;\r
+ foundchain = ii;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ if (fatom != null) //)&& chain.ds != null)\r
+ {\r
+ chain = (PDBChain) pdb.chains.elementAt(foundchain);\r
+\r
+ // SMJS TODO\r
+ // int tmp = chain.ds.seqstart + fatom.resNumber - chain.offset;\r
+ // int pos = chain.ds.findIndex(tmp);\r
+ // System.out.println("Found seq " + chain.ds.name + " " + tmp + " " + pos);\r
+ }\r
+ }\r
+\r
+ return fatom;\r
+ }\r
+\r
+ public void update(Graphics g) {\r
+ paint(g);\r
+ }\r
+}\r
--- /dev/null
+package MCview;\r
+\r
+import javax.swing.*;\r
+import java.awt.event.*;\r
+import jalview.datamodel.*;\r
+import jalview.gui.*;\r
+import jalview.io.EBIFetchClient;\r
+import java.awt.event.ActionListener;\r
+import java.awt.event.ActionEvent;\r
+import java.io.FileOutputStream;\r
+import org.jibble.epsgraphics.EpsGraphics2D;\r
+import javax.imageio.ImageIO;\r
+import java.awt.image.BufferedImage;\r
+import java.awt.Graphics;\r
+import java.awt.Graphics2D;\r
+import java.awt.RenderingHints;\r
+\r
+public class PDBViewer extends JInternalFrame implements Runnable\r
+{\r
+ PDBEntry pdb;\r
+ Sequence sequence;\r
+ SeqCanvas seqcanvas;\r
+ PDBCanvas pdbcanvas;\r
+\r
+\r
+ public PDBViewer(PDBEntry entry,\r
+ Sequence seq,\r
+ SeqCanvas seqcanvas)\r
+ {\r
+ pdb = entry;\r
+ sequence = seq;\r
+ this.seqcanvas = seqcanvas;\r
+\r
+\r
+ try\r
+ {\r
+ jbInit();\r
+ }\r
+ catch (Exception ex)\r
+ {\r
+ ex.printStackTrace();\r
+ }\r
+\r
+ pdbcanvas = new PDBCanvas();\r
+ setContentPane(pdbcanvas);\r
+ Desktop.addInternalFrame(this,\r
+ sequence.getName() + ":" + pdb.getId()\r
+ + " Method: " + pdb.getProperty().get("method")\r
+ + " Chain:" + pdb.getProperty().get("chains"),\r
+ 400, 400);\r
+\r
+ Thread worker = new Thread(this);\r
+ worker.start();\r
+ }\r
+\r
+ public void run()\r
+ {\r
+ try\r
+ {\r
+ EBIFetchClient ebi = new EBIFetchClient();\r
+ String[] result = ebi.fetchData("pdb:" + pdb.getId(), null,\r
+ null);\r
+\r
+ PDBfile pdbfile = new PDBfile(result);\r
+\r
+ pdbcanvas.setPDBFile(pdbfile,\r
+ sequence,\r
+ seqcanvas.getSequenceRenderer(),\r
+ seqcanvas.getFeatureRenderer());\r
+\r
+ seqcanvas.setPDBViewer(pdbcanvas);\r
+ }\r
+ catch (Exception ex)\r
+ {\r
+ ex.printStackTrace();\r
+ }\r
+ }\r
+\r
+ private void jbInit()\r
+ throws Exception\r
+ {\r
+ this.addKeyListener(new KeyAdapter()\r
+ {\r
+ public void keyPressed(KeyEvent evt)\r
+ {\r
+ System.out.println("key press");\r
+ pdbcanvas.keyPressed(evt);\r
+ }\r
+ });\r
+\r
+ this.setJMenuBar(jMenuBar1);\r
+ fileMenu.setText("File");\r
+ coloursMenu.setText("Colours");\r
+ saveMenu.setActionCommand("Save Image");\r
+ saveMenu.setText("Save As");\r
+ png.setText("PNG");\r
+ png.addActionListener(new ActionListener()\r
+ {\r
+ public void actionPerformed(ActionEvent e)\r
+ {\r
+ png_actionPerformed(e);\r
+ }\r
+ });\r
+ eps.setText("EPS");\r
+ eps.addActionListener(new ActionListener()\r
+ {\r
+ public void actionPerformed(ActionEvent e)\r
+ {\r
+ eps_actionPerformed(e);\r
+ }\r
+ });\r
+ mapping.setText("View Mapping");\r
+ wire.setText("Wireframe");\r
+ wire.addActionListener(new ActionListener()\r
+ {\r
+ public void actionPerformed(ActionEvent e)\r
+ {\r
+ wire_actionPerformed(e);\r
+ }\r
+ });\r
+ depth.setSelected(true);\r
+ depth.setText("Depthcue");\r
+ depth.addActionListener(new ActionListener()\r
+ {\r
+ public void actionPerformed(ActionEvent e)\r
+ {\r
+ depth_actionPerformed(e);\r
+ }\r
+ });\r
+ zbuffer.setSelected(true);\r
+ zbuffer.setText("Z Buffering");\r
+ zbuffer.addActionListener(new ActionListener()\r
+ {\r
+ public void actionPerformed(ActionEvent e)\r
+ {\r
+ zbuffer_actionPerformed(e);\r
+ }\r
+ });\r
+ charge.setText("Charge & Cysteine");\r
+ charge.addActionListener(new ActionListener()\r
+ {\r
+ public void actionPerformed(ActionEvent e)\r
+ {\r
+ charge_actionPerformed(e);\r
+ }\r
+ });\r
+ hydro.setText("Hydrophobicity");\r
+ hydro.addActionListener(new ActionListener()\r
+ {\r
+ public void actionPerformed(ActionEvent e)\r
+ {\r
+ hydro_actionPerformed(e);\r
+ }\r
+ });\r
+ chain.setText("By Chain");\r
+ chain.addActionListener(new ActionListener()\r
+ {\r
+ public void actionPerformed(ActionEvent e)\r
+ {\r
+ chain_actionPerformed(e);\r
+ }\r
+ });\r
+ seqButton.setSelected(true);\r
+ seqButton.setText("By Sequence");\r
+ seqButton.addActionListener(new ActionListener()\r
+ {\r
+ public void actionPerformed(ActionEvent e)\r
+ {\r
+ seqButton_actionPerformed(e);\r
+ }\r
+ });\r
+ molecule.setText("By Molecule");\r
+ molecule.addActionListener(new ActionListener()\r
+ {\r
+ public void actionPerformed(ActionEvent e)\r
+ {\r
+ molecule_actionPerformed(e);\r
+ }\r
+ });\r
+ jMenuBar1.add(fileMenu);\r
+ jMenuBar1.add(coloursMenu);\r
+ fileMenu.add(saveMenu);\r
+ fileMenu.add(mapping);\r
+ saveMenu.add(png);\r
+ saveMenu.add(eps);\r
+ coloursMenu.add(seqButton);\r
+ coloursMenu.add(chain);\r
+ coloursMenu.add(hydro);\r
+ coloursMenu.add(charge);\r
+ coloursMenu.addSeparator();\r
+ coloursMenu.add(wire);\r
+ coloursMenu.add(depth);\r
+ coloursMenu.add(zbuffer);\r
+ coloursMenu.add(molecule);\r
+ ButtonGroup bg = new ButtonGroup();\r
+ bg.add(seqButton);\r
+ bg.add(chain);\r
+ bg.add(hydro);\r
+ bg.add(charge);\r
+ }\r
+\r
+ JMenuBar jMenuBar1 = new JMenuBar();\r
+ JMenu fileMenu = new JMenu();\r
+ JMenu coloursMenu = new JMenu();\r
+ JMenu saveMenu = new JMenu();\r
+ JMenuItem png = new JMenuItem();\r
+ JMenuItem eps = new JMenuItem();\r
+ JMenuItem mapping = new JMenuItem();\r
+ JCheckBoxMenuItem wire = new JCheckBoxMenuItem();\r
+ JCheckBoxMenuItem depth = new JCheckBoxMenuItem();\r
+ JCheckBoxMenuItem zbuffer = new JCheckBoxMenuItem();\r
+ JRadioButtonMenuItem charge = new JRadioButtonMenuItem();\r
+ JRadioButtonMenuItem hydro = new JRadioButtonMenuItem();\r
+ JRadioButtonMenuItem chain = new JRadioButtonMenuItem();\r
+ JRadioButtonMenuItem seqButton = new JRadioButtonMenuItem();\r
+ JCheckBoxMenuItem molecule = new JCheckBoxMenuItem();\r
+\r
+ public void png_actionPerformed(ActionEvent e)\r
+ {\r
+ int width = pdbcanvas.getWidth();\r
+ int height = pdbcanvas.getHeight();\r
+\r
+ try\r
+ {\r
+ jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(\r
+ "LAST_DIRECTORY"), new String[] { "png" },\r
+ new String[] { "Portable network graphics" },\r
+ "Portable network graphics");\r
+\r
+ chooser.setFileView(new jalview.io.JalviewFileView());\r
+ chooser.setDialogTitle("Create PNG Image of Molecule");\r
+ chooser.setToolTipText("Save");\r
+\r
+ int value = chooser.showSaveDialog(this);\r
+\r
+ if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)\r
+ {\r
+ return;\r
+ }\r
+\r
+ jalview.bin.Cache.setProperty("LAST_DIRECTORY",\r
+ chooser.getSelectedFile().getParent());\r
+\r
+ FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());\r
+\r
+ BufferedImage bi = new BufferedImage(width, height,\r
+ BufferedImage.TYPE_INT_RGB);\r
+ Graphics png = bi.getGraphics();\r
+ Graphics2D ig2 = (Graphics2D) png;\r
+ ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
+ RenderingHints.VALUE_ANTIALIAS_ON);\r
+\r
+\r
+ pdbcanvas.drawAll(png, width, height);\r
+\r
+ ImageIO.write(bi, "png", out);\r
+ out.close();\r
+ }\r
+ catch (Exception ex)\r
+ {\r
+ ex.printStackTrace();\r
+ }\r
+ }\r
+\r
+ public void eps_actionPerformed(ActionEvent e)\r
+ {\r
+ boolean accurateText = true;\r
+\r
+ String renderStyle = jalview.bin.Cache.getDefault("EPS_RENDERING",\r
+ "Prompt each time");\r
+\r
+ // If we need to prompt, and if the GUI is visible then\r
+ // Prompt for EPS rendering style\r
+ if (renderStyle.equalsIgnoreCase("Prompt each time")\r
+ && !\r
+ (System.getProperty("java.awt.headless") != null\r
+ && System.getProperty("java.awt.headless").equals("true")))\r
+ {\r
+ EPSOptions eps = new EPSOptions();\r
+ renderStyle = eps.getValue();\r
+\r
+ if (renderStyle==null || eps.cancelled)\r
+ return;\r
+\r
+\r
+ }\r
+\r
+ if (renderStyle.equalsIgnoreCase("text"))\r
+ {\r
+ accurateText = false;\r
+ }\r
+\r
+ int width = getWidth();\r
+ int height = getHeight();\r
+\r
+ try\r
+ {\r
+ jalview.io.JalviewFileChooser chooser = new jalview.io.JalviewFileChooser(jalview.bin.Cache.getProperty(\r
+ "LAST_DIRECTORY"), new String[] { "eps" },\r
+ new String[] { "Encapsulated Postscript" },\r
+ "Encapsulated Postscript");\r
+ chooser.setFileView(new jalview.io.JalviewFileView());\r
+ chooser.setDialogTitle("Create EPS file from tree");\r
+ chooser.setToolTipText("Save");\r
+\r
+ int value = chooser.showSaveDialog(this);\r
+\r
+ if (value != jalview.io.JalviewFileChooser.APPROVE_OPTION)\r
+ {\r
+ return;\r
+ }\r
+\r
+ jalview.bin.Cache.setProperty("LAST_DIRECTORY",\r
+ chooser.getSelectedFile().getParent());\r
+\r
+ FileOutputStream out = new FileOutputStream(chooser.getSelectedFile());\r
+ EpsGraphics2D pg = new EpsGraphics2D("Tree", out, 0, 0, width,\r
+ height);\r
+ Graphics2D ig2 = (Graphics2D) pg;\r
+ ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,\r
+ RenderingHints.VALUE_ANTIALIAS_ON);\r
+\r
+\r
+ pg.setAccurateTextMode(accurateText);\r
+\r
+ pdbcanvas.drawAll(pg, width, height);\r
+\r
+ pg.flush();\r
+ pg.close();\r
+ }\r
+ catch (Exception ex)\r
+ {\r
+ ex.printStackTrace();\r
+ }\r
+\r
+ }\r
+\r
+ public void charge_actionPerformed(ActionEvent e)\r
+ {\r
+ pdbcanvas.pdb.setChargeColours();\r
+ pdbcanvas.redrawneeded=true;\r
+ pdbcanvas.repaint();\r
+ }\r
+\r
+ public void hydro_actionPerformed(ActionEvent e)\r
+ {\r
+ pdbcanvas.pdb.setHydrophobicityColours();\r
+ pdbcanvas.redrawneeded=true;\r
+ pdbcanvas.repaint();\r
+ }\r
+\r
+ public void chain_actionPerformed(ActionEvent e)\r
+ {\r
+ pdbcanvas.pdb.setChainColours();\r
+ pdbcanvas.redrawneeded=true;\r
+ pdbcanvas.repaint();\r
+ }\r
+\r
+ public void zbuffer_actionPerformed(ActionEvent e)\r
+ {\r
+ pdbcanvas.zbuffer = ! pdbcanvas.zbuffer;\r
+ pdbcanvas.redrawneeded=true;\r
+ pdbcanvas.repaint();\r
+ }\r
+\r
+ public void molecule_actionPerformed(ActionEvent e)\r
+ {\r
+ pdbcanvas.bymolecule = ! pdbcanvas.bymolecule;\r
+ pdbcanvas.redrawneeded=true;\r
+ pdbcanvas.repaint();\r
+ }\r
+\r
+ public void depth_actionPerformed(ActionEvent e)\r
+ {\r
+ pdbcanvas.depthcue = ! pdbcanvas.depthcue;\r
+ pdbcanvas.redrawneeded=true;\r
+ pdbcanvas.repaint();\r
+ }\r
+\r
+ public void wire_actionPerformed(ActionEvent e)\r
+ {\r
+ pdbcanvas.wire = ! pdbcanvas.wire;\r
+ pdbcanvas.redrawneeded=true;\r
+ pdbcanvas.repaint();\r
+ }\r
+\r
+ public void seqButton_actionPerformed(ActionEvent e)\r
+ {\r
+ pdbcanvas.bysequence = seqButton.isSelected();\r
+ pdbcanvas.updateSeqColours();\r
+ }\r
+\r
+ void clearButtonGroup()\r
+ {\r
+ charge.setSelected(false);\r
+ hydro.setSelected(false);\r
+ chain.setSelected(false);\r
+ seqButton.setSelected(false);\r
+ }\r
+}\r