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 |
//NOTE:a case sensitive pdb_strand_id can be mapped to more than one asym_ids!!!!!!!!!! |
103 |
this.pdbChainCode=pdbChainCode; // NOTE! pdb chain code are case sensitive! |
104 |
this.model=model_serial; |
105 |
this.db=db; |
106 |
|
107 |
this.conn = conn; |
108 |
this.entrykey=get_entry_key(); |
109 |
this.asymid=get_asym_id(); // sets asymid and chainCode |
110 |
this.entitykey=get_entity_key(); |
111 |
this.alt_locs_sql_str=get_atom_alt_locs(); |
112 |
|
113 |
this.sequence = read_seq(); |
114 |
this.fullLength = sequence.length(); |
115 |
|
116 |
this.pdbresser2resser = get_ressers_mapping(); |
117 |
|
118 |
this.read_atomData(); // populates resser_atom2atomserial, resser2restype, atomser2coord, atomser2resser |
119 |
|
120 |
this.obsLength = resser2restype.size(); |
121 |
|
122 |
// we initialise resser2pdbresser from the pdbresser2resser HashMap |
123 |
this.resser2pdbresser = new HashMap<Integer, String>(); |
124 |
for (String pdbresser:pdbresser2resser.keySet()){ |
125 |
resser2pdbresser.put(pdbresser2resser.get(pdbresser), pdbresser); |
126 |
} |
127 |
|
128 |
secondaryStructure = new SecondaryStructure(); // create empty secondary structure first to make sure object is not null |
129 |
readSecStructure(); |
130 |
if(!secondaryStructure.isEmpty()) { |
131 |
secondaryStructure.setComment("Pdbase"); |
132 |
} |
133 |
|
134 |
// initialising atomser2atom from resser_atom2atomserial |
135 |
atomser2atom = new HashMap<Integer, String>(); |
136 |
for (String resser_atom:resser_atom2atomserial.keySet()){ |
137 |
int atomserial = resser_atom2atomserial.get(resser_atom); |
138 |
String atom = resser_atom.split("_")[1]; |
139 |
atomser2atom.put(atomserial,atom); |
140 |
} |
141 |
} |
142 |
|
143 |
private int get_entry_key() throws PdbCodeNotFoundError, SQLException { |
144 |
String sql="SELECT entry_key FROM "+db+".struct WHERE entry_id='"+pdbCode.toUpperCase()+"'"; |
145 |
Statement stmt = conn.createStatement(); |
146 |
ResultSet rsst = stmt.executeQuery(sql); |
147 |
if (rsst.next()) { |
148 |
entrykey = rsst.getInt(1); |
149 |
if (! rsst.isLast()) { |
150 |
//System.err.println("More than 1 entry_key match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode); |
151 |
throw new PdbCodeNotFoundError("More than 1 entry_key match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode); |
152 |
} |
153 |
} else { |
154 |
//System.err.println("No entry_key match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode); |
155 |
throw new PdbCodeNotFoundError("No entry_key match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode); |
156 |
} |
157 |
rsst.close(); |
158 |
stmt.close(); |
159 |
return entrykey; |
160 |
} |
161 |
|
162 |
private String get_asym_id() throws PdbChainCodeNotFoundError, SQLException { |
163 |
String pdbstrandid=pdbChainCode; |
164 |
if (pdbChainCode.equals(NULL_CHAIN_CODE)){ |
165 |
pdbstrandid="A"; |
166 |
} |
167 |
// NOTE: 'limit 1' not really needed since there can be only one asym_id |
168 |
// per entry_key,pdb_strand_id combination (pdb_strand_id case sensitive!) |
169 |
String sql="SELECT asym_id " + |
170 |
" FROM "+db+".pdbx_poly_seq_scheme " + |
171 |
" WHERE entry_key=" + entrykey + |
172 |
" AND pdb_strand_id='"+pdbstrandid+"' " + |
173 |
" LIMIT 1"; |
174 |
|
175 |
Statement stmt = conn.createStatement(); |
176 |
ResultSet rsst = stmt.executeQuery(sql); |
177 |
if (rsst.next()) { |
178 |
asymid = rsst.getString(1); |
179 |
} else { |
180 |
//System.err.println("No asym_id match for entry_key="+entrykey+", pdb_strand_id="+pdbChainCode); |
181 |
throw new PdbChainCodeNotFoundError("No asym_id match for entry_key="+entrykey+", pdb_strand_id="+pdbChainCode); |
182 |
} |
183 |
rsst.close(); |
184 |
stmt.close(); |
185 |
// we set the internal chain identifier chainCode from asymid |
186 |
chainCode = asymid; |
187 |
return asymid; |
188 |
} |
189 |
|
190 |
// NOTE: Entity key not really needed since there can be only one entity_key |
191 |
// per entry_key,asym_id combination |
192 |
private int get_entity_key() throws PdbaseInconsistencyError, SQLException { |
193 |
String sql="SELECT entity_key " + |
194 |
" FROM "+db+".struct_asym " + |
195 |
" WHERE entry_key="+ entrykey + |
196 |
" AND id='"+asymid+"'"; |
197 |
|
198 |
Statement stmt = conn.createStatement(); |
199 |
ResultSet rsst = stmt.executeQuery(sql); |
200 |
if (rsst.next()) { |
201 |
entitykey = rsst.getInt(1); |
202 |
if (! rsst.isLast()) { |
203 |
//System.err.println("More than 1 entity_key match for entry_key="+entrykey+", asym_id="+asymid); |
204 |
throw new PdbaseInconsistencyError("More than 1 entity_key match for entry_key="+entrykey+", asym_id="+asymid); |
205 |
} |
206 |
} else { |
207 |
//System.err.println("No entity_key match for entry_key="+entrykey+", asym_id="+asymid); |
208 |
throw new PdbaseInconsistencyError("No entity_key match for entry_key="+entrykey+", asym_id="+asymid); |
209 |
} |
210 |
rsst.close(); |
211 |
stmt.close(); |
212 |
return entitykey; |
213 |
} |
214 |
|
215 |
private String get_atom_alt_locs() throws PdbaseInconsistencyError, SQLException{ |
216 |
ArrayList<String> alt_ids = new ArrayList<String>(); |
217 |
HashMap<String,Integer> alt_ids2keys = new HashMap<String,Integer>(); |
218 |
String alt_loc_field="label_alt_key"; |
219 |
String sql="SELECT id, atom_sites_alt_key FROM "+db+".atom_sites_alt WHERE entry_key="+entrykey; |
220 |
|
221 |
Statement stmt = conn.createStatement(); |
222 |
ResultSet rsst = stmt.executeQuery(sql); |
223 |
int count=0; |
224 |
while (rsst.next()) { |
225 |
count++; |
226 |
alt_ids.add(rsst.getString(1)); |
227 |
alt_ids2keys.put(rsst.getString(1), rsst.getInt(2)); |
228 |
} |
229 |
if (count!=0){ |
230 |
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 |
231 |
//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!"); |
232 |
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!"); |
233 |
} |
234 |
alt_ids.remove("."); |
235 |
Collections.sort(alt_ids); |
236 |
String lowest_alt_id = alt_ids.get(0); |
237 |
alt_locs_sql_str = "("+alt_loc_field+"="+alt_ids2keys.get(".")+" OR "+alt_loc_field+"="+alt_ids2keys.get(lowest_alt_id)+")"; |
238 |
} else { |
239 |
alt_locs_sql_str=alt_loc_field+" IS NULL"; |
240 |
} |
241 |
|
242 |
rsst.close(); |
243 |
stmt.close(); |
244 |
|
245 |
return alt_locs_sql_str; |
246 |
} |
247 |
|
248 |
private void read_atomData() throws PdbaseInconsistencyError, SQLException{ |
249 |
resser_atom2atomserial = new HashMap<String,Integer>(); |
250 |
resser2restype = new HashMap<Integer,String>(); |
251 |
atomser2coord = new HashMap<Integer,Point3d>(); |
252 |
atomser2resser = new HashMap<Integer,Integer>(); |
253 |
|
254 |
// NOTE: label_entity_key not really needed since there can be only one entity_key |
255 |
// per entry_key,asym_id combination |
256 |
String sql = "SELECT id, label_atom_id, label_comp_id, label_seq_id, Cartn_x, Cartn_y, Cartn_z " + |
257 |
" FROM "+db+".atom_site " + |
258 |
" WHERE entry_key="+entrykey + |
259 |
" AND label_asym_id='"+asymid+"' " + |
260 |
" AND label_entity_key="+ entitykey + |
261 |
" AND model_num="+ model + |
262 |
" AND "+alt_locs_sql_str; |
263 |
|
264 |
Statement stmt = conn.createStatement(); |
265 |
ResultSet rsst = stmt.executeQuery(sql); |
266 |
int count=0; |
267 |
while (rsst.next()){ |
268 |
count++; |
269 |
|
270 |
int atomserial = rsst.getInt(1); // atomserial |
271 |
String atom = rsst.getString(2).trim(); // atom |
272 |
String res_type = rsst.getString(3).trim(); // res_type |
273 |
int res_serial = rsst.getInt(4); // res_serial |
274 |
double x = rsst.getDouble(5); // x |
275 |
double y = rsst.getDouble(6); // y |
276 |
double z = rsst.getDouble(7); // z |
277 |
Point3d coords = new Point3d(x, y, z); |
278 |
if (AAinfo.isValidAA(res_type)) { |
279 |
atomser2coord.put(atomserial, coords); |
280 |
atomser2resser.put(atomserial, res_serial); |
281 |
resser2restype.put(res_serial, res_type); |
282 |
if (AAinfo.isValidAtomWithOXT(res_type,atom)){ |
283 |
resser_atom2atomserial.put(res_serial+"_"+atom, atomserial); |
284 |
} |
285 |
} |
286 |
|
287 |
} |
288 |
if (count==0){ |
289 |
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+"'"); |
290 |
} |
291 |
rsst.close(); |
292 |
stmt.close(); |
293 |
} |
294 |
|
295 |
private String read_seq() throws PdbaseInconsistencyError, SQLException{ |
296 |
String sequence=""; |
297 |
|
298 |
// we use seq_id+0 (implicitly converts to int) in ORDER BY because seq_id is varchar!! |
299 |
String sql="SELECT mon_id" + |
300 |
" FROM "+db+".pdbx_poly_seq_scheme " + |
301 |
" WHERE entry_key=" + entrykey + |
302 |
" AND asym_id='"+asymid+"' " + |
303 |
" ORDER BY seq_id+0"; |
304 |
|
305 |
Statement stmt = conn.createStatement(); |
306 |
ResultSet rsst = stmt.executeQuery(sql); |
307 |
int count=0; |
308 |
while (rsst.next()) { |
309 |
count++; |
310 |
String res_type = rsst.getString(1); |
311 |
if (AAinfo.isValidAA(res_type)){ |
312 |
sequence+=AAinfo.threeletter2oneletter(res_type); |
313 |
} else { |
314 |
sequence+=NONSTANDARD_AA_LETTER; |
315 |
} |
316 |
} |
317 |
if (count==0) { |
318 |
//System.err.println("No sequence data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid); |
319 |
throw new PdbaseInconsistencyError("No sequence data match for entry_key="+entrykey+", asym_id="+asymid); |
320 |
} |
321 |
rsst.close(); |
322 |
stmt.close(); |
323 |
|
324 |
return sequence; |
325 |
} |
326 |
|
327 |
private HashMap<String,Integer> get_ressers_mapping() throws PdbaseInconsistencyError, SQLException{ |
328 |
String pdbstrandid=pdbChainCode; |
329 |
if (pdbChainCode.equals(NULL_CHAIN_CODE)){ |
330 |
pdbstrandid="A"; |
331 |
} |
332 |
|
333 |
HashMap<String,Integer> map = new HashMap<String, Integer>(); |
334 |
String sql="SELECT seq_id, concat(pdb_seq_num,IF(pdb_ins_code='.','',pdb_ins_code))" + |
335 |
" FROM "+db+".pdbx_poly_seq_scheme " + |
336 |
" WHERE entry_key=" + entrykey + |
337 |
" AND asym_id='"+asymid+"' " + |
338 |
" AND pdb_strand_id='"+pdbstrandid+"' " + |
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 |
} |