na pdb: recognise NA residue names
[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     Object symbol;
208     boolean nucleotide=false;
209     StringBuffer seq = new StringBuffer();
210     Vector resFeatures = new Vector();
211     Vector resAnnotation = new Vector();
212     int i, iSize = atoms.size() - 1;
213     int resNumber = -1;
214     for (i = 0; i <= iSize; i++)
215     {
216       Atom tmp = (Atom) atoms.elementAt(i);
217       resNumber = tmp.resNumber;
218       int res = resNumber;
219
220       if (i == 0)
221       {
222         offset = resNumber;
223       }
224
225       Vector resAtoms = new Vector();
226       // Add atoms to a vector while the residue number
227       // remains the same as the first atom's resNumber (res)
228       while ((resNumber == res) && (i < atoms.size()))
229       {
230         resAtoms.addElement((Atom) atoms.elementAt(i));
231         i++;
232
233         if (i < atoms.size())
234         {
235           resNumber = ((Atom) atoms.elementAt(i)).resNumber;
236         }
237         else
238         {
239           resNumber++;
240         }
241       }
242
243       // We need this to keep in step with the outer for i = loop
244       i--;
245
246       // Make a new Residue object with the new atoms vector
247       residues.addElement(new Residue(resAtoms, resNumber - 1, count));
248
249       Residue tmpres = (Residue) residues.lastElement();
250       Atom tmpat = (Atom) tmpres.atoms.elementAt(0);
251       // Make A new SequenceFeature for the current residue numbering
252       SequenceFeature sf = new SequenceFeature("RESNUM", tmpat.resName
253               + ":" + tmpat.resNumIns + " " + pdbid + id, "", offset
254               + count, offset + count, pdbid);
255       // MCview.PDBChain.PDBFILEFEATURE);
256       resFeatures.addElement(sf);
257       resAnnotation.addElement(new Annotation(tmpat.tfactor));
258       // Keep totting up the sequence
259       if ((symbol=ResidueProperties.getAA3Hash().get(tmpat.resName)) == null)
260       {
261         if (ResidueProperties.nucleotideIndex[tmpat.resName.charAt(0)]==-1)
262         {
263           seq.append("X");
264         // System.err.println("PDBReader:Null aa3Hash for " +
265         // tmpat.resName);
266         } else {
267           // nucleotide flag
268           nucleotide=true;
269           seq.append(tmpat.resName.charAt(0));
270         }
271       }
272       else
273       {
274         if (nucleotide)
275         {
276           System.err.println("Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
277         }
278         seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
279       }
280       count++;
281     }
282
283     if (id.length() < 1)
284     {
285       id = " ";
286     }
287
288     sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
289                                                                         // resNumber-offset
290                                                                         // ~=
291                                                                         // seq.size()
292     // Add normalised feature scores to RESNUM indicating start/end of sequence
293     //      sf.setScore(offset+count);
294                                                                            
295                                                                        
296     // System.out.println("PDB Sequence is :\nSequence = " + seq);
297     // System.out.println("No of residues = " + residues.size());
298     for (i = 0, iSize = resFeatures.size(); i < iSize; i++)
299     {
300       sequence.addSequenceFeature((SequenceFeature) resFeatures
301               .elementAt(i));
302       resFeatures.setElementAt(null, i);
303     }
304     Annotation[] annots = new Annotation[resAnnotation.size()];
305     float max = 0;
306     for (i = 0, iSize = annots.length; i < iSize; i++)
307     {
308       annots[i] = (Annotation) resAnnotation.elementAt(i);
309       if (annots[i].value > max)
310         max = annots[i].value;
311       resAnnotation.setElementAt(null, i);
312     }
313     AlignmentAnnotation tfactorann = new AlignmentAnnotation(
314             "PDB.TempFactor", "Temperature Factor for "
315                     + sequence.getName(), annots, 0, max,
316             AlignmentAnnotation.LINE_GRAPH);
317     tfactorann.setSequenceRef(sequence);
318     sequence.addAlignmentAnnotation(tfactorann);
319   }
320
321   public void setChargeColours()
322   {
323     for (int i = 0; i < bonds.size(); i++)
324     {
325       try
326       {
327         Bond b = (Bond) bonds.elementAt(i);
328
329         if (b.at1.resName.equalsIgnoreCase("ASP")
330                 || b.at1.resName.equalsIgnoreCase("GLU"))
331         {
332           b.startCol = Color.red;
333         }
334         else if (b.at1.resName.equalsIgnoreCase("LYS")
335                 || b.at1.resName.equalsIgnoreCase("ARG"))
336         {
337           b.startCol = Color.blue;
338         }
339         else if (b.at1.resName.equalsIgnoreCase("CYS"))
340         {
341           b.startCol = Color.yellow;
342         }
343         else
344         {
345           b.startCol = Color.lightGray;
346         }
347
348         if (b.at2.resName.equalsIgnoreCase("ASP")
349                 || b.at2.resName.equalsIgnoreCase("GLU"))
350         {
351           b.endCol = Color.red;
352         }
353         else if (b.at2.resName.equalsIgnoreCase("LYS")
354                 || b.at2.resName.equalsIgnoreCase("ARG"))
355         {
356           b.endCol = Color.blue;
357         }
358         else if (b.at2.resName.equalsIgnoreCase("CYS"))
359         {
360           b.endCol = Color.yellow;
361         }
362         else
363         {
364           b.endCol = Color.lightGray;
365         }
366       } catch (Exception e)
367       {
368         Bond b = (Bond) bonds.elementAt(i);
369         b.startCol = Color.gray;
370         b.endCol = Color.gray;
371       }
372     }
373   }
374
375   public void setChainColours(jalview.schemes.ColourSchemeI cs)
376   {
377     Bond b;
378     int index;
379     for (int i = 0; i < bonds.size(); i++)
380     {
381       try
382       {
383         b = (Bond) bonds.elementAt(i);
384
385         index = ((Integer) ResidueProperties.aa3Hash.get(b.at1.resName))
386                 .intValue();
387         b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
388
389         index = ((Integer) ResidueProperties.aa3Hash.get(b.at2.resName))
390                 .intValue();
391         b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
392
393       } catch (Exception e)
394       {
395         b = (Bond) bonds.elementAt(i);
396         b.startCol = Color.gray;
397         b.endCol = Color.gray;
398       }
399     }
400   }
401
402   public void setChainColours(Color col)
403   {
404     for (int i = 0; i < bonds.size(); i++)
405     {
406       Bond tmp = (Bond) bonds.elementAt(i);
407       tmp.startCol = col;
408       tmp.endCol = col;
409     }
410   }
411
412   public AlignmentAnnotation[] transferResidueAnnotation(SequenceI seq,
413           String status)
414   {
415     AlignmentAnnotation[] transferred = null;
416
417     return transferred;
418
419   }
420
421   /**
422    * copy any sequence annotation onto the sequence mapped using the provided
423    * StructureMapping
424    * 
425    * @param mapping
426    */
427   public void transferResidueAnnotation(StructureMapping mapping)
428   {
429     SequenceI sq = mapping.getSequence();
430     if (sq != null)
431     {
432       if (sequence != null && sequence.getAnnotation() != null)
433       {
434
435       }
436       float min = -1, max = 0;
437       Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
438       for (int i = sq.getStart(), j = sq.getEnd(), k = 0; i <= j; i++, k++)
439       {
440         int prn = mapping.getPDBResNum(k + 1);
441
442         an[k] = new Annotation((float) prn);
443         if (min == -1)
444         {
445           min = k;
446           max = k;
447         }
448         else
449         {
450           if (min > k)
451           {
452             min = k;
453           }
454           else if (max < k)
455           {
456             max = k;
457           }
458         }
459       }
460       sq
461               .addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
462                       "PDB Residue Numbering for " + this.pdbid + ":"
463                               + this.id, an, (float) min, (float) max,
464                       AlignmentAnnotation.LINE_GRAPH));
465     }
466   }
467 }