na pdb: detect P backbone
[jalview.git] / src / MCview / PDBfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Development Version 2.4.1)
3  * Copyright (C) 2009 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.io.*;
22 import java.util.*;
23
24 import java.awt.*;
25
26 import jalview.datamodel.*;
27 import jalview.io.FileParse;
28
29 public class PDBfile extends jalview.io.AlignFile
30 {
31   public Vector chains;
32
33   public String id;
34
35   /**
36    * set to true to add chain alignment annotation as visible annotation.
37    */
38   boolean VisibleChainAnnotation = false;
39
40   public PDBfile(String inFile, String inType) throws IOException
41   {
42     super(inFile, inType);
43   }
44
45   public PDBfile(FileParse source) throws IOException
46   {
47     super(source);
48   }
49
50   public String print()
51   {
52     return null;
53   }
54
55   public void parse() throws IOException
56   {
57     // TODO set the filename sensibly
58     id = (inFile == null || inFile.getName()==null || inFile.getName().length()==0) ? "PDBFILE" : inFile.getName();
59     try
60     {
61       chains = new Vector();
62
63       PDBChain tmpchain;
64       String line;
65       boolean modelFlag = false;
66       boolean terFlag = false;
67       String lastID="";
68
69       int index = 0;
70       String atomnam=null;
71       while ((line = nextLine()) != null)
72       {
73         if (line.indexOf("HEADER") == 0)
74         {
75           if (line.length()>62)
76           {
77             String tid;
78             if (line.length()>67) {
79               tid = line.substring(62, 67).trim();
80             } else {
81               tid=line.substring(62).trim();
82             }
83             if (tid.length()>0)
84             {
85               id = tid;
86             }
87             continue;
88           }
89         }
90         // Were we to do anything with SEQRES - we start it here
91         if (line.indexOf("SEQRES") == 0)
92         {
93         }
94
95         if (line.indexOf("MODEL") == 0)
96         {
97           modelFlag = true;
98         }
99
100         if (line.indexOf("TER") == 0)
101         {
102           terFlag = true;
103         }
104
105         if (modelFlag && line.indexOf("ENDMDL") == 0)
106         {
107           break;
108         }
109         if (line.indexOf("ATOM") == 0
110                 || (line.indexOf("HETATM") == 0 && !terFlag))
111         {
112           terFlag = false;
113
114           // Jalview is only interested in CA bonds????
115           atomnam = line.substring(12, 15).trim();
116           if (!atomnam.equals("CA") && !atomnam.equals("P"))
117           {
118             continue;
119           }
120
121           Atom tmpatom = new Atom(line);
122           tmpchain = findChain(tmpatom.chain);
123           if (tmpchain != null)
124           {
125             if (tmpatom.resNumIns.trim().equals(lastID))
126             {
127               //  phosphorylated protein - seen both CA and P..
128               continue;
129             }
130             tmpchain.atoms.addElement(tmpatom);
131           }
132           else
133           {
134             tmpchain = new PDBChain(id, tmpatom.chain);
135             chains.addElement(tmpchain);
136             tmpchain.atoms.addElement(tmpatom);
137           }
138           lastID = tmpatom.resNumIns.trim();
139         }
140         index++;
141       }
142
143       makeResidueList();
144       makeCaBondList();
145
146       if (id == null)
147       {
148         id = inFile.getName();
149       }
150       for (int i = 0; i < chains.size(); i++)
151       {
152         SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
153         dataset.setName(id + "|" + dataset.getName());
154         PDBEntry entry = new PDBEntry();
155         entry.setId(id);
156         if (inFile != null)
157         {
158           entry.setFile(inFile.getAbsolutePath());
159         }
160         dataset.addPDBId(entry);
161         SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
162                                                         // maintain reference to
163                                                         // dataset
164         seqs.addElement(chainseq);
165         AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
166         if (chainannot != null)
167         {
168           for (int ai = 0; ai < chainannot.length; ai++)
169           {
170             chainannot[ai].visible = VisibleChainAnnotation;
171             annotations.addElement(chainannot[ai]);
172           }
173         }
174       }
175     } catch (OutOfMemoryError er)
176     {
177       System.out.println("OUT OF MEMORY LOADING PDB FILE");
178       throw new IOException("Out of memory loading PDB File");
179     }
180   }
181
182   public void makeResidueList()
183   {
184     for (int i = 0; i < chains.size(); i++)
185     {
186       ((PDBChain) chains.elementAt(i)).makeResidueList();
187     }
188   }
189
190   public void makeCaBondList()
191   {
192     for (int i = 0; i < chains.size(); i++)
193     {
194       ((PDBChain) chains.elementAt(i)).makeCaBondList();
195     }
196   }
197
198   public PDBChain findChain(String id)
199   {
200     for (int i = 0; i < chains.size(); i++)
201     {
202       if (((PDBChain) chains.elementAt(i)).id.equals(id))
203       {
204         return (PDBChain) chains.elementAt(i);
205       }
206     }
207
208     return null;
209   }
210
211   public void setChargeColours()
212   {
213     for (int i = 0; i < chains.size(); i++)
214     {
215       ((PDBChain) chains.elementAt(i)).setChargeColours();
216     }
217   }
218
219   public void setColours(jalview.schemes.ColourSchemeI cs)
220   {
221     for (int i = 0; i < chains.size(); i++)
222     {
223       ((PDBChain) chains.elementAt(i)).setChainColours(cs);
224     }
225   }
226
227   public void setChainColours()
228   {
229     for (int i = 0; i < chains.size(); i++)
230     {
231       ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
232               1.0f / (float) i, .4f, 1.0f));
233     }
234   }
235 }