63728ae816fe12b96a448fde9e38f7febb4aa25a
[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 import jalview.io.AppletFormatAdapter;\r
28 \r
29 \r
30 public class PDBfile extends jalview.io.FileParse {\r
31     public Vector chains = new Vector();\r
32     Vector lineArray = new Vector();\r
33     public String id;\r
34 \r
35     public PDBfile(String[] lines) {\r
36         for (int i = 0; i < lines.length; i++)\r
37             lineArray.addElement(lines[i]);\r
38 \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 \r
51         if (inType.equals(AppletFormatAdapter.FILE)) {\r
52             dataIn = new BufferedReader(new FileReader(inFile));\r
53         }\r
54         else if(inType.equals(AppletFormatAdapter.PASTE))\r
55         {\r
56             dataIn = new BufferedReader(new StringReader(inFile));\r
57         }\r
58         else if (inType.equalsIgnoreCase(AppletFormatAdapter.CLASSLOADER))\r
59         {\r
60           java.io.InputStream is = getClass().getResourceAsStream("/" +\r
61               inFile);\r
62 \r
63           dataIn = new BufferedReader(new java.io.InputStreamReader(is));\r
64         }\r
65         else\r
66         {\r
67             URL url = new URL(inFile);\r
68             dataIn = new BufferedReader(new InputStreamReader(url.openStream()));\r
69         }\r
70 \r
71         while ((line = dataIn.readLine()) != null) {\r
72             lineArray.addElement(line);\r
73         }\r
74 \r
75 \r
76         parse();\r
77         lineArray = null;\r
78     }\r
79 \r
80     public void parse()\r
81     {\r
82         PDBChain tmpchain;\r
83         String line;\r
84         boolean modelFlag = false;\r
85         boolean terFlag = false;\r
86 \r
87 \r
88         for (int i = 0; i < lineArray.size(); i++)\r
89         {\r
90 \r
91            line = lineArray.elementAt(i).toString();\r
92 \r
93 \r
94            if (line.indexOf("HEADER") == 0)\r
95            {\r
96              id = line.substring(62, 67).trim();\r
97              continue;\r
98            }\r
99 \r
100            if(line.indexOf("MODEL")==0)\r
101              modelFlag = true;\r
102 \r
103            if(line.indexOf("TER")==0)\r
104              terFlag = true;\r
105 \r
106            if(modelFlag && line.indexOf("ENDMDL")==0)\r
107              break;\r
108 \r
109            if (    line.indexOf("ATOM")==0\r
110                || (line.indexOf("HETATM")==0 && !terFlag)\r
111              )\r
112             {\r
113               terFlag = false;\r
114 \r
115 \r
116               //Jalview is only interested in CA bonds????\r
117               if (!line.substring(12, 15).trim().equals("CA"))\r
118               {\r
119                 continue;\r
120               }\r
121 \r
122               Atom tmpatom = new Atom(line);\r
123 \r
124               tmpchain = findChain(tmpatom.chain);\r
125               if (tmpchain != null)\r
126               {\r
127                 tmpchain.atoms.addElement(tmpatom);\r
128               }\r
129               else\r
130               {\r
131                 tmpchain = new PDBChain(tmpatom.chain);\r
132                 chains.addElement(tmpchain);\r
133                 tmpchain.atoms.addElement(tmpatom);\r
134               }\r
135 \r
136           }\r
137         }\r
138 \r
139        makeResidueList();\r
140        makeCaBondList();\r
141     }\r
142 \r
143     public void makeResidueList() {\r
144         for (int i = 0; i < chains.size(); i++) {\r
145             ((PDBChain) chains.elementAt(i)).makeResidueList();\r
146         }\r
147     }\r
148 \r
149     public void makeCaBondList() {\r
150         for (int i = 0; i < chains.size(); i++) {\r
151             ((PDBChain) chains.elementAt(i)).makeCaBondList();\r
152         }\r
153     }\r
154 \r
155     public PDBChain findChain(String id) {\r
156         for (int i = 0; i < chains.size(); i++) {\r
157             if (((PDBChain) chains.elementAt(i)).id.equals(id)) {\r
158                 return (PDBChain) chains.elementAt(i);\r
159             }\r
160         }\r
161 \r
162         return null;\r
163     }\r
164 \r
165     public void setChargeColours() {\r
166         for (int i = 0; i < chains.size(); i++) {\r
167             ((PDBChain) chains.elementAt(i)).setChargeColours();\r
168         }\r
169     }\r
170 \r
171     public void setColours(jalview.schemes.ColourSchemeI cs) {\r
172         for (int i = 0; i < chains.size(); i++) {\r
173             ((PDBChain) chains.elementAt(i)).setChainColours(cs);\r
174         }\r
175     }\r
176 \r
177     public void setChainColours()\r
178     {\r
179        for (int i = 0; i < chains.size(); i++)\r
180        {\r
181             ((PDBChain) chains.elementAt(i)).setChainColours(\r
182                      Color.getHSBColor(1.0f / (float)i, .4f, 1.0f)\r
183                 );\r
184         }\r
185     }\r
186 }\r