-
Notifications
You must be signed in to change notification settings - Fork 1
/
win_stats_ts.py
142 lines (125 loc) · 5.04 KB
/
win_stats_ts.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
import allel
import subprocess, msprime, pyslim
import matplotlib.pyplot as plt
import numpy as np
import os
import random
import re
from glob import glob
import pickle
import sys
import itertools
from timeit import default_timer as timer
def ac_from_ts(ts, n_pops, N):
'''
This function takes a tree sequence, and returns tuple with a list of allele counts for each subpop and the positions'''
acs=[]
hap = allel.HaplotypeArray(ts.genotype_matrix())
geno = hap.to_genotypes(ploidy=2)
for i in range(n_pops):
subpop_indexes = list(np.arange(i*N,(i+1)*N))
acs.append(geno.count_alleles(subpop=subpop_indexes))
pos=np.array([s.position for s in ts.sites()])
return(acs, pos)
def win_pi_sims(path, neut_mut, n_pops, n_sims, T, win_size, L, N):
foname = os.path.basename(path[:-1])
print(("Base filename:"+foname), flush=True)
x = np.arange(n_pops)
combs = list(itertools.combinations(x, 2))
pis=np.zeros((len(T),n_sims,n_pops,int(L/win_size)))
div=np.zeros((len(T), n_sims,len(combs),int(L/win_size)))
fst=np.zeros((len(T), n_sims,len(combs),int(L/win_size)))
tajd=np.zeros((len(T),n_sims,n_pops,int(L/win_size)))
for t in range(len(T)):
for i in range(n_sims):
files = glob(path+str(T[t])+"N_sim_"+str(i)+"_RAND_*[0-9]_overlaid.trees")
print(files)
assert (len(files) == 1), str(len(files))+" file(s) found with glob T: "+str(T[t])+" sim:"+str(i)
filename= files[0]
print(filename)
ts = pyslim.load(filename).simplify()
#print(("Pi0: ", ts.pairwise_diversity(samples=ts.samples(population=0)),"Pi1: ", ts.pairwise_diversity(samples=ts.samples(population=1))), flush=True)
s1 = timer()
acs, pos = ac_from_ts(ts, n_pops, N)
for j in range(n_pops):
pi, windows, n_bases, counts = allel.windowed_diversity(pos, acs[j], size=win_size, start=1, stop=L)
pis[t,i,j,:] = pi
D, windows, counts = allel.windowed_tajima_d(pos, acs[j], size=win_size, start=1, stop=L)
tajd[t,i,j,:] = D
s2 = timer()
print(("Calculating windowed Pi/TajD... Time elapsed (min):"+str(round((s2-s1)/60,3))), flush=True)
s1=timer()
for k in range(len(combs)):
dxy, windows, n_bases, counts = allel.windowed_divergence(pos, acs[combs[k][0]], acs[combs[k][1]], size=win_size, start=1, stop=L)
div[t,i,k,:] = dxy
fstat, windows, counts = allel.windowed_hudson_fst(pos, acs[combs[k][0]], acs[combs[k][1]], size=win_size, start=1, stop=L)
fst[t,i,k,:] = fstat
s2 = timer()
print(("Calculating windowed Dxy and Fst... Time elapsed (min):"+str(round((s2-s1)/60,3))), flush=True)
s1 = timer()
print((pis.shape), flush=True)
print((tajd.shape), flush=True)
print((div.shape), flush=True)
output = open(path+foname+'_pis.pkl', 'wb')
pickle.dump(pis, output)
output.close()
output = open(path+foname+'_tajd.pkl', 'wb')
pickle.dump(tajd, output)
output.close()
output = open(path+foname+'_div.pkl', 'wb')
pickle.dump(div, output)
output.close()
output = open(path+foname+'_fst.pkl', 'wb')
pickle.dump(fst, output)
output.close()
if (0):
plt.subplot(2, 1, 1)
plt.plot(np.transpose(pis[0,0,:]), "-")
plt.title('0N after split')
plt.ylabel('Pi')
plt.subplot(2, 1, 2)
plt.plot(np.transpose(pis[9,0,:]), "-")
plt.title('10N after split')
plt.xlabel('Window')
plt.ylabel('Pi')
plt.tight_layout()
plt.savefig(path+foname+'_landscape.pdf')
plt.close()
s2 = timer()
print(("Saving stats and plots to file... Time elapsed (min):"+str(round((s2-s1)/60,3))), flush=True)
#sys.stdout.flush()
s1 = timer()
path = sys.argv[1]
win_size=int(sys.argv[2])
L=int(sys.argv[3])
n_sims=int(sys.argv[4])
neut_mut = 1e-8
n_pops = 2
N=10000
#T=np.arange(0.1,1.1,step=0.1)
#T=np.concatenate([T,np.array([2.0])])
#T=np.around(T, 1)
#T=np.arange(0,11,step=1)
T=np.concatenate([np.arange(0,2.2,step=0.4), np.arange(4,11,step=2)])
T = [float("%.1f"%t) for t in T]
s2 = timer()
print(("Initializing... Time elapsed (min):"+str(round((s2-s1)/60,3))), flush=True)
win_pi_sims(path, neut_mut, n_pops, n_sims, T, win_size, L, N)
"""
path = "/Users/murillo/Drive/phd/w19/rotation/trees/"
folders = glob("N_*_mu_*_L0_*")
paths = [path+folders[i]+"/" for i in range(len(folders))]
total_mut = 1e-8
n_pops = 2
n_sims=1
win_size = 500000
T=np.arange(1,11,step=1)
for i in range(len(paths)):
path = paths[i]
foname = os.path.basename(path[:-1])
print((foname), flush=True)
matches = re.match( r'N_(\d+)_mu_(.*)_r_(.*)_deff_(\d+)_L_(\d+)_L0_(.+)_L1_(\d+)', foname)
N, mu, r, deff, L, L0, L1 = matches.groups()
del_mut = mu
win_pi_sims(path, neut_mut, del_mut, n_pops, n_sims, T, win_size)"""
#python3 mimulus_win_pi.py /Users/murillo/Drive/phd/w19/rotation/trees/N_10000_mu_0_r_2e-08_deff_0_L_20000000_L0_0_L1_0_m_0/ 500000 1