From 8893c2ac9824d4c566ae69c73d34889c5316b058 Mon Sep 17 00:00:00 2001 From: amwaterhouse Date: Tue, 6 Dec 2005 11:40:04 +0000 Subject: [PATCH] PDB structure viewer updated --- src/MCview/AppletPDBCanvas.java | 984 ++++++++++++++++++++++++++++++++++++++ src/MCview/AppletPDBViewer.java | 237 ++++++++++ src/MCview/Atom.java | 42 +- src/MCview/Bond.java | 12 +- src/MCview/MCMatrix.java | 9 - src/MCview/PDBCanvas.java | 986 +++++++++++++++++++++++++++++++++++++++ src/MCview/PDBChain.java | 151 +++--- src/MCview/PDBViewer.java | 349 ++++++++++++++ src/MCview/PDBfile.java | 122 +++-- src/MCview/Residue.java | 11 +- src/MCview/myAtom.java | 71 --- src/MCview/rotCanvas.java | 730 ----------------------------- 12 files changed, 2730 insertions(+), 974 deletions(-) create mode 100755 src/MCview/AppletPDBCanvas.java create mode 100755 src/MCview/AppletPDBViewer.java create mode 100755 src/MCview/PDBCanvas.java create mode 100755 src/MCview/PDBViewer.java delete mode 100755 src/MCview/myAtom.java delete mode 100755 src/MCview/rotCanvas.java diff --git a/src/MCview/AppletPDBCanvas.java b/src/MCview/AppletPDBCanvas.java new file mode 100755 index 0000000..d6247f4 --- /dev/null +++ b/src/MCview/AppletPDBCanvas.java @@ -0,0 +1,984 @@ +/* +* 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 MCview; + +import jalview.analysis.AlignSeq; + +import jalview.datamodel.*; + +// JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug +import java.awt.*; +import java.awt.event.*; + +import java.io.*; + +import java.util.*; + + + +public class AppletPDBCanvas extends Panel implements MouseListener, MouseMotionListener +{ + + MCMatrix idmat = new MCMatrix(3, 3); + MCMatrix objmat = new MCMatrix(3, 3); + boolean redrawneeded = true; + int omx = 0; + int mx = 0; + int omy = 0; + int my = 0; + public PDBfile pdb; + int bsize; + Image img; + Graphics ig; + Dimension prefsize; + float[] centre = new float[3]; + float[] width = new float[3]; + float maxwidth; + float scale; + String inStr; + String inType; + boolean bysequence = true; + boolean depthcue = true; + boolean wire = false; + boolean bymolecule = false; + boolean zbuffer = true; + boolean dragging; + int xstart; + int xend; + int ystart; + int yend; + int xmid; + int ymid; + Font font = new Font("Helvetica", Font.PLAIN, 10); + jalview.appletgui.SeqCanvas seqcanvas; + public Sequence sequence; + final StringBuffer mappingDetails = new StringBuffer(); + String appletToolTip = null; + int toolx, tooly; + PDBChain mainchain; + Vector highlightRes; + boolean pdbAction = false; + Bond highlightBond1, highlightBond2; + boolean errorLoading = false; + + public AppletPDBCanvas(jalview.appletgui.SeqCanvas seqcanvas, Sequence seq) + { + this.seqcanvas = seqcanvas; + this.sequence = seq; + + seqcanvas.setPDBCanvas(this); + addKeyListener(new KeyAdapter() + { + + public void keyPressed(KeyEvent evt) + { + doKeyPressed(evt); + } + }); + } + + + public void setPDBFile(PDBfile pdb) + { + int max = -10; + int maxchain = -1; + int pdbstart = 0; + int pdbend = 0; + int seqstart = 0; + int seqend = 0; + AlignSeq maxAlignseq = null;; + + for (int i = 0; i < pdb.chains.size(); i++) + { + + mappingDetails.append("\n\nPDB Sequence is :\nSequence = " + ((PDBChain) pdb.chains.elementAt(i)).sequence.getSequence()); + mappingDetails.append("\nNo of residues = " + ((PDBChain) pdb.chains.elementAt(i)).residues.size()+"\n\n"); + + // Now lets compare the sequences to get + // the start and end points. + // Align the sequence to the pdb + AlignSeq as = new AlignSeq(sequence, + ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep"); + as.calcScoreMatrix(); + as.traceAlignment(); + PrintStream ps = new PrintStream(System.out) + { + public void print(String x) { + mappingDetails.append(x); + } + public void println() + { + mappingDetails.append("\n"); + } + }; + + as.printAlignment(ps); + + if (as.maxscore > max) { + max = as.maxscore; + maxchain = i; + + pdbstart = as.seq2start; + pdbend = as.seq2end; + seqstart = as.seq1start + sequence.getStart()-1; + seqend = as.seq1end + sequence.getEnd()-1; + maxAlignseq = as; + } + + mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend); + mappingDetails.append("\nSEQ start/end "+ seqstart + " " + seqend); + } + + mainchain = (PDBChain) pdb.chains.elementAt(maxchain); + + mainchain.pdbstart = pdbstart; + mainchain.pdbend = pdbend; + mainchain.seqstart = seqstart; + mainchain.seqend = seqend; + mainchain.isVisible = true; + mainchain.makeExactMapping(maxAlignseq, sequence); + + this.pdb = pdb; + this.prefsize = new Dimension(getSize().width, getSize().height); + + //Initialize the matrices to identity + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (i != j) { + idmat.addElement(i, j, 0); + objmat.addElement(i, j, 0); + } else { + idmat.addElement(i, j, 1); + objmat.addElement(i, j, 1); + } + } + } + + addMouseMotionListener(this); + addMouseListener(this); + + + findCentre(); + findWidth(); + + setupBonds(); + + scale = findScale(); + } + + + Vector visiblebonds; + void setupBonds() + { + // Sort the bonds by z coord + visiblebonds = new Vector(); + + for (int ii = 0; ii < pdb.chains.size(); ii++) + { + if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible) + { + Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds; + + for (int i = 0; i < tmp.size(); i++) + { + visiblebonds.addElement(tmp.elementAt(i)); + } + } + } + updateSeqColours(); + redrawneeded = true; + repaint(); + } + + + public void findWidth() { + float[] max = new float[3]; + float[] min = new float[3]; + + max[0] = (float) -1e30; + max[1] = (float) -1e30; + max[2] = (float) -1e30; + + min[0] = (float) 1e30; + min[1] = (float) 1e30; + min[2] = (float) 1e30; + + for (int ii = 0; ii < pdb.chains.size(); ii++) { + if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) { + Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; + + for (int i = 0; i < bonds.size(); i++) { + Bond tmp = (Bond) bonds.elementAt(i); + + if (tmp.start[0] >= max[0]) { + max[0] = tmp.start[0]; + } + + if (tmp.start[1] >= max[1]) { + max[1] = tmp.start[1]; + } + + if (tmp.start[2] >= max[2]) { + max[2] = tmp.start[2]; + } + + if (tmp.start[0] <= min[0]) { + min[0] = tmp.start[0]; + } + + if (tmp.start[1] <= min[1]) { + min[1] = tmp.start[1]; + } + + if (tmp.start[2] <= min[2]) { + min[2] = tmp.start[2]; + } + + if (tmp.end[0] >= max[0]) { + max[0] = tmp.end[0]; + } + + if (tmp.end[1] >= max[1]) { + max[1] = tmp.end[1]; + } + + if (tmp.end[2] >= max[2]) { + max[2] = tmp.end[2]; + } + + if (tmp.end[0] <= min[0]) { + min[0] = tmp.end[0]; + } + + if (tmp.end[1] <= min[1]) { + min[1] = tmp.end[1]; + } + + if (tmp.end[2] <= min[2]) { + min[2] = tmp.end[2]; + } + } + } + } + + width[0] = (float) Math.abs(max[0] - min[0]); + width[1] = (float) Math.abs(max[1] - min[1]); + width[2] = (float) Math.abs(max[2] - min[2]); + + maxwidth = width[0]; + + if (width[1] > width[0]) { + maxwidth = width[1]; + } + + if (width[2] > width[1]) { + maxwidth = width[2]; + } + + // System.out.println("Maxwidth = " + maxwidth); + } + + public float findScale() { + int dim; + int width; + int height; + + if (getSize().width != 0) { + width = getSize().width; + height = getSize().height; + } else { + width = prefsize.width; + height = prefsize.height; + } + + if (width < height) { + dim = width; + } else { + dim = height; + } + + return (float) (dim / (1.5d * maxwidth)); + } + + public void findCentre() { + float xtot = 0; + float ytot = 0; + float ztot = 0; + + int bsize = 0; + + //Find centre coordinate + for (int ii = 0; ii < pdb.chains.size(); ii++) { + if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) { + Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; + + bsize += bonds.size(); + + for (int i = 0; i < bonds.size(); i++) { + xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] + + ((Bond) bonds.elementAt(i)).end[0]; + + ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] + + ((Bond) bonds.elementAt(i)).end[1]; + + ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] + + ((Bond) bonds.elementAt(i)).end[2]; + } + } + } + + centre[0] = xtot / (2 * (float) bsize); + centre[1] = ytot / (2 * (float) bsize); + centre[2] = ztot / (2 * (float) bsize); + } + + public void paint(Graphics g) + { + + if(errorLoading) + { + g.setColor(Color.white); + g.fillRect(0,0,getSize().width, getSize().height); + g.setColor(Color.black); + g.setFont(new Font("Verdana", Font.BOLD, 14)); + g.drawString("Error loading PDB data!!", 50, getSize().height/2); + return; + } + else if(visiblebonds==null) + { + g.setColor(Color.black); + g.setFont(new Font("Verdana", Font.BOLD, 14)); + g.drawString("Fetching PDB data...", 50, getSize().height/2); + return; + } + + + + //Only create the image at the beginning - + //this saves much memory usage + if ((img == null) || (prefsize.width != getSize().width) || + (prefsize.height != getSize().height)) { + + try{ prefsize.width = getSize().width; + prefsize.height = getSize().height; + + scale = findScale(); + img = createImage(prefsize.width, prefsize.height); + ig = img.getGraphics(); + + redrawneeded = true; + }catch(Exception ex) + { + ex.printStackTrace(); + } + } + + + if (redrawneeded) + { + drawAll(ig, prefsize.width, prefsize.height); + redrawneeded = false; + } + if(appletToolTip!=null) + { + ig.setColor(Color.red); + ig.drawString(appletToolTip, toolx, tooly); + } + + g.drawImage(img, 0, 0, this); + + pdbAction = false; + } + + public void drawAll(Graphics g, int width, int height) + { + ig.setColor(Color.black); + ig.fillRect(0, 0, width, height); + drawScene(ig); + drawLabels(ig); + } + + + public void updateSeqColours() + { + if (pdbAction) + { + return; + } + + if(bysequence && pdb!=null) + { + for (int ii = 0; ii < pdb.chains.size(); ii++) + { + colourBySequence((PDBChain) pdb.chains.elementAt(ii)); + } + } + + redrawneeded=true; + repaint(); + } + + + int findTrueIndex(int pos) + { + // returns the alignment position for a residue + int j = sequence.getStart(); + int i = 0; + + while ( (i < sequence.getLength()) && (j <= sequence.getEnd()) && (j <= pos+1)) + { + if (!jalview.util.Comparison.isGap(sequence.getCharAt(i))) + { + j++; + } + + i++; + } + + if(i>1) + i--; + + if ( (j == sequence.getEnd()) && (j < pos)) + { + return sequence.getEnd() + 1; + } + else + { + return i; + } + } + + + // This method has been taken out of PDBChain to allow + // Applet and Application specific sequence renderers to be used + void colourBySequence(PDBChain chain) + { + for (int i = 0; i < chain.bonds.size(); i++) + { + Bond tmp = (Bond) chain.bonds.elementAt(i); + tmp.startCol = Color.lightGray; + tmp.endCol = Color.lightGray; + if(chain!=mainchain) + continue; + + if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) && + (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1))) + { + + int index = findTrueIndex(tmp.at1.alignmentMapping); + //sequence.findIndex(tmp.at1.alignmentMapping); + if (index != -1) + { + tmp.startCol = seqcanvas.getSequenceRenderer(). + getResidueBoxColour( sequence, index); + + // tmp.startCol = seqcanvas.getFeatureRenderer(). + // findFeatureColour(tmp.startCol, sequence, index); + } + } + + int index = findTrueIndex(tmp.at2.alignmentMapping); + //sequence.findIndex( tmp.at2.alignmentMapping ); + if (index != -1) + { + tmp.endCol = seqcanvas.getSequenceRenderer(). + getResidueBoxColour( sequence, index); + // tmp.endCol = seqcanvas.getFeatureRenderer(). + // findFeatureColour(tmp.endCol, sequence, index); + } + } + } + + + public void drawScene(Graphics g) + { + + if (zbuffer) { + Zsort.Zsort(visiblebonds); + } + + + Bond tmpBond=null; + for (int i = 0; i < visiblebonds.size(); i++) + { + tmpBond = (Bond) visiblebonds.elementAt(i); + + + xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + + (getSize().width / 2)); + ystart = (int) (((tmpBond.start[1] - centre[1]) * scale) + + (getSize().height / 2)); + + xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + + (getSize().width / 2)); + yend = (int) (((tmpBond.end[1] - centre[1]) * scale) + + (getSize().height / 2)); + + xmid = (xend + xstart) / 2; + ymid = (yend + ystart) / 2; + + if (depthcue && !bymolecule) + { + if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) { + g.setColor(tmpBond.startCol.darker().darker()); + drawLine(g, xstart, ystart, xmid, ymid); + + g.setColor(tmpBond.endCol.darker().darker()); + drawLine(g, xmid, ymid, xend, yend); + } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) { + g.setColor(tmpBond.startCol.darker()); + drawLine(g, xstart, ystart, xmid, ymid); + + g.setColor(tmpBond.endCol.darker()); + drawLine(g, xmid, ymid, xend, yend); + } else { + g.setColor(tmpBond.startCol); + drawLine(g, xstart, ystart, xmid, ymid); + + g.setColor(tmpBond.endCol); + drawLine(g, xmid, ymid, xend, yend); + } + + } else if (depthcue && bymolecule) { + if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) { + g.setColor(Color.green.darker().darker()); + drawLine(g, xstart, ystart, xend, yend); + } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) { + g.setColor(Color.green.darker()); + drawLine(g, xstart, ystart, xend, yend); + } else { + g.setColor(Color.green); + drawLine(g, xstart, ystart, xend, yend); + } + } else if (!depthcue && !bymolecule) { + g.setColor(tmpBond.startCol); + drawLine(g, xstart, ystart, xmid, ymid); + g.setColor(tmpBond.endCol); + drawLine(g, xmid, ymid, xend, yend); + } else { + drawLine(g, xstart, ystart, xend, yend); + } + + if(highlightBond1!=null && highlightBond1==tmpBond) + { + g.setColor(Color.white); + drawLine(g, xmid, ymid, xend, yend); + } + + if(highlightBond2!=null && highlightBond2==tmpBond) + { + g.setColor(Color.white); + drawLine(g, xstart, ystart, xmid, ymid); + } + + } + } + + public void drawLine(Graphics g, int x1, int y1, int x2, int y2) { + if (!wire) { + if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) { + g.drawLine(x1, y1, x2, y2); + g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1); + g.drawLine(x1, y1 - 1, x2, y2 - 1); + } else { + g.setColor(g.getColor().brighter()); + g.drawLine(x1, y1, x2, y2); + g.drawLine(x1 + 1, y1, x2 + 1, y2); + g.drawLine(x1 - 1, y1, x2 - 1, y2); + } + } else { + g.drawLine(x1, y1, x2, y2); + } + } + + public Dimension minimumsize() { + return prefsize; + } + + public Dimension preferredsize() { + return prefsize; + } + + public void doKeyPressed(KeyEvent evt) + { + if (evt.getKeyCode() == KeyEvent.VK_UP) + { + scale = (float) (scale * 1.1); + redrawneeded = true; + repaint(); + } + else if (evt.getKeyCode() == KeyEvent.VK_DOWN) + { + scale = (float) (scale * 0.9); + redrawneeded = true; + repaint(); + } + } + + public void mousePressed(MouseEvent e) { + pdbAction = true; + Atom fatom = findAtom(e.getX(), e.getY()); + if(fatom!=null) + { + fatom.isSelected = !fatom.isSelected; + + redrawneeded = true; + repaint(); + if (foundchain != -1) + { + PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain); + if (chain == mainchain) + { + if (fatom.alignmentMapping != -1) + { + if (highlightRes == null) + highlightRes = new Vector(); + + if (highlightRes.contains(fatom.alignmentMapping+"" + "")) + highlightRes.removeElement(fatom.alignmentMapping + ""); + else + highlightRes.addElement(fatom.alignmentMapping + ""); + } + } + } + + } + mx = e.getX(); + my = e.getY(); + omx = mx; + omy = my; + dragging = false; + } + + public void mouseMoved(MouseEvent e) { + pdbAction = true; + if(highlightBond1!=null) + { + highlightBond1.at2.isSelected = false; + highlightBond2.at1.isSelected = false; + highlightBond1 = null; + highlightBond2 = null; + } + + Atom fatom = findAtom(e.getX(), e.getY()); + + PDBChain chain = null; + if(foundchain!=-1) + { + chain = (PDBChain) pdb.chains.elementAt(foundchain); + if(chain == mainchain) + { + highlightSeqcanvas( fatom.alignmentMapping ); + } + } + + if (fatom != null) { + toolx = e.getX(); + tooly = e.getY(); + + appletToolTip = chain.id+":"+ fatom.resNumber+" "+ fatom.resName; + redrawneeded = true; + repaint(); + } else { + highlightSeqcanvas( -1); + appletToolTip = null; + redrawneeded = true; + repaint(); + } + } + + + void highlightSeqcanvas(int pos) + { + int index = seqcanvas.getViewport().getAlignment().findIndex(sequence); + + int size = pos==-1?0:3; + + if(highlightRes!=null) + size += highlightRes.size()*3; + + int [] array = new int[size]; + int i=0; + if(highlightRes!=null) + { + for (i = 0; i < highlightRes.size(); i++) + { + int a = Integer.parseInt(highlightRes.elementAt( + i).toString())+1; + array[i * 3] = index; + array[ (i * 3) + 1] = a; + array[ (i * 3) + 2] = a; + } + } + + if(pos!=-1) + { + array[i * 3] = index; + array[i * 3 + 1] = pos+1; + array[i * 3 + 2] = pos+1; + } + + seqcanvas.highlightSearchResults(array); + } + + + public void mouseClicked(MouseEvent e) { + } + + public void mouseEntered(MouseEvent e) { + } + + public void mouseExited(MouseEvent e) { + } + + public void mouseDragged(MouseEvent evt) { + int x = evt.getX(); + int y = evt.getY(); + mx = x; + my = y; + + MCMatrix objmat = new MCMatrix(3, 3); + objmat.setIdentity(); + + if ((evt.getModifiers() & Event.META_MASK) != 0) { + objmat.rotatez((float) ((mx - omx))); + } else { + objmat.rotatex((float) ((my - omy))); + objmat.rotatey((float) ((omx - mx))); + } + + //Alter the bonds + for (int ii = 0; ii < pdb.chains.size(); ii++) { + Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; + + for (int i = 0; i < bonds.size(); i++) { + Bond tmpBond = (Bond) bonds.elementAt(i); + + //Translate the bond so the centre is 0,0,0 + tmpBond.translate(-centre[0], -centre[1], -centre[2]); + + //Now apply the rotation matrix + tmpBond.start = objmat.vectorMultiply(tmpBond.start); + tmpBond.end = objmat.vectorMultiply(tmpBond.end); + + //Now translate back again + tmpBond.translate(centre[0], centre[1], centre[2]); + } + } + + objmat = null; + + omx = mx; + omy = my; + + dragging = true; + + redrawneeded = true; + + repaint(); + } + + public void mouseReleased(MouseEvent evt) { + dragging = false; + return; + } + + void drawLabels(Graphics g) { + + for (int ii = 0; ii < pdb.chains.size(); ii++) + { + PDBChain chain = (PDBChain) pdb.chains.elementAt(ii); + + if (chain.isVisible) + { + Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; + + for (int i = 0; i < bonds.size(); i++) + { + Bond tmpBond = (Bond) bonds.elementAt(i); + + if (tmpBond.at1.isSelected) + { + labelAtom(g, tmpBond, 1); + } + + if (tmpBond.at2.isSelected) + { + + labelAtom(g, tmpBond, 2); + } + } + } + } + } + + public void labelAtom(Graphics g, Bond b, int n) { + g.setFont(font); + + if (n == 1) { + int xstart = (int) (((b.start[0] - centre[0]) * scale) + + (getSize().width / 2)); + int ystart = (int) (((b.start[1] - centre[1]) * scale) + + (getSize().height / 2)); + + g.setColor(Color.red); + g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart); + } + + if (n == 2) { + int xstart = (int) (((b.end[0] - centre[0]) * scale) + + (getSize().width / 2)); + int ystart = (int) (((b.end[1] - centre[1]) * scale) + + (getSize().height / 2)); + + g.setColor(Color.red); + g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart); + } + } + + int foundchain = -1; + public Atom findAtom(int x, int y) { + Atom fatom = null; + + foundchain = -1; + + for (int ii = 0; ii < pdb.chains.size(); ii++) + { + PDBChain chain = (PDBChain) pdb.chains.elementAt(ii); + int truex; + Bond tmpBond=null; + + if (chain.isVisible) + { + Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; + + for (int i = 0; i < bonds.size(); i++) + { + tmpBond = (Bond) bonds.elementAt(i); + + truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + + (getSize().width / 2)); + + if (Math.abs(truex - x) <= 2) + { + int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) + + (getSize().height / 2)); + + if (Math.abs(truey - y) <= 2) + { + fatom = tmpBond.at1; + foundchain = ii; + break; + } + } + } + + // Still here? Maybe its the last bond + + truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) + + (getSize().width / 2)); + + if (Math.abs(truex - x) <= 2) + { + int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) + + (getSize().height / 2)); + + if (Math.abs(truey - y) <= 2) + { + fatom = tmpBond.at2; + foundchain = ii; + break; + } + } + + } + + if (fatom != null) //)&& chain.ds != null) + { + chain = (PDBChain) pdb.chains.elementAt(foundchain); + } + } + + return fatom; + } + + public void update(Graphics g) + { + paint(g); + } + + public void highlightRes(int ii) + { + + if (highlightRes != null + && highlightRes.contains((ii-1) + "")) + { + return; + } + + int index = -1; + Bond tmpBond; + for(index=0; index 0) + { + highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1); + highlightBond1.at2.isSelected = true; + } + + if (index != mainchain.bonds.size()) + { + highlightBond2 = (Bond) mainchain.bonds.elementAt(index); + highlightBond2.at1.isSelected = true; + } + + break; + } + } + + redrawneeded = true; + repaint(); + } + + + public void setAllchainsVisible(boolean b) + { + for (int ii = 0; ii < pdb.chains.size(); ii++) + { + PDBChain chain = (PDBChain) pdb.chains.elementAt(ii); + chain.isVisible = b; + } + mainchain.isVisible = true; + findCentre(); + setupBonds(); + } + +} diff --git a/src/MCview/AppletPDBViewer.java b/src/MCview/AppletPDBViewer.java new file mode 100755 index 0000000..c3063a8 --- /dev/null +++ b/src/MCview/AppletPDBViewer.java @@ -0,0 +1,237 @@ +package MCview; + +import java.awt.*; + +import java.awt.event.*; +import jalview.datamodel.*; +import jalview.appletgui.*; +import java.awt.event.ActionListener; +import java.awt.event.ActionEvent; + + +public class AppletPDBViewer extends Frame +{ + PDBEntry pdb; + Sequence sequence; + AppletPDBCanvas pdbcanvas; + + + public AppletPDBViewer(String pdbtext,String type, + Sequence seq, + SeqCanvas seqcanvas) + { + sequence = seq; + + try + { + jbInit(); + } + catch (Exception ex) + { + ex.printStackTrace(); + } + + pdbcanvas = new AppletPDBCanvas(seqcanvas, seq); + + add(pdbcanvas, BorderLayout.CENTER); + + StringBuffer title = new StringBuffer(sequence.getName() + ":"); + + jalview.bin.JalviewLite.addFrame(this,title.toString(),400, 400); + + try{ + PDBfile pdbfile = new PDBfile(pdbtext, type); + pdbcanvas.setPDBFile(pdbfile); + } + catch(Exception ex){ + pdbcanvas.errorLoading = true; + pdbcanvas.repaint(); + } + } + + + private void jbInit() + throws Exception + { + this.setMenuBar(jMenuBar1); + fileMenu.setLabel("File"); + coloursMenu.setLabel("Colours"); + mapping.setLabel("View Mapping"); + mapping.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + mapping_actionPerformed(); + } + }); + wire.setLabel("Wireframe"); + wire.addItemListener(new ItemListener() + { + public void itemStateChanged(ItemEvent e) + { + wire_actionPerformed(); + } + }); + depth.setState(true); + depth.setLabel("Depthcue"); + depth.addItemListener(new ItemListener() + { + public void itemStateChanged(ItemEvent e) + { + depth_actionPerformed(); + } + }); + zbuffer.setState(true); + zbuffer.setLabel("Z Buffering"); + zbuffer.addItemListener(new ItemListener() + { + public void itemStateChanged(ItemEvent e) + { + zbuffer_actionPerformed(); + } + }); + charge.setLabel("Charge & Cysteine"); + charge.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + charge_actionPerformed(); + } + }); + hydro.setLabel("Hydrophobicity"); + hydro.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + hydro_actionPerformed(); + } + }); + chain.setLabel("By Chain"); + chain.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e) + { + chain_actionPerformed(); + } + }); + seqButton.setLabel("By Sequence"); + seqButton.addActionListener(new ActionListener() + { + public void actionPerformed(ActionEvent e){ + seqButton_actionPerformed(); + } + }); + allchains.setLabel("All Chains Visible"); + allchains.addItemListener(new ItemListener() + { + public void itemStateChanged(ItemEvent itemEvent) + { + allchains_itemStateChanged(itemEvent); + } + }); + jMenuBar1.add(fileMenu); + jMenuBar1.add(coloursMenu); + fileMenu.add(mapping);; + + coloursMenu.add(seqButton); + coloursMenu.add(chain); + coloursMenu.add(hydro); + coloursMenu.add(charge); + coloursMenu.addSeparator(); + coloursMenu.add(wire); + coloursMenu.add(depth); + coloursMenu.add(zbuffer); + coloursMenu.add(allchains); + allchains.setState(true); + } + + MenuBar jMenuBar1 = new MenuBar(); + Menu fileMenu = new Menu(); + Menu coloursMenu = new Menu(); + MenuItem mapping = new MenuItem(); + CheckboxGroup bg = new CheckboxGroup(); + CheckboxMenuItem wire = new CheckboxMenuItem(); + CheckboxMenuItem depth = new CheckboxMenuItem(); + CheckboxMenuItem zbuffer = new CheckboxMenuItem(); + + MenuItem charge = new MenuItem(); + MenuItem hydro = new MenuItem(); + MenuItem chain = new MenuItem(); + MenuItem seqButton = new MenuItem(); + + CheckboxMenuItem allchains = new CheckboxMenuItem(); + + public void charge_actionPerformed() + { + clearButtonGroup(); + pdbcanvas.pdb.setChargeColours(); + pdbcanvas.redrawneeded=true; + pdbcanvas.repaint(); + } + + public void hydro_actionPerformed() + { + clearButtonGroup(); + pdbcanvas.pdb.setHydrophobicityColours(); + pdbcanvas.redrawneeded=true; + pdbcanvas.repaint(); + } + + public void chain_actionPerformed() + { + clearButtonGroup(); + pdbcanvas.pdb.setChainColours(); + pdbcanvas.redrawneeded=true; + pdbcanvas.repaint(); + } + + public void zbuffer_actionPerformed() + { + pdbcanvas.zbuffer = ! pdbcanvas.zbuffer; + pdbcanvas.redrawneeded=true; + pdbcanvas.repaint(); + } + + public void depth_actionPerformed() + { + pdbcanvas.depthcue = ! pdbcanvas.depthcue; + pdbcanvas.redrawneeded=true; + pdbcanvas.repaint(); + } + + public void wire_actionPerformed() + { + pdbcanvas.wire = ! pdbcanvas.wire; + pdbcanvas.redrawneeded=true; + pdbcanvas.repaint(); + } + + public void seqButton_actionPerformed() + { + clearButtonGroup(); + pdbcanvas.bysequence = true; + pdbcanvas.updateSeqColours(); + pdbcanvas.repaint(); + } + + void clearButtonGroup() + { + pdbcanvas.bysequence = false; + pdbcanvas.bymolecule = false; + } + + public void mapping_actionPerformed() + { + jalview.appletgui.CutAndPasteTransfer cap + = new jalview.appletgui.CutAndPasteTransfer(false, null); + Frame frame = new Frame(); + frame.add(cap); + jalview.bin.JalviewLite.addFrame(frame, "PDB - Sequence Mapping", 500, 600); + cap.setText(pdbcanvas.mappingDetails.toString()); + } + + public void allchains_itemStateChanged(ItemEvent itemEvent) + { + pdbcanvas.setAllchainsVisible(allchains.getState()); + } +} diff --git a/src/MCview/Atom.java b/src/MCview/Atom.java index 0aaf040..51c4f01 100755 --- a/src/MCview/Atom.java +++ b/src/MCview/Atom.java @@ -20,13 +20,11 @@ package MCview; import java.awt.*; -import java.util.*; - public class Atom { - double x; - double y; - double z; + float x; + float y; + float z; int number; String name; String resName; @@ -34,36 +32,24 @@ public class Atom { int type; Color color; String chain; + int alignmentMapping=-1; - public Atom(StringTokenizer str) { - this.number = (new Integer(str.nextToken())).intValue(); - this.name = str.nextToken(); - this.resName = str.nextToken(); + public boolean isSelected = false; - String tmpstr = new String(); + public Atom(String str) + { + name = str.substring(12,15).trim(); - try { - tmpstr = str.nextToken(); + resName = str.substring(17,20); - this.resNumber = (new Integer(tmpstr).intValue()); - this.chain = "A"; - this.color = Color.green; - } catch (NumberFormatException e) { - this.chain = tmpstr; + chain = str.substring(21,22); - if (tmpstr.equals("A")) { - this.color = new Color((float) Math.random(), - (float) Math.random(), (float) Math.random()); - } else { - this.color = Color.red; - } + resNumber = Integer.parseInt(str.substring(22,26).trim()); - this.resNumber = (new Integer(str.nextToken()).intValue()); - } + this.x = (float) (new Float(str.substring(30,38).trim()).floatValue()); + this.y = (float) (new Float(str.substring(38,46).trim()).floatValue()); + this.z = (float) (new Float(str.substring(47,55).trim()).floatValue()); - this.x = (double) (new Double(str.nextToken()).floatValue()); - this.y = (double) (new Double(str.nextToken()).floatValue()); - this.z = (double) (new Double(str.nextToken()).floatValue()); } public void setColor(Color col) { diff --git a/src/MCview/Bond.java b/src/MCview/Bond.java index 17f66bb..5f162b0 100755 --- a/src/MCview/Bond.java +++ b/src/MCview/Bond.java @@ -26,10 +26,10 @@ public class Bond { float[] end; Color startCol; Color endCol; - public myAtom at1; - public myAtom at2; + public Atom at1; + public Atom at2; - public Bond(float[] start, float[] end, myAtom at1, myAtom at2) { + public Bond(float[] start, float[] end, Atom at1, Atom at2) { this.start = start; this.end = end; this.startCol = at1.color; @@ -55,12 +55,6 @@ public class Bond { this.endCol = bond.endCol; } - public void print() { - System.out.println("Start " + start[0] + " " + start[1] + " " + - start[2]); - System.out.println("End " + end[0] + " " + end[1] + " " + end[2]); - } - public float length() { float len = ((end[0] - start[0]) * (end[0] - start[0])) + ((end[1] - start[1]) * (end[1] - start[1])) + diff --git a/src/MCview/MCMatrix.java b/src/MCview/MCMatrix.java index 2effba6..66c3798 100755 --- a/src/MCview/MCMatrix.java +++ b/src/MCview/MCMatrix.java @@ -34,15 +34,6 @@ public class MCMatrix { matrix[i][j] = value; } - public void print() { - System.out.println(matrix[0][0] + " " + matrix[0][1] + " " + - matrix[0][2]); - System.out.println(matrix[1][0] + " " + matrix[1][1] + " " + - matrix[1][2]); - System.out.println(matrix[2][0] + " " + matrix[2][1] + " " + - matrix[2][2]); - } - public void rotatex(float degrees) { mycos = (float) (Math.cos(degrees * myconst)); mysin = (float) (Math.sin(degrees * myconst)); diff --git a/src/MCview/PDBCanvas.java b/src/MCview/PDBCanvas.java new file mode 100755 index 0000000..17db1a0 --- /dev/null +++ b/src/MCview/PDBCanvas.java @@ -0,0 +1,986 @@ +/* +* 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 MCview; + +import jalview.analysis.AlignSeq; + +import jalview.datamodel.*; + +// JBPNote TODO: This class is quite noisy - needs proper log.info/log.debug +import java.awt.*; +import java.awt.event.*; + +import java.io.*; + +import java.util.*; + +import javax.swing.*; + + +public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListener +{ + MCMatrix idmat = new MCMatrix(3, 3); + MCMatrix objmat = new MCMatrix(3, 3); + boolean redrawneeded = true; + int omx = 0; + int mx = 0; + int omy = 0; + int my = 0; + public PDBfile pdb; + int bsize; + Image img; + Graphics ig; + Dimension prefsize; + float[] centre = new float[3]; + float[] width = new float[3]; + float maxwidth; + float scale; + String inStr; + String inType; + boolean bysequence = true; + boolean depthcue = true; + boolean wire = false; + boolean bymolecule = false; + boolean zbuffer = true; + boolean dragging; + int xstart; + int xend; + int ystart; + int yend; + int xmid; + int ymid; + Font font = new Font("Helvetica", Font.PLAIN, 10); + jalview.gui.SeqCanvas seqcanvas; + public Sequence sequence; + final StringBuffer mappingDetails = new StringBuffer(); + PDBChain mainchain; + Vector highlightRes; + boolean pdbAction = false; + + public PDBCanvas(jalview.gui.SeqCanvas seqcanvas, Sequence seq) + { + this.seqcanvas = seqcanvas; + this.sequence = seq; + seqcanvas.setPDBCanvas(this); + } + + public void setPDBFile(PDBfile pdb) + { + int max = -10; + int maxchain = -1; + int pdbstart = 0; + int pdbend = 0; + int seqstart = 0; + int seqend = 0; + AlignSeq maxAlignseq = null; + + for (int i = 0; i < pdb.chains.size(); i++) + { + + mappingDetails.append("\n\nPDB Sequence is :\nSequence = " + ((PDBChain) pdb.chains.elementAt(i)).sequence.getSequence()); + mappingDetails.append("\nNo of residues = " + ((PDBChain) pdb.chains.elementAt(i)).residues.size()+"\n\n"); + + // Now lets compare the sequences to get + // the start and end points. + // Align the sequence to the pdb + AlignSeq as = new AlignSeq(sequence, + ((PDBChain) pdb.chains.elementAt(i)).sequence, "pep"); + as.calcScoreMatrix(); + as.traceAlignment(); + PrintStream ps = new PrintStream(System.out) + { + public void print(String x) { + mappingDetails.append(x); + } + public void println() + { + mappingDetails.append("\n"); + } + }; + + as.printAlignment(ps); + + + + if (as.maxscore > max) + { + max = as.maxscore; + maxchain = i; + pdbstart = as.seq2start; + pdbend = as.seq2end; + seqstart = as.seq1start + sequence.getStart()-1; + seqend = as.seq1end + sequence.getEnd()-1; + maxAlignseq = as; + } + + mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend); + mappingDetails.append("\nSEQ start/end "+ seqstart + " " + seqend); + } + + mainchain = (PDBChain) pdb.chains.elementAt(maxchain); + + mainchain.pdbstart = pdbstart; + mainchain.pdbend = pdbend; + mainchain.seqstart = seqstart; + mainchain.seqend = seqend; + mainchain.isVisible = true; + mainchain.makeExactMapping(maxAlignseq, sequence); + + this.pdb = pdb; + this.prefsize = new Dimension(getWidth(), getHeight()); + + //Initialize the matrices to identity + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + if (i != j) { + idmat.addElement(i, j, 0); + objmat.addElement(i, j, 0); + } else { + idmat.addElement(i, j, 1); + objmat.addElement(i, j, 1); + } + } + } + + addMouseMotionListener(this); + addMouseListener(this); + + addMouseWheelListener(new MouseWheelListener() + { + public void mouseWheelMoved(MouseWheelEvent e) + { + if (e.getWheelRotation() > 0) + { + scale = (float) (scale * 1.1); + redrawneeded = true; + repaint(); + } + + else + { + scale = (float) (scale * 0.9); + redrawneeded = true; + repaint(); + } + } + }); + + + findCentre(); + findWidth(); + + setupBonds(); + + scale = findScale(); + + ToolTipManager.sharedInstance().registerComponent(this); + ToolTipManager.sharedInstance().setInitialDelay(0); + ToolTipManager.sharedInstance().setDismissDelay(10000); + } + + + Vector visiblebonds; + void setupBonds() + { + // Sort the bonds by z coord + visiblebonds = new Vector(); + + for (int ii = 0; ii < pdb.chains.size(); ii++) + { + if ( ( (PDBChain) pdb.chains.elementAt(ii)).isVisible) + { + Vector tmp = ( (PDBChain) pdb.chains.elementAt(ii)).bonds; + + for (int i = 0; i < tmp.size(); i++) + { + visiblebonds.addElement(tmp.elementAt(i)); + } + } + } + + updateSeqColours(); + redrawneeded = true; + repaint(); + } + + + public void findWidth() { + float[] max = new float[3]; + float[] min = new float[3]; + + max[0] = (float) -1e30; + max[1] = (float) -1e30; + max[2] = (float) -1e30; + + min[0] = (float) 1e30; + min[1] = (float) 1e30; + min[2] = (float) 1e30; + + for (int ii = 0; ii < pdb.chains.size(); ii++) { + if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) { + Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; + + for (int i = 0; i < bonds.size(); i++) { + Bond tmp = (Bond) bonds.elementAt(i); + + if (tmp.start[0] >= max[0]) { + max[0] = tmp.start[0]; + } + + if (tmp.start[1] >= max[1]) { + max[1] = tmp.start[1]; + } + + if (tmp.start[2] >= max[2]) { + max[2] = tmp.start[2]; + } + + if (tmp.start[0] <= min[0]) { + min[0] = tmp.start[0]; + } + + if (tmp.start[1] <= min[1]) { + min[1] = tmp.start[1]; + } + + if (tmp.start[2] <= min[2]) { + min[2] = tmp.start[2]; + } + + if (tmp.end[0] >= max[0]) { + max[0] = tmp.end[0]; + } + + if (tmp.end[1] >= max[1]) { + max[1] = tmp.end[1]; + } + + if (tmp.end[2] >= max[2]) { + max[2] = tmp.end[2]; + } + + if (tmp.end[0] <= min[0]) { + min[0] = tmp.end[0]; + } + + if (tmp.end[1] <= min[1]) { + min[1] = tmp.end[1]; + } + + if (tmp.end[2] <= min[2]) { + min[2] = tmp.end[2]; + } + } + } + } + /* + System.out.println("xmax " + max[0] + " min " + min[0]); + System.out.println("ymax " + max[1] + " min " + min[1]); + System.out.println("zmax " + max[2] + " min " + min[2]);*/ + + width[0] = (float) Math.abs(max[0] - min[0]); + width[1] = (float) Math.abs(max[1] - min[1]); + width[2] = (float) Math.abs(max[2] - min[2]); + + maxwidth = width[0]; + + if (width[1] > width[0]) { + maxwidth = width[1]; + } + + if (width[2] > width[1]) { + maxwidth = width[2]; + } + + // System.out.println("Maxwidth = " + maxwidth); + } + + public float findScale() { + int dim; + int width; + int height; + + if (getWidth() != 0) { + width = getWidth(); + height = getHeight(); + } else { + width = prefsize.width; + height = prefsize.height; + } + + if (width < height) { + dim = width; + } else { + dim = height; + } + + return (float) (dim / (1.5d * maxwidth)); + } + + public void findCentre() { + float xtot = 0; + float ytot = 0; + float ztot = 0; + + int bsize = 0; + + //Find centre coordinate + for (int ii = 0; ii < pdb.chains.size(); ii++) { + if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) { + Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; + + bsize += bonds.size(); + + for (int i = 0; i < bonds.size(); i++) { + xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] + + ((Bond) bonds.elementAt(i)).end[0]; + + ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] + + ((Bond) bonds.elementAt(i)).end[1]; + + ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] + + ((Bond) bonds.elementAt(i)).end[2]; + } + } + } + + centre[0] = xtot / (2 * (float) bsize); + centre[1] = ytot / (2 * (float) bsize); + centre[2] = ztot / (2 * (float) bsize); + } + + public void paintComponent(Graphics g) + { + super.paintComponent(g); + + if(visiblebonds==null) + { + g.setColor(Color.black); + g.setFont(new Font("Verdana", Font.BOLD, 14)); + g.drawString("Retrieving PDB data....", 20, getHeight()/2); + return; + } + + + //Only create the image at the beginning - + //this saves much memory usage + if ((img == null) + || (prefsize.width != getWidth()) + || (prefsize.height != getHeight())) + + { + prefsize.width = getWidth(); + prefsize.height = getHeight(); + + scale = findScale(); + img = createImage(prefsize.width, prefsize.height); + ig = img.getGraphics(); + Graphics2D ig2 = (Graphics2D) ig; + + ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, + RenderingHints.VALUE_ANTIALIAS_ON); + + redrawneeded = true; + } + + + if (redrawneeded) + { + drawAll(ig, prefsize.width, prefsize.height); + redrawneeded = false; + } + + g.drawImage(img, 0, 0, this); + + pdbAction = false; + } + + public void drawAll(Graphics g, int width, int height) + { + g.setColor(Color.black); + g.fillRect(0, 0, width, height); + drawScene(g); + drawLabels(g); + } + + + public void updateSeqColours() + { + if(pdbAction) + { + return; + } + + // System.out.println("update seq colours"); + if(bysequence && pdb!=null) + { + for (int ii = 0; ii < pdb.chains.size(); ii++) + { + colourBySequence( (PDBChain) pdb.chains.elementAt(ii)); + } + } + + redrawneeded=true; + repaint(); + } + + int findTrueIndex(int pos) + { + // returns the alignment position for a residue + int j = sequence.getStart(); + int i = 0; + + while ( (i < sequence.getLength()) && (j <= sequence.getEnd()) && (j <= pos+1)) + { + if (!jalview.util.Comparison.isGap(sequence.getCharAt(i))) + { + j++; + } + + i++; + } + + if(i>1) + i--; + + if ( (j == sequence.getEnd()) && (j < pos)) + { + return sequence.getEnd() + 1; + } + else + { + return i; + } + } + + // This method has been taken out of PDBChain to allow + // Applet and Application specific sequence renderers to be used + void colourBySequence(PDBChain chain) + { + // System.out.println("colour by seq"); + for (int i = 0; i < chain.bonds.size(); i++) + { + Bond tmp = (Bond) chain.bonds.elementAt(i); + tmp.startCol = Color.lightGray; + tmp.endCol = Color.lightGray; + + if(chain!=mainchain) + continue; + + if ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) && + (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1))) + { + int index = findTrueIndex(tmp.at1.alignmentMapping); + //sequence.findIndex(tmp.at1.alignmentMapping); + if (index != -1) + { + tmp.startCol = seqcanvas.getSequenceRenderer(). + getResidueBoxColour( sequence, index); + if(tmp.startCol==null) + tmp.startCol = Color.white; + + tmp.startCol = seqcanvas.getFeatureRenderer(). + findFeatureColour(tmp.startCol, sequence, index); + } + } + + if ( (tmp.at2.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) && + (tmp.at2.resNumber <= ( (chain.pdbend + chain.offset) - 1))) + { + + int index = findTrueIndex(tmp.at2.alignmentMapping); + //sequence.findIndex( tmp.at2.alignmentMapping ); + if (index != -1) + { + tmp.endCol = seqcanvas.getSequenceRenderer(). + getResidueBoxColour( sequence, index); + if(tmp.endCol==null) + tmp.endCol = Color.white; + tmp.endCol = seqcanvas.getFeatureRenderer(). + findFeatureColour(tmp.endCol, sequence, index); + } + } + } + } + + + public void drawScene(Graphics g) + { + if (zbuffer) + { + Zsort.Zsort(visiblebonds); + } + + Bond tmpBond=null; + for (int i = 0; i < visiblebonds.size(); i++) + { + tmpBond = (Bond) visiblebonds.elementAt(i); + + xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + + (getWidth() / 2)); + ystart = (int) (((tmpBond.start[1] - centre[1]) * scale) + + (getHeight() / 2)); + + xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + + (getWidth() / 2)); + yend = (int) (((tmpBond.end[1] - centre[1]) * scale) + + (getHeight() / 2)); + + xmid = (xend + xstart) / 2; + ymid = (yend + ystart) / 2; + + if (depthcue && !bymolecule) + { + if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) { + g.setColor(tmpBond.startCol.darker().darker()); + drawLine(g, xstart, ystart, xmid, ymid); + g.setColor(tmpBond.endCol.darker().darker()); + drawLine(g, xmid, ymid, xend, yend); + } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) { + g.setColor(tmpBond.startCol.darker()); + drawLine(g, xstart, ystart, xmid, ymid); + + g.setColor(tmpBond.endCol.darker()); + drawLine(g, xmid, ymid, xend, yend); + } else { + g.setColor(tmpBond.startCol); + drawLine(g, xstart, ystart, xmid, ymid); + + g.setColor(tmpBond.endCol); + drawLine(g, xmid, ymid, xend, yend); + } + } else if (depthcue && bymolecule) { + if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) { + g.setColor(Color.green.darker().darker()); + drawLine(g, xstart, ystart, xend, yend); + } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) { + g.setColor(Color.green.darker()); + drawLine(g, xstart, ystart, xend, yend); + } else { + g.setColor(Color.green); + drawLine(g, xstart, ystart, xend, yend); + } + } else if (!depthcue && !bymolecule) { + g.setColor(tmpBond.startCol); + drawLine(g, xstart, ystart, xmid, ymid); + g.setColor(tmpBond.endCol); + drawLine(g, xmid, ymid, xend, yend); + } else { + drawLine(g, xstart, ystart, xend, yend); + } + + if(highlightBond1!=null && highlightBond1==tmpBond) + { + g.setColor(tmpBond.endCol.brighter().brighter().brighter().brighter()); + drawLine(g, xmid, ymid, xend, yend); + } + + if(highlightBond2!=null && highlightBond2==tmpBond) + { + g.setColor(tmpBond.startCol.brighter().brighter().brighter().brighter()); + drawLine(g, xstart, ystart, xmid, ymid); + } + + } + + + } + + public void drawLine(Graphics g, int x1, int y1, int x2, int y2) { + if (!wire) { + if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) { + g.drawLine(x1, y1, x2, y2); + g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1); + g.drawLine(x1, y1 - 1, x2, y2 - 1); + } else { + g.setColor(g.getColor().brighter()); + g.drawLine(x1, y1, x2, y2); + g.drawLine(x1 + 1, y1, x2 + 1, y2); + g.drawLine(x1 - 1, y1, x2 - 1, y2); + } + } else { + g.drawLine(x1, y1, x2, y2); + } + } + + public Dimension minimumsize() { + return prefsize; + } + + public Dimension preferredsize() { + return prefsize; + } + + public void keyPressed(KeyEvent evt) + { + if (evt.getKeyCode() == KeyEvent.VK_UP) + { + scale = (float) (scale * 1.1); + redrawneeded = true; + repaint(); + } + else if (evt.getKeyCode() == KeyEvent.VK_DOWN) + { + scale = (float) (scale * 0.9); + redrawneeded = true; + repaint(); + } + } + + public void mousePressed(MouseEvent e) + { + pdbAction = true; + Atom fatom = findAtom(e.getX(), e.getY()); + if(fatom!=null) + { + fatom.isSelected = !fatom.isSelected; + + redrawneeded = true; + repaint(); + if (foundchain != -1) + { + PDBChain chain = (PDBChain) pdb.chains.elementAt(foundchain); + if (chain == mainchain) + { + if (fatom.alignmentMapping != -1) + { + if (highlightRes == null) + highlightRes = new Vector(); + + if (highlightRes.contains(fatom.alignmentMapping+"" + "")) + highlightRes.remove(fatom.alignmentMapping + ""); + else + highlightRes.add(fatom.alignmentMapping + ""); + } + } + } + + } + mx = e.getX(); + my = e.getY(); + omx = mx; + omy = my; + dragging = false; + } + + public void mouseMoved(MouseEvent e) + { + pdbAction = true; + if(highlightBond1!=null) + { + highlightBond1.at2.isSelected = false; + highlightBond2.at1.isSelected = false; + highlightBond1 = null; + highlightBond2 = null; + } + + Atom fatom = findAtom(e.getX(), e.getY()); + + PDBChain chain = null; + if(foundchain!=-1) + { + chain = (PDBChain) pdb.chains.elementAt(foundchain); + if(chain == mainchain) + { + highlightSeqcanvas( fatom.alignmentMapping ); + } + } + + if (fatom != null) + { + this.setToolTipText(chain.id+":"+ fatom.resNumber+" "+ fatom.resName); + } else + { + highlightSeqcanvas( -1); + this.setToolTipText(null); + } + } + + + void highlightSeqcanvas(int pos) + { + int index = seqcanvas.getViewport().getAlignment().findIndex(sequence); + + int size = pos==-1?0:3; + + if(highlightRes!=null) + size += highlightRes.size()*3; + + int [] array = new int[size]; + int i=0; + if(highlightRes!=null) + { + for (i = 0; i < highlightRes.size(); i++) + { + int a = Integer.parseInt(highlightRes.elementAt( + i).toString())+1; + array[i * 3] = index; + array[ (i * 3) + 1] = a; + array[ (i * 3) + 2] = a; + } + } + + if(pos!=-1) + { + array[i * 3] = index; + array[i * 3 + 1] = pos+1; + array[i * 3 + 2] = pos+1; + } + + seqcanvas.highlightSearchResults(array); + } + + + public void mouseClicked(MouseEvent e) { } + + public void mouseEntered(MouseEvent e) { } + + public void mouseExited(MouseEvent e) { } + + public void mouseDragged(MouseEvent evt) + { + int x = evt.getX(); + int y = evt.getY(); + mx = x; + my = y; + + + MCMatrix objmat = new MCMatrix(3, 3); + objmat.setIdentity(); + + if ((evt.getModifiers() & Event.META_MASK) != 0) { + objmat.rotatez((float) ((mx - omx))); + } else { + objmat.rotatex((float) ((my - omy))); + objmat.rotatey((float) ((omx - mx))); + } + + //Alter the bonds + for (int ii = 0; ii < pdb.chains.size(); ii++) { + Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; + + for (int i = 0; i < bonds.size(); i++) { + Bond tmpBond = (Bond) bonds.elementAt(i); + + //Translate the bond so the centre is 0,0,0 + tmpBond.translate(-centre[0], -centre[1], -centre[2]); + + //Now apply the rotation matrix + tmpBond.start = objmat.vectorMultiply(tmpBond.start); + tmpBond.end = objmat.vectorMultiply(tmpBond.end); + + //Now translate back again + tmpBond.translate(centre[0], centre[1], centre[2]); + } + } + + objmat = null; + + omx = mx; + omy = my; + + dragging = true; + + redrawneeded = true; + + repaint(); + } + + public void mouseReleased(MouseEvent evt) + { + dragging = false; + return; + } + + void drawLabels(Graphics g) { + + for (int ii = 0; ii < pdb.chains.size(); ii++) + { + PDBChain chain = (PDBChain) pdb.chains.elementAt(ii); + + if (chain.isVisible) + { + Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; + + for (int i = 0; i < bonds.size(); i++) + { + Bond tmpBond = (Bond) bonds.elementAt(i); + + if (tmpBond.at1.isSelected) + { + labelAtom(g, tmpBond, 1); + } + + if (tmpBond.at2.isSelected) + { + + labelAtom(g, tmpBond, 2); + } + } + } + } + } + + public void labelAtom(Graphics g, Bond b, int n) { + g.setFont(font); + g.setColor(Color.red); + if (n == 1) + { + int xstart = (int) (((b.start[0] - centre[0]) * scale) + + (getWidth() / 2)); + int ystart = (int) (((b.start[1] - centre[1]) * scale) + + (getHeight() / 2)); + + g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart); + } + + if (n == 2) { + int xstart = (int) (((b.end[0] - centre[0]) * scale) + + (getWidth() / 2)); + int ystart = (int) (((b.end[1] - centre[1]) * scale) + + (getHeight() / 2)); + + g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart); + } + } + + int foundchain = -1; + public Atom findAtom(int x, int y) { + Atom fatom = null; + + foundchain = -1; + + for (int ii = 0; ii < pdb.chains.size(); ii++) + { + PDBChain chain = (PDBChain) pdb.chains.elementAt(ii); + int truex; + Bond tmpBond=null; + + if (chain.isVisible) + { + Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; + + for (int i = 0; i < bonds.size(); i++) + { + tmpBond = (Bond) bonds.elementAt(i); + + truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + + (getWidth() / 2)); + + if (Math.abs(truex - x) <= 2) + { + int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) + + (getHeight() / 2)); + + if (Math.abs(truey - y) <= 2) + { + fatom = tmpBond.at1; + foundchain = ii; + break; + } + } + } + + // Still here? Maybe its the last bond + + truex = (int) ( ( (tmpBond.end[0] - centre[0]) * scale) + + (getWidth() / 2)); + + if (Math.abs(truex - x) <= 2) + { + int truey = (int) ( ( (tmpBond.end[1] - centre[1]) * scale) + + (getHeight() / 2)); + + if (Math.abs(truey - y) <= 2) + { + fatom = tmpBond.at2; + foundchain = ii; + break; + } + } + + } + + if (fatom != null) //)&& chain.ds != null) + { + chain = (PDBChain) pdb.chains.elementAt(foundchain); + } + } + + return fatom; + } + + Bond highlightBond1, highlightBond2; + public void highlightRes(int ii) + { + + if (highlightRes != null + && highlightRes.contains((ii-1) + "")) + { + return; + } + + int index = -1; + Bond tmpBond; + for(index=0; index 0) + { + highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1); + highlightBond1.at2.isSelected = true; + } + + if (index != mainchain.bonds.size()) + { + highlightBond2 = (Bond) mainchain.bonds.elementAt(index); + highlightBond2.at1.isSelected = true; + } + + break; + } + } + + redrawneeded = true; + repaint(); + } + + public void setAllchainsVisible(boolean b) + { + for (int ii = 0; ii < pdb.chains.size(); ii++) + { + PDBChain chain = (PDBChain) pdb.chains.elementAt(ii); + chain.isVisible = b; + } + mainchain.isVisible = true; + findCentre(); + setupBonds(); + } +} diff --git a/src/MCview/PDBChain.java b/src/MCview/PDBChain.java index 192156e..5f23a26 100755 --- a/src/MCview/PDBChain.java +++ b/src/MCview/PDBChain.java @@ -25,6 +25,7 @@ import jalview.schemes.ResidueProperties; import java.awt.*; import java.util.*; +import jalview.analysis.AlignSeq; public class PDBChain { @@ -34,13 +35,12 @@ public class PDBChain { public Vector residues = new Vector(); public int offset; public Sequence sequence; - public boolean isVisible = false; + public boolean isVisible = true; public int pdbstart = 0; public int pdbend = 0; public int seqstart = 0; public int seqend = 0; - //public DrawableSequence ds; public PDBChain(String id) { this.id = id; } @@ -57,22 +57,60 @@ public class PDBChain { return tmp; } - public void makeCaBondList() { - for (int i = 0; i < (residues.size() - 1); i++) { + void makeExactMapping(AlignSeq as, Sequence s1) + { + int pdbpos = as.getSeq2Start()-2; + int alignpos = s1.getStart() + as.getSeq1Start()-3; + + for(int i=0; i max) { - max = as.maxscore; - maxchain = i; - - pdbstart = as.seq2start; - pdbend = as.seq2end; - seqstart = as.seq1start - 1; - seqend = as.seq1end - 1; - } - - System.out.println("PDB start/end " + pdbstart + " " + pdbend); - System.out.println("SEQ start/end " + seqstart + " " + seqend); - } - - ((PDBChain) pdb.chains.elementAt(maxchain)).pdbstart = pdbstart; - ((PDBChain) pdb.chains.elementAt(maxchain)).pdbend = pdbend; - ((PDBChain) pdb.chains.elementAt(maxchain)).seqstart = seqstart; - ((PDBChain) pdb.chains.elementAt(maxchain)).seqend = seqend; - ((PDBChain) pdb.chains.elementAt(maxchain)).isVisible = true; - ((PDBChain) pdb.chains.elementAt(maxchain)).sequence = sequence; - ((PDBChain) pdb.chains.elementAt(maxchain)).colourBySequence(av, - sequence); - - this.pdb = pdb; - this.prefsize = new Dimension(getWidth(), getHeight()); - - //Initialize the matrices to identity - for (int i = 0; i < 3; i++) { - for (int j = 0; j < 3; j++) { - if (i != j) { - idmat.addElement(i, j, 0); - objmat.addElement(i, j, 0); - } else { - idmat.addElement(i, j, 1); - objmat.addElement(i, j, 1); - } - } - } - - addMouseMotionListener(this); - addMouseListener(this); - addKeyListener(this); - - addPDBfile(); - ToolTipManager.sharedInstance().registerComponent(this); - ToolTipManager.sharedInstance().setInitialDelay(0); - ToolTipManager.sharedInstance().setDismissDelay(10000); - - } - - public void addPDBfile() { - findCentre(); - findWidth(); - - scale = findScale(); - - System.out.println("Scale factor = " + scale); - } - - public void deleteBonds() { - scale = 0; - maxwidth = 0; - - width[0] = 0; - width[1] = 0; - width[2] = 0; - - centre[0] = 0; - centre[1] = 0; - centre[2] = 0; - - for (int i = 0; i < pdb.chains.size(); i++) { - ((PDBChain) pdb.chains.elementAt(i)).bonds = null; - } - } - - public void findWidth() { - float[] max = new float[3]; - float[] min = new float[3]; - - max[0] = (float) -1e30; - max[1] = (float) -1e30; - max[2] = (float) -1e30; - - min[0] = (float) 1e30; - min[1] = (float) 1e30; - min[2] = (float) 1e30; - - for (int ii = 0; ii < pdb.chains.size(); ii++) { - if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) { - Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; - - for (int i = 0; i < bonds.size(); i++) { - Bond tmp = (Bond) bonds.elementAt(i); - - if (tmp.start[0] >= max[0]) { - max[0] = tmp.start[0]; - } - - if (tmp.start[1] >= max[1]) { - max[1] = tmp.start[1]; - } - - if (tmp.start[2] >= max[2]) { - max[2] = tmp.start[2]; - } - - if (tmp.start[0] <= min[0]) { - min[0] = tmp.start[0]; - } - - if (tmp.start[1] <= min[1]) { - min[1] = tmp.start[1]; - } - - if (tmp.start[2] <= min[2]) { - min[2] = tmp.start[2]; - } - - if (tmp.end[0] >= max[0]) { - max[0] = tmp.end[0]; - } - - if (tmp.end[1] >= max[1]) { - max[1] = tmp.end[1]; - } - - if (tmp.end[2] >= max[2]) { - max[2] = tmp.end[2]; - } - - if (tmp.end[0] <= min[0]) { - min[0] = tmp.end[0]; - } - - if (tmp.end[1] <= min[1]) { - min[1] = tmp.end[1]; - } - - if (tmp.end[2] <= min[2]) { - min[2] = tmp.end[2]; - } - } - } - } - - System.out.println("xmax " + max[0] + " min " + min[0]); - System.out.println("ymax " + max[1] + " min " + min[1]); - System.out.println("zmax " + max[2] + " min " + min[2]); - - width[0] = (float) Math.abs(max[0] - min[0]); - width[1] = (float) Math.abs(max[1] - min[1]); - width[2] = (float) Math.abs(max[2] - min[2]); - - maxwidth = width[0]; - - if (width[1] > width[0]) { - maxwidth = width[1]; - } - - if (width[2] > width[1]) { - maxwidth = width[2]; - } - - System.out.println("Maxwidth = " + maxwidth); - } - - public float findScale() { - int dim; - int width; - int height; - - if (getWidth() != 0) { - width = getWidth(); - height = getHeight(); - } else { - width = prefsize.width; - height = prefsize.height; - } - - if (width < height) { - dim = width; - } else { - dim = height; - } - - return (float) (dim / (1.5d * maxwidth)); - } - - public void findCentre() { - float xtot = 0; - float ytot = 0; - float ztot = 0; - - int bsize = 0; - - //Find centre coordinate - for (int ii = 0; ii < pdb.chains.size(); ii++) { - if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) { - Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; - - bsize += bonds.size(); - - for (int i = 0; i < bonds.size(); i++) { - xtot = xtot + ((Bond) bonds.elementAt(i)).start[0] + - ((Bond) bonds.elementAt(i)).end[0]; - - ytot = ytot + ((Bond) bonds.elementAt(i)).start[1] + - ((Bond) bonds.elementAt(i)).end[1]; - - ztot = ztot + ((Bond) bonds.elementAt(i)).start[2] + - ((Bond) bonds.elementAt(i)).end[2]; - } - } - } - - centre[0] = xtot / (2 * (float) bsize); - centre[1] = ytot / (2 * (float) bsize); - centre[2] = ztot / (2 * (float) bsize); - } - - public void paint(Graphics g) { - //Only create the image at the beginning - - //this saves much memory usage - if ((img == null) || (prefsize.width != getWidth()) || - (prefsize.height != getHeight())) { - prefsize.width = getWidth(); - prefsize.height = getHeight(); - - scale = findScale(); - img = createImage(prefsize.width, prefsize.height); - ig = img.getGraphics(); - - redrawneeded = true; - } - - if (redrawneeded == true) { - drawBackground(ig, Color.black); - drawScene(ig); - redrawneeded = false; - } else { - ig = img.getGraphics(); - } - - g.drawImage(img, 0, 0, this); - } - - public void drawBackground(Graphics g, Color col) { - g.setColor(col); - g.fillRect(0, 0, prefsize.width, prefsize.height); - } - - public void drawScene(Graphics g) { - // Sort the bonds by z coord - Vector bonds = new Vector(); - - for (int ii = 0; ii < pdb.chains.size(); ii++) { - if (((PDBChain) pdb.chains.elementAt(ii)).isVisible) { - Vector tmp = ((PDBChain) pdb.chains.elementAt(ii)).bonds; - - for (int i = 0; i < tmp.size(); i++) { - bonds.addElement(tmp.elementAt(i)); - } - } - } - - if (zbuffer) { - Zsort.Zsort(bonds); - } - - for (int i = 0; i < bonds.size(); i++) { - Bond tmpBond = (Bond) bonds.elementAt(i); - - xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + - (getWidth() / 2)); - ystart = (int) (((tmpBond.start[1] - centre[1]) * scale) + - (getHeight() / 2)); - - xend = (int) (((tmpBond.end[0] - centre[0]) * scale) + - (getWidth() / 2)); - yend = (int) (((tmpBond.end[1] - centre[1]) * scale) + - (getHeight() / 2)); - - xmid = (xend + xstart) / 2; - ymid = (yend + ystart) / 2; - - if (depthcue && !bymolecule) { - if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) { - g.setColor(tmpBond.startCol.darker().darker()); - drawLine(g, xstart, ystart, xmid, ymid); - - g.setColor(tmpBond.endCol.darker().darker()); - drawLine(g, xmid, ymid, xend, yend); - } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) { - g.setColor(tmpBond.startCol.darker()); - drawLine(g, xstart, ystart, xmid, ymid); - - g.setColor(tmpBond.endCol.darker()); - drawLine(g, xmid, ymid, xend, yend); - } else { - g.setColor(tmpBond.startCol); - drawLine(g, xstart, ystart, xmid, ymid); - - g.setColor(tmpBond.endCol); - drawLine(g, xmid, ymid, xend, yend); - } - } else if (depthcue && bymolecule) { - if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) { - g.setColor(Color.green.darker().darker()); - drawLine(g, xstart, ystart, xend, yend); - } else if (tmpBond.start[2] < (centre[2] + (maxwidth / 6))) { - g.setColor(Color.green.darker()); - drawLine(g, xstart, ystart, xend, yend); - } else { - g.setColor(Color.green); - drawLine(g, xstart, ystart, xend, yend); - } - } else if (!depthcue && !bymolecule) { - g.setColor(tmpBond.startCol); - drawLine(g, xstart, ystart, xmid, ymid); - g.setColor(tmpBond.endCol); - drawLine(g, xmid, ymid, xend, yend); - } else { - drawLine(g, xstart, ystart, xend, yend); - } - } - } - - public void drawLine(Graphics g, int x1, int y1, int x2, int y2) { - if (!wire) { - if (((float) Math.abs(y2 - y1) / (float) Math.abs(x2 - x1)) < 0.5) { - g.drawLine(x1, y1, x2, y2); - g.drawLine(x1 + 1, y1 + 1, x2 + 1, y2 + 1); - g.drawLine(x1, y1 - 1, x2, y2 - 1); - } else { - g.setColor(g.getColor().brighter()); - g.drawLine(x1, y1, x2, y2); - g.drawLine(x1 + 1, y1, x2 + 1, y2); - g.drawLine(x1 - 1, y1, x2 - 1, y2); - } - } else { - g.drawLine(x1, y1, x2, y2); - } - } - - public Dimension minimumsize() { - return prefsize; - } - - public Dimension preferredsize() { - return prefsize; - } - - public void keyTyped(KeyEvent evt) { - } - - public void keyReleased(KeyEvent evt) { - } - - public void keyPressed(KeyEvent evt) { - int key = evt.getKeyChar(); - - if (evt.getKeyCode() == KeyEvent.VK_UP) { - scale = (float) (scale * 1.1); - redrawneeded = true; - repaint(); - } else if (evt.getKeyCode() == KeyEvent.VK_DOWN) { - scale = (float) (scale * 0.9); - redrawneeded = true; - repaint(); - } else if (key == 'w') { - wire = !wire; - System.out.println("wireframe " + wire); - redrawneeded = true; - repaint(); - } else if (key == 'd') { - depthcue = !depthcue; - System.out.println("Depth cueing is " + depthcue); - redrawneeded = true; - repaint(); - } else if (key == 'm') { - bymolecule = !bymolecule; - System.out.println("Bymolecule is " + bymolecule); - redrawneeded = true; - repaint(); - } else if (key == 'z') { - zbuffer = !zbuffer; - System.out.println("Z buffering is " + zbuffer); - redrawneeded = true; - repaint(); - } else if (key == 'c') { - bymolecule = false; - pdb.setChainColours(); - System.out.println("Colouring by chain"); - redrawneeded = true; - repaint(); - } else if (key == 'h') { - bymolecule = false; - pdb.setHydrophobicityColours(); - System.out.println("Colouring by hydrophobicity"); - redrawneeded = true; - repaint(); - } else if (key == 'q') { - bymolecule = false; - pdb.setChargeColours(); - System.out.println("Colouring charges and cysteines"); - redrawneeded = true; - repaint(); - } - - return; - } - - public void mousePressed(MouseEvent e) { - mx = e.getX(); - my = e.getY(); - omx = mx; - omy = my; - dragging = false; - } - - public void mouseMoved(MouseEvent e) { - myAtom fatom = null; - - for (int ii = 0; ii < pdb.chains.size(); ii++) { - PDBChain chain = (PDBChain) pdb.chains.elementAt(ii); - - if (chain.isVisible) { - Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; - - for (int i = 0; i < bonds.size(); i++) { - Bond tmpBond = (Bond) bonds.elementAt(i); - int truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + - (getWidth() / 2)); - - if (Math.abs(truex - e.getX()) <= 2) { - int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) + - (getHeight() / 2)); - - if (Math.abs(truey - e.getY()) <= 2) { - fatom = tmpBond.at1; - } - } - } - } - } - - if (fatom != null) { - this.setToolTipText(fatom.resName); - } else { - this.setToolTipText(null); - } - } - - public void mouseClicked(MouseEvent e) { - } - - public void mouseEntered(MouseEvent e) { - } - - public void mouseExited(MouseEvent e) { - } - - public void mouseDragged(MouseEvent evt) { - int x = evt.getX(); - int y = evt.getY(); - mx = x; - my = y; - - MCMatrix objmat = new MCMatrix(3, 3); - objmat.setIdentity(); - - if ((evt.getModifiers() & Event.META_MASK) != 0) { - objmat.rotatez((float) ((mx - omx))); - } else { - objmat.rotatex((float) ((my - omy))); - objmat.rotatey((float) ((omx - mx))); - } - - //Alter the bonds - for (int ii = 0; ii < pdb.chains.size(); ii++) { - Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; - - for (int i = 0; i < bonds.size(); i++) { - Bond tmpBond = (Bond) bonds.elementAt(i); - - //Translate the bond so the centre is 0,0,0 - tmpBond.translate(-centre[0], -centre[1], -centre[2]); - - //Now apply the rotation matrix - tmpBond.start = objmat.vectorMultiply(tmpBond.start); - tmpBond.end = objmat.vectorMultiply(tmpBond.end); - - //Now translate back again - tmpBond.translate(centre[0], centre[1], centre[2]); - } - } - - objmat = null; - - omx = mx; - omy = my; - - redrawneeded = true; - - paint(this.getGraphics()); - - dragging = true; - - return; - } - - public void mouseReleased(MouseEvent evt) { - //int x = evt.getX(); - //int y = evt.getY(); - - //if (!dragging) { - // myAtom tmp = findAtom(x, y); - //} - - drawLabels(); - - return; - } - - public void drawLabels() { - redrawneeded = true; - paint(this.getGraphics()); - - for (int ii = 0; ii < pdb.chains.size(); ii++) { - PDBChain chain = (PDBChain) pdb.chains.elementAt(ii); - - if (chain.isVisible) { - Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; - - for (int i = 0; i < bonds.size(); i++) { - Bond tmpBond = (Bond) bonds.elementAt(i); - - if (tmpBond.at1.isSelected) { - labelAtom(img.getGraphics(), tmpBond, 1); - } - - if (tmpBond.at2.isSelected) { - labelAtom(img.getGraphics(), tmpBond, 2); - } - } - } - } - - this.getGraphics().drawImage(img, 0, 0, this); - - dragging = false; - } - - public void labelAtom(Graphics g, Bond b, int n) { - g.setFont(font); - - if (n == 1) { - int xstart = (int) (((b.start[0] - centre[0]) * scale) + - (getWidth() / 2)); - int ystart = (int) (((b.start[1] - centre[1]) * scale) + - (getHeight() / 2)); - - g.setColor(Color.red); - g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart); - } - - if (n == 2) { - int xstart = (int) (((b.end[0] - centre[0]) * scale) + - (getWidth() / 2)); - int ystart = (int) (((b.end[1] - centre[1]) * scale) + - (getHeight() / 2)); - - g.setColor(Color.red); - g.drawString(b.at2.resName + "-" + b.at2.resNumber, xstart, ystart); - } - } - - public myAtom findAtom(int x, int y) { - myAtom fatom = null; - - int foundchain = -1; - - for (int ii = 0; ii < pdb.chains.size(); ii++) { - PDBChain chain = (PDBChain) pdb.chains.elementAt(ii); - - if (chain.isVisible) { - Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; - - for (int i = 0; i < bonds.size(); i++) { - Bond tmpBond = (Bond) bonds.elementAt(i); - - int truex = (int) (((tmpBond.start[0] - centre[0]) * scale) + - (getWidth() / 2)); - - if (Math.abs(truex - x) <= 2) { - int truey = (int) (((tmpBond.start[1] - centre[1]) * scale) + - (getHeight() / 2)); - - if (Math.abs(truey - y) <= 2) { - System.out.println("Found match"); - System.out.println(x + " " + y); - System.out.println(truex + " " + truey); - System.out.println(tmpBond.start[0] + " " + - tmpBond.start[1]); - System.out.println("Atom 1 = " + - tmpBond.at1.resName + " " + - tmpBond.at1.resNumber + " " + - tmpBond.at1.chain); - fatom = tmpBond.at1; - fatom.isSelected = !fatom.isSelected; - foundchain = ii; - } - } - } - } - - if (fatom != null) //)&& chain.ds != null) - { - chain = (PDBChain) pdb.chains.elementAt(foundchain); - - // SMJS TODO - // int tmp = chain.ds.seqstart + fatom.resNumber - chain.offset; - // int pos = chain.ds.findIndex(tmp); - // System.out.println("Found seq " + chain.ds.name + " " + tmp + " " + pos); - } - } - - return fatom; - } - - public void update(Graphics g) { - paint(g); - } -} -- 1.7.10.2