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