1 |
package proteinstructure; |
2 |
|
3 |
import java.util.TreeMap; |
4 |
import java.util.ArrayList; |
5 |
|
6 |
|
7 |
public class NbhProbDistribution { |
8 |
|
9 |
final static int MAXRANK = 21; |
10 |
|
11 |
double entropy; |
12 |
TreeMap<String,Double> dist; |
13 |
TreeMap<String,Integer> ranks; |
14 |
|
15 |
public NbhProbDistribution(TreeMap<String,Double> dist) { |
16 |
this.dist=dist; |
17 |
this.entropy=calculateEntropy(); |
18 |
getRanks(); //initialises ranks TreeMap |
19 |
} |
20 |
|
21 |
public double getProb(String res){ |
22 |
return dist.get(res); |
23 |
} |
24 |
|
25 |
public int getRank(String res) { |
26 |
return ranks.get(res); |
27 |
} |
28 |
|
29 |
public double getEntropy(){ |
30 |
return entropy; |
31 |
} |
32 |
|
33 |
public ArrayList<String> getResiduesSortedByRank(){ |
34 |
ArrayList<String> sortedResidues = new ArrayList<String>(); |
35 |
for (int i=1;i<=21;i++){ |
36 |
for (String res:ranks.keySet()){ |
37 |
if (ranks.get(res)<=i && !sortedResidues.contains(res)){ |
38 |
sortedResidues.add(res); |
39 |
} |
40 |
} |
41 |
} |
42 |
return sortedResidues; |
43 |
} |
44 |
|
45 |
private void getRanks(){ |
46 |
ranks = new TreeMap<String, Integer>(); |
47 |
// first we set the residues with prob=0.0 to MAXRANK |
48 |
for (String res:dist.keySet()){ |
49 |
if (!ranks.containsKey(res)) { // we don't check the ones already assigned |
50 |
double prob = dist.get(res); |
51 |
if (prob==0.0){ |
52 |
ranks.put(res,MAXRANK); |
53 |
} |
54 |
} |
55 |
} |
56 |
// now we set ranks for the rest |
57 |
int lastRank = 0; |
58 |
double lastMax = 0.0; |
59 |
int numberNonZeroProb = dist.size()-ranks.size(); |
60 |
for (int rank=1;rank<=numberNonZeroProb;rank++){ |
61 |
double max = 0.0; |
62 |
String maxres=""; |
63 |
for (String res:dist.keySet()){ |
64 |
if (!ranks.containsKey(res)) { |
65 |
double prob = dist.get(res); |
66 |
if (prob>=max){ |
67 |
max = prob; |
68 |
maxres = res; |
69 |
} |
70 |
} |
71 |
} |
72 |
if (max==lastMax){ |
73 |
ranks.put(maxres, lastRank); |
74 |
} else { |
75 |
ranks.put(maxres,rank); |
76 |
lastRank = rank; |
77 |
} |
78 |
lastMax = max; |
79 |
} |
80 |
} |
81 |
|
82 |
private double calculateEntropy(){ |
83 |
double sumplogp=0.0; |
84 |
for (double prob:dist.values()){ |
85 |
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) |
86 |
sumplogp += prob*(Math.log(prob)/Math.log(2)); |
87 |
} |
88 |
} |
89 |
return (double) (-1)*sumplogp; |
90 |
} |
91 |
|
92 |
} |