JAL-1432 updated copyright notices
[jalview.git] / src / MCview / PDBfile.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
3  * Copyright (C) 2014 The Jalview Authors
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  * The Jalview Authors are detailed in the 'AUTHORS' file.
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 - try using data source name.
58     id = safeName(getDataName());
59
60     chains = new Vector();
61
62     PDBChain tmpchain;
63     String line = null;
64     boolean modelFlag = false;
65     boolean terFlag = false;
66     String lastID = "";
67
68     int index = 0;
69     String atomnam = null;
70     try
71     {
72       while ((line = nextLine()) != null)
73       {
74         if (line.indexOf("HEADER") == 0)
75         {
76           if (line.length() > 62)
77           {
78             String tid;
79             if (line.length() > 67)
80             {
81               tid = line.substring(62, 67).trim();
82             }
83             else
84             {
85               tid = line.substring(62).trim();
86             }
87             if (tid.length() > 0)
88             {
89               id = tid;
90             }
91             continue;
92           }
93         }
94         // Were we to do anything with SEQRES - we start it here
95         if (line.indexOf("SEQRES") == 0)
96         {
97         }
98
99         if (line.indexOf("MODEL") == 0)
100         {
101           modelFlag = true;
102         }
103
104         if (line.indexOf("TER") == 0)
105         {
106           terFlag = true;
107         }
108
109         if (modelFlag && line.indexOf("ENDMDL") == 0)
110         {
111           break;
112         }
113         if (line.indexOf("ATOM") == 0
114                 || (line.indexOf("HETATM") == 0 && !terFlag))
115         {
116           terFlag = false;
117
118           // Jalview is only interested in CA bonds????
119           atomnam = line.substring(12, 15).trim();
120           if (!atomnam.equals("CA") && !atomnam.equals("P"))
121           {
122             continue;
123           }
124
125           Atom tmpatom = new Atom(line);
126           tmpchain = findChain(tmpatom.chain);
127           if (tmpchain != null)
128           {
129             if (tmpatom.resNumIns.trim().equals(lastID))
130             {
131               // phosphorylated protein - seen both CA and P..
132               continue;
133             }
134             tmpchain.atoms.addElement(tmpatom);
135           }
136           else
137           {
138             tmpchain = new PDBChain(id, tmpatom.chain);
139             chains.addElement(tmpchain);
140             tmpchain.atoms.addElement(tmpatom);
141           }
142           lastID = tmpatom.resNumIns.trim();
143         }
144         index++;
145       }
146
147       makeResidueList();
148       makeCaBondList();
149
150       if (id == null)
151       {
152         id = inFile.getName();
153       }
154       for (int i = 0; i < chains.size(); i++)
155       {
156         SequenceI dataset = ((PDBChain) chains.elementAt(i)).sequence;
157         dataset.setName(id + "|" + dataset.getName());
158         PDBEntry entry = new PDBEntry();
159         entry.setId(id);
160         if (inFile != null)
161         {
162           entry.setFile(inFile.getAbsolutePath());
163         }
164         else
165         {
166           // TODO: decide if we should dump the datasource to disk
167           entry.setFile(getDataName());
168         }
169         dataset.addPDBId(entry);
170         SequenceI chainseq = dataset.deriveSequence(); // PDBChain objects
171         // maintain reference to
172         // dataset
173         seqs.addElement(chainseq);
174         AlignmentAnnotation[] chainannot = chainseq.getAnnotation();
175         if (chainannot != null)
176         {
177           for (int ai = 0; ai < chainannot.length; ai++)
178           {
179             chainannot[ai].visible = VisibleChainAnnotation;
180             annotations.addElement(chainannot[ai]);
181           }
182         }
183       }
184     } catch (OutOfMemoryError er)
185     {
186       System.out.println("OUT OF MEMORY LOADING PDB FILE");
187       throw new IOException("Out of memory loading PDB File");
188     } catch (NumberFormatException ex)
189     {
190       if (line != null)
191       {
192         System.err.println("Couldn't read number from line:");
193         System.err.println(line);
194       }
195     }
196   }
197
198   /**
199    * make a friendly ID string.
200    * 
201    * @param dataName
202    * @return truncated dataName to after last '/'
203    */
204   private String safeName(String dataName)
205   {
206     int p = 0;
207     while ((p = dataName.indexOf("/")) > -1 && p < dataName.length())
208     {
209       dataName = dataName.substring(p + 1);
210     }
211     return dataName;
212   }
213
214   public void makeResidueList()
215   {
216     for (int i = 0; i < chains.size(); i++)
217     {
218       ((PDBChain) chains.elementAt(i)).makeResidueList();
219     }
220   }
221
222   public void makeCaBondList()
223   {
224     for (int i = 0; i < chains.size(); i++)
225     {
226       ((PDBChain) chains.elementAt(i)).makeCaBondList();
227     }
228   }
229
230   public PDBChain findChain(String id)
231   {
232     for (int i = 0; i < chains.size(); i++)
233     {
234       if (((PDBChain) chains.elementAt(i)).id.equals(id))
235       {
236         return (PDBChain) chains.elementAt(i);
237       }
238     }
239
240     return null;
241   }
242
243   public void setChargeColours()
244   {
245     for (int i = 0; i < chains.size(); i++)
246     {
247       ((PDBChain) chains.elementAt(i)).setChargeColours();
248     }
249   }
250
251   public void setColours(jalview.schemes.ColourSchemeI cs)
252   {
253     for (int i = 0; i < chains.size(); i++)
254     {
255       ((PDBChain) chains.elementAt(i)).setChainColours(cs);
256     }
257   }
258
259   public void setChainColours()
260   {
261     for (int i = 0; i < chains.size(); i++)
262     {
263       ((PDBChain) chains.elementAt(i)).setChainColours(Color.getHSBColor(
264               1.0f / (float) i, .4f, 1.0f));
265     }
266   }
267 }