PDBChain maintains reference to dataset of sequences created by PDBfile reader.
[jalview.git] / src / MCview / PDBChain.java
1 /*
2 * Jalview - A Sequence Alignment Editor and Viewer
3 * Copyright (C) 2006 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 jalview.datamodel.*;
22
23 import jalview.schemes.ResidueProperties;
24
25 import java.awt.*;
26
27 import java.util.*;
28 import jalview.analysis.AlignSeq;
29
30
31 public class PDBChain {
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         this.pdbid=pdbid.toLowerCase();
51         this.id = id;
52     }
53
54     public String print() {
55         String tmp = "";
56
57         for (int i = 0; i < bonds.size(); i++) {
58             tmp = tmp + ((Bond) bonds.elementAt(i)).at1.resName + " " +
59                 ((Bond) bonds.elementAt(i)).at1.resNumber + " " + offset +
60                 "\n";
61         }
62
63         return tmp;
64     }
65
66     void makeExactMapping(AlignSeq as, Sequence s1)
67     {
68         int pdbpos =   as.getSeq2Start()-2;
69         int alignpos = s1.getStart() + as.getSeq1Start()-3;
70
71         for(int i=0; i<as.astr1.length(); i++)
72         {
73             if (as.astr1.charAt(i) != '-')
74             {
75               alignpos++;
76             }
77
78             if (as.astr2.charAt(i) != '-')
79             {
80               pdbpos++;
81             }
82
83             if (as.astr1.charAt(i) == as.astr2.charAt(i))
84             {
85                 Residue res = (Residue) residues.elementAt(pdbpos);
86                 Enumeration en = res.atoms.elements();
87                 while (en.hasMoreElements())
88                 {
89                   Atom atom = (Atom) en.nextElement();
90                   atom.alignmentMapping = alignpos;
91                 }
92             }
93         }
94     }
95     /**
96      * copy over the RESNUM seqfeatures from the internal chain sequence to the mapped sequence
97      * @param seq
98      * @param status The Status of the transferred annotation
99      */
100     public void transferRESNUMFeatures(SequenceI seq, String status) {
101         SequenceI sq = seq;
102         while (sq!=null) {
103             if (sq==sequence)
104                 return;
105             sq = sq.getDatasetSequence();
106         }
107         /** 
108          * Remove any existing features for this chain if they exist ?
109          * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures();
110             int totfeat=seqsfeatures.length;
111             // Remove any features for this exact chain ?
112             for (int i=0; i<seqsfeatures.length; i++) {
113             } */
114         if (status==null)
115             status = PDBChain.IEASTATUS;
116         SequenceFeature[] features = sequence.getSequenceFeatures();
117         for (int i=0; i<features.length; i++) {
118             if (features[i].getFeatureGroup().equals(PDBChain.PDBFILEFEATURE)) {
119                 SequenceFeature tx = new SequenceFeature(features[i]);
120                 tx.setBegin(1+((Atom)((Residue)residues.elementAt(tx.getBegin()-offset)).atoms.elementAt(0)).alignmentMapping);
121                 tx.setEnd(1+((Atom)((Residue)residues.elementAt(tx.getEnd()-offset)).atoms.elementAt(0)).alignmentMapping);
122                 tx.setStatus(status+((tx.getStatus()==null || tx.getStatus().length()==0) ? "" : ":"+tx.getStatus()));
123                 seq.addSequenceFeature(tx);
124             }
125         }
126     }
127
128     public void makeCaBondList()
129     {
130         for (int i = 0; i < (residues.size() - 1); i++)
131         {
132             Residue tmpres = (Residue) residues.elementAt(i);
133             Residue tmpres2 = (Residue) residues.elementAt(i + 1);
134             Atom at1 = tmpres.findAtom("CA");
135             Atom at2 = tmpres2.findAtom("CA");
136
137             if ((at1 != null) && (at2 != null))
138             {
139                 if (at1.chain.equals(at2.chain))
140                 {
141                     makeBond(at1, at2);
142                 }
143             }
144             else
145               System.out.println("not found "+i);
146         }
147     }
148
149     public void makeBond(Atom at1, Atom at2) {
150         float[] start = new float[3];
151         float[] end = new float[3];
152
153         start[0] = at1.x;
154         start[1] = at1.y;
155         start[2] = at1.z;
156
157         end[0] = at2.x;
158         end[1] = at2.y;
159         end[2] = at2.z;
160
161         bonds.addElement(new Bond(start, end, at1, at2));
162     }
163
164     public void makeResidueList() {
165         int count = 0;
166         StringBuffer seq = new StringBuffer();
167         Vector resFeatures=new Vector();
168         int i, iSize = atoms.size()-1;
169         int resNumber=-1;
170         for (i = 0; i <= iSize; i++)
171         {
172             Atom tmp = (Atom) atoms.elementAt(i);
173             resNumber = tmp.resNumber;
174             int res = resNumber;
175
176             if (i == 0) {
177                 offset = resNumber;
178             }
179
180             Vector resAtoms = new Vector();
181             //Add atoms to a vector while the residue number
182             //remains the same as the first atom's resNumber (res)
183             while ((resNumber == res) && (i < atoms.size())) {
184                 resAtoms.addElement((Atom) atoms.elementAt(i));
185                 i++;
186
187                 if (i < atoms.size()) {
188                     resNumber = ((Atom) atoms.elementAt(i)).resNumber;
189                 } else {
190                     resNumber++;
191                 }
192             }
193
194             //We need this to keep in step with the outer for i = loop
195             i--;
196
197             //Make a new Residue object with the new atoms vector
198             residues.addElement(new Residue(resAtoms, resNumber - 1, count));
199             
200             Residue tmpres = (Residue) residues.lastElement();
201             Atom tmpat = (Atom) tmpres.atoms.elementAt(0);
202             // Make A new SequenceFeature for the current residue numbering
203             SequenceFeature sf = 
204                 new SequenceFeature("RESNUM",tmpat.resName+":"+tmpat.resNumIns+" "+pdbid+id,
205                         "",offset+count,offset+count,MCview.PDBChain.PDBFILEFEATURE);
206             resFeatures.addElement(sf);
207             // Keep totting up the sequence
208             if (ResidueProperties.getAA3Hash().get(tmpat.resName) == null)
209             {
210                 seq.append("X") ;
211                //  System.err.println("PDBReader:Null aa3Hash for " +
212                //     tmpat.resName);
213             } else {
214
215                 seq.append(ResidueProperties.aa[((Integer) ResidueProperties.getAA3Hash()
216                                                                                   .get(tmpat.resName)).intValue()]);
217             }
218             count++;
219         }
220
221         if(id.length()<1 || id.equals(" "))
222            id = "_";
223         
224         sequence = new Sequence(id, seq.toString(), offset, resNumber-1); // Note: resNumber-offset ~= seq.size() 
225       //  System.out.println("PDB Sequence is :\nSequence = " + seq);
226      //   System.out.println("No of residues = " + residues.size());
227         for (i=0,iSize=resFeatures.size(); i<iSize; i++) {
228             sequence.addSequenceFeature((SequenceFeature) resFeatures.elementAt(i));
229             resFeatures.setElementAt(null, i);
230         }
231    }
232
233     public void setChargeColours() {
234         for (int i = 0; i < bonds.size(); i++) {
235             try {
236                 Bond b = (Bond) bonds.elementAt(i);
237
238                 if (b.at1.resName.equalsIgnoreCase("ASP") ||
239                         b.at1.resName.equalsIgnoreCase("GLU")) {
240                     b.startCol = Color.red;
241                 } else if (b.at1.resName.equalsIgnoreCase("LYS") ||
242                         b.at1.resName.equalsIgnoreCase("ARG")) {
243                     b.startCol = Color.blue;
244                 } else if (b.at1.resName.equalsIgnoreCase("CYS")) {
245                     b.startCol = Color.yellow;
246                 } else {
247                     //int atno = ((Integer) ResidueProperties.getAA3Hash().get(b.at1.resName.toUpperCase())).intValue();
248                     b.startCol = Color.lightGray;
249                 }
250
251                 if (b.at2.resName.equalsIgnoreCase("ASP") ||
252                         b.at2.resName.equalsIgnoreCase("GLU")) {
253                     b.endCol = Color.red;
254                 } else if (b.at2.resName.equalsIgnoreCase("LYS") ||
255                         b.at2.resName.equalsIgnoreCase("ARG")) {
256                     b.endCol = Color.blue;
257                 } else if (b.at2.resName.equalsIgnoreCase("CYS")) {
258                     b.endCol = Color.yellow;
259                 } else {
260                     //int atno = ((Integer) ResidueProperties.getAA3Hash().get(b.at2.resName.toUpperCase())).intValue();
261                     b.endCol = Color.lightGray;
262                 }
263             } catch (Exception e) {
264                 Bond b = (Bond) bonds.elementAt(i);
265                 b.startCol = Color.gray;
266                 b.endCol = Color.gray;
267             }
268         }
269     }
270
271
272     public void setChainColours(jalview.schemes.ColourSchemeI cs)
273     {
274         Bond b;
275         for (int i = 0; i < bonds.size(); i++) {
276             try {
277               b = (Bond) bonds.elementAt(i);
278
279             /*  ( (Bond) bonds.elementAt(i)).startCol = cs.findColour(
280                   ResidueProperties.codonTranslate(
281                       ResidueProperties.aa3Hash.get(b.at1.resName).toString().charAt(0)
282                   );
283
284               b.endCol = cs.findColour(
285                   ResidueProperties.aa[ ( (Integer) ResidueProperties.aa3Hash.
286                                          get(b.at2.resName)).intValue()]
287                   );*/
288
289             } catch (Exception e)
290             {
291                 b = (Bond) bonds.elementAt(i);
292                 b.startCol = Color.gray;
293                 b.endCol = Color.gray;
294             }
295         }
296     }
297
298
299
300     public void setChainColours(Color col)
301     {
302         for (int i = 0; i < bonds.size(); i++)
303         {
304             Bond tmp = (Bond) bonds.elementAt(i);
305             tmp.startCol = col;
306             tmp.endCol = col;
307         }
308     }
309 }