ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/proteinstructure/MsdsdPdb.java
Revision: 210
Committed: Wed Jun 27 15:57:39 2007 UTC (17 years, 3 months ago) by duarte
File size: 9865 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.HashMap;
8    
9     import tools.MySQLConnection;
10    
11     /**
12     * A single chain pdb protein structure loaded from a MSDSD database
13     * See http://www.ebi.ac.uk/msd-srv/docs/dbdoc/refaindex.html to know what MSDSD is
14     *
15     * @author Jose Duarte
16     * Class: MsdsdPdb
17     * Package: proteinstructure
18     */
19     public class MsdsdPdb extends Pdb {
20    
21     private final static String MYSQLSERVER="white";
22     private final static String MYSQLUSER=MySQLConnection.getUserName();
23     private final static String MYSQLPWD="nieve";
24     //private final static String DEFAULT_MYMSDSD_DB="my_msdsd_00_07_a";
25     private final static String DEFAULT_MSDSD_DB="msdsd_00_07_a";
26    
27     private MySQLConnection conn;
28    
29     private int chainid;
30     private int modelid;
31    
32     // TODO for this to be able to be used by other people we need to do things without a myMsdsdDb (or also distribute our fixes database)
33     private String myMsdsdDb; // our database with add-ons and fixes to msdsd
34    
35     /**
36     * Constructs Pdb object given pdb code and pdb chain code.
37     * Model will be DEFAULT_MODEL
38     * MySQLConnection is taken from defaults in MsdsdPdb class: MYSQLSERVER, MYSQLUSER, MYSQLPWD
39     * Database is taken from default msdsd database in MsdsdPdb class: DEFAULT_MSDSD_DB
40     * @param pdbCode
41     * @param pdbChainCode
42     * @throws MsdsdAcCodeNotFoundError
43     * @throws MsdsdInconsistentResidueNumbersError
44     * @throws SQLException
45     */
46     public MsdsdPdb (String pdbCode, String pdbChainCode) throws MsdsdAcCodeNotFoundError, MsdsdInconsistentResidueNumbersError, SQLException {
47     this(pdbCode,pdbChainCode,DEFAULT_MODEL,DEFAULT_MSDSD_DB,new MySQLConnection(MYSQLSERVER,MYSQLUSER,MYSQLPWD));
48     }
49    
50     /**
51     * Constructs Pdb object given pdb code, pdb chain code, db and MySQLConnection
52     * Model will be DEFAULT_MODEL
53     * db must be a msdsd database
54     * @param pdbCode
55     * @param pdbChainCode
56     * @param db
57     * @param conn
58     * @throws MsdsdAcCodeNotFoundError
59     * @throws MsdsdInconsistentResidueNumbersError
60 duarte 210 * @throws SQLException
61 duarte 207 */
62 duarte 210 public MsdsdPdb (String pdbCode, String pdbChainCode, String db, MySQLConnection conn) throws MsdsdAcCodeNotFoundError, MsdsdInconsistentResidueNumbersError, SQLException {
63 duarte 207 this(pdbCode,pdbChainCode,DEFAULT_MODEL,db,conn);
64     }
65    
66     /**
67     * Constructs Pdb object given pdb code, pdb chain code and a model serial
68     * MySQLConnection is taken from defaults in MsdsdPdb class: MYSQLSERVER, MYSQLUSER, MYSQLPWD
69     * Database is taken from default msdsd database in MsdsdPdb class: DEFAULT_MSDSD_DB
70     * @param pdbCode
71     * @param pdbChainCode
72     * @param model_serial
73     * @throws MsdsdAcCodeNotFoundError
74     * @throws MsdsdInconsistentResidueNumbersError
75     * @throws SQLException
76     */
77     public MsdsdPdb (String pdbCode, String pdbChainCode, int model_serial) throws MsdsdAcCodeNotFoundError, MsdsdInconsistentResidueNumbersError, SQLException {
78     this(pdbCode,pdbChainCode,model_serial,DEFAULT_MSDSD_DB,new MySQLConnection(MYSQLSERVER,MYSQLUSER,MYSQLPWD));
79     }
80    
81     /**
82     * Constructs Pdb object given pdb code, pdb chain code, model serial, a source db and a MySQLConnection.
83     * db must be a msdsd database
84     * @param pdbCode
85     * @param pdbChainCode
86     * @param model_serial
87     * @param db
88     * @param conn
89     * @throws MsdsdAcCodeNotFoundError
90     * @throws MsdsdInconsistentResidueNumbersError
91 duarte 210 * @throws SQLException
92 duarte 207 */
93 duarte 210 public MsdsdPdb (String pdbCode, String pdbChainCode, int model_serial, String db, MySQLConnection conn) throws MsdsdAcCodeNotFoundError, MsdsdInconsistentResidueNumbersError, SQLException {
94 duarte 207 this.pdbCode=pdbCode;
95     this.pdbChainCode=pdbChainCode;
96     this.model=model_serial;
97     this.db=db;
98     this.myMsdsdDb="my_"+db; // i.e. for db=msdsd_00_07_a then myMsdsdDb=my_msdsd_00_07_a
99    
100     this.conn = conn;
101    
102     this.getchainid();// initialises chainid, modelid and chainCode
103    
104     if (check_inconsistent_res_numbering()){
105     throw new MsdsdInconsistentResidueNumbersError("Inconsistent residue numbering in msdsd for accession_code "+this.pdbCode+", chain_pdb_code "+this.pdbChainCode);
106     }
107    
108     this.sequence = read_seq();
109     this.pdbresser2resser = get_ressers_mapping();
110    
111     this.read_atomData();
112    
113     // we initialise resser2pdbresser from the pdbresser2resser HashMap
114     this.resser2pdbresser = new HashMap<Integer, String>();
115     for (String pdbresser:pdbresser2resser.keySet()){
116     resser2pdbresser.put(pdbresser2resser.get(pdbresser), pdbresser);
117     }
118     }
119    
120 duarte 210 private void getchainid() throws MsdsdAcCodeNotFoundError, SQLException {
121 duarte 207 chainid=0;
122     String chaincodestr="='"+pdbChainCode+"'";
123     if (pdbChainCode.equals("NULL")){
124     chaincodestr="IS NULL";
125     }
126     String sql = "SELECT chain_id, model_id, pchain_code " +
127     " FROM "+myMsdsdDb+".mmol_chain_info " +
128     " WHERE accession_code='"+pdbCode+"' " +
129     " AND chain_pdb_code "+chaincodestr +
130     " AND chain_type='C' " +
131     " AND asu_chain=1 " +
132     " AND model_serial="+model;
133    
134 duarte 210 Statement stmt = conn.createStatement();
135     ResultSet rsst = stmt.executeQuery(sql);
136     if (rsst.next()) {
137     chainid = rsst.getInt(1);
138     modelid = rsst.getInt(2);
139     chainCode=rsst.getString(3);
140     if (! rsst.isLast()) {
141     System.err.println("More than 1 chain_id match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode);
142     throw new MsdsdAcCodeNotFoundError("More than 1 chain_id match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode);
143     }
144     } else {
145     System.err.println("No chain_id match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode);
146     throw new MsdsdAcCodeNotFoundError("No chain_id could be matched for accession_code "+pdbCode+", chain_pdb_code "+pdbChainCode);
147     }
148     rsst.close();
149     stmt.close();
150 duarte 207 }
151    
152 duarte 210 private boolean check_inconsistent_res_numbering() throws SQLException{
153 duarte 207 int count=0;
154     int numserial=0;
155 duarte 210
156     String sql="SELECT count(*) " +
157     " FROM "+myMsdsdDb+".problem_serial_chain " +
158     " WHERE chain_id="+chainid +
159     " AND (min_serial!=1 OR num_serial!=num_dist_serial OR num_serial!=max_serial-min_serial+1)";
160     Statement stmt = conn.createStatement();
161     ResultSet rsst = stmt.executeQuery(sql);
162     while (rsst.next()) {
163     count = rsst.getInt(1);
164     if (count>0){
165 duarte 207 return true;
166     }
167     }
168 duarte 210 sql="SELECT num_serial FROM "+myMsdsdDb+".problem_serial_chain WHERE chain_id="+chainid;
169     rsst = stmt.executeQuery(sql);
170     int check = 0;
171     while (rsst.next()){
172     check++;
173     numserial=rsst.getInt(1);
174     }
175     if (check!=1){
176     System.err.println("No num_serial match or more than 1 match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode);
177     }
178     String allresseq = read_seq();
179     if (allresseq.length()!=numserial){
180     System.err.println("num_serial and length of all_res_seq don't match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode);
181     return true;
182     }
183     rsst.close();
184     stmt.close();
185 duarte 207 return false;
186     }
187    
188 duarte 210 private void read_atomData() throws SQLException{
189 duarte 207 resser_atom2atomserial = new HashMap<String,Integer>();
190     resser2restype = new HashMap<Integer,String>();
191     atomser2coord = new HashMap<Integer,Double[]>();
192     atomser2resser = new HashMap<Integer,Integer>();
193    
194     String sql = "SELECT serial,chem_atom_name,code_3_letter,residue_serial,x,y,z " +
195     " FROM "+db+".atom_data " +
196     " WHERE (model_id = "+modelid+") " +
197     " AND (chain_id = "+chainid+") " +
198     " AND (graph_alt_code_used = 1) " +
199     " AND (graph_standard_aa=1) " +
200     " AND (pdb_group = 'A')" +
201     " ORDER BY chain_code, residue_serial, serial";
202    
203 duarte 210 Statement stmt = conn.createStatement();
204     ResultSet rsst = stmt.executeQuery(sql);
205     int count=0;
206     while (rsst.next()){
207     count++;
208    
209     int atomserial = rsst.getInt(1); // atomserial
210     String atom = rsst.getString(2).trim(); // atom
211     String res_type = rsst.getString(3).trim(); // res_type
212     int res_serial = rsst.getInt(4); // res_serial
213     double x = rsst.getDouble(5); // x
214     double y = rsst.getDouble(6); // y
215     double z = rsst.getDouble(7); // z
216     Double[] coords = {x, y, z};
217     ArrayList<String> aalist=AA.aas();
218     if (aalist.contains(res_type)) {
219     atomser2coord.put(atomserial, coords);
220     atomser2resser.put(atomserial, res_serial);
221     resser2restype.put(res_serial, res_type);
222     ArrayList<String> atomlist = aas2atoms.get(res_type);
223     if (atomlist.contains(atom)){
224     resser_atom2atomserial.put(res_serial+"_"+atom, atomserial);
225     }
226 duarte 207 }
227 duarte 210
228    
229 duarte 207 }
230 duarte 210 if (count==0){
231     System.err.println("atom data query returned no data at all for model_id="+modelid+", model_id="+modelid);
232     }
233     rsst.close();
234     stmt.close();
235 duarte 207 }
236    
237 duarte 210 private String read_seq() throws SQLException{
238 duarte 207 String allresseq="";
239     String sql="SELECT all_res_seq FROM "+myMsdsdDb+".chain_seq WHERE chain_id="+chainid;
240    
241 duarte 210 Statement stmt = conn.createStatement();
242     ResultSet rsst = stmt.executeQuery(sql);
243     int check = 0;
244     if (rsst.next()) {
245     check++;
246     allresseq=rsst.getString(1);
247     }
248     if (check!=1) {
249     System.err.println("No all_res_seq match or more than 1 match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode+", chain_id="+chainid);
250     }
251     rsst.close();
252     stmt.close();
253    
254 duarte 207 return allresseq;
255     }
256    
257 duarte 210 private HashMap<String,Integer> get_ressers_mapping() throws SQLException {
258 duarte 207 HashMap<String,Integer> map = new HashMap<String, Integer>();
259     String sql="SELECT serial, concat(pdb_seq,IF(pdb_insert_code IS NULL,'',pdb_insert_code)) " +
260     " FROM "+db+".residue " +
261     " WHERE chain_id="+chainid+
262     " AND pdb_seq IS NOT NULL";
263 duarte 210
264     Statement stmt = conn.createStatement();
265     ResultSet rsst = stmt.executeQuery(sql);
266     int count=0;
267     while (rsst.next()) {
268     count++;
269     int resser = rsst.getInt(1);
270     String pdbresser = rsst.getString(2);
271     map.put(pdbresser, resser);
272     }
273     if (count==0) {
274     System.err.println("No residue serials mapping data match for chain_id="+chainid);
275 duarte 207 }
276 duarte 210 rsst.close();
277     stmt.close();
278 duarte 207
279     return map;
280     }
281    
282     }