-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsuperSFS.py
242 lines (224 loc) · 8.73 KB
/
superSFS.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 4 17:12:08 2021
@author: Chauvet
"""
import pandas as pd
import os
import sys
import matplotlib.pyplot as plt
def for_sfs(outg,thr,i,o):
with open(i) as f:
for l in f:
if '##' in l:
with open(o,'a') as f1:
f1.write(l)
continue
elif '#CHROM' in l:
l_o=l.split()
# print(l)
og_list=[]
with open(outg) as f2:
for og in f2:
og=og.strip()
if len(og)<2:
print('wrong out group data')
else:
og_list+=[l_o.index(og)]
with open(o,'a') as f1:
f1.write(l)
# print(l[9:-6])
else:
l_o=l.split()
om=''
for n in og_list:
om+=l_o[n]
c_a=om.count('1')
if c_a>int(thr):
l_n=l_o[:3]+[l_o[4]]+[l_o[3]]+l_o[5:9]
l_t=l_o[9:]
for gt in l_t:
if gt[0]=='0':
gt_n='1'+'|'
else:
gt_n='0'+'|'
if gt[2]=='0':
gt_n+='1'
else:
gt_n+='0'
l_n+=[gt_n]
l_n='\t'.join(l_n)+'\n'
# print(l_n)
else:
l_n=l
with open(o,'a') as f1:
f1.write(l_n)
# print(og_list)
def get_anno(a):
pf=pd.read_csv(a,sep='\t')
g_l={}
for index in pf.index:
a=pf.iloc[index].values
if a[1] not in g_l.keys():
g_l[a[1]]=[a[0]]
else:
g_l[a[1]]=g_l[a[1]]+[a[0]]
# print(g_l['Wine'])
return g_l
# print(g_l)
def get_index(i,anno):
with open(i) as f:
g_i={}
for l in f:
if '#CHROM' in l:
l=l.split()
for key in anno.keys():
for x in anno[key]:
if key not in g_i.keys():
g_i[key]=[l.index(x)]
else:
g_i[key]=g_i[key]+[l.index(x)]
return g_i
break
def count_derall(i2,g_i,anno_list,o):
with open(i2) as f:
for l in f:
if '#' in l:
continue
else:
l=l.split()
g_d={}
for key in g_i.keys():
gt=''
for x in g_i[key]:
gt+=l[x]
n_d=gt.count('1')
g_d[key]=n_d
# print(g_d)
g_da=''
with open(o,'a') as f1:
for x in anno_list:
g_da+=str(g_d[x])+'\t'
g_da=g_da.strip()+'\n'
# print(g_da)
f1.write(g_da)
def ind_sfs(i,o,gp,start):
pf=pd.read_csv(i,sep='\t')
# print(pf.head())
pf_c=pf.groupby(gp).count()
# print(pf_c)
num=[]
cou=[]
for index in pf_c.index:
num+=[index]
cou+=[pf_c.loc[index].values[0]]
gp_n=pd.DataFrame()
gp_n['NUM']=num
gp_n['COUNT']=cou
t_all=gp_n['COUNT'].sum()
gp_n['PER']=gp_n['COUNT']/t_all
len_x=max(gp_n['NUM'])
if start==0:
with open(o+'%s.txt' %gp, 'a') as f1:
f1.write('1 observation'+'\n')
n1a=''
for n1 in num:
n1a+='d0_'+str(n1)+'\t'
f1.write(n1a.strip()+'\n')
n2a=''
for n2 in cou:
n2a+=str(n2)+'\t'
f1.write(n2a.strip()+'\n')
# print(len_x)
# print(gp_n['COUNT'].values)
plt.figure(figsize=(50, 30), dpi=300)
ax=plt.gca()
ax.set_ylabel("PCT. sites", labelpad=15, font = {'family': 'serif',
# 'style': 'italic',
'weight': 'bold',
'size': 60,
})
ax.set_xlabel("No. derived allele copies",labelpad=15, fontdict = {'family': 'serif',
# 'style': 'italic',
'weight': 'bold',
'size': 60,
})
ax.set_title(gp, y=1.02, fontdict={'family': 'serif',
'weight': 'bold',
'size': 70})
ax.spines['bottom'].set_linewidth(5)
ax.spines['left'].set_linewidth(5)
ax.spines['top'].set_linewidth(5)
ax.spines['right'].set_linewidth(5)
plt.xlim(start-1,len_x+1)
plt.tick_params(which='major', length=30,labelsize=50,width=5,pad=10)
plt.tick_params(which='minor',length=20,width=3)
x_la=[start]
for x_l in range(1,len_x//10+1):
x_la+=[x_l*10]
plt.xticks(x_la)
if len_x>100:
plt.xticks(rotation=45)
plt.bar(gp_n['NUM'][start:],gp_n['PER'][start:])
plt.savefig(o+'%s_{}.pdf'.format(start) %gp)
if len(sys.argv) < 3:
print('''\nThis is a tool for speculation of ancestral allel, calculation of sfs and drawing its bar plot. It is easy-to-use and runing fast. What you should prepare is the phased vcf file containg the data of populations you interested and the outgroup, the outgroup name file, and the annotation file. Enjoy it!!!\n')
It has four models:
0:Using all function, from original vcf data to sfs barplot
1: Only speculate the ancestral allel and output new vcf file using speculated allel as reference
2: Only count the frequency of derived allel in each snp of each population
3: Only draw bar polt of sfs using data generated from the results of calutation of sfs
Example:
Model 0: python superSFS 0 ogdir threshold vcfdir annodir plotdir group
Model 1: python superSFS 1 ogdir threshold vcfdir outdir
Model 2: python superSFS 2 annodir modir coutdir
Model 3: python superSFS 3 coutdir plotdir group
Explation for each parameter:
ogdir: direction of outgroup names file
threshold: a number that if the sum of variant allel in outpgroup greater than it,the variant allel will be counted as ancestral allel
vcfdir: direction of vcf data
vannodir: direction of annotation file with sample names in first column and group name in second colum. This file should has header in first row
vmodir: assign the output direction of generated vcf file using speculated allel as reference
countdir: assign the output direction of calculation of derived allels for each snp in each group
plotdir: assign the output direction of bar plot of sfs
group: the group that you want to analysis''')
else:
opt=sys.argv[1]
if str(opt)=='0':
ogdir, threshold, vcfdir, annodir, plotdir, group = sys.argv[2:]
modir=ogdir+'temp'
coutdir=ogdir+'temp1'
for_sfs(ogdir,threshold,vcfdir,modir)
anno=get_anno(annodir)
g_i=get_index(modir,anno)
anno_list=list(anno.keys())
if not os.path.isfile(coutdir):
with open(coutdir,'w') as f:
name=''
for x in anno_list:
name+=x+'\t'
name=name.strip()+'\n'
f.write(name)
count_derall(modir,g_i,anno_list,coutdir)
ind_sfs(coutdir,plotdir,group,0)
os.remove(modir)
os.remove(coutdir)
elif str(opt)=='1':
ogdir, threshold, vcfdir, outdir = sys.argv[2:]
for_sfs(ogdir,threshold,vcfdir,outdir)
elif str(opt)=='2':
annodir, modir, coutdir = sys.argv[2:]
anno=get_anno(annodir)
g_i=get_index(modir,anno)
anno_list=list(anno.keys())
if not os.path.isfile(coutdir):
with open(coutdir,'w') as f:
name=''
for x in anno_list:
name+=x+'\t'
name=name.strip()+'\n'
f.write(name)
count_derall(modir,g_i,anno_list,coutdir)
elif str(opt)=='3':
coutdir, plotdir, group = sys.argv[2:]
ind_sfs(coutdir,plotdir,group,0)