47f681716ea15683aa67792df3ddf1308a13889a
[jalview.git] / src / MCview / PDBChain.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package MCview;
19
20 import java.util.*;
21
22 import java.awt.*;
23
24 import jalview.analysis.*;
25 import jalview.datamodel.*;
26 import jalview.schemes.*;
27 import jalview.structure.StructureMapping;
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
36   private static final String IEASTATUS = "IEA:jalview";
37
38   public String id;
39
40   public Vector bonds = new Vector();
41
42   public Vector atoms = new Vector();
43
44   public Vector residues = new Vector();
45
46   public int offset;
47
48   public Sequence sequence;
49
50   public boolean isNa = false;
51
52   public boolean isVisible = true;
53
54   public int pdbstart = 0;
55
56   public int pdbend = 0;
57
58   public int seqstart = 0;
59
60   public int seqend = 0;
61
62   public String pdbid = "";
63
64   public PDBChain(String pdbid, String id)
65   {
66     this.pdbid = pdbid.toLowerCase();
67     this.id = id;
68   }
69
70   public String print()
71   {
72     String tmp = "";
73
74     for (int i = 0; i < bonds.size(); i++)
75     {
76       tmp = tmp + ((Bond) bonds.elementAt(i)).at1.resName + " "
77               + ((Bond) bonds.elementAt(i)).at1.resNumber + " " + offset
78               + "\n";
79     }
80
81     return tmp;
82   }
83
84   public void makeExactMapping(AlignSeq as, SequenceI s1)
85   {
86     int pdbpos = as.getSeq2Start() - 2;
87     int alignpos = s1.getStart() + as.getSeq1Start() - 3;
88
89     for (int i = 0; i < as.astr1.length(); i++)
90     {
91       if (as.astr1.charAt(i) != '-')
92       {
93         alignpos++;
94       }
95
96       if (as.astr2.charAt(i) != '-')
97       {
98         pdbpos++;
99       }
100
101       if (as.astr1.charAt(i) == as.astr2.charAt(i))
102       {
103         Residue res = (Residue) residues.elementAt(pdbpos);
104         Enumeration en = res.atoms.elements();
105         while (en.hasMoreElements())
106         {
107           Atom atom = (Atom) en.nextElement();
108           atom.alignmentMapping = alignpos;
109         }
110       }
111     }
112   }
113
114   /**
115    * copy over the RESNUM seqfeatures from the internal chain sequence to the
116    * mapped sequence
117    * 
118    * @param seq
119    * @param status
120    *          The Status of the transferred annotation
121    * @return the features added to sq (or its dataset)
122    */
123   public SequenceFeature[] transferRESNUMFeatures(SequenceI seq,
124           String status)
125   {
126     SequenceI sq = seq;
127     while (sq != null && sq.getDatasetSequence() != null)
128     {
129       sq = sq.getDatasetSequence();
130       if (sq == sequence)
131       {
132         return null;
133       }
134     }
135     /**
136      * Remove any existing features for this chain if they exist ?
137      * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures(); int
138      * totfeat=seqsfeatures.length; // Remove any features for this exact chain
139      * ? for (int i=0; i<seqsfeatures.length; i++) { }
140      */
141     if (status == null)
142     {
143       status = PDBChain.IEASTATUS;
144     }
145     SequenceFeature[] features = sequence.getSequenceFeatures();
146     for (int i = 0; i < features.length; i++)
147     {
148       if (features[i].getFeatureGroup().equals(pdbid))
149       {
150         SequenceFeature tx = new SequenceFeature(features[i]);
151         tx.setBegin(1 + ((Atom) ((Residue) residues.elementAt(tx.getBegin()
152                 - offset)).atoms.elementAt(0)).alignmentMapping);
153         tx.setEnd(1 + ((Atom) ((Residue) residues.elementAt(tx.getEnd()
154                 - offset)).atoms.elementAt(0)).alignmentMapping);
155         tx.setStatus(status
156                 + ((tx.getStatus() == null || tx.getStatus().length() == 0) ? ""
157                         : ":" + 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     boolean na = false;
168     int numNa = 0;
169     for (int i = 0; i < (residues.size() - 1); i++)
170     {
171       Residue tmpres = (Residue) residues.elementAt(i);
172       Residue tmpres2 = (Residue) residues.elementAt(i + 1);
173       Atom at1 = tmpres.findAtom("CA");
174       Atom at2 = tmpres2.findAtom("CA");
175       na = false;
176       if ((at1 == null) && (at2 == null))
177       {
178         na = true;
179         at1 = tmpres.findAtom("P");
180         at2 = tmpres2.findAtom("P");
181       }
182       if ((at1 != null) && (at2 != null))
183       {
184         if (at1.chain.equals(at2.chain))
185         {
186           if (na)
187           {
188             numNa++;
189           }
190           makeBond(at1, at2);
191         }
192       }
193       else
194       {
195         System.out.println("not found " + i);
196       }
197     }
198     if (numNa > 0 && ((numNa / residues.size()) > 0.99))
199     {
200       isNa = true;
201     }
202   }
203
204   public void makeBond(Atom at1, Atom at2)
205   {
206     float[] start = new float[3];
207     float[] end = new float[3];
208
209     start[0] = at1.x;
210     start[1] = at1.y;
211     start[2] = at1.z;
212
213     end[0] = at2.x;
214     end[1] = at2.y;
215     end[2] = at2.z;
216
217     bonds.addElement(new Bond(start, end, at1, at2));
218   }
219
220   public void makeResidueList()
221   {
222     int count = 0;
223     Object symbol;
224     boolean nucleotide = false;
225     StringBuffer seq = new StringBuffer();
226     Vector resFeatures = new Vector();
227     Vector resAnnotation = new Vector();
228     int i, iSize = atoms.size() - 1;
229     int resNumber = -1;
230     for (i = 0; i <= iSize; i++)
231     {
232       Atom tmp = (Atom) atoms.elementAt(i);
233       resNumber = tmp.resNumber;
234       int res = resNumber;
235
236       if (i == 0)
237       {
238         offset = resNumber;
239       }
240
241       Vector resAtoms = new Vector();
242       // Add atoms to a vector while the residue number
243       // remains the same as the first atom's resNumber (res)
244       while ((resNumber == res) && (i < atoms.size()))
245       {
246         resAtoms.addElement((Atom) atoms.elementAt(i));
247         i++;
248
249         if (i < atoms.size())
250         {
251           resNumber = ((Atom) atoms.elementAt(i)).resNumber;
252         }
253         else
254         {
255           resNumber++;
256         }
257       }
258
259       // We need this to keep in step with the outer for i = loop
260       i--;
261
262       // Make a new Residue object with the new atoms vector
263       residues.addElement(new Residue(resAtoms, resNumber - 1, count));
264
265       Residue tmpres = (Residue) residues.lastElement();
266       Atom tmpat = (Atom) tmpres.atoms.elementAt(0);
267       // Make A new SequenceFeature for the current residue numbering
268       SequenceFeature sf = new SequenceFeature("RESNUM", tmpat.resName
269               + ":" + tmpat.resNumIns + " " + pdbid + id, "", offset
270               + count, offset + count, pdbid);
271       // MCview.PDBChain.PDBFILEFEATURE);
272       resFeatures.addElement(sf);
273       resAnnotation.addElement(new Annotation(tmpat.tfactor));
274       // Keep totting up the sequence
275       if ((symbol = ResidueProperties.getAA3Hash().get(tmpat.resName)) == null)
276       {
277         String nucname = tmpat.resName.trim();
278         if (ResidueProperties.nucleotideIndex[nucname.charAt(0)] == -1)
279         {
280           seq.append("X");
281           // System.err.println("PDBReader:Null aa3Hash for " +
282           // tmpat.resName);
283         }
284         else
285         {
286           // nucleotide flag
287           nucleotide = true;
288           seq.append(nucname.charAt(0));
289         }
290       }
291       else
292       {
293         if (nucleotide)
294         {
295           System.err
296                   .println("Warning: mixed nucleotide and amino acid chain.. its gonna do bad things to you!");
297         }
298         seq.append(ResidueProperties.aa[((Integer) symbol).intValue()]);
299       }
300       count++;
301     }
302
303     if (id.length() < 1)
304     {
305       id = " ";
306     }
307     isNa = nucleotide;
308     sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note:
309     // resNumber-offset
310     // ~=
311     // seq.size()
312     // Add normalised feature scores to RESNUM indicating start/end of sequence
313     // sf.setScore(offset+count);
314
315     // System.out.println("PDB Sequence is :\nSequence = " + seq);
316     // System.out.println("No of residues = " + residues.size());
317     for (i = 0, iSize = resFeatures.size(); i < iSize; i++)
318     {
319       sequence.addSequenceFeature((SequenceFeature) resFeatures
320               .elementAt(i));
321       resFeatures.setElementAt(null, i);
322     }
323     Annotation[] annots = new Annotation[resAnnotation.size()];
324     float max = 0;
325     for (i = 0, iSize = annots.length; i < iSize; i++)
326     {
327       annots[i] = (Annotation) resAnnotation.elementAt(i);
328       if (annots[i].value > max)
329         max = annots[i].value;
330       resAnnotation.setElementAt(null, i);
331     }
332     AlignmentAnnotation tfactorann = new AlignmentAnnotation(
333             "PDB.TempFactor", "Temperature Factor for "
334                     + sequence.getName(), annots, 0, max,
335             AlignmentAnnotation.LINE_GRAPH);
336     tfactorann.setSequenceRef(sequence);
337     sequence.addAlignmentAnnotation(tfactorann);
338   }
339
340   public void setChargeColours()
341   {
342     for (int i = 0; i < bonds.size(); i++)
343     {
344       try
345       {
346         Bond b = (Bond) bonds.elementAt(i);
347
348         if (b.at1.resName.equalsIgnoreCase("ASP")
349                 || b.at1.resName.equalsIgnoreCase("GLU"))
350         {
351           b.startCol = Color.red;
352         }
353         else if (b.at1.resName.equalsIgnoreCase("LYS")
354                 || b.at1.resName.equalsIgnoreCase("ARG"))
355         {
356           b.startCol = Color.blue;
357         }
358         else if (b.at1.resName.equalsIgnoreCase("CYS"))
359         {
360           b.startCol = Color.yellow;
361         }
362         else
363         {
364           b.startCol = Color.lightGray;
365         }
366
367         if (b.at2.resName.equalsIgnoreCase("ASP")
368                 || b.at2.resName.equalsIgnoreCase("GLU"))
369         {
370           b.endCol = Color.red;
371         }
372         else if (b.at2.resName.equalsIgnoreCase("LYS")
373                 || b.at2.resName.equalsIgnoreCase("ARG"))
374         {
375           b.endCol = Color.blue;
376         }
377         else if (b.at2.resName.equalsIgnoreCase("CYS"))
378         {
379           b.endCol = Color.yellow;
380         }
381         else
382         {
383           b.endCol = Color.lightGray;
384         }
385       } catch (Exception e)
386       {
387         Bond b = (Bond) bonds.elementAt(i);
388         b.startCol = Color.gray;
389         b.endCol = Color.gray;
390       }
391     }
392   }
393
394   public void setChainColours(jalview.schemes.ColourSchemeI cs)
395   {
396     Bond b;
397     int index;
398     for (int i = 0; i < bonds.size(); i++)
399     {
400       try
401       {
402         b = (Bond) bonds.elementAt(i);
403
404         index = ((Integer) ResidueProperties.aa3Hash.get(b.at1.resName))
405                 .intValue();
406         b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
407
408         index = ((Integer) ResidueProperties.aa3Hash.get(b.at2.resName))
409                 .intValue();
410         b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
411
412       } catch (Exception e)
413       {
414         b = (Bond) bonds.elementAt(i);
415         b.startCol = Color.gray;
416         b.endCol = Color.gray;
417       }
418     }
419   }
420
421   public void setChainColours(Color col)
422   {
423     for (int i = 0; i < bonds.size(); i++)
424     {
425       Bond tmp = (Bond) bonds.elementAt(i);
426       tmp.startCol = col;
427       tmp.endCol = col;
428     }
429   }
430
431   public AlignmentAnnotation[] transferResidueAnnotation(SequenceI seq,
432           String status)
433   {
434     AlignmentAnnotation[] transferred = null;
435
436     return transferred;
437
438   }
439
440   /**
441    * copy any sequence annotation onto the sequence mapped using the provided
442    * StructureMapping
443    * 
444    * @param mapping
445    */
446   public void transferResidueAnnotation(StructureMapping mapping)
447   {
448     SequenceI sq = mapping.getSequence();
449     if (sq != null)
450     {
451       if (sequence != null && sequence.getAnnotation() != null)
452       {
453
454       }
455       float min = -1, max = 0;
456       Annotation[] an = new Annotation[sq.getEnd() - sq.getStart() + 1];
457       for (int i = sq.getStart(), j = sq.getEnd(), k = 0; i <= j; i++, k++)
458       {
459         int prn = mapping.getPDBResNum(k + 1);
460
461         an[k] = new Annotation((float) prn);
462         if (min == -1)
463         {
464           min = k;
465           max = k;
466         }
467         else
468         {
469           if (min > k)
470           {
471             min = k;
472           }
473           else if (max < k)
474           {
475             max = k;
476           }
477         }
478       }
479       sq.addAlignmentAnnotation(new AlignmentAnnotation("PDB.RESNUM",
480               "PDB Residue Numbering for " + this.pdbid + ":" + this.id,
481               an, (float) min, (float) max, AlignmentAnnotation.LINE_GRAPH));
482     }
483   }
484 }