PDB residue numberings transfered and alignment view refreshed to update featureRenderer.
[jalview.git] / src / MCview / PDBChain.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer
3  * Copyright (C) 2007 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 java.util.*;
22
23 import java.awt.*;
24
25 import jalview.analysis.*;
26 import jalview.datamodel.*;
27 import jalview.schemes.*;
28
29 public class PDBChain
30 {
31   /**
32    * SequenceFeature group for PDB File features added to sequences
33    */
34   private static final String PDBFILEFEATURE = "PDBFile";
35   private static final String IEASTATUS = "IEA:jalview";
36   public String id;
37   public Vector bonds = new Vector();
38   public Vector atoms = new Vector();
39   public Vector residues = new Vector();
40   public int offset;
41   public Sequence sequence;
42   public boolean isVisible = true;
43   public int pdbstart = 0;
44   public int pdbend = 0;
45   public int seqstart = 0;
46   public int seqend = 0;
47   public String pdbid = "";
48   public PDBChain(String pdbid, String id)
49   {
50     this.pdbid = pdbid.toLowerCase();
51     this.id = id;
52   }
53
54   public String print()
55   {
56     String tmp = "";
57
58     for (int i = 0; i < bonds.size(); i++)
59     {
60       tmp = tmp + ( (Bond) bonds.elementAt(i)).at1.resName + " " +
61           ( (Bond) bonds.elementAt(i)).at1.resNumber + " " + offset +
62           "\n";
63     }
64
65     return tmp;
66   }
67
68   public void makeExactMapping(AlignSeq as, SequenceI s1)
69   {
70     int pdbpos = as.getSeq2Start() - 2;
71     int alignpos = s1.getStart() + as.getSeq1Start() - 3;
72
73     for (int i = 0; i < as.astr1.length(); i++)
74     {
75       if (as.astr1.charAt(i) != '-')
76       {
77         alignpos++;
78       }
79
80       if (as.astr2.charAt(i) != '-')
81       {
82         pdbpos++;
83       }
84
85       if (as.astr1.charAt(i) == as.astr2.charAt(i))
86       {
87         Residue res = (Residue) residues.elementAt(pdbpos);
88         Enumeration en = res.atoms.elements();
89         while (en.hasMoreElements())
90         {
91           Atom atom = (Atom) en.nextElement();
92           atom.alignmentMapping = alignpos;
93         }
94       }
95     }
96   }
97
98   /**
99    * copy over the RESNUM seqfeatures from the internal chain sequence to the mapped sequence
100    * @param seq
101    * @param status The Status of the transferred annotation
102    * @return the features added to sq (or its dataset)
103    */
104   public SequenceFeature[] transferRESNUMFeatures(SequenceI seq, String status)
105   {
106     SequenceI sq = seq;
107     while (sq!=null && sq.getDatasetSequence()!=null)
108     {
109       sq = sq.getDatasetSequence();
110       if (sq == sequence)
111       {
112         return null;
113       }
114     }
115     /**
116      * Remove any existing features for this chain if they exist ?
117      * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures();
118         int totfeat=seqsfeatures.length;
119         // Remove any features for this exact chain ?
120         for (int i=0; i<seqsfeatures.length; i++) {
121         } */
122     if (status == null)
123     {
124       status = PDBChain.IEASTATUS;
125     }
126     SequenceFeature[] features = sequence.getSequenceFeatures();
127     for (int i = 0; i < features.length; i++)
128     {
129       if (features[i].getFeatureGroup().equals(PDBChain.PDBFILEFEATURE))
130       {
131         SequenceFeature tx = new SequenceFeature(features[i]);
132         tx.setBegin(1 +
133                     ( (Atom) ( (Residue) residues.elementAt(tx.getBegin() - offset)).
134                      atoms.elementAt(0)).alignmentMapping);
135         tx.setEnd(1 +
136                   ( (Atom) ( (Residue) residues.elementAt(tx.getEnd() - offset)).
137                    atoms.elementAt(0)).alignmentMapping);
138         tx.setStatus(status +
139                      ( (tx.getStatus() == null || tx.getStatus().length() == 0) ?
140                       "" : ":" + tx.getStatus()));
141         if (tx.begin!=0 && tx.end!=0)
142           sq.addSequenceFeature(tx);
143       }
144     }
145     return features;
146   }
147
148   public void makeCaBondList()
149   {
150     for (int i = 0; i < (residues.size() - 1); i++)
151     {
152       Residue tmpres = (Residue) residues.elementAt(i);
153       Residue tmpres2 = (Residue) residues.elementAt(i + 1);
154       Atom at1 = tmpres.findAtom("CA");
155       Atom at2 = tmpres2.findAtom("CA");
156
157       if ( (at1 != null) && (at2 != null))
158       {
159         if (at1.chain.equals(at2.chain))
160         {
161           makeBond(at1, at2);
162         }
163       }
164       else
165       {
166         System.out.println("not found " + i);
167       }
168     }
169   }
170
171   public void makeBond(Atom at1, Atom at2)
172   {
173     float[] start = new float[3];
174     float[] end = new float[3];
175
176     start[0] = at1.x;
177     start[1] = at1.y;
178     start[2] = at1.z;
179
180     end[0] = at2.x;
181     end[1] = at2.y;
182     end[2] = at2.z;
183
184     bonds.addElement(new Bond(start, end, at1, at2));
185   }
186
187   public void makeResidueList()
188   {
189     int count = 0;
190     StringBuffer seq = new StringBuffer();
191     Vector resFeatures = new Vector();
192     int i, iSize = atoms.size() - 1;
193     int resNumber = -1;
194     for (i = 0; i <= iSize; i++)
195     {
196       Atom tmp = (Atom) atoms.elementAt(i);
197       resNumber = tmp.resNumber;
198       int res = resNumber;
199
200       if (i == 0)
201       {
202         offset = resNumber;
203       }
204
205       Vector resAtoms = new Vector();
206       //Add atoms to a vector while the residue number
207       //remains the same as the first atom's resNumber (res)
208       while ( (resNumber == res) && (i < atoms.size()))
209       {
210         resAtoms.addElement( (Atom) atoms.elementAt(i));
211         i++;
212
213         if (i < atoms.size())
214         {
215           resNumber = ( (Atom) atoms.elementAt(i)).resNumber;
216         }
217         else
218         {
219           resNumber++;
220         }
221       }
222
223       //We need this to keep in step with the outer for i = loop
224       i--;
225
226       //Make a new Residue object with the new atoms vector
227       residues.addElement(new Residue(resAtoms, resNumber - 1, count));
228
229       Residue tmpres = (Residue) residues.lastElement();
230       Atom tmpat = (Atom) tmpres.atoms.elementAt(0);
231       // Make A new SequenceFeature for the current residue numbering
232       SequenceFeature sf =
233           new SequenceFeature("RESNUM",
234                               tmpat.resName + ":" + tmpat.resNumIns + " " +
235                               pdbid + id,
236                               "", offset + count, offset + count,
237                               MCview.PDBChain.PDBFILEFEATURE);
238       resFeatures.addElement(sf);
239       // Keep totting up the sequence
240       if (ResidueProperties.getAA3Hash().get(tmpat.resName) == null)
241       {
242         seq.append("X");
243         //  System.err.println("PDBReader:Null aa3Hash for " +
244         //     tmpat.resName);
245       }
246       else
247       {
248
249         seq.append(ResidueProperties.aa[ ( (Integer) ResidueProperties.
250                                           getAA3Hash()
251                                           .get(tmpat.resName)).intValue()]);
252       }
253       count++;
254     }
255
256     if (id.length() < 1)
257     {
258       id = " ";
259     }
260
261     sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note: resNumber-offset ~= seq.size()
262     //  System.out.println("PDB Sequence is :\nSequence = " + seq);
263     //   System.out.println("No of residues = " + residues.size());
264     for (i = 0, iSize = resFeatures.size(); i < iSize; i++)
265     {
266       sequence.addSequenceFeature( (SequenceFeature) resFeatures.elementAt(i));
267       resFeatures.setElementAt(null, i);
268     }
269   }
270
271   public void setChargeColours()
272   {
273     for (int i = 0; i < bonds.size(); i++)
274     {
275       try
276       {
277         Bond b = (Bond) bonds.elementAt(i);
278
279         if (b.at1.resName.equalsIgnoreCase("ASP") ||
280             b.at1.resName.equalsIgnoreCase("GLU"))
281         {
282           b.startCol = Color.red;
283         }
284         else if (b.at1.resName.equalsIgnoreCase("LYS") ||
285                  b.at1.resName.equalsIgnoreCase("ARG"))
286         {
287           b.startCol = Color.blue;
288         }
289         else if (b.at1.resName.equalsIgnoreCase("CYS"))
290         {
291           b.startCol = Color.yellow;
292         }
293         else
294         {
295           b.startCol = Color.lightGray;
296         }
297
298         if (b.at2.resName.equalsIgnoreCase("ASP") ||
299             b.at2.resName.equalsIgnoreCase("GLU"))
300         {
301           b.endCol = Color.red;
302         }
303         else if (b.at2.resName.equalsIgnoreCase("LYS") ||
304                  b.at2.resName.equalsIgnoreCase("ARG"))
305         {
306           b.endCol = Color.blue;
307         }
308         else if (b.at2.resName.equalsIgnoreCase("CYS"))
309         {
310           b.endCol = Color.yellow;
311         }
312         else
313         {
314           b.endCol = Color.lightGray;
315         }
316       }
317       catch (Exception e)
318       {
319         Bond b = (Bond) bonds.elementAt(i);
320         b.startCol = Color.gray;
321         b.endCol = Color.gray;
322       }
323     }
324   }
325
326   public void setChainColours(jalview.schemes.ColourSchemeI cs)
327   {
328     Bond b;
329     int index;
330     for (int i = 0; i < bonds.size(); i++)
331     {
332       try
333       {
334         b = (Bond) bonds.elementAt(i);
335
336         index = ( (Integer) ResidueProperties.aa3Hash.get(b.at1.
337             resName)).intValue();
338         b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
339
340         index = ( (Integer) ResidueProperties.aa3Hash.get(b.at2.resName)).
341             intValue();
342         b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
343
344       }
345       catch (Exception e)
346       {
347         b = (Bond) bonds.elementAt(i);
348         b.startCol = Color.gray;
349         b.endCol = Color.gray;
350       }
351     }
352   }
353
354   public void setChainColours(Color col)
355   {
356     for (int i = 0; i < bonds.size(); i++)
357     {
358       Bond tmp = (Bond) bonds.elementAt(i);
359       tmp.startCol = col;
360       tmp.endCol = col;
361     }
362   }
363 }