ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/proteinstructure/PdbaseInfo.java
Revision: 124
Committed: Wed May 9 11:57:24 2007 UTC (17 years, 4 months ago) by duarte
File size: 8607 byte(s)
Log Message:
Added error handling for PdbaseInfo: 2 new exception classes
Line User Rev File contents
1 duarte 123 package proteinstructure;
2    
3     import tools.MySQLConnection;
4 duarte 124
5     import java.security.AccessControlException;
6 duarte 123 import java.sql.Statement;
7     import java.sql.ResultSet;
8     import java.sql.SQLException;
9     import java.util.ArrayList;
10     import java.util.HashMap;
11     import java.util.Collections;
12    
13     public class PdbaseInfo {
14     public final static String MYSQLSERVER="white";
15     public final static String MYSQLUSER="duarte"; //TODO implement getuser method
16     public final static String MYSQLPWD="nieve";
17     public final static String mymsdsdDB="my_msdsd_00_07_a";
18     public final static String msdsdDB="msdsd_00_07_a";
19     public final static String pdbaseDB="pdbase";
20    
21     MySQLConnection conn;
22     String accode="";
23     String chaincode="";
24     int model=1;
25     int entrykey;
26     String asymid;
27     int entitykey;
28     String alt_locs_sql_str;
29    
30 duarte 124 PdbaseInfo (String accode, String chaincode, int model_serial, String db) throws PdbaseInconsistencyError, PdbaseAcCodeNotFoundError{
31 duarte 123 this.accode=accode;
32     this.chaincode=chaincode;
33     this.model=model_serial;
34     this.conn = new MySQLConnection(MYSQLSERVER,MYSQLUSER,MYSQLPWD,db);
35     this.entrykey=get_entry_key();
36     this.asymid=get_asym_id();
37     this.entitykey=get_entity_key();
38     this.alt_locs_sql_str=get_atom_alt_locs();
39    
40     }
41    
42 duarte 124 PdbaseInfo (String accode, String chaincode, String db) throws PdbaseInconsistencyError, PdbaseAcCodeNotFoundError {
43 duarte 123 this(accode,chaincode,1,db);
44     }
45 duarte 124 PdbaseInfo (String accode, String chaincode, int model_serial) throws PdbaseInconsistencyError, PdbaseAcCodeNotFoundError {
46 duarte 123 this(accode,chaincode,model_serial,pdbaseDB);
47     }
48    
49 duarte 124 PdbaseInfo (String accode, String chaincode) throws PdbaseInconsistencyError, PdbaseAcCodeNotFoundError {
50 duarte 123 this(accode,chaincode,1,pdbaseDB);
51     }
52    
53     public void close() {
54     conn.close();
55     }
56    
57 duarte 124 public int get_entry_key() throws PdbaseAcCodeNotFoundError {
58 duarte 123 String sql="SELECT entry_key FROM struct WHERE entry_id='"+accode.toUpperCase()+"'";
59     try {
60     Statement stmt = conn.createStatement();
61     ResultSet rsst = stmt.executeQuery(sql);
62     if (rsst.next()) {
63     entrykey = rsst.getInt(1);
64     if (! rsst.isLast()) {
65     System.err.println("More than 1 entry_key match for accession_code="+accode+", chain_pdb_code="+chaincode);
66 duarte 124 throw new PdbaseAcCodeNotFoundError();
67 duarte 123 }
68     } else {
69     System.err.println("No entry_key match for accession_code="+accode+", chain_pdb_code="+chaincode);
70 duarte 124 throw new PdbaseAcCodeNotFoundError();
71 duarte 123 }
72     rsst.close();
73     stmt.close();
74     } catch (SQLException e) {
75     e.printStackTrace();
76     }
77     return entrykey;
78     }
79    
80 duarte 124 public String get_asym_id() throws PdbaseInconsistencyError {
81 duarte 123 String pdbstrandid=chaincode;
82     if (chaincode.equals("NULL")){
83     pdbstrandid="A";
84     }
85     String sql="SELECT asym_id " +
86     " FROM pdbx_poly_seq_scheme " +
87     " WHERE entry_key=" + entrykey +
88     " AND pdb_strand_id='"+pdbstrandid+"' " +
89     " LIMIT 1";
90     try {
91     Statement stmt = conn.createStatement();
92     ResultSet rsst = stmt.executeQuery(sql);
93     if (rsst.next()) {
94     asymid = rsst.getString(1);
95     } else {
96     System.err.println("No asym_id match for entry_key="+entrykey+", pdb_strand_id="+chaincode);
97 duarte 124 throw new PdbaseInconsistencyError("No asym_id match for entry_key="+entrykey+", pdb_strand_id="+chaincode);
98 duarte 123 }
99     rsst.close();
100     stmt.close();
101     } catch (SQLException e) {
102     e.printStackTrace();
103     }
104     return asymid;
105     }
106    
107 duarte 124 public int get_entity_key() throws PdbaseInconsistencyError {
108 duarte 123 String sql="SELECT entity_key " +
109     " FROM struct_asym " +
110     " WHERE entry_key="+ entrykey +
111     " AND id='"+asymid+"'";
112     try {
113     Statement stmt = conn.createStatement();
114     ResultSet rsst = stmt.executeQuery(sql);
115     if (rsst.next()) {
116     entitykey = rsst.getInt(1);
117     if (! rsst.isLast()) {
118     System.err.println("More than 1 entity_key match for entry_key="+entrykey+", asym_id="+asymid);
119 duarte 124 throw new PdbaseInconsistencyError("More than 1 entity_key match for entry_key="+entrykey+", asym_id="+asymid);
120 duarte 123 }
121     } else {
122     System.err.println("No entity_key match for entry_key="+entrykey+", asym_id="+asymid);
123 duarte 124 throw new PdbaseInconsistencyError("No entity_key match for entry_key="+entrykey+", asym_id="+asymid);
124 duarte 123 }
125     rsst.close();
126     stmt.close();
127     } catch (SQLException e) {
128     e.printStackTrace();
129     }
130     return entitykey;
131     }
132    
133 duarte 124 public String get_atom_alt_locs() throws PdbaseInconsistencyError{
134 duarte 123 ArrayList<String> alt_ids = new ArrayList<String>();
135     HashMap<String,Integer> alt_ids2keys = new HashMap<String,Integer>();
136     String alt_loc_field="label_alt_key";
137     String sql="SELECT id, atom_sites_alt_key FROM atom_sites_alt WHERE entry_key="+entrykey;
138     try {
139     Statement stmt = conn.createStatement();
140     ResultSet rsst = stmt.executeQuery(sql);
141     int count=0;
142     while (rsst.next()) {
143     count++;
144     alt_ids.add(rsst.getString(1));
145     alt_ids2keys.put(rsst.getString(1), rsst.getInt(2));
146     }
147     if (count!=0){
148     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
149     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 "+pdbaseDB+" db!");
150 duarte 124 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 "+pdbaseDB+" db!");
151 duarte 123 }
152     alt_ids.remove(".");
153     Collections.sort(alt_ids);
154     String lowest_alt_id = alt_ids.get(0);
155     alt_locs_sql_str = "("+alt_loc_field+"="+alt_ids2keys.get(".")+" OR "+alt_loc_field+"="+alt_ids2keys.get(lowest_alt_id)+")";
156     } else {
157     alt_locs_sql_str=alt_loc_field+" IS NULL";
158     }
159    
160     rsst.close();
161     stmt.close();
162     } catch (SQLException e) {
163     e.printStackTrace();
164     }
165    
166     return alt_locs_sql_str;
167     }
168    
169 duarte 124 public ArrayList<ArrayList> read_atomData() throws PdbaseInconsistencyError{
170 duarte 123 ArrayList<ArrayList> resultset = new ArrayList<ArrayList>();
171     String sql = "SELECT id, label_atom_id, label_comp_id, label_asym_id, label_seq_id, Cartn_x, Cartn_y, Cartn_z " +
172     " FROM atom_site " +
173     " WHERE entry_key="+entrykey +
174     " AND label_asym_id='"+asymid+"' " +
175     " AND label_entity_key="+ entitykey +
176     " AND model_num="+ model +
177     " AND "+alt_locs_sql_str;
178     try {
179     Statement stmt = conn.createStatement();
180     ResultSet rsst = stmt.executeQuery(sql);
181 duarte 124 int count=0;
182 duarte 123 while (rsst.next()){
183 duarte 124 count++;
184 duarte 123 ArrayList thisrecord = new ArrayList();
185     thisrecord.add(rsst.getInt(1)); //atomserial
186     thisrecord.add(rsst.getString(2).trim()); // atom
187     thisrecord.add(rsst.getString(3).trim()); // res_type
188     thisrecord.add(rsst.getString(4).trim()); // chain
189     thisrecord.add(rsst.getInt(5)); //res_serial
190     thisrecord.add(rsst.getDouble(6)); // x
191     thisrecord.add(rsst.getDouble(7)); // y
192     thisrecord.add(rsst.getDouble(8)); // z
193    
194     resultset.add(thisrecord);
195     }
196 duarte 124 if (count==0){
197     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+"'");
198     }
199 duarte 123 rsst.close();
200     stmt.close();
201     } catch (SQLException e) {
202     e.printStackTrace();
203     }
204     return resultset;
205     }
206    
207 duarte 124 public String read_seq() throws PdbaseInconsistencyError{
208 duarte 123 String sequence="";
209     String pdbstrandid=chaincode;
210     if (chaincode.equals("NULL")){
211     pdbstrandid="A";
212     }
213     // we use seq_id+0 (implicitly converts to int) in ORDER BY because seq_id is varchar!!
214     String sql="SELECT mon_id" +
215     " FROM pdbx_poly_seq_scheme " +
216     " WHERE entry_key=" + entrykey +
217     " AND asym_id='"+asymid+"' " +
218     " AND pdb_strand_id='"+pdbstrandid+"' " +
219     " ORDER BY seq_id+0";
220     try {
221     Statement stmt = conn.createStatement();
222     ResultSet rsst = stmt.executeQuery(sql);
223     ArrayList<String> aalist=AA.aas();
224     int count=0;
225     while (rsst.next()) {
226     count++;
227     String res_type = rsst.getString(1);
228     if (aalist.contains(res_type)){
229     sequence+=AA.threeletter2oneletter(res_type);
230     } else {
231     sequence+="X";
232     }
233     }
234     if (count==0) {
235     System.err.println("No sequence data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
236 duarte 124 throw new PdbaseInconsistencyError("No sequence data match for entry_key="+entrykey+", asym_id="+asymid+", pdb_strand_id="+pdbstrandid);
237 duarte 123 }
238     rsst.close();
239     stmt.close();
240     } catch (SQLException e) {
241     e.printStackTrace();
242     }
243    
244     return sequence;
245     }
246     }