ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/branches/aglappe-jung/iterateUpperNbs.java
Revision: 419
Committed: Thu Nov 22 14:09:18 2007 UTC (16 years, 10 months ago) by duarte
File size: 12342 byte(s)
Log Message:
Re-branching for JUNG2 development
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) throws SQLException {
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 // remove from here for unperturbed version
65 System.out.println("retrieving the entire 1st and 2nd shell");
66 sql = "select j_num, j_res, j_sstype, min(shell) as shell, count(*) as cn from temp_shell group by j_num;";
67 stmt = conn.createStatement();
68 rsst = stmt.executeQuery(sql);
69 // counting shell2
70 n2=0;
71 while (rsst.next()) {
72 if ( rsst.getInt( 4)==2) { // count 2nd shell entry
73 n2++;
74 if ( rsst.getInt( 1)==resnr) { // this is the central node -> get type and secondary structure
75 restype = rsst.getString( 2).toUpperCase();
76 ressec = rsst.getString( 3).toUpperCase();
77 } // end if central residue
78 } // end if 2nds shell
79 System.out.println(n2+":"+rsst.getInt( 1)+"\t"+rsst.getString( 2)+"\t"+rsst.getString( 3)+"\t"+rsst.getInt( 4)+"\t"+rsst.getInt( 5));
80 } // end while
81 System.out.println("SIZE 1st shell "+n1);
82 System.out.println("SIZE 2nd shell "+n2);
83 System.out.println("GraphID "+graphid+" Central residue is "+restype+":"+resnr+":"+ressec);
84 rsst.close();
85 stmt.close();
86
87 // create matrices accordingly
88
89 // percolate the environment
90 System.out.println("removing a contact from shell1 -> shell2");
91 sql = "select j_num, j_res, j_sstype, shell from temp_shell where i_num="+resnr+" and shell = 1 order by rand() limit 1;";
92 stmt = conn.createStatement();
93 rsst = stmt.executeQuery(sql);
94 if (rsst.next()) {
95 j_num12 = rsst.getInt( 1);
96 System.out.println(rsst.getInt( 4)+" -> 2 : ("+rsst.getString( 2)+":"+j_num12+":"+rsst.getString( 3)+")");
97 }
98 rsst.close();
99 stmt.close();
100 stmt = conn.createStatement();
101 stmt.executeUpdate("delete from temp_shell where j_num="+j_num12+";");
102 stmt.close();
103
104
105 // and move an indirect nbor from shell2 -> shell 1;
106 System.out.println("adding a contact from shell2 -> shell1");
107 sql = "select j_num, j_res, j_sstype, shell from temp_shell where i_num!="+resnr+" and j_num!="+resnr+" and i_num!="+j_num12+" and j_num!="+j_num12+" and shell = 2 order by rand() limit 1;";
108 stmt = conn.createStatement();
109 rsst = stmt.executeQuery(sql);
110 if (rsst.next()) {
111 j_num21 = rsst.getInt( 1);
112 System.out.println(rsst.getInt( 4)+" -> 1 : ("+rsst.getString( 2)+":"+j_num21+":"+rsst.getString( 3)+")");
113 }
114 rsst.close();
115 stmt.close();
116 stmt = conn.createStatement();
117 stmt.executeUpdate("update temp_shell set i_num="+resnr+", i_res='"+restype+"', j_sstype='"+ressec+"', shell=1 where j_num="+j_num21+";");
118 stmt.close();
119 stmt = conn.createStatement();
120 stmt.executeUpdate("delete from temp_shell where shell>1;");
121 stmt.close();
122 n1 = 0;
123 System.out.println("re-building the 2nd shell");
124 sql = "select distinct j_num from temp_shell where shell=1 order by j_num;";
125 stmt = conn.createStatement();
126 rsst = stmt.executeQuery(sql);
127 while (rsst.next()) {
128 n1++;
129 j_num = rsst.getInt(1);
130 System.out.println(n1+":"+j_num);
131 jst = conn.createStatement();
132 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+";";
133 // System.out.println(">"+sql);
134 jst.executeUpdate( sql);
135 jst.close();
136 } // end while
137 rsst.close();
138 stmt.close();
139 // end of remove for unperturbed version
140
141 System.out.println("retrieving the entire 1st and 2nd shell");
142 sql = "select j_num, j_res, j_sstype, min(shell) as shell, count(*) as cn from temp_shell group by j_num order by j_num;";
143 stmt = conn.createStatement();
144 rsst = stmt.executeQuery(sql);
145 // counting shell2
146 n2=0;
147 while (rsst.next()) {
148 if ( rsst.getInt( 4)==2) { // count 2nd shell entry
149 n2++;
150 if ( rsst.getInt( 1)==resnr) { // this is the central node -> get type and secondary structure
151 restype = rsst.getString( 2).toUpperCase();
152 ressec = rsst.getString( 3).toUpperCase();
153 } // end if central residue
154 } // end if 2nds shell
155 System.out.println(n2+":"+rsst.getInt( 1)+"\t"+rsst.getString( 2)+"\t"+rsst.getString( 3)+"\t"+rsst.getInt( 4)+"\t"+rsst.getInt( 5));
156 } // end while
157 System.out.println("SIZE 1st shell "+n1);
158 System.out.println("SIZE 2nd shell "+n2);
159 System.out.println("GraphID "+graphid+" Central residue is "+restype+":"+resnr+":"+ressec);
160
161
162 for (j=0; j<=n2; j++) { // outer loop through all indirect contacts
163 // System.out.print(i+" - ");
164 sumdelta=0;
165 for (i=0; i<=n1; i++) { // inner loop through all direct contacts
166 mymove = "("+i+","+j+")";
167 ni = 0;
168 nj = 0;
169 nbs="%";
170 rsst.beforeFirst();
171 while (rsst.next()) {
172 j_num = rsst.getInt( 1);
173 j_res = rsst.getString(2);
174 j_sec = rsst.getString(3);
175 j_shell = rsst.getInt( 4);
176 j_cnsize = rsst.getInt( 5);
177
178 if (j_shell==1) { // a direct 1st shell neighbour
179 ni++;
180 if (ni!=i) {// if this is NOT the one direct nb 2B dropped
181 nbs+=j_res.toUpperCase()+"%";
182 } else { // this one IS dropped
183 mymove += "(-"+j_res+":"+j_num+":"+j_sec+"/"+j_cnsize+")";
184 } // end if ni!=i
185 } else { // 2nd shell neighbour
186 nj++;
187 if (j_num==resnr) { // the central residue is part if the 2nd shell
188 if (nj!=j) { // drop x if marked for inclusion
189 nbs+="x%";
190 } else {
191 mymove += " no x ...";
192 }
193 } else { // this is not x
194 if (nj==j) { // this is the 2nd shell nb 2B included
195 nbs+=j_res.toUpperCase()+"%";
196 mymove += "(+"+j_res+":"+j_num+":"+j_sec+"/"+j_cnsize+")";
197 } // end if
198 } // end if this is central residue x
199 } // end if 1st/2nd shell
200 } // end while through the entire nbhood
201
202 getEntropy( nbs, restype);
203 if (i==0 && j==0) { // original nbhoodstring without any insertions/deletions
204 orgEntropy = lastEntropy;
205 orgFreq = lastFreq;
206 orgAUC = lastAUC;
207 orgavgk = lastavgk;
208 orgdevk = lastdevk;
209 orgRank = lastRank;
210 orgTotal= lastTotal;
211 } // end if 0/0 for defining org*
212 if (lastRank > 0) {
213 sumdelta += (lastRank-orgRank);
214 } else {
215 sumdelta += (maxRank-orgRank);
216 }
217
218 // 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));
219 //if ((lastRank>0 && lastRank<orgRank) || (i==0 && j==0)) {
220 System.out.print(mymove+"\t"+nbs);
221 printValues();
222 //}
223
224
225 // System.out.println(".");
226 } // close inner loop (i)
227 System.out.println( "Summ "+j+":"+sumdelta);
228 } // next outerloop (j)
229 rsst.close();
230 stmt.close();
231
232 } catch (SQLException e) {
233 e.printStackTrace();
234 System.err.println("SQLException: " + e.getMessage());
235 System.err.println("SQLState: " + e.getSQLState());
236 } // end try/catch
237 System.out.println("fin.");
238 } // end main
239
240 public static void printValues() {
241 System.out.print( "\t"+lastTotal+"("+(lastTotal-orgTotal)+")");
242 System.out.print( "\t"+String.format("%.5f", lastEntropy) +" ("+String.format("%.5f", lastEntropy-orgEntropy)+")");
243 System.out.print( "\t#"+String.format("%2d",lastRank) +" ("+String.format("%2d",(lastRank-orgRank))+")");
244 System.out.print( "\t"+String.format("%.5f", lastFreq) +" ("+String.format("%.5f", (lastFreq-orgFreq))+")");
245 System.out.print( "\t"+String.format("%.5f", lastAUC) +" ("+String.format("%.5f", (lastAUC-orgAUC))+")");
246 System.out.print( "\t"+String.format("%.2f", lastavgk) +" ("+String.format("%.2f", (lastavgk-orgavgk))+")");
247 System.out.print( "\t"+String.format("%.2f", lastdevk) +" ("+String.format("%.2f", (lastdevk-orgdevk))+")");
248 System.out.println("");
249 }
250
251 public static void getEntropy( String nbs, String centRes) {
252 String sql, res;
253 Statement stmt;
254 ResultSet rsst;
255 double p, psum=0.0, logp, plogp, plogpsum=0.0;
256 try {
257 sql = "select count(*) from single_model_node where n like '"+nbs+"';";
258 // System.out.println( sql);
259 stmt = conn.createStatement();
260 rsst = stmt.executeQuery(sql);
261 if (rsst.next()) lastTotal = rsst.getInt( 1);
262 rsst.close();
263 stmt.close();
264
265 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;";
266 stmt = conn.createStatement();
267 rsst = stmt.executeQuery(sql);
268 // System.out.println("rank : res : total t : fraction p : log2(p) : -p*log2(p)");
269 int rank = 0;
270 boolean seenCentRes = false;
271 lastAUC = 0.0;
272 lastRank = 0;
273 lastFreq = 0.0;
274 lastavgk = 0.0;
275 lastdevk = 0.0;
276 while (rsst.next()) {
277 rank ++;
278 res = rsst.getString(1); // 1st column -- res
279 p = rsst.getDouble(3); // 3rd: fraction p
280 // System.out.print(rank+ " : " + res+" : "+num+ " : " + p);
281 logp = Math.log(p)/Math.log(2.0); // to basis 2 for info in bits
282 // System.out.print(" : " + logp);
283 plogp = -1.0 * p * logp;
284 // System.out.print(" : " + plogp);
285 plogpsum += plogp;
286 psum += p;
287
288 if (res.equals(centRes)) {
289 // System.out.print(" <==" + centRes);
290 seenCentRes = true;
291 lastFreq = p;
292 lastRank = rank;
293 lastavgk = rsst.getDouble(4);
294 lastdevk = rsst.getDouble(5);
295 }
296 if (seenCentRes) lastAUC += p;
297 // System.out.println("");
298 }
299 // System.out.println("Sum :"+lastTotal+" : "+psum+" : "+plogpsum);
300 rsst.close();
301 stmt.close();
302 lastEntropy = plogpsum;
303 if (lastRank==0) lastRank = maxRank;
304 } catch (SQLException e) {
305 e.printStackTrace();
306 System.err.println("SQLException: " + e.getMessage());
307 System.err.println("SQLState: " + e.getSQLState());
308 }
309
310 } // end of getEntropy
311
312 } // end class