1 |
import tools.MySQLConnection; |
2 |
|
3 |
import java.sql.SQLException; |
4 |
import java.sql.Statement; |
5 |
import java.sql.ResultSet; |
6 |
|
7 |
public class iterateNbs { |
8 |
|
9 |
/** |
10 |
* |
11 |
* exchange of one neighbor at a time by a common neighbor |
12 |
* @author lappe |
13 |
*/ |
14 |
|
15 |
static String user = "lappe" ; // change user name!! |
16 |
static MySQLConnection conn; |
17 |
static double lastEntropy=0.0, lastFreq, lastAUC, lastavgk, lastdevk; |
18 |
static double orgEntropy=0.0, orgFreq, orgAUC, orgavgk, orgdevk; |
19 |
static int lastRank, lastTotal; |
20 |
static int orgRank, orgTotal; |
21 |
|
22 |
public static void main(String[] args) throws SQLException { |
23 |
|
24 |
if (args.length<2){ |
25 |
System.err.println("The graph_id and residue-nr. needs to be given .... i.e. 9 28"); |
26 |
System.exit(1); |
27 |
} |
28 |
int graphid = Integer.parseInt( args[0]); |
29 |
int resnr = Integer.parseInt( args[1]); |
30 |
int n1=0, n2=0, ni=0, nj=0, j_num=0, j_shell, j_cnsize, i, j; |
31 |
conn = new MySQLConnection("white",user,"nieve","pdb_reps_graph_4_2_a"); |
32 |
String sql, j_res, j_sec, restype="?", ressec="?", nbs_lo, nbs_up; |
33 |
Statement stmt, jst; |
34 |
ResultSet rsst; |
35 |
|
36 |
try { |
37 |
System.out.println("getting direct neighborhood ... "); |
38 |
stmt = conn.createStatement(); |
39 |
stmt.executeUpdate("drop table if exists temp_shell;"); |
40 |
stmt.close(); |
41 |
|
42 |
stmt = conn.createStatement(); |
43 |
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+";"); |
44 |
stmt.close(); |
45 |
|
46 |
System.out.println("building the 2nd shell"); |
47 |
sql = "select j_num, j_res, j_sstype from temp_shell where shell=1;"; |
48 |
stmt = conn.createStatement(); |
49 |
rsst = stmt.executeQuery(sql); |
50 |
while (rsst.next()) { |
51 |
n1++; |
52 |
j_num = rsst.getInt(1); |
53 |
System.out.println(n1+":"+j_num); |
54 |
jst = conn.createStatement(); |
55 |
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+";"; |
56 |
// System.out.println(">"+sql); |
57 |
jst.executeUpdate( sql); |
58 |
jst.close(); |
59 |
} // end while |
60 |
rsst.close(); |
61 |
stmt.close(); |
62 |
|
63 |
System.out.println("retrieving the entire 1st and 2nd shell"); |
64 |
sql = "select j_num, j_res, j_sstype, min(shell) as shell, count(*) as cn from temp_shell group by j_num;"; |
65 |
stmt = conn.createStatement(); |
66 |
rsst = stmt.executeQuery(sql); |
67 |
// counting shell2 |
68 |
n2=0; |
69 |
while (rsst.next()) { |
70 |
if ( rsst.getInt( 4)==2) { // count 2nd shell entry |
71 |
n2++; |
72 |
if ( rsst.getInt( 1)==resnr) { // this is the central node -> get type and secondary structure |
73 |
restype = rsst.getString( 2).toUpperCase(); |
74 |
ressec = rsst.getString( 3).toUpperCase(); |
75 |
} // end if central residue |
76 |
} // end if 2nds shell |
77 |
System.out.println(n2+":"+rsst.getInt( 1)+"\t"+rsst.getString( 2)+"\t"+rsst.getString( 3)+"\t"+rsst.getInt( 4)+"\t"+rsst.getInt( 5)); |
78 |
} // end while |
79 |
System.out.println("SIZE 1st shell "+n1); |
80 |
System.out.println("SIZE 2nd shell "+n2); |
81 |
System.out.println("GraphID "+graphid+" Central residue is "+restype+":"+resnr+":"+ressec); |
82 |
|
83 |
for (i=0; i<=n1; i++) { // 1st loop through all direct contacts |
84 |
// System.out.print(i+" - "); |
85 |
for (j=0; j<=n2; j++) { // 2nd loop through all indirect contacts |
86 |
System.out.print("("+i+","+j+")"); |
87 |
ni = 0; |
88 |
nj = 0; |
89 |
nbs_lo="%"; |
90 |
nbs_up="%"; |
91 |
rsst.beforeFirst(); |
92 |
while (rsst.next()) { |
93 |
j_num = rsst.getInt( 1); |
94 |
j_res = rsst.getString(2); |
95 |
j_sec = rsst.getString(3); |
96 |
j_shell = rsst.getInt( 4); |
97 |
j_cnsize = rsst.getInt( 5); |
98 |
|
99 |
if (j_shell==1) { // a direct 1st shell neighbour |
100 |
ni++; |
101 |
if (ni!=i) {// if this is NOT the one direct nb 2B dropped |
102 |
nbs_lo+=j_res+"%"; |
103 |
nbs_up+=j_res+"%"; |
104 |
} else { // this one IS dropped |
105 |
System.out.print("(-"+j_res+":"+j_num+":"+j_sec+"/"+j_cnsize+")"); |
106 |
} // end if ni!=i |
107 |
} else { // 2nd shell neighbour |
108 |
nj++; |
109 |
if (j_num==resnr) { // the central residue is part if the 2nd shell |
110 |
if (nj!=j) { // drop x if marked for inclusion |
111 |
nbs_lo +="x%"; |
112 |
nbs_up +="x%"; |
113 |
} else { |
114 |
System.out.print(" no x ..."); |
115 |
} |
116 |
} else { // this is not x |
117 |
if (nj==j) { // this is the 2nd shell nb 2B included |
118 |
nbs_lo+=j_res.toLowerCase()+"%"; |
119 |
nbs_up+=j_res.toUpperCase()+"%"; |
120 |
System.out.print("(+"+j_res+":"+j_num+":"+j_sec+"/"+j_cnsize+")"); |
121 |
} // end if |
122 |
} // end if this is central residue x |
123 |
} // end if 1st/2nd shell |
124 |
} // end while through the entire nbhood |
125 |
|
126 |
getEntropy( nbs_lo, restype); |
127 |
if (i==0 && j==0) { // original nbhoodstring without any insertions/deletions |
128 |
orgEntropy = lastEntropy; |
129 |
orgFreq = lastFreq; |
130 |
orgAUC = lastAUC; |
131 |
orgavgk = lastavgk; |
132 |
orgdevk = lastdevk; |
133 |
orgRank = lastRank; |
134 |
orgTotal= lastTotal; |
135 |
} // end if 0/0 for defining org* |
136 |
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)); |
137 |
System.out.print("\tlo "+nbs_lo); |
138 |
printValues(); |
139 |
System.out.print("\tup "+nbs_up); |
140 |
getEntropy(nbs_up, restype); |
141 |
printValues(); |
142 |
|
143 |
// System.out.println("."); |
144 |
} // close for loop 2 (j) |
145 |
} // next loop 1 (i) |
146 |
rsst.close(); |
147 |
stmt.close(); |
148 |
|
149 |
} catch (SQLException e) { |
150 |
e.printStackTrace(); |
151 |
System.err.println("SQLException: " + e.getMessage()); |
152 |
System.err.println("SQLState: " + e.getSQLState()); |
153 |
} // end try/catch |
154 |
System.out.println("fin."); |
155 |
} // end main |
156 |
|
157 |
public static void printValues() { |
158 |
System.out.print( "\t"+lastTotal+"("+(lastTotal-orgTotal)+")"); |
159 |
System.out.print( "\t"+String.format("%.5f", lastEntropy) +" ("+String.format("%.5f", lastEntropy-orgEntropy)+")"); |
160 |
System.out.print( "\t#"+String.format("%2d",lastRank) +" ("+String.format("%2d",(lastRank-orgRank))+")"); |
161 |
System.out.print( "\t"+String.format("%.5f", lastFreq) +" ("+String.format("%.5f", (lastFreq-orgFreq))+")"); |
162 |
System.out.print( "\t"+String.format("%.5f", lastAUC) +" ("+String.format("%.5f", (lastAUC-orgAUC))+")"); |
163 |
System.out.print( "\t"+String.format("%.2f", lastavgk) +" ("+String.format("%.2f", (lastavgk-orgavgk))+")"); |
164 |
System.out.print( "\t"+String.format("%.2f", lastdevk) +" ("+String.format("%.2f", (lastdevk-orgdevk))+")"); |
165 |
System.out.println(""); |
166 |
} |
167 |
|
168 |
public static void getEntropy( String nbs, String centRes) { |
169 |
String sql, res; |
170 |
Statement stmt; |
171 |
ResultSet rsst; |
172 |
double p, psum=0.0, logp, plogp, plogpsum=0.0; |
173 |
try { |
174 |
sql = "select count(*) from single_model_node where n like '"+nbs+"';"; |
175 |
// System.out.println( sql); |
176 |
stmt = conn.createStatement(); |
177 |
rsst = stmt.executeQuery(sql); |
178 |
if (rsst.next()) lastTotal = rsst.getInt( 1); |
179 |
rsst.close(); |
180 |
stmt.close(); |
181 |
|
182 |
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;"; |
183 |
stmt = conn.createStatement(); |
184 |
rsst = stmt.executeQuery(sql); |
185 |
// System.out.println("rank : res : total t : fraction p : log2(p) : -p*log2(p)"); |
186 |
int rank = 0; |
187 |
boolean seenCentRes = false; |
188 |
lastAUC = 0.0; |
189 |
lastRank = 0; |
190 |
lastFreq = 0.0; |
191 |
lastavgk = 0.0; |
192 |
lastdevk = 0.0; |
193 |
while (rsst.next()) { |
194 |
rank ++; |
195 |
res = rsst.getString(1); // 1st column -- res |
196 |
//num = rsst.getInt(2); // 2nd column -- num |
197 |
p = rsst.getDouble(3); // 3rd: fraction p |
198 |
// System.out.print(rank+ " : " + res+" : "+num+ " : " + p); |
199 |
logp = Math.log(p)/Math.log(2.0); // to basis 2 for info in bits |
200 |
// System.out.print(" : " + logp); |
201 |
plogp = -1.0 * p * logp; |
202 |
// System.out.print(" : " + plogp); |
203 |
plogpsum += plogp; |
204 |
psum += p; |
205 |
|
206 |
if (res.equals(centRes)) { |
207 |
// System.out.print(" <==" + centRes); |
208 |
seenCentRes = true; |
209 |
lastFreq = p; |
210 |
lastRank = rank; |
211 |
lastavgk = rsst.getDouble(4); |
212 |
lastdevk = rsst.getDouble(5); |
213 |
} |
214 |
if (seenCentRes) lastAUC += p; |
215 |
// System.out.println(""); |
216 |
} |
217 |
// System.out.println("Sum :"+lastTotal+" : "+psum+" : "+plogpsum); |
218 |
rsst.close(); |
219 |
stmt.close(); |
220 |
lastEntropy = plogpsum; |
221 |
|
222 |
} catch (SQLException e) { |
223 |
e.printStackTrace(); |
224 |
System.err.println("SQLException: " + e.getMessage()); |
225 |
System.err.println("SQLState: " + e.getSQLState()); |
226 |
} |
227 |
|
228 |
} // end of getEntropy |
229 |
|
230 |
} // end class |