/
AnnotateMuts.py
647 lines (592 loc) · 46.4 KB
/
AnnotateMuts.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
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
'''
Created on Mar 29, 2016
@author: Husen M. Umer
'''
import sys,os
from pybedtools import BedTool
import numpy as np
import math
from collections import Counter
'''The input file should have the following format:
9-col mutation-info
6-col motif-info
1-col entropy diff
1-col snv_pos at motif
4-col element-info
'''
def group_snvs_motifs_with_element_names(snv_motif_element_input_file, colnum_mut_info=1, colnum_motif_info=10, colnum_element_info=18):
snv_motif_element_grouped = '.'.join(snv_motif_element_input_file.split('.')[0:-1])+"_grouped.bed" + str(colnum_motif_info+6+2)
if not os.path.exists(snv_motif_element_grouped):
bedTools_obj = BedTool(snv_motif_element_input_file)
bedTools_obj.groupby(g=range(colnum_mut_info, colnum_element_info), c=colnum_element_info+3, o = ['distinct']).saveas(snv_motif_element_grouped)
return snv_motif_element_grouped
def annotate_snvs_with_elements(snv_motif_element_grouped_input_file, tumor_cells_dict, tumor_cells_tracks_dict, list_of_cell_tracks, assay_type_cell_tracks_dict, motifTFName_TFNames_matches_dict,
snv_motif_element_grouped_output_file="", normal_expression_per_cell_per_TF = {}, tumor_expression_per_cell_per_TF = {},
index_mut_info=0, index_motif_info=9, index_element_info=15, col_names = [], run_training = True, weights_per_param_dict = {}, take_abs_entropy_diff=True, log_base=10, header=True):
index_tumor_name = 5
index_motif_name = 12
overlaps_info_index = 17
sep = '\t'
if snv_motif_element_grouped_output_file=="":
snv_motif_element_grouped_output_file = snv_motif_element_grouped_input_file + '_annotated'
with open(snv_motif_element_grouped_input_file, 'r') as snv_motif_element_grouped_infile:
with open(snv_motif_element_grouped_output_file, 'w') as snv_motif_element_grouped_outfile:
if header:
snv_motif_element_grouped_outfile.write(sep.join(col_names) + '\n')
split_line = snv_motif_element_grouped_infile.readline().strip().split(sep)
overlapping_motifs_same_mutation = []
index_mutations_info = 0; index_mutated_motif_info = 9
index_tf_normal_expr=17; index_tf_tumor_expr=18;
index_same_factor=19;
current_mutation = split_line[index_mutations_info:index_mutated_motif_info]
write_last_mut_lines = False
counter = 0
sum = 0
print("Mutation Annotation has Started")
while len(split_line)>=overlaps_info_index:
"a line every after processing each 100000 lines"
counter+=1
if counter==100000:
sum+=counter
print("#lines processed: " + str(sum) + " from " + snv_motif_element_grouped_input_file + " into " + snv_motif_element_grouped_output_file)
counter = 0
#define and initialize the variables
same_factor = 'NA'
same_cell_same_factor = ['NA']
other_cells_same_factor = ['NA']#for other cell names
other_factors = 'NA'
same_cell_other_factors = ['NA']#names of other factors sames cell
same_cell_other_factors_names = ['NA']
dnase = 'NA'
same_cell_dnase = ['NA']
other_cells_dnase = ['NA']#for other cell names
contactingdomain = 'NA'
same_cell_contactingdomain = ['NA']
other_cells_contactingdomain = ['NA']
loopdomain = 'NA'
same_cell_loopdomain = ['NA']
other_cells_loopdomain = ['NA']
chromatin_states = 'NA'
same_cell_chromatin_states = ['NA']
same_cell_chromatin_states_lables = ['NA']#just for counting
other_cell_chromatin_states = ['NA']
other_cell_chromatin_states_lables = ['NA']#just for counting
replicationdomain = 'NA'
same_cell_replicationdomain = ['NA']
same_cells_replicationdomain_lables = ['NA']#just for counting
other_cells_replicationdomain = ['NA']
other_cells_replicationdomain_lables = ['NA']#just for counting
CAGE_expr = 'NA'
same_cell_CAGE_expr = ['NA']
same_cell_CAGE_expr_values = 'NA'
other_cells_CAGE_expr = ['NA']
other_cells_CAGE_expr_values = 'NA'
normal_expression_level_of_this_motif_from_itsFactors = 'NA'
normal_expression_level_of_this_motif_from_overlappingFactors = ['NA']
tumor_expression_level_of_this_motif_from_itsFactors = 'NA'
tumor_expression_level_of_this_motif_from_overlappingFactors = ['NA']
overlap_track_names = split_line[overlaps_info_index].split(',')#get the track names from the overlapping column which is originally taken from the combined cell chromatin data bed4 file.
tumor_name = split_line[index_tumor_name]#type of tumor which this mutation belong to
factor_name_from_motif_name = split_line[index_motif_name].strip().split('_')[0].upper()
if tumor_name in tumor_cells_dict.keys():#assign the values to 0 when the track was found otherwise let it be NA
cells_of_this_tumor = tumor_cells_dict[tumor_name]#from the TumorCell dict get the matching cells of this tumor
for overlap_track in overlap_track_names:
overlap_track_split = overlap_track.split('#')
cell_name_from_overlap_track = overlap_track_split[0]
assay_type = overlap_track_split[1]
if overlap_track_split[0]+'#'+overlap_track_split[1] in tumor_cells_tracks_dict[tumor_name]:#{'PBCA-DE': ['SK-N-SH#ChromHMM#ChromatinStates', 'SK-N-SH#YY1',...], ...}
#a matching cell is found
#for tracks that have no value just showing in which of the matching cells it is found is sufficient
if assay_type == "DNase-seq":
if same_cell_dnase[0]=='NA':
same_cell_dnase = []
same_cell_dnase.append(cell_name_from_overlap_track)
elif assay_type == "ContactingDomain":
if same_cell_contactingdomain[0]=="NA":
same_cell_contactingdomain=[]
same_cell_contactingdomain.append(cell_name_from_overlap_track)
elif assay_type == "LoopDomain":
if same_cell_loopdomain[0]=="NA":
same_cell_loopdomain=[]
same_cell_loopdomain.append(cell_name_from_overlap_track)
#for those tracks that have a value we need to show their value in each of the matching cells
elif assay_type == "ChromHMM":
if same_cell_chromatin_states[0]=="NA":
same_cell_chromatin_states=[]
same_cell_chromatin_states_lables = []
same_cell_chromatin_states.append(overlap_track)
same_cell_chromatin_states_lables.append(overlap_track_split[2])
elif assay_type == "RepliDomain":
if same_cell_replicationdomain[0]=="NA":
same_cell_replicationdomain=[]
same_cells_replicationdomain_lables = []
same_cell_replicationdomain.append(overlap_track)
same_cells_replicationdomain_lables.append(overlap_track_split[2])
elif assay_type == "FANTOM":
if same_cell_CAGE_expr[0]=='NA':
same_cell_CAGE_expr = []
same_cell_CAGE_expr.append(overlap_track)
if same_cell_CAGE_expr_values=='NA':
same_cell_CAGE_expr_values = 0.0
same_cell_CAGE_expr_values += float(overlap_track_split[2])
else:#ChIP-seq #if assay_type != "ChromHMM" and assay_type != "DNase-seq" and assay_type != "ContactingDomain" and assay_type != "LoopDomain" and assay_type != "FANTOM" and assay_type != "RepliDomain":
if overlap_track.split('#')[1].upper() == factor_name_from_motif_name or overlap_track.split('#')[1].upper() in motifTFName_TFNames_matches_dict[factor_name_from_motif_name]:
#the motif has a matching TF overlap
if same_cell_same_factor[0] == 'NA':
same_cell_same_factor = []
same_cell_same_factor.append(cell_name_from_overlap_track)#save what cells had the signal
else:
if same_cell_other_factors[0]=="NA":
same_cell_other_factors = []
same_cell_other_factors.append(overlap_track.split('#')[1])
if same_cell_other_factors_names[0]=="NA":
same_cell_other_factors_names = []
same_cell_other_factors_names.append(overlap_track)#save what factors have binding at this place
else:#the tracks belong to a non-matching cell
if assay_type == "DNase-seq":
if other_cells_dnase[0]=='NA':
other_cells_dnase = []
other_cells_dnase.append(cell_name_from_overlap_track)
elif assay_type == "ContactingDomain":
if other_cells_contactingdomain[0]=="NA":
other_cells_contactingdomain=[]
other_cells_contactingdomain.append(cell_name_from_overlap_track)
elif assay_type == "LoopDomain":
if other_cells_loopdomain[0]=="NA":
other_cells_loopdomain=[]
other_cells_loopdomain.append(cell_name_from_overlap_track)
elif assay_type == "ChromHMM":
if other_cell_chromatin_states[0]=="NA":
other_cell_chromatin_states=[]
other_cell_chromatin_states_lables = []
other_cell_chromatin_states.append(overlap_track)
other_cell_chromatin_states_lables.append(overlap_track_split[2])
elif assay_type == "RepliDomain":
if other_cells_replicationdomain[0]=="NA":
other_cells_replicationdomain=[]
other_cells_replicationdomain_lables = []
other_cells_replicationdomain.append(overlap_track)
other_cells_replicationdomain_lables.append(overlap_track_split[2])
elif assay_type == "FANTOM":
if other_cells_CAGE_expr[0]=='NA':
other_cells_CAGE_expr = []
other_cells_CAGE_expr.append(overlap_track)
if other_cells_CAGE_expr_values=='NA':
other_cells_CAGE_expr_values = 0.0
other_cells_CAGE_expr_values += float(overlap_track_split[2])
else:#ChIP-seq
if overlap_track.split('#')[1].upper() == factor_name_from_motif_name or overlap_track.split('#')[1].upper() in motifTFName_TFNames_matches_dict[factor_name_from_motif_name]:
#the motif has a matching TF overlap
if other_cells_same_factor[0] == 'NA':
other_cells_same_factor = []
other_cells_same_factor.append(cell_name_from_overlap_track)#save what cells had the signal
#Get expression level for the TF from the matching tumor cell lines
for cell in cells_of_this_tumor:
#Normal gene expression values
if cell+'#'+factor_name_from_motif_name in normal_expression_per_cell_per_TF.keys():
if normal_expression_per_cell_per_TF[cell+'#'+factor_name_from_motif_name]!='NA':
if normal_expression_level_of_this_motif_from_overlappingFactors[0] == 'NA':
normal_expression_level_of_this_motif_from_overlappingFactors = []
normal_expression_level_of_this_motif_from_overlappingFactors.append(normal_expression_per_cell_per_TF[cell+'#'+factor_name_from_motif_name])
else:
for alt_factor in motifTFName_TFNames_matches_dict[factor_name_from_motif_name]:
if cell+'#'+alt_factor in normal_expression_per_cell_per_TF.keys():
if normal_expression_per_cell_per_TF[cell+'#'+alt_factor]!='NA':
if normal_expression_level_of_this_motif_from_overlappingFactors[0] == 'NA':
normal_expression_level_of_this_motif_from_overlappingFactors = []
normal_expression_level_of_this_motif_from_overlappingFactors.append(normal_expression_per_cell_per_TF[cell+'#'+alt_factor])
#Tumor gene expression values
if cell+'#'+factor_name_from_motif_name in tumor_expression_per_cell_per_TF.keys():
if tumor_expression_per_cell_per_TF[cell+'#'+factor_name_from_motif_name]!='NA':
if tumor_expression_level_of_this_motif_from_overlappingFactors[0] == 'NA':
tumor_expression_level_of_this_motif_from_overlappingFactors = []
tumor_expression_level_of_this_motif_from_overlappingFactors.append(tumor_expression_per_cell_per_TF[cell+'#'+factor_name_from_motif_name])
else:
for alt_factor in motifTFName_TFNames_matches_dict[factor_name_from_motif_name]:
if cell+'#'+alt_factor in tumor_expression_per_cell_per_TF.keys():
if tumor_expression_per_cell_per_TF[cell+'#'+alt_factor ]!='NA':
if tumor_expression_level_of_this_motif_from_overlappingFactors[0] == 'NA':
tumor_expression_level_of_this_motif_from_overlappingFactors = []
tumor_expression_level_of_this_motif_from_overlappingFactors.append(tumor_expression_per_cell_per_TF[cell+'#'+alt_factor])
#Get final values for each of the variables
#Normal gene expression
if normal_expression_level_of_this_motif_from_overlappingFactors[0]!='NA':
normal_expression_level_of_this_motif_from_itsFactors = np.mean(normal_expression_level_of_this_motif_from_overlappingFactors)
#Tumor gene expression
if tumor_expression_level_of_this_motif_from_overlappingFactors[0]!='NA':
tumor_expression_level_of_this_motif_from_itsFactors = np.mean(tumor_expression_level_of_this_motif_from_overlappingFactors)
#Same TF
same_tf_chipseq_in_cells_for_this_tumor = 0
for cell in cells_of_this_tumor:
if "ChIP-seq#"+cell in assay_type_cell_tracks_dict.keys():
if factor_name_from_motif_name in assay_type_cell_tracks_dict["ChIP-seq#"+cell]:
same_tf_chipseq_in_cells_for_this_tumor += 1
else:
for alt_factor in motifTFName_TFNames_matches_dict[factor_name_from_motif_name]:
if alt_factor in assay_type_cell_tracks_dict["ChIP-seq#"+cell]:
same_tf_chipseq_in_cells_for_this_tumor += 1
break
same_tf_in_cells_for_this_tumor = 0
if same_cell_same_factor[0]!='NA':
same_factor=1.0
same_tf_in_cells_for_this_tumor = len(set(same_cell_same_factor))
elif other_cells_same_factor[0]!='NA':
if same_tf_chipseq_in_cells_for_this_tumor>0:#if chip-seq for the factor was available for the matching cell but no signal was found then report zero and don't consider the other cells
same_factor = 0
same_cell_same_factor[0] = 'None'
else:#if no chip-seq was available for the factor in the matching cells then check the other cells
same_factor=len(set(other_cells_same_factor))*0.25
if same_factor>1.0:
same_factor=1.0
else: # same_cell_same_factor[0]=='NA' and other_cells_same_factor[0]=='NA':
same_tf_other_cells = False
for assay_name in assay_type_cell_tracks_dict.keys():
if "ChIP-seq#" in assay_name:
if factor_name_from_motif_name in assay_type_cell_tracks_dict[assay_name]:
same_tf_other_cells += 1
else:
for alt_factor in motifTFName_TFNames_matches_dict[factor_name_from_motif_name]:
if alt_factor in assay_type_cell_tracks_dict[assay_name]:
same_tf_other_cells += 1
break
if same_tf_chipseq_in_cells_for_this_tumor>0:#if chip-seq for the factor was available for the matching cell but no signal was found then report zero and don't consider the other cells
same_factor = 0
same_cell_same_factor[0] = 'None'
if same_tf_other_cells-same_tf_chipseq_in_cells_for_this_tumor>0:
other_cells_same_factor[0] = 'None'
same_factor = 0
else:
same_factor = 'NA'
#Other TFs - same cell
if same_cell_other_factors[0]!='NA':
other_factors = len(set(same_cell_other_factors))
'''if len(set(same_cell_other_factors))>=1:
other_factors = 1#len(set(same_cell_other_factors))
else:
other_factors = 0'''
else:
num_tfs_for_this_tumor = 0
for cell in cells_of_this_tumor:
if "ChIP-seq#"+cell in assay_type_cell_tracks_dict.keys():
num_tfs_for_this_tumor+=len(assay_type_cell_tracks_dict["ChIP-seq#"+cell])
if num_tfs_for_this_tumor-same_tf_chipseq_in_cells_for_this_tumor>0:
same_cell_other_factors[0] = 'None'
same_cell_other_factors_names[0] = 'None'
other_factors = 0
else:
other_factors = 'NA'
#DNase1
if same_cell_dnase[0]!='NA':
dnase=1.0
elif other_cells_dnase[0]!='NA':
dnase=len(set(other_cells_dnase))/len(set(assay_type_cell_tracks_dict['DNase-seq']))
if dnase>1.0:
dnase=1.0
else: #if same_cell_dnase[0]=='NA' and other_cells_dnase[0]=='NA':
num_dnase_in_cells_of_this_tumor = 0
num_dnase_in_other_cells = 0
if "DNase-seq" in assay_type_cell_tracks_dict.keys():
for cell in cells_of_this_tumor:
if cell in assay_type_cell_tracks_dict["DNase-seq"]:
num_dnase_in_cells_of_this_tumor+=1
num_dnase_in_other_cells = len(assay_type_cell_tracks_dict["DNase-seq"])-num_dnase_in_cells_of_this_tumor
if num_dnase_in_cells_of_this_tumor>0 or num_dnase_in_other_cells>0:
if num_dnase_in_cells_of_this_tumor>0:
same_cell_dnase[0] = 'None'
dnase = 0
if num_dnase_in_other_cells>0:
other_cells_dnase[0] = 'None'
dnase = 0
else:
dnase = 'NA'
#TAD contacting domains
if same_cell_contactingdomain[0]!='NA':
contactingdomain=1.0
elif other_cells_contactingdomain[0]!='NA':
contactingdomain=len(set(other_cells_contactingdomain))/len(set(assay_type_cell_tracks_dict['ContactingDomain']))
if contactingdomain>1.0:
contactingdomain=1.0
else: #if same_cell_contactingdomain[0]=='NA' and other_cells_contactingdomain[0]=='NA':
num_contactingdomain_in_cells_of_this_tumor = 0
num_contactingdomain_in_other_cells = 0
if "ContactingDomain" in assay_type_cell_tracks_dict.keys():
for cell in cells_of_this_tumor:
if cell in assay_type_cell_tracks_dict["ContactingDomain"]:
num_contactingdomain_in_cells_of_this_tumor+=1
num_contactingdomain_in_other_cells = len(assay_type_cell_tracks_dict["ContactingDomain"])-num_contactingdomain_in_cells_of_this_tumor
if num_contactingdomain_in_cells_of_this_tumor>0 or num_contactingdomain_in_other_cells>0:
if num_contactingdomain_in_cells_of_this_tumor>0:
same_cell_contactingdomain[0] = 'None'
contactingdomain = 0
if num_contactingdomain_in_other_cells>0:
other_cells_contactingdomain[0] = 'None'
contactingdomain = 0
else:
contactingdomain = 'NA'
#TAD Loops
if same_cell_loopdomain[0]!='NA':
loopdomain=1.0
elif other_cells_loopdomain[0]!='NA':
loopdomain=len(set(other_cells_loopdomain))/len(set(assay_type_cell_tracks_dict['LoopDomain']))
if loopdomain>1.0:
loopdomain=1.0
else: #if same_cell_loopdomain[0]=='NA' and other_cells_loopdomain[0]=='NA':
num_loopdomain_in_cells_of_this_tumor = 0
num_loopdomain_in_other_cells = 0
if "LoopDomain" in assay_type_cell_tracks_dict.keys():
for cell in cells_of_this_tumor:
if cell in assay_type_cell_tracks_dict["LoopDomain"]:
num_loopdomain_in_cells_of_this_tumor+=1
num_loopdomain_in_other_cells = len(assay_type_cell_tracks_dict["LoopDomain"])-num_loopdomain_in_cells_of_this_tumor
if num_loopdomain_in_cells_of_this_tumor>0 or num_loopdomain_in_other_cells>0:
if num_loopdomain_in_cells_of_this_tumor>0:
same_cell_loopdomain[0] = 'None'
loopdomain = 0
if num_loopdomain_in_other_cells>0:
other_cells_loopdomain[0] = 'None'
loopdomain = 0
else:
loopdomain = 'NA'
#CAGE
if same_cell_CAGE_expr_values!='NA':
CAGE_expr = same_cell_CAGE_expr_values/(len(same_cell_CAGE_expr)*1.0)
elif other_cells_CAGE_expr_values!='NA':
CAGE_expr= other_cells_CAGE_expr_values/len(set(assay_type_cell_tracks_dict['FANTOM']))
else: #same_cell_CAGE_expr_values=='NA' and other_cells_CAGE_expr_values=='NA':
num_cage_expr_in_cells_of_this_tumor = 0
num_cage_expr_in_other_cells = 0
if "FANTOM" in assay_type_cell_tracks_dict.keys():
for cell in cells_of_this_tumor:
if cell in assay_type_cell_tracks_dict["FANTOM"]:
num_cage_expr_in_cells_of_this_tumor+=1
num_cage_expr_in_other_cells = len(assay_type_cell_tracks_dict["FANTOM"])-num_cage_expr_in_cells_of_this_tumor
if num_cage_expr_in_cells_of_this_tumor>0 or num_cage_expr_in_other_cells>0:
if num_cage_expr_in_cells_of_this_tumor>0:
same_cell_CAGE_expr[0] = 'None'
CAGE_expr = 0
if num_cage_expr_in_other_cells>0:
other_cells_CAGE_expr[0] = 'None'
CAGE_expr = 0
else:
CAGE_expr = 'NA'
#chromHMM
if same_cell_chromatin_states_lables[0]!='NA':
chromatin_states = Counter(same_cell_chromatin_states_lables).most_common(1)[0][0]
elif other_cell_chromatin_states_lables[0]!='NA':
chromatin_states = Counter(other_cell_chromatin_states_lables).most_common(1)[0][0]
else:
num_chromatin_states_in_cells_of_this_tumor = 0
num_chromatin_states_in_other_cells = 0
if "ChromatinStates" in assay_type_cell_tracks_dict.keys():
for cell in cells_of_this_tumor:
if cell in assay_type_cell_tracks_dict["ChromatinStates"]:
num_chromatin_states_in_cells_of_this_tumor+=1
num_chromatin_states_in_other_cells = len(assay_type_cell_tracks_dict["ChromatinStates"])-num_chromatin_states_in_cells_of_this_tumor
if num_chromatin_states_in_cells_of_this_tumor>0 or num_chromatin_states_in_other_cells>0:
if num_chromatin_states_in_cells_of_this_tumor>0:
same_cell_chromatin_states[0] = 'None'
same_cell_chromatin_states_lables[0] = 'None'
chromatin_states = 'None'
if num_chromatin_states_in_other_cells>0:
other_cell_chromatin_states[0] = 'None'
other_cell_chromatin_states_lables[0]='None'
chromatin_states = 'None'
else:
chromatin_states = 'NA'
#Replication domains
if same_cells_replicationdomain_lables[0]!='NA':
replicationdomain = Counter(same_cells_replicationdomain_lables).most_common(1)[0][0]
elif other_cells_replicationdomain_lables[0]!='NA':
replicationdomain = Counter(other_cells_replicationdomain_lables).most_common(1)[0][0]
else:
num_replicationdomain_in_cells_of_this_tumor = 0
num_replicationdomain_in_other_cells = 0
if "RepliDomain" in assay_type_cell_tracks_dict.keys():
for cell in cells_of_this_tumor:
if cell in assay_type_cell_tracks_dict["RepliDomain"]:
num_replicationdomain_in_cells_of_this_tumor+=1
num_replicationdomain_in_other_cells = len(assay_type_cell_tracks_dict["RepliDomain"])-num_replicationdomain_in_cells_of_this_tumor
if num_replicationdomain_in_cells_of_this_tumor>0 or num_replicationdomain_in_other_cells>0:
if num_replicationdomain_in_cells_of_this_tumor>0:
same_cell_replicationdomain[0] = 'None'
same_cells_replicationdomain_lables[0] = 'None'
replicationdomain = 'None'
if num_replicationdomain_in_other_cells>0:
other_cells_replicationdomain[0] = 'None'
other_cells_replicationdomain_lables[0]='None'
replicationdomain = 'None'
else:
replicationdomain = 'NA'
cols_to_use_cobminedcells = [str(same_factor), str(other_factors), str(dnase), str(contactingdomain), str(loopdomain), str(CAGE_expr), chromatin_states, replicationdomain]
#report the calculated variables and entropy_diff,motif name info ,mut info
output_line = ('\t'.join(split_line[index_mut_info: index_mut_info+9]) + sep + sep.join(split_line[index_motif_info: index_motif_info+8])
#TF, gene expr (norml|tumor)
+ sep + str(normal_expression_level_of_this_motif_from_itsFactors)
+ sep + str(tumor_expression_level_of_this_motif_from_itsFactors)
#info for scoring
+ sep + sep.join(cols_to_use_cobminedcells)#same_factor_overlaps + sep + all_factors_same_cell_overlaps + sep + dnase_overlap + sep + contactingdomain_overlap + sep + loopdomain_overlap + sep + chromhmm_overlap_to_report + sep + replicationtiming_label_overlap_to_report + sep + cage_overlap
#overlapping tracks from matching cells (TFBS, DNase1, chromatin state, replication label, contacting domain, loop domain)
+ sep + ','.join(set(same_cell_same_factor))
+ sep + ','.join(set(same_cell_dnase))
+ sep + ','.join(set(same_cell_chromatin_states))
+ sep + ','.join(set(same_cell_replicationdomain))
+ sep + ','.join(set(same_cell_contactingdomain))
+ sep + ','.join(set(same_cell_loopdomain))
+ sep + ','.join(set(same_cell_CAGE_expr))
#Same cell Other factors
+ sep + ','.join(set(same_cell_other_factors_names))
+ sep + ','.join(set(same_cell_other_factors))
#the same factor in other cells
+ sep + ','.join(set(other_cells_same_factor))
#DNase1
+ sep + ','.join(set(other_cells_dnase))
#Chromatin states
+ sep + ','.join(set(other_cell_chromatin_states))
#Replication domains
+ sep + ','.join(set(other_cells_replicationdomain))
#Contacting domains
+ sep + ','.join(set(other_cells_contactingdomain))
#Loop domains
+ sep + ','.join(set(other_cells_loopdomain))
#CAGE
+ sep + ','.join(set(other_cells_CAGE_expr))
+ '\n')
overlap_line = output_line.strip().split(sep)
if overlap_line[index_mutations_info:index_mutated_motif_info] != current_mutation:#when a new mutation is processed write the content of the dict to the output file to save results of the previous mutation (from all the tf families, with one line for each family - only the one with the highest diff_freq)
#scan the dict to determine what to write from the overlapping motifs of this mut
annotated_mutations_output_line = []
if run_training:
annotated_mutations_output_lines = get_overlapping_motifs_to_write(overlapping_motifs_same_mutation, index_same_factor=19, index_other_factors=20, index_dnase=21, index_cage_expr=24)
if len(annotated_mutations_output_lines)>0:
annotated_mutations_output_line = get_one_motif(annotated_mutations_output_lines, index_entropy_diff=15, sep=sep)
else:
annotated_mutations_output_line = filter_and_score_overlapping_motifs_to_write(overlapping_motifs_same_mutation, sep, col_names, weights_per_param_dict, take_abs_entropy_diff=take_abs_entropy_diff, log_base=log_base)
if len(annotated_mutations_output_line)>0:
snv_motif_element_grouped_outfile.write('\n'.join(annotated_mutations_output_line)+'\n')
overlapping_motifs_same_mutation = []
current_mutation = overlap_line[index_mutations_info:index_mutated_motif_info]
if overlap_line not in overlapping_motifs_same_mutation:
overlapping_motifs_same_mutation.append(overlap_line)
line = snv_motif_element_grouped_infile.readline()
split_line = line.strip().split(sep)
if line=="":
write_last_mut_lines = True
#snv_motif_element_grouped_outfile.write(output_line)
if write_last_mut_lines:
annotated_mutations_output_line = []
if run_training:
annotated_mutations_output_lines = get_overlapping_motifs_to_write(overlapping_motifs_same_mutation, index_same_factor=19, index_other_factors=20, index_dnase=21, index_cage_expr=24)
if len(annotated_mutations_output_lines)>0:
annotated_mutations_output_line = get_one_motif(annotated_mutations_output_lines, index_entropy_diff=15, sep=sep)
else:
annotated_mutations_output_line = filter_and_score_overlapping_motifs_to_write(overlapping_motifs_same_mutation, sep, col_names, weights_per_param_dict, take_abs_entropy_diff=take_abs_entropy_diff, log_base=log_base)
if len(annotated_mutations_output_line)>0:
snv_motif_element_grouped_outfile.write('\n'.join(annotated_mutations_output_line) + '\n' )
overlapping_motifs_same_mutation = []
print("Finished the annotation process")
return snv_motif_element_grouped_output_file
"""
Keep motifs that have a direct factor support, if non then keep motifs with indirect support (same factor other cells) and same cell DNase1, if none then keep motifs for which the factor chip-seq has not been performed in any cell but dnase from some cell and other factors from the same cell do exist.
"""
def get_overlapping_motifs_to_write(overlapping_motifs_same_mutation,
index_same_factor=19, index_other_factors=20, index_dnase=21, index_cage_expr=24):
motifs_with_samefactor_support = []
motifs_with_samefactor_othercells_support= []
motifs_with_na_same_factor = []
motifs_with_support_of_no_binding = []
for mut_info in overlapping_motifs_same_mutation:
if mut_info[index_same_factor] == "NA":
motifs_with_na_same_factor.append(mut_info)
elif float(mut_info[index_same_factor])>=1.0:#direct support from the same factor or at least x other cells
motifs_with_samefactor_support.append(mut_info)
elif float(mut_info[index_same_factor]) > 0:#indirect support from the same factor other cells (value is between 0-1)
motifs_with_samefactor_othercells_support.append(mut_info)
elif float(mut_info[index_same_factor]) == 0.0 or mut_info[index_same_factor] == "0":
motifs_with_support_of_no_binding.append(mut_info)
if len(motifs_with_samefactor_support)>0:
return motifs_with_samefactor_support
elif len(motifs_with_samefactor_othercells_support)>0:
return motifs_with_samefactor_othercells_support
elif len(motifs_with_support_of_no_binding)>0:
return motifs_with_support_of_no_binding
elif len(motifs_with_na_same_factor)>0:
return motifs_with_na_same_factor
def get_one_motif(annotated_mutations_output_lines, index_entropy_diff=15, sep='\t'):
index_highest_entropy_diff = 0
highest_entropy_diff = 0
for i in range(0, len(annotated_mutations_output_lines)):
if abs(float(annotated_mutations_output_lines[i][index_entropy_diff])) > highest_entropy_diff:
highest_entropy_diff = abs(float(annotated_mutations_output_lines[i][index_entropy_diff]))
index_highest_entropy_diff = i
return [sep.join(annotated_mutations_output_lines[index_highest_entropy_diff])]
def filter_and_score_overlapping_motifs_to_write(overlapping_motifs_same_mutation, sep, col_names, weights_per_param_dict, index_normal_expression_level_of_this_motif=17, index_tumor_expression_level_of_this_motif=18, index_same_factor=19, index_all_factors_same_cell_overlaps=20, index_dnase=21, index_cage_overlap=24, take_abs_entropy_diff=True, log_base=10):
overlapping_motifs_to_report = []
for mut_motif_info in overlapping_motifs_same_mutation:
Sum_params_with_logit_larger_than_zero = 0.0 #weighted accumulative score
Sum_params_with_logit_smaller_than_zero = 0.0
final_score_is_calculated = False
filter = add_this_line_to_overlapping_motifs_check(mut_motif_info, index_normal_expression_level_of_this_motif, index_tumor_expression_level_of_this_motif, index_same_factor)
if filter:
for param in weights_per_param_dict.keys():
if param in col_names:# and weights_per_param_dict[param]>0:
if mut_motif_info[col_names.index(param)]!="NA" and mut_motif_info[col_names.index(param)]!="None":
if float(mut_motif_info[col_names.index(param)]) != 0:
final_score_is_calculated = True#if the value of this feature is not NA and not zero then use it to calculate its annotation score
if param == 'normal_expression_level_of_this_motif' or param == 'tumor_expression_level_of_this_motif' or param == 'other_factors' or param == 'CAGE_expr':
if float(mut_motif_info[col_names.index(param)]) >=1: #log10 of x is in minus when x<1
try:
new_value = (math.log(float(mut_motif_info[col_names.index(param)]), log_base))
if weights_per_param_dict[param]>0:
new_value*=weights_per_param_dict[param]
Sum_params_with_logit_larger_than_zero+=new_value
else:
Sum_params_with_logit_smaller_than_zero+=new_value
mut_motif_info[col_names.index(param)] = str(new_value)
except ValueError:
continue
elif param == 'Entropy_diff':
new_value = abs(float(mut_motif_info[col_names.index(param)]))
if weights_per_param_dict[param]>0:
new_value*=weights_per_param_dict[param]
Sum_params_with_logit_larger_than_zero+=new_value
else:
Sum_params_with_logit_smaller_than_zero+=new_value
if take_abs_entropy_diff:
#keep the minus to differentiate between motif disruption and enhancement
if float(mut_motif_info[col_names.index(param)])<0:
new_value = new_value*-1
mut_motif_info[col_names.index(param)] = str(new_value)
else:
new_value = float(mut_motif_info[col_names.index(param)])
if weights_per_param_dict[param]>0:
new_value*=weights_per_param_dict[param]
Sum_params_with_logit_larger_than_zero+=new_value
else:
Sum_params_with_logit_smaller_than_zero+=new_value
mut_motif_info[col_names.index(param)] = str(new_value)
if final_score_is_calculated:#in case the final score was computed then write the results out
#use the log_coeffecients and report exp(final score)
Sum_params_with_logit_smaller_than_zero = 0
mut_motif_info.append("{0:.3f}".format(np.exp(Sum_params_with_logit_larger_than_zero)))#+Sum_params_with_logit_smaller_than_zero))#Ignore negative coefficients
overlapping_motifs_to_report.append(sep.join(mut_motif_info))
return overlapping_motifs_to_report
def add_this_line_to_overlapping_motifs_check(mut_motif_info, index_normal_expression_level_of_this_motif, index_tumor_expression_level_of_this_motif, index_same_factor):
tf_expression_check = False
samefactor_check = False
if mut_motif_info[index_normal_expression_level_of_this_motif]!="NA":
if float(mut_motif_info[index_normal_expression_level_of_this_motif])>=1:
tf_expression_check = True
if mut_motif_info[index_tumor_expression_level_of_this_motif]!="NA":
if float(mut_motif_info[index_tumor_expression_level_of_this_motif])>=1:
tf_expression_check = True
if mut_motif_info[index_normal_expression_level_of_this_motif]=="NA" and mut_motif_info[index_tumor_expression_level_of_this_motif]=="NA":
tf_expression_check = True
if mut_motif_info[index_same_factor]!="NA":
if float(mut_motif_info[index_same_factor])>0:
samefactor_check = True
else:
samefactor_check = True
if tf_expression_check and samefactor_check:
return True
else:
return False