transfer resnum features now returns features (could be passed to feature renderer)
[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      * @return the features added to sq (or its dataset)
100      */
101     public SequenceFeature[] transferRESNUMFeatures(SequenceI seq, String status) {
102         SequenceI sq = seq;
103         while (sq!=null) {
104             if (sq==sequence)
105                 return null;
106             sq = sq.getDatasetSequence();
107         }
108         /** 
109          * Remove any existing features for this chain if they exist ?
110          * SequenceFeature[] seqsfeatures=seq.getSequenceFeatures();
111             int totfeat=seqsfeatures.length;
112             // Remove any features for this exact chain ?
113             for (int i=0; i<seqsfeatures.length; i++) {
114             } */
115         if (status==null)
116             status = PDBChain.IEASTATUS;
117         SequenceFeature[] features = sequence.getSequenceFeatures();
118         for (int i=0; i<features.length; i++) {
119             if (features[i].getFeatureGroup().equals(PDBChain.PDBFILEFEATURE)) {
120                 SequenceFeature tx = new SequenceFeature(features[i]);
121                 tx.setBegin(1+((Atom)((Residue)residues.elementAt(tx.getBegin()-offset)).atoms.elementAt(0)).alignmentMapping);
122                 tx.setEnd(1+((Atom)((Residue)residues.elementAt(tx.getEnd()-offset)).atoms.elementAt(0)).alignmentMapping);
123                 tx.setStatus(status+((tx.getStatus()==null || tx.getStatus().length()==0) ? "" : ":"+tx.getStatus()));
124                 seq.addSequenceFeature(tx);
125             }
126         }
127         return features;
128     }
129
130     public void makeCaBondList()
131     {
132         for (int i = 0; i < (residues.size() - 1); i++)
133         {
134             Residue tmpres = (Residue) residues.elementAt(i);
135             Residue tmpres2 = (Residue) residues.elementAt(i + 1);
136             Atom at1 = tmpres.findAtom("CA");
137             Atom at2 = tmpres2.findAtom("CA");
138
139             if ((at1 != null) && (at2 != null))
140             {
141                 if (at1.chain.equals(at2.chain))
142                 {
143                     makeBond(at1, at2);
144                 }
145             }
146             else
147               System.out.println("not found "+i);
148         }
149     }
150
151     public void makeBond(Atom at1, Atom at2) {
152         float[] start = new float[3];
153         float[] end = new float[3];
154
155         start[0] = at1.x;
156         start[1] = at1.y;
157         start[2] = at1.z;
158
159         end[0] = at2.x;
160         end[1] = at2.y;
161         end[2] = at2.z;
162
163         bonds.addElement(new Bond(start, end, at1, at2));
164     }
165
166     public void makeResidueList() {
167         int count = 0;
168         StringBuffer seq = new StringBuffer();
169         Vector resFeatures=new Vector();
170         int i, iSize = atoms.size()-1;
171         int resNumber=-1;
172         for (i = 0; i <= iSize; i++)
173         {
174             Atom tmp = (Atom) atoms.elementAt(i);
175             resNumber = tmp.resNumber;
176             int res = resNumber;
177
178             if (i == 0) {
179                 offset = resNumber;
180             }
181
182             Vector resAtoms = new Vector();
183             //Add atoms to a vector while the residue number
184             //remains the same as the first atom's resNumber (res)
185             while ((resNumber == res) && (i < atoms.size())) {
186                 resAtoms.addElement((Atom) atoms.elementAt(i));
187                 i++;
188
189                 if (i < atoms.size()) {
190                     resNumber = ((Atom) atoms.elementAt(i)).resNumber;
191                 } else {
192                     resNumber++;
193                 }
194             }
195
196             //We need this to keep in step with the outer for i = loop
197             i--;
198
199             //Make a new Residue object with the new atoms vector
200             residues.addElement(new Residue(resAtoms, resNumber - 1, count));
201             
202             Residue tmpres = (Residue) residues.lastElement();
203             Atom tmpat = (Atom) tmpres.atoms.elementAt(0);
204             // Make A new SequenceFeature for the current residue numbering
205             SequenceFeature sf = 
206                 new SequenceFeature("RESNUM",tmpat.resName+":"+tmpat.resNumIns+" "+pdbid+id,
207                         "",offset+count,offset+count,MCview.PDBChain.PDBFILEFEATURE);
208             resFeatures.addElement(sf);
209             // Keep totting up the sequence
210             if (ResidueProperties.getAA3Hash().get(tmpat.resName) == null)
211             {
212                 seq.append("X") ;
213                //  System.err.println("PDBReader:Null aa3Hash for " +
214                //     tmpat.resName);
215             } else {
216
217                 seq.append(ResidueProperties.aa[((Integer) ResidueProperties.getAA3Hash()
218                                                                                   .get(tmpat.resName)).intValue()]);
219             }
220             count++;
221         }
222
223         if(id.length()<1 || id.equals(" "))
224            id = "_";
225         
226         sequence = new Sequence(id, seq.toString(), offset, resNumber-1); // Note: resNumber-offset ~= seq.size() 
227       //  System.out.println("PDB Sequence is :\nSequence = " + seq);
228      //   System.out.println("No of residues = " + residues.size());
229         for (i=0,iSize=resFeatures.size(); i<iSize; i++) {
230             sequence.addSequenceFeature((SequenceFeature) resFeatures.elementAt(i));
231             resFeatures.setElementAt(null, i);
232         }
233    }
234
235     public void setChargeColours() {
236         for (int i = 0; i < bonds.size(); i++) {
237             try {
238                 Bond b = (Bond) bonds.elementAt(i);
239
240                 if (b.at1.resName.equalsIgnoreCase("ASP") ||
241                         b.at1.resName.equalsIgnoreCase("GLU")) {
242                     b.startCol = Color.red;
243                 } else if (b.at1.resName.equalsIgnoreCase("LYS") ||
244                         b.at1.resName.equalsIgnoreCase("ARG")) {
245                     b.startCol = Color.blue;
246                 } else if (b.at1.resName.equalsIgnoreCase("CYS")) {
247                     b.startCol = Color.yellow;
248                 } else {
249                     //int atno = ((Integer) ResidueProperties.getAA3Hash().get(b.at1.resName.toUpperCase())).intValue();
250                     b.startCol = Color.lightGray;
251                 }
252
253                 if (b.at2.resName.equalsIgnoreCase("ASP") ||
254                         b.at2.resName.equalsIgnoreCase("GLU")) {
255                     b.endCol = Color.red;
256                 } else if (b.at2.resName.equalsIgnoreCase("LYS") ||
257                         b.at2.resName.equalsIgnoreCase("ARG")) {
258                     b.endCol = Color.blue;
259                 } else if (b.at2.resName.equalsIgnoreCase("CYS")) {
260                     b.endCol = Color.yellow;
261                 } else {
262                     //int atno = ((Integer) ResidueProperties.getAA3Hash().get(b.at2.resName.toUpperCase())).intValue();
263                     b.endCol = Color.lightGray;
264                 }
265             } catch (Exception e) {
266                 Bond b = (Bond) bonds.elementAt(i);
267                 b.startCol = Color.gray;
268                 b.endCol = Color.gray;
269             }
270         }
271     }
272
273
274     public void setChainColours(jalview.schemes.ColourSchemeI cs)
275     {
276         Bond b;
277         for (int i = 0; i < bonds.size(); i++) {
278             try {
279               b = (Bond) bonds.elementAt(i);
280
281             /*  ( (Bond) bonds.elementAt(i)).startCol = cs.findColour(
282                   ResidueProperties.codonTranslate(
283                       ResidueProperties.aa3Hash.get(b.at1.resName).toString().charAt(0)
284                   );
285
286               b.endCol = cs.findColour(
287                   ResidueProperties.aa[ ( (Integer) ResidueProperties.aa3Hash.
288                                          get(b.at2.resName)).intValue()]
289                   );*/
290
291             } catch (Exception e)
292             {
293                 b = (Bond) bonds.elementAt(i);
294                 b.startCol = Color.gray;
295                 b.endCol = Color.gray;
296             }
297         }
298     }
299
300
301
302     public void setChainColours(Color col)
303     {
304         for (int i = 0; i < bonds.size(); i++)
305         {
306             Bond tmp = (Bond) bonds.elementAt(i);
307             tmp.startCol = col;
308             tmp.endCol = col;
309         }
310     }
311 }