Use PDB column format
[jalview.git] / src / MCview / PDBfile.java
1 /*\r
2 * Jalview - A Sequence Alignment Editor and Viewer\r
3 * Copyright (C) 2005 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 java.io.*;\r
22 \r
23 import java.net.*;\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.FileParse {\r
30     public Vector chains = new Vector();\r
31     Vector lineArray = new Vector();\r
32     String id;\r
33 \r
34     public PDBfile(String[] lines) {\r
35         for (int i = 0; i < lines.length; i++)\r
36             lineArray.addElement(lines[i]);\r
37 \r
38         noLines = lineArray.size();\r
39         parse();\r
40     }\r
41 \r
42     public PDBfile(String inFile, String inType) throws IOException {\r
43         super(inFile, inType);\r
44 \r
45         String line;\r
46         this.lineArray = new Vector();\r
47 \r
48         BufferedReader dataIn;\r
49 \r
50         if (inType.equals("File")) {\r
51             dataIn = new BufferedReader(new FileReader(inFile));\r
52         }\r
53         else if(inType.equals("Paste"))\r
54         {\r
55             dataIn = new BufferedReader(new StringReader(inFile));\r
56         }\r
57         else {\r
58             URL url = new URL(inFile);\r
59             this.fileSize = 0;\r
60             dataIn = new BufferedReader(new InputStreamReader(url.openStream()));\r
61         }\r
62 \r
63         while ((line = dataIn.readLine()) != null) {\r
64             lineArray.addElement(line);\r
65         }\r
66 \r
67         noLines = lineArray.size();\r
68 \r
69         parse();\r
70     }\r
71 \r
72     public void parse()\r
73     {\r
74         String line;\r
75         for (int i = 0; i < lineArray.size(); i++)\r
76         {\r
77 \r
78            line = lineArray.elementAt(i).toString();\r
79 \r
80            if (line.indexOf("HEADER") == 0)\r
81            {\r
82              id = line.substring(62, 67).trim();\r
83              continue;\r
84            }\r
85 \r
86                 if (line.indexOf("ATOM") == 0) {\r
87                     try {\r
88                         Atom tmpatom = new Atom(line);\r
89 \r
90                         if (findChain(tmpatom.chain) != null)\r
91                         {\r
92                             findChain(tmpatom.chain).atoms.addElement(tmpatom);\r
93                         } else\r
94                         {\r
95                             PDBChain tmpchain = new PDBChain(tmpatom.chain);\r
96                             chains.addElement(tmpchain);\r
97                             tmpchain.atoms.addElement(tmpatom);\r
98                         }\r
99                     } catch (NumberFormatException e) {\r
100                         System.err.println("Caught" + e);\r
101                         System.err.println("Record not added to PDB model:" +\r
102                             lineArray.elementAt(i).toString());\r
103                     }\r
104                 }\r
105         }\r
106 \r
107         makeResidueList();\r
108         makeCaBondList();\r
109     }\r
110 \r
111     public void makeResidueList() {\r
112         for (int i = 0; i < chains.size(); i++) {\r
113             ((PDBChain) chains.elementAt(i)).makeResidueList();\r
114         }\r
115     }\r
116 \r
117     public void makeCaBondList() {\r
118         for (int i = 0; i < chains.size(); i++) {\r
119             ((PDBChain) chains.elementAt(i)).makeCaBondList();\r
120         }\r
121     }\r
122 \r
123     public PDBChain findChain(String id) {\r
124         for (int i = 0; i < chains.size(); i++) {\r
125             if (((PDBChain) chains.elementAt(i)).id.equals(id)) {\r
126                 return (PDBChain) chains.elementAt(i);\r
127             }\r
128         }\r
129 \r
130         return null;\r
131     }\r
132 \r
133     public void setChargeColours() {\r
134         for (int i = 0; i < chains.size(); i++) {\r
135             ((PDBChain) chains.elementAt(i)).setChargeColours();\r
136         }\r
137     }\r
138 \r
139     public void setHydrophobicityColours() {\r
140         for (int i = 0; i < chains.size(); i++) {\r
141             ((PDBChain) chains.elementAt(i)).setHydrophobicityColours();\r
142         }\r
143     }\r
144 \r
145     public void setChainColours()\r
146     {\r
147        for (int i = 0; i < chains.size(); i++)\r
148        {\r
149             ((PDBChain) chains.elementAt(i)).setChainColours(\r
150                      Color.getHSBColor(1.0f / (float)i, .4f, 1.0f)\r
151                 );\r
152         }\r
153     }\r
154 }\r