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