tfactor and occupancy extracted from PDB file, and tfactor added as sequence associat...
[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     Vector resAnnotation = new Vector();
193     int i, iSize = atoms.size() - 1;
194     int resNumber = -1;
195     for (i = 0; i <= iSize; i++)
196     {
197       Atom tmp = (Atom) atoms.elementAt(i);
198       resNumber = tmp.resNumber;
199       int res = resNumber;
200
201       if (i == 0)
202       {
203         offset = resNumber;
204       }
205
206       Vector resAtoms = new Vector();
207       //Add atoms to a vector while the residue number
208       //remains the same as the first atom's resNumber (res)
209       while ( (resNumber == res) && (i < atoms.size()))
210       {
211         resAtoms.addElement( (Atom) atoms.elementAt(i));
212         i++;
213
214         if (i < atoms.size())
215         {
216           resNumber = ( (Atom) atoms.elementAt(i)).resNumber;
217         }
218         else
219         {
220           resNumber++;
221         }
222       }
223
224       //We need this to keep in step with the outer for i = loop
225       i--;
226
227       //Make a new Residue object with the new atoms vector
228       residues.addElement(new Residue(resAtoms, resNumber - 1, count));
229
230       Residue tmpres = (Residue) residues.lastElement();
231       Atom tmpat = (Atom) tmpres.atoms.elementAt(0);
232       // Make A new SequenceFeature for the current residue numbering
233       SequenceFeature sf =
234           new SequenceFeature("RESNUM",
235                               tmpat.resName + ":" + tmpat.resNumIns + " " +
236                               pdbid + id,
237                               "", offset + count, offset + count,
238                               MCview.PDBChain.PDBFILEFEATURE);
239       resFeatures.addElement(sf);
240       resAnnotation.addElement(new Annotation("","",'\0',tmpat.tfactor));
241       // Keep totting up the sequence
242       if (ResidueProperties.getAA3Hash().get(tmpat.resName) == null)
243       {
244         seq.append("X");
245         //  System.err.println("PDBReader:Null aa3Hash for " +
246         //     tmpat.resName);
247       }
248       else
249       {
250
251         seq.append(ResidueProperties.aa[ ( (Integer) ResidueProperties.
252                                           getAA3Hash()
253                                           .get(tmpat.resName)).intValue()]);
254       }
255       count++;
256     }
257
258     if (id.length() < 1)
259     {
260       id = " ";
261     }
262
263     sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note: resNumber-offset ~= seq.size()
264     //  System.out.println("PDB Sequence is :\nSequence = " + seq);
265     //   System.out.println("No of residues = " + residues.size());
266     for (i = 0, iSize = resFeatures.size(); i < iSize; i++)
267     {
268       sequence.addSequenceFeature( (SequenceFeature) resFeatures.elementAt(i));
269       resFeatures.setElementAt(null, i);
270     }
271     Annotation[] annots = new Annotation[resAnnotation.size()];
272     float max=0;
273     for (i=0,iSize=annots.length; i<iSize; i++)
274     {
275       annots[i] = (Annotation) resAnnotation.elementAt(i);
276       if (annots[i].value>max)
277         max = annots[i].value;
278       resAnnotation.setElementAt(null, i);
279     }
280     AlignmentAnnotation tfactorann = new AlignmentAnnotation("PDB.CATempFactor","CA Temperature Factor", 
281             annots, 0, max, AlignmentAnnotation.LINE_GRAPH);
282     tfactorann.setSequenceRef(sequence);
283     sequence.addAlignmentAnnotation(tfactorann);
284   }
285
286   public void setChargeColours()
287   {
288     for (int i = 0; i < bonds.size(); i++)
289     {
290       try
291       {
292         Bond b = (Bond) bonds.elementAt(i);
293
294         if (b.at1.resName.equalsIgnoreCase("ASP") ||
295             b.at1.resName.equalsIgnoreCase("GLU"))
296         {
297           b.startCol = Color.red;
298         }
299         else if (b.at1.resName.equalsIgnoreCase("LYS") ||
300                  b.at1.resName.equalsIgnoreCase("ARG"))
301         {
302           b.startCol = Color.blue;
303         }
304         else if (b.at1.resName.equalsIgnoreCase("CYS"))
305         {
306           b.startCol = Color.yellow;
307         }
308         else
309         {
310           b.startCol = Color.lightGray;
311         }
312
313         if (b.at2.resName.equalsIgnoreCase("ASP") ||
314             b.at2.resName.equalsIgnoreCase("GLU"))
315         {
316           b.endCol = Color.red;
317         }
318         else if (b.at2.resName.equalsIgnoreCase("LYS") ||
319                  b.at2.resName.equalsIgnoreCase("ARG"))
320         {
321           b.endCol = Color.blue;
322         }
323         else if (b.at2.resName.equalsIgnoreCase("CYS"))
324         {
325           b.endCol = Color.yellow;
326         }
327         else
328         {
329           b.endCol = Color.lightGray;
330         }
331       }
332       catch (Exception e)
333       {
334         Bond b = (Bond) bonds.elementAt(i);
335         b.startCol = Color.gray;
336         b.endCol = Color.gray;
337       }
338     }
339   }
340
341   public void setChainColours(jalview.schemes.ColourSchemeI cs)
342   {
343     Bond b;
344     int index;
345     for (int i = 0; i < bonds.size(); i++)
346     {
347       try
348       {
349         b = (Bond) bonds.elementAt(i);
350
351         index = ( (Integer) ResidueProperties.aa3Hash.get(b.at1.
352             resName)).intValue();
353         b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
354
355         index = ( (Integer) ResidueProperties.aa3Hash.get(b.at2.resName)).
356             intValue();
357         b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
358
359       }
360       catch (Exception e)
361       {
362         b = (Bond) bonds.elementAt(i);
363         b.startCol = Color.gray;
364         b.endCol = Color.gray;
365       }
366     }
367   }
368
369   public void setChainColours(Color col)
370   {
371     for (int i = 0; i < bonds.size(); i++)
372     {
373       Bond tmp = (Bond) bonds.elementAt(i);
374       tmp.startCol = col;
375       tmp.endCol = col;
376     }
377   }
378 }