ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/proteinstructure/PdbaseInfo.java
Revision: 202
Committed: Thu Jun 21 17:18:11 2007 UTC (17 years, 4 months ago) by duarte
File size: 10348 byte(s)
Log Message:
MySQLConnection now throwing SQLException on connect
Many files changed following this: all calling classes now re-throwing or catching the SQLException
Line File contents
1 package proteinstructure;
2
3 import tools.MySQLConnection;
4
5 import java.sql.Statement;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.util.ArrayList;
9 import java.util.HashMap;
10 import java.util.Collections;
11
12 public class PdbaseInfo {
13 public final static String MYSQLSERVER="white";
14 public final static String MYSQLUSER=getUserName();
15 public final static String MYSQLPWD="nieve";
16 public final static String mymsdsdDB="my_msdsd_00_07_a";
17 public final static String msdsdDB="msdsd_00_07_a";
18 public final static String pdbaseDB="pdbase";
19
20 MySQLConnection conn;
21 String accode="";
22 String chaincode="";
23 int model=DEFAULT_MODEL;
24 String chain=""; // the internal pdbase chain identifier (asym_id)
25 int entrykey;
26 String asymid;
27 int entitykey;
28 String alt_locs_sql_str;
29
30 static int DEFAULT_MODEL=1;
31
32 PdbaseInfo (String accode, String chaincode, int model_serial, String db) throws PdbaseInconsistencyError, PdbaseAcCodeNotFoundError, SQLException{
33 this.accode=accode;
34 this.chaincode=chaincode;
35 this.model=model_serial;
36 this.conn = new MySQLConnection(MYSQLSERVER,MYSQLUSER,MYSQLPWD,db);
37 this.entrykey=get_entry_key();
38 this.asymid=get_asym_id();
39 this.entitykey=get_entity_key();
40 this.alt_locs_sql_str=get_atom_alt_locs();
41
42 }
43
44 PdbaseInfo (String accode, String chaincode, String db) throws PdbaseInconsistencyError, PdbaseAcCodeNotFoundError, SQLException {
45 this(accode,chaincode,DEFAULT_MODEL,db);
46 }
47 PdbaseInfo (String accode, String chaincode, int model_serial) throws PdbaseInconsistencyError, PdbaseAcCodeNotFoundError, SQLException {
48 this(accode,chaincode,model_serial,pdbaseDB);
49 }
50
51 PdbaseInfo (String accode, String chaincode) throws PdbaseInconsistencyError, PdbaseAcCodeNotFoundError, SQLException {
52 this(accode,chaincode,DEFAULT_MODEL,pdbaseDB);
53 }
54
55 /** get user name from operating system (for use as database username) */
56 private static String getUserName() {
57 String user = null;
58 user = System.getProperty("user.name");
59 if(user == null) {
60 System.err.println("Could not get user name from operating system. Exiting");
61 System.exit(1);
62 }
63 return user;
64 }
65
66 public void close() {
67 conn.close();
68 }
69
70 public int get_entry_key() throws PdbaseAcCodeNotFoundError {
71 String sql="SELECT entry_key FROM struct WHERE entry_id='"+accode.toUpperCase()+"'";
72 try {
73 Statement stmt = conn.createStatement();
74 ResultSet rsst = stmt.executeQuery(sql);
75 if (rsst.next()) {
76 entrykey = rsst.getInt(1);
77 if (! rsst.isLast()) {
78 System.err.println("More than 1 entry_key match for accession_code="+accode+", chain_pdb_code="+chaincode);
79 throw new PdbaseAcCodeNotFoundError();
80 }
81 } else {
82 System.err.println("No entry_key match for accession_code="+accode+", chain_pdb_code="+chaincode);
83 throw new PdbaseAcCodeNotFoundError();
84 }
85 rsst.close();
86 stmt.close();
87 } catch (SQLException e) {
88 e.printStackTrace();
89 }
90 return entrykey;
91 }
92
93 public String get_asym_id() throws PdbaseInconsistencyError {
94 String pdbstrandid=chaincode;
95 if (chaincode.equals("NULL")){
96 pdbstrandid="A";
97 }
98 String sql="SELECT asym_id " +
99 " FROM pdbx_poly_seq_scheme " +
100 " WHERE entry_key=" + entrykey +
101 " AND pdb_strand_id='"+pdbstrandid+"' " +
102 " LIMIT 1";
103 try {
104 Statement stmt = conn.createStatement();
105 ResultSet rsst = stmt.executeQuery(sql);
106 if (rsst.next()) {
107 asymid = rsst.getString(1);
108 } else {
109 System.err.println("No asym_id match for entry_key="+entrykey+", pdb_strand_id="+chaincode);
110 throw new PdbaseInconsistencyError("No asym_id match for entry_key="+entrykey+", pdb_strand_id="+chaincode);
111 }
112 rsst.close();
113 stmt.close();
114 } catch (SQLException e) {
115 e.printStackTrace();
116 }
117 // we set the internal chain identifier self.chain from asymid
118 chain = asymid;
119 return asymid;
120 }
121
122 public int get_entity_key() throws PdbaseInconsistencyError {
123 String sql="SELECT entity_key " +
124 " FROM struct_asym " +
125 " WHERE entry_key="+ entrykey +
126 " AND id='"+asymid+"'";
127 try {
128 Statement stmt = conn.createStatement();
129 ResultSet rsst = stmt.executeQuery(sql);
130 if (rsst.next()) {
131 entitykey = rsst.getInt(1);
132 if (! rsst.isLast()) {
133 System.err.println("More than 1 entity_key match for entry_key="+entrykey+", asym_id="+asymid);
134 throw new PdbaseInconsistencyError("More than 1 entity_key match for entry_key="+entrykey+", asym_id="+asymid);
135 }
136 } else {
137 System.err.println("No entity_key match for entry_key="+entrykey+", asym_id="+asymid);
138 throw new PdbaseInconsistencyError("No entity_key match for entry_key="+entrykey+", asym_id="+asymid);
139 }
140 rsst.close();
141 stmt.close();
142 } catch (SQLException e) {
143 e.printStackTrace();
144 }
145 return entitykey;
146 }
147
148 public String get_atom_alt_locs() throws PdbaseInconsistencyError{
149 ArrayList<String> alt_ids = new ArrayList<String>();
150 HashMap<String,Integer> alt_ids2keys = new HashMap<String,Integer>();
151 String alt_loc_field="label_alt_key";
152 String sql="SELECT id, atom_sites_alt_key FROM atom_sites_alt WHERE entry_key="+entrykey;
153 try {
154 Statement stmt = conn.createStatement();
155 ResultSet rsst = stmt.executeQuery(sql);
156 int count=0;
157 while (rsst.next()) {
158 count++;
159 alt_ids.add(rsst.getString(1));
160 alt_ids2keys.put(rsst.getString(1), rsst.getInt(2));
161 }
162 if (count!=0){
163 if ((! alt_ids.contains(".")) || alt_ids.indexOf(".")!=alt_ids.lastIndexOf(".")){ // second term is a way of finding out if there is more than 1 ocurrence of "." in the ArrayList
164 System.err.println("alt_codes exist for entry_key "+entrykey+" but there is either no default value '.' or more than 1 '.'. Something wrong with this entry_key or with "+pdbaseDB+" db!");
165 throw new PdbaseInconsistencyError("alt_codes exist for entry_key "+entrykey+" but there is either no default value '.' or more than 1 '.'. Something wrong with this entry_key or with "+pdbaseDB+" db!");
166 }
167 alt_ids.remove(".");
168 Collections.sort(alt_ids);
169 String lowest_alt_id = alt_ids.get(0);
170 alt_locs_sql_str = "("+alt_loc_field+"="+alt_ids2keys.get(".")+" OR "+alt_loc_field+"="+alt_ids2keys.get(lowest_alt_id)+")";
171 } else {
172 alt_locs_sql_str=alt_loc_field+" IS NULL";
173 }
174
175 rsst.close();
176 stmt.close();
177 } catch (SQLException e) {
178 e.printStackTrace();
179 }
180
181 return alt_locs_sql_str;
182 }
183
184 public ArrayList<ArrayList> read_atomData() throws PdbaseInconsistencyError{
185 ArrayList<ArrayList> resultset = new ArrayList<ArrayList>();
186 String sql = "SELECT id, label_atom_id, label_comp_id, label_seq_id, Cartn_x, Cartn_y, Cartn_z " +
187 " FROM atom_site " +
188 " WHERE entry_key="+entrykey +
189 " AND label_asym_id='"+asymid+"' " +
190 " AND label_entity_key="+ entitykey +
191 " AND model_num="+ model +
192 " AND "+alt_locs_sql_str;
193 try {
194 Statement stmt = conn.createStatement();
195 ResultSet rsst = stmt.executeQuery(sql);
196 int count=0;
197 while (rsst.next()){
198 count++;
199 ArrayList<Comparable> thisrecord = new ArrayList<Comparable>();
200 thisrecord.add(rsst.getInt(1)); //atomserial
201 thisrecord.add(rsst.getString(2).trim()); // atom
202 thisrecord.add(rsst.getString(3).trim()); // res_type
203 thisrecord.add(rsst.getInt(4)); //res_serial
204 thisrecord.add(rsst.getDouble(5)); // x
205 thisrecord.add(rsst.getDouble(6)); // y
206 thisrecord.add(rsst.getDouble(7)); // z
207
208 resultset.add(thisrecord);
209 }
210 if (count==0){
211 throw new PdbaseInconsistencyError("atom data query returned no data at all for entry_key="+entrykey+", asym_id="+asymid+", entity_key="+entitykey+", model_num="+model+", alt_locs_sql_str='"+alt_locs_sql_str+"'");
212 }
213 rsst.close();
214 stmt.close();
215 } catch (SQLException e) {
216 e.printStackTrace();
217 }
218 return resultset;
219 }
220
221 public String read_seq() throws PdbaseInconsistencyError{
222 String sequence="";
223 String pdbstrandid=chaincode;
224 if (chaincode.equals("NULL")){
225 pdbstrandid="A";
226 }
227 // we use seq_id+0 (implicitly converts to int) in ORDER BY because seq_id is varchar!!
228 String sql="SELECT mon_id" +
229 " FROM pdbx_poly_seq_scheme " +
230 " WHERE entry_key=" + entrykey +
231 " AND asym_id='"+asymid+"' " +
232 " AND pdb_strand_id='"+pdbstrandid+"' " +
233 " ORDER BY seq_id+0";
234 try {
235 Statement stmt = conn.createStatement();
236 ResultSet rsst = stmt.executeQuery(sql);
237 ArrayList<String> aalist=AA.aas();
238 int count=0;
239 while (rsst.next()) {
240 count++;
241 String res_type = rsst.getString(1);
242 if (aalist.contains(res_type)){
243 sequence+=AA.threeletter2oneletter(res_type);
244 } else {
245 sequence+="X";
246 }
247 }
248 if (count==0) {
249 System.err.println("No sequence data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
250 throw new PdbaseInconsistencyError("No sequence data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
251 }
252 rsst.close();
253 stmt.close();
254 } catch (SQLException e) {
255 e.printStackTrace();
256 }
257
258 return sequence;
259 }
260
261 public HashMap<String,Integer> get_ressers_mapping() throws PdbaseInconsistencyError{
262 String pdbstrandid=chaincode;
263 if (chaincode.equals("NULL")){
264 pdbstrandid="A";
265 }
266
267 HashMap<String,Integer> map = new HashMap<String, Integer>();
268 String sql="SELECT seq_id, concat(auth_seq_num,IF(pdb_ins_code='.','',pdb_ins_code))" +
269 " FROM pdbx_poly_seq_scheme " +
270 " WHERE entry_key=" + entrykey +
271 " AND asym_id='"+asymid+"' " +
272 " AND pdb_strand_id='"+pdbstrandid+"' " +
273 " AND auth_seq_num!='?'" +
274 " ORDER BY seq_id+0";
275 try {
276 Statement stmt = conn.createStatement();
277 ResultSet rsst = stmt.executeQuery(sql);
278 int count=0;
279 while (rsst.next()) {
280 count++;
281 int resser = Integer.parseInt(rsst.getString(1));
282 String pdbresser = rsst.getString(2);
283 map.put(pdbresser, resser);
284 }
285 if (count==0) {
286 System.err.println("No residue serials mapping data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
287 throw new PdbaseInconsistencyError("No residue serials mapping data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
288 }
289 rsst.close();
290 stmt.close();
291 } catch (SQLException e) {
292 e.printStackTrace();
293 }
294
295 return map;
296 }
297 }