-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathReNOVo.py
executable file
·280 lines (234 loc) · 7.57 KB
/
ReNOVo.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
#!/usr/bin/env python
# coding=utf-8
# Authors: Emanuele Bonetti (emanuele.bonetti@ieo.it), Giulia Tini (giulia.tini@ieo.it)
# v. 1.0
# ReNOVo is free non-commercial software.
# Users need to obtain the ANNOVAR licence by themselves.
# Contact the Authors for commercial use.
# modules
import subprocess as sp
import sys, os, argparse, tempfile, shutil
from os.path import isfile, join
def dir_path(string):
if os.path.isdir(string):
return string
else:
raise NotADirectoryError(string)
parser = argparse.ArgumentParser(
description="Given as input a folder containing the VCF or annovar input (AVinput) files, this program applies the Random Forest model of ReNOVo and returns the tabular annovar like files with the classification provided by the model itself."
)
parser.add_argument(
"-p",
"--path",
type=dir_path,
help="the path to VCFs/AVinputs directory",
required=True,
)
parser.add_argument(
"-a",
"--annovar",
type=dir_path,
help="the path to ANNOVAR directory",
required=True,
)
parser.add_argument(
"-d",
"--database",
type=dir_path,
default=None,
help="the path to ANNOVAR database directory, if not defined, annovar/humandb will be used",
)
parser.add_argument(
"-b",
"--build",
type=str,
default="hg19",
help="the reference genome build (hg19 or hg38), default is hg19",
)
parser.add_argument(
"-c",
"--clinvar",
type=str,
default="clinvar_20200316",
help="Clivar database, default is clinvar_20200316",
)
args = parser.parse_args()
# if not defined, use annovar/humandb
if args.database is None:
args.database = f"{args.annovar}/humandb"
scripts_folder = f"{os.path.dirname(__file__)}Scripts/"
# temporary folder creation
if not os.path.exists(args.path + "/temporary"):
os.mkdir(args.path + "/temporary")
# output dir creation
if not os.path.exists(args.path + "/ReNOVo_output"):
os.mkdir(args.path + "/ReNOVo_output")
# Get file list
onlyfiles = [f for f in os.listdir(args.path) if isfile(join(args.path, f))]
welcomeMessage = """=============================================================================
ReNOVo
Interpretation of Pathogenic/Benign for variants using python and R scripts of ReNOVo.
Authors: Emanuele Bonetti (emanuele.bonetti@ieo.it), Giulia Tini (giulia.tini@ieo.it)
v. 1.0
ReNOVo is free non-commercial software.
Users need to obtain the ANNOVAR licence by themselves.
Contact the Authors for commercial use.
=============================================================================
"""
endMessage = """=============================================================================
Thanks for using ReNOVo!
=============================================================================
"""
print(welcomeMessage)
# Check annovar dbs
# DBs: refGene,ensGene,avsnp150,gnomad211_exome,dbnsfp35c,intervar_20180118,clinvar_20200316
print("Cheking all ANNOVAR datasets...")
# change this string to upgrade ANNOVAR datasets
dbs = [
"refGene",
"ensGene",
"avsnp150",
"gnomad211_exome",
"dbnsfp35c",
"intervar_20180118",
args.clinvar,
]
try:
onlyfiles_annovar = [
f for f in os.listdir(args.database) if isfile(join(args.database, f))
]
for db in dbs:
db_str = args.build + "_" + db + ".txt"
if db_str not in onlyfiles_annovar:
# download
command = (
"perl "
+ args.annovar
+ "/annotate_variation.pl -buildver "
+ args.build
+ " -downdb -webfrom annovar "
+ db
+ " "
+ args.database
)
process = sp.Popen(command, shell=True)
process.wait()
else:
print("dataset " + db + " already downloaded... skipping... ")
except IOError:
print("Error: Annovar path is probably wrong: " + args.annovar)
sys.exit()
# Run commands for each file
for file_name in onlyfiles:
command = ""
# change the commands below to upgrade the ANNOVAR datasets
if "vcf" in file_name[-3:]: # check vcf input or AV
command = (
"perl "
+ args.annovar
+ "/table_annovar.pl "
+ args.path
+ "/"
+ file_name
+ " "
+ args.database
+ " --buildver "
+ args.build
+ " --out "
+ args.path
+ "/temporary/"
+ file_name
+ " --protocol refGene,ensGene,avsnp150,gnomad211_exome,dbnsfp35c,intervar_20180118," + args.clinvar + " --remove --operation g,g,f,f,f,f,f --nastring . --vcfinput"
)
print("VCF file! Launching ANNOVAR!\n")
elif "avinput" in file_name:
command = (
"perl "
+ args.annovar
+ "/table_annovar.pl "
+ args.path
+ "/"
+ file_name
+ " "
+ args.database
+ " --buildver "
+ args.build
+ " --out "
+ args.path
+ "/temporary/"
+ file_name
+ " --protocol refGene,ensGene,avsnp150,gnomad211_exome,dbnsfp35c,intervar_20180118," + args.clinvar + " --remove --operation g,g,f,f,f,f,f --nastring . --vcfinput"
)
print("avinput file! Launching ANNOVAR!\n")
else:
print(file_name + " is not a VCF or av input... skipped...")
continue
# Running Annovar!
print("ANNOVAR Command: " + command + "\n")
process = sp.Popen(command, shell=True)
process.wait()
# Running preprocessing.R!
print("Preprocessing... NA imputation and columns names changing... ")
path_to_an_out = (
args.path + "/temporary/" + file_name + "." + args.build + "_multianno.txt"
)
path_to_temp = args.path + "/temporary/"
try:
os.listdir(scripts_folder)
except OSError:
print(
"Error: check scripts folder, if you want to run ReNOVo from any position change the scripts path with the full path"
)
sys.exit()
command = (
"Rscript "
+ scripts_folder
+ "preprocessing.R "
+ path_to_an_out
+ " "
+ path_to_temp
)
process = sp.Popen(command, shell=True)
process.wait()
print("DONE!\n")
# Running Renovo_implementation.py!
print("ReNOVo implementation... ")
path_to_Renovo_implementation_input = args.path + "/temporary/input_RF.tab"
output_file_name = (
args.path
+ "/ReNOVo_output/"
+ file_name.strip().split(".")[0]
+ "_ReNOVo_and_ANNOVAR_implemented.txt"
)
command = (
"python "
+ scripts_folder
+ "Renovo_implementation.py "
+ path_to_Renovo_implementation_input
+ " "
+ path_to_an_out
+ " "
+ output_file_name
)
process = sp.Popen(command, shell=True)
process.wait()
print("DONE!\n")
# output
print("Output generated! in " + output_file_name + "\n")
# remove temporary directory (and all its content)
dir_to_remove = args.path + "/temporary/"
try:
shutil.rmtree(dir_to_remove)
except OSError as e:
print("Error: %s : %s" % (dir_to_remove, e.strerror))
print("removed " + dir_to_remove + "...")
# remove renovo output directory (if empty: program crash o empty input directory)
dir_to_remove = args.path + "/ReNOVo_output/"
if len(os.listdir(dir_to_remove)) == 0:
try:
shutil.rmtree(dir_to_remove)
except OSError as e:
print("Error: %s : %s" % (dir_to_remove, e.strerror))
print("removed " + dir_to_remove + "...")
# endMessage
print(endMessage)