ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/owl/trunk/proteinstructure/NbhProbDistribution.java
Revision: 199
Committed: Mon Jun 18 09:03:39 2007 UTC (17 years, 3 months ago) by duarte
File size: 1367 byte(s)
Log Message:
Implemented getRanks()
Line User Rev File contents
1 duarte 181 package proteinstructure;
2    
3     import java.util.TreeMap;
4     import java.util.HashMap;
5 duarte 199 import java.util.ArrayList;
6 duarte 181
7     public class NbhProbDistribution {
8    
9     double entropy;
10     HashMap<String,Double> dist;
11     TreeMap<String,Integer> ranks;
12    
13     public NbhProbDistribution(HashMap<String,Double> dist) {
14     this.dist=dist;
15     this.entropy=calculateEntropy();
16     getRanks(); //initialises ranks TreeMap
17     }
18    
19     public double getProb(String res){
20     return dist.get(res);
21     }
22    
23     public int getRank(String res) {
24     return ranks.get(res);
25     }
26    
27     public double getEntropy(){
28     return entropy;
29     }
30    
31     private void getRanks(){
32 duarte 199 ranks = new TreeMap<String, Integer>();
33     ArrayList<String> doneRes = new ArrayList<String>();
34     for (int rank=1;rank<=20;rank++){
35     double max = 0.0;
36     String maxres="";
37     for (String res:dist.keySet()){
38     if (!doneRes.contains(res)) {
39     double prob = dist.get(res);
40     if (prob>=max){
41     max = prob;
42     maxres = res;
43     }
44     }
45     }
46     ranks.put(maxres,rank);
47     doneRes.add(maxres);
48     }
49 duarte 181 }
50    
51     private double calculateEntropy(){
52     double sumplogp=0.0;
53     for (double prob:dist.values()){
54 duarte 183 if (prob!=0){ // plogp is defined to be 0 when p=0 (because of limit). If we let java calculate it, it gives NaN (-infinite) because it tries to compute log(0)
55     sumplogp += prob*(Math.log(prob)/Math.log(2));
56     }
57 duarte 181 }
58 duarte 183 return (double) (-1)*sumplogp;
59 duarte 181 }
60    
61     }