-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathplot_pairwise_percents.py
54 lines (37 loc) · 1.22 KB
/
plot_pairwise_percents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
"""
Plot the pairwise percents.
The perl code perl average_pairwise.pl generates a JSON data structure with the percent IDs from the same organisms at the different levels
This plots that
"""
import json
import matplotlib.pyplot as plt
def median(lst):
sortedLst = sorted(lst)
lstLen = len(lst)
index = (lstLen - 1) // 2 # // is the floor division
if (lstLen % 2):
return sortedLst[index]
else:
return (sortedLst[index] + sortedLst[index + 1]) / 2.0
filename = '/home/redwards/Desktop/all_pairwise.json'
with open(filename, 'r') as f:
data = json.load(f)
tax = ['kingdom', 'phylum', 'class', 'order', 'family', 'genus', 'species', 'strain']
alldata = []
for t in tax:
# data[t] = map(float, data[t])
floatdata = map(float, data[t])
alldata.append(floatdata)
print("{}\t{}\t{}\t{}".format(t, len(floatdata), 1.0*sum(floatdata)/len(floatdata), median(floatdata)))
fig = plt.figure()
ax = fig.add_subplot(111)
#ax.boxplot(alldata)
tax.insert(0, "")
ax.violinplot(alldata, showmeans=True)
ax.set_xlabel("Phylogeny")
ax.set_ylabel("Average percent identity")
ax.set_xticklabels(tax)
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
fig.set_facecolor('white')
plt.show()