276525b34f4ca81bd610a15743da984675cc8299
[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 import jalview.analysis.AlignSeq;\r
29 \r
30 \r
31 public class PDBChain {\r
32     public String id;\r
33     public Vector bonds = new Vector();\r
34     public Vector atoms = new Vector();\r
35     public Vector residues = new Vector();\r
36     public int offset;\r
37     public Sequence sequence;\r
38     public boolean isVisible = true;\r
39     public int pdbstart = 0;\r
40     public int pdbend = 0;\r
41     public int seqstart = 0;\r
42     public int seqend = 0;\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     void makeExactMapping(AlignSeq as, Sequence s1)\r
62     {\r
63         int pdbpos =   as.getSeq2Start()-2;\r
64         int alignpos = s1.getStart() + as.getSeq1Start()-3;\r
65 \r
66         for(int i=0; i<as.astr1.length(); i++)\r
67         {\r
68             if (as.astr1.charAt(i) != '-')\r
69             {\r
70               alignpos++;\r
71             }\r
72 \r
73             if (as.astr2.charAt(i) != '-')\r
74             {\r
75               pdbpos++;\r
76             }\r
77 \r
78             if (as.astr1.charAt(i) == as.astr2.charAt(i))\r
79             {\r
80                 Residue res = (Residue) residues.elementAt(pdbpos);\r
81                 Enumeration en = res.atoms.elements();\r
82                 while (en.hasMoreElements())\r
83                 {\r
84                   Atom atom = (Atom) en.nextElement();\r
85                   atom.alignmentMapping = alignpos;\r
86                 }\r
87             }\r
88         }\r
89 \r
90     }\r
91 \r
92 \r
93     public void makeCaBondList()\r
94     {\r
95         for (int i = 0; i < (residues.size() - 1); i++)\r
96         {\r
97             Residue tmpres = (Residue) residues.elementAt(i);\r
98             Residue tmpres2 = (Residue) residues.elementAt(i + 1);\r
99             Atom at1 = tmpres.findAtom("CA");\r
100             Atom at2 = tmpres2.findAtom("CA");\r
101 \r
102             if ((at1 != null) && (at2 != null))\r
103             {\r
104                 if (at1.chain.equals(at2.chain))\r
105                 {\r
106                     makeBond(at1, at2);\r
107                 }\r
108             }\r
109         }\r
110     }\r
111 \r
112     public void makeBond(Atom at1, Atom at2) {\r
113         float[] start = new float[3];\r
114         float[] end = new float[3];\r
115 \r
116         start[0] = at1.x;\r
117         start[1] = at1.y;\r
118         start[2] = at1.z;\r
119 \r
120         end[0] = at2.x;\r
121         end[1] = at2.y;\r
122         end[2] = at2.z;\r
123 \r
124         bonds.addElement(new Bond(start, end, at1, at2));\r
125     }\r
126 \r
127     public void makeResidueList() {\r
128         int count = 0;\r
129         StringBuffer seq = new StringBuffer();\r
130 \r
131         int i, iSize = atoms.size()-1;\r
132         for (i = 0; i < iSize; i++)\r
133         {\r
134             Atom tmp = (Atom) atoms.elementAt(i);\r
135             int resNumber = tmp.resNumber;\r
136             int res = resNumber;\r
137 \r
138             if (i == 0) {\r
139                 offset = resNumber;\r
140             }\r
141 \r
142             Vector resAtoms = new Vector();\r
143 \r
144             resAtoms.addElement((Atom) atoms.elementAt(i));\r
145             i++;\r
146             resNumber = ((Atom) atoms.elementAt(i)).resNumber;\r
147 \r
148             //Add atoms to a vector while the residue number\r
149             //remains the same\r
150             while ((resNumber == res) && (i < atoms.size())) {\r
151                 resAtoms.addElement((Atom) atoms.elementAt(i));\r
152                 i++;\r
153 \r
154                 if (i < atoms.size()) {\r
155                     resNumber = ((Atom) atoms.elementAt(i)).resNumber;\r
156                 } else {\r
157                     resNumber++;\r
158                 }\r
159             }\r
160 \r
161             //We need this to keep in step with the outer for i = loop\r
162             i--;\r
163 \r
164             //Make a new Residue object with the new atoms vector\r
165             residues.addElement(new Residue(resAtoms, resNumber - 1, count));\r
166             count++;\r
167 \r
168             Residue tmpres = (Residue) residues.lastElement();\r
169             Atom tmpat = (Atom) tmpres.atoms.elementAt(0);\r
170 \r
171             // Keep totting up the sequence\r
172             if (ResidueProperties.getAA3Hash().get(tmpat.resName) == null)\r
173             {\r
174                 seq.append("X") ;\r
175                //  System.err.println("PDBReader:Null aa3Hash for " +\r
176                //     tmpat.resName);\r
177             } else {\r
178 \r
179                 seq.append(ResidueProperties.aa[((Integer) ResidueProperties.getAA3Hash()\r
180                                                                                   .get(tmpat.resName)).intValue()]);\r
181             }\r
182         }\r
183 \r
184         if(id.length()<1 || id.equals(" "))\r
185            id = "_";\r
186 \r
187         sequence = new Sequence(id, seq.toString(), 1, seq.length());\r
188       //  System.out.println("PDB Sequence is :\nSequence = " + seq);\r
189      //   System.out.println("No of residues = " + residues.size());\r
190     }\r
191 \r
192     public void setChargeColours() {\r
193         for (int i = 0; i < bonds.size(); i++) {\r
194             try {\r
195                 Bond b = (Bond) bonds.elementAt(i);\r
196 \r
197                 if (b.at1.resName.toUpperCase().equals("ASP") ||\r
198                         b.at1.resName.toUpperCase().equals("GLU")) {\r
199                     b.startCol = Color.red;\r
200                 } else if (b.at1.resName.toUpperCase().equals("LYS") ||\r
201                         b.at1.resName.toUpperCase().equals("ARG")) {\r
202                     b.startCol = Color.blue;\r
203                 } else if (b.at1.resName.toUpperCase().equals("CYS")) {\r
204                     b.startCol = Color.yellow;\r
205                 } else {\r
206                     //int atno = ((Integer) ResidueProperties.getAA3Hash().get(b.at1.resName.toUpperCase())).intValue();\r
207                     b.startCol = Color.lightGray;\r
208                 }\r
209 \r
210                 if (b.at2.resName.toUpperCase().equals("ASP") ||\r
211                         b.at2.resName.toUpperCase().equals("GLU")) {\r
212                     b.endCol = Color.red;\r
213                 } else if (b.at2.resName.toUpperCase().equals("LYS") ||\r
214                         b.at2.resName.toUpperCase().equals("ARG")) {\r
215                     b.endCol = Color.blue;\r
216                 } else if (b.at2.resName.toUpperCase().equals("CYS")) {\r
217                     b.endCol = Color.yellow;\r
218                 } else {\r
219                     //int atno = ((Integer) ResidueProperties.getAA3Hash().get(b.at2.resName.toUpperCase())).intValue();\r
220                     b.endCol = Color.lightGray;\r
221                 }\r
222             } catch (Exception e) {\r
223                 Bond b = (Bond) bonds.elementAt(i);\r
224                 b.startCol = Color.gray;\r
225                 b.endCol = Color.gray;\r
226             }\r
227         }\r
228     }\r
229 \r
230     public void setHydrophobicityColours() {\r
231         float hydmin = (float) ResidueProperties.getHydmin();\r
232         float hydmax = (float) ResidueProperties.getHydmax();\r
233         double[] hyd = ResidueProperties.getHyd();\r
234 \r
235         Hashtable AA3Hash = ResidueProperties.getAA3Hash();\r
236 \r
237         for (int i = 0; i < bonds.size(); i++) {\r
238             try {\r
239                 Bond b = (Bond) bonds.elementAt(i);\r
240 \r
241                 int atno = ((Integer) AA3Hash.get(b.at1.resName.toUpperCase())).intValue();\r
242                 float red = ((float) hyd[atno] - hydmin) / (hydmax - hydmin);\r
243 \r
244                 if (red > (float) 1.0) {\r
245                     red = (float) 1.0;\r
246                 }\r
247 \r
248                 if (red < (float) 0.0) {\r
249                     red = (float) 0.0;\r
250                 }\r
251 \r
252                 b.startCol = new Color(red, (float) 0.0, (float) 1.0 - red);\r
253                 atno = ((Integer) AA3Hash.get(b.at2.resName.toUpperCase())).intValue();\r
254 \r
255                 red = ((float) hyd[atno] - hydmin) / (hydmax - hydmin);\r
256 \r
257                 if (red > (float) 1.0) {\r
258                     red = (float) 1.0;\r
259                 }\r
260 \r
261                 if (red < (float) 0.0) {\r
262                     red = (float) 0.0;\r
263                 }\r
264 \r
265                 b.endCol = new Color(red, (float) 0.2, (float) 1.0 - red);\r
266             } catch (Exception e) {\r
267                 Bond b = (Bond) bonds.elementAt(i);\r
268                 b.startCol = Color.gray;\r
269                 b.endCol = Color.gray;\r
270             }\r
271         }\r
272     }\r
273 \r
274 \r
275 \r
276     public void setChainColours(Color col)\r
277     {\r
278         for (int i = 0; i < bonds.size(); i++)\r
279         {\r
280             Bond tmp = (Bond) bonds.elementAt(i);\r
281             tmp.startCol = col;\r
282             tmp.endCol = col;\r
283         }\r
284     }\r
285 }\r