1 |
package proteinstructure; |
2 |
|
3 |
import java.util.TreeMap; |
4 |
import java.util.HashMap; |
5 |
import java.util.ArrayList; |
6 |
|
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 |
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 |
} |
50 |
|
51 |
private double calculateEntropy(){ |
52 |
double sumplogp=0.0; |
53 |
for (double prob:dist.values()){ |
54 |
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 |
} |
58 |
return (double) (-1)*sumplogp; |
59 |
} |
60 |
|
61 |
} |