Jalview 2.6 source licence
[jalview.git] / src / MCview / PDBfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.6)
3  * Copyright (C) 2010 J Procter, AM Waterhouse, G Barton, M Clamp, S Searle
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
10  * 
11  * Jalview is distributed in the hope that it will be useful, but 
12  * WITHOUT ANY WARRANTY; without even the implied warranty 
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14  * PURPOSE.  See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 package MCview;
19
20 import java.io.*;
21 import java.util.*;
22
23 import java.awt.*;
24
25 import jalview.datamodel.*;
26 import jalview.io.FileParse;
27
28 public class PDBfile extends jalview.io.AlignFile
29 {
30   public Vector chains;
31
32   public String id;
33
34   /**
35    * set to true to add chain alignment annotation as visible annotation.
36    */
37   boolean VisibleChainAnnotation = false;
38
39   public PDBfile(String inFile, String inType) throws IOException
40   {
41     super(inFile, inType);
42   }
43
44   public PDBfile(FileParse source) throws IOException
45   {
46     super(source);
47   }
48
49   public String print()
50   {
51     return null;
52   }
53
54   public void parse() throws IOException
55   {
56     // TODO set the filename sensibly - try using data source name.
57     id = safeName(getDataName());
58
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             {
80               tid = line.substring(62, 67).trim();
81             }
82             else
83             {
84               tid = line.substring(62).trim();
85             }
86             if (tid.length() > 0)
87             {
88               id = tid;
89             }
90             continue;
91           }
92         }
93         // Were we to do anything with SEQRES - we start it here
94         if (line.indexOf("SEQRES") == 0)
95         {
96         }
97
98         if (line.indexOf("MODEL") == 0)
99         {
100           modelFlag = true;
101         }
102
103         if (line.indexOf("TER") == 0)
104         {
105           terFlag = true;
106         }
107
108         if (modelFlag && line.indexOf("ENDMDL") == 0)
109         {
110           break;
111         }
112         if (line.indexOf("ATOM") == 0
113                 || (line.indexOf("HETATM") == 0 && !terFlag))
114         {
115           terFlag = false;
116
117           // Jalview is only interested in CA bonds????
118           atomnam = line.substring(12, 15).trim();
119           if (!atomnam.equals("CA") && !atomnam.equals("P"))
120           {
121             continue;
122           }
123
124           Atom tmpatom = new Atom(line);
125           tmpchain = findChain(tmpatom.chain);
126           if (tmpchain != null)
127           {
128             if (tmpatom.resNumIns.trim().equals(lastID))
129             {
130               // phosphorylated protein - seen both CA and P..
131               continue;
132             }
133             tmpchain.atoms.addElement(tmpatom);
134           }
135           else
136           {
137             tmpchain = new PDBChain(id, tmpatom.chain);
138             chains.addElement(tmpchain);
139             tmpchain.atoms.addElement(tmpatom);
140           }
141           lastID = tmpatom.resNumIns.trim();
142         }
143         index++;
144       }
145
146       makeResidueList();
147       makeCaBondList();
148
149       if (id == null)
150       {
151         id = inFile.getName();
152       }
153       for (int i = 0; i < chains.size(); i++)
154       {
155         SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
156         dataset.setName(id + "|" + dataset.getName());
157         PDBEntry entry = new PDBEntry();
158         entry.setId(id);
159         if (inFile != null)
160         {
161           entry.setFile(inFile.getAbsolutePath());
162         }
163         else
164         {
165           // TODO: decide if we should dump the datasource to disk
166           entry.setFile(getDataName());
167         }
168         dataset.addPDBId(entry);
169         SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
170         // maintain reference to
171         // dataset
172         seqs.addElement(chainseq);
173         AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
174         if (chainannot != null)
175         {
176           for (int ai = 0; ai < chainannot.length; ai++)
177           {
178             chainannot[ai].visible = VisibleChainAnnotation;
179             annotations.addElement(chainannot[ai]);
180           }
181         }
182       }
183     } catch (OutOfMemoryError er)
184     {
185       System.out.println("OUT OF MEMORY LOADING PDB FILE");
186       throw new IOException("Out of memory loading PDB File");
187     }
188   }
189
190   /**
191    * make a friendly ID string.
192    * 
193    * @param dataName
194    * @return truncated dataName to after last '/'
195    */
196   private String safeName(String dataName)
197   {
198     int p = 0;
199     while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
200     {
201       dataName = dataName.substring(p + 1);
202     }
203     return dataName;
204   }
205
206   public void makeResidueList()
207   {
208     for (int i = 0; i < chains.size(); i++)
209     {
210       ((PDBChain) chains.elementAt(i)).makeResidueList();
211     }
212   }
213
214   public void makeCaBondList()
215   {
216     for (int i = 0; i < chains.size(); i++)
217     {
218       ((PDBChain) chains.elementAt(i)).makeCaBondList();
219     }
220   }
221
222   public PDBChain findChain(String id)
223   {
224     for (int i = 0; i < chains.size(); i++)
225     {
226       if (((PDBChain) chains.elementAt(i)).id.equals(id))
227       {
228         return (PDBChain) chains.elementAt(i);
229       }
230     }
231
232     return null;
233   }
234
235   public void setChargeColours()
236   {
237     for (int i = 0; i < chains.size(); i++)
238     {
239       ((PDBChain) chains.elementAt(i)).setChargeColours();
240     }
241   }
242
243   public void setColours(jalview.schemes.ColourSchemeI cs)
244   {
245     for (int i = 0; i < chains.size(); i++)
246     {
247       ((PDBChain) chains.elementAt(i)).setChainColours(cs);
248     }
249   }
250
251   public void setChainColours()
252   {
253     for (int i = 0; i < chains.size(); i++)
254     {
255       ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
256               1.0f / (float) i, .4f, 1.0f));
257     }
258   }
259 }