-
Notifications
You must be signed in to change notification settings - Fork 3
/
LD-annot0.4.py
471 lines (394 loc) · 13.4 KB
/
LD-annot0.4.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
#!/usr/bin/python3
# -*- coding: iso-8859-1 -*-
import sys, fileinput
import string
import math
import time
import sys
import operator
import copy
import numpy as np
import os
def documentation():
print('''
There are missing inputfile in the command
Usage:
python annot-GBS.py data annotation-file candidate-marker-list type threshold output_name SNP-map-file*
data: either a vcf file or a path to genotype files from SNPchips
annotation-file: a gff file with annotations
candidate-marker: a list of candidate variant with chr/contig name, and position
type: type of region the user wants to use; either "gene", "mRNA", "CDS"
threshold: r2 (linkage) threshold at which stops the range of interest.
output_name: selfexplaining name
SNP-map-file: only required with SNPchips data
''')
try:
da = sys.argv[1]
an = sys.argv[2]
cm = sys.argv[3]
ty = sys.argv[4]
thr = sys.argv[5]
o = sys.argv[6]
if da[-4:] != '.vcf':
sm = sys.argv[7]
else:
pass
except:
documentation()
sys.exit(1)
lf = os.listdir()
if "checkpoint" not in lf:
if da[-4:] != '.vcf':
#format testing for SNPchips data
os.chdir(da)
files = os.listdir('./')
print("File checked to verify format:")
print(files[0])
t = files[0]
f = open(t,'r')
a = 0
ln=[] #list of markers names
for line in f:
if a > 10:
line = line.replace('-\t','N\t')
l = line.strip().split('\t')
if len(l[2]) == 1 :
if len(l[3]) == 1:
pass
else:
print("There's a problem with input files, please make sure your files\nare tabulated separated and alleles are in the third and fourth column")
exit(1)
else:
print("There's a problem with input files, please make sure your files\nare tabulated separated and alleles are in the third and fourth column")
exit(1)
ln.append(l[0])
else:
a += 1
pass
f.close()
os.chdir('../')
#testing if chromosomes names fit between map and SNP files
print("Checking format for:")
print("\t",str(sm))
map = open(sm,'r')
lmap = []
lkmap = []
line = map.readline()
for line in map:
l = line.strip().split('\t')
lmap.append(l[1])
lkmap.append(l[2])
for i in ln:
if i not in lmap:
print("genotyped SNPs are not mapped.")
else:
pass
map.close()
#testing if chromosomes names fit between gff and SNP files
print("Checking format for:")
print('\t',an)
gff =open(an,'r')
lannot = []
for line in gff:
if line[0] == '#':
pass
else:
l = line.strip().split('\t')
lannot.append(l[0])
gff.close()
miss = []
for i in lkmap:
if i in lannot:
pass
else:
miss.append(i)
umiss = list(set(miss))
print("There's no annotations for chromosome:")
print('\t',','.join(umiss))
print("if this is not empty, At least some SNPs won't have annotations which might be related to discrepancies regarding chromosome names.")
print("Maybe you should verify chromosome names in the annotation file.",'\n')
#list of chromosome names from candidate SNPs
print("checking format for:")
print('\t',cm)
can = open(cm,'r')
lcan = []
for line in can:
l = line.strip().split('\t')
if l[0] == 'CHROM':
pass
else:
if l[0].isnumeric() or l[0] == 'X' or l[0] == 'Y':
if l[1].isnumeric():
lcan.append(l[0])
else:
print("SNP Position column doesn't contain only numbers")
exit(1)
else:
print("Chromosome column contains other names than a number or X or Y")
miss = []
for i in lcan:
if i in lkmap:
pass
else:
miss.append(i)
umiss = list(set(miss))
print("These candidate may not be mapped:")
print(','.join(umiss))
print("if this is not empty, At least some SNPs won't have genome location, this might be related to discrepancies regarding chromosome names.")
print("Maybe you should verify chromosome names in the those files.")
#that's where starts the checkup of data included into a vcf file
else:
#format testing for vcf data
print("checking format for:")
print('\t',da)
f = open(da,'r')
a = 0
ln = []
for line in f:
l = line.strip().split('\t')
if line[0] == '#CHROM':
a += 1
if l[1] != 'POS' or l[3] != REF or l[4] != ALT:
print("There's a problem with the vcf input file, please verify your file")
exit(1)
else:
pass
elif a >= 1:
if l[0].isnumeric() or l[0] == 'X' or l[0] == 'Y':
ln.append(l[0])
else:
print("chromosome numbers should be numbers or X or Y")
exit(1)
else:
pass
#format testing for congruence between chromosome names over data and annotation
#list of chromosome names from candidate SNPs
print("checking format for:")
print('\t',cm)
can = open(cm,'r')
lcan = []
for line in can:
l = line.strip().split('\t')
if l[0] == 'CHROM':
pass
else:
if l[0].isnumeric() or l[0] == 'X' or l[0] == 'Y':
if l[1].isnumeric():
lcan.append(l[0])
else:
print("Position column doesn't contain only numbers")
exit(1)
else:
print("Chromosome column contains other names than a number or X or Y")
#list of chromosome names from annotation file
print("checking format for:")
print('\t',an)
gff = open(an,'r')
lannot = []
for line in gff:
if line[0] == '#':
pass
else:
l = line.strip().split('\t')
lannot.append(l[0])
gff.close()
#real test for congruence
for i in ln:
if i in lannot:
pass
else:
print("At least some SNPs are not in the annotation file, might be related to discrepancies regarding chromosome names")
exit(1)
if i in lcan:
pass
else:
print("None of the SNPs are candidate, might be related to discrepancies regarding chromosome names")
exit(1)
c = open("checkpoint", 'w')
c.write("verif_format\n")
c.close()
#run the bash script
if da[-4:] != '.vcf':
os.system('bash calculLD.1.sh %s %s %s %s %s %s' % (da, an, cm, ty, thr,sm))
else:
os.system('bash calculLD.1.sh %s %s %s %s %s' % (da, an, cm, ty, thr))
#continuation of the python procedure
thr = sys.argv[5] # threshold
kind = sys.argv[4] #type
temp = open('meanLD','r') #meanLD
line = temp.readline()
l = line.strip().split()
mean = int(float(l[0]))
#dictionnary of all genes annotations
f = open(sys.argv[2],'r') #gff file
dica = {}
for line in f:
if line[0] != '#':
l = line.strip().split('\t')
if l[2] == kind:
reg = l[0]+'_'+l[3]+'_'+l[4]
dica[reg] = l[-1]
#list of candidate SNPs
#SNP = 'chr_pos'
g = open(sys.argv[3],'r')
lsc = []
line = g.readline()
for line in g:
l = line.strip().split('\t')
SNP = l[0]+'_'+l[1]
lsc.append(SNP)
#dictionnary of candidate regions
#SNP = 'chr_pos'
h = open('mydata.ld','r')
dics = {}
lowl=[]
for line in h:
l = line.strip().split('\t')
SNP1 = l[0]+'_'+l[1]
SNP2 = l[3]+'_'+l[4]
if SNP1 in lsc:
if float(l[-1]) >= float(thr):
if SNP1 in dics.keys():
dics[SNP1].append(l[4])
else:
dics[SNP1] = [l[4]]
else:
if SNP1 not in lowl:
lowl.append(SNP1)
else:
pass
if SNP2 in lsc:
if float(l[-1]) >= float(thr):
if SNP2 in dics.keys():
dics[SNP2].append(l[1])
else:
dics[SNP2] = [l[1]]
else:
if SNP2 not in lowl:
lowl.append(SNP2)
else:
pass
#at the end we get a dictionnary with position for SNPs in linkage
#writing output
o = open(sys.argv[6],'w')
o.write("SNP\tchromosome\tregion_start\tregion_end\tgene_start\tgene_end\tannotation\n")
for i in lsc:
j = i.split('_')
if i not in dics.keys() and i not in lowl:
z = str(i)+'_alone'
dics[z] = [(int(j[1]) - mean) , (int(j[1]) + mean)]
else:
pass
for i in dics:
j = i.split('_')
up = int(max(dics[i]))
down = int(min(dics[i]))
for k in dica.keys():
l = k.split('_')
if j[0] == l[0]:
if int(l[1]) < down < int(l[2]) or int(l[1]) < up < int(l[2]) or down < int(l[1]) < up or down < int(l[2]) < up :
res = [str(i),str(l[0]),str(down),str(up),str(l[1]),str(l[2])]
res.append(dica[k])
o.write('\t'.join(res)+'\n')
else:
pass
else:
pass
f.close()
g.close()
h.close()
o.close()
#here start the pipeline when format has already been checked
#as written in the checkpoint file
else:
#run the bash script
if da[-4:] != '.vcf':
os.system('bash calculLD.1.sh %s %s %s %s %s %s' % (da, an, cm, ty, thr,sm))
else:
os.system('bash calculLD.1.sh %s %s %s %s %s' % (da, an, cm, ty, thr))
#continuation of the python procedure
thr = sys.argv[5] # threshold
kind = sys.argv[4] #type
temp = open('meanLD','r') #meanLD
line = temp.readline()
l = line.strip().split()
mean = int(float(l[0]))
#dictionnary of all genes annotations
f = open(sys.argv[2],'r') #gff file
dica = {}
for line in f:
if line[0] != '#':
l = line.strip().split('\t')
if l[2] == kind:
reg = l[0]+'_'+l[3]+'_'+l[4]
dica[reg] = l[-1]
#list of candidate SNPs
#SNP = 'chr_pos'
g = open(sys.argv[3],'r')
lsc = []
line = g.readline()
for line in g:
l = line.strip().split('\t')
SNP = l[0]+'_'+l[1]
lsc.append(SNP)
#dictionnary of candidate regions
#SNP = 'chr_pos'
h = open('mydata.ld','r')
dics = {}
lowl=[]
for line in h:
l = line.strip().split('\t')
SNP1 = l[0]+'_'+l[1]
SNP2 = l[3]+'_'+l[4]
if SNP1 in lsc:
if float(l[-1]) >= float(thr):
if SNP1 in dics.keys():
dics[SNP1].append(l[4])
else:
dics[SNP1] = [l[4]]
else:
if SNP1 not in lowl:
lowl.append(SNP1)
else:
pass
if SNP2 in lsc:
if float(l[-1]) >= float(thr):
if SNP2 in dics.keys():
dics[SNP2].append(l[1])
else:
dics[SNP2] = [l[1]]
else:
if SNP2 not in lowl:
lowl.append(SNP2)
else:
pass
#at the end we get a dictionnary with position for SNPs in linkage
#writing output
o = open(sys.argv[6],'w')
o.write("SNP\tchromosome\tregion_start\tregion_end\tgene_start\tgene_end\tannotation\n")
for i in lsc:
j = i.split('_')
if i not in dics.keys() and i not in lowl:
z = str(i)+'_alone'
dics[z] = [(int(j[1]) - mean) , (int(j[1]) + mean)]
else:
pass
for i in dics:
j = i.split('_')
up = int(max(dics[i]))
down = int(min(dics[i]))
for k in dica.keys():
l = k.split('_')
if j[0] == l[0]:
if int(l[1]) < up < int(l[2]) or int(l[1]) < down < int(l[2]) or down < int(l[1]) < up or down < int(l[2]) < up:
res = [str(i), str(l[0]),str(down), str(up),str(l[1]),str(l[2])]
res.append(dica[k])
o.write('\t'.join(res)+'\n')
else:
pass
else:
pass
f.close()
g.close()
h.close()
o.close()