-
Notifications
You must be signed in to change notification settings - Fork 0
/
variants_mysql_table_prepare.py
165 lines (152 loc) · 8.85 KB
/
variants_mysql_table_prepare.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
#!/usr/bin/env python
# coding: utf-8
import os
import sys
import glob
try:
infile = sys.argv[1]
except:
sys.exit('<variants_table e.g Homo_sapiens.GRCh38.95.variants.intersections>')
outfile = '.'.join(infile.split('.')[:3]) + '.vrnts'
def rel_canvas_pos(elem_genomic_start, elem_genomic_end, utr_length):
elem_genomic_start, elem_genomic_end, utr_length = int(elem_genomic_start), int(elem_genomic_end), int(utr_length)
segment = round((1/float(utr_length) * 200),5)
if elem_genomic_start == 1:
#pos_start = round(((int(elem_genomic_start) * 200)/float(utr_length)),5)
pos_start = 1
if elem_genomic_end == utr_length:
pos_end = 200.0
else:
pos_end = round((elem_genomic_end * segment),5)
span = pos_end - pos_start
elif elem_genomic_start == utr_length:
pos_start = 200.0
pos_end = 0.0
span = pos_end
elif elem_genomic_start == elem_genomic_end:
pos_start = round(((int(elem_genomic_start) * 200)/float(utr_length)),5)
pos_end = 0.0
span = pos_end
else:
pos_start = round(((int(elem_genomic_start) * 200)/float(utr_length)),5)
if elem_genomic_end == utr_length:
pos_end = 200 - pos_start
span = pos_end
else:
pos_end = round((elem_genomic_end * segment),5)
span = pos_end - pos_start
#if str(pos_start).startswith('0.'):
# pos_start = pos_start + 1
return (pos_start, span)
def genomic_pos(line):
if line != []:
chrom = line[0]
utr_start = int(line[1])
utr_end = int(line[2])
variant_start = int(line[8])
variant_end = int(line[9])
overlap = int(line[-1])
strand = line[5]
utr_length = (utr_end - utr_start) + 1
if line[5] == '+':
if utr_start < variant_start and utr_end > variant_end: #phast interna
genomic_variant_start = str((variant_start - utr_start)+1)
genomic_variant_end = str(((variant_start - utr_start)+1) + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(variant_start), str(variant_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start < variant_start and utr_end < variant_end: #phast cavallo 3 primo
genomic_variant_start = str((variant_start - utr_start)+1)
genomic_variant_end = str(((variant_start - utr_start)+1) + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(variant_start), str(utr_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start > variant_start and utr_end > variant_end: #phast cavallo 5 primo
genomic_variant_start = str(1)
genomic_variant_end = str(1 + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(utr_start), str(variant_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start > variant_start and utr_end < variant_end: #phast esterna > utr_length
genomic_variant_start = str(1)
genomic_variant_end = str(1 + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(utr_start), str(utr_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start == variant_start and utr_end == variant_end:
genomic_variant_start = str(1)
genomic_variant_end = str(1 + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(variant_start), str(variant_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start < variant_start and utr_end == variant_end:
genomic_variant_start = str((variant_start - utr_start)+1)
genomic_variant_end = str(((variant_start - utr_start)+1) + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(variant_start), str(variant_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start > variant_start and utr_end == variant_end:
genomic_variant_start = str(1)
genomic_variant_end = str(1 + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(utr_start), str(variant_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start == variant_start and utr_end > variant_end:
genomic_variant_start = str(1)
genomic_variant_end = str(1 + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(variant_start), str(variant_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start == variant_start and utr_end < variant_end:
genomic_variant_start = str(1)
genomic_variant_end = str(1 + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(variant_start), str(utr_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
else:
if utr_start < variant_start and utr_end > variant_end: #phast interna
genomic_variant_start = str((utr_end - variant_end)+1)
genomic_variant_end = str(((utr_end - variant_end)+1) + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(variant_start), str(variant_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start > variant_start and utr_end > variant_end: #phast cavallo 3 primo
genomic_variant_start = str((utr_end - variant_end)+1)
genomic_variant_end = str(((utr_end - variant_end)+1) + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(utr_start), str(variant_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start < variant_start and utr_end < variant_end: #phast cavallo 5 primo
genomic_variant_start = str(1)
genomic_variant_end = str(1 + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(variant_start), str(utr_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start > variant_start and utr_end < variant_end:
genomic_variant_start = str(1)
genomic_variant_end = str(1 + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(utr_start), str(utr_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start == variant_start and utr_end == variant_end:
genomic_variant_start = str(1)
genomic_variant_end = str(1 + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(variant_start), str(variant_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start == variant_start and utr_end < variant_end:
genomic_variant_start = str(1)
genomic_variant_end = str(1 + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(variant_start), str(utr_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start == variant_start and utr_end > variant_end:
genomic_variant_start = str((utr_end - variant_end)+1)
genomic_variant_end = str(((utr_end - variant_end)+1) + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(utr_start), str(utr_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start > variant_start and utr_end == variant_end:
genomic_variant_start = str(1)
genomic_variant_end = str(1 + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(utr_start), str(variant_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
elif utr_start < variant_start and utr_end == variant_end:
genomic_variant_start = str(1)
genomic_variant_end = str(1 + overlap)
return(genomic_variant_start, genomic_variant_end, chrom, str(variant_start), str(variant_end), strand, rel_canvas_pos(genomic_variant_start, genomic_variant_end, utr_length))
dic_line = {}
with open(infile,'r') as f:
for line in f:
#line = line.strip('\n').split('\t')
line = filter(None, (line.strip('\n').replace('\'','').split('\t')))
genomic_variants = genomic_pos(line)
dic_line.setdefault((line[0],line[3], line[4], line[5]), ([], [], [], []))
if line[6] == '5_utr':
dic_line.setdefault((line[0],line[3], line[4], line[5]))[0].append(line)
dic_line.setdefault((line[0],line[3], line[4], line[5]))[1].append(genomic_variants)
else:
dic_line.setdefault((line[0],line[3], line[4], line[5]))[2].append(line)
dic_line.setdefault((line[0],line[3], line[4], line[5]))[3].append(genomic_variants)
with open(outfile, 'w+') as g:
g.write('{}\t{}\t{}\t{}\t{}\n'.format('Chrom','Gene_ID','Transcript_id','Strand','Variants'))
for k,v in sorted(dic_line.items(), key= lambda x: int(x[0][0]) if x[0][0].isdigit() else x[0][0]):
g.write('{}\t{}\n'.format('\t'.join(k), v))
cmd = 'rm -rf {}'
file_to_remove = glob.glob('*.tmp*')
print 'Removing temporary files..'
for elem in file_to_remove:
os.system(cmd.format(elem))
print 'DONE!'
print '----------------------------'
print 'Gzipping {}'.format(outfile)
cmd2 = 'gzip {}'.format(outfile)
os.system(cmd2)
print 'DONE!'