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