GPL license added
[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 \r
20 package MCview;\r
21 \r
22 import jalview.datamodel.*;\r
23 import java.io.*;\r
24 import java.net.*;\r
25 import java.util.*;\r
26 \r
27 \r
28 public class PDBfile extends jalview.io.FileParse {\r
29 \r
30   public Vector chains = new Vector();\r
31 \r
32   Vector lineArray = new Vector();\r
33 \r
34   public PDBfile(String [] lines)\r
35   {\r
36     for(int i=0; i<lines.length; i++)\r
37       lineArray.addElement(lines[i]);\r
38 \r
39     noLines = lineArray.size();\r
40     parse();\r
41   }\r
42 \r
43   public PDBfile(String inFile, String inType) throws IOException {\r
44 \r
45     super(inFile,inType);\r
46 \r
47     String line;\r
48     this.lineArray = new Vector();\r
49     BufferedReader dataIn;\r
50 \r
51     if (inType.equals("File"))\r
52       dataIn =  new BufferedReader(new FileReader( inFile ));\r
53 \r
54     else\r
55     {\r
56       URL url = new URL(inFile);\r
57       this.fileSize = 0;\r
58       dataIn = new BufferedReader(new InputStreamReader(url.openStream()));\r
59     }\r
60 \r
61     while ((line = dataIn.readLine()) != null) {\r
62       lineArray.addElement(line);\r
63     }\r
64     noLines = lineArray.size();\r
65 \r
66     parse();\r
67 \r
68 }\r
69 \r
70 \r
71   public void parse() {\r
72 \r
73     for (int i = 0; i < lineArray.size(); i++) {\r
74       StringTokenizer str = new StringTokenizer(lineArray.elementAt(i).toString());\r
75       if (str.hasMoreTokens()) {\r
76         String inStr = str.nextToken();\r
77 \r
78         if (inStr.indexOf("ATOM") != -1) {\r
79           try {\r
80             myAtom tmpatom = new myAtom(str);\r
81             if (findChain(tmpatom.chain) != null) {\r
82            //   System.out.println("Adding to chain " + tmpatom.chain);\r
83               findChain(tmpatom.chain).atoms.addElement(tmpatom);\r
84             } else {\r
85             //  System.out.println("Making chain " + tmpatom.chain);\r
86               PDBChain tmpchain = new PDBChain(tmpatom.chain);\r
87               chains.addElement(tmpchain);\r
88               tmpchain.atoms.addElement(tmpatom);\r
89             }\r
90           } catch(NumberFormatException e) {\r
91             System.err.println("Caught" +  e);\r
92             System.err.println("Record not added to PDB model:"+lineArray.elementAt(i).toString());\r
93           }\r
94         }\r
95       }\r
96     }\r
97     makeResidueList();\r
98     makeCaBondList();\r
99     //    for (int i=0; i < chains.size() ; i++) {\r
100     //  String pog = ((PDBChain)chains.elementAt(i)).print();\r
101     //  System.out.println(pog);\r
102     // }\r
103   }\r
104 \r
105   public void makeResidueList() {\r
106     for (int i=0; i < chains.size() ; i++) {\r
107       ((PDBChain)chains.elementAt(i)).makeResidueList();\r
108     }\r
109   }\r
110   public void makeCaBondList() {\r
111     for (int i=0; i < chains.size() ; i++) {\r
112       ((PDBChain)chains.elementAt(i)).makeCaBondList();\r
113     }\r
114   }\r
115 \r
116   public PDBChain findChain(String id) {\r
117     for (int i=0; i < chains.size(); i++) {\r
118       // System.out.println("ID = " + id + " " +((PDBChain)chains.elementAt(i)).id);\r
119       if (((PDBChain)chains.elementAt(i)).id.equals(id)) {\r
120         return (PDBChain)chains.elementAt(i);\r
121       }\r
122     }\r
123     return null;\r
124   }\r
125 \r
126 \r
127   public void setChargeColours() {\r
128     for (int i=0; i < chains.size(); i++) {\r
129       ((PDBChain)chains.elementAt(i)).setChargeColours();\r
130     }\r
131   }\r
132 \r
133   public void setHydrophobicityColours() {\r
134     for (int i=0; i < chains.size(); i++) {\r
135       ((PDBChain)chains.elementAt(i)).setHydrophobicityColours();\r
136     }\r
137   }\r
138 \r
139   public void colourBySequence(Sequence seq) {\r
140 //SMJS TODO\r
141 //    int max = seq.maxchain;\r
142 //    if (seq.maxchain != -1) {\r
143 //      ((PDBChain)chains.elementAt(max)).colourBySequence(seq);\r
144 //    }\r
145   }\r
146 \r
147   public void setChainColours() {\r
148     for (int i=0; i < chains.size(); i++) {\r
149       ((PDBChain)chains.elementAt(i)).setChainColours();\r
150     }\r
151   }\r
152   public static void main(String[] args) {\r
153     try {\r
154       PDBfile pdb = new PDBfile("enkp1.pdb","File");\r
155     } catch(IOException e) {\r
156       System.out.println(e);\r
157       e.printStackTrace();\r
158       System.exit(0);\r
159     }\r
160   }\r
161 }\r
162 \r
163 \r
164 \r