blank space for chain name if empty
[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
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   private static final String IEASTATUS = "IEA:jalview";
36   public String id;
37   public Vector bonds = new Vector();
38   public Vector atoms = new Vector();
39   public Vector residues = new Vector();
40   public int offset;
41   public Sequence sequence;
42   public boolean isVisible = true;
43   public int pdbstart = 0;
44   public int pdbend = 0;
45   public int seqstart = 0;
46   public int seqend = 0;
47   public String pdbid = "";
48   public PDBChain(String pdbid, String id)
49   {
50     this.pdbid = pdbid.toLowerCase();
51     this.id = id;
52   }
53
54   public String print()
55   {
56     String tmp = "";
57
58     for (int i = 0; i < bonds.size(); i++)
59     {
60       tmp = tmp + ( (Bond) bonds.elementAt(i)).at1.resName + " " +
61           ( (Bond) bonds.elementAt(i)).at1.resNumber + " " + offset +
62           "\n";
63     }
64
65     return tmp;
66   }
67
68   public void makeExactMapping(AlignSeq as, SequenceI s1)
69   {
70     int pdbpos = as.getSeq2Start() - 2;
71     int alignpos = s1.getStart() + as.getSeq1Start() - 3;
72
73     for (int i = 0; i < as.astr1.length(); i++)
74     {
75       if (as.astr1.charAt(i) != '-')
76       {
77         alignpos++;
78       }
79
80       if (as.astr2.charAt(i) != '-')
81       {
82         pdbpos++;
83       }
84
85       if (as.astr1.charAt(i) == as.astr2.charAt(i))
86       {
87         Residue res = (Residue) residues.elementAt(pdbpos);
88         Enumeration en = res.atoms.elements();
89         while (en.hasMoreElements())
90         {
91           Atom atom = (Atom) en.nextElement();
92           atom.alignmentMapping = alignpos;
93         }
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)
108     {
109       if (sq == sequence)
110       {
111         return null;
112       }
113       sq = sq.getDatasetSequence();
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(PDBChain.PDBFILEFEATURE))
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         seq.addSequenceFeature(tx);
142       }
143     }
144     return features;
145   }
146
147   public void makeCaBondList()
148   {
149     for (int i = 0; i < (residues.size() - 1); i++)
150     {
151       Residue tmpres = (Residue) residues.elementAt(i);
152       Residue tmpres2 = (Residue) residues.elementAt(i + 1);
153       Atom at1 = tmpres.findAtom("CA");
154       Atom at2 = tmpres2.findAtom("CA");
155
156       if ( (at1 != null) && (at2 != null))
157       {
158         if (at1.chain.equals(at2.chain))
159         {
160           makeBond(at1, at2);
161         }
162       }
163       else
164       {
165         System.out.println("not found " + i);
166       }
167     }
168   }
169
170   public void makeBond(Atom at1, Atom at2)
171   {
172     float[] start = new float[3];
173     float[] end = new float[3];
174
175     start[0] = at1.x;
176     start[1] = at1.y;
177     start[2] = at1.z;
178
179     end[0] = at2.x;
180     end[1] = at2.y;
181     end[2] = at2.z;
182
183     bonds.addElement(new Bond(start, end, at1, at2));
184   }
185
186   public void makeResidueList()
187   {
188     int count = 0;
189     StringBuffer seq = new StringBuffer();
190     Vector resFeatures = new Vector();
191     int i, iSize = atoms.size() - 1;
192     int resNumber = -1;
193     for (i = 0; i <= iSize; i++)
194     {
195       Atom tmp = (Atom) atoms.elementAt(i);
196       resNumber = tmp.resNumber;
197       int res = resNumber;
198
199       if (i == 0)
200       {
201         offset = resNumber;
202       }
203
204       Vector resAtoms = new Vector();
205       //Add atoms to a vector while the residue number
206       //remains the same as the first atom's resNumber (res)
207       while ( (resNumber == res) && (i < atoms.size()))
208       {
209         resAtoms.addElement( (Atom) atoms.elementAt(i));
210         i++;
211
212         if (i < atoms.size())
213         {
214           resNumber = ( (Atom) atoms.elementAt(i)).resNumber;
215         }
216         else
217         {
218           resNumber++;
219         }
220       }
221
222       //We need this to keep in step with the outer for i = loop
223       i--;
224
225       //Make a new Residue object with the new atoms vector
226       residues.addElement(new Residue(resAtoms, resNumber - 1, count));
227
228       Residue tmpres = (Residue) residues.lastElement();
229       Atom tmpat = (Atom) tmpres.atoms.elementAt(0);
230       // Make A new SequenceFeature for the current residue numbering
231       SequenceFeature sf =
232           new SequenceFeature("RESNUM",
233                               tmpat.resName + ":" + tmpat.resNumIns + " " +
234                               pdbid + id,
235                               "", offset + count, offset + count,
236                               MCview.PDBChain.PDBFILEFEATURE);
237       resFeatures.addElement(sf);
238       // Keep totting up the sequence
239       if (ResidueProperties.getAA3Hash().get(tmpat.resName) == null)
240       {
241         seq.append("X");
242         //  System.err.println("PDBReader:Null aa3Hash for " +
243         //     tmpat.resName);
244       }
245       else
246       {
247
248         seq.append(ResidueProperties.aa[ ( (Integer) ResidueProperties.
249                                           getAA3Hash()
250                                           .get(tmpat.resName)).intValue()]);
251       }
252       count++;
253     }
254
255     if (id.length() < 1)
256     {
257       id = " ";
258     }
259
260     sequence = new Sequence(id, seq.toString(), offset, resNumber - 1); // Note: resNumber-offset ~= seq.size()
261     //  System.out.println("PDB Sequence is :\nSequence = " + seq);
262     //   System.out.println("No of residues = " + residues.size());
263     for (i = 0, iSize = resFeatures.size(); i < iSize; i++)
264     {
265       sequence.addSequenceFeature( (SequenceFeature) resFeatures.elementAt(i));
266       resFeatures.setElementAt(null, i);
267     }
268   }
269
270   public void setChargeColours()
271   {
272     for (int i = 0; i < bonds.size(); i++)
273     {
274       try
275       {
276         Bond b = (Bond) bonds.elementAt(i);
277
278         if (b.at1.resName.equalsIgnoreCase("ASP") ||
279             b.at1.resName.equalsIgnoreCase("GLU"))
280         {
281           b.startCol = Color.red;
282         }
283         else if (b.at1.resName.equalsIgnoreCase("LYS") ||
284                  b.at1.resName.equalsIgnoreCase("ARG"))
285         {
286           b.startCol = Color.blue;
287         }
288         else if (b.at1.resName.equalsIgnoreCase("CYS"))
289         {
290           b.startCol = Color.yellow;
291         }
292         else
293         {
294           b.startCol = Color.lightGray;
295         }
296
297         if (b.at2.resName.equalsIgnoreCase("ASP") ||
298             b.at2.resName.equalsIgnoreCase("GLU"))
299         {
300           b.endCol = Color.red;
301         }
302         else if (b.at2.resName.equalsIgnoreCase("LYS") ||
303                  b.at2.resName.equalsIgnoreCase("ARG"))
304         {
305           b.endCol = Color.blue;
306         }
307         else if (b.at2.resName.equalsIgnoreCase("CYS"))
308         {
309           b.endCol = Color.yellow;
310         }
311         else
312         {
313           b.endCol = Color.lightGray;
314         }
315       }
316       catch (Exception e)
317       {
318         Bond b = (Bond) bonds.elementAt(i);
319         b.startCol = Color.gray;
320         b.endCol = Color.gray;
321       }
322     }
323   }
324
325   public void setChainColours(jalview.schemes.ColourSchemeI cs)
326   {
327     Bond b;
328     int index;
329     for (int i = 0; i < bonds.size(); i++)
330     {
331       try
332       {
333         b = (Bond) bonds.elementAt(i);
334
335         index = ( (Integer) ResidueProperties.aa3Hash.get(b.at1.
336             resName)).intValue();
337         b.startCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
338
339         index = ( (Integer) ResidueProperties.aa3Hash.get(b.at2.resName)).
340             intValue();
341         b.endCol = cs.findColour(ResidueProperties.aa[index].charAt(0));
342
343       }
344       catch (Exception e)
345       {
346         b = (Bond) bonds.elementAt(i);
347         b.startCol = Color.gray;
348         b.endCol = Color.gray;
349       }
350     }
351   }
352
353   public void setChainColours(Color col)
354   {
355     for (int i = 0; i < bonds.size(); i++)
356     {
357       Bond tmp = (Bond) bonds.elementAt(i);
358       tmp.startCol = col;
359       tmp.endCol = col;
360     }
361   }
362 }