sequence is char []
[jalview.git] / src / MCview / PDBChain.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2006 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             else\r
109               System.out.println("not found "+i);\r
110         }\r
111     }\r
112 \r
113     public void makeBond(Atom at1, Atom at2) {\r
114         float[] start = new float[3];\r
115         float[] end = new float[3];\r
116 \r
117         start[0] = at1.x;\r
118         start[1] = at1.y;\r
119         start[2] = at1.z;\r
120 \r
121         end[0] = at2.x;\r
122         end[1] = at2.y;\r
123         end[2] = at2.z;\r
124 \r
125         bonds.addElement(new Bond(start, end, at1, at2));\r
126     }\r
127 \r
128     public void makeResidueList() {\r
129         int count = 0;\r
130         StringBuffer seq = new StringBuffer();\r
131 \r
132         int i, iSize = atoms.size()-1;\r
133         for (i = 0; i < iSize; i++)\r
134         {\r
135             Atom tmp = (Atom) atoms.elementAt(i);\r
136             int resNumber = tmp.resNumber;\r
137             int res = resNumber;\r
138 \r
139             if (i == 0) {\r
140                 offset = resNumber;\r
141             }\r
142 \r
143             Vector resAtoms = new Vector();\r
144 \r
145             resAtoms.addElement((Atom) atoms.elementAt(i));\r
146             i++;\r
147             resNumber = ((Atom) atoms.elementAt(i)).resNumber;\r
148 \r
149             //Add atoms to a vector while the residue number\r
150             //remains the same\r
151             while ((resNumber == res) && (i < atoms.size())) {\r
152                 resAtoms.addElement((Atom) atoms.elementAt(i));\r
153                 i++;\r
154 \r
155                 if (i < atoms.size()) {\r
156                     resNumber = ((Atom) atoms.elementAt(i)).resNumber;\r
157                 } else {\r
158                     resNumber++;\r
159                 }\r
160             }\r
161 \r
162             //We need this to keep in step with the outer for i = loop\r
163             i--;\r
164 \r
165             //Make a new Residue object with the new atoms vector\r
166             residues.addElement(new Residue(resAtoms, resNumber - 1, count));\r
167             count++;\r
168 \r
169             Residue tmpres = (Residue) residues.lastElement();\r
170             Atom tmpat = (Atom) tmpres.atoms.elementAt(0);\r
171 \r
172             // Keep totting up the sequence\r
173             if (ResidueProperties.getAA3Hash().get(tmpat.resName) == null)\r
174             {\r
175                 seq.append("X") ;\r
176                //  System.err.println("PDBReader:Null aa3Hash for " +\r
177                //     tmpat.resName);\r
178             } else {\r
179 \r
180                 seq.append(ResidueProperties.aa[((Integer) ResidueProperties.getAA3Hash()\r
181                                                                                   .get(tmpat.resName)).intValue()]);\r
182             }\r
183         }\r
184 \r
185         if(id.length()<1 || id.equals(" "))\r
186            id = "_";\r
187 \r
188         sequence = new Sequence(id, seq.toString(), 1, seq.length());\r
189       //  System.out.println("PDB Sequence is :\nSequence = " + seq);\r
190      //   System.out.println("No of residues = " + residues.size());\r
191     }\r
192 \r
193     public void setChargeColours() {\r
194         for (int i = 0; i < bonds.size(); i++) {\r
195             try {\r
196                 Bond b = (Bond) bonds.elementAt(i);\r
197 \r
198                 if (b.at1.resName.equalsIgnoreCase("ASP") ||\r
199                         b.at1.resName.equalsIgnoreCase("GLU")) {\r
200                     b.startCol = Color.red;\r
201                 } else if (b.at1.resName.equalsIgnoreCase("LYS") ||\r
202                         b.at1.resName.equalsIgnoreCase("ARG")) {\r
203                     b.startCol = Color.blue;\r
204                 } else if (b.at1.resName.equalsIgnoreCase("CYS")) {\r
205                     b.startCol = Color.yellow;\r
206                 } else {\r
207                     //int atno = ((Integer) ResidueProperties.getAA3Hash().get(b.at1.resName.toUpperCase())).intValue();\r
208                     b.startCol = Color.lightGray;\r
209                 }\r
210 \r
211                 if (b.at2.resName.equalsIgnoreCase("ASP") ||\r
212                         b.at2.resName.equalsIgnoreCase("GLU")) {\r
213                     b.endCol = Color.red;\r
214                 } else if (b.at2.resName.equalsIgnoreCase("LYS") ||\r
215                         b.at2.resName.equalsIgnoreCase("ARG")) {\r
216                     b.endCol = Color.blue;\r
217                 } else if (b.at2.resName.equalsIgnoreCase("CYS")) {\r
218                     b.endCol = Color.yellow;\r
219                 } else {\r
220                     //int atno = ((Integer) ResidueProperties.getAA3Hash().get(b.at2.resName.toUpperCase())).intValue();\r
221                     b.endCol = Color.lightGray;\r
222                 }\r
223             } catch (Exception e) {\r
224                 Bond b = (Bond) bonds.elementAt(i);\r
225                 b.startCol = Color.gray;\r
226                 b.endCol = Color.gray;\r
227             }\r
228         }\r
229     }\r
230 \r
231 \r
232     public void setChainColours(jalview.schemes.ColourSchemeI cs)\r
233     {\r
234         Bond b;\r
235         for (int i = 0; i < bonds.size(); i++) {\r
236             try {\r
237               b = (Bond) bonds.elementAt(i);\r
238 \r
239             /*  ( (Bond) bonds.elementAt(i)).startCol = cs.findColour(\r
240                   ResidueProperties.codonTranslate(\r
241                       ResidueProperties.aa3Hash.get(b.at1.resName).toString().charAt(0)\r
242                   );\r
243 \r
244               b.endCol = cs.findColour(\r
245                   ResidueProperties.aa[ ( (Integer) ResidueProperties.aa3Hash.\r
246                                          get(b.at2.resName)).intValue()]\r
247                   );*/\r
248 \r
249             } catch (Exception e)\r
250             {\r
251                 b = (Bond) bonds.elementAt(i);\r
252                 b.startCol = Color.gray;\r
253                 b.endCol = Color.gray;\r
254             }\r
255         }\r
256     }\r
257 \r
258 \r
259 \r
260     public void setChainColours(Color col)\r
261     {\r
262         for (int i = 0; i < bonds.size(); i++)\r
263         {\r
264             Bond tmp = (Bond) bonds.elementAt(i);\r
265             tmp.startCol = col;\r
266             tmp.endCol = col;\r
267         }\r
268     }\r
269 }\r