ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/branches/aglappe-jung/proteinstructure/PdbasePdb.java
Revision: 419
Committed: Thu Nov 22 14:09:18 2007 UTC (16 years, 11 months ago) by duarte
File size: 15966 byte(s)
Log Message:
Re-branching for JUNG2 development
Line User Rev File contents
1 duarte 207 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 duarte 219 import java.util.regex.Matcher;
10     import java.util.regex.Pattern;
11 duarte 207
12 duarte 226 import javax.vecmath.Point3d;
13    
14 duarte 207 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 stehr 215 * @throws PdbCodeNotFoundError
47 duarte 207 * @throws SQLException
48 stehr 215 * @throws PdbChainCodeNotFoundError
49 duarte 207 */
50 stehr 215 public PdbasePdb (String pdbCode, String pdbChainCode) throws PdbaseInconsistencyError, PdbCodeNotFoundError, SQLException, PdbChainCodeNotFoundError {
51 duarte 207 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 stehr 215 * @throws PdbCodeNotFoundError
64 duarte 210 * @throws SQLException
65 stehr 215 * @throws PdbChainCodeNotFoundError
66 duarte 207 */
67 stehr 215 public PdbasePdb (String pdbCode, String pdbChainCode, String db, MySQLConnection conn) throws PdbaseInconsistencyError, PdbCodeNotFoundError, SQLException, PdbChainCodeNotFoundError {
68 duarte 207 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 stehr 215 * @throws PdbCodeNotFoundError
80 duarte 207 * @throws SQLException
81 stehr 215 * @throws PdbChainCodeNotFoundError
82 duarte 207 */
83 stehr 215 public PdbasePdb (String pdbCode, String pdbChainCode, int model_serial) throws PdbaseInconsistencyError, PdbCodeNotFoundError, SQLException, PdbChainCodeNotFoundError {
84 duarte 207 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 stehr 215 * @throws PdbCodeNotFoundError
97 duarte 210 * @throws SQLException
98 stehr 215 * @throws PdbChainCodeNotFoundError
99 duarte 207 */
100 stehr 215 public PdbasePdb (String pdbCode, String pdbChainCode, int model_serial, String db, MySQLConnection conn) throws PdbaseInconsistencyError, PdbCodeNotFoundError, SQLException, PdbChainCodeNotFoundError {
101 stehr 217 this.pdbCode=pdbCode.toLowerCase(); // our convention: pdb codes are lower case
102 duarte 401 //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 duarte 207 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 duarte 278 this.fullLength = sequence.length();
115    
116 duarte 207 this.pdbresser2resser = get_ressers_mapping();
117    
118     this.read_atomData(); // populates resser_atom2atomserial, resser2restype, atomser2coord, atomser2resser
119    
120 duarte 278 this.obsLength = resser2restype.size();
121    
122 duarte 207 // 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 duarte 219
128 stehr 274 secondaryStructure = new SecondaryStructure(); // create empty secondary structure first to make sure object is not null
129 duarte 219 readSecStructure();
130 stehr 274 if(!secondaryStructure.isEmpty()) {
131     secondaryStructure.setComment("Pdbase");
132 stehr 259 }
133 duarte 237
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 duarte 207 }
142    
143 stehr 215 private int get_entry_key() throws PdbCodeNotFoundError, SQLException {
144 duarte 207 String sql="SELECT entry_key FROM "+db+".struct WHERE entry_id='"+pdbCode.toUpperCase()+"'";
145 duarte 210 Statement stmt = conn.createStatement();
146     ResultSet rsst = stmt.executeQuery(sql);
147     if (rsst.next()) {
148     entrykey = rsst.getInt(1);
149     if (! rsst.isLast()) {
150 stehr 215 //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 duarte 207 }
153 duarte 210 } else {
154 stehr 215 //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 duarte 207 }
157 duarte 210 rsst.close();
158     stmt.close();
159 duarte 207 return entrykey;
160     }
161    
162 stehr 215 private String get_asym_id() throws PdbChainCodeNotFoundError, SQLException {
163 duarte 207 String pdbstrandid=pdbChainCode;
164 stehr 336 if (pdbChainCode.equals(NULL_CHAIN_CODE)){
165 duarte 207 pdbstrandid="A";
166     }
167 filippis 400 // 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 duarte 207 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 duarte 210
175     Statement stmt = conn.createStatement();
176     ResultSet rsst = stmt.executeQuery(sql);
177     if (rsst.next()) {
178     asymid = rsst.getString(1);
179     } else {
180 stehr 215 //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 duarte 207 }
183 duarte 210 rsst.close();
184     stmt.close();
185 duarte 207 // we set the internal chain identifier chainCode from asymid
186     chainCode = asymid;
187     return asymid;
188     }
189    
190 filippis 400 // NOTE: Entity key not really needed since there can be only one entity_key
191     // per entry_key,asym_id combination
192 duarte 210 private int get_entity_key() throws PdbaseInconsistencyError, SQLException {
193 duarte 207 String sql="SELECT entity_key " +
194     " FROM "+db+".struct_asym " +
195     " WHERE entry_key="+ entrykey +
196     " AND id='"+asymid+"'";
197 duarte 210
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 stehr 215 //System.err.println("More than 1 entity_key match for entry_key="+entrykey+", asym_id="+asymid);
204 duarte 210 throw new PdbaseInconsistencyError("More than 1 entity_key match for entry_key="+entrykey+", asym_id="+asymid);
205 duarte 207 }
206 duarte 210 } else {
207 stehr 215 //System.err.println("No entity_key match for entry_key="+entrykey+", asym_id="+asymid);
208 duarte 210 throw new PdbaseInconsistencyError("No entity_key match for entry_key="+entrykey+", asym_id="+asymid);
209 duarte 207 }
210 duarte 210 rsst.close();
211     stmt.close();
212 duarte 207 return entitykey;
213     }
214    
215 duarte 210 private String get_atom_alt_locs() throws PdbaseInconsistencyError, SQLException{
216 duarte 207 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 duarte 210 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 duarte 207 }
229 duarte 210 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 stehr 215 //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 duarte 210 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 duarte 207 return alt_locs_sql_str;
246     }
247    
248 duarte 210 private void read_atomData() throws PdbaseInconsistencyError, SQLException{
249 duarte 207 resser_atom2atomserial = new HashMap<String,Integer>();
250     resser2restype = new HashMap<Integer,String>();
251 duarte 226 atomser2coord = new HashMap<Integer,Point3d>();
252 duarte 207 atomser2resser = new HashMap<Integer,Integer>();
253    
254 filippis 400 // NOTE: label_entity_key not really needed since there can be only one entity_key
255     // per entry_key,asym_id combination
256 duarte 207 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 duarte 210 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 duarte 226 Point3d coords = new Point3d(x, y, z);
278 duarte 326 if (AAinfo.isValidAA(res_type)) {
279 duarte 210 atomser2coord.put(atomserial, coords);
280     atomser2resser.put(atomserial, res_serial);
281     resser2restype.put(res_serial, res_type);
282 duarte 326 if (AAinfo.isValidAtomWithOXT(res_type,atom)){
283 duarte 210 resser_atom2atomserial.put(res_serial+"_"+atom, atomserial);
284 duarte 207 }
285     }
286 duarte 210
287 duarte 207 }
288 duarte 210 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 duarte 207 }
294    
295 duarte 210 private String read_seq() throws PdbaseInconsistencyError, SQLException{
296 duarte 207 String sequence="";
297 filippis 400
298 duarte 207 // 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 duarte 210 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 duarte 326 if (AAinfo.isValidAA(res_type)){
312     sequence+=AAinfo.threeletter2oneletter(res_type);
313 duarte 210 } else {
314     sequence+=NONSTANDARD_AA_LETTER;
315     }
316     }
317     if (count==0) {
318 stehr 215 //System.err.println("No sequence data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
319 filippis 400 throw new PdbaseInconsistencyError("No sequence data match for entry_key="+entrykey+", asym_id="+asymid);
320 duarte 210 }
321     rsst.close();
322     stmt.close();
323    
324 duarte 207 return sequence;
325     }
326    
327 duarte 210 private HashMap<String,Integer> get_ressers_mapping() throws PdbaseInconsistencyError, SQLException{
328 duarte 207 String pdbstrandid=pdbChainCode;
329 stehr 336 if (pdbChainCode.equals(NULL_CHAIN_CODE)){
330 duarte 207 pdbstrandid="A";
331     }
332    
333     HashMap<String,Integer> map = new HashMap<String, Integer>();
334 filippis 400 String sql="SELECT seq_id, concat(pdb_seq_num,IF(pdb_ins_code='.','',pdb_ins_code))" +
335 duarte 207 " 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 duarte 210
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 stehr 215 //System.err.println("No residue serials mapping data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
352 duarte 210 throw new PdbaseInconsistencyError("No residue serials mapping data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
353 duarte 207 }
354 duarte 210 rsst.close();
355     stmt.close();
356 duarte 207
357     return map;
358     }
359    
360 duarte 219 private void readSecStructure() throws SQLException {
361 stehr 274 this.secondaryStructure = new SecondaryStructure();
362 duarte 219
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 stehr 274 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 duarte 219 }
390 stehr 274 if(ssType != SecStrucElement.OTHER) {
391     SecStrucElement ssElem = new SecStrucElement(ssType, beg, end, ssId);
392     secondaryStructure.add(ssElem);
393     }
394 duarte 219 }
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 stehr 274 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 duarte 219 }
416     rsst.close();
417     stmt.close();
418    
419     }
420 duarte 207 }