-
Notifications
You must be signed in to change notification settings - Fork 2
/
metaSNV_DistDiv.py
executable file
·348 lines (250 loc) · 12.6 KB
/
metaSNV_DistDiv.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
#!/usr/bin/env python
import os
import sys
import time
import argparse
import glob
from shutil import copyfile
from multiprocessing import Pool
from functools import partial
try:
import numpy as np
except ImportError:
sys.stderr.write("Numpy is necessary to run this script.\n")
sys.exit(1)
try:
import pandas as pd
except ImportError:
sys.stderr.write("Pandas is necessary to run this script.\n")
sys.exit(1)
basedir = os.path.dirname(os.path.abspath(__file__))
############################################################
### Parse Commandline Arguments
def get_arguments():
'''
Get commandline arguments and return namespace
'''
## Initialize Parser
parser = argparse.ArgumentParser(prog='metaSNV_DistDiv.py', description='metaSNV distance and diversity computation', epilog='''Note:''', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# Not Showns:
parser.add_argument('--version', action='version', version='%(prog)s 2.0', help=argparse.SUPPRESS)
parser.add_argument("--debug", action="store_true", help=argparse.SUPPRESS)
# REQUIRED arguments:
parser.add_argument('--filt', metavar=': Filtered frequency files', help="Folder containing /pop/*.filtered.freq", required = True)
# OPTIONAL arguments:
parser.add_argument('--dist',action='store_true', help="Compute distances")
parser.add_argument('--div',action='store_true', help="Compute Diversity and FST")
parser.add_argument('--divNS',action='store_true', help="Computing piN and piS")
parser.add_argument('--matched',action='store_true', help="Computing on matched positions only")
parser.add_argument('--n_threads',metavar=': Number of Processes',default=1,type=int, help="Number of jobs to run simmultaneously.")
return parser.parse_args()
############################################################
### Basic functions
def file_check():
''' Check if required files exist (True / False)'''
args.projdir = '/'.join(args.filt.rstrip('/').split('/')[:-1])
args.pars = args.filt.rstrip('/').split('/')[-1].strip('filtered')
args.coverage_file = args.projdir+'/'+args.projdir.split('/')[-1]+'.all_cov.tab'
args.percentage_file = args.projdir+'/'+args.projdir.split('/')[-1]+'.all_perc.tab'
args.bedfile = args.projdir+'/'+'bed_header'
print("Checking for necessary input files...")
if os.path.isfile(args.coverage_file) and os.path.isfile(args.percentage_file) and os.path.isfile(args.bedfile):
print("found: '{}' \nfound:'{}' \nfound:'{}'".format(args.coverage_file, args.percentage_file, args.bedfile))
else:
sys.exit("\nERROR: No such file '{}',\nERROR: No such file '{}',\nERROR: No such file '{}'".format(args.coverage_file, args.percentage_file, args.bedfile))
def print_arguments():
## Print Defined Arguments:
print("")
print("Checking required arguments:")
if args.filt:
print("Filtered folder : {}".format(args.filt) )
print("Checking optional arguments:")
if args.dist:
print("Computing distances : {}".format(args.dist) )
if args.div:
print("Computing diversity and FST : {}".format(args.div) )
if args.divNS:
print("Computing N and S diversities : {}".format(args.divNS) )
if args.matched:
print("Matching positions (present in 90% of the samples) : {}".format(args.matched) )
if args.n_threads:
print("Number of parallel processes : {}".format(args.n_threads) )
print("")
############################################################
### Distances
def l1nonans(d1,d2):
return np.abs(d1 - d2).mean()
def alleledist(d1,d2, threshold=.6):
return (np.abs(d1 - d2) > threshold).mean()
def computeDist(filt_file):
''' Compute distances per species '''
species = filt_file.split('/')[-1].replace('.freq','')
data = pd.read_table(filt_file, index_col=0, na_values=['-1']).T
dist = [[l1nonans(data.iloc[i], data.iloc[j]) for i in range(len(data))] for j in range(len(data))]
dist = pd.DataFrame(dist, index=data.index, columns=data.index)
dist.to_csv(outdir + '/' + '%s.mann.dist' % species, sep='\t')
dist = [[alleledist(data.iloc[i], data.iloc[j]) for i in range(len(data))] for j in range(len(data))]
dist = pd.DataFrame(dist, index=data.index, columns=data.index)
dist.to_csv(outdir + '/' + '%s.allele.dist' %species , sep='\t')
def computeAllDist(args):
print "Computing distances"
allFreq = glob.glob(args.filt + '/pop/*.freq')
p = Pool(processes = args.n_threads)
p.map(computeDist, allFreq)
p.close()
p.join()
############################################################
### Pairwise Diversity
def compute_diversity(sample1, sample2):
'''Pairwise nucleotide diversity'''
sample1nd = sample1.reset_index().drop_duplicates(subset='index', keep=False).set_index('index')
sample2nd = sample2.reset_index().drop_duplicates(subset='index', keep=False).set_index('index')
sample2nd = sample2nd.reindex(index=sample1nd.index)
s1 = sample1nd.values
s2 = sample2nd.values
valid = ~(np.isnan(s1) | np.isnan(s2))
s1 = s1[valid]
s2 = s2[valid]
s1 = np.vstack([s1, 1 - s1])
s2 = np.vstack([s2, 1 - s2])
dist_nd = (s1[0]*s2[1]+s1[1]*s2[0]).sum()
def position_diversity(x):
out = np.outer(x.s1.values, x.s2.values)
return np.nansum(out) - np.nansum(out.diagonal())
sample1d = sample1.ix[sample1.index[sample1.index.duplicated()]]
sample2d = sample2.ix[sample2.index[sample2.index.duplicated()]]
if not len(sample1d) or not len(sample2d):
# No duplicates
return dist_nd
both = pd.DataFrame({'s1' : sample1d, 's2' : sample2d})
both = both.reset_index()
both = pd.concat([both,(1. - both.groupby('index').sum()).reset_index()])
dist_d = both.groupby('index', group_keys=False).apply(position_diversity).sum()
return dist_d + dist_nd
############################################################
### Per Species Diversity
def computeDiv(filt_file, horizontal_coverage, vertical_coverage, bedfile_tab, matched):
'''Per species computation'''
species = int(filt_file.split('/')[-1].split('.')[0])
data = pd.read_table(filt_file, index_col=0, na_values=['-1'])
pre_index = [i.split(':') for i in list(data.index)]
# Setting index for each position
data = data.set_index(pd.Index([item[0] + ':' + item[1] + ':' + item[2] for item in pre_index]))
data = data.sort_index()
########
## If matched, filter for 'common' positions :
if matched:
def filt_proportion(x):
if len(x) == 2:
x = x.iloc[1]
n = np.count_nonzero(np.isnan(x))
return n>(len(x)*(0.1))
index_drop = [index for index in data.index if filt_proportion(data.loc[index])]
data = data.drop(index_drop)
########
## Number of bases observed :
genome_length = bedfile_tab.loc[str(species), 2].sum()
# Genome length corrected for horizontal coverage
correction_coverage = [[(min(horizontal_coverage.loc[species, i], horizontal_coverage.loc[species, j]) * genome_length) / 100 for i in data.columns] for j in data.columns]
########
## Vertical coverage in pi within : AvgCov / (AvgCov - 1)
for i in data.columns:
j = list(data.columns).index(i)
correction_within = vertical_coverage.loc[species,i] / (vertical_coverage.loc[species,i] - 1)
correction_coverage[j][j] = correction_coverage[j][j] / correction_within
div = [[compute_diversity(data.iloc[:, i], data.iloc[:, j]) / correction_coverage[j][i] for i in range(j + 1)] for j in range(len(data.columns))]
FST = [[(1-(div[i][i]+div[j][j])/(2*div[j][i])) for i in range(j + 1)] for j in range(len(div))]
div = pd.DataFrame(div, index=data.columns, columns=data.columns)
FST = pd.DataFrame(FST, index=data.columns, columns=data.columns)
div.to_csv(outdir + '/' + '%s.diversity' % species, sep='\t')
FST.to_csv(outdir + '/' + '%s.FST' % species, sep='\t')
############################################################
### Per Species N & S Diversity
def computeDivNS(filt_file, horizontal_coverage, vertical_coverage, bedfile_tab, matched):
'''Per species computation'''
species = int(filt_file.split('/')[-1].split('.')[0])
data = pd.read_table(filt_file, index_col=0, na_values=['-1'])
pre_index = [i.split(':') for i in list(data.index)]
# Setting index for each position
index1 = [item[0] + ':' + item[1] + ':' + item[2] for item in pre_index]
# Setting index for Non-synonymous vs Synonymous
index2 = [item[4].split('[')[0] for item in pre_index]
data = data.set_index(pd.MultiIndex.from_arrays([index1, index2], names=['index', 'significance']))
data = data.sort_index()
data_N = data.xs('N', level='significance')
data_S = data.xs('S', level='significance')
########
## If matched, filter for 'common' positions :
if matched:
def filt_proportion(x):
if len(x) == 2:
x = x.iloc[1]
n = np.count_nonzero(np.isnan(x))
return n>(len(x)*(0.1))
index_drop = [index for index in data_N.index if filt_proportion(data_N.loc[index])]
data_N = data_N.drop(index_drop)
index_drop = [index for index in data_S.index if filt_proportion(data_S.loc[index])]
data_S = data_S.drop(index_drop)
########
## Number of bases observed :
genome_length = bedfile_tab.loc[str(species), 2].sum()
# Genome length corrected for horizontal coverage
correction_coverage = [[(min(horizontal_coverage.loc[species, i], horizontal_coverage.loc[species, j]) * genome_length) / 100 for i in data.columns] for j in data.columns]
########
## Vertical coverage in pi within : AvgCov / (AvgCov - 1)
for i in data.columns:
j = list(data.columns).index(i)
correction_within = vertical_coverage.loc[species,i] / (vertical_coverage.loc[species,i] - 1)
correction_coverage[j][j] = correction_coverage[j][j] / correction_within
div_N = [[compute_diversity(data_N.iloc[:, i], data_N.iloc[:, j]) / correction_coverage[j][i] for i in range(j + 1)] for j in range(len(data_N.columns))]
div_N = pd.DataFrame(div_N, index=data_N.columns, columns=data_N.columns)
div_N.to_csv(outdir + '/' + '%s.N_diversity' % species, sep='\t')
div_S = [[compute_diversity(data_S.iloc[:, i], data_S.iloc[:, j]) / correction_coverage[j][i] for i in range(j + 1)] for j in range(len(data_S.columns))]
div_S = pd.DataFrame(div_S, index=data_S.columns, columns=data_S.columns)
div_S.to_csv(outdir + '/' + '%s.S_diversity' % species, sep='\t')
############################################################
### Compute Diversity for all Species
def computeAllDiv(args):
'''Computing diversities & FST'''
print "Computing diversities & FST"
# Load external info : Coverage, genomes size, genes size
horizontal_coverage = pd.read_table(args.percentage_file, skiprows=[1], index_col=0)
vertical_coverage = pd.read_table(args.coverage_file, skiprows=[1], index_col=0)
bedfile_tab = pd.read_table(args.bedfile, index_col=0, header=None)
bed_index = [i.split('.')[0] for i in list(bedfile_tab.index)]
bedfile_tab = bedfile_tab.set_index(pd.Index(bed_index))
#All filtered.freq files in input folder
allFreq = glob.glob(args.filt + '/pop/*.freq')
if args.div:
p = Pool(processes = args.n_threads)
partial_Div = partial(computeDiv, horizontal_coverage = horizontal_coverage, vertical_coverage = vertical_coverage, bedfile_tab = bedfile_tab, matched = args.matched)
p.map(partial_Div, allFreq)
p.close()
p.join()
if args.divNS:
p = Pool(processes = args.n_threads)
partial_DivNS = partial(computeDivNS, horizontal_coverage = horizontal_coverage, vertical_coverage = vertical_coverage, bedfile_tab = bedfile_tab, matched = args.matched)
p.map(partial_DivNS, allFreq)
p.close()
p.join()
############################################################
### Script
if __name__ == "__main__":
#####################
args = get_arguments()
print_arguments()
file_check()
if args.debug:
print_arguments()
#####################
if args.matched:
outdir = args.projdir + '/distances' + args.pars + '.matched_pos/'
else:
outdir = args.projdir + '/distances' + args.pars +'/'
if not os.path.exists(outdir):
os.makedirs(outdir)
#####################
if args.dist:
computeAllDist(args)
if args.div or args.divNS:
computeAllDiv(args)