-
Notifications
You must be signed in to change notification settings - Fork 2
/
autoOncotator.py
221 lines (189 loc) · 10.4 KB
/
autoOncotator.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 4 22:46:11 2018
@author: shanghungshih
"""
import sys
import os
import time
import warnings
import argparse
import logging
from textwrap import dedent
from selenium import webdriver
from multiprocessing import Pool
logger_stderr = logging.getLogger(__name__+'stderr')
logger_stderr.setLevel(logging.INFO)
stderr_handler = logging.StreamHandler()
stderr_handler.setFormatter(logging.Formatter('%(levelname)-8s %(message)s'))
logger_stderr.addHandler(stderr_handler)
logger_null = logging.getLogger(__name__+'null')
null_handler = logging.NullHandler()
logger_null.addHandler(null_handler)
VERSION = (1, 1, 0)
__version__ = '.'.join(map(str, VERSION[0:3])) + ''.join(VERSION[3:])
class autoOncotator:
def __init__(self, filesize, project, input_dir, output_dir, tsv, maf, num):
self.filesize = filesize
self.project = project
self.input_dir = input_dir
self.output_dir = output_dir
self.tsv = project.replace('.vcf', '.'+tsv)
self.maf = maf
self.num = num
def remove_snp(self, project, input_dir, output_dir):
in_file = os.path.join(input_dir, project)
out_file = os.path.join(output_dir, project.replace('.vcf', '.filter.vcf'))
out = open(out_file, 'w+')
with open(in_file, 'r') as f:
for i in f.readlines():
if i.count('#') == 0:
if i.split(sep='\t')[0].split(sep='chr')[1].isnumeric() == True or i.split(sep='\t')[0].split(sep='chr')[1].endswith('X') == True or i.split(sep='\t')[0].split(sep='chr')[1].endswith('Y') == True:
if i.split(sep='\t')[2].startswith('rs') == False:
out.writelines(i)
out.close()
def vcf2oncotator(self, project, output_dir, tsv):
in_file = os.path.join(output_dir, project.replace('.vcf', '.filter.vcf'))
out_file = os.path.join(os.getcwd(), tsv)
out = open(out_file, 'w+')
with open(in_file, 'r') as f:
for l in f.readlines():
if l.count('#') == 0:
if l.split(sep='\t')[0].split(sep='chr')[1].isnumeric() == True or l.split(sep='\t')[0].split(sep='chr')[1].endswith('X') == True or l.split(sep='\t')[0].split(sep='chr')[1].endswith('Y') == True:
if l.split(sep='\t')[2].startswith('rs') == False:
# SNV
if len(l.split(sep='\t')[3]) == 1 and len(l.split(sep='\t')[4]) == 1:
line = str(l.split(sep='\t')[0].split(sep='chr')[1])+'\t'+str(l.split(sep='\t')[1])+'\t'+str(l.split(sep='\t')[1])+'\t'+str(l.split(sep='\t')[3])+'\t'+str(l.split(sep='\t')[4])+'\n'
out.writelines(line)
# deletion
if len(l.split(sep='\t')[3]) != 1 and len(l.split(sep='\t')[4]) == 1:
line = str(l.split(sep='\t')[0].split(sep='chr')[1])+'\t'+str(int(l.split(sep='\t')[1])+1)+'\t'+str(int(l.split(sep='\t')[1])+len(l.split(sep='\t')[3][1:]))+'\t'+str(l.split(sep='\t')[3][1:])+'\t'+'-'+'\n'
out.writelines(line)
# insertion
if len(l.split(sep='\t')[3]) == 1 and len(l.split(sep='\t')[4]) != 1:
line = str(l.split(sep='\t')[0].split(sep='chr')[1])+'\t'+str(int(l.split(sep='\t')[1]))+'\t'+str(int(l.split(sep='\t')[1])+len(l.split(sep='\t')[4])-1)+'\t'+'-'+'\t'+str(l.split(sep='\t')[4][1:])+'\n'
out.writelines(line)
out.close()
def oncotator(self, filesize, project, output_dir, tsv, maf, num):
fail = []
xpath = '/html/body/div[2]/div[3]/div[1]/a[1]'
output_dir_raw = output_dir
output_dir = os.path.join(output_dir, project.split('.')[0])
if os.path.exists(output_dir) is False:
os.mkdir(output_dir)
profile = webdriver.FirefoxProfile()
profile.set_preference('browser.download.folderList', 2)
profile.set_preference('browser.download.dir', output_dir)
profile.set_preference('browser.download.manager.showWhenStarting', False)
profile.set_preference('browser.helperApps.neverAsk.saveToDisk', 'text/html')
driver = webdriver.Firefox(firefox_profile=profile)
driver.get('http://portals.broadinstitute.org/oncotator/')
driver.find_element_by_id('id_file').send_keys(os.path.join(os.getcwd(), tsv))
driver.find_element_by_name('upload_submit').click()
if int(filesize) > 7500000:
while True:
try:
time.sleep(480)
driver.find_element_by_xpath(xpath).click()
time.sleep(10)
os.system('mv %s %s' %(os.path.join(output_dir, 'oncotator.maf.txt'), os.path.join(output_dir, project.replace('.vcf', '.'+maf))))
os.system('mv %s %s' %(os.path.join(output_dir, project.replace('.vcf', '.'+maf)), output_dir_raw))
break
except:
fail.append(project)
warnings.warn('selenium webdriver error occor: [%s]' %(project))
else:
while True:
try:
time.sleep(240)
driver.find_element_by_xpath(xpath).click()
time.sleep(10)
os.system('mv %s %s' %(os.path.join(output_dir, 'oncotator.maf.txt'), os.path.join(output_dir, project.replace('.vcf', '.'+maf))))
os.system('mv %s %s' %(os.path.join(output_dir, project.replace('.vcf', '.'+maf)), output_dir_raw))
break
except:
fail.append(project)
warnings.warn('selenium webdriver error occor: [%s]' %(project))
driver.close()
os.rmdir(output_dir)
return fail
def work_log(work_data):
startTime = time.time()
p1 = autoOncotator(work_data[0], work_data[1], args.input_dir, args.output_dir, args.tsv, args.maf, work_data[2])
p1.remove_snp(p1.project, p1.input_dir, p1.output_dir)
p1.vcf2oncotator(p1.project, p1.output_dir, p1.tsv)
fail = p1.oncotator(p1.filesize, p1.project, p1.output_dir, p1.tsv, p1.maf, p1.num)
endTime = time.time()
logger_stderr.info('Done for vcf [%s] : %.2f min.' %(work_data[1], (endTime-startTime)/60))
if len(fail) != 0:
logger_stderr.info('Fail for annotation: [%s]' %(args.input_dir))
def pool_handler():
p = Pool(args.pool_size)
p.map(work_log, work)
p.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter, description=dedent("""\
Testing environment: Python 3.6
Inputs:
1. vcf file: Specify the file name with the -v or --vcf argument.
Outputs:
1. filter.vcf file: Remove SNP from the specified vcf file.
2. tsv file: Generate the input format file for Oncotator.
3. maf file: Annotated variants by Oncotator(http://portals.broadinstitute.org/oncotator/) using selenium webdriver.
Usage:
1. List all vcfs in specified directory (e.g. current working directory)
python3 autoOncotator.py -l .
2. for running one vcf file
python3 autoOncotator.py -v test_562.mutect2.vcf
3. for Parallelly running mutiple vcf files (e.g. pool size = 15)
* Notes: 'no space' between vcf files
python3 autoOncotator.py -p 10 -v test_562.mutect2.vcf,test_553.mutect2.vcf,test_546.mutect2.vcf,test_543.mutect2.vcf,test_556.mutect2.vcf,test_544.mutect2.vcf,test_573.mutect2.vcf,test_548.mutect2.vcf,test_579.mutect2.vcf,test_565.mutect2.vcf
"""))
optional = parser._action_groups.pop()
required = parser.add_argument_group('required arguments')
required.add_argument('-v', '--vcf', type=str, help='Variant call file, vcf format.')
optional.add_argument('-i', '--input_dir', type=str, default=os.getcwd(), help='input directory (default: current working directory)')
optional.add_argument('-o', '--output_dir', type=str, default=os.getcwd(), help='output directory (default: current working directory)')
optional.add_argument('-t', '--tsv', type=str, default='tsv', help='Generate the input format file for Oncotator, you can defined the name of the file (default: tsv)')
optional.add_argument('-m', '--maf', type=str, default='oncotator.maf.txt', help='output file name (default: oncotator.maf.txt)')
optional.add_argument('-l', '--list_id', type=str, help='list all vcf files in specified directory')
optional.add_argument('-p', '--pool_size', type=int, default=1, help='Pool size of multi-thread for parallel computing (default: 1)')
optional.add_argument('-V', '--version', action='version', version='%(prog)s ' + __version__)
parser._action_groups.append(optional)
args = parser.parse_args()
if args.vcf is None and args.list_id is None:
parser.print_help()
if args.vcf:
vcfs = str(args.vcf).split(',')
logger_stderr.info('Loading %s...' %(vcfs))
if args.input_dir:
logger_stderr.info('Input directory: [%s]' %(args.input_dir))
if args.output_dir:
logger_stderr.info('Output directory: [%s]' %(args.output_dir))
if args.tsv:
logger_stderr.info('Name of input file for Oncotator: [%s]' %(args.tsv))
if args.maf:
logger_stderr.info('Output file name: [%s]' %(args.maf))
if args.pool_size:
logger_stderr.info('Pool size of multi-thread for parallel computing: [%s]' %(args.pool_size))
os.system('ls -lt %s > project_size.txt' %(args.input_dir))
work = []
project_size = {}
with open('project_size.txt', 'r') as f:
ct = 0
for i in f.readlines():
if i.split()[-1] in vcfs:
work.append([i.split()[4], i.split()[-1], ct])
ct+=1
os.system('rm project_size.txt')
logger_stderr.info('Add to queuing: %s' %(work))
pool_handler()
if args.list_id is not None:
test = [i for i in os.listdir(args.list_id) if '.vcf' in i and '.filter.vcf' not in i]
txt = str()
for i in test:
txt += i+','
logger_stderr.info('list all vcfs in [%s]' %(args.list_id))
logger_stderr.info('Num. of list: [%s]' %(len(test)))
logger_stderr.info('list: [%s]' %(txt[:-1]))