ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/iterateUpperNbs.java
Revision: 180
Committed: Fri Jun 1 15:17:40 2007 UTC (17 years, 4 months ago) by lappe
File size: 10275 byte(s)
Log Message:
Line File contents
1 import tools.MySQLConnection;
2
3 import java.sql.SQLException;
4 import java.sql.Statement;
5 import java.sql.ResultSet;
6
7 public class iterateUpperNbs {
8
9 /**
10 *
11 * exchange of one neighbor at a time by a common neighbor
12 * @author lappe
13 */
14 static int maxRank = 31; // value to replace for non-existence of central redue in the resultvector (rank=0)
15 // higher values should penalize non-existence more
16 static String user = "lappe" ; // change user name!!
17 static MySQLConnection conn;
18 static double lastEntropy=0.0, lastFreq, lastAUC, lastavgk, lastdevk;
19 static double orgEntropy=0.0, orgFreq, orgAUC, orgavgk, orgdevk;
20 static int lastRank, lastTotal;
21 static int orgRank, orgTotal;
22
23 public static void main(String[] args) {
24
25 if (args.length<2){
26 System.err.println("The graph_id and residue-nr. needs to be given .... i.e. 9 28");
27 System.exit(1);
28 }
29 int graphid = Integer.parseInt( args[0]);
30 int resnr = Integer.parseInt( args[1]);
31 int n1=0, n2=0, ni=0, nj=0, j_num=0, j_shell, j_cnsize, i, j, sumdelta, j_num12=0, j_num21=0;
32 conn = new MySQLConnection("white",user,"nieve","pdb_reps_graph_4_2"); // the UPPERCASE DB!
33 String sql, j_res, j_sec, restype="?", ressec="?", nbs, mymove;
34 Statement stmt, jst;
35 ResultSet rsst;
36
37 try {
38 System.out.println("getting direct neighborhood ... ");
39 stmt = conn.createStatement();
40 stmt.executeUpdate("drop table if exists temp_shell;");
41 stmt.close();
42
43 stmt = conn.createStatement();
44 stmt.executeUpdate("create table temp_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+";");
45 stmt.close();
46
47 System.out.println("building the 2nd shell");
48 sql = "select j_num, j_res, j_sstype from temp_shell where shell=1;";
49 stmt = conn.createStatement();
50 rsst = stmt.executeQuery(sql);
51 while (rsst.next()) {
52 n1++;
53 j_num = rsst.getInt(1);
54 System.out.println(n1+":"+j_num);
55 jst = conn.createStatement();
56 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+";";
57 // System.out.println(">"+sql);
58 jst.executeUpdate( sql);
59 jst.close();
60 } // end while
61 rsst.close();
62 stmt.close();
63
64 // percolate the environment
65 System.out.println("moving a contact from shell1 -> shell2");
66 sql = "select j_num, j_res, j_sstype, shell from temp_shell where i_num="+resnr+" and shell = 1 order by rand() limit 1;";
67 stmt = conn.createStatement();
68 rsst = stmt.executeQuery(sql);
69 if (rsst.next()) {
70 j_num12 = rsst.getInt( 1);
71 System.out.println(rsst.getInt( 4)+" -> 2 : ("+rsst.getString( 2)+":"+j_num12+":"+rsst.getString( 3)+")");
72 }
73 rsst.close();
74 stmt.close();
75 stmt = conn.createStatement();
76 stmt.executeUpdate("update temp_shell set shell=2 where j_num="+j_num12+";");
77 stmt.close();
78
79
80 // and move an indirect nbor from shell2 -> shell 1;
81 System.out.println("moving a contact from shell2 -> shell1");
82 sql = "select j_num, j_res, j_sstype, shell from temp_shell where i_num!="+resnr+" and j_num!="+resnr+" and shell = 2 order by rand() limit 1;";
83 stmt = conn.createStatement();
84 rsst = stmt.executeQuery(sql);
85 if (rsst.next()) {
86 j_num21 = rsst.getInt( 1);
87 System.out.println(rsst.getInt( 4)+" -> 1 : ("+rsst.getString( 2)+":"+j_num21+":"+rsst.getString( 3)+")");
88 }
89 rsst.close();
90 stmt.close();
91 stmt = conn.createStatement();
92 stmt.executeUpdate("update temp_shell set shell=1 where j_num="+j_num21+";");
93 stmt.close();
94
95 System.out.println("retrieving the entire 1st and 2nd shell");
96 sql = "select j_num, j_res, j_sstype, min(shell) as shell, count(*) as cn from temp_shell group by j_num;";
97 stmt = conn.createStatement();
98 rsst = stmt.executeQuery(sql);
99 // counting shell2
100 n2=0;
101 while (rsst.next()) {
102 if ( rsst.getInt( 4)==2) { // count 2nd shell entry
103 n2++;
104 if ( rsst.getInt( 1)==resnr) { // this is the central node -> get type and secondary structure
105 restype = rsst.getString( 2).toUpperCase();
106 ressec = rsst.getString( 3).toUpperCase();
107 } // end if central residue
108 } // end if 2nds shell
109 System.out.println(n2+":"+rsst.getInt( 1)+"\t"+rsst.getString( 2)+"\t"+rsst.getString( 3)+"\t"+rsst.getInt( 4)+"\t"+rsst.getInt( 5));
110 } // end while
111 System.out.println("SIZE 1st shell "+n1);
112 System.out.println("SIZE 2nd shell "+n2);
113 System.out.println("GraphID "+graphid+" Central residue is "+restype+":"+resnr+":"+ressec);
114
115 for (j=0; j<=n2; j++) { // outer loop through all indirect contacts
116 // System.out.print(i+" - ");
117 sumdelta=0;
118 for (i=0; i<=n1; i++) { // inner loop through all direct contacts
119 mymove = "("+i+","+j+")";
120 ni = 0;
121 nj = 0;
122 nbs="%";
123 rsst.beforeFirst();
124 while (rsst.next()) {
125 j_num = rsst.getInt( 1);
126 j_res = rsst.getString(2);
127 j_sec = rsst.getString(3);
128 j_shell = rsst.getInt( 4);
129 j_cnsize = rsst.getInt( 5);
130
131 if (j_shell==1) { // a direct 1st shell neighbour
132 ni++;
133 if (ni!=i) {// if this is NOT the one direct nb 2B dropped
134 nbs+=j_res.toUpperCase()+"%";
135 } else { // this one IS dropped
136 mymove += "(-"+j_res+":"+j_num+":"+j_sec+"/"+j_cnsize+")";
137 } // end if ni!=i
138 } else { // 2nd shell neighbour
139 nj++;
140 if (j_num==resnr) { // the central residue is part if the 2nd shell
141 if (nj!=j) { // drop x if marked for inclusion
142 nbs+="x%";
143 } else {
144 mymove += " no x ...";
145 }
146 } else { // this is not x
147 if (nj==j) { // this is the 2nd shell nb 2B included
148 nbs+=j_res.toUpperCase()+"%";
149 mymove += "(+"+j_res+":"+j_num+":"+j_sec+"/"+j_cnsize+")";
150 } // end if
151 } // end if this is central residue x
152 } // end if 1st/2nd shell
153 } // end while through the entire nbhood
154
155 getEntropy( nbs, restype);
156 if (i==0 && j==0) { // original nbhoodstring without any insertions/deletions
157 orgEntropy = lastEntropy;
158 orgFreq = lastFreq;
159 orgAUC = lastAUC;
160 orgavgk = lastavgk;
161 orgdevk = lastdevk;
162 orgRank = lastRank;
163 orgTotal= lastTotal;
164 } // end if 0/0 for defining org*
165 if (lastRank > 0) {
166 sumdelta += (lastRank-orgRank);
167 } else {
168 sumdelta += (maxRank-orgRank);
169 }
170
171 // System.out.println(" t="+orgTotal+" \tentropy = "+String.format("%.5f", orgEntropy)+" \trank#"+String.format("%2d",orgRank)+" \tp("+restype+") = "+String.format("%.5f",orgFreq)+" \t\tAUC = "+String.format("%.5f",orgAUC)+" \t\tavg(k)="+String.format("%.2f",orgavgk)+"\tstddev="+String.format("%.2f",orgdevk));
172 //if ((lastRank>0 && lastRank<orgRank) || (i==0 && j==0)) {
173 System.out.print(mymove+"\t"+nbs);
174 printValues();
175 //}
176
177
178 // System.out.println(".");
179 } // close inner loop (i)
180 System.out.println( "Summ "+j+":"+sumdelta);
181 } // next outerloop (j)
182 rsst.close();
183 stmt.close();
184
185 } catch (SQLException e) {
186 e.printStackTrace();
187 System.err.println("SQLException: " + e.getMessage());
188 System.err.println("SQLState: " + e.getSQLState());
189 } // end try/catch
190 System.out.println("fin.");
191 } // end main
192
193 public static void printValues() {
194 System.out.print( "\t"+lastTotal+"("+(lastTotal-orgTotal)+")");
195 System.out.print( "\t"+String.format("%.5f", lastEntropy) +" ("+String.format("%.5f", lastEntropy-orgEntropy)+")");
196 System.out.print( "\t#"+String.format("%2d",lastRank) +" ("+String.format("%2d",(lastRank-orgRank))+")");
197 System.out.print( "\t"+String.format("%.5f", lastFreq) +" ("+String.format("%.5f", (lastFreq-orgFreq))+")");
198 System.out.print( "\t"+String.format("%.5f", lastAUC) +" ("+String.format("%.5f", (lastAUC-orgAUC))+")");
199 System.out.print( "\t"+String.format("%.2f", lastavgk) +" ("+String.format("%.2f", (lastavgk-orgavgk))+")");
200 System.out.print( "\t"+String.format("%.2f", lastdevk) +" ("+String.format("%.2f", (lastdevk-orgdevk))+")");
201 System.out.println("");
202 }
203
204 public static void getEntropy( String nbs, String centRes) {
205 String sql, res;
206 Statement stmt;
207 ResultSet rsst;
208 double p, psum=0.0, logp, plogp, plogpsum=0.0;
209 try {
210 sql = "select count(*) from single_model_node where n like '"+nbs+"';";
211 // System.out.println( sql);
212 stmt = conn.createStatement();
213 rsst = stmt.executeQuery(sql);
214 if (rsst.next()) lastTotal = rsst.getInt( 1);
215 rsst.close();
216 stmt.close();
217
218 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;";
219 stmt = conn.createStatement();
220 rsst = stmt.executeQuery(sql);
221 // System.out.println("rank : res : total t : fraction p : log2(p) : -p*log2(p)");
222 int rank = 0;
223 boolean seenCentRes = false;
224 lastAUC = 0.0;
225 lastRank = 0;
226 lastFreq = 0.0;
227 lastavgk = 0.0;
228 lastdevk = 0.0;
229 while (rsst.next()) {
230 rank ++;
231 res = rsst.getString(1); // 1st column -- res
232 p = rsst.getDouble(3); // 3rd: fraction p
233 // System.out.print(rank+ " : " + res+" : "+num+ " : " + p);
234 logp = Math.log(p)/Math.log(2.0); // to basis 2 for info in bits
235 // System.out.print(" : " + logp);
236 plogp = -1.0 * p * logp;
237 // System.out.print(" : " + plogp);
238 plogpsum += plogp;
239 psum += p;
240
241 if (res.equals(centRes)) {
242 // System.out.print(" <==" + centRes);
243 seenCentRes = true;
244 lastFreq = p;
245 lastRank = rank;
246 lastavgk = rsst.getDouble(4);
247 lastdevk = rsst.getDouble(5);
248 }
249 if (seenCentRes) lastAUC += p;
250 // System.out.println("");
251 }
252 // System.out.println("Sum :"+lastTotal+" : "+psum+" : "+plogpsum);
253 rsst.close();
254 stmt.close();
255 lastEntropy = plogpsum;
256 if (lastRank==0) lastRank = maxRank;
257 } catch (SQLException e) {
258 e.printStackTrace();
259 System.err.println("SQLException: " + e.getMessage());
260 System.err.println("SQLState: " + e.getSQLState());
261 }
262
263 } // end of getEntropy
264
265 } // end class