1 |
lappe |
160 |
import tools.MySQLConnection; |
2 |
|
|
|
3 |
|
|
import java.sql.SQLException; |
4 |
|
|
import java.sql.Statement; |
5 |
|
|
import java.sql.ResultSet; |
6 |
|
|
|
7 |
|
|
public class listInfoLoss { |
8 |
|
|
|
9 |
|
|
/** |
10 |
|
|
* info loss / entropy calculations given a nbhoodstring |
11 |
|
|
* dropping one neighbor at a time |
12 |
|
|
* @author lappe |
13 |
|
|
*/ |
14 |
|
|
|
15 |
|
|
static String user = "lappe" ; // change user name!! |
16 |
|
|
static MySQLConnection conn; |
17 |
|
|
static double orgFreq, lastFreq, orgAUC, lastAUC; |
18 |
|
|
static int orgRank, lastRank, orgTotal, lastTotal; |
19 |
|
|
|
20 |
|
|
public static void main(String[] args) { |
21 |
|
|
double entropy = 0.0, newentropy=0.0, gain; |
22 |
|
|
String nbhood = "", front, middle, tail, newhood="", central ="I"; |
23 |
|
|
if (args.length<2){ |
24 |
|
|
System.err.println("The starting neighborhood-string and central residue type needs to be given .... i.e. %K%D%L%I%x%D%C% I"); |
25 |
|
|
System.exit(1); |
26 |
|
|
} |
27 |
|
|
nbhood = args[0]; |
28 |
|
|
central = args[1]; |
29 |
|
|
|
30 |
|
|
conn = new MySQLConnection("white",user,"nieve","pdb_reps_graph_4_2_a"); |
31 |
|
|
int l = (int)((nbhood.length()-1)/2); |
32 |
|
|
|
33 |
|
|
System.out.println("ListInfoLoss"); |
34 |
|
|
System.out.print("0 - ("+nbhood+")("+l+") "); |
35 |
|
|
entropy = getEntropy( nbhood, central); |
36 |
|
|
|
37 |
|
|
// System.out.println("Symbols in nbhood :"+l); |
38 |
|
|
orgFreq = lastFreq; |
39 |
|
|
orgAUC = lastAUC; |
40 |
|
|
orgRank = lastRank; |
41 |
|
|
orgTotal = lastTotal; |
42 |
|
|
System.out.println("central "+central+". \tt="+orgTotal+" \tentropy = "+String.format("%.5f", entropy)+" \trank#"+lastRank+" \tp("+central+") = "+String.format("%.5f",lastFreq)+" \t\tAUC = "+String.format("%.5f",lastAUC)); |
43 |
|
|
// System.out.println("Symbols in nbhood :"+l); |
44 |
|
|
|
45 |
|
|
for (int i = 0; i<l; i++) { |
46 |
|
|
front = nbhood.substring(0,i*2); |
47 |
|
|
middle = nbhood.substring(i*2, i*2+2); |
48 |
|
|
tail = nbhood.substring(i*2+2); |
49 |
|
|
System.out.print((i+1)+" - "+front+"("+middle+")"+tail); |
50 |
|
|
|
51 |
|
|
newhood = front+tail; |
52 |
|
|
|
53 |
|
|
System.out.print( " -> " + newhood); |
54 |
|
|
newentropy = getEntropy( newhood, central); |
55 |
|
|
gain = newentropy-entropy; |
56 |
|
|
System.out.print( "\t"+lastTotal+"("+(lastTotal-orgTotal)+")"); |
57 |
|
|
System.out.print( "\t"+String.format("%.5f", newentropy) +" ("+String.format("%.5f", gain)+")"); |
58 |
|
|
System.out.print( "\t#"+lastRank +" ("+(lastRank-orgRank)+")"); |
59 |
|
|
System.out.print( "\t"+String.format("%.5f", lastFreq) +" ("+String.format("%.5f", (lastFreq-orgFreq))+")"); |
60 |
|
|
System.out.print( "\t"+String.format("%.5f", lastAUC) +" ("+String.format("%.5f", (lastAUC-orgAUC))+")"); |
61 |
|
|
|
62 |
|
|
System.out.println( "."); |
63 |
|
|
} // next symbol in nbhood; |
64 |
|
|
|
65 |
|
|
System.out.println("fin."); |
66 |
|
|
} |
67 |
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
|
|
public static double getEntropy( String nbs, String centRes) { |
71 |
|
|
int num; |
72 |
|
|
String sql, res; |
73 |
|
|
Statement stmt; |
74 |
|
|
ResultSet rsst; |
75 |
|
|
double p, psum=0.0, logp, plogp, plogpsum=0.0; |
76 |
|
|
|
77 |
|
|
try { |
78 |
|
|
sql = "select count(*) from single_model_node where n like '"+nbs+"';"; |
79 |
|
|
// System.out.println( sql); |
80 |
|
|
stmt = conn.createStatement(); |
81 |
|
|
rsst = stmt.executeQuery(sql); |
82 |
|
|
if (rsst.next()) lastTotal = rsst.getInt( 1); |
83 |
|
|
rsst.close(); |
84 |
|
|
stmt.close(); |
85 |
|
|
|
86 |
|
|
sql = "select res, count(*) as t, count(*)/"+lastTotal+" as p from single_model_node where n like '"+nbs+"' group by res order by p DESC;"; |
87 |
|
|
stmt = conn.createStatement(); |
88 |
|
|
rsst = stmt.executeQuery(sql); |
89 |
|
|
// System.out.println("rank : res : total t : fraction p : log2(p) : -p*log2(p)"); |
90 |
|
|
int rank = 0; |
91 |
|
|
boolean seenCentRes = false; |
92 |
|
|
lastAUC= 0; |
93 |
|
|
while (rsst.next()) { |
94 |
|
|
rank ++; |
95 |
|
|
res = rsst.getString(1); // 1st column -- res |
96 |
|
|
num = rsst.getInt(2); // 2nd column -- num |
97 |
|
|
p = rsst.getDouble(3); // 3rd: fraction p |
98 |
|
|
// System.out.print(rank+ " : " + res+" : "+num+ " : " + p); |
99 |
|
|
logp = Math.log(p)/Math.log(2.0); // to basis 2 for info in bits |
100 |
|
|
// System.out.print(" : " + logp); |
101 |
|
|
plogp = -1.0 * p * logp; |
102 |
|
|
// System.out.print(" : " + plogp); |
103 |
|
|
plogpsum += plogp; |
104 |
|
|
psum += p; |
105 |
|
|
|
106 |
|
|
if (res.equals(centRes)) { |
107 |
|
|
// System.out.print(" <==" + centRes); |
108 |
|
|
seenCentRes = true; |
109 |
|
|
lastFreq = p; |
110 |
|
|
lastRank = rank; |
111 |
|
|
} |
112 |
|
|
if (seenCentRes) lastAUC += p; |
113 |
|
|
// System.out.println(""); |
114 |
|
|
} |
115 |
|
|
// System.out.println("Sum :"+lastTotal+" : "+psum+" : "+plogpsum); |
116 |
|
|
rsst.close(); |
117 |
|
|
stmt.close(); |
118 |
|
|
|
119 |
|
|
} catch (SQLException e) { |
120 |
|
|
e.printStackTrace(); |
121 |
|
|
System.err.println("SQLException: " + e.getMessage()); |
122 |
|
|
System.err.println("SQLState: " + e.getSQLState()); |
123 |
|
|
} |
124 |
|
|
return plogpsum; |
125 |
|
|
} // end of getEntropy |
126 |
|
|
|
127 |
|
|
} |