ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/listInfoGain.java
Revision: 151
Committed: Wed May 16 14:05:48 2007 UTC (17 years, 4 months ago) by lappe
File size: 3346 byte(s)
Log Message:
working version for each single nbring residue 
Line User Rev File contents
1 lappe 148 import tools.MySQLConnection;
2    
3     import java.sql.SQLException;
4     import java.sql.Statement;
5     import java.sql.ResultSet;
6    
7     public class listInfoGain {
8    
9     /**
10     * "Hello World" for entropy calculations given a nbhoodstring
11     *
12     * @author lappe
13     */
14    
15     static String user = "lappe" ; // change user name!!
16     static MySQLConnection conn;
17    
18     public static void main(String[] args) {
19     double entropy = 0.0, newentropy=0.0, gain, mingain=99999999.9;
20     String nbhood = "", front, middle, tail, newhood="";
21     if (args.length<1){
22     System.err.println("The starting neighborhood-string needs to be given .... i.e. %K%D%L%I%x%D%C%");
23     System.exit(1);
24     }
25     nbhood = args[0];
26    
27     conn = new MySQLConnection("white",user,"nieve","pdb_reps_graph_4_2");
28     int l = (int)((nbhood.length()-1)/2);
29     int N = 1;
30     System.out.println("ListInfoGain");
31 lappe 151 System.out.print("0 - (%x%)("+l+") ");
32     entropy = getEntropy( "%x%");
33 lappe 148 System.out.println( entropy + " bits.");
34     System.out.println("Symbols in nbhood :"+l);
35    
36     for (int i = 0; i<l; i++) {
37     front = nbhood.substring(0,i*2);
38     middle = nbhood.substring(i*2, i*2+2);
39     tail = nbhood.substring(i*2+2);
40     System.out.print((i+1)+" - "+front+"("+middle+")"+tail);
41    
42     if (middle.equals("%x")) { // switch from N to C of X
43     N = -1;
44 lappe 151 newhood = "%x%";
45 lappe 148 } else {
46     if (N < 0) // we are in the C-terminal section
47     newhood = "%x"+middle+"%";
48     else // N terminal (before X)
49     newhood = middle+"%x%";
50     } // end if
51 lappe 151 System.out.print( " -> " + newhood);
52 lappe 148 newentropy = getEntropy( newhood);
53     gain = newentropy-entropy;
54     System.out.println( " : "+newentropy +"bits, gain="+gain);
55    
56     } // next symbol in nbhood;
57    
58    
59    
60     System.out.println("fin.");
61     }
62    
63    
64    
65     public static double getEntropy( String nbs) {
66     int total = 0, num;
67     String sql, res;
68     Statement stmt;
69     ResultSet rsst;
70     double p, psum=0.0, logp, plogp, plogpsum=0.0;
71    
72     try {
73     sql = "select count(*) from single_model_node where n like '"+nbs+"';";
74     // System.out.println( sql);
75     stmt = conn.createStatement();
76     rsst = stmt.executeQuery(sql);
77     if (rsst.next()) total = rsst.getInt( 1);
78     rsst.close();
79     stmt.close();
80    
81     sql = "select res, count(*) as t, count(*)/"+total+" as p from single_model_node where n like '"+nbs+"' group by res order by res;";
82     stmt = conn.createStatement();
83     rsst = stmt.executeQuery(sql);
84     // System.out.println("res : total t : fraction p : log2(p) : -p*log2(p)");
85     while (rsst.next()) {
86     res = rsst.getString(1); // 1st column -- res
87     num = rsst.getInt(2); // 2nd column -- num
88     p = rsst.getDouble(3); // 3rd: fraction p
89     // System.out.print(res+" : "+num+ " : " + p);
90     logp = Math.log(p)/Math.log(2.0); // to basis 2 for info in bits
91     // System.out.print(" : " + logp);
92     plogp = -1.0 * p * logp;
93     // System.out.print(" : " + plogp);
94     plogpsum += plogp;
95     psum += p;
96     // System.out.println("");
97     }
98     // System.out.println("Sum :"+total+" : "+psum+" : "+plogpsum);
99     rsst.close();
100     stmt.close();
101    
102     } catch (SQLException e) {
103     e.printStackTrace();
104     System.err.println("SQLException: " + e.getMessage());
105     System.err.println("SQLState: " + e.getSQLState());
106     }
107     return plogpsum;
108     } // end of getEntropy
109    
110     }