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