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