From 001bbb88f11b8a6cb2fb67aa30a64fe15ae454fb Mon Sep 17 00:00:00 2001 From: amwaterhouse Date: Fri, 25 Nov 2005 17:56:20 +0000 Subject: [PATCH] Read in Hetatms, allow for models, highlight in canvas --- src/MCview/Atom.java | 5 +- src/MCview/PDBCanvas.java | 282 +++++++++++++++++++++++++++++++-------------- src/MCview/PDBChain.java | 38 +++--- src/MCview/PDBViewer.java | 13 ++- src/MCview/PDBfile.java | 25 +++- src/MCview/Residue.java | 9 +- 6 files changed, 253 insertions(+), 119 deletions(-) diff --git a/src/MCview/Atom.java b/src/MCview/Atom.java index 0815dc0..6ae2baa 100755 --- a/src/MCview/Atom.java +++ b/src/MCview/Atom.java @@ -20,8 +20,6 @@ package MCview; import java.awt.*; -import java.util.*; - public class Atom { float x; @@ -39,7 +37,7 @@ public class Atom { public Atom(String str) { - name = str.substring(12,16); + name = str.substring(12,15).trim(); resName = str.substring(17,20); @@ -50,6 +48,7 @@ public class Atom { 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()); + } public void setColor(Color col) { diff --git a/src/MCview/PDBCanvas.java b/src/MCview/PDBCanvas.java index 5ff0f1f..b3eb23f 100755 --- a/src/MCview/PDBCanvas.java +++ b/src/MCview/PDBCanvas.java @@ -66,33 +66,29 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe int xmid; int ymid; Font font = new Font("Helvetica", Font.PLAIN, 10); - jalview.gui.SequenceRenderer sr; - jalview.gui.FeatureRenderer fr; 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; - sr = seqcanvas.getSequenceRenderer(); - fr = seqcanvas.getFeatureRenderer(); - seqcanvas.setPDBCanvas(this); } public void setPDBFile(PDBfile pdb) { - this.sr = sr; - this.fr = fr; int max = -10; int maxchain = -1; int pdbstart = 0; int pdbend = 0; int seqstart = 0; int seqend = 0; + int [] mapping = null; for (int i = 0; i < pdb.chains.size(); i++) { @@ -120,14 +116,17 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe as.printAlignment(ps); - if (as.maxscore > max) { + + + 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; + mapping = as.getExactMapping(); } mappingDetails.append("\nPDB start/end " + pdbstart + " " + pdbend); @@ -141,9 +140,10 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe mainchain.seqstart = seqstart; mainchain.seqend = seqend; mainchain.isVisible = true; - mainchain.sequence = sequence; + mainchain.seqMapping = mapping; + // mainchain.sequence = sequence; - this.pdb = pdb; + this.pdb = pdb; this.prefsize = new Dimension(getWidth(), getHeight()); //Initialize the matrices to identity @@ -195,6 +195,8 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe findCentre(); findWidth(); + setupBonds(); + scale = findScale(); @@ -202,10 +204,29 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe 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)); + } + } + } } - public void deleteBonds() { + public void deleteBonds() { scale = 0; maxwidth = 0; @@ -367,8 +388,8 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe centre[2] = ztot / (2 * (float) bsize); } - public void paintComponent(Graphics g) { - + public void paintComponent(Graphics g) + { super.paintComponent(g); if(pdb==null) @@ -382,8 +403,11 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe //Only create the image at the beginning - //this saves much memory usage - if ((img == null) || (prefsize.width != getWidth()) || - (prefsize.height != getHeight())) { + if ((img == null) + || (prefsize.width != getWidth()) + || (prefsize.height != getHeight())) + + { prefsize.width = getWidth(); prefsize.height = getHeight(); @@ -395,7 +419,6 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe ig2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); - redrawneeded = true; } @@ -407,6 +430,8 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe } g.drawImage(img, 0, 0, this); + + pdbAction = false; } public void drawAll(Graphics g, int width, int height) @@ -420,6 +445,12 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe 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++) @@ -430,77 +461,69 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe redrawneeded=true; repaint(); - } // 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 ( (tmp.at1.resNumber >= ( (chain.offset + chain.pdbstart) - 1)) && (tmp.at1.resNumber <= ( (chain.offset + chain.pdbend) - 1))) { - int pos = chain.seqstart + - (tmp.at1.resNumber - chain.pdbstart - chain.offset); - - int index = sequence.findIndex(pos); - tmp.startCol = sr.findSequenceColour(Color.lightGray, sequence, index); + int pos = // chain.seqstart + Don't include seqstart here, start from 0 + (tmp.at1.resNumber - chain.pdbstart - chain.offset)+1; - tmp.startCol = fr.findFeatureColour(tmp.startCol, sequence, index); + int index = chain.seqMapping[pos]; + if (index != -1) + { + tmp.startCol = seqcanvas.getSequenceRenderer().findSequenceColour( + Color.lightGray, sequence, index); - } - else - { - tmp.startCol = Color.gray; + 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 pos = chain.seqstart + - (tmp.at2.resNumber - chain.pdbstart - chain.offset); - int index = sequence.findIndex(pos); + int pos = // chain.seqstart + Don't include seqstart here, start from 0 + (tmp.at2.resNumber - chain.pdbstart - chain.offset)+1; - tmp.endCol = sr.findSequenceColour(tmp.endCol, sequence, index); - tmp.endCol = fr.findFeatureColour(tmp.endCol, sequence, index); - } - else - { - tmp.endCol = Color.gray; + int index = chain.seqMapping[pos]; + if (index != -1) + { + tmp.endCol = seqcanvas.getSequenceRenderer().findSequenceColour( + tmp.endCol, sequence, index); + tmp.endCol = seqcanvas.getFeatureRenderer().findFeatureColour(tmp. + endCol, sequence, index); + } } } } - 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; + public void drawScene(Graphics g) + { - for (int i = 0; i < tmp.size(); i++) - { - bonds.addElement(tmp.elementAt(i)); - } - } - } - if (zbuffer) { - Zsort.Zsort(bonds); + if (zbuffer) + { + Zsort.Zsort(visiblebonds); } Bond tmpBond=null; - for (int i = 0; i < bonds.size(); i++) { - tmpBond = (Bond) bonds.elementAt(i); + for (int i = 0; i < visiblebonds.size(); i++) + { + tmpBond = (Bond) visiblebonds.elementAt(i); xstart = (int) (((tmpBond.start[0] - centre[0]) * scale) + (getWidth() / 2)); @@ -515,7 +538,8 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe xmid = (xend + xstart) / 2; ymid = (yend + ystart) / 2; - if (depthcue && !bymolecule) { + if (depthcue && !bymolecule) + { if (tmpBond.start[2] < (centre[2] - (maxwidth / 6))) { g.setColor(tmpBond.startCol.darker().darker()); drawLine(g, xstart, ystart, xmid, ymid); @@ -613,13 +637,37 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe } } - public void mousePressed(MouseEvent e) { + 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) + { + int pos = // chain.seqstart + Don't include seqstart here, start from 0 + (fatom.resNumber - chain.pdbstart - chain.offset) +1 ; + + if (chain.seqMapping[pos] != -1) + { + if (highlightRes == null) + highlightRes = new Vector(); + + if (highlightRes.contains((chain.seqMapping[pos]) + "")) + highlightRes.remove((chain.seqMapping[pos]) + ""); + else + highlightRes.add((chain.seqMapping[pos]) + ""); + } + } + } + } mx = e.getX(); my = e.getY(); @@ -628,7 +676,16 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe dragging = false; } - public void mouseMoved(MouseEvent e) { + 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()); @@ -638,43 +695,72 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe chain = (PDBChain) pdb.chains.elementAt(foundchain); if(chain == mainchain) { - int pos = chain.seqstart + - (fatom.resNumber - chain.pdbstart - chain.offset) + 1; - - int index = seqcanvas.getViewport().getAlignment().findIndex( - sequence); + int pos = // chain.seqstart + Don't include seqstart here, start from 0 + (fatom.resNumber - chain.pdbstart - chain.offset) +1 ; - seqcanvas.highlightSearchResults(new int[] - {index, pos, pos}); + highlightSeqcanvas( chain.seqMapping[pos] ); } } - else - seqcanvas.highlightSearchResults(null); if (fatom != null) { this.setToolTipText(chain.id+":"+ fatom.resNumber+" "+ fatom.resName); } else { - this.setToolTipText(""); + highlightSeqcanvas( -1); + this.setToolTipText(null); } } - public void mouseClicked(MouseEvent e) { - } - public void mouseEntered(MouseEvent e) { - } + void highlightSeqcanvas(int pos) + { + int index = seqcanvas.getViewport().getAlignment().findIndex(sequence); - public void mouseExited(MouseEvent e) { + 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 mouseDragged(MouseEvent evt) { + + 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(); @@ -716,7 +802,8 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe repaint(); } - public void mouseReleased(MouseEvent evt) { + public void mouseReleased(MouseEvent evt) + { dragging = false; return; } @@ -729,20 +816,17 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe if (chain.isVisible) { - Vector bonds = ((PDBChain) pdb.chains.elementAt(ii)).bonds; - for (int i = 0; i < bonds.size(); i++) + for (int i = 0; i < chain.bonds.size(); i++) { - Bond tmpBond = (Bond) bonds.elementAt(i); + Bond tmpBond = (Bond) chain.bonds.elementAt(i); if (tmpBond.at1.isSelected) { labelAtom(g, tmpBond, 1); } - - if (tmpBond.at2.isSelected) + else if (tmpBond.at2.isSelected) { - labelAtom(g, tmpBond, 2); } } @@ -752,14 +836,14 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe public void labelAtom(Graphics g, Bond b, int n) { g.setFont(font); - - if (n == 1) { + 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.setColor(Color.red); g.drawString(b.at1.resName + "-" + b.at1.resNumber, xstart, ystart); } @@ -769,7 +853,6 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe 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); } } @@ -841,12 +924,28 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe } Bond highlightBond1, highlightBond2; + String alignMouseOver; public void highlightRes(int ii) { + + int index = ii - mainchain.seqstart; + + if (highlightRes!=null + && highlightRes.contains( mainchain.seqMapping[index]+ "")) + { + return; + } + + if(highlightBond1!=null) + highlightBond1.at2.isSelected = false; + + if(highlightBond2!=null) + highlightBond2.at1.isSelected = false; + highlightBond1 = null; highlightBond2 = null; - int index = ii - mainchain.seqstart; + if(index <0 ) return; @@ -856,10 +955,16 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe if(index>0) { highlightBond1 = (Bond) mainchain.bonds.elementAt(index - 1); + highlightBond1.at2.isSelected = true; + // highlightBond1.at2.isSelected = true; } if(index!=mainchain.bonds.size()) + { highlightBond2 = (Bond) mainchain.bonds.elementAt(index); + highlightBond2.at1.isSelected = true; + // highlightBond2.at2.isSelected = true; + } } redrawneeded = true; @@ -875,5 +980,8 @@ public class PDBCanvas extends JPanel implements MouseListener, MouseMotionListe } mainchain.isVisible = true; findCentre(); + setupBonds(); + redrawneeded = true; + repaint(); } } diff --git a/src/MCview/PDBChain.java b/src/MCview/PDBChain.java index 43c8e9d..3cf73d1 100755 --- a/src/MCview/PDBChain.java +++ b/src/MCview/PDBChain.java @@ -39,6 +39,7 @@ public class PDBChain { public int pdbend = 0; public int seqstart = 0; public int seqend = 0; + public int [] seqMapping; //public DrawableSequence ds; public PDBChain(String id) { @@ -57,15 +58,19 @@ public class PDBChain { return tmp; } - public void makeCaBondList() { - for (int i = 0; i < (residues.size() - 1); i++) { + public void makeCaBondList() + { + for (int i = 0; i < (residues.size() - 1); i++) + { Residue tmpres = (Residue) residues.elementAt(i); Residue tmpres2 = (Residue) residues.elementAt(i + 1); Atom at1 = tmpres.findAtom("CA"); Atom at2 = tmpres2.findAtom("CA"); - if ((at1 != null) && (at2 != null)) { - if (at1.chain.equals(at2.chain)) { + if ((at1 != null) && (at2 != null)) + { + if (at1.chain.equals(at2.chain)) + { makeBond(at1, at2); } } @@ -89,11 +94,12 @@ public class PDBChain { public void makeResidueList() { int count = 0; - String seq = ""; + StringBuffer seq = new StringBuffer(); - for (int i = 0; i < atoms.size(); i++) { + int i, iSize = atoms.size()-1; + for (i = 0; i < iSize; i++) + { Atom tmp = (Atom) atoms.elementAt(i); - //String resName = tmp.resName; int resNumber = tmp.resNumber; int res = resNumber; @@ -131,21 +137,23 @@ public class PDBChain { Atom tmpat = (Atom) tmpres.atoms.elementAt(0); // Keep totting up the sequence - if (ResidueProperties.getAA3Hash().get(tmpat.resName) == null) { - System.err.println("PDBReader:Null aa3Hash for " + - tmpat.resName); + if (ResidueProperties.getAA3Hash().get(tmpat.resName) == null) + { + seq.append("X") ; + // System.err.println("PDBReader:Null aa3Hash for " + + // tmpat.resName); } else { - String tmpres2 = ResidueProperties.aa[((Integer) ResidueProperties.getAA3Hash() - .get(tmpat.resName)).intValue()]; - seq = seq + tmpres2; + + seq.append(ResidueProperties.aa[((Integer) ResidueProperties.getAA3Hash() + .get(tmpat.resName)).intValue()]); } } if(id.length()<1 || id.equals(" ")) id = "_"; - sequence = new Sequence(id, seq, 1, seq.length()); - // System.out.println("PDB Sequence is :\nSequence = " + seq); + sequence = new Sequence(id, seq.toString(), 1, seq.length()); + // System.out.println("PDB Sequence is :\nSequence = " + seq); // System.out.println("No of residues = " + residues.size()); } diff --git a/src/MCview/PDBViewer.java b/src/MCview/PDBViewer.java index 06b6f65..082ce6e 100755 --- a/src/MCview/PDBViewer.java +++ b/src/MCview/PDBViewer.java @@ -19,9 +19,13 @@ public class PDBViewer extends JInternalFrame implements Runnable Sequence seq, SeqCanvas seqcanvas) { + pdb = entry; sequence = seq; + Thread worker = new Thread(this); + worker.start(); + try { jbInit(); @@ -42,10 +46,8 @@ public class PDBViewer extends JInternalFrame implements Runnable title.append( " Chain:" ); title.append( pdb.getProperty().get("chains")); } - Desktop.addInternalFrame(this,title.toString(),400, 400); - Thread worker = new Thread(this); - worker.start(); + Desktop.addInternalFrame(this,title.toString(),400, 400); } public void run() @@ -53,8 +55,7 @@ public class PDBViewer extends JInternalFrame implements Runnable try { EBIFetchClient ebi = new EBIFetchClient(); - String[] result = ebi.fetchData("pdb:" + pdb.getId(), null, - null); + String[] result = ebi.fetchData("pdb:" + pdb.getId(), "default","raw"); PDBfile pdbfile = new PDBfile(result); @@ -337,7 +338,7 @@ public class PDBViewer extends JInternalFrame implements Runnable public void mapping_actionPerformed(ActionEvent e) { jalview.gui.CutAndPasteTransfer cap = new jalview.gui.CutAndPasteTransfer(); - Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 500, 600); + Desktop.addInternalFrame(cap, "PDB - Sequence Mapping", 550, 600); cap.setText(pdbcanvas.mappingDetails.toString()); } diff --git a/src/MCview/PDBfile.java b/src/MCview/PDBfile.java index 2b22b24..c4fe563 100755 --- a/src/MCview/PDBfile.java +++ b/src/MCview/PDBfile.java @@ -71,7 +71,9 @@ public class PDBfile extends jalview.io.FileParse { public void parse() { + PDBChain tmpchain; String line; + boolean modelFlag = false; for (int i = 0; i < lineArray.size(); i++) { @@ -83,16 +85,29 @@ public class PDBfile extends jalview.io.FileParse { continue; } - if (line.indexOf("ATOM") == 0) { + if(line.indexOf("MODEL")==0) + modelFlag = true; + + if(modelFlag && line.indexOf("ENDMDL")==0) + break; + + if (line.indexOf("ATOM")==0 || line.indexOf("HETATM")==0 ) + { try { Atom tmpatom = new Atom(line); - if (findChain(tmpatom.chain) != null) + //Jalview is only interested in CA bonds???? + if(!tmpatom.name.equals("CA")) + continue; + + tmpchain = findChain(tmpatom.chain); + if ( tmpchain != null) { - findChain(tmpatom.chain).atoms.addElement(tmpatom); - } else + tmpchain.atoms.addElement(tmpatom); + } + else { - PDBChain tmpchain = new PDBChain(tmpatom.chain); + tmpchain = new PDBChain(tmpatom.chain); chains.addElement(tmpchain); tmpchain.atoms.addElement(tmpatom); } diff --git a/src/MCview/Residue.java b/src/MCview/Residue.java index efaf1f0..0f24137 100755 --- a/src/MCview/Residue.java +++ b/src/MCview/Residue.java @@ -33,9 +33,12 @@ public class Residue { this.count = count; } - public Atom findAtom(String name) { - for (int i = 0; i < atoms.size(); i++) { - if (((Atom) atoms.elementAt(i)).name.equals(name)) { + public Atom findAtom(String name) + { + for (int i = 0; i < atoms.size(); i++) + { + if (((Atom) atoms.elementAt(i)).name.equals(name)) + { return (Atom) atoms.elementAt(i); } } -- 1.7.10.2