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