Memory check on PDB file
[jalview.git] / src / MCview / PDBfile.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2006 AM Waterhouse, J Procter, G Barton, M Clamp, S Searle\r
4 *\r
5 * This program is free software; you can redistribute it and/or\r
6 * modify it under the terms of the GNU General Public License\r
7 * as published by the Free Software Foundation; either version 2\r
8 * of the License, or (at your option) any later version.\r
9 *\r
10 * This program is distributed in the hope that it will be useful,\r
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 * GNU General Public License for more details.\r
14 *\r
15 * You should have received a copy of the GNU General Public License\r
16 * along with this program; if not, write to the Free Software\r
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA\r
18 */\r
19 package MCview;\r
20 \r
21 import jalview.datamodel.*;\r
22 \r
23 import java.io.*;\r
24 \r
25 import java.util.*;\r
26 import java.awt.Color;\r
27 \r
28 \r
29 public class PDBfile extends jalview.io.AlignFile {\r
30     public Vector chains;\r
31     public String id;\r
32 \r
33     public PDBfile(String inFile, String inType) throws IOException\r
34     {\r
35         super(inFile, inType);\r
36     }\r
37 \r
38     public String print()\r
39     {\r
40       return null;\r
41     }\r
42 \r
43     public void parse() throws IOException\r
44     {\r
45       try{\r
46         chains = new Vector();\r
47 \r
48         PDBChain tmpchain;\r
49         String line;\r
50         boolean modelFlag = false;\r
51         boolean terFlag = false;\r
52 \r
53         int index = 0;\r
54         while ( (line = nextLine()) != null)\r
55         {\r
56           if (line.indexOf("HEADER") == 0)\r
57           {\r
58             id = line.substring(62, 67).trim();\r
59             continue;\r
60           }\r
61 \r
62           if (line.indexOf("MODEL") == 0)\r
63             modelFlag = true;\r
64 \r
65           if (line.indexOf("TER") == 0)\r
66             terFlag = true;\r
67 \r
68           if (modelFlag && line.indexOf("ENDMDL") == 0)\r
69             break;\r
70 \r
71           if (line.indexOf("ATOM") == 0\r
72               || (line.indexOf("HETATM") == 0 && !terFlag)\r
73               )\r
74           {\r
75             terFlag = false;\r
76 \r
77             //Jalview is only interested in CA bonds????\r
78             if (!line.substring(12, 15).trim().equals("CA"))\r
79             {\r
80               continue;\r
81             }\r
82 \r
83             Atom tmpatom = new Atom(line);\r
84             tmpchain = findChain(tmpatom.chain);\r
85             if (tmpchain != null)\r
86             {\r
87               tmpchain.atoms.addElement(tmpatom);\r
88             }\r
89             else\r
90             {\r
91               tmpchain = new PDBChain(tmpatom.chain);\r
92               chains.addElement(tmpchain);\r
93               tmpchain.atoms.addElement(tmpatom);\r
94             }\r
95           }\r
96           index++;\r
97         }\r
98 \r
99         makeResidueList();\r
100         makeCaBondList();\r
101 \r
102         if (id == null)\r
103         {\r
104           id = inFile.getName();\r
105         }\r
106         for (int i = 0; i < chains.size(); i++)\r
107         {\r
108           SequenceI seq = ( (PDBChain) chains.elementAt(i)).\r
109               sequence;\r
110           seq.setName(id + "|" + seq.getName());\r
111           Sequence dataset = new Sequence(seq.\r
112                                           getName(),\r
113                                           seq.getSequence().toString(),\r
114                                           seq.getStart(), seq.getEnd());\r
115 \r
116           PDBEntry entry = new PDBEntry();\r
117           entry.setId(id);\r
118           if (inFile != null)\r
119             entry.setFile(inFile.getAbsolutePath());\r
120 \r
121           seq.setDatasetSequence(dataset);\r
122           dataset.addPDBId(entry);\r
123 \r
124           getSeqs().addElement(seq);\r
125         }\r
126       }catch(OutOfMemoryError er)\r
127       {\r
128         System.out.println("OUT OF MEMORY LOADING PDB FILE");\r
129         throw new IOException("Out of memory loading PDB File");\r
130       }\r
131     }\r
132 \r
133     public void makeResidueList() {\r
134         for (int i = 0; i < chains.size(); i++) {\r
135             ((PDBChain) chains.elementAt(i)).makeResidueList();\r
136         }\r
137     }\r
138 \r
139     public void makeCaBondList() {\r
140         for (int i = 0; i < chains.size(); i++) {\r
141             ((PDBChain) chains.elementAt(i)).makeCaBondList();\r
142         }\r
143     }\r
144 \r
145     public PDBChain findChain(String id) {\r
146         for (int i = 0; i < chains.size(); i++) {\r
147             if (((PDBChain) chains.elementAt(i)).id.equals(id)) {\r
148                 return (PDBChain) chains.elementAt(i);\r
149             }\r
150         }\r
151 \r
152         return null;\r
153     }\r
154 \r
155     public void setChargeColours() {\r
156         for (int i = 0; i < chains.size(); i++) {\r
157             ((PDBChain) chains.elementAt(i)).setChargeColours();\r
158         }\r
159     }\r
160 \r
161     public void setColours(jalview.schemes.ColourSchemeI cs) {\r
162         for (int i = 0; i < chains.size(); i++) {\r
163             ((PDBChain) chains.elementAt(i)).setChainColours(cs);\r
164         }\r
165     }\r
166 \r
167     public void setChainColours()\r
168     {\r
169        for (int i = 0; i < chains.size(); i++)\r
170        {\r
171             ((PDBChain) chains.elementAt(i)).setChainColours(\r
172                      Color.getHSBColor(1.0f / (float)i, .4f, 1.0f)\r
173                 );\r
174         }\r
175     }\r
176 }\r