-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathannotation.py
More file actions
executable file
·603 lines (504 loc) · 21.2 KB
/
annotation.py
File metadata and controls
executable file
·603 lines (504 loc) · 21.2 KB
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#!/usr/bin/env python
import os
import sys
import logging
import argparse
import pandas as pd
import numpy as np
import re
import subprocess
from tabulate import tabulate
from misc import check_create_dir, execute_subprocess
from pandarallel import pandarallel
logger = logging.getLogger()
##Import files containing annotation info and convert them to dictionary
#script_dir = os.path.dirname(os.path.realpath(__file__))
def tsv_to_vcf(tsv_file):
pandarallel.initialize()
df = pd.read_csv(tsv_file, sep="\t")
is_empty = df.shape[0] == 0
#df.insert(2, 'ID', '.')
df.fillna(".", inplace=True)
df["PASS"].replace({True: 'PASS'}, inplace=True)
df.rename(columns={"REGION": "#CHROM", "GFF_FEATURE": "ID", "ALT_QUAL": "QUAL", "PASS": "FILTER"}, inplace=True)
fial_columns = ['#CHROM', 'POS', 'ID', 'REF', 'ALT','QUAL', 'FILTER', 'INFO']
if not is_empty:
df['INFO'] = df.parallel_apply(lambda x: "CODON={}-{};AA={}-{};DP={};ALT_FREQ={:.2f}".format(x.REF_CODON, x.ALT_CODON, x.REF_AA, x.ALT_AA, x.TOTAL_DP, x.ALT_FREQ), axis=1)
else:
df = df.reindex(columns = fial_columns)
df = df[fial_columns]
return df
def snpeff_execution(vcf_file, annot_file, database=False):
df_vcf = pd.read_csv(vcf_file, sep="\t")
if df_vcf.shape[0] != 0:
cmd = ["snpEff", "-noStats", database, vcf_file]
with open(annot_file, "w+") as outfile:
#calculate coverage and save it in th eoutput file
subprocess.run(cmd,
stdout=outfile, stderr=subprocess.PIPE, check=True, universal_newlines=True)
else:
with open(annot_file, "w+") as outfile:
outfile.write('No annotation found')
def import_annot_to_pandas(vcf_file, sep='\t'):
"""
Order several annoattion by:
Putative impact: Effects having higher putative impact are first.
Effect type: Effects assumed to be more deleterious effects first.
Canonical transcript before non-canonical.
Marker genomic coordinates (e.g. genes starting before first)
https://pcingola.github.io/SnpEff/se_inputoutput/
Parse vcf outputted by snpEFF which adds the ANN field
Dependences: calculate_ALT_AD
calculate_true_ALT
"""
pandarallel.initialize()
header_lines = 0
with open(vcf_file) as f:
first_line = f.readline().strip()
if first_line == 'No annotation found':
return pd.read_csv(vcf_file, sep=sep)
next_line = f.readline().strip()
while next_line.startswith("##"):
header_lines = header_lines + 1
#logger.info(next_line)
next_line = f.readline()
#Use first line as header
df = pd.read_csv(vcf_file, sep=sep, skiprows=[header_lines], header=header_lines)
ann_headers = ['Allele',
'Annotation',
'Annotation_Impact',
'Gene_Name',
'Gene_ID',
'Feature_Type',
'Feature_ID',
'Transcript_BioType',
'Rank',
'HGVS.c',
'HGVS.p',
'cDNA.pos / cDNA.length',
'CDS.pos / CDS.length',
'AA.pos / AA.length',
'ERRORS / WARNINGS / INFO']
anlelle_headers = ['Codon_change', 'AA_change', 'DP', 'ALT_FREQ']
#Apply function to split and recover the first 15 fields = only first anotations, the most likely
df[anlelle_headers] = df.parallel_apply(lambda x: x.INFO.split(';')[0:4], axis=1, result_type="expand")
for head in anlelle_headers:
df[head] = df[head].str.split("=").str[-1]
df['TMP_ANN_16'] = df['INFO'].parallel_apply(lambda x: ('|').join(x.split('|')[0:15]))
df.INFO = df.INFO.str.split("ANN=").str[-1]
df = df.join(df.pop('INFO')
.str.strip(',')
.str.split(',', expand=True)
.stack()
.reset_index(level=1, drop=True)
.rename('INFO')).reset_index(drop=True)
df['TMP_ANN_16'] = df['INFO'].parallel_apply(lambda x: ('|').join(x.split('|')[0:15]))
df[ann_headers] = df['TMP_ANN_16'].str.split('|', expand=True)
df['HGVS.c'] = df['HGVS.c'].str.split(".").str[-1]
df['HGVS.p'] = df['HGVS.p'].str.split(".").str[-1].replace('', '-')
df.drop(["INFO", "TMP_ANN_16"], inplace = True, axis = 1)
return df
def annotate_snpeff(input_tsv_file, output_vcf_file, output_annot_file, database='NC_045512.2'):
vcf_df = tsv_to_vcf(input_tsv_file)
vcf_df.to_csv(output_vcf_file, sep="\t", index=False)
#Execure snpEff
snpeff_execution(output_vcf_file, output_annot_file, database=database)
#Format annot vcf and remove vcf
annot_df = import_annot_to_pandas(output_annot_file)
annot_df.to_csv(output_annot_file, sep="\t", index=False)
os.remove(output_vcf_file)
def annotate_pangolin(input_file, output_folder, output_filename, threads=8, max_ambig=0.6):
cmd = ["pangolin", input_file, "--outdir", output_folder, "--outfile", output_filename, "--threads", str(threads), "--max-ambig", str(max_ambig)]
execute_subprocess(cmd)
return 'pangolin executed in sample {}'.format(input_file)
def get_reverse(nucleotyde):
nucleotyde = str(nucleotyde)
nucleotyde_rev = {'A' : 'T',
'T' : 'A',
'C' : 'G',
'G': 'C'}
if len(nucleotyde) > 1:
nucleotyde_str = nucleotyde[::-1] #Reverse nucleotide
nucleotyde_str_fin = "".join([nucleotyde_rev[x] for x in nucleotyde_str]) #Complement nucleotide
return nucleotyde_str_fin
else:
return nucleotyde_rev[nucleotyde]
def import_VCF_to_pandas(vcf_file):
header_lines = 0
with open(vcf_file) as f:
first_line = f.readline().strip()
next_line = f.readline().strip()
while next_line.startswith("##"):
header_lines = header_lines + 1
#logger.info(next_line)
next_line = f.readline()
if first_line.startswith('##'):
df = pd.read_csv(vcf_file, sep='\t', skiprows=[header_lines], header=header_lines)
df['ALT']=df['ALT'].str.upper()
df['REF']=df['REF'].str.upper()
#Check INFO
if 'INFO' in df.columns:
return df
else:
last_column = df.columns[-1]
df = df.rename(columns={last_column: 'INFO'})
return df
else:
logger.info("This vcf file is not properly formatted")
sys.exit(1)
def annotate_vcfs(tsv_df, vcfs):
df = pd.read_csv(tsv_df, sep="\t")
for vcf in vcfs:
logger.info("ANNOTATING VCF: {}".format(vcf))
header = (".").join(vcf.split("/")[-1].split(".")[0:-1])
dfvcf = import_VCF_to_pandas(vcf)
dfvcf = dfvcf[['POS', 'REF', 'ALT', 'INFO']]
dfvcf = dfvcf.rename(columns={'INFO': header})
df = df.merge(dfvcf, how='left')
return df
def bed_to_df(bed_file):
"""
Import bed file separated by tabs into a pandas df
-Handle header line
-Handle with and without description (If there is no description adds true or false to annotated df)
"""
header_lines = 0
#Handle likely header by checking colums 2 and 3 as numbers
with open(bed_file, 'r') as f:
next_line = f.readline().strip()
line_split = next_line.split(None) #This split by any blank character
start = line_split[1]
end = line_split[2]
while not start.isdigit() and not end.isdigit():
header_lines = header_lines + 1
next_line = f.readline().strip()
line_split = next_line.split(None) #This split by any blank character
start = line_split[1]
end = line_split[2]
if header_lines == 0:
df = pd.read_csv(bed_file, sep="\t", header=None) #delim_whitespace=True
else:
df = pd.read_csv(bed_file, sep="\t", skiprows=header_lines, header=None) #delim_whitespace=True
df = df.iloc[:,0:4]
df.columns = ["#CHROM", "start", "end", "description"]
return df
def add_bed_info(bed_df, position):
"""
Identify a position within a range
credits: https://stackoverflow.com/questions/6053974/python-efficiently-check-if-integer-is-within-many-ranges
"""
#dict_position = bed_to_dict(bed_file)
if any(start <= position <= end for (start, end) in zip(bed_df.start.values.tolist(), bed_df.end.values.tolist())):
description_out = bed_df.description[(bed_df.start <= position) & (bed_df.end >= position)].values[0]
return description_out
else:
return None
def annotate_bed_s(tsv_df, bed_files):
pandarallel.initialize()
with open(tsv_df, 'r') as f:
content = f.read().strip()
if content == 'No annotation found':
return pd.DataFrame(columns=['POS', 'REF', 'ALT', 'INFO'])
else:
df = pd.read_csv(tsv_df, sep="\t")
variable_list = [ x.split("/")[-1].split(".")[0] for x in bed_files] #extract file name and use it as header
for variable_name, bed_file in zip(variable_list,bed_files):
logger.info("ANNOTATING BED: {}".format(bed_file))
bed_annot_df = bed_to_df(bed_file)
df[variable_name] = df['POS'].parallel_apply(lambda x: add_bed_info(bed_annot_df,x))
return df
def user_annotation(tsv_file, output_file, vcf_files=[], bed_files=[]):
bed_df = annotate_bed_s(tsv_file, bed_files)
vcf_df = annotate_vcfs(tsv_file, vcf_files)
df = bed_df.merge(vcf_df)
df.to_csv(output_file, sep="\t", index=False)
def checkAA(snpEffRow, dfAnnot):
df = dfAnnot
df['aaAnnot'] = df['aa'] + ":" + df['annot']
presence_list = [annot in snpEffRow for annot in dfAnnot.aa]
annotation_list = np.array(df.aaAnnot.tolist())
return (',').join(annotation_list[np.array(presence_list)])
def annotate_aas(annot_file, aas):
pandarallel.initialize()
df = pd.read_csv(annot_file, sep="\t")
for aa in aas:
header = (".").join(aa.split("/")[-1].split(".")[0:-1])
dfaa = pd.read_csv(aa, sep="\t", names=['aa', 'annot'])
if not header in df.columns:
logger.info("ANNOTATING AA: {}".format(aa))
df[header] = df.parallel_apply(lambda x: checkAA(x['HGVS.p'], dfaa), axis=1)
else:
logger.info("SKIPPED AA: {}".format(aa))
return df
def user_annotation_aa(annot_file, output_file, aa_files=[]):
with open(annot_file, 'r') as f:
content = f.read().strip()
if content == 'No annotation found':
logger.debug("{} file has NO Annotation".format(annot_file))
with open(output_file, 'w+') as fout:
fout.write('No annotation found')
else:
df = annotate_aas(annot_file, aa_files)
# Filter SNPEff output with aa annotations
df.drop_duplicates(subset=['HGVS.p'], keep='first', inplace=True) #There may be 1+ calls in the same position due to diferents reference ID genes. Useful for the -A flag.
df.to_csv(output_file, sep="\t", index=False)
html_template = """
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css" rel="stylesheet" type="text/css" />
<link href="https://nightly.datatables.net/css/dataTables.bootstrap4.css" rel="stylesheet" type="text/css" />
<script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
<script src="https://nightly.datatables.net/js/dataTables.bootstrap4.js"></script>
<style>
body {
font: 90%/1rem "Helvetica Neue", HelveticaNeue, Verdana, Arial, Helvetica, sans-serif;
margin: 0;
padding: 0;
color: #333;
background-color: #fff;
}
</style>
<meta charset=utf-8 />
<title>COVID Variant report</title>
<meta name="description" content="https://github.com/pedroscampoy/covid_multianalysis">
<meta name="author" content="pedroscampoy@gmail.com">
</head>
<body>
<div class="container-fluid">
TABLESUMMARY
</div>
<script>
$(document).ready( function () {
var table = $('#variants').DataTable({
orderCellsTop: true,
initComplete: function () {
this.api().columns([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]).every( function () {
var column = this;
var select = $('<select><option value=""></option></select>')
.appendTo( $('thead tr:eq(1) th:eq(' + this.index() + ')') )
.on( 'change', function () {
var val = $.fn.dataTable.util.escapeRegex(
$(this).val()
);
column
.search( val ? '^'+val+'$' : '', true, false )
.draw();
} );
column.data().unique().sort().each( function ( d, j ) {
select.append( '<option value="'+d+'">'+d+'</option>' )
} );
} );
}
});
} );
</script>
</body>
</html>
"""
report_samples_html = """
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-3.3.1.min.js"></script>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.css"
rel="stylesheet"
type="text/css"
/>
<link
href="https://nightly.datatables.net/css/dataTables.bootstrap4.css"
rel="stylesheet"
type="text/css"
/>
<script src="https://nightly.datatables.net/js/jquery.dataTables.js"></script>
<script src="https://nightly.datatables.net/js/dataTables.bootstrap4.js"></script>
<style>
html {
height: 100%;
}
body {
font: 90%/1rem "Helvetica Neue", HelveticaNeue, Verdana, Arial,
Helvetica, sans-serif;
margin: 0;
padding: 0;
color: #333;
background-color: #fff;
height: 100%;
}
.dropdown {
margin: 20px;
}
.dropdown-menu {
max-height: 20rem;
overflow-y: auto;
}
object {
width: 100%;
height: 100%;
}
</style>
<meta charset="utf-8" />
<title>COVID Variant report</title>
<meta
name="description"
content="https://github.com/pedroscampoy/covid_multianalysis"
/>
<meta name="author" content="pedroscampoy@gmail.com" />
</head>
<body>
<div class="dropdown">
<button
class="btn btn-secondary dropdown-toggle"
type="button"
id="dropdown_samples"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false"
>
Sample
</button>
<div id="menu" class="dropdown-menu" aria-labelledby="dropdown_samples">
<form class="px-4 py-2">
<input
type="search"
class="form-control"
id="searchSample"
placeholder="20000000"
autofocus="autofocus"
/>
</form>
<div id="menuItems"></div>
<div id="empty" class="dropdown-header">No samples found</div>
</div>
</div>
<div class="container-fluid w-100 h-100 mh-100" id="display-table">
</div>
<script>
$(document).ready(function () {
var table = $("#variants").DataTable({
orderCellsTop: true,
initComplete: function () {
this.api()
.columns([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
.every(function () {
var column = this;
var select = $('<select><option value=""></option></select>')
.appendTo($("thead tr:eq(1) th:eq(" + this.index() + ")"))
.on("change", function () {
var val = $.fn.dataTable.util.escapeRegex($(this).val());
column
.search(val ? "^" + val + "$" : "", true, false)
.draw();
});
column
.data()
.unique()
.sort()
.each(function (d, j) {
select.append(
'<option value="' + d + '">' + d + "</option>"
);
});
});
},
});
});
//https://stackoverflow.com/questions/45007712/bootstrap-4-dropdown-with-search
//Initialize with the list of symbols
let names = ["ALLSAMPLES"];
//Find the input search box
let search = document.getElementById("searchSample");
//Find every item inside the dropdown
let items = document.getElementsByClassName("dropdown-item");
buildDropDown = (values) => {
let contents = [];
for (let name of values) {
contents.push(
'<input type="button" class="dropdown-item" type="button" value="' +
name +
'"/>'
);
}
$("#menuItems").append(contents.join(""));
//Hide the row that shows no items were found
$("#empty").hide();
}
//Capture the event when user types into the search box
window.addEventListener("input", () => filter(search.value.trim().toLowerCase()));
//For every word entered by the user, check if the symbol starts with that word
//If it does show the symbol, else hide it
function filter(word) {
let length = items.length;
let collection = [];
let hidden = 0;
for (let i = 0; i < length; i++) {
if (items[i].value.toLowerCase().includes(word)) {
$(items[i]).show();
} else {
$(items[i]).hide();
hidden++;
}
}
//If all items are hidden, show the empty view
if (hidden === length) {
$("#empty").show();
} else {
$("#empty").hide();
}
}
//If the user clicks on any item, set the title of the button as the text of the item
$("#menuItems").on("click", ".dropdown-item", function () {
$("#dropdown_samples").text($(this)[0].value);
$("#dropdown_samples").dropdown("toggle");
document.getElementById("display-table").innerHTML=`<object type="text/html" data="${$(this)[0].value}.html" ></object>`;
});
buildDropDown(names);
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-beta.2/js/bootstrap.bundle.min.js"></script>
</body>
</html>
"""
def annotation_to_html(file_annot, sample):
pandarallel.initialize()
folder = ('/').join(file_annot.split('/')[0:-1])
logger.debug('Adapting html in sample: {}'.format(sample))
with open(file_annot, 'r') as f:
content = f.read().strip()
if content == "No annotation found":
logger.debug("{} file has NO Annotation".format(file_annot))
# with open(os.path.join(folder, sample + .html), 'w+') as fout:
# fout.write('No annotation found')
else:
df = pd.read_csv(file_annot, sep="\t", dtype=str)
df['ALT_FREQ'] = df['ALT_FREQ'].astype(float)
df['POS'] = df['POS'].astype(int)
logger.debug('read csv {}'.format(file_annot))
#dtype={"user_id": int, "username": "string"}
df = df [['#CHROM', 'POS', 'REF', 'ALT', 'Codon_change',
'AA_change', 'DP', 'ALT_FREQ', 'Annotation',
'Annotation_Impact', 'Gene_Name', 'HGVS.p'] + df.columns[26:].tolist()]
if 'Variants' in df.columns:
df = df.drop('Variants', axis=1)
if 'DVariant' in df.columns:
df = df.drop('DVariant', axis=1)
df = df.drop_duplicates(subset=['#CHROM', 'POS', 'REF', 'ALT'], keep="first")
df = df[df.ALT_FREQ >= 0.2]
handle_aa = lambda x: None if x != x else x.split(':')[1]
df.iloc[:,12:] = df.iloc[:,12:].parallel_applymap(handle_aa)
df = pd.melt(df, id_vars=['#CHROM', 'POS', 'REF', 'ALT', 'Codon_change', 'AA_change', 'DP',
'ALT_FREQ', 'Annotation', 'Annotation_Impact', 'Gene_Name', 'HGVS.p'], value_vars=df.columns[12:].tolist())
if 'variable' in df.columns:
df = df.drop('variable', axis=1)
df = df.rename(columns={'value': 'variable'})
table = tabulate(df, headers='keys', tablefmt='html', showindex=False)
table = table.replace("<table>", "<table id=\"variants\" class=\"table table-striped table-bordered nowrap\" width=\"100%\">")
table = table.replace("style=\"text-align: right;\"", "")
row_filter = "<tr>\n" + "<th></th>\n" * len(df.columns) + "</tr>\n"
table = table.replace("</tr>\n</thead>", "</tr>\n" + row_filter + "</thead>")
final_html = html_template.replace('TABLESUMMARY', table)
with open(os.path.join(folder, sample + ".html"), 'w+') as f:
f.write(final_html)
if __name__ == '__main__':
logger.info("#################### ANNOTATION #########################")