forked from pjweggy/CLADES
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectSumStat.py
More file actions
316 lines (279 loc) · 8.48 KB
/
CollectSumStat.py
File metadata and controls
316 lines (279 loc) · 8.48 KB
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
__author__ = 'pjweggy'
import re
import numpy as np
#from SumStat import *
#import math
import sys
def RecogIndv(line,delim):
if re.match('[a-d][1-2][0-9A-Za-z]+'+delim+'[A-D][1-2]',line):
return line
else:
return ''
def Process(fout,rawdata,delim,nbin,nsites):
if len(rawdata) != 0:
[popsize,Allmat,classlabel]=Dict2Mat(rawdata,delim)
Allmat,snplist=Seq2SNP(Allmat)
sumstat1=[]
startpoint=GetSP(popsize)
for i in range(len(popsize)-1):
for j in range(i+1,len(popsize),1):
pos=range(startpoint[i],(startpoint[i]+popsize[i]),1)+range(startpoint[j],(startpoint[j]+popsize[j]),1)
hap=Allmat[pos,:]
if np.size(sumstat1)==0:
sumstat1=CollectSS2pop(hap,popsize[i],popsize[j],nbin,nsites,snplist)
else:
sumstat1=np.vstack((sumstat1,CollectSS2pop(hap,popsize[i],popsize[j],nbin,nsites,snplist)))
WriteSS(fout,sumstat1,classlabel)
def WriteSS(fout,sumstat,classlabel):
nsam,nft=sumstat.shape
for i in range(nsam):
s=classlabel[i]+' '
for j in range(nft):
s=s+str(j+1)+':'+str("%.6f" % sumstat[i,j])+' '
s=s+'\n'
fout.write(s)
def CollectSS2pop(hap,popsize1,popsize2,nbin,nsites,snplist):
hap1=hap[0:popsize1,:]
hap2=hap[popsize1:,:]
sfs=SFS_fold_bin(hap,nbin,nsites)
#sfs=SFS_fold_bin(hap,nbin)
#pdmean,pdstd=PairDiff(hap)
pdmean1=PairDiff(hap1)
pdmean2=PairDiff(hap2)
pdmean3=PairDiff2(hap1,hap2)
pdmean4=(pdmean1+pdmean2)/2
n_prvpos=prvpos(hap,popsize1,popsize2,nsites)
lsubseq=Lsubseq(hap,popsize1,popsize2,nsites,snplist)
#n_prvpos=prvpos(hap,popsize1,popsize2)
#lsubseq=Lsubseq(hap,popsize1,popsize2)
if pdmean4==0:
ktheta=2
else:
ktheta=pdmean3/pdmean4
if pdmean3==0:
Fst=0
else:
Fst=(pdmean3-pdmean4)/pdmean3
ss=np.hstack((sfs,ktheta))
ss=np.hstack((ss,Fst))
#ss=np.hstack((ss,pdmean4))
ss=np.hstack((ss,n_prvpos))
ss=np.hstack((ss,lsubseq))
return ss
def SFS_fold(hap,nsites):
if isinstance(hap,list):
hap_dim=list()
hap_dim.append(1)
hap_dim.append(len(hap))
else:
hap_dim=hap.shape
if hap_dim[0]%2!=0:
sfs=np.zeros((hap_dim[0]+1)/2)
else:
sfs=np.zeros(hap_dim[0]/2+1)
for i in range(hap_dim[1]):
temp=(hap[:,i] == 1).sum()
if temp <= (hap_dim[0]/2):
sfs[int(temp)] += 1
else:
sfs[hap_dim[0]-int(temp)] += 1
return sfs/hap_dim[1]
#return sfs/nsites
###Folded SFS with bin
def SFS_fold_bin(hap,nbin,nsites):
sfs=SFS_fold(hap,nsites)
nlen=sfs.shape[0]
window=nlen/nbin
sfs_bin=np.zeros(nbin)
for i in range(nbin):
if i != (nbin-1):
sfs_bin[i]=sum(sfs[window*i:window*(i+1)])
else:
sfs_bin[i]=sum(sfs[window*i:])
return sfs_bin
###Intro-population Pairwise difference
def PairDiff(hap):
hap_dim=hap.shape
pairdiff=list()
for i in range(hap_dim[0]-1):
for j in range(i+1,hap_dim[0],1):
pairdiff.append(pd(hap[i,:],hap[j,:]))
#print pairdiff
#pairdiff_reg=[x*(float(1.0)/hap_dim[1]) for x in pairdiff] ##regularize pairwise difference
pdmean=np.mean(pairdiff)
if len(pairdiff)==0:
pdmean=0.0
#pdstd=std(pairdiff_reg)
return pdmean #,pdstd
def pd(hap1,hap2):
pd=0
for i in range(len(hap1)):
if abs(hap1[i]-hap2[i])>0:
pd+=1
return pd
###Outra-population Pairwise difference
def PairDiff2(hap1,hap2):
hap1_dim=hap1.shape[0]
hap2_dim=hap2.shape[0]
pairdiff=list()
for i in range(hap1_dim):
for j in range(hap2_dim):
pairdiff.append(pd(hap1[i,:],hap2[j,:]))
#print pairdiff
#pairdiff_reg=[x*(float(1.0)/hap1.shape[1]) for x in pairdiff]
pdmean=np.mean(pairdiff)
#pdstd=std(pairdiff)
return pdmean#,pdstd
#percentage of private positions overall SNPs
def prvpos(hap,popsize1,popsize2,nsites):
hap_dim=hap.shape
n_prvpos=0
for i in range(hap_dim[1]):
n0hap1=(hap[0:popsize1,i]==0).sum()
n1hap1=(hap[0:popsize1,i]==1).sum()
n0hap2=(hap[popsize1:,i]==0).sum()
n1hap2=(hap[popsize1:,i]==1).sum()
if (n0hap1==popsize1 or n1hap1==popsize1) and (n0hap2!=popsize2 and n1hap2 != popsize2):
#print i+1
n_prvpos += 1
elif (n0hap2==popsize2 or n1hap2==popsize2) and (n0hap1 != popsize1 and n1hap1 !=popsize1):
#print i+1
n_prvpos +=1
return float(n_prvpos)/hap_dim[1]
#return float(n_prvpos)/nsites
def Lsubseq(hap,popsize1,popsize2,nsites,snplist):
tract=list()
lsubseq=list()
nsnp=hap.shape[1]
for i in range(popsize1):
for j in range(popsize1,(popsize1+popsize2),1):
nshare=0
##compare two sequences
for k in range(nsnp):
if hap[i,k] == hap[j,k] :
if k==0:
nshare += (snplist[k]+1)
else:
nshare += snplist[k]-snplist[k-1]
if k == nsnp-1:
tract.append(nshare)
nshare=0
else:
if nshare != 0:
tract.append(nshare)
nshare = 0
lsubseq.append(max(tract))
tract=list()
Lsubseq=max(lsubseq)
#return float(Lsubseq)/hap.shape[1]
return float(Lsubseq)/nsites
def GetSP(popsize):
startpoint=list()
startpoint.append(0)
s=0
for i in range(len(popsize)-1):
s+=popsize[i]
startpoint.append(s)
return startpoint
def Dict2Mat(rawdata,delim):
speciesname=list()
speciesinfo=dict()
template=''
Allmat=[]
for key in sorted(rawdata.iterkeys()):
###process key
r=re.compile('([a-b][1-2])([0-9]+)('+delim+')([A-B][1-2])')
m=r.match(key)
spn=m.group(1)
if spn in speciesinfo.keys():
speciesinfo[spn]+=1
else:
speciesname.append(spn)
speciesinfo[spn]=1
### process sequence
if len(template)==0:
template=rawdata[key]
Allmat=Gen01fromSeq(rawdata[key],template)
else:
#Update(template,rawdata[key])
Allmat=np.vstack((Allmat,Gen01fromSeq(rawdata[key],template)))
popsize=list()
for spn in speciesname:
popsize.append(speciesinfo[spn])
classlabel=list()
for i in range(len(speciesname)-1):
for j in range(i+1,len(speciesname),1):
if speciesname[i][0]==speciesname[j][0]:
classlabel.append('-1')
else:
classlabel.append('+1')
return popsize,Allmat,classlabel
def Seq2SNP(Allmat):
nr,nc=Allmat.shape
snplist=list()
for i in range(nc):
if sum(Allmat[:,i]) != 0:
snplist.append(i)
AllSNP=np.array(Allmat[:,snplist])
# print AllSNP.shape
return AllSNP,snplist
def Update(template,newtem):
for i in range(len(template)):
if (template[i]=='-' and newtem[i]!='-'):
if i!=(len(template)-1):
tmp=template[0:i]+newtem[i]+template[(i+1):]
else:
tmp=template[0:i]+newtem[i]
template=tmp
return template
def Gen01fromSeq(seq,template):
hap=list()
for i in range(len(template)):
if seq[i]=='-':
hap.append(2)
elif seq[i]==template[i]:
hap.append(0)
else:
hap.append(1)
return hap
def GetSeq(line):
header=line.split()[0]
line=line.replace(header,'')
line=line.replace(' ','')
line=line.replace('\n','')
return line
prefix=sys.argv[1]
#prefix='temp'
'''
theta=float(sys.argv[2])
tau=float(sys.argv[3])
if tau<0.0001:
seqprefix=prefix+'_'+str(theta)+'_'+str(("%.5f" % tau))
else:
seqprefix=prefix+'_'+str(theta)+'_'+str(tau)
'''
delim='\^'
rawdata=dict()
nbin=3
nsites=10000
#f1=open(seqprefix+'_seq.txt','rb')
#fout=open(seqprefix+'.sumstat','ab')
f1=open(prefix+'_seq.txt','rb')
fout=open(prefix+'.sumstat','ab')
for line in f1:
if line[0:2]!='\n':
header=line.split()[0]
else:
header=''
indvName=RecogIndv(header,delim)
if len(indvName)>0:
seq=GetSeq(line)
rawdata[indvName]=seq
else:
Process(fout,rawdata,delim,nbin,nsites)
rawdata.clear()
Process(fout,rawdata,delim,nbin,nsites)
rawdata.clear()
#print rawdata
f1.close()
fout.close()