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