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