ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/listInfoLoss.java
Revision: 202
Committed: Thu Jun 21 17:18:11 2007 UTC (17 years, 4 months ago) by duarte
File size: 4352 byte(s)
Log Message:
MySQLConnection now throwing SQLException on connect
Many files changed following this: all calling classes now re-throwing or catching the SQLException
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 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) throws SQLException {
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 String sql, res;
72 Statement stmt;
73 ResultSet rsst;
74 double p, psum=0.0, logp, plogp, plogpsum=0.0;
75
76 try {
77 sql = "select count(*) from single_model_node where n like '"+nbs+"';";
78 // System.out.println( sql);
79 stmt = conn.createStatement();
80 rsst = stmt.executeQuery(sql);
81 if (rsst.next()) lastTotal = rsst.getInt( 1);
82 rsst.close();
83 stmt.close();
84
85 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;";
86 stmt = conn.createStatement();
87 rsst = stmt.executeQuery(sql);
88 // System.out.println("rank : res : total t : fraction p : log2(p) : -p*log2(p)");
89 int rank = 0;
90 boolean seenCentRes = false;
91 lastAUC= 0;
92 while (rsst.next()) {
93 rank ++;
94 res = rsst.getString(1); // 1st column -- res
95 //num = rsst.getInt(2); // 2nd column -- num
96 p = rsst.getDouble(3); // 3rd: fraction p
97 // System.out.print(rank+ " : " + res+" : "+num+ " : " + p);
98 logp = Math.log(p)/Math.log(2.0); // to basis 2 for info in bits
99 // System.out.print(" : " + logp);
100 plogp = -1.0 * p * logp;
101 // System.out.print(" : " + plogp);
102 plogpsum += plogp;
103 psum += p;
104
105 if (res.equals(centRes)) {
106 // System.out.print(" <==" + centRes);
107 seenCentRes = true;
108 lastFreq = p;
109 lastRank = rank;
110 }
111 if (seenCentRes) lastAUC += p;
112 // System.out.println("");
113 }
114 // System.out.println("Sum :"+lastTotal+" : "+psum+" : "+plogpsum);
115 rsst.close();
116 stmt.close();
117
118 } catch (SQLException e) {
119 e.printStackTrace();
120 System.err.println("SQLException: " + e.getMessage());
121 System.err.println("SQLState: " + e.getSQLState());
122 }
123 return plogpsum;
124 } // end of getEntropy
125
126 }