From 9b22c11a0e12a9320e41eca2cbcbe30f81eb1cb4 Mon Sep 17 00:00:00 2001 From: amwaterhouse Date: Thu, 18 Nov 2004 18:43:47 +0000 Subject: [PATCH] New files --- src/MCview/Atom.java | 52 ++++ src/MCview/Bond.java | 65 +++++ src/MCview/Bond.jn | 57 ++++ src/MCview/Bond.jsave | 57 ++++ src/MCview/MCMatrix.java | 143 ++++++++++ src/MCview/PDBChain.java | 261 ++++++++++++++++++ src/MCview/PDBfile.java | 137 ++++++++++ src/MCview/Residue.java | 28 ++ src/MCview/Zsort.java | 50 ++++ src/MCview/myAtom.java | 55 ++++ src/MCview/rotCanvas.java | 655 +++++++++++++++++++++++++++++++++++++++++++++ 11 files changed, 1560 insertions(+) create mode 100755 src/MCview/Atom.java create mode 100755 src/MCview/Bond.java create mode 100755 src/MCview/Bond.jn create mode 100755 src/MCview/Bond.jsave create mode 100755 src/MCview/MCMatrix.java create mode 100755 src/MCview/PDBChain.java create mode 100755 src/MCview/PDBfile.java create mode 100755 src/MCview/Residue.java create mode 100755 src/MCview/Zsort.java create mode 100755 src/MCview/myAtom.java create mode 100755 src/MCview/rotCanvas.java diff --git a/src/MCview/Atom.java b/src/MCview/Atom.java new file mode 100755 index 0000000..c89d489 --- /dev/null +++ b/src/MCview/Atom.java @@ -0,0 +1,52 @@ +package MCview; +import java.util.*; +import java.awt.*; + +public class Atom { + double x; + double y; + double z; + int number; + String name; + String resName; + int resNumber; + int type; + Color color; + String chain; + + public Atom(StringTokenizer str) { + this.number = (new Integer(str.nextToken())).intValue(); + this.name = str.nextToken(); + this.resName = str.nextToken(); + + String tmpstr = new String(); + + try { + tmpstr = str.nextToken(); + + this.resNumber = (new Integer(tmpstr).intValue()); + this.chain = "A"; + this.color = Color.green; + + } catch(NumberFormatException e) { + this.chain = tmpstr; + + if (tmpstr.equals("A")) { + this.color = new Color((float)Math.random(),(float)Math.random(),(float)Math.random()); + } else { + this.color = Color.red; + } + this.resNumber = (new Integer(str.nextToken()).intValue()); + } + + 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) { + this.color = col; + } + +} diff --git a/src/MCview/Bond.java b/src/MCview/Bond.java new file mode 100755 index 0000000..60b2f21 --- /dev/null +++ b/src/MCview/Bond.java @@ -0,0 +1,65 @@ +package MCview; + +import java.awt.*; + +public class Bond { + + float start[]; + float end[]; + + Color startCol; + Color endCol; + + public myAtom at1; + public myAtom at2; + + public Bond(float[] start, float[] end, myAtom at1, myAtom at2) { + this.start = start; + this.end = end; + this.startCol = at1.color; + this.endCol = at2.color; + this.at1 = at1; + this.at2 = at2; + } + public Bond(Bond bond) { + this.start = new float[3]; + + this.start[0] = bond.start[0]; + this.start[1] = bond.start[1]; + this.start[2] = bond.start[2]; + + this.end = new float[3]; + + this.end[0] = bond.end[0]; + this.end[1] = bond.end[1]; + this.end[2] = bond.end[2]; + + this.startCol = bond.startCol; + 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]) + + (end[2] - start[2])*(end[2] - start[2]); + + len = (float)(Math.sqrt(len)); + + return len; + } + public void translate(float x, float y, float z) { + start[0] = (start[0] + x); + end[0] = (end[0] + x); + + start[1] = (start[1] + y); + end[1] = (end[1] + y); + + start[2] = (start[2] + z); + end[2] = (end[2] + z); + } +} diff --git a/src/MCview/Bond.jn b/src/MCview/Bond.jn new file mode 100755 index 0000000..b52e445 --- /dev/null +++ b/src/MCview/Bond.jn @@ -0,0 +1,57 @@ +package MCview; + +import java.awt.*; + +public class Bond { + + float start[]; + float end[]; + + Color startCol; + Color endCol; + + public myAtom at1; + public myAtom at2; + + public Bond(float[] start, float[] end, myAtom at1, myAtom at2) { + this.start = start; + this.end = end; + this.startCol = at1.color; + this.endCol = at2.color; + this.at1 = at1; + this.at2 = at2; + } + public Bond(Bond bond) { + this.start = new float[3]; + this.start[0] = bond.start[0]; + this.start[1] = bond.start[1]; + this.start[2] = bond.start[2]; + this.end = new float[3]; + this.end[0] = bond.end[0]; + this.end[1] = bond.end[1]; + this.end[2] = bond.end[2]; + this.startCol = bond.startCol; + 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]) + + (end[2] - start[2])*(end[2] - start[2]); + len = (float)(Math.sqrt(len)); + return len; + } + public void translate(float x, float y, float z) { + start[0] = (start[0] + x); + end[0] = (end[0] + x); + start[1] =(start[1] + y); + end[1] = (end[1] + y); + start[2] = (start[2] + z); + end[2] = (end[2] + z); + } +} diff --git a/src/MCview/Bond.jsave b/src/MCview/Bond.jsave new file mode 100755 index 0000000..0d0e560 --- /dev/null +++ b/src/MCview/Bond.jsave @@ -0,0 +1,57 @@ +package MCview; + +import java.awt.*; + +public class Bond { + + float start[]; + float end[]; + + Color startCol; + Color endCol; + + public myAtom at1; + public myAtom at2; + + public Bond(float[] start, float[] end, myAtom at1, myAtom at2) { + this.start = start; + this.end = end; + this.startCol = at1.color; + this.endCol = at2.color; + this.at1 = at1; + this.at2 = at2; + } + public Bond(Bond bond) { + this.start = new float[3]; + this.start[0] = bond.start[0]; + this.start[1] = bond.start[1]; + this.start[2] = bond.start[2]; + this.end = new float[3]; + this.end[0] = bond.end[0]; + this.end[1] = bond.end[1]; + this.end[2] = bond.end[2]; + this.startCol = bond.startCol; + 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]) + + (end[2] - start[2])*(end[2] - start[2]); + len = (float)(Math.sqrt(len)); + return len; + } + public void translate(float x, float y, float z) { + start[0] = (start[0] + x); + end[0] = (end[0] + x); + start[1] =(start[1] + y); + end[1] = (end[1] + y); + start[2] = (start[2] + z); + end[2] = (end[2] + z); + } +} diff --git a/src/MCview/MCMatrix.java b/src/MCview/MCMatrix.java new file mode 100755 index 0000000..4994573 --- /dev/null +++ b/src/MCview/MCMatrix.java @@ -0,0 +1,143 @@ +package MCview; + +public class MCMatrix { + float matrix[][]; + float tmp[][]; + + float mycos; + float mysin; + float myconst = (float)(Math.PI/180); + + public MCMatrix(int rows, int cols) { + matrix = new float[rows][cols]; + tmp = new float[rows][cols]; + } + + public void addElement(int i, int j, float value) { + 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)); + + tmp[0][0] = 1; + tmp[0][1] = 0; + tmp[0][2] = 0; + tmp[1][0] = 0; + tmp[1][1] = mycos; + tmp[1][2] = mysin; + tmp[2][0] = 0; + tmp[2][1] = -mysin; + tmp[2][2] = mycos; + preMultiply(tmp); + } + + public void rotatez(float degrees) { + mycos = (float)(Math.cos(degrees*myconst)); + mysin = (float)(Math.sin(degrees*myconst)); + + tmp[0][0] = mycos; + tmp[0][1] = -mysin; + tmp[0][2] = 0; + tmp[1][0] = mysin; + tmp[1][1] = mycos; + tmp[1][2] = 0; + tmp[2][0] = 0; + tmp[2][1] = 0; + tmp[2][2] = 1; + + preMultiply(tmp); + } + + public void rotatey(float degrees) { + mycos = (float)(Math.cos(degrees*myconst)); + mysin = (float)(Math.sin(degrees*myconst)); + + tmp[0][0] = mycos; + tmp[0][1] = 0; + tmp[0][2] = -mysin; + tmp[1][0] = 0; + tmp[1][1] = 1; + tmp[1][2] = 0; + tmp[2][0] = mysin; + tmp[2][1] = 0; + tmp[2][2] = mycos; + + preMultiply(tmp); + } + + public float[] vectorMultiply(float[] vect) { + float temp[] = new float[3]; + + temp[0] = vect[0]; + temp[1] = vect[1]; + temp[2] = vect[2]; + + for (int i = 0; i < 3; i++) { + temp[i] = (float)matrix[i][0]*vect[0] + (float)matrix[i][1]*vect[1] +(float)matrix[i][2]*vect[2]; + } + + vect[0] = temp[0]; + vect[1] = temp[1]; + vect[2] = temp[2]; + + return vect; + } + + public void preMultiply(float mat[][]) { + float tmp[][] = new float[3][3]; + + for (int i = 0; i < 3 ; i++) { + for (int j = 0; j < 3; j++ ) { + tmp[i][j] = mat[i][0]*matrix[0][j] + + mat[i][1]*matrix[1][j] + + mat[i][2]*matrix[2][j]; + } + } + + for (int i = 0; i < 3 ; i++) { + for (int j = 0; j < 3; j++ ) { + matrix[i][j] = tmp[i][j]; + } + } + } + + public void postMultiply(float mat[][]) { + float tmp[][] = new float[3][3]; + + for (int i = 0; i < 3 ; i++) { + for (int j = 0; j < 3; j++ ) { + tmp[i][j] = matrix[i][0]*mat[0][j] + + matrix[i][1]*mat[1][j] + + matrix[i][2]*mat[2][j]; + } + } + + for (int i = 0; i < 3 ; i++) { + for (int j = 0; j < 3; j++ ) { + matrix[i][j] = tmp[i][j]; + } + } + } + + public void setIdentity() { + matrix[0][0] = 1; + matrix[1][1] = 1; + matrix[2][2] = 1; + matrix[0][1] = 0; + matrix[0][2] = 0; + matrix[1][0] = 0; + matrix[1][2] = 0; + matrix[2][0] = 0; + matrix[2][1] = 0; + } +} + + diff --git a/src/MCview/PDBChain.java b/src/MCview/PDBChain.java new file mode 100755 index 0000000..6a72786 --- /dev/null +++ b/src/MCview/PDBChain.java @@ -0,0 +1,261 @@ +/* Copyright (C) 1998 Michele Clamp + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ +package MCview; + +import jalview.datamodel.*; +import jalview.schemes.ResidueProperties; +import jalview.gui.*; + +import java.awt.*; +import java.util.*; + + +public class PDBChain { + + public String id; + public Vector bonds = new Vector(); + public Vector atoms = new Vector(); + public Vector residues = new Vector(); + public int offset; + + public Sequence sequence; + public boolean isVisible = false; + + //public DrawableSequence ds; + + public PDBChain(String id) { + this.id = id; + } + + + public String print() { + String tmp = ""; + for (int i=0; i < bonds.size() ;i++) { + tmp = tmp + ((Bond)bonds.elementAt(i)).at1.resName + " " + ((Bond)bonds.elementAt(i)).at1.resNumber +" " + offset+ "\n"; + } + return tmp; + } + 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); + myAtom at1 = tmpres.findAtom("CA"); + myAtom at2 = tmpres2.findAtom("CA"); + if ((at1 != null) && (at2 != null)) { + if (at1.chain.equals(at2.chain)) { + makeBond(at1,at2); + } + } + } + } + + public void makeBond(myAtom at1, myAtom at2) { + float[] start = new float[3]; + float[] end = new float[3]; + + start[0] = at1.x; + start[1] = at1.y; + start[2] = at1.z; + + end[0] = at2.x; + end[1] = at2.y; + end[2] = at2.z; + + bonds.addElement(new Bond(start, end, at1,at2)); + } + + public void makeResidueList() { + int count = 0; + String seq = ""; + for (int i = 0; i < atoms.size(); i++) { + + myAtom tmp = (myAtom)atoms.elementAt(i); + String resName = tmp.resName; + int resNumber = tmp.resNumber; + int res = resNumber; + + if (i ==0) { + offset = resNumber; + } + Vector resAtoms = new Vector(); + + resAtoms.addElement((myAtom)atoms.elementAt(i)); + i++; + resNumber = ((myAtom)atoms.elementAt(i)).resNumber; + + //Add atoms to a vector while the residue number + //remains the same + while ((resNumber == res) && (i < atoms.size())) { + + resAtoms.addElement((myAtom)atoms.elementAt(i)); + i++; + if (i < atoms.size()) { + resNumber = ((myAtom)atoms.elementAt(i)).resNumber; + } else { + resNumber++; + } + } + + //We need this to keep in step with the outer for i = loop + i--; + + //Make a new Residue object with the new atoms vector + residues.addElement(new Residue(resAtoms, resNumber - 1,count)); + count++; + Residue tmpres = (Residue)residues.lastElement(); + myAtom tmpat = (myAtom)tmpres.atoms.elementAt(0); + + // Keep totting up the sequence + if (ResidueProperties.getAA3Hash().get(tmpat.resName) == null) { + System.out.println("Null aa3Hash for " + tmpat.resName); + } else { + String tmpres2 = + ResidueProperties.aa[((Integer)ResidueProperties.getAA3Hash().get(tmpat.resName)).intValue()]; + seq = seq + tmpres2; + } + // System.out.println(tmpat.resName + " " + tmpres2); + } + sequence = new Sequence("PDB_seq",seq,1,seq.length()); + System.out.println("Sequence = " + seq); + System.out.println("No of residues = " +residues.size()); + } + + public void setChargeColours() { + for (int i = 0; i < bonds.size(); i++) { + try { + Bond b = (Bond)bonds.elementAt(i); + + if (b.at1.resName.toUpperCase().equals("ASP") || b.at1.resName.toUpperCase().equals("GLU")) { + b.startCol = Color.red; + } else if (b.at1.resName.toUpperCase().equals("LYS") || b.at1.resName.toUpperCase().equals("ARG")) { + b.startCol = Color.blue; + } else if (b.at1.resName.toUpperCase().equals("CYS")) { + b.startCol = Color.yellow; + } else { + int atno = ((Integer)ResidueProperties.getAA3Hash().get(b.at1.resName.toUpperCase())).intValue(); + b.startCol = Color.lightGray; + } + if (b.at2.resName.toUpperCase().equals("ASP") || b.at2.resName.toUpperCase().equals("GLU")) { + b.endCol = Color.red; + } else if (b.at2.resName.toUpperCase().equals("LYS") || b.at2.resName.toUpperCase().equals("ARG")) { + b.endCol = Color.blue; + } else if (b.at2.resName.toUpperCase().equals("CYS")) { + b.endCol = Color.yellow; + } else { + int atno = ((Integer)ResidueProperties.getAA3Hash().get(b.at2.resName.toUpperCase())).intValue(); + b.endCol = Color.lightGray; + } + } catch (Exception e) { + Bond b = (Bond)bonds.elementAt(i); + b.startCol = Color.gray; + b.endCol = Color.gray; + } + } + } + + public void setHydrophobicityColours() { + float hydmin = (float)ResidueProperties.getHydmin(); + float hydmax = (float)ResidueProperties.getHydmax(); + double[] hyd = ResidueProperties.getHyd(); + + Hashtable AA3Hash = ResidueProperties.getAA3Hash(); + + for (int i = 0; i < bonds.size(); i++) { + try { + Bond b = (Bond)bonds.elementAt(i); + + int atno = ((Integer)AA3Hash.get(b.at1.resName.toUpperCase())).intValue(); + float red = ((float)hyd[atno] - hydmin)/(hydmax - hydmin); + + if (red > (float)1.0) { + red = (float)1.0; + } + if (red < (float)0.0) { + red = (float)0.0; + } + + b.startCol = new Color(red,(float)0.0,(float)1.0-red); + atno = ((Integer)AA3Hash.get(b.at2.resName.toUpperCase())).intValue(); + + red = ((float)hyd[atno] - hydmin)/(hydmax - hydmin); + + if (red > (float)1.0) { + red = (float)1.0; + } + if (red < (float)0.0) { + red = (float)0.0; + } + + b.endCol = new Color(red,(float)0.2,(float)1.0-red); + } catch (Exception e) { + Bond b = (Bond)bonds.elementAt(i); + b.startCol = Color.gray; + b.endCol = Color.gray; + } + } + } + + + public void colourBySequence(DrawableSequence seq) { + +// for (int i = 0; i < bonds.size(); i++) { +// Bond tmp = (Bond)bonds.elementAt(i); +// try { +// +// if (tmp.at1.resNumber >= (offset + seq.pdbstart - 1) && tmp.at1.resNumber <= (offset + seq.pdbend - 1)) { +// +// int pos = seq.seqtart() + (tmp.at1.resNumber - seq.pdbstart - offset) ; +// int index = seq.findIndex(pos); +// +// tmp.startCol = (Color)seq.getResidueBoxColour(index); +// +// } else { +// tmp.startCol = Color.gray; +// } +// +// if (tmp.at2.resNumber >= (offset + seq.pdbstart -1) && tmp.at2.resNumber <= (seq.pdbend+offset-1)) { +// +// int pos = seq.seqstart + (tmp.at2.resNumber - seq.pdbstart-offset); +// int index = seq.findIndex(pos); +// +// tmp.endCol = (Color)seq.getResidueBoxColour(index); +// +// } else { +// tmp.endCol = Color.gray; +// } +// } catch (Exception e) { +// tmp.startCol = Color.lightGray; +// tmp.endCol = Color.lightGray; +// } +// } + } + + public void setChainColours() { + for (int i = 0; i < bonds.size(); i++) { + Bond tmp = (Bond)bonds.elementAt(i); + try { + tmp.startCol = (Color) ResidueProperties.getChainColours().get(id); + tmp.endCol = (Color) ResidueProperties.getChainColours().get(id); + } catch (Exception e) { + tmp.startCol = Color.lightGray; + tmp.endCol = Color.lightGray; + } + } + } +} + + diff --git a/src/MCview/PDBfile.java b/src/MCview/PDBfile.java new file mode 100755 index 0000000..ce98a06 --- /dev/null +++ b/src/MCview/PDBfile.java @@ -0,0 +1,137 @@ +package MCview; + +import jalview.gui.*; +import java.io.*; +import java.net.*; +import java.util.*; + + +public class PDBfile extends jalview.io.FileParse { + + public Vector chains = new Vector(); + + Vector lineArray = new Vector(); + + public PDBfile(String inFile, String inType) throws IOException { + + super(inFile,inType); + + String line; + this.lineArray = new Vector(); + BufferedReader dataIn; + + if (inType.equals("File")) + dataIn = new BufferedReader(new FileReader( inFile )); + + else + { + URL url = new URL(inFile); + this.fileSize = 0; + dataIn = new BufferedReader(new InputStreamReader(url.openStream())); + } + + while ((line = dataIn.readLine()) != null) { + lineArray.addElement(line); + } + noLines = lineArray.size(); + + parse(); + +} + + + public void parse() { + + System.out.println("Parsing"); + + for (int i = 0; i < lineArray.size(); i++) { + StringTokenizer str = new StringTokenizer(lineArray.elementAt(i).toString()); + if (str.hasMoreTokens()) { + String inStr = str.nextToken(); + + if (inStr.indexOf("ATOM") != -1) { + try { + myAtom tmpatom = new myAtom(str); + if (findChain(tmpatom.chain) != null) { + System.out.println("Adding to chain " + tmpatom.chain); + findChain(tmpatom.chain).atoms.addElement(tmpatom); + } else { + System.out.println("Making chain " + tmpatom.chain); + PDBChain tmpchain = new PDBChain(tmpatom.chain); + chains.addElement(tmpchain); + tmpchain.atoms.addElement(tmpatom); + } + } catch(NumberFormatException e) { + System.out.println("Caught" + e); + System.out.println("Atom not added"); + } + } + } + } + makeResidueList(); + makeCaBondList(); + // for (int i=0; i < chains.size() ; i++) { + // String pog = ((PDBChain)chains.elementAt(i)).print(); + // System.out.println(pog); + // } + } + + public void makeResidueList() { + for (int i=0; i < chains.size() ; i++) { + ((PDBChain)chains.elementAt(i)).makeResidueList(); + } + } + public void makeCaBondList() { + for (int i=0; i < chains.size() ; i++) { + ((PDBChain)chains.elementAt(i)).makeCaBondList(); + } + } + + public PDBChain findChain(String id) { + for (int i=0; i < chains.size(); i++) { + // System.out.println("ID = " + id + " " +((PDBChain)chains.elementAt(i)).id); + if (((PDBChain)chains.elementAt(i)).id.equals(id)) { + return (PDBChain)chains.elementAt(i); + } + } + return null; + } + + + public void setChargeColours() { + for (int i=0; i < chains.size(); i++) { + ((PDBChain)chains.elementAt(i)).setChargeColours(); + } + } + + public void setHydrophobicityColours() { + for (int i=0; i < chains.size(); i++) { + ((PDBChain)chains.elementAt(i)).setHydrophobicityColours(); + } + } + + public void colourBySequence(DrawableSequence seq) { +//SMJS TODO +// int max = seq.maxchain; +// if (seq.maxchain != -1) { +// ((PDBChain)chains.elementAt(max)).colourBySequence(seq); +// } + } + + public void setChainColours() { + for (int i=0; i < chains.size(); i++) { + ((PDBChain)chains.elementAt(i)).setChainColours(); + } + } + public static void main(String[] args) { + try { + PDBfile pdb = new PDBfile("enkp1.pdb","File"); + } catch(IOException e) { + System.out.println(e); + System.exit(0); + } + } +} + + + diff --git a/src/MCview/Residue.java b/src/MCview/Residue.java new file mode 100755 index 0000000..6e058f3 --- /dev/null +++ b/src/MCview/Residue.java @@ -0,0 +1,28 @@ +package MCview; + +import java.util.*; + + +public class Residue { + Vector atoms = new Vector(); + + int number; + int count; + int seqnumber; + + public Residue(Vector atoms, int number, int count) { + this.atoms = atoms; + this.number = number; + this.count = count; + } + + public myAtom findAtom(String name) { + + for (int i = 0; i < atoms.size(); i++) { + if (((myAtom)atoms.elementAt(i)).name.equals(name)) { + return (myAtom)atoms.elementAt(i); + } + } + return null; + } +} diff --git a/src/MCview/Zsort.java b/src/MCview/Zsort.java new file mode 100755 index 0000000..4116ff3 --- /dev/null +++ b/src/MCview/Zsort.java @@ -0,0 +1,50 @@ +package MCview; + +import java.util.*; + +public class Zsort { + + public static void Zsort(Vector bonds) { + + sort(bonds,0,bonds.size()-1); + } + + + + public static void sort(Vector bonds,int p, int r) { + int q; + + if (p < r) { + q = partition(bonds,p,r); + sort(bonds,p,q); + sort(bonds,q+1,r); + } + } + + private static int partition(Vector bonds, int p, int r) { + float x = ((Bond)bonds.elementAt(p)).start[2]; + int i = p-1; + int j = r+1; + + while(true) { + do { + j = j-1; + } while (j >= 0 && ((Bond)bonds.elementAt(j)).start[2] > x); + + do { + i = i+1; + } while (i < bonds.size() && ((Bond)bonds.elementAt(i)).start[2] < x); + + if ( i < j) { + Bond tmp = (Bond)bonds.elementAt(i); + bonds.setElementAt(bonds.elementAt(j),i); + bonds.setElementAt(tmp,j); + } else { + return j; + } + } + } +} + + + diff --git a/src/MCview/myAtom.java b/src/MCview/myAtom.java new file mode 100755 index 0000000..93166a5 --- /dev/null +++ b/src/MCview/myAtom.java @@ -0,0 +1,55 @@ +package MCview; + +import java.util.*; +import java.awt.*; + +public class myAtom { + float x; + float y; + float z; + + public int number; + public String name; + public String resName; + public int resNumber; + public int type; + public Color color; + public String chain; + public boolean isSelected = false; + + public myAtom(StringTokenizer str) { + + this.number = (new Integer(str.nextToken())).intValue(); + this.name = str.nextToken(); + this.resName = str.nextToken(); + + String tmpstr = new String(); + + try { + tmpstr = str.nextToken(); + this.resNumber = (new Integer(tmpstr).intValue()); + this.chain = "A"; + this.color = Color.lightGray; + + } catch(NumberFormatException e) { + this.chain = tmpstr; + + if (tmpstr.equals("A")) { + this.color = Color.lightGray; + + } else { + this.color = Color.red; + } + + this.resNumber = (new Integer(str.nextToken()).intValue()); + } + + this.x = (float)(new Float(str.nextToken()).floatValue()); + this.y = (float)(new Float(str.nextToken()).floatValue()); + this.z = (float)(new Float(str.nextToken()).floatValue()); + } + + public void setColor(Color col) { + this.color = col; + } +} diff --git a/src/MCview/rotCanvas.java b/src/MCview/rotCanvas.java new file mode 100755 index 0000000..4690a15 --- /dev/null +++ b/src/MCview/rotCanvas.java @@ -0,0 +1,655 @@ +package MCview; + +import java.awt.*; +import java.awt.event.*; +import java.util.*; +import java.io.*; +import javax.swing.*; + +public class rotCanvas extends JPanel implements KeyListener, + 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 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); + + public rotCanvas(PDBfile pdb) throws IOException { + + 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); + + } + + 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, width, 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) { + //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