ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/proteinstructure/PdbasePdb.java
Revision: 210
Committed: Wed Jun 27 15:57:39 2007 UTC (17 years, 3 months ago) by duarte
File size: 12543 byte(s)
Log Message:
Now throwing also SQLExceptions for individual queries, not just for connections
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    
10     import tools.MySQLConnection;
11    
12     /**
13     * A single chain pdb protein structure loaded from a PDBASE database
14     * See http://openmms.sdsc.edu/OpenMMS-1.5.1_Std/openmms/docs/guides/PDBase.html to know what PDBASE is
15     *
16     * @author Jose Duarte
17     * Class: PdbasePdb
18     * Package: proteinstructure
19     */
20     public class PdbasePdb extends Pdb {
21    
22     private final static String MYSQLSERVER="white";
23     private final static String MYSQLUSER=MySQLConnection.getUserName();
24     private final static String MYSQLPWD="nieve";
25     private final static String DEFAULT_PDBASE_DB="pdbase";
26    
27     private MySQLConnection conn;
28    
29     private int entrykey;
30     private String asymid;
31     private int entitykey;
32     private String alt_locs_sql_str;
33    
34     /**
35     * Constructs Pdb object given pdb code and pdb chain code.
36     * Model will be DEFAULT_MODEL
37     * MySQLConnection is taken from defaults in PdbasePdb class: MYSQLSERVER, MYSQLUSER, MYSQLPWD
38     * Database is taken from default pdbase database in PdbasePdb class: DEFAULT_PDBASE_DB
39     * @param pdbCode
40     * @param pdbChainCode
41     * @throws PdbaseInconsistencyError
42     * @throws PdbaseAcCodeNotFoundError
43     * @throws SQLException
44     */
45     public PdbasePdb (String pdbCode, String pdbChainCode) throws PdbaseInconsistencyError, PdbaseAcCodeNotFoundError, SQLException {
46     this(pdbCode, pdbChainCode, DEFAULT_MODEL, DEFAULT_PDBASE_DB, new MySQLConnection(MYSQLSERVER,MYSQLUSER,MYSQLPWD));
47     }
48    
49     /**
50     * Constructs Pdb object given pdb code, pdb chain code, source db and a MySQLConnection
51     * Model will be DEFAULT_MODEL
52     * The db must be a pdbase database
53     * @param pdbCode
54     * @param pdbChainCode
55     * @param db
56     * @param conn
57     * @throws PdbaseInconsistencyError
58     * @throws PdbaseAcCodeNotFoundError
59 duarte 210 * @throws SQLException
60 duarte 207 */
61 duarte 210 public PdbasePdb (String pdbCode, String pdbChainCode, String db, MySQLConnection conn) throws PdbaseInconsistencyError, PdbaseAcCodeNotFoundError, SQLException {
62 duarte 207 this(pdbCode,pdbChainCode,DEFAULT_MODEL,db, conn);
63     }
64    
65     /**
66     * Constructs Pdb object given pdb code, pdb chain code and model serial.
67     * MySQLConnection is taken from defaults in PdbasePdb class: MYSQLSERVER, MYSQLUSER, MYSQLPWD
68     * Database is taken from default pdbase database in PdbasePdb class: DEFAULT_PDBASE_DB
69     * @param pdbCode
70     * @param pdbChainCode
71     * @param model_serial
72     * @throws PdbaseInconsistencyError
73     * @throws PdbaseAcCodeNotFoundError
74     * @throws SQLException
75     */
76     public PdbasePdb (String pdbCode, String pdbChainCode, int model_serial) throws PdbaseInconsistencyError, PdbaseAcCodeNotFoundError, SQLException {
77     this(pdbCode, pdbChainCode, model_serial, DEFAULT_PDBASE_DB, new MySQLConnection(MYSQLSERVER,MYSQLUSER,MYSQLPWD));
78     }
79    
80     /**
81     * Constructs Pdb object given pdb code, pdb chain code, model serial, source db and a MySQLConnection.
82     * The db must be a pdbase database
83     * @param pdbCode
84     * @param pdbChainCode
85     * @param model_serial
86     * @param db
87     * @param conn
88     * @throws PdbaseInconsistencyError
89     * @throws PdbaseAcCodeNotFoundError
90 duarte 210 * @throws SQLException
91 duarte 207 */
92 duarte 210 public PdbasePdb (String pdbCode, String pdbChainCode, int model_serial, String db, MySQLConnection conn) throws PdbaseInconsistencyError, PdbaseAcCodeNotFoundError, SQLException {
93 duarte 207 this.pdbCode=pdbCode;
94     this.pdbChainCode=pdbChainCode;
95     this.model=model_serial;
96     this.db=db;
97    
98     this.conn = conn;
99     this.entrykey=get_entry_key();
100     this.asymid=get_asym_id(); // sets asymid and chainCode
101     this.entitykey=get_entity_key();
102     this.alt_locs_sql_str=get_atom_alt_locs();
103    
104     this.chainCode = getChainCode();
105     this.sequence = read_seq();
106     this.pdbresser2resser = get_ressers_mapping();
107    
108     this.read_atomData(); // populates resser_atom2atomserial, resser2restype, atomser2coord, atomser2resser
109    
110     // we initialise resser2pdbresser from the pdbresser2resser HashMap
111     this.resser2pdbresser = new HashMap<Integer, String>();
112     for (String pdbresser:pdbresser2resser.keySet()){
113     resser2pdbresser.put(pdbresser2resser.get(pdbresser), pdbresser);
114     }
115     }
116    
117 duarte 210 private int get_entry_key() throws PdbaseAcCodeNotFoundError, SQLException {
118 duarte 207 String sql="SELECT entry_key FROM "+db+".struct WHERE entry_id='"+pdbCode.toUpperCase()+"'";
119 duarte 210 Statement stmt = conn.createStatement();
120     ResultSet rsst = stmt.executeQuery(sql);
121     if (rsst.next()) {
122     entrykey = rsst.getInt(1);
123     if (! rsst.isLast()) {
124     System.err.println("More than 1 entry_key match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode);
125     throw new PdbaseAcCodeNotFoundError();
126 duarte 207 }
127 duarte 210 } else {
128     System.err.println("No entry_key match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode);
129     throw new PdbaseAcCodeNotFoundError();
130 duarte 207 }
131 duarte 210 rsst.close();
132     stmt.close();
133 duarte 207 return entrykey;
134     }
135    
136 duarte 210 private String get_asym_id() throws PdbaseInconsistencyError, SQLException {
137 duarte 207 String pdbstrandid=pdbChainCode;
138     if (pdbChainCode.equals("NULL")){
139     pdbstrandid="A";
140     }
141     String sql="SELECT asym_id " +
142     " FROM "+db+".pdbx_poly_seq_scheme " +
143     " WHERE entry_key=" + entrykey +
144     " AND pdb_strand_id='"+pdbstrandid+"' " +
145     " LIMIT 1";
146 duarte 210
147     Statement stmt = conn.createStatement();
148     ResultSet rsst = stmt.executeQuery(sql);
149     if (rsst.next()) {
150     asymid = rsst.getString(1);
151     } else {
152     System.err.println("No asym_id match for entry_key="+entrykey+", pdb_strand_id="+pdbChainCode);
153     throw new PdbaseInconsistencyError("No asym_id match for entry_key="+entrykey+", pdb_strand_id="+pdbChainCode);
154 duarte 207 }
155 duarte 210 rsst.close();
156     stmt.close();
157 duarte 207 // we set the internal chain identifier chainCode from asymid
158     chainCode = asymid;
159     return asymid;
160     }
161    
162 duarte 210 private int get_entity_key() throws PdbaseInconsistencyError, SQLException {
163 duarte 207 String sql="SELECT entity_key " +
164     " FROM "+db+".struct_asym " +
165     " WHERE entry_key="+ entrykey +
166     " AND id='"+asymid+"'";
167 duarte 210
168     Statement stmt = conn.createStatement();
169     ResultSet rsst = stmt.executeQuery(sql);
170     if (rsst.next()) {
171     entitykey = rsst.getInt(1);
172     if (! rsst.isLast()) {
173     System.err.println("More than 1 entity_key match for entry_key="+entrykey+", asym_id="+asymid);
174     throw new PdbaseInconsistencyError("More than 1 entity_key match for entry_key="+entrykey+", asym_id="+asymid);
175 duarte 207 }
176 duarte 210 } else {
177     System.err.println("No entity_key match for entry_key="+entrykey+", asym_id="+asymid);
178     throw new PdbaseInconsistencyError("No entity_key match for entry_key="+entrykey+", asym_id="+asymid);
179 duarte 207 }
180 duarte 210 rsst.close();
181     stmt.close();
182 duarte 207 return entitykey;
183     }
184    
185 duarte 210 private String get_atom_alt_locs() throws PdbaseInconsistencyError, SQLException{
186 duarte 207 ArrayList<String> alt_ids = new ArrayList<String>();
187     HashMap<String,Integer> alt_ids2keys = new HashMap<String,Integer>();
188     String alt_loc_field="label_alt_key";
189     String sql="SELECT id, atom_sites_alt_key FROM "+db+".atom_sites_alt WHERE entry_key="+entrykey;
190    
191 duarte 210 Statement stmt = conn.createStatement();
192     ResultSet rsst = stmt.executeQuery(sql);
193     int count=0;
194     while (rsst.next()) {
195     count++;
196     alt_ids.add(rsst.getString(1));
197     alt_ids2keys.put(rsst.getString(1), rsst.getInt(2));
198 duarte 207 }
199 duarte 210 if (count!=0){
200     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
201     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!");
202     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!");
203     }
204     alt_ids.remove(".");
205     Collections.sort(alt_ids);
206     String lowest_alt_id = alt_ids.get(0);
207     alt_locs_sql_str = "("+alt_loc_field+"="+alt_ids2keys.get(".")+" OR "+alt_loc_field+"="+alt_ids2keys.get(lowest_alt_id)+")";
208     } else {
209     alt_locs_sql_str=alt_loc_field+" IS NULL";
210     }
211    
212     rsst.close();
213     stmt.close();
214    
215 duarte 207 return alt_locs_sql_str;
216     }
217    
218 duarte 210 private void read_atomData() throws PdbaseInconsistencyError, SQLException{
219 duarte 207 resser_atom2atomserial = new HashMap<String,Integer>();
220     resser2restype = new HashMap<Integer,String>();
221     atomser2coord = new HashMap<Integer,Double[]>();
222     atomser2resser = new HashMap<Integer,Integer>();
223    
224    
225     String sql = "SELECT id, label_atom_id, label_comp_id, label_seq_id, Cartn_x, Cartn_y, Cartn_z " +
226     " FROM "+db+".atom_site " +
227     " WHERE entry_key="+entrykey +
228     " AND label_asym_id='"+asymid+"' " +
229     " AND label_entity_key="+ entitykey +
230     " AND model_num="+ model +
231     " AND "+alt_locs_sql_str;
232    
233 duarte 210 Statement stmt = conn.createStatement();
234     ResultSet rsst = stmt.executeQuery(sql);
235     int count=0;
236     while (rsst.next()){
237     count++;
238    
239     int atomserial = rsst.getInt(1); // atomserial
240     String atom = rsst.getString(2).trim(); // atom
241     String res_type = rsst.getString(3).trim(); // res_type
242     int res_serial = rsst.getInt(4); // res_serial
243     double x = rsst.getDouble(5); // x
244     double y = rsst.getDouble(6); // y
245     double z = rsst.getDouble(7); // z
246     Double[] coords = {x, y, z};
247     ArrayList<String> aalist=AA.aas();
248     if (aalist.contains(res_type)) {
249     atomser2coord.put(atomserial, coords);
250     atomser2resser.put(atomserial, res_serial);
251     resser2restype.put(res_serial, res_type);
252     ArrayList<String> atomlist = aas2atoms.get(res_type);
253     if (atomlist.contains(atom)){
254     resser_atom2atomserial.put(res_serial+"_"+atom, atomserial);
255 duarte 207 }
256     }
257 duarte 210
258 duarte 207 }
259 duarte 210 if (count==0){
260     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+"'");
261     }
262     rsst.close();
263     stmt.close();
264 duarte 207 }
265    
266 duarte 210 private String read_seq() throws PdbaseInconsistencyError, SQLException{
267 duarte 207 String sequence="";
268     String pdbstrandid=pdbChainCode;
269     if (pdbChainCode.equals("NULL")){
270     pdbstrandid="A";
271     }
272     // we use seq_id+0 (implicitly converts to int) in ORDER BY because seq_id is varchar!!
273     String sql="SELECT mon_id" +
274     " FROM "+db+".pdbx_poly_seq_scheme " +
275     " WHERE entry_key=" + entrykey +
276     " AND asym_id='"+asymid+"' " +
277     " AND pdb_strand_id='"+pdbstrandid+"' " +
278     " ORDER BY seq_id+0";
279    
280 duarte 210 Statement stmt = conn.createStatement();
281     ResultSet rsst = stmt.executeQuery(sql);
282     ArrayList<String> aalist=AA.aas();
283     int count=0;
284     while (rsst.next()) {
285     count++;
286     String res_type = rsst.getString(1);
287     if (aalist.contains(res_type)){
288     sequence+=AA.threeletter2oneletter(res_type);
289     } else {
290     sequence+=NONSTANDARD_AA_LETTER;
291     }
292     }
293     if (count==0) {
294     System.err.println("No sequence data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
295     throw new PdbaseInconsistencyError("No sequence data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
296     }
297     rsst.close();
298     stmt.close();
299    
300 duarte 207 return sequence;
301     }
302    
303 duarte 210 private HashMap<String,Integer> get_ressers_mapping() throws PdbaseInconsistencyError, SQLException{
304 duarte 207 String pdbstrandid=pdbChainCode;
305     if (pdbChainCode.equals("NULL")){
306     pdbstrandid="A";
307     }
308    
309     HashMap<String,Integer> map = new HashMap<String, Integer>();
310     String sql="SELECT seq_id, concat(auth_seq_num,IF(pdb_ins_code='.','',pdb_ins_code))" +
311     " FROM "+db+".pdbx_poly_seq_scheme " +
312     " WHERE entry_key=" + entrykey +
313     " AND asym_id='"+asymid+"' " +
314     " AND pdb_strand_id='"+pdbstrandid+"' " +
315     " AND auth_seq_num!='?'" +
316     " ORDER BY seq_id+0";
317 duarte 210
318     Statement stmt = conn.createStatement();
319     ResultSet rsst = stmt.executeQuery(sql);
320     int count=0;
321     while (rsst.next()) {
322     count++;
323     int resser = Integer.parseInt(rsst.getString(1));
324     String pdbresser = rsst.getString(2);
325     map.put(pdbresser, resser);
326     }
327     if (count==0) {
328     System.err.println("No residue serials mapping data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
329     throw new PdbaseInconsistencyError("No residue serials mapping data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
330 duarte 207 }
331 duarte 210 rsst.close();
332     stmt.close();
333 duarte 207
334     return map;
335     }
336    
337     }