-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathenvironment_violin_lot.py
86 lines (69 loc) · 2.14 KB
/
environment_violin_lot.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
"""
import os
import sys
import argparse
import gzip
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
def plot(f, figf, freqmin):
freq = {}
with open(f, 'r') as i:
for l in i:
p=l.strip().split("\t")
if 'human' in p[0]:
p[0]='human'
if 'human' in p[1]:
p[1]='human'
if p[0] < p[1]:
(g1, g2) = (p[0], p[1])
else:
(g2, g1) = (p[0], p[1])
if g1 not in freq:
freq[g1] = {}
if g2 not in freq[g1]:
freq[g1][g2] = []
freq[g1][g2].append(float(p[2]))
labels = []
scores = []
sames = []
count = 1
ticks = []
for g1 in freq.keys():
for g2 in freq[g1].keys():
if len(freq[g1][g2]) < freqmin:
continue
labels.append("{}-{}".format(g1, g2))
scores.append(freq[g1][g2])
if g1 == g2:
sames.append(1)
else:
sames.append(0)
ticks.append(count)
count += 1
fig = plt.figure()
ax = fig.add_subplot(111)
# ax.boxplot(alldata)
vp = ax.violinplot(scores, ticks, showmeans=True)
for i, j in enumerate(vp['bodies']):
if sames[i]:
j.set_color('red')
ax.set_xlabel("Environments")
ax.set_ylabel("Genome Distances")
ax.set_xticks(ticks)
ax.set_xticklabels(labels, rotation='vertical')
ax.get_xaxis().tick_bottom()
ax.get_yaxis().tick_left()
fig.set_facecolor('white')
plt.tight_layout()
#plt.show()
fig.savefig(figf)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Draw a voilin plot of same and different environments")
parser.add_argument('-f', help='file of environments and distances', required=True)
parser.add_argument('-o', help='output image file', default='out.png')
parser.add_argument('-m', help='Minimum frequency (default=100000)', default=100000)
parser.add_argument('-v', help='verbose output')
args = parser.parse_args()
plot(args.f, args.o, int(args.m))