Read in Hetatms, allow for models, highlight in canvas
[jalview.git] / src / MCview / PDBChain.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package MCview;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 import jalview.schemes.ResidueProperties;\r
24 \r
25 import java.awt.*;\r
26 \r
27 import java.util.*;\r
28 \r
29 \r
30 public class PDBChain {\r
31     public String id;\r
32     public Vector bonds = new Vector();\r
33     public Vector atoms = new Vector();\r
34     public Vector residues = new Vector();\r
35     public int offset;\r
36     public Sequence sequence;\r
37     public boolean isVisible = true;\r
38     public int pdbstart = 0;\r
39     public int pdbend = 0;\r
40     public int seqstart = 0;\r
41     public int seqend = 0;\r
42     public int [] seqMapping;\r
43 \r
44     //public DrawableSequence ds;\r
45     public PDBChain(String id) {\r
46         this.id = id;\r
47     }\r
48 \r
49     public String print() {\r
50         String tmp = "";\r
51 \r
52         for (int i = 0; i < bonds.size(); i++) {\r
53             tmp = tmp + ((Bond) bonds.elementAt(i)).at1.resName + " " +\r
54                 ((Bond) bonds.elementAt(i)).at1.resNumber + " " + offset +\r
55                 "\n";\r
56         }\r
57 \r
58         return tmp;\r
59     }\r
60 \r
61     public void makeCaBondList()\r
62     {\r
63         for (int i = 0; i < (residues.size() - 1); i++)\r
64         {\r
65             Residue tmpres = (Residue) residues.elementAt(i);\r
66             Residue tmpres2 = (Residue) residues.elementAt(i + 1);\r
67             Atom at1 = tmpres.findAtom("CA");\r
68             Atom at2 = tmpres2.findAtom("CA");\r
69 \r
70             if ((at1 != null) && (at2 != null))\r
71             {\r
72                 if (at1.chain.equals(at2.chain))\r
73                 {\r
74                     makeBond(at1, at2);\r
75                 }\r
76             }\r
77         }\r
78     }\r
79 \r
80     public void makeBond(Atom at1, Atom at2) {\r
81         float[] start = new float[3];\r
82         float[] end = new float[3];\r
83 \r
84         start[0] = at1.x;\r
85         start[1] = at1.y;\r
86         start[2] = at1.z;\r
87 \r
88         end[0] = at2.x;\r
89         end[1] = at2.y;\r
90         end[2] = at2.z;\r
91 \r
92         bonds.addElement(new Bond(start, end, at1, at2));\r
93     }\r
94 \r
95     public void makeResidueList() {\r
96         int count = 0;\r
97         StringBuffer seq = new StringBuffer();\r
98 \r
99         int i, iSize = atoms.size()-1;\r
100         for (i = 0; i < iSize; i++)\r
101         {\r
102             Atom tmp = (Atom) atoms.elementAt(i);\r
103             int resNumber = tmp.resNumber;\r
104             int res = resNumber;\r
105 \r
106             if (i == 0) {\r
107                 offset = resNumber;\r
108             }\r
109 \r
110             Vector resAtoms = new Vector();\r
111 \r
112             resAtoms.addElement((Atom) atoms.elementAt(i));\r
113             i++;\r
114             resNumber = ((Atom) atoms.elementAt(i)).resNumber;\r
115 \r
116             //Add atoms to a vector while the residue number\r
117             //remains the same\r
118             while ((resNumber == res) && (i < atoms.size())) {\r
119                 resAtoms.addElement((Atom) atoms.elementAt(i));\r
120                 i++;\r
121 \r
122                 if (i < atoms.size()) {\r
123                     resNumber = ((Atom) atoms.elementAt(i)).resNumber;\r
124                 } else {\r
125                     resNumber++;\r
126                 }\r
127             }\r
128 \r
129             //We need this to keep in step with the outer for i = loop\r
130             i--;\r
131 \r
132             //Make a new Residue object with the new atoms vector\r
133             residues.addElement(new Residue(resAtoms, resNumber - 1, count));\r
134             count++;\r
135 \r
136             Residue tmpres = (Residue) residues.lastElement();\r
137             Atom tmpat = (Atom) tmpres.atoms.elementAt(0);\r
138 \r
139             // Keep totting up the sequence\r
140             if (ResidueProperties.getAA3Hash().get(tmpat.resName) == null)\r
141             {\r
142                 seq.append("X") ;\r
143                //  System.err.println("PDBReader:Null aa3Hash for " +\r
144                //     tmpat.resName);\r
145             } else {\r
146 \r
147                 seq.append(ResidueProperties.aa[((Integer) ResidueProperties.getAA3Hash()\r
148                                                                                   .get(tmpat.resName)).intValue()]);\r
149             }\r
150         }\r
151 \r
152         if(id.length()<1 || id.equals(" "))\r
153            id = "_";\r
154 \r
155         sequence = new Sequence(id, seq.toString(), 1, seq.length());\r
156       //  System.out.println("PDB Sequence is :\nSequence = " + seq);\r
157      //   System.out.println("No of residues = " + residues.size());\r
158     }\r
159 \r
160     public void setChargeColours() {\r
161         for (int i = 0; i < bonds.size(); i++) {\r
162             try {\r
163                 Bond b = (Bond) bonds.elementAt(i);\r
164 \r
165                 if (b.at1.resName.toUpperCase().equals("ASP") ||\r
166                         b.at1.resName.toUpperCase().equals("GLU")) {\r
167                     b.startCol = Color.red;\r
168                 } else if (b.at1.resName.toUpperCase().equals("LYS") ||\r
169                         b.at1.resName.toUpperCase().equals("ARG")) {\r
170                     b.startCol = Color.blue;\r
171                 } else if (b.at1.resName.toUpperCase().equals("CYS")) {\r
172                     b.startCol = Color.yellow;\r
173                 } else {\r
174                     //int atno = ((Integer) ResidueProperties.getAA3Hash().get(b.at1.resName.toUpperCase())).intValue();\r
175                     b.startCol = Color.lightGray;\r
176                 }\r
177 \r
178                 if (b.at2.resName.toUpperCase().equals("ASP") ||\r
179                         b.at2.resName.toUpperCase().equals("GLU")) {\r
180                     b.endCol = Color.red;\r
181                 } else if (b.at2.resName.toUpperCase().equals("LYS") ||\r
182                         b.at2.resName.toUpperCase().equals("ARG")) {\r
183                     b.endCol = Color.blue;\r
184                 } else if (b.at2.resName.toUpperCase().equals("CYS")) {\r
185                     b.endCol = Color.yellow;\r
186                 } else {\r
187                     //int atno = ((Integer) ResidueProperties.getAA3Hash().get(b.at2.resName.toUpperCase())).intValue();\r
188                     b.endCol = Color.lightGray;\r
189                 }\r
190             } catch (Exception e) {\r
191                 Bond b = (Bond) bonds.elementAt(i);\r
192                 b.startCol = Color.gray;\r
193                 b.endCol = Color.gray;\r
194             }\r
195         }\r
196     }\r
197 \r
198     public void setHydrophobicityColours() {\r
199         float hydmin = (float) ResidueProperties.getHydmin();\r
200         float hydmax = (float) ResidueProperties.getHydmax();\r
201         double[] hyd = ResidueProperties.getHyd();\r
202 \r
203         Hashtable AA3Hash = ResidueProperties.getAA3Hash();\r
204 \r
205         for (int i = 0; i < bonds.size(); i++) {\r
206             try {\r
207                 Bond b = (Bond) bonds.elementAt(i);\r
208 \r
209                 int atno = ((Integer) AA3Hash.get(b.at1.resName.toUpperCase())).intValue();\r
210                 float red = ((float) hyd[atno] - hydmin) / (hydmax - hydmin);\r
211 \r
212                 if (red > (float) 1.0) {\r
213                     red = (float) 1.0;\r
214                 }\r
215 \r
216                 if (red < (float) 0.0) {\r
217                     red = (float) 0.0;\r
218                 }\r
219 \r
220                 b.startCol = new Color(red, (float) 0.0, (float) 1.0 - red);\r
221                 atno = ((Integer) AA3Hash.get(b.at2.resName.toUpperCase())).intValue();\r
222 \r
223                 red = ((float) hyd[atno] - hydmin) / (hydmax - hydmin);\r
224 \r
225                 if (red > (float) 1.0) {\r
226                     red = (float) 1.0;\r
227                 }\r
228 \r
229                 if (red < (float) 0.0) {\r
230                     red = (float) 0.0;\r
231                 }\r
232 \r
233                 b.endCol = new Color(red, (float) 0.2, (float) 1.0 - red);\r
234             } catch (Exception e) {\r
235                 Bond b = (Bond) bonds.elementAt(i);\r
236                 b.startCol = Color.gray;\r
237                 b.endCol = Color.gray;\r
238             }\r
239         }\r
240     }\r
241 \r
242 \r
243 \r
244     public void setChainColours(Color col)\r
245     {\r
246         for (int i = 0; i < bonds.size(); i++)\r
247         {\r
248             Bond tmp = (Bond) bonds.elementAt(i);\r
249             tmp.startCol = col;\r
250             tmp.endCol = col;\r
251         }\r
252     }\r
253 }\r