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.Collections; |
7 |
|
|
import java.util.TreeMap; |
8 |
|
|
|
9 |
|
|
import tools.MySQLConnection; |
10 |
|
|
|
11 |
|
|
/** |
12 |
|
|
* A residue interaction graph derived from a single chain pdb protein structure loaded from a graph database in aglappe's format |
13 |
|
|
* |
14 |
|
|
* @author Jose Duarte |
15 |
|
|
* Class: DbGraph |
16 |
|
|
* Package: proteinstructure |
17 |
|
|
*/ |
18 |
|
|
public class DbGraph extends Graph { |
19 |
|
|
|
20 |
|
|
private final static String MYSQLSERVER="white"; |
21 |
|
|
private final static String MYSQLUSER=MySQLConnection.getUserName(); |
22 |
|
|
private final static String MYSQLPWD="nieve"; |
23 |
|
|
|
24 |
|
|
private int graphid=0; |
25 |
|
|
//private int sm_id=0; // for future use |
26 |
|
|
|
27 |
|
|
private String dbname; |
28 |
|
|
private MySQLConnection conn; |
29 |
|
|
|
30 |
|
|
/** |
31 |
|
|
* Constructs Graph object from graph db, given the dbname, pdbCode, pdbChainCode (classic pdb chain code), ct and cutoff |
32 |
|
|
* and passing a MySQLConnection |
33 |
|
|
* @param dbname |
34 |
|
|
* @param conn |
35 |
|
|
* @param pdbCode |
36 |
|
|
* @param pdbChainCode |
37 |
|
|
* @param cutoff |
38 |
|
|
* @param ct |
39 |
|
|
* @throws GraphIdNotFoundError |
40 |
|
|
*/ |
41 |
|
|
public DbGraph(String dbname, MySQLConnection conn, String pdbCode, String pdbChainCode, double cutoff, String ct) throws GraphIdNotFoundError { |
42 |
|
|
this.dbname=dbname; |
43 |
|
|
this.conn=conn; |
44 |
|
|
this.cutoff=cutoff; |
45 |
|
|
this.pdbCode=pdbCode; |
46 |
|
|
this.ct=ct; |
47 |
|
|
// we set the sequence to empty when we read from graph db. We don't have the full sequence in graph db |
48 |
|
|
// when we pass the sequence in getCM to the ContactMap constructor we want to have either a full sequence (with unobserveds) or a blank in case we don't have the info |
49 |
|
|
this.sequence=""; |
50 |
|
|
//TODO graphs in db are never directed, so this doesn't really apply here. Must solve all this! |
51 |
|
|
if (ct.contains("/")){ |
52 |
|
|
directed=true; |
53 |
|
|
} |
54 |
|
|
|
55 |
|
|
getgraphid(); // initialises graphid, sm_id and chainCode |
56 |
|
|
read_graph_from_db(); // gets contacts, nodes and sequence |
57 |
|
|
|
58 |
|
|
this.obsLength=nodes.size(); |
59 |
|
|
if (!sequence.equals("")){ |
60 |
|
|
this.fullLength=sequence.length(); |
61 |
|
|
} else { |
62 |
|
|
// if nodes TreeMap has correct residue numbering then this should get the right full length, |
63 |
|
|
// we will only miss: gaps (unobserved residues) at the end of the sequence. Those we can't know unless full sequence is given |
64 |
|
|
this.fullLength=Collections.max(nodes.keySet()); |
65 |
|
|
} |
66 |
|
|
this.numContacts=contacts.size(); |
67 |
|
|
this.modified=false; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
/** |
71 |
|
|
* Constructs Graph object from graph db, given the dbname, pdbCode, pdbChainCode (classic pdb chain code), ct and cutoff |
72 |
|
|
* MySQLConnection is taken from defaults in DbGraph class: MYSQLSERVER, MYSQLUSER, MYSQLPWD |
73 |
|
|
* @param dbname |
74 |
|
|
* @param pdbCode |
75 |
|
|
* @param pdbChainCode |
76 |
|
|
* @param cutoff |
77 |
|
|
* @param ct |
78 |
|
|
* @throws GraphIdNotFoundError |
79 |
|
|
* @throws SQLException |
80 |
|
|
*/ |
81 |
|
|
public DbGraph(String dbname, String pdbCode, String pdbChainCode, double cutoff, String ct) throws GraphIdNotFoundError, SQLException{ |
82 |
|
|
this(dbname,new MySQLConnection(MYSQLSERVER,MYSQLUSER,MYSQLPWD),pdbCode,pdbChainCode,cutoff,ct); |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
/** |
87 |
|
|
* Constructs Graph object from graph db, given the graphid and passing a MySQLConnection |
88 |
|
|
* @param dbname |
89 |
|
|
* @param conn |
90 |
|
|
* @param graphid |
91 |
|
|
* @throws GraphIdNotFoundError |
92 |
|
|
*/ |
93 |
|
|
public DbGraph(String dbname, MySQLConnection conn, int graphid) throws GraphIdNotFoundError { |
94 |
|
|
this.dbname=dbname; |
95 |
|
|
this.conn=conn; |
96 |
|
|
this.graphid=graphid; |
97 |
|
|
// we set the sequence to empty when we read from graph db. We don't have the full sequence in graph db |
98 |
|
|
// when we pass the sequence in getCM to the ContactMap constructor we want to have either a full sequence (with unobserveds) or a blank in case we don't have the info |
99 |
|
|
this.sequence=""; |
100 |
|
|
|
101 |
|
|
read_graph_from_db(); // gets contacts, nodes and sequence |
102 |
|
|
get_db_graph_info(); // gets pdbCode, pdbChainCode, chainCode, ct and cutoff from db (from graph_id) |
103 |
|
|
|
104 |
|
|
//TODO graphs in db are never directed, so this doesn't really apply here. Must solve all this! |
105 |
|
|
if (ct.contains("/")){ |
106 |
|
|
directed=true; |
107 |
|
|
} |
108 |
|
|
this.obsLength=nodes.size(); |
109 |
|
|
if (!sequence.equals("")){ |
110 |
|
|
this.fullLength=sequence.length(); |
111 |
|
|
} else { |
112 |
|
|
// if nodes TreeMap has correct residue numbering then this should get the right full length, |
113 |
|
|
// we will only miss: gaps (unobserved residues) at the end of the sequence. Those we can't know unless full sequence is given |
114 |
|
|
this.fullLength=Collections.max(nodes.keySet()); |
115 |
|
|
} |
116 |
|
|
this.numContacts=contacts.size(); |
117 |
|
|
this.modified=false; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
/** |
121 |
|
|
* Constructs Graph object from graph db, given the graphid |
122 |
|
|
* MySQLConnection is taken from defaults in DbGraph class: MYSQLSERVER, MYSQLUSER, MYSQLPWD |
123 |
|
|
* @param dbname |
124 |
|
|
* @param graphid |
125 |
|
|
* @throws GraphIdNotFoundError |
126 |
|
|
* @throws SQLException |
127 |
|
|
*/ |
128 |
|
|
public DbGraph(String dbname, int graphid) throws GraphIdNotFoundError, SQLException{ |
129 |
|
|
this(dbname,new MySQLConnection(MYSQLSERVER,MYSQLUSER,MYSQLPWD), graphid); |
130 |
|
|
} |
131 |
|
|
|
132 |
|
|
/** |
133 |
|
|
* Reads contacts and nodes from db. |
134 |
|
|
* The db must be a graph db following our standard format, i.e. must have tables: |
135 |
|
|
* chain_graph, single_model_graph, single_model_node, single_model_edge |
136 |
|
|
* We don't care here about the origin of the data (msdsd, pdbase, predicted) for the generation of the graph as long as it follows our data format |
137 |
|
|
* We read both edges and nodes from single_model_edge and single_model_node. |
138 |
|
|
* The sequence is set to blank, as we can't get the full sequence from graph db |
139 |
|
|
* @param conn |
140 |
|
|
*/ |
141 |
|
|
private void read_graph_from_db(){ |
142 |
|
|
contacts = new ContactList(); |
143 |
|
|
nodes = new TreeMap<Integer, String>(); |
144 |
|
|
try { |
145 |
|
|
// we read only half of the matrix (contacts in one direction only) so that we have the same type of contacts as when creating Graph from Pdb object |
146 |
|
|
String sql="SELECT i_num,j_num FROM "+dbname+".single_model_edge WHERE graph_id="+graphid+" AND j_num>i_num ORDER BY i_num,j_num "; |
147 |
|
|
Statement stmt = conn.createStatement(); |
148 |
|
|
ResultSet rsst = stmt.executeQuery(sql); |
149 |
|
|
while (rsst.next()) { |
150 |
|
|
int i=rsst.getInt(1); |
151 |
|
|
int j=rsst.getInt(2); |
152 |
|
|
contacts.add(new Contact(i,j)); |
153 |
|
|
} |
154 |
|
|
rsst.close(); |
155 |
|
|
stmt.close(); |
156 |
|
|
sql="SELECT num,res FROM "+dbname+".single_model_node WHERE graph_id="+graphid+" ORDER BY num "; |
157 |
|
|
stmt = conn.createStatement(); |
158 |
|
|
rsst = stmt.executeQuery(sql); |
159 |
|
|
while (rsst.next()){ |
160 |
|
|
int num=rsst.getInt(1); |
161 |
|
|
String res=rsst.getString(2); |
162 |
|
|
nodes.put(num, AA.oneletter2threeletter(res)); |
163 |
|
|
} |
164 |
|
|
rsst.close(); |
165 |
|
|
stmt.close(); |
166 |
|
|
} catch (SQLException e) { |
167 |
|
|
e.printStackTrace(); |
168 |
|
|
} |
169 |
|
|
|
170 |
|
|
} |
171 |
|
|
|
172 |
|
|
private void getgraphid () throws GraphIdNotFoundError{ |
173 |
|
|
// input is pdbChainCode i.e. pdb chain code |
174 |
|
|
// we take chainCode (internal chain identifier, pchain_code for msdsd and asym_id for pdbase) from pchain_code field in chain_graph |
175 |
|
|
// (in the chain_graph table the internal chain identifier is called 'pchain_code') |
176 |
|
|
int pgraphid=0; |
177 |
|
|
String chainstr="='"+pdbChainCode+"' "; |
178 |
|
|
if (pdbChainCode.equals("NULL")){ |
179 |
|
|
chainstr=" IS NULL "; |
180 |
|
|
} |
181 |
|
|
try { |
182 |
|
|
String sql="SELECT graph_id, pchain_code FROM "+dbname+".chain_graph WHERE accession_code='"+pdbCode+"' AND chain_pdb_code"+chainstr+" AND dist="+cutoff; |
183 |
|
|
Statement stmt = conn.createStatement(); |
184 |
|
|
ResultSet rsst = stmt.executeQuery(sql); |
185 |
|
|
int check=0; |
186 |
|
|
while (rsst.next()) { |
187 |
|
|
check++; |
188 |
|
|
pgraphid=rsst.getInt(1); |
189 |
|
|
chainCode=rsst.getString(2); |
190 |
|
|
} |
191 |
|
|
if (check!=1){ |
192 |
|
|
System.err.println("No pgraph_id match or more than 1 match for accession_code="+pdbCode+", chain_pdb_code="+pdbChainCode+", dist="+cutoff); |
193 |
|
|
} |
194 |
|
|
rsst.close(); |
195 |
|
|
stmt.close(); |
196 |
|
|
// we set the ctstr to the same as ct except in ALL case, where it is BB+SC+BB/SC |
197 |
|
|
String ctstr=ct; |
198 |
|
|
if (ct.equals("ALL")){ |
199 |
|
|
ctstr="BB+SC+BB/SC"; |
200 |
|
|
} |
201 |
|
|
sql="SELECT graph_id,single_model_id FROM "+dbname+".single_model_graph WHERE pgraph_id="+pgraphid+" AND CT='"+ctstr+"' AND dist="+cutoff+" AND CR='(true)' AND CW=1"; |
202 |
|
|
stmt = conn.createStatement(); |
203 |
|
|
rsst = stmt.executeQuery(sql); |
204 |
|
|
check=0; |
205 |
|
|
while (rsst.next()){ |
206 |
|
|
check++; |
207 |
|
|
graphid=rsst.getInt(1); |
208 |
|
|
//sm_id=rsst.getInt(2); // we might want to use it in the future |
209 |
|
|
} |
210 |
|
|
if (check!=1){ |
211 |
|
|
System.err.println("No graph_id match or more than 1 match for pgraph_id="+pgraphid+", CT="+ctstr+" and cutoff="+cutoff); |
212 |
|
|
throw new GraphIdNotFoundError("No graph_id match or more than 1 match for pgraph_id="+pgraphid+", CT="+ctstr+" and cutoff="+cutoff); |
213 |
|
|
} |
214 |
|
|
} catch (SQLException e) { |
215 |
|
|
e.printStackTrace(); |
216 |
|
|
} |
217 |
|
|
|
218 |
|
|
} |
219 |
|
|
|
220 |
|
|
private void get_db_graph_info() throws GraphIdNotFoundError { |
221 |
|
|
try { |
222 |
|
|
int pgraphid=0; |
223 |
|
|
String sql="SELECT pgraph_id,CT,dist FROM "+dbname+".single_model_graph WHERE graph_id="+graphid; |
224 |
|
|
Statement stmt = conn.createStatement(); |
225 |
|
|
ResultSet rsst = stmt.executeQuery(sql); |
226 |
|
|
int check=0; |
227 |
|
|
while (rsst.next()) { |
228 |
|
|
check++; |
229 |
|
|
pgraphid=rsst.getInt(1); |
230 |
|
|
ct=rsst.getString(2); |
231 |
|
|
if (ct.equals("BB+SC+BB/SC")) ct="ALL"; |
232 |
|
|
cutoff=rsst.getDouble(3); |
233 |
|
|
} |
234 |
|
|
if (check!=1){ |
235 |
|
|
System.err.println("No pgraph_id match or more than 1 match for graph_id="+graphid); |
236 |
|
|
throw new GraphIdNotFoundError("No pgraph_id match or more than 1 match for graph_id="+graphid+" in db"+conn.getDbname()); |
237 |
|
|
} |
238 |
|
|
rsst.close(); |
239 |
|
|
stmt.close(); |
240 |
|
|
sql="SELECT accession_code, chain_pdb_code, pchain_code FROM "+dbname+".chain_graph WHERE graph_id="+pgraphid; |
241 |
|
|
stmt = conn.createStatement(); |
242 |
|
|
rsst = stmt.executeQuery(sql); |
243 |
|
|
check=0; |
244 |
|
|
while (rsst.next()){ |
245 |
|
|
check++; |
246 |
|
|
pdbCode=rsst.getString(1); |
247 |
|
|
pdbChainCode=rsst.getString(2); |
248 |
|
|
// java returns a null if the field is a database null, we want actually the "NULL" string in that case |
249 |
|
|
if (pdbChainCode==null) pdbChainCode="NULL"; |
250 |
|
|
chainCode=rsst.getString(3); |
251 |
|
|
} |
252 |
|
|
if (check!=1){ |
253 |
|
|
System.err.println("No accession_code+chain_pdb_code+pchain_code match or more than 1 match for graph_id="+pgraphid+" in chain_graph table"); |
254 |
|
|
} |
255 |
|
|
rsst.close(); |
256 |
|
|
stmt.close(); |
257 |
|
|
} catch (SQLException e) { |
258 |
|
|
e.printStackTrace(); |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
} |
262 |
|
|
|
263 |
|
|
} |