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