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