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