apply gpl development license
[jalview.git] / src / MCview / PDBChain.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 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       // MCview.PDBChain.PDBFILEFEATURE);
254       resFeatures.addElement(sf);
255       resAnnotation.addElement(new Annotation(tmpat.tfactor));
256       // Keep totting up the sequence
257       if (ResidueProperties.getAA3Hash().get(tmpat.resName) == null)
258       {
259         seq.append("X");
260         // System.err.println("PDBReader:Null aa3Hash for " +
261         // tmpat.resName);
262       }
263       else
264       {
265
266         seq.append(ResidueProperties.aa[((Integer) ResidueProperties
267                 .getAA3Hash().get(tmpat.resName)).intValue()]);
268       }
269       count++;
270     }
271
272     if (id.length() < 1)
273     {
274       id = " ";
275     }
276
277     sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
278                                                                         // resNumber-offset
279                                                                         // ~=
280                                                                         // seq.size()
281     // Add normalised feature scores to RESNUM indicating start/end of sequence
282     //      sf.setScore(offset+count);
283                                                                            
284                                                                        
285     // System.out.println("PDB Sequence is :\nSequence = " + seq);
286     // System.out.println("No of residues = " + residues.size());
287     for (i = 0, iSize = resFeatures.size(); i < iSize; i++)
288     {
289       sequence.addSequenceFeature((SequenceFeature) resFeatures
290               .elementAt(i));
291       resFeatures.setElementAt(null, i);
292     }
293     Annotation[] annots = new Annotation[resAnnotation.size()];
294     float max = 0;
295     for (i = 0, iSize = annots.length; i < iSize; i++)
296     {
297       annots[i] = (Annotation) resAnnotation.elementAt(i);
298       if (annots[i].value > max)
299         max = annots[i].value;
300       resAnnotation.setElementAt(null, i);
301     }
302     AlignmentAnnotation tfactorann = new AlignmentAnnotation(
303             "PDB.CATempFactor", "CA Temperature Factor for "
304                     + sequence.getName(), annots, 0, max,
305             AlignmentAnnotation.LINE_GRAPH);
306     tfactorann.setSequenceRef(sequence);
307     sequence.addAlignmentAnnotation(tfactorann);
308   }
309
310   public void setChargeColours()
311   {
312     for (int i = 0; i < bonds.size(); i++)
313     {
314       try
315       {
316         Bond b = (Bond) bonds.elementAt(i);
317
318         if (b.at1.resName.equalsIgnoreCase("ASP")
319                 || b.at1.resName.equalsIgnoreCase("GLU"))
320         {
321           b.startCol = Color.red;
322         }
323         else if (b.at1.resName.equalsIgnoreCase("LYS")
324                 || b.at1.resName.equalsIgnoreCase("ARG"))
325         {
326           b.startCol = Color.blue;
327         }
328         else if (b.at1.resName.equalsIgnoreCase("CYS"))
329         {
330           b.startCol = Color.yellow;
331         }
332         else
333         {
334           b.startCol = Color.lightGray;
335         }
336
337         if (b.at2.resName.equalsIgnoreCase("ASP")
338                 || b.at2.resName.equalsIgnoreCase("GLU"))
339         {
340           b.endCol = Color.red;
341         }
342         else if (b.at2.resName.equalsIgnoreCase("LYS")
343                 || b.at2.resName.equalsIgnoreCase("ARG"))
344         {
345           b.endCol = Color.blue;
346         }
347         else if (b.at2.resName.equalsIgnoreCase("CYS"))
348         {
349           b.endCol = Color.yellow;
350         }
351         else
352         {
353           b.endCol = Color.lightGray;
354         }
355       } catch (Exception e)
356       {
357         Bond b = (Bond) bonds.elementAt(i);
358         b.startCol = Color.gray;
359         b.endCol = Color.gray;
360       }
361     }
362   }
363
364   public void setChainColours(jalview.schemes.ColourSchemeI cs)
365   {
366     Bond b;
367     int index;
368     for (int i = 0; i < bonds.size(); i++)
369     {
370       try
371       {
372         b = (Bond) bonds.elementAt(i);
373
374         index = ((Integer) ResidueProperties.aa3Hash.get(b.at1.resName))
375                 .intValue();
376         b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
377
378         index = ((Integer) ResidueProperties.aa3Hash.get(b.at2.resName))
379                 .intValue();
380         b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
381
382       } catch (Exception e)
383       {
384         b = (Bond) bonds.elementAt(i);
385         b.startCol = Color.gray;
386         b.endCol = Color.gray;
387       }
388     }
389   }
390
391   public void setChainColours(Color col)
392   {
393     for (int i = 0; i < bonds.size(); i++)
394     {
395       Bond tmp = (Bond) bonds.elementAt(i);
396       tmp.startCol = col;
397       tmp.endCol = col;
398     }
399   }
400
401   public AlignmentAnnotation[] transferResidueAnnotation(SequenceI seq,
402           String status)
403   {
404     AlignmentAnnotation[] transferred = null;
405
406     return transferred;
407
408   }
409
410   /**
411    * copy any sequence annotation onto the sequence mapped using the provided
412    * StructureMapping
413    * 
414    * @param mapping
415    */
416   public void transferResidueAnnotation(StructureMapping mapping)
417   {
418     SequenceI sq = mapping.getSequence();
419     if (sq != null)
420     {
421       if (sequence != null && sequence.getAnnotation() != null)
422       {
423
424       }
425       float min = -1, max = 0;
426       Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
427       for (int i = sq.getStart(), j = sq.getEnd(), k = 0; i <= j; i++, k++)
428       {
429         int prn = mapping.getPDBResNum(k + 1);
430
431         an[k] = new Annotation((float) prn);
432         if (min == -1)
433         {
434           min = k;
435           max = k;
436         }
437         else
438         {
439           if (min > k)
440           {
441             min = k;
442           }
443           else if (max < k)
444           {
445             max = k;
446           }
447         }
448       }
449       sq
450               .addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
451                       "PDB Residue Numbering for " + this.pdbid + ":"
452                               + this.id, an, (float) min, (float) max,
453                       AlignmentAnnotation.LINE_GRAPH));
454     }
455   }
456 }