update author list in license for (JAL-826)
[jalview.git] / src / MCview / PDBfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.7)
3  * Copyright (C) 2011 J Procter, AM Waterhouse, J Engelhardt, LM Lui, 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       chains = new Vector();
60
61       PDBChain tmpchain;
62       String line=null;
63       boolean modelFlag = false;
64       boolean terFlag = false;
65       String lastID = "";
66
67       int index = 0;
68       String atomnam = null;
69       try
70       {
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     catch (NumberFormatException ex)
189     {
190       if (line!=null) {
191         System.err.println("Couldn't read number from line:");
192         System.err.println(line);
193       }
194     }
195   }
196
197   /**
198    * make a friendly ID string.
199    * 
200    * @param dataName
201    * @return truncated dataName to after last '/'
202    */
203   private String safeName(String dataName)
204   {
205     int p = 0;
206     while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
207     {
208       dataName = dataName.substring(p + 1);
209     }
210     return dataName;
211   }
212
213   public void makeResidueList()
214   {
215     for (int i = 0; i < chains.size(); i++)
216     {
217       ((PDBChain) chains.elementAt(i)).makeResidueList();
218     }
219   }
220
221   public void makeCaBondList()
222   {
223     for (int i = 0; i < chains.size(); i++)
224     {
225       ((PDBChain) chains.elementAt(i)).makeCaBondList();
226     }
227   }
228
229   public PDBChain findChain(String id)
230   {
231     for (int i = 0; i < chains.size(); i++)
232     {
233       if (((PDBChain) chains.elementAt(i)).id.equals(id))
234       {
235         return (PDBChain) chains.elementAt(i);
236       }
237     }
238
239     return null;
240   }
241
242   public void setChargeColours()
243   {
244     for (int i = 0; i < chains.size(); i++)
245     {
246       ((PDBChain) chains.elementAt(i)).setChargeColours();
247     }
248   }
249
250   public void setColours(jalview.schemes.ColourSchemeI cs)
251   {
252     for (int i = 0; i < chains.size(); i++)
253     {
254       ((PDBChain) chains.elementAt(i)).setChainColours(cs);
255     }
256   }
257
258   public void setChainColours()
259   {
260     for (int i = 0; i < chains.size(); i++)
261     {
262       ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
263               1.0f / (float) i, .4f, 1.0f));
264     }
265   }
266 }