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