1 |
import tools.MySQLConnection; |
2 |
|
3 |
import java.sql.SQLException; |
4 |
import java.sql.Statement; |
5 |
import java.sql.ResultSet; |
6 |
|
7 |
public class cnEvalTargetNbhood { |
8 |
|
9 |
/** |
10 |
* CN2 size x sumdelta |
11 |
* iterating over all steps by exchange of one neighbor at a time by a common neighbor |
12 |
* with subsequent scoring |
13 |
* @author lappe |
14 |
*/ |
15 |
static int maxRank = 21; // value to replace for non-existence of central redue in the resultvector (rank=0) |
16 |
// higher values should penalize non-existence more |
17 |
static int VL=1; // Verbosity Level |
18 |
static String user = "lappe" ; // change user name!! |
19 |
static MySQLConnection conn; |
20 |
static String backgrndDB = "pdb_reps_graph_4_2"; |
21 |
static String targetNodes = "target_node"; |
22 |
static String targetEdges = "target_edge"; |
23 |
static double lastEntropy=0.0, lastFreq, lastAUC, lastavgk, lastdevk, lastCorr; |
24 |
static int lastRank, lastTotal, lastxcn=0; |
25 |
static int graphid=0, resnr=0; |
26 |
static int size1=0, size2=0; // the dimensions of the matrices = |shell1|x|shell2| = nr of direct(1) x indirect(2) nbs |
27 |
static String rsideline[], rheadline[]={" -->\t(-)1st shell\t0"," |\t \t-"," |\t(+) \t0"," V\t2nd shell\t-", " \t \tX"}; |
28 |
static String sideline[], headline[]={"\t\t0","\t\t-","\t\t0","\t\t-","\t\tX"}; |
29 |
static String restype="?", ressec="?", newnbhood=""; |
30 |
static int cn1[], cn2[], matsize[][]; |
31 |
static int sumdelta[][], rank[][], total[][], cnall[][]; |
32 |
static double cnsize[][], correct[][], entropy[][], freq[][], AUC[][]; |
33 |
static String newnbs[][], nbstring[][], moveset[][]; |
34 |
|
35 |
static int printRank=1, |
36 |
printTotal = 2, |
37 |
printEntropy = 3, |
38 |
printFreq = 4, |
39 |
printAUC = 5, |
40 |
printNbstring= 6, |
41 |
printMoveset = 7, |
42 |
printdeltaRank=8, |
43 |
printCNSize =9, |
44 |
printCNSxdelta=10, |
45 |
printMatSize =11, |
46 |
printCNSdMSxdelta=12, |
47 |
printCorrect = 13, |
48 |
printCordMSxdelta=14; |
49 |
|
50 |
public static void main(String[] args) { |
51 |
|
52 |
if (args.length<2){ |
53 |
System.err.println("The graph_id and residue-nr. needs to be given .... i.e. 7 110"); |
54 |
System.exit(1); |
55 |
} |
56 |
graphid = Integer.parseInt( args[0]); |
57 |
resnr = Integer.parseInt( args[1]); |
58 |
int j_num=0, oj_num=0, oj_shell, oj_cnsize, i, j, x, oi, oj, o1, o2, ixnum, jxnum, score=0; |
59 |
boolean overx = false; |
60 |
String sql, oj_res, oj_sec; |
61 |
Statement mstmt, mjst, nstmt; |
62 |
ResultSet mrsst, nrsst; |
63 |
|
64 |
try { |
65 |
conn = new MySQLConnection("white",user,"nieve", backgrndDB); // the UPPERCASE DB! |
66 |
System.out.println("Scoring Target neighborhoods v.0.4. "); |
67 |
|
68 |
sql = "select num, res, sstype from "+targetNodes+" where graph_id="+graphid+" and num="+resnr+";"; |
69 |
mstmt = conn.createStatement(); |
70 |
mrsst = mstmt.executeQuery(sql); |
71 |
if (mrsst.next()) { |
72 |
// this is the central node -> get type and secondary structure |
73 |
restype = mrsst.getString( 2).toUpperCase(); |
74 |
ressec = mrsst.getString( 3).toUpperCase(); |
75 |
} // end if central residue |
76 |
mrsst.close(); |
77 |
mstmt.close(); |
78 |
System.out.println("GraphID "+graphid+" Central residue is "+restype+":"+resnr+":"+ressec); |
79 |
|
80 |
// retrieve the original nbhood into orig_shell |
81 |
System.out.println("retrieving original first shell ... "); |
82 |
mstmt = conn.createStatement(); |
83 |
mstmt.executeUpdate("drop table if exists orig_shell;"); |
84 |
mstmt.close(); |
85 |
|
86 |
mstmt = conn.createStatement(); |
87 |
mstmt.executeUpdate("create table orig_shell as select i_num, i_res, j_num, j_res, j_sstype, 1 as shell from "+targetEdges+" where graph_id="+graphid+" and i_num="+resnr+";"); |
88 |
mstmt.close(); |
89 |
|
90 |
System.out.println("adding the original 2nd shell ..."); |
91 |
sql = "select j_num from orig_shell where shell=1;"; |
92 |
mstmt = conn.createStatement(); |
93 |
mrsst = mstmt.executeQuery(sql); |
94 |
i=0; |
95 |
while (mrsst.next()) { |
96 |
i++; |
97 |
oj_num = mrsst.getInt(1); |
98 |
System.out.println(i+":"+oj_num); |
99 |
mjst = conn.createStatement(); |
100 |
sql = "insert into orig_shell select i_num, i_res, j_num, j_res, j_sstype, 2 as shell from "+targetEdges+" where graph_id="+graphid+" and i_num="+oj_num+";"; |
101 |
// System.out.println(">"+sql); |
102 |
mjst.executeUpdate( sql); |
103 |
mjst.close(); |
104 |
} // end while |
105 |
mrsst.close(); |
106 |
mstmt.close(); |
107 |
|
108 |
System.out.println("gathering the original 1st and 2nd shell nbs."); |
109 |
sql = "select j_num, j_res, j_sstype, min(shell) as shell, count(*) as cn from orig_shell where j_num!="+resnr+" group by j_num order by j_num;"; |
110 |
mstmt = conn.createStatement(); |
111 |
mrsst = mstmt.executeQuery(sql); |
112 |
o1=0; |
113 |
o2=0; |
114 |
while (mrsst.next()) { |
115 |
if ( mrsst.getInt( 4)==1) { // count 1st shell entry |
116 |
o1++; |
117 |
System.out.print("1#"+o1); |
118 |
rheadline[0]+="\t"+o1; |
119 |
rheadline[1]+="\t"+mrsst.getString(2); // res |
120 |
rheadline[2]+="\t"+mrsst.getInt(1); // resnum |
121 |
rheadline[3]+="\t"+mrsst.getString(3); // SStype |
122 |
rheadline[4]+="\t("+mrsst.getInt(5)+")"; // CN |
123 |
} // end if 2st shell |
124 |
if ( mrsst.getInt( 4)==2) { // count 2nd shell entry |
125 |
o2++; |
126 |
System.out.print("2#"+o2); |
127 |
} // end if 2nd shell |
128 |
System.out.println(" :\t"+mrsst.getInt( 1)+"\t"+mrsst.getString( 2)+"\t"+mrsst.getString( 3)+"\t"+mrsst.getInt( 4)+"\t"+mrsst.getInt( 5)); |
129 |
} // end while |
130 |
System.out.println("Orig.SIZE 1st shell "+o1); |
131 |
System.out.println("Orig.SIZE 2nd shell "+o2); |
132 |
rheadline[4] = rheadline[4].replace("X", ("("+o1)+")"); |
133 |
rsideline = new String[o2+1]; |
134 |
rsideline[0]="+0\tRnum:S(cn)"; |
135 |
sumdelta = new int[(o1+1)][(o2+1)]; |
136 |
newnbs = new String[(o1+1)][(o2+1)]; |
137 |
cnall = new int[(o1+1)][(o2+1)]; |
138 |
matsize = new int[(o1+1)][(o2+1)]; |
139 |
correct = new double[(o1+1)][(o2+1)]; |
140 |
// creating the perturbed version of shell 1 into temp_shell |
141 |
for (j=0; j<=o2; j++) { // <=o2 outer loop through all originally indirect contacts |
142 |
|
143 |
for (i=0; i<=o1; i++) { // inner loop through all originally direct contacts |
144 |
if (VL>=1) { |
145 |
System.out.println("---------------------------------------------"); |
146 |
System.out.println("Creating perturbed nbhood ("+i+","+j+")\t"); |
147 |
} |
148 |
// clear first |
149 |
nstmt = conn.createStatement(); |
150 |
nstmt.executeUpdate("drop table if exists temp_shell;"); |
151 |
nstmt.close(); |
152 |
nstmt = conn.createStatement(); |
153 |
nstmt.executeUpdate("create table temp_shell select * from orig_shell limit 0;"); |
154 |
nstmt.close(); |
155 |
oi = 0; |
156 |
oj = 0; |
157 |
mrsst.beforeFirst(); |
158 |
newnbhood=""; |
159 |
overx = false; |
160 |
ixnum=0; |
161 |
jxnum=0; |
162 |
while (mrsst.next()) { |
163 |
oj_num = mrsst.getInt( 1); |
164 |
oj_res = mrsst.getString(2); |
165 |
oj_sec = mrsst.getString(3); |
166 |
oj_shell = mrsst.getInt( 4); |
167 |
oj_cnsize = mrsst.getInt( 5); |
168 |
if (oj_num>resnr) { // we are over x |
169 |
if (!overx) { |
170 |
newnbhood+="x"; |
171 |
overx=true; |
172 |
} // end if over x |
173 |
} // END IF J > X |
174 |
if (oj_shell==1) { // a direct 1st shell neighbour |
175 |
oi++; |
176 |
if (oi!=i) {// if this is NOT the one direct nb 2B dropped |
177 |
// include as 1st shell nbor into temp_shell |
178 |
nstmt = conn.createStatement(); |
179 |
sql = "insert into temp_shell values("+resnr+",\'"+restype+"\',"+oj_num+",\'"+oj_res+"\',\'"+oj_sec+"\', 1);"; |
180 |
// System.out.println("oi>"+ sql); |
181 |
nstmt.executeUpdate(sql); |
182 |
nstmt.close(); |
183 |
newnbhood+=oj_res; |
184 |
} else { |
185 |
ixnum=oj_num; |
186 |
} // end if ni!=i |
187 |
} else { // 2nd shell neighbour |
188 |
oj++; |
189 |
if (oj==j) { // this is the 2nd shell nb 2B included |
190 |
// put as new 1st shell nbor |
191 |
nstmt = conn.createStatement(); |
192 |
sql = "insert into temp_shell values("+resnr+",\'"+restype+"\',"+oj_num+",\'"+oj_res+"\',\'"+oj_sec+"\', 1);"; |
193 |
// System.out.println("oj>"+ sql); |
194 |
nstmt.executeUpdate(sql); |
195 |
nstmt.close(); |
196 |
newnbhood+=oj_res; |
197 |
jxnum=oj_num; |
198 |
} // end if |
199 |
if (j==0) { // creating the sideline ruler array for the output |
200 |
rsideline[oj] = "+"+oj+"\t"+oj_res+""+oj_num+":"+oj_sec+"("+oj_cnsize+")"; |
201 |
} // end if j==0 |
202 |
} // end if 1st/2nd shell |
203 |
|
204 |
} // end while through the entire nbhood |
205 |
if (!overx) { // we haven't seen a nb > x yet |
206 |
newnbhood+="x"; // x sits at the end of the nbhoodstring |
207 |
overx=true; |
208 |
} // end if over x |
209 |
// System.out.println("new direct nbhood "+newnbhood); |
210 |
// Now the "updated" / perturbed version of shell 1 is in temp_shell |
211 |
// we can build 2nd shell accordingly. |
212 |
// System.out.println("building the 2nd shell"); |
213 |
sql = "select j_num, j_res, j_sstype from temp_shell where shell=1;"; |
214 |
nstmt = conn.createStatement(); |
215 |
nrsst = nstmt.executeQuery(sql); |
216 |
x = 0; |
217 |
while (nrsst.next()) { |
218 |
x++; |
219 |
j_num = nrsst.getInt( 1); |
220 |
// System.out.println(x+":"+nrsst.getString( 2)+" "+j_num+" "+nrsst.getString( 3)); |
221 |
mjst = conn.createStatement(); |
222 |
sql = "insert into temp_shell select i_num, i_res, j_num, j_res, j_sstype, 2 as shell from "+targetEdges+" where graph_id="+graphid+" and i_num="+j_num+";"; |
223 |
// System.out.println(">"+sql); |
224 |
mjst.executeUpdate( sql); |
225 |
mjst.close(); |
226 |
} // end while |
227 |
nrsst.close(); |
228 |
nstmt.close(); |
229 |
// and score this move |
230 |
lastxcn=0; |
231 |
newnbs[i][j]= newnbhood; |
232 |
System.out.print("\n["+i+"]["+j+"]\t-"+ixnum+"/+"+jxnum+"\t"+newnbs[i][j]+"\t "); |
233 |
score = scoreCurrentNbhood( ixnum, jxnum); |
234 |
sumdelta[i][j] = score; |
235 |
cnall[i][j] = lastxcn; |
236 |
matsize[i][j] = (size1*size2)-1; |
237 |
correct[i][j] = lastCorr; |
238 |
if (VL>=1) { |
239 |
reportMatrix( printRank ); |
240 |
reportMatrix( printdeltaRank ); |
241 |
reportMatrix( printCNSize ); |
242 |
reportMatrix( printCNSxdelta); |
243 |
System.out.println("SumDeltaRank Score = \t"+score); |
244 |
System.out.println("corrected Score = \t"+correct[i][j]); |
245 |
System.out.println("Matrix size n1*n2-1= \t("+size1+"*"+size2+")-1 ="+matsize[i][j]); |
246 |
System.out.println("CN1 x CN2 product = \t"+cnall[i][j]); |
247 |
} else { |
248 |
System.out.print("\t"+score+"*"+cnall[i][j]+"\t= "+(score*cnall[i][j])); |
249 |
} |
250 |
// reportMatrix( printNbstring ); |
251 |
} // next i |
252 |
System.out.println("\t"); |
253 |
} // next j |
254 |
// report total matrix sumdelta |
255 |
// if (VL>=1) { |
256 |
System.out.println("GraphID "+graphid+" Central residue is "+restype+":"+resnr+":"+ressec); |
257 |
System.out.println("backgroundDB"+backgrndDB+" \t maxRank : "+maxRank); |
258 |
reportResults( o1, o2, printRank); |
259 |
reportResults( o1, o2, printCNSize); |
260 |
reportResults( o1, o2, printMatSize); |
261 |
//} |
262 |
reportResults( o1, o2, printCNSxdelta ); |
263 |
reportResults( o1, o2, printCNSdMSxdelta ); |
264 |
reportResults( o1, o2, printCorrect); |
265 |
reportResults( o1, o2, printCordMSxdelta); |
266 |
// Cleanup ... |
267 |
mrsst.close(); |
268 |
mstmt.close(); |
269 |
|
270 |
} catch (SQLException e) { |
271 |
e.printStackTrace(); |
272 |
System.err.println("SQLException: " + e.getMessage()); |
273 |
System.err.println("SQLState: " + e.getSQLState()); |
274 |
} // end try/catch |
275 |
System.out.println("fin."); |
276 |
} // end main |
277 |
|
278 |
|
279 |
|
280 |
public static void reportResults( int o1, int o2, int what2print) { |
281 |
System.out.println("Printing "+what2print); |
282 |
if (what2print==printRank) System.out.println("Overall resulting SumDeltaRank Matrix" ); |
283 |
if (what2print==printNbstring) System.out.println("Overall tested new nbhoodStrings" ); |
284 |
if (what2print==printCNSize) System.out.println("Overall cnsize" ); |
285 |
if (what2print==printCNSxdelta) System.out.println("Overall (cnsize*sumDeltaRank)" ); |
286 |
/*if (what2print==printTotal) System.out.print("total[i][j]" ); |
287 |
if (what2print==printEntropy) System.out.print("entropy[i][j]" ); |
288 |
if (what2print==printFreq ) System.out.print("freq[i][j]" ); |
289 |
if (what2print==printAUC) System.out.print("AUC[i][j]" ); |
290 |
|
291 |
if (what2print==printMoveset) System.out.print("moveset[i][j]" ); |
292 |
if (what2print==printdeltaRank) System.out.print("rank[i][j]-rank[0][0]" );) */ |
293 |
|
294 |
// print headerline(s) |
295 |
System.out.println(rheadline[0]); |
296 |
System.out.println(rheadline[1]); |
297 |
System.out.println(rheadline[2]); |
298 |
System.out.println(rheadline[3]); |
299 |
System.out.println(rheadline[4]); |
300 |
for (int j=0; j<=o2; j++) { |
301 |
// print rsideline |
302 |
System.out.print( rsideline[j]+"\t"); |
303 |
for ( int i=0; i<=o1; i++) { |
304 |
if (what2print==printRank) System.out.print( sumdelta[i][j] ); |
305 |
if (what2print==printNbstring) System.out.print( newnbs[i][j] ); |
306 |
if (what2print==printCNSize) System.out.print( cnall[i][j] ); |
307 |
if (what2print==printCNSxdelta) System.out.print( sumdelta[i][j]*cnall[i][j] ); |
308 |
if (what2print==printMatSize) System.out.print( matsize[i][j] ); |
309 |
if (what2print==printCNSdMSxdelta) System.out.print( sumdelta[i][j]*cnall[i][j]/matsize[i][j] ); |
310 |
if (what2print==printCorrect) System.out.print( correct[i][j] ); |
311 |
if (what2print==printCordMSxdelta) System.out.print( correct[i][j]*cnall[i][j]/matsize[i][j] ); |
312 |
System.out.print("\t"); |
313 |
} // next i |
314 |
System.out.println(""); |
315 |
} // next j |
316 |
} // end of reportResults |
317 |
|
318 |
|
319 |
|
320 |
public static int scoreCurrentNbhood( int ixnum, int jxnum) { |
321 |
int ixcn=0, jxcn=0, n1=0, n2=0, ni, nj, i, j, j_num, j_shell, j_cnsize, sumdeltarank=0; |
322 |
String sql, j_res, j_sec, nbs, mymove, precol; |
323 |
boolean overx = false; |
324 |
lastCorr = 0.0; |
325 |
Statement stmt; |
326 |
ResultSet rsst; |
327 |
|
328 |
try { |
329 |
headline[0]="\t\t\t0"; |
330 |
headline[1]="\t\t\t-"; |
331 |
headline[2]="\t\t\t0"; |
332 |
headline[3]="\t\t\t-"; |
333 |
headline[4]="\t\t\tX"; |
334 |
|
335 |
// System.out.println("retrieving the entire nbhood (1st and 2nd shell)"); |
336 |
sql = "select j_num, j_res, j_sstype, min(shell) as shell, count(*) as cn from temp_shell where j_num!="+resnr+" group by j_num order by j_num;"; |
337 |
stmt = conn.createStatement(); |
338 |
rsst = stmt.executeQuery(sql); |
339 |
// counting shell2 |
340 |
n2=0; |
341 |
while (rsst.next()) { |
342 |
if ( rsst.getInt( 4)==1) { // count 1st shell entry |
343 |
n1++; |
344 |
// System.out.print("1#"+n1); |
345 |
headline[0]+="\t"+n1; |
346 |
headline[1]+="\t"+rsst.getString(2); // res |
347 |
headline[2]+="\t"+rsst.getInt(1); // resnum |
348 |
headline[3]+="\t"+rsst.getString(3); // SStype |
349 |
headline[4]+="\t("+((rsst.getInt(5))-1)+")"; // CNSize |
350 |
} // end if 2st shell |
351 |
if ( rsst.getInt( 4)==2) { // count 2nd shell entry |
352 |
n2++; |
353 |
// System.out.print("2#"+n2); |
354 |
} // end if 2nd shell |
355 |
// System.out.println(" :\t"+rsst.getInt( 1)+"\t"+rsst.getString( 2)+"\t"+rsst.getString( 3)+"\t"+rsst.getInt( 4)+"\t"+rsst.getInt( 5)); |
356 |
} // end while |
357 |
size1 = n1; |
358 |
size2 = n2; |
359 |
ixcn = n1; |
360 |
jxcn = n1; |
361 |
if (VL>=1) { |
362 |
System.out.println("|1st shell|="+size1+" \tx\t |2nd shell|="+size2); |
363 |
} |
364 |
headline[4]=headline[4].replace("X",("("+size1+")")); |
365 |
|
366 |
// n1 and n2 are known, initialise matrices accordingly. |
367 |
// nbhood, move, rank, entropy, freq, AUC etc. (evtl.+ degree(?)) |
368 |
rank = new int[(n1+1)][(n2+1)]; |
369 |
rank[0][0]=maxRank; |
370 |
total = new int[(n1+1)][(n2+1)]; |
371 |
entropy = new double[(n1+1)][(n2+1)]; |
372 |
freq = new double[(n1+1)][(n2+1)]; |
373 |
AUC = new double[(n1+1)][(n2+1)]; |
374 |
nbstring = new String[(n1+1)][(n2+1)]; |
375 |
moveset = new String[(n1+1)][(n2+1)]; |
376 |
sideline = new String[n2+1]; |
377 |
cn1 = new int[n1+1]; |
378 |
cn2 = new int[n2+1]; |
379 |
cnsize = new double[(n1+1)][(n2+1)]; |
380 |
|
381 |
for (j=0; j<=n2; j++) { // outer loop through all indirect contacts |
382 |
for (i=0; i<=n1; i++) { // inner loop through all direct contacts |
383 |
mymove = ""; |
384 |
overx = false; |
385 |
if (VL>=1) { |
386 |
System.out.print("("+i+","+j+")\t"); |
387 |
} |
388 |
ni = 0; |
389 |
nj = 0; |
390 |
sideline[0]="+0\tRnum:S("+n1+")"; |
391 |
cn1[0]=n1; |
392 |
cn2[0]=n1; |
393 |
nbs="%"; |
394 |
rsst.beforeFirst(); |
395 |
while (rsst.next()) { |
396 |
j_num = rsst.getInt(1); |
397 |
j_res = rsst.getString(2); |
398 |
j_sec = rsst.getString(3); |
399 |
j_shell = rsst.getInt(4); |
400 |
j_cnsize = rsst.getInt(5); |
401 |
|
402 |
// if edge exists between the dropped direct and the new (ind.)nbour then cnsize of dropped+1 |
403 |
|
404 |
if (j_num>resnr) { // we are over x |
405 |
if (!overx) { |
406 |
nbs+="x%"; |
407 |
overx=true; |
408 |
} // end if over x |
409 |
} // END IF J > X |
410 |
if (j_shell==1) { // a direct 1st shell neighbour |
411 |
ni++; |
412 |
if (ni!=i) {// if this is NOT the one direct nb 2B dropped |
413 |
nbs+=j_res.toUpperCase()+"%"; // it is included |
414 |
if ( j_num==jxnum && j==0) { // This is the direct nb dropped |
415 |
jxcn=j_cnsize; |
416 |
if (VL>=2) System.out.print("(j"+jxnum+":"+jxcn+")"); |
417 |
} |
418 |
} else { // this one IS dropped |
419 |
mymove += "(-"+j_res+":"+j_num+":"+j_sec+"/"+j_cnsize+")"; |
420 |
cn1[ni]=j_cnsize; |
421 |
} // end if ni!=i |
422 |
|
423 |
} else { // 2nd shell neighbour |
424 |
nj++; |
425 |
if (nj==j) { // this is the 2nd shell nb 2B included |
426 |
nbs+=j_res.toUpperCase()+"%"; |
427 |
mymove += "(+"+j_res+":"+j_num+":"+j_sec+"/"+j_cnsize+")"; |
428 |
if ( j_num==ixnum && i==0) { // This is the dropped direct nb, no 2b found in 2ns shell |
429 |
ixcn=j_cnsize; |
430 |
if (VL>=2) System.out.print("(i"+ixnum+":"+ixcn+")"); |
431 |
} |
432 |
cn2[nj] = j_cnsize; |
433 |
} // end if |
434 |
|
435 |
// // only once for building the sidelines |
436 |
if (j==0) { |
437 |
sideline[nj] = "+"+nj+"\t"+j_res+""+j_num+":"+j_sec+"("+j_cnsize+")"; |
438 |
// was here: cn2[nj] = j_cnsize; |
439 |
} // end if sideline |
440 |
} // end if 1st/2nd shell |
441 |
|
442 |
} // end while through the entire nbhood |
443 |
|
444 |
if (!overx) { // in case x is the very last we haven't seen it yet |
445 |
nbs+="x%"; // add it in the end |
446 |
overx=true; |
447 |
} // end if over x |
448 |
if (VL>=1) { |
449 |
System.out.print("("+nbs+")\t"); |
450 |
} |
451 |
nbstring[i][j] = nbs; |
452 |
moveset[i][j] = mymove; |
453 |
if (VL>=2) System.out.print(" "+mymove); |
454 |
precol = nbstring[i][0]; |
455 |
getEntropy( nbs, restype, precol); |
456 |
if (lastRank==0) lastRank = maxRank; |
457 |
rank[i][j] = lastRank; |
458 |
entropy[i][j] = lastEntropy; |
459 |
freq[i][j] = lastFreq; |
460 |
AUC[i][j] = lastAUC; |
461 |
total[i][j]= lastTotal; |
462 |
/*if ( edgeExists( u, v)) { |
463 |
cnsize[i][j]=(double) ((double)cn2[j]-1)/((double)cn1[i]+1); |
464 |
if (VL>=1) System.out.print("+-"+cn2[j]+"/"+cn1[i]+"="+String.format("%.3f",cnsize[i][j])+"\t"); |
465 |
} else { */ |
466 |
cnsize[i][j]=(double) ((double)cn2[j])/((double)cn1[i]); |
467 |
if (VL>=2) System.out.print("= "+cn2[j]+"/"+cn1[i]+"="+String.format("%.3f",cnsize[i][j])+"\t"); |
468 |
// } |
469 |
|
470 |
if (lastRank > 0) { |
471 |
sumdeltarank += ( (lastRank-rank[0][0]) ); |
472 |
lastCorr += ( (lastRank-rank[0][0])*cnsize[i][j] ); |
473 |
} else { |
474 |
sumdeltarank += ( (maxRank-rank[0][0]) ); |
475 |
lastCorr += ( (maxRank-rank[0][0])*cnsize[i][j] ); |
476 |
} // end if lastRank was defined |
477 |
|
478 |
if (VL>=2) System.out.print(""+lastRank+"\t"); |
479 |
} // close inner loop (i) |
480 |
if (VL>=1) { |
481 |
System.out.println("."); |
482 |
} else { |
483 |
System.out.print("."); |
484 |
} |
485 |
} // next outerloop (j) |
486 |
lastxcn=(ixcn*jxcn); |
487 |
if (VL>=1) { |
488 |
System.out.println("lastxcn=(ixcn*jxcn)=("+ixcn+"*"+jxcn+")="+lastxcn); |
489 |
} |
490 |
rsst.close(); |
491 |
stmt.close(); |
492 |
|
493 |
} catch (SQLException e) { |
494 |
e.printStackTrace(); |
495 |
System.err.println("SQLException: " + e.getMessage()); |
496 |
System.err.println("SQLState: " + e.getSQLState()); |
497 |
} // end try/catch |
498 |
// System.out.println("fin."); |
499 |
return sumdeltarank; |
500 |
} // end scoreCurrentNbhood |
501 |
|
502 |
public static boolean edgeExists( int u_num, int v_num) { |
503 |
int c=0; |
504 |
String sql; |
505 |
boolean found = false; |
506 |
Statement st; |
507 |
ResultSet rs; |
508 |
try { |
509 |
sql = "select count(*) from temp_shell where i_num="+u_num+" and j_num="+v_num+";"; |
510 |
st = conn.createStatement(); |
511 |
rs = st.executeQuery(sql); |
512 |
if (rs.next()) c = rs.getInt(1); |
513 |
found = (c>0); |
514 |
rs.close(); |
515 |
st.close(); |
516 |
} catch (SQLException e) { |
517 |
e.printStackTrace(); |
518 |
System.err.println("SQLException: " + e.getMessage()); |
519 |
System.err.println("SQLState: " + e.getSQLState()); |
520 |
} // end try/catch |
521 |
return found; |
522 |
} // end edgeExists( u_num, v_num) |
523 |
|
524 |
public static void reportMatrix( int what2print) { |
525 |
System.out.println("\nPrinting "+what2print); |
526 |
if (what2print==printRank) System.out.print("rank[i][j]" ); |
527 |
if (what2print==printTotal) System.out.print("total[i][j]" ); |
528 |
if (what2print==printEntropy) System.out.print("entropy[i][j]" ); |
529 |
if (what2print==printFreq ) System.out.print("freq[i][j]" ); |
530 |
if (what2print==printAUC) System.out.print("AUC[i][j]" ); |
531 |
if (what2print==printNbstring) System.out.print("nbstring[i][j]" ); |
532 |
if (what2print==printMoveset) System.out.print("moveset[i][j]" ); |
533 |
if (what2print==printdeltaRank) System.out.print("rank[i][j]-rank[0][0]" ); |
534 |
if (what2print==printCNSize) System.out.print("cnsize[i][j]" ); |
535 |
if (what2print==printCNSxdelta) System.out.print("cnsize[i][j]*(rank[i][j]-rank[0][0])" ); |
536 |
|
537 |
System.out.println("..."); |
538 |
// print headerline(s) |
539 |
System.out.println(headline[0]); |
540 |
System.out.println(headline[1]); |
541 |
System.out.println(headline[2]); |
542 |
System.out.println(headline[3]); |
543 |
System.out.println(headline[4]); |
544 |
for (int j=0; j<=size2; j++) { |
545 |
// print sideline |
546 |
System.out.print( sideline[j]+"\t"); |
547 |
for ( int i=0; i<=size1; i++) { |
548 |
if (what2print==printRank) System.out.print( rank[i][j] ); |
549 |
if (what2print==printTotal) System.out.print( total[i][j] ); |
550 |
if (what2print==printEntropy) System.out.print( entropy[i][j] ); |
551 |
if (what2print==printFreq ) System.out.print( freq[i][j] ); |
552 |
if (what2print==printAUC) System.out.print( AUC[i][j] ); |
553 |
if (what2print==printNbstring) System.out.print( nbstring[i][j] ); |
554 |
if (what2print==printMoveset) System.out.print( moveset[i][j] ); |
555 |
if (what2print==printdeltaRank) System.out.print( rank[i][j]-rank[0][0] ); |
556 |
if (what2print==printCNSize) System.out.print( String.format("%.3f",cnsize[i][j]) ); |
557 |
if (what2print==printCNSxdelta) System.out.print( String.format("%.3f", (cnsize[i][j]*(rank[i][j]-rank[0][0]))) ); |
558 |
System.out.print("\t"); |
559 |
} // next i |
560 |
System.out.println(""); |
561 |
} // next j |
562 |
} // end of report |
563 |
|
564 |
public static void getEntropy( String nbs, String centRes, String predec) { |
565 |
String sql, res, this_n, prec_n; |
566 |
Statement stmt; |
567 |
ResultSet rsst; |
568 |
double p, psum=0.0, logp, plogp, plogpsum=0.0; |
569 |
int counter=0, c=0, lastc=0; |
570 |
try { |
571 |
// Hashing first row tables comes first |
572 |
// System.out.println("nbs : "+nbs); |
573 |
// System.out.println("predec: "+predec); |
574 |
this_n = nbs.replace("%",""); |
575 |
prec_n = predec.replace("%",""); |
576 |
// System.out.println("this_n: ["+this_n+"]"); |
577 |
// System.out.println("prec_n: ["+prec_n+"]"); |
578 |
if (prec_n.equals(this_n)) { |
579 |
// System.out.println("have to create db for this "+prec_n); |
580 |
sql = "create table IF NOT EXISTS nbhashtables."+prec_n+" as select res, n, k from single_model_node where n like '"+nbs+"';"; |
581 |
// System.out.println(" >> "+sql); |
582 |
stmt = conn.createStatement(); |
583 |
stmt.executeUpdate( sql); |
584 |
stmt.close(); |
585 |
} // else System.out.println("using preceding db of "+prec_n); |
586 |
|
587 |
// now we can safely derive the estimates from the hashtable |
588 |
sql = "select count(*) from nbhashtables."+prec_n+" where n like '"+nbs+"';"; |
589 |
// System.out.println( sql); |
590 |
stmt = conn.createStatement(); |
591 |
rsst = stmt.executeQuery(sql); |
592 |
if (rsst.next()) lastTotal = rsst.getInt( 1); |
593 |
rsst.close(); |
594 |
stmt.close(); |
595 |
|
596 |
sql = "select res, count(*) as t, count(*)/"+lastTotal+" as p, avg( k), stddev( k) from nbhashtables."+prec_n+" where n like '"+nbs+"' group by res order by p DESC;"; |
597 |
stmt = conn.createStatement(); |
598 |
rsst = stmt.executeQuery(sql); |
599 |
// System.out.println("rank : res : total t : fraction p : log2(p) : -p*log2(p)"); |
600 |
int rank = 0; |
601 |
boolean seenCentRes = false; |
602 |
lastAUC = 0.0; |
603 |
lastRank = 0; |
604 |
lastFreq = 0.0; |
605 |
lastavgk = 0.0; |
606 |
lastdevk = 0.0; |
607 |
while (rsst.next()) { |
608 |
counter++; |
609 |
res = rsst.getString(1); // 1st column -- res |
610 |
c = rsst.getInt( 2); // 2nds column : count/residue |
611 |
p = rsst.getDouble(3); // 3rd: fraction p |
612 |
if (VL>=2) System.out.print(counter+"/"+rank+ " : " + res+" : "+c+ " : " + p); |
613 |
logp = Math.log(p)/Math.log(2.0); // to basis 2 for info in bits |
614 |
// System.out.print(" : " + logp); |
615 |
plogp = -1.0 * p * logp; |
616 |
// System.out.print(" : " + plogp); |
617 |
plogpsum += plogp; |
618 |
psum += p; |
619 |
|
620 |
if ((c == lastc) && (lastc>0)) { // tie |
621 |
if (VL>=2) System.out.print(" <-- TIE!"); |
622 |
lastRank = counter; |
623 |
} // end if |
624 |
|
625 |
if (res.equals(centRes)) { |
626 |
if (VL>=2) System.out.print(" <== " + centRes); |
627 |
seenCentRes = true; |
628 |
lastFreq = p; |
629 |
lastRank = rank; |
630 |
lastavgk = rsst.getDouble(4); |
631 |
lastdevk = rsst.getDouble(5); |
632 |
lastc = c; |
633 |
} |
634 |
if (seenCentRes) lastAUC += p; |
635 |
|
636 |
if (VL>=2) System.out.println(""); |
637 |
} |
638 |
// System.out.println("Sum :"+lastTotal+" : "+psum+" : "+plogpsum); |
639 |
if (VL>=2) System.out.println("=> rank "+lastRank); |
640 |
rsst.close(); |
641 |
stmt.close(); |
642 |
lastEntropy = plogpsum; |
643 |
if (lastRank==0) lastRank = maxRank; |
644 |
} catch (SQLException e) { |
645 |
e.printStackTrace(); |
646 |
System.err.println("SQLException: " + e.getMessage()); |
647 |
System.err.println("SQLState: " + e.getSQLState()); |
648 |
} |
649 |
|
650 |
} // end of getEntropy |
651 |
|
652 |
} // end class |