-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsvannotate.py
executable file
·254 lines (225 loc) · 7.29 KB
/
svannotate.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
@Description: Generate human-readable interpretations of structural variants.
@author: [email protected]
"""
import os
import sys
import requests
import pandas as pd
import numpy as np
import logging
import StringIO
import argparse
import multiprocessing as mp
import contextlib
from main.constants import *
from main.models import sv, build_cache
from main.annotation import get_variant_annotation
from main.notes import get_notes
from main.models import timestamp
import traceback
#suppress pandas copy warning
pd.options.mode.chained_assignment = None
# Global Variables from constants
oncoKb = OncoKb_known_fusions
refFlat = refFlat_canonical
tumourSuppressor = IMPACT_TumourSuppressors
hotspot = IMPACT_Hotspots
cache = None
VERBOSE = False
def main():
parser = argparse.ArgumentParser(description="SV annotator")
analysis_type = parser.add_mutually_exclusive_group(required=True)
# single sv processing
analysis_type.add_argument(
"-sv",
"--structural_variant",
type=str,
action="store",
help="Structural variant string",
)
# batch processing
analysis_type.add_argument(
"-i",
"--input_file",
type=argparse.FileType("r"),
action="store",
help="input file of multiple structural variants",
)
parser.add_argument(
"-o",
"--out_file",
type=str,
# type=argparse.FileType("w"),
action="store",
default=os.path.join(os.getcwd(), "SVannotated_results.txt"),
help="output file to print annotation results for multiple structural variants",
)
parser.add_argument(
"-of", "--offline", action="store_true", help="run annotation using cache"
)
parser.add_argument(
"-v", "--verbose", action="store_true", help="Print detailed exceptions"
)
args = parser.parse_args()
# Set verbose if required
global VERBOSE
if args.verbose:
global VERBOSE
VERBOSE = True
# Create the logger
logger = logging.getLogger("basic_logger")
logger.setLevel(logging.DEBUG)
# Setup the console handler with a StringIO object
log_capture_string = StringIO.StringIO()
ch = logging.StreamHandler(log_capture_string)
ch.setLevel(logging.DEBUG)
# Add the console handler to the logger
logger.addHandler(ch)
# Call main function
if args.input_file:
sv_all_data = pd.read_csv(args.input_file, header="infer", sep="\t", dtype=str)
svdata = sv_all_data[
[
"TumorId",
"NormalId",
"Chr1",
"Pos1",
"Chr2",
"Pos2",
"SV_Type",
"Gene1",
"Gene2",
"Site1Description",
"Site2Description",
"Fusion",
]
]
cache_input_data = pd.concat(
[
svdata[["Chr1", "Pos1"]].rename(
columns={"Chr1": "#CHROM", "Pos1": "POS"}
),
svdata[["Chr2", "Pos2"]].rename(
columns={"Chr2": "#CHROM", "Pos2": "POS"}
),
],
ignore_index=True,
axis=0,
)
cache_input_data.drop_duplicates(inplace=True)
cache_input_data["ID"], cache_input_data["REF"], cache_input_data[
"ALT"
] = ".,N,-".split(",")
try:
print(timestamp() + "Proceeding to build annotation cache...")
global cache
cache = build_cache(
cache_input_data[["#CHROM", "POS", "ID", "REF", "ALT"]],
transcript_reference,
VERBOSE,
)
except ValueError:
print(timestamp() + "Input SV data appears to be empty. Annotation process will exit.")
print(traceback.format_exc())
except Exception as e:
print(timestamp() + "Failed to build annotation cache using VEP.")
print(traceback.format_exc())
svdata["coord1"] = svdata["Chr1"] + ":" + svdata["Pos1"]
svdata["coord2"] = svdata["Chr2"] + ":" + svdata["Pos2"]
svdata["Genes"] = svdata["Gene1"] + " / " + svdata["Gene2"]
svdata["SV_Type"] = svdata["SV_Type"].apply(
lambda x: "INVERSION"
if x == "INV"
else (
"TRANSLOCATION"
if x == "TRA"
else ("DELETION" if x == "DEL" else "DUPLICATION")
)
)
svdata = svdata.drop(["Chr1", "Chr2", "Pos1", "Pos2"], axis=1)
col = [
u"SV_Type",
u"coord1",
u"coord2",
u"Genes",
u"Site1Description",
u"Site2Description",
u"Fusion",
]
sv_strings = (
svdata[col]
.apply(lambda row: ",".join(row.values.astype(str)), axis=1)
.tolist()
)
cores = int(mp.cpu_count() - 1)
try:
# create jobs
print(timestamp() + "Starting variant annotation...")
with contextlib.closing(mp.Pool(processes=cores)) as pool:
annotated_SVs = pool.map(annotate_SV, sv_strings)
except Exception as e:
print(e)
new = pd.concat(
[
sv_all_data,
pd.DataFrame(
annotated_SVs,
columns=["Note", "Annotation", "Position", "oncokb_sv_type"],
),
],
axis=1,
)
new.to_csv(args.out_file, header=True, sep="\t", index=False)
print(timestamp() + "Completed variant annotation!")
# note, annotation, position = annotate_SV(args.sv, logger)
# Send log contents to a string and close the stream
# log_contents = log_capture_string.getvalue()
# log_capture_string.close()
# if note and position and annotation:
# status = "SUCCESS"
# else:
# status = "FAILED"
# result = {
# "Note": note,
# "Annotation": annotation,
# "Position": position,
# "status": status,
# "message": log_contents.replace("\n", "; "),
# }
# print(result)
def annotate_SV(raw):
"""
Main function to initialize sv and breakpoints
objects based on given inputs and call methods to
generate annotation and notes.
str -> tuple
"""
note, annotation, position, oncokb_sv_type = [None] * 4
try:
svtype, bkp1, bkp2, genes, site1, site2, description = raw.split(",")
except ValueError:
return note, annotation, position, oncokb_sv_type
try:
variant = sv(svtype, bkp1, bkp2, genes, site1, site2, description)
variant.expand(
transcript_reference,
kinase_annotation,
hotspot,
tumourSuppressor,
oncoKb,
cache,
)
annotation = get_variant_annotation(variant)
annotation, note, position, oncokb_sv_type = get_notes(
variant, refFlat_summary, kinase_annotation
)
except Exception as e:
if VERBOSE:
print(raw + "\n" + traceback.format_exc(e))
return note, annotation, position, oncokb_sv_type
return note, annotation, position, oncokb_sv_type
if __name__ == "__main__":
main()