1 |
package proteinstructure; |
2 |
|
3 |
import java.sql.ResultSet; |
4 |
import java.sql.SQLException; |
5 |
import java.sql.Statement; |
6 |
import java.util.ArrayList; |
7 |
import java.util.Collections; |
8 |
import java.util.HashMap; |
9 |
import java.util.regex.Matcher; |
10 |
import java.util.regex.Pattern; |
11 |
|
12 |
import javax.vecmath.Point3d; |
13 |
|
14 |
import tools.MySQLConnection; |
15 |
|
16 |
/** |
17 |
* A single chain pdb protein structure loaded from a PDBASE database |
18 |
* See http://openmms.sdsc.edu/OpenMMS-1.5.1_Std/openmms/docs/guides/PDBase.html to know what PDBASE is |
19 |
* |
20 |
* @author Jose Duarte |
21 |
* Class: PdbasePdb |
22 |
* Package: proteinstructure |
23 |
*/ |
24 |
public class PdbasePdb extends Pdb { |
25 |
|
26 |
private final static String MYSQLSERVER="white"; |
27 |
private final static String MYSQLUSER=MySQLConnection.getUserName(); |
28 |
private final static String MYSQLPWD="nieve"; |
29 |
private final static String DEFAULT_PDBASE_DB="pdbase"; |
30 |
|
31 |
private MySQLConnection conn; |
32 |
|
33 |
private int entrykey; |
34 |
private String asymid; |
35 |
private int entitykey; |
36 |
private String alt_locs_sql_str; |
37 |
|
38 |
/** |
39 |
* Constructs Pdb object given pdb code and pdb chain code. |
40 |
* Model will be DEFAULT_MODEL |
41 |
* MySQLConnection is taken from defaults in PdbasePdb class: MYSQLSERVER, MYSQLUSER, MYSQLPWD |
42 |
* Database is taken from default pdbase database in PdbasePdb class: DEFAULT_PDBASE_DB |
43 |
* @param pdbCode |
44 |
* @param pdbChainCode |
45 |
* @throws PdbaseInconsistencyError |
46 |
* @throws PdbCodeNotFoundError |
47 |
* @throws SQLException |
48 |
* @throws PdbChainCodeNotFoundError |
49 |
*/ |
50 |
public PdbasePdb (String pdbCode, String pdbChainCode) throws PdbaseInconsistencyError, PdbCodeNotFoundError, SQLException, PdbChainCodeNotFoundError { |
51 |
this(pdbCode, pdbChainCode, DEFAULT_MODEL, DEFAULT_PDBASE_DB, new MySQLConnection(MYSQLSERVER,MYSQLUSER,MYSQLPWD)); |
52 |
} |
53 |
|
54 |
/** |
55 |
* Constructs Pdb object given pdb code, pdb chain code, source db and a MySQLConnection |
56 |
* Model will be DEFAULT_MODEL |
57 |
* The db must be a pdbase database |
58 |
* @param pdbCode |
59 |
* @param pdbChainCode |
60 |
* @param db |
61 |
* @param conn |
62 |
* @throws PdbaseInconsistencyError |
63 |
* @throws PdbCodeNotFoundError |
64 |
* @throws SQLException |
65 |
* @throws PdbChainCodeNotFoundError |
66 |
*/ |
67 |
public PdbasePdb (String pdbCode, String pdbChainCode, String db, MySQLConnection conn) throws PdbaseInconsistencyError, PdbCodeNotFoundError, SQLException, PdbChainCodeNotFoundError { |
68 |
this(pdbCode,pdbChainCode,DEFAULT_MODEL,db, conn); |
69 |
} |
70 |
|
71 |
/** |
72 |
* Constructs Pdb object given pdb code, pdb chain code and model serial. |
73 |
* MySQLConnection is taken from defaults in PdbasePdb class: MYSQLSERVER, MYSQLUSER, MYSQLPWD |
74 |
* Database is taken from default pdbase database in PdbasePdb class: DEFAULT_PDBASE_DB |
75 |
* @param pdbCode |
76 |
* @param pdbChainCode |
77 |
* @param model_serial |
78 |
* @throws PdbaseInconsistencyError |
79 |
* @throws PdbCodeNotFoundError |
80 |
* @throws SQLException |
81 |
* @throws PdbChainCodeNotFoundError |
82 |
*/ |
83 |
public PdbasePdb (String pdbCode, String pdbChainCode, int model_serial) throws PdbaseInconsistencyError, PdbCodeNotFoundError, SQLException, PdbChainCodeNotFoundError { |
84 |
this(pdbCode, pdbChainCode, model_serial, DEFAULT_PDBASE_DB, new MySQLConnection(MYSQLSERVER,MYSQLUSER,MYSQLPWD)); |
85 |
} |
86 |
|
87 |
/** |
88 |
* Constructs Pdb object given pdb code, pdb chain code, model serial, source db and a MySQLConnection. |
89 |
* The db must be a pdbase database |
90 |
* @param pdbCode |
91 |
* @param pdbChainCode |
92 |
* @param model_serial |
93 |
* @param db |
94 |
* @param conn |
95 |
* @throws PdbaseInconsistencyError |
96 |
* @throws PdbCodeNotFoundError |
97 |
* @throws SQLException |
98 |
* @throws PdbChainCodeNotFoundError |
99 |
*/ |
100 |
public PdbasePdb (String pdbCode, String pdbChainCode, int model_serial, String db, MySQLConnection conn) throws PdbaseInconsistencyError, PdbCodeNotFoundError, SQLException, PdbChainCodeNotFoundError { |
101 |
this.pdbCode=pdbCode.toLowerCase(); // our convention: pdb codes are lower case |
102 |
this.pdbChainCode=pdbChainCode.toUpperCase(); // our convention: chain codes are upper case |
103 |
this.model=model_serial; |
104 |
this.db=db; |
105 |
|
106 |
this.conn = conn; |
107 |
this.entrykey=get_entry_key(); |
108 |
this.asymid=get_asym_id(); // sets asymid and chainCode |
109 |
this.entitykey=get_entity_key(); |
110 |
this.alt_locs_sql_str=get_atom_alt_locs(); |
111 |
|
112 |
this.sequence = read_seq(); |
113 |
this.fullLength = sequence.length(); |
114 |
|
115 |
this.pdbresser2resser = get_ressers_mapping(); |
116 |
|
117 |
this.read_atomData(); // populates resser_atom2atomserial, resser2restype, atomser2coord, atomser2resser |
118 |
|
119 |
this.obsLength = resser2restype.size(); |
120 |
|
121 |
// we initialise resser2pdbresser from the pdbresser2resser HashMap |
122 |
this.resser2pdbresser = new HashMap<Integer, String>(); |
123 |
for (String pdbresser:pdbresser2resser.keySet()){ |
124 |
resser2pdbresser.put(pdbresser2resser.get(pdbresser), pdbresser); |
125 |
} |
126 |
|
127 |
secondaryStructure = new SecondaryStructure(); // create empty secondary structure first to make sure object is not null |
128 |
readSecStructure(); |
129 |
if(!secondaryStructure.isEmpty()) { |
130 |
secondaryStructure.setComment("Pdbase"); |
131 |
} |
132 |
|
133 |
// initialising atomser2atom from resser_atom2atomserial |
134 |
atomser2atom = new HashMap<Integer, String>(); |
135 |
for (String resser_atom:resser_atom2atomserial.keySet()){ |
136 |
int atomserial = resser_atom2atomserial.get(resser_atom); |
137 |
String atom = resser_atom.split("_")[1]; |
138 |
atomser2atom.put(atomserial,atom); |
139 |
} |
140 |
} |
141 |
|
142 |
private int get_entry_key() throws PdbCodeNotFoundError, SQLException { |
143 |
String sql="SELECT entry_key FROM "+db+".struct WHERE entry_id='"+pdbCode.toUpperCase()+"'"; |
144 |
Statement stmt = conn.createStatement(); |
145 |
ResultSet rsst = stmt.executeQuery(sql); |
146 |
if (rsst.next()) { |
147 |
entrykey = rsst.getInt(1); |
148 |
if (! rsst.isLast()) { |
149 |
//System.err.println("More than 1 entry_key match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode); |
150 |
throw new PdbCodeNotFoundError("More than 1 entry_key match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode); |
151 |
} |
152 |
} else { |
153 |
//System.err.println("No entry_key match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode); |
154 |
throw new PdbCodeNotFoundError("No entry_key match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode); |
155 |
} |
156 |
rsst.close(); |
157 |
stmt.close(); |
158 |
return entrykey; |
159 |
} |
160 |
|
161 |
private String get_asym_id() throws PdbChainCodeNotFoundError, SQLException { |
162 |
String pdbstrandid=pdbChainCode; |
163 |
if (pdbChainCode.equals("NULL")){ |
164 |
pdbstrandid="A"; |
165 |
} |
166 |
String sql="SELECT asym_id " + |
167 |
" FROM "+db+".pdbx_poly_seq_scheme " + |
168 |
" WHERE entry_key=" + entrykey + |
169 |
" AND pdb_strand_id='"+pdbstrandid+"' " + |
170 |
" LIMIT 1"; |
171 |
|
172 |
Statement stmt = conn.createStatement(); |
173 |
ResultSet rsst = stmt.executeQuery(sql); |
174 |
if (rsst.next()) { |
175 |
asymid = rsst.getString(1); |
176 |
} else { |
177 |
//System.err.println("No asym_id match for entry_key="+entrykey+", pdb_strand_id="+pdbChainCode); |
178 |
throw new PdbChainCodeNotFoundError("No asym_id match for entry_key="+entrykey+", pdb_strand_id="+pdbChainCode); |
179 |
} |
180 |
rsst.close(); |
181 |
stmt.close(); |
182 |
// we set the internal chain identifier chainCode from asymid |
183 |
chainCode = asymid; |
184 |
return asymid; |
185 |
} |
186 |
|
187 |
private int get_entity_key() throws PdbaseInconsistencyError, SQLException { |
188 |
String sql="SELECT entity_key " + |
189 |
" FROM "+db+".struct_asym " + |
190 |
" WHERE entry_key="+ entrykey + |
191 |
" AND id='"+asymid+"'"; |
192 |
|
193 |
Statement stmt = conn.createStatement(); |
194 |
ResultSet rsst = stmt.executeQuery(sql); |
195 |
if (rsst.next()) { |
196 |
entitykey = rsst.getInt(1); |
197 |
if (! rsst.isLast()) { |
198 |
//System.err.println("More than 1 entity_key match for entry_key="+entrykey+", asym_id="+asymid); |
199 |
throw new PdbaseInconsistencyError("More than 1 entity_key match for entry_key="+entrykey+", asym_id="+asymid); |
200 |
} |
201 |
} else { |
202 |
//System.err.println("No entity_key match for entry_key="+entrykey+", asym_id="+asymid); |
203 |
throw new PdbaseInconsistencyError("No entity_key match for entry_key="+entrykey+", asym_id="+asymid); |
204 |
} |
205 |
rsst.close(); |
206 |
stmt.close(); |
207 |
return entitykey; |
208 |
} |
209 |
|
210 |
private String get_atom_alt_locs() throws PdbaseInconsistencyError, SQLException{ |
211 |
ArrayList<String> alt_ids = new ArrayList<String>(); |
212 |
HashMap<String,Integer> alt_ids2keys = new HashMap<String,Integer>(); |
213 |
String alt_loc_field="label_alt_key"; |
214 |
String sql="SELECT id, atom_sites_alt_key FROM "+db+".atom_sites_alt WHERE entry_key="+entrykey; |
215 |
|
216 |
Statement stmt = conn.createStatement(); |
217 |
ResultSet rsst = stmt.executeQuery(sql); |
218 |
int count=0; |
219 |
while (rsst.next()) { |
220 |
count++; |
221 |
alt_ids.add(rsst.getString(1)); |
222 |
alt_ids2keys.put(rsst.getString(1), rsst.getInt(2)); |
223 |
} |
224 |
if (count!=0){ |
225 |
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 |
226 |
//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 "+DEFAULT_PDBASE_DB+" db!"); |
227 |
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 "+DEFAULT_PDBASE_DB+" db!"); |
228 |
} |
229 |
alt_ids.remove("."); |
230 |
Collections.sort(alt_ids); |
231 |
String lowest_alt_id = alt_ids.get(0); |
232 |
alt_locs_sql_str = "("+alt_loc_field+"="+alt_ids2keys.get(".")+" OR "+alt_loc_field+"="+alt_ids2keys.get(lowest_alt_id)+")"; |
233 |
} else { |
234 |
alt_locs_sql_str=alt_loc_field+" IS NULL"; |
235 |
} |
236 |
|
237 |
rsst.close(); |
238 |
stmt.close(); |
239 |
|
240 |
return alt_locs_sql_str; |
241 |
} |
242 |
|
243 |
private void read_atomData() throws PdbaseInconsistencyError, SQLException{ |
244 |
resser_atom2atomserial = new HashMap<String,Integer>(); |
245 |
resser2restype = new HashMap<Integer,String>(); |
246 |
atomser2coord = new HashMap<Integer,Point3d>(); |
247 |
atomser2resser = new HashMap<Integer,Integer>(); |
248 |
|
249 |
|
250 |
String sql = "SELECT id, label_atom_id, label_comp_id, label_seq_id, Cartn_x, Cartn_y, Cartn_z " + |
251 |
" FROM "+db+".atom_site " + |
252 |
" WHERE entry_key="+entrykey + |
253 |
" AND label_asym_id='"+asymid+"' " + |
254 |
" AND label_entity_key="+ entitykey + |
255 |
" AND model_num="+ model + |
256 |
" AND "+alt_locs_sql_str; |
257 |
|
258 |
Statement stmt = conn.createStatement(); |
259 |
ResultSet rsst = stmt.executeQuery(sql); |
260 |
int count=0; |
261 |
while (rsst.next()){ |
262 |
count++; |
263 |
|
264 |
int atomserial = rsst.getInt(1); // atomserial |
265 |
String atom = rsst.getString(2).trim(); // atom |
266 |
String res_type = rsst.getString(3).trim(); // res_type |
267 |
int res_serial = rsst.getInt(4); // res_serial |
268 |
double x = rsst.getDouble(5); // x |
269 |
double y = rsst.getDouble(6); // y |
270 |
double z = rsst.getDouble(7); // z |
271 |
Point3d coords = new Point3d(x, y, z); |
272 |
if (AAinfo.isValidAA(res_type)) { |
273 |
atomser2coord.put(atomserial, coords); |
274 |
atomser2resser.put(atomserial, res_serial); |
275 |
resser2restype.put(res_serial, res_type); |
276 |
if (AAinfo.isValidAtomWithOXT(res_type,atom)){ |
277 |
resser_atom2atomserial.put(res_serial+"_"+atom, atomserial); |
278 |
} |
279 |
} |
280 |
|
281 |
} |
282 |
if (count==0){ |
283 |
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+"'"); |
284 |
} |
285 |
rsst.close(); |
286 |
stmt.close(); |
287 |
} |
288 |
|
289 |
private String read_seq() throws PdbaseInconsistencyError, SQLException{ |
290 |
String sequence=""; |
291 |
String pdbstrandid=pdbChainCode; |
292 |
if (pdbChainCode.equals("NULL")){ |
293 |
pdbstrandid="A"; |
294 |
} |
295 |
// we use seq_id+0 (implicitly converts to int) in ORDER BY because seq_id is varchar!! |
296 |
String sql="SELECT mon_id" + |
297 |
" FROM "+db+".pdbx_poly_seq_scheme " + |
298 |
" WHERE entry_key=" + entrykey + |
299 |
" AND asym_id='"+asymid+"' " + |
300 |
" AND pdb_strand_id='"+pdbstrandid+"' " + |
301 |
" ORDER BY seq_id+0"; |
302 |
|
303 |
Statement stmt = conn.createStatement(); |
304 |
ResultSet rsst = stmt.executeQuery(sql); |
305 |
int count=0; |
306 |
while (rsst.next()) { |
307 |
count++; |
308 |
String res_type = rsst.getString(1); |
309 |
if (AAinfo.isValidAA(res_type)){ |
310 |
sequence+=AAinfo.threeletter2oneletter(res_type); |
311 |
} else { |
312 |
sequence+=NONSTANDARD_AA_LETTER; |
313 |
} |
314 |
} |
315 |
if (count==0) { |
316 |
//System.err.println("No sequence data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid); |
317 |
throw new PdbaseInconsistencyError("No sequence data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid); |
318 |
} |
319 |
rsst.close(); |
320 |
stmt.close(); |
321 |
|
322 |
return sequence; |
323 |
} |
324 |
|
325 |
private HashMap<String,Integer> get_ressers_mapping() throws PdbaseInconsistencyError, SQLException{ |
326 |
String pdbstrandid=pdbChainCode; |
327 |
if (pdbChainCode.equals("NULL")){ |
328 |
pdbstrandid="A"; |
329 |
} |
330 |
|
331 |
HashMap<String,Integer> map = new HashMap<String, Integer>(); |
332 |
//TODO revise: do we want auth_seq_num or pdb_seq_num here?? |
333 |
String sql="SELECT seq_id, concat(auth_seq_num,IF(pdb_ins_code='.','',pdb_ins_code))" + |
334 |
" FROM "+db+".pdbx_poly_seq_scheme " + |
335 |
" WHERE entry_key=" + entrykey + |
336 |
" AND asym_id='"+asymid+"' " + |
337 |
" AND pdb_strand_id='"+pdbstrandid+"' " + |
338 |
" AND auth_seq_num!='?'" + |
339 |
" ORDER BY seq_id+0"; |
340 |
|
341 |
Statement stmt = conn.createStatement(); |
342 |
ResultSet rsst = stmt.executeQuery(sql); |
343 |
int count=0; |
344 |
while (rsst.next()) { |
345 |
count++; |
346 |
int resser = Integer.parseInt(rsst.getString(1)); |
347 |
String pdbresser = rsst.getString(2); |
348 |
map.put(pdbresser, resser); |
349 |
} |
350 |
if (count==0) { |
351 |
//System.err.println("No residue serials mapping data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid); |
352 |
throw new PdbaseInconsistencyError("No residue serials mapping data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid); |
353 |
} |
354 |
rsst.close(); |
355 |
stmt.close(); |
356 |
|
357 |
return map; |
358 |
} |
359 |
|
360 |
private void readSecStructure() throws SQLException { |
361 |
this.secondaryStructure = new SecondaryStructure(); |
362 |
|
363 |
// HELIX AND TURN -- struct_conf table |
364 |
String sql = "SELECT id,beg_label_seq_id,end_label_seq_id " + |
365 |
" FROM "+db+".struct_conf " + |
366 |
" WHERE entry_key="+entrykey+ |
367 |
" AND beg_label_asym_id='"+asymid+"'"; |
368 |
Statement stmt = conn.createStatement(); |
369 |
ResultSet rsst = stmt.executeQuery(sql); |
370 |
int count=0; |
371 |
while (rsst.next()) { |
372 |
count++; |
373 |
String id = rsst.getString(1).trim(); // id is either HELIX_Pnn or TURN_Pnn |
374 |
Pattern p = Pattern.compile("^(\\w).+_P(\\d)+$"); |
375 |
Matcher m = p.matcher(id); |
376 |
String ssId="Unknown"; |
377 |
if (m.find()){ |
378 |
ssId = m.group(1)+m.group(2); // e.g.: Hnn (helices) or Tnn (turns) |
379 |
} |
380 |
int beg = rsst.getInt(2); |
381 |
int end =rsst.getInt(3); |
382 |
char ssType = SecStrucElement.OTHER; |
383 |
if(id.startsWith("H")) { |
384 |
ssType = SecStrucElement.HELIX; |
385 |
} else if(id.startsWith("T")) { |
386 |
ssType = SecStrucElement.TURN; |
387 |
} else { |
388 |
System.err.println("Unknown secondary structure type " + id + " encountered when reading from Pdbase. Skipping."); |
389 |
} |
390 |
if(ssType != SecStrucElement.OTHER) { |
391 |
SecStrucElement ssElem = new SecStrucElement(ssType, beg, end, ssId); |
392 |
secondaryStructure.add(ssElem); |
393 |
} |
394 |
} |
395 |
rsst.close(); |
396 |
stmt.close(); |
397 |
|
398 |
// SHEET -- struct_sheet_range table |
399 |
sql = "SELECT sheet_id, id, beg_label_seq_id, end_label_seq_id " + |
400 |
" FROM "+db+".struct_sheet_range " + |
401 |
" WHERE entry_key="+entrykey+ |
402 |
" AND beg_label_asym_id='"+asymid+"'"; |
403 |
stmt = conn.createStatement(); |
404 |
rsst = stmt.executeQuery(sql); |
405 |
count=0; |
406 |
while (rsst.next()) { |
407 |
count++; |
408 |
String sheetid = rsst.getString(1).trim(); |
409 |
int id = rsst.getInt(2); |
410 |
int beg = rsst.getInt(3); |
411 |
int end =rsst.getInt(4); |
412 |
String ssId=SecStrucElement.STRAND+sheetid+id; // e.g.: SA1, SA2..., SB1, SB2,... |
413 |
SecStrucElement ssElem = new SecStrucElement(SecStrucElement.STRAND, beg, end, ssId); |
414 |
secondaryStructure.add(ssElem); |
415 |
} |
416 |
rsst.close(); |
417 |
stmt.close(); |
418 |
|
419 |
} |
420 |
} |