-
Notifications
You must be signed in to change notification settings - Fork 1
/
projection_score.R
1182 lines (879 loc) · 41.6 KB
/
projection_score.R
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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
library(abind)
source("preprocess_windows.R")
source("assess_panel.R")
source("efficient_panel_sbs.R")
#GLOBAL_OUT_DIR = "~/projects/hotspot_signature_panel/out/"
#GLOBAL_PANEL_DIR = paste0(GLOBAL_OUT_DIR, "SIGNATURE_PANELS/")
#GLOBAL_PANEL_SBS_DIR = paste0(GLOBAL_PANEL_DIR, "SBS_MATRICES/")
#GLOBAL_PANEL_SIG_EST_DIR = paste0(GLOBAL_PANEL_DIR, "SIGNATURE_ESTIMATES/")
#GLOBAL_LOGFILE_DIR = paste0(GLOBAL_OUT_DIR, "LOG_FILES/")
source("GLOBAL_CONFIG.R")
library(doParallel)
registerDoParallel(cores=GLOBAL_NCORES)
# given a training set, find windows of the genome which maximize this objective function:
# SUM_(over active samples) alpha_r - SUM_(over inactive samples) alpha_r
# to do this what needs to happen?
# 1) compute alpha_r for all signatures, regions, and samples
# a) make an SBS count vector for each region & sample,
# b) compute the scalar projection of this vector with each signature.
# c) save these somehow
# 2) mark samples with their signature activity
# assess_panel.R function get_sig_activity_labels() does this
# 3) given train and test sets, find the regions which maximize the objective function on the training set
# a) sum the alpha_r's across positive patients, sum alpha_r's across negative patients, take difference
# 4) evaluate AUROC on test set
###################
# loading data #
###################
# load outputs of 1a)
load_sbs_array <- function(chrom, win_size=10^5) {
return(readRDS(paste0(GLOBAL_CHR_MTX_DIR, "samp_window_sbs_array_winsize", win_size, "_chr", chrom, ".rds")))
}
# load outputs of 1b & c)
# old
load_ps_score_mtx_ls <- function(sig_num, mode="100k") {
if ((mode != "100k") & (mode != "10k")) {
stop("Bad mode argument given to load_ps_score_mtx_ls(). Should be \"100k\" or \"10k\"")
}
if (mode=="100k") {
return(load_100k_ps_score_mtx_ls(sig_num))
}
if (mode=="10k") {
return(load_10k_ps_score_mtx_ls(sig_num))
}
}
load_10k_ps_score_mtx_ls <- function(sig_num) {
return(readRDS(paste0(GLOBAL_CHR_MTX_DIR, "10k_proj_scores/projection_score_mtx_ls_sig", sig_num, ".rds")))
}
load_100k_ps_score_mtx_ls <- function(sig_num) {
return(readRDS(paste0(GLOBAL_CHR_MTX_DIR, "100k_proj_scores/projection_score_mtx_ls_sig", sig_num, ".rds")))
}
load_combined_100k_arr <- function() {
filename = paste0(GLOBAL_DATA_DIR, "COMBINED_sbs_array_winsize1e+05.rds")
arr = readRDS(filename)
return(arr)
}
# helper function
top_n <- function(x, n) {
if (length(x) < n) {
n = length(x)
}
return( sort(x, decreasing=TRUE)[1:n] )
}
#############################################
# quicker way to convert panel to sbs tsv #
#############################################
# just find the relevant windows in the sbs array and sum the vectors together, then save it in the same format
# that sbs_df_from_mut_df() or whatever uses
#####################################
# 1a) compute 96SBS count array #
#####################################
# this is for non-overlapping windows
# (i.e. [n_sample x n_window x n_mutation_categores])
# create a array containing the 96-sbs mutation count for each sample and each region
get_sbs_counts_chrom_windows <- function(mut_df, chrom, win_size=10^6, debug=TRUE) {
name_ls = list()
samp_names = as.character(unique(mut_df$Patient))
n_samp = length(samp_names)
name_ls[[1]] = samp_names
windows = windows_in_chromosome(mut_df, chrom, win_size) # function in preprocess_windows.R
n_window = length(windows)
win_names = window_names(windows, chrom)
name_ls[[2]] = win_names
n_categories = 96
name_ls[[3]] = mut_types() # function in preprocess_windows.R
chrom_df = mut_df[mut_df$Chromosome==chrom, c("Patient", "Chromosome", "Start.Position", "SBS_96")]
arr = array(dim=c(n_samp, n_window, n_categories), dimnames=name_ls)
if (debug) {print(paste0(Sys.time(), " Starting main loop"))}
for (s in 1:n_samp) {
if (debug & (s==1 | s==2 | s==3 | s %% 50 == 0)) {
print(paste0(s, "/", n_samp))
}
curr_samp = samp_names[s]
samp_df = chrom_df[chrom_df$Patient == curr_samp,]
for (w in 1:n_window) {
curr_window = windows[[w]]
curr_window_name = win_names[w]
arr[curr_samp, curr_window_name, ] = get_96sbs_vec(samp_df, curr_window)
}
}
if (debug) {print(paste0(Sys.time(), " finished"))}
return(arr)
}
parallel_get_sbs_counts_chrom_windows <- function(mut_df, chrom, win_size, debug=TRUE) {
name_ls = list()
samp_names = as.character(unique(mut_df$Patient))
n_samp = length(samp_names)
name_ls[[1]] = samp_names
windows = windows_in_chromosome(mut_df, chrom, win_size) # function in preprocess_windows.R
n_window = length(windows)
win_names = window_names(windows, chrom)
name_ls[[2]] = win_names
n_categories = 96
name_ls[[3]] = mut_types() # function in preprocess_windows.R
chrom_df = mut_df[mut_df$Chromosome==chrom, c("Patient", "Chromosome", "Start.Position", "SBS_96")]
#arr = array(dim=c(n_samp, n_window, n_categories), dimnames=name_ls)
acomb <- function(...) abind(..., along=3)
if (debug) {print(paste0(Sys.time(), " Starting main loop"))}
arr <- foreach (s=1:n_samp, .combine='acomb', .multicombine=TRUE) %dopar% {
curr_samp = samp_names[s]
if (debug & (s==1 | s==2 | s==3 | s %% 50 == 0)) {
print(paste0(s, "/", n_samp))
}
curr_samp = samp_names[s]
#samp_name_ls = list()
#samp_name_ls[[1]] = curr_samp
#samp_name_ls[[2]] = win_names
#samp_name_ls[[3]] = name_ls[[3]]
#samp_arr = array(dim=c(1, n_window, n_categories), dimnames=samp_name_ls)
samp_df = chrom_df[chrom_df$Patient == curr_samp,]
samp_window_sbs_mtx = matrix(nrow=n_window, ncol=n_categories)
#samp_window_sbs_mtx <- foreach(w=1:n_window, .combine=rbind) %do% {
# curr_window = windows[[w]]
# curr_window_name = win_names[w]
# get_96sbs_vec(samp_df, curr_window)
# #arr[curr_samp, curr_window_name, ] = get_96sbs_vec(samp_df, curr_window)
#}
for (w in 1:n_window) {
curr_window = windows[[w]]
curr_window_name = win_names[w]
samp_window_sbs_mtx[w, ] = get_96sbs_vec(samp_df, curr_window)
}
rownames(samp_window_sbs_mtx) = win_names
colnames(samp_window_sbs_mtx) = mut_types()
samp_window_sbs_mtx
}
arr = aperm(arr, c(3,1,2)) # rearrange array so that samples are in first dimension
dimnames(arr)[[1]] <- samp_names
if (debug) {print(paste0(Sys.time(), " finished")) }
return(arr)
}
get_96sbs_vec <- function(samp_df, window, mut_categories=NULL) {
if (is.null(mut_categories)) {
mut_categories=mut_types()
}
start = window[1]
end = window[2]
muts = samp_df[samp_df$Start.Position >= start & samp_df$Start.Position < end,]$SBS_96
vec = integer(96)
names(vec) = mut_categories
for (i in 1:length(mut_categories)) {
cat = mut_categories[i]
vec[i] = sum(muts==cat)
}
return(vec)
}
save_96sbs_arrays <- function(win_size=10^5, mut_df=NULL, debug=TRUE) {
if (is.null(mut_df)) {
mut_df = load_nz_mutation_df()
}
chrom_vec = c(1:22, "X", "Y")
for (chrom in chrom_vec) {
if (debug) { print(paste0("Starting get_sbs_counts_chrom_windows() for chromosome ", chrom)) }
sbs_arr = get_sbs_counts_chrom_windows(mut_df, chrom, win_size=win_size)
filename = paste0(GLOBAL_CHR_MTX_DIR, "samp_window_sbs_array_winsize", win_size, "_chr", chrom, ".rds")
if (debug) { print(paste0("Saving sbs_array at: ", filename)) }
saveRDS(sbs_arr, filename)
}
}
parallel_save_96sbs_arrays <- function(win_size=10^5, mut_df=NULL, start_at=NULL, debug=TRUE) {
if (is.null(mut_df)) {
mut_df = load_nz_mutation_df()
}
chrom_vec = c(1:22, "X", "Y")
if (!is.null(start_at)) {
chrom_vec = chrom_vec[start_at:length(chrom_vec)]
}
for (chrom in chrom_vec) {
if (debug) { print(paste0("Starting get_sbs_counts_chrom_windows() for chromosome ", chrom)) }
sbs_arr = parallel_get_sbs_counts_chrom_windows(mut_df, chrom, win_size=win_size)
filename = paste0(GLOBAL_CHR_MTX_DIR, "samp_window_sbs_array_winsize", win_size, "_chr", chrom, ".rds")
if (debug) { print(paste0("Saving sbs_array at: ", filename)) }
saveRDS(sbs_arr, filename)
}
}
######################################
# compute scalar projection #
######################################
# scalar projection of a in the direction of b
scalar_projection <- function(a, b) {
unit_b = b / magnitude(b)
ret = dot_prod(a, unit_b)
return( ret )
}
magnitude <- function(v) {
sum_sq = sum(v^2)
return( sum_sq^(1/2) )
}
dot_prod <- function(v1, v2) {
return( sum(v1 %*% v2) )
}
do_f <- function(f, ls, s) {
ret = list()
for (i in 1:length(ls)) {
ret[[i]] = f(ls[[i]], s)
}
return(ret)
}
###############################################
# compute score for each window and sample #
###############################################
score_sbs_array <- function(score_fn, sig_vec, sbs_array) {
samp_names = dimnames(sbs_array)[[1]]
window_names = dimnames(sbs_array)[[2]]
score_mtx = matrix(nrow=length(samp_names), ncol=length(window_names))
rownames(score_mtx) = samp_names
colnames(score_mtx) = window_names
for (i in 1:length(samp_names)) {
for (j in 1:length(window_names)) {
sbs_vec = sbs_array[i, j, ]
score = score_fn(sbs_vec, sig_vec)
score_mtx[i,j] = score
}
}
return(score_mtx)
}
ps_sbs_array <- function(sig_vec, sbs_array) {
return( score_sbs_array(scalar_projection, sig_vec, sbs_array) )
}
##################################################################
# 1b & c) compute projection score for each window and patient #
##################################################################
projection_scores_by_sig <- function(sig_vec, win_size=10^5, debug=TRUE) {
chrom_vec = c(1:22, "X", "Y")
score_mtx_ls = list()
for (i in 1:length(chrom_vec)) {
chrom = chrom_vec[i]
if (debug) { print(paste0("projection_scores_by_sig() starting chromosome ", chrom)) }
chr_sbs_arr = load_sbs_array(chrom, win_size)
chr_score_mtx = ps_sbs_array(sig_vec, chr_sbs_arr)
score_mtx_ls[[i]] = chr_score_mtx
}
return(score_mtx_ls)
}
save_projection_scores <- function(win_size=10^5, sig_vec=NULL, debug=TRUE) {
sig_df = load_COSMIC_signatures()
if (is.null(sig_vec)) {
sig_vec = 1:nrow(sig_df)
}
for (s in 1:nrow(sig_df)) {
sig_vec = as.numeric(sig_df[s, c(-1)])
if (debug) { print(paste0("running projection_scores_by_sig() for signature ", s)) }
score_mtx_ls = projection_scores_by_sig(sig_vec, win_size)
outfile = paste0(GLOBAL_CHR_MTX_DIR, "projection_score_mtx_ls_sig", s, ".rds")
saveRDS(score_mtx_ls, outfile)
}
}
############################
# test/train framework #
############################
test_train_random_split <- function(sample_names, test_set_proportion) {
num_in_test = as.integer(test_set_proportion * length(sample_names))
test_inds = sample.int(length(sample_names), size=num_in_test)
test_set = sample_names[test_inds]
train_set = sample_names[-test_inds]
ls = list()
ls[[1]] = test_set
ls[[2]] = train_set
return(ls)
}
tt_stratified_split <- function(sig_num, sample_names, test_set_proportion, global_sig_df=NULL, activity_thresh=0.05, debug=FALSE) {
if (debug) {print(paste0("Stratified split of test and train for Signature ", sig_num))}
label_vec = get_sig_activity_labels(sig_num, global_sig_df, activity_thresh)
n_samp = length(label_vec)
n_pos_samp = sum(label_vec) # gives number of samples labeled TRUE
pos_prop = n_pos_samp / n_samp
neg_prop = 1 - pos_prop
if (debug) { print(paste0("num samples: ", n_samp, " num active samples: ", n_pos_samp,
" proportion of active samples: ", pos_prop)) }
num_in_test = as.integer(test_set_proportion * length(sample_names))
num_pos_in_test = as.integer(pos_prop * num_in_test)
num_neg_in_test = num_in_test - num_pos_in_test
pos_samp_names = names(which(label_vec==TRUE))
neg_samp_names = names(which(label_vec==FALSE))
pos_test = sample(pos_samp_names, num_pos_in_test)
neg_test = sample(neg_samp_names, num_neg_in_test)
if (debug) {print(paste0("test set size: ", length(pos_test) + length(neg_test),
" num active: ", length(pos_test),
" num inactive: ", length(neg_test)))}
pos_train = setdiff(pos_samp_names, pos_test)
neg_train = setdiff(neg_samp_names, neg_test)
test_set = c(pos_test, neg_test)
train_set = c(pos_train, neg_train)
ls = list()
ls[[1]] = test_set
ls[[2]] = train_set
return(ls)
}
# get labels for signature activity in all patients
# if a sample has at least 5% signature contribution, we say that it is active (modify thresh parameter to change)
get_sig_activity_labels <- function(sig_num, global_sig_df=NULL, thresh=0.05) {
if (is.null(global_sig_df)) {
global_sig_df = load_nz_sig_estimates(norm=TRUE)
}
sig_name = paste0("Signature.", sig_num)
ret = global_sig_df[ , sig_name] >= thresh
names(ret) = global_sig_df$Patient
return(ret)
}
# returns a vector which contains the objective function score for each region of the genome
compute_obj_score_ps <- function(sig_num, train_samps, score_mtx_ls=NULL, obj_fn=NULL, global_sig_df=NULL, activation_thresh=0.05, debug=TRUE) {
if (is.null(score_mtx_ls)) {
if (debug) {print("compute_obj_score_ps() recieved no score_mtx_ls, loading...")}
score_mtx_ls = load_ps_score_mtx_ls(sig_num)
}
if (is.null(obj_fn)) {
if (debug) {print("compute_obj_score_ps() recieved no obj_fn, using default (simple_obj_fn).")}
obj_fn = simple_obj_fn
}
if (is.null(global_sig_df)) {
if (debug) {print("compute_obj_score_ps() recieved no global_sig_df, loading...")}
global_sig_df = load_nz_sig_estimates(norm=TRUE)
}
activity_vec = get_sig_activity_labels(sig_num, global_sig_df, activation_thresh) #default thresh is 5%
train_act_vec = activity_vec[train_samps]
pos_samps = names(train_act_vec[train_act_vec==TRUE])
neg_samps = names(train_act_vec[train_act_vec==FALSE])
n_windows = 0
window_names = character(0)
for (i in 1:length(score_mtx_ls)) {
n_windows = n_windows + ncol(score_mtx_ls[[i]])
window_names = c(window_names, colnames(score_mtx_ls[[i]]) )
}
obj_vec = numeric(n_windows)
names(obj_vec) = window_names
# main loop, compute objective function on each window
for (i in 1:length(score_mtx_ls)) {
if (debug) { print(paste0(Sys.time(), " compute_obj_score_ps() chrom ", i, "/", length(score_mtx_ls))) }
if (debug) { print(paste0(Sys.time(), " compute_obj_score_ps() chrom ", i, "/", length(score_mtx_ls))) }
score_mtx = score_mtx_ls[[i]]
for (j in 1:ncol(score_mtx)) {
window = colnames(score_mtx)[j]
obj_vec[window] = obj_fn(window, pos_samps, neg_samps, score_mtx)
}
}
return(obj_vec)
}
# where score_mtx is [samples x regions]
simple_obj_fn <- function(region, pos_samps, neg_samps, score_mtx) {
t1 = sum(score_mtx[pos_samps, region])
t2 = sum(score_mtx[neg_samps, region])
return(t1 - t2)
}
obj_fn_sqrt <- function(region, pos_samps, neg_samps, score_mtx) {
t1 = sum(score_mtx[pos_samps, region]^(1/2))
t2 = sum(score_mtx[neg_samps, region]^(1/2))
return(t1 - t2)
}
obj_fn_class_balance <- function(region, pos_samps, neg_samps, score_mtx) {
lambda = length(pos_samps) / length(neg_samps)
t1 = sum(score_mtx[pos_samps, region]^(1/2))
t2 = sum(score_mtx[neg_samps, region]^(1/2))
return(t1 - (lambda * t2))
}
# estimate pval by comparing to baseline panel AUROC
est_pval <- function(obj_auroc, sig_num, test_set, global_sig_df=NULL, eval_mode="auroc", debug=FALSE) {
if (debug) {print("running compute_baseline_auroc()")}
baseline_aurocs = compute_baseline_auroc(sig_num, test_set, global_sig_df, eval_mode=eval_mode)
if (debug) {print(paste0("Estimating p-val from ", length(baseline_aurocs), " random panels."))}
if (debug) {print(paste0("Summary statistics for random panel AUROCs - ",
"max: ", round(max(baseline_aurocs), 4), " ",
"med: ", round(median(baseline_aurocs), 4)) ) }
n_better = sum(baseline_aurocs >= obj_auroc)
pval = n_better / length(baseline_aurocs)
if (debug) {print(paste0("estimated pval: ", pval))}
return(pval)
}
###########################
# random helper functions #
###########################
# save panel sbs df as a tsv
save_panel_sbs_tsv <- function(sbs_df, outfile) {
write.table(sbs_df, file=outfile, sep="\t", quote=FALSE)
}
############# main ################
# eval mode can be set to "auroc" or "aupr"
sig_specific_main <- function(sig_num, mut_df=NULL, global_sig_df=NULL, sbs_array=NULL, windows_in_panel=27, eval_mode="auroc", debug=TRUE) {
#ret = matrix(nrow = 30, ncol=3)
#rownames(ret) = 1:30
#colnames(ret) = c("simple_obj_fn", "obj_fn_sqrt", "obj_fn_class_balance")
if (is.null(mut_df)) {
if (debug) { print(paste0(Sys.time()," loading mut_df")) }
mut_df = load_nz_mut_df_with_sigprob()
}
if (is.null(global_sig_df)) {
if (debug) { print(paste0(Sys.time(), " loading global_sig_df")) }
global_sig_df = load_nz_sig_estimates(norm=TRUE)
}
if (is.null(sbs_array)) {
print(paste0(Sys.time(), " loading default sbs_array (NOTE: THE 100KB WINDOW SBS ARRAY IS THE DEFAULT)"))
sbs_array = load_combined_100k_arr()
}
samp_names = as.character(global_sig_df$Patient)
if (debug) { print(paste0(Sys.time(), " splitting test and train")) }
#test_train = test_train_random_split(samp_names, .10)
test_train = tt_stratified_split(sig_num, samp_names, .10, global_sig_df, debug=debug)
test_set = test_train[[1]]
train_set = test_train[[2]]
timestamp_tag = format(Sys.time(), "%d-%b-%Y_%H-%M")
test_train_outfile = paste0(GLOBAL_LOGFILE_DIR, "test_train_sig", sig_num, "_", timestamp_tag, ".rds")
if (debug) {print(paste0("saving test & train set to : ", test_train_outfile))}
saveRDS(test_train, test_train_outfile)
if (debug) { print(paste0(Sys.time(), " starting main loop")) }
#for (sig_num in 1:30) {
# if test set contains all positive or all negative examples, go to next signature
activity_vec = get_sig_activity_labels(sig_num, global_sig_df, 0.05)
test_activity = activity_vec[test_set]
check = sum(test_activity)
if (check==0 | check==length(test_set)) {
if (debug) {print("bad test set")}
next
}
if (!(eval_mode %in% c("auroc", "aupr")) ) {
stop("eval_mode must be set to either \"auroc\" or \"aupr\".")
}
if (debug) { print(paste0(Sys.time(), " sig_num: ", sig_num, " loading score_mtx_ls")) }
score_mtx_ls = load_ps_score_mtx_ls(sig_num)
if (debug) { print(paste0(Sys.time(), " compute_obj_score_ps() with simple_obj_fn")) }
obj_v1 = compute_obj_score_ps(sig_num, train_set, score_mtx_ls, obj_fn=simple_obj_fn, global_sig_df=global_sig_df)
if (debug) { print(paste0(Sys.time(), " compute_obj_score_ps() with obj_fn_sqrt")) }
obj_v2 = compute_obj_score_ps(sig_num, train_set, score_mtx_ls, obj_fn=obj_fn_sqrt, global_sig_df=global_sig_df)
if (debug) { print(paste0(Sys.time(), " compute_obj_score_ps() with obj_fn_class_balance")) }
obj_v3 = compute_obj_score_ps(sig_num, train_set, score_mtx_ls, obj_fn=obj_fn_class_balance, global_sig_df=global_sig_df)
if (debug) { print(paste0(Sys.time(), " getting panel windows")) }
panel_1_windows = names( top_n(obj_v1, windows_in_panel) )
panel_2_windows = names( top_n(obj_v2, windows_in_panel) )
panel_3_windows = names( top_n(obj_v3, windows_in_panel) )
if (debug) {print(paste0(Sys.time(), " generating panel dfs")) }
#panel_1_df = select_window_muts(panel_1_windows, mut_df)
#panel_2_df = select_window_muts(panel_2_windows, mut_df)
#panel_3_df = select_window_muts(panel_3_windows, mut_df)
panel_1_df = get_panel_sbs_df(panel_1_windows, sbs_array)
panel_2_df = get_panel_sbs_df(panel_2_windows, sbs_array)
panel_3_df = get_panel_sbs_df(panel_3_windows, sbs_array)
sbs_outfile_1 = paste0(GLOBAL_PANEL_SBS_DIR, "ps_obj1_sig", sig_num, "_panel_sbs_", timestamp_tag, ".tsv")
sbs_outfile_2 = paste0(GLOBAL_PANEL_SBS_DIR, "ps_obj2_sig", sig_num, "_panel_sbs_", timestamp_tag, ".tsv")
sbs_outfile_3 = paste0(GLOBAL_PANEL_SBS_DIR, "ps_obj3_sig", sig_num, "_panel_sbs_", timestamp_tag, ".tsv")
if (debug) {print(paste0(Sys.time(), " saving to:")); print(sbs_outfile_1); print(sbs_outfile_2); print(sbs_outfile_3)}
#save_panel_sbs_tsv(sbs_df_from_mut_df(panel_1_df), sbs_outfile_1)
#save_panel_sbs_tsv(sbs_df_from_mut_df(panel_2_df), sbs_outfile_2)
#save_panel_sbs_tsv(sbs_df_from_mut_df(panel_3_df), sbs_outfile_3)
save_panel_sbs_tsv(panel_1_df, sbs_outfile_1)
save_panel_sbs_tsv(panel_2_df, sbs_outfile_2)
save_panel_sbs_tsv(panel_3_df, sbs_outfile_3)
sig_est_outfile_1 = paste0(GLOBAL_PANEL_SIG_EST_DIR, "ps_obj1_sig", sig_num, "_est_", timestamp_tag, ".tsv")
sig_est_outfile_2 = paste0(GLOBAL_PANEL_SIG_EST_DIR, "ps_obj2_sig", sig_num, "_est_", timestamp_tag, ".tsv")
sig_est_outfile_3 = paste0(GLOBAL_PANEL_SIG_EST_DIR, "ps_obj3_sig", sig_num, "_est_", timestamp_tag, ".tsv")
if (debug) {print("running signature estimator for panel 1")}
system(paste0("python signature-estimation-py/signature_estimation.py -mf ", sbs_outfile_1,
" -sf ", GLOBAL_DATA_DIR, "cosmic-signatures.tsv ",
" -of ", sig_est_outfile_1)
)
if (debug) {print("running signature estimator for panel 2")}
system(paste0("python signature-estimation-py/signature_estimation.py -mf ", sbs_outfile_2,
" -sf ", GLOBAL_DATA_DIR, "cosmic-signatures.tsv ",
" -of ", sig_est_outfile_2)
)
if (debug) {print("running signature estimator for panel 3")}
system(paste0("python signature-estimation-py/signature_estimation.py -mf ", sbs_outfile_3,
" -sf ", GLOBAL_DATA_DIR, "cosmic-signatures.tsv ",
" -of ", sig_est_outfile_3)
)
if (eval_mode=="auroc") {
if (debug) {print(paste0(Sys.time(), " running compute_panel_auroc()"))}
auroc_1 = compute_panel_auroc(sig_num, test_set, sig_est_outfile_1, global_sig_df=global_sig_df)
auroc_2 = compute_panel_auroc(sig_num, test_set, sig_est_outfile_2, global_sig_df=global_sig_df)
auroc_3 = compute_panel_auroc(sig_num, test_set, sig_est_outfile_3, global_sig_df=global_sig_df)
} else if (eval_mode=="aupr") {
if (debug) {print(paste0(Sys.time(), " running compute_panel_aupr()"))}
auroc_1 = compute_panel_aupr(sig_num, test_set, sig_est_outfile_1, global_sig_df=global_sig_df)
auroc_2 = compute_panel_aupr(sig_num, test_set, sig_est_outfile_2, global_sig_df=global_sig_df)
auroc_3 = compute_panel_aupr(sig_num, test_set, sig_est_outfile_3, global_sig_df=global_sig_df)
} else {
# this shouldn't happen, gets checked for above.
stop("eval_mode must be set to either \"auroc\" or \"aupr\". (this shouldn't happen here)")
}
best_auroc = max(c(auroc_1, auroc_2, auroc_3))
if (debug) {print(paste0(Sys.time(), " results for signature ", sig_num, ": ")); print(auroc_1); print(auroc_2); print(auroc_3)}
if (debug) {print(paste0(Sys.time(), " running est_pval()")) }
pval = est_pval(best_auroc, sig_num, test_set, global_sig_df, debug=debug)
if (debug) { print("saving results file")}
safe_ret = matrix(c(auroc_1, auroc_2, auroc_3, best_auroc, pval))
rownames(safe_ret) = c("simple_obj_fn", "obj_fn_sqrt", "obj_fn_class_balance", "best", "est pval")
colnames(safe_ret) = c("test_set_auroc")
safe_ret_outfile = paste0(GLOBAL_PANEL_DIR, "ps_panel_results_sig", sig_num, "_", timestamp_tag, ".txt")
write.table(safe_ret, file=safe_ret_outfile)
#ret[sig_num, ] = c(auroc_1, auroc_2, auroc_3)
#}
#auc_mtx_outfile = paste0(GLOBAL_OUT_DIR, "ps_auc_mtx_", timestamp_tag, ".txt")
#write.table(ret, file=auc_mtx_outfile)
}
main <- function(mut_df=NULL, global_sig_df=NULL, debug=TRUE) {
ret = matrix(nrow = 30, ncol=3)
rownames(ret) = 1:30
colnames(ret) = c("simple_obj_fn", "obj_fn_sqrt", "obj_fn_class_balance")
if (is.null(mut_df)) {
if (debug) { print(paste0(Sys.time()," loading mut_df")) }
mut_df = load_nz_mut_df_with_sigprob()
}
if (is.null(global_sig_df)) {
if (debug) { print(paste0(Sys.time(), " loading global_sig_df")) }
global_sig_df = load_nz_sig_estimates(norm=TRUE)
}
samp_names = as.character(global_sig_df$Patient)
if (debug) { print(paste0(Sys.time(), " splitting test and train")) }
test_train = test_train_random_split(samp_names, .10)
test_set = test_train[[1]]
train_set = test_train[[2]]
timestamp_tag = format(Sys.time(), "%d-%b-%Y_%H-%M")
if (debug) { print(paste0(Sys.time(), " starting main loop")) }
for (sig_num in 1:30) {
# if test set contains all positive or all negative examples, go to next signature
activity_vec = get_sig_activity_labels(sig_num, global_sig_df, 0.05)
test_activity = activity_vec[test_set]
check = sum(test_activity)
if (check==0 | check==length(test_set)) {
if (debug) {print("bad test set")}
next
}
if (debug) { print(paste0(Sys.time(), " sig_num: ", sig_num, " loading score_mtx_ls")) }
score_mtx_ls = load_ps_score_mtx_ls(sig_num)
if (debug) { print(paste0(Sys.time(), " compute_obj_score_ps() with simple_obj_fn")) }
obj_v1 = compute_obj_score_ps(sig_num, train_set, score_mtx_ls, obj_fn=simple_obj_fn, global_sig_df=global_sig_df)
if (debug) { print(paste0(Sys.time(), " compute_obj_score_ps() with obj_fn_sqrt")) }
obj_v2 = compute_obj_score_ps(sig_num, train_set, score_mtx_ls, obj_fn=obj_fn_sqrt, global_sig_df=global_sig_df)
if (debug) { print(paste0(Sys.time(), " compute_obj_score_ps() with obj_fn_class_balance")) }
obj_v3 = compute_obj_score_ps(sig_num, train_set, score_mtx_ls, obj_fn=obj_fn_class_balance, global_sig_df=global_sig_df)
if (debug) { print(paste0(Sys.time(), " getting panel windows")) }
panel_1_windows = names( top_n(obj_v1, 27) )
panel_2_windows = names( top_n(obj_v2, 27) )
panel_3_windows = names( top_n(obj_v3, 27) )
if (debug) {print(paste0(Sys.time(), " generating panel dfs")) }
panel_1_df = select_window_muts(panel_1_windows, mut_df)
panel_2_df = select_window_muts(panel_2_windows, mut_df)
panel_3_df = select_window_muts(panel_3_windows, mut_df)
sbs_outfile_1 = paste0(GLOBAL_PANEL_SBS_DIR, "ps_obj1_sig", sig_num, "_panel_sbs_", timestamp_tag, ".tsv")
sbs_outfile_2 = paste0(GLOBAL_PANEL_SBS_DIR, "ps_obj2_sig", sig_num, "_panel_sbs_", timestamp_tag, ".tsv")
sbs_outfile_3 = paste0(GLOBAL_PANEL_SBS_DIR, "ps_obj3_sig", sig_num, "_panel_sbs_", timestamp_tag, ".tsv")
if (debug) {print(paste0(Sys.time(), " saving to:")); print(sbs_outfile_1); print(sbs_outfile_2); print(sbs_outfile_3)}
save_panel_sbs_tsv(sbs_df_from_mut_df(panel_1_df), sbs_outfile_1)
save_panel_sbs_tsv(sbs_df_from_mut_df(panel_2_df), sbs_outfile_2)
save_panel_sbs_tsv(sbs_df_from_mut_df(panel_3_df), sbs_outfile_3)
sig_est_outfile_1 = paste0(GLOBAL_PANEL_SIG_EST_DIR, "ps_obj1_sig", sig_num, "_est_", timestamp_tag, ".tsv")
sig_est_outfile_2 = paste0(GLOBAL_PANEL_SIG_EST_DIR, "ps_obj2_sig", sig_num, "_est_", timestamp_tag, ".tsv")
sig_est_outfile_3 = paste0(GLOBAL_PANEL_SIG_EST_DIR, "ps_obj3_sig", sig_num, "_est_", timestamp_tag, ".tsv")
if (debug) {print("running signature estimator for panel 1")}
system(paste0("python signature-estimation-py/signature_estimation.py -mf ", sbs_outfile_1,
" -sf ", GLOBAL_DATA_DIR, "cosmic-signatures.tsv ",
" -of ", sig_est_outfile_1)
)
if (debug) {print("running signature estimator for panel 2")}
system(paste0("python signature-estimation-py/signature_estimation.py -mf ", sbs_outfile_2,
" -sf ", GLOBAL_DATA_DIR, "cosmic-signatures.tsv ",
" -of ", sig_est_outfile_2)
)
if (debug) {print("running signature estimator for panel 3")}
system(paste0("python signature-estimation-py/signature_estimation.py -mf ", sbs_outfile_3,
" -sf ", GLOBAL_DATA_DIR, "cosmic-signatures.tsv ",
" -of ", sig_est_outfile_3)
)
if (debug) {print(paste0(Sys.time(), " running compute_panel_auroc()"))}
auroc_1 = compute_panel_auroc(sig_num, test_set, sig_est_outfile_1, global_sig_df=global_sig_df)
auroc_2 = compute_panel_auroc(sig_num, test_set, sig_est_outfile_2, global_sig_df=global_sig_df)
auroc_3 = compute_panel_auroc(sig_num, test_set, sig_est_outfile_3, global_sig_df=global_sig_df)
if (debug) {print(paste0(Sys.time(), " results for signature ", sig_num, ": ")); print(auroc_1); print(auroc_2); print(auroc_3)}
if (debug) {
print("saving incomplete results file")
safe_ret = matrix(c(auroc_1, auroc_2, auroc_3))
rownames(safe_ret) = c("simple_obj_fn", "obj_fn_sqrt", "obj_fn_class_balance")
colnames(safe_ret) = c("test_set_auroc")
safe_ret_outfile = paste0(GLOBAL_PANEL_DIR, "ps_panel_inprogress_results_sig", sig_num, "_", timestamp_tag, ".txt")
write.table(safe_ret, file=safe_ret_outfile)
}
ret[sig_num, ] = c(auroc_1, auroc_2, auroc_3)
}
auc_mtx_outfile = paste0(GLOBAL_OUT_DIR, "ps_auc_mtx_", timestamp_tag, ".txt")
write.table(ret, file=auc_mtx_outfile)
}
run_this <- function(mut_df=NULL, global_sig_df=NULL, sbs_arr=NULL) {
if(is.null(mut_df)) { mut_df = load_nz_mut_df_with_sigprob() }
if(is.null(global_sig_df)) { global_sig_df = load_nz_sig_estimates(norm=TRUE) }
if(is.null(sbs_arr)) { sbs_arr = load_combined_100k_arr() }
for (s in c(2,2,2,2,2, 3,3,3,3,3, 5,5,5,5,5, 9,9,9,9,9, 13,13,13,13,13, 16,16,16,16,16)) {
sig_specific_main(s, mut_df, global_sig_df, sbs_arr)
}
}
compute_auprs <- function() {
mut_df = load_nz_mut_df_with_sigprob()
global_sig_df = load_nz_sig_estimates(norm=TRUE)
for (s in c(1,1,1,1,1, 8,8,8,8,8, 18,18,18,18,18, 30,30,30,30,30)) {
sig_specific_main(s, mut_df, global_sig_df, eval_mode="aupr")
}
}
# eval mode can be set to "auroc" or "aupr"
half_sig_specific_main <- function(sig_num, file_tag, global_sig_df=NULL, sbs_array=NULL, win_mode="100k", windows_in_panel=27, eval_mode="auroc", debug=TRUE) {
#ret = matrix(nrow = 30, ncol=3)
#rownames(ret) = 1:30
#colnames(ret) = c("simple_obj_fn", "obj_fn_sqrt", "obj_fn_class_balance")
#if (is.null(mut_df)) {
# if (debug) { print(paste0(Sys.time()," loading mut_df")) }
# mut_df = load_nz_mut_df_with_sigprob()
#}
if (is.null(global_sig_df)) {
if (debug) { print(paste0(Sys.time(), " loading global_sig_df")) }
global_sig_df = load_nz_sig_estimates(norm=TRUE)
}
if (is.null(sbs_array)) {
print(paste0(Sys.time(), " loading default sbs_array (NOTE: THE 100KB WINDOW SBS ARRAY IS THE DEFAULT)"))
sbs_array = load_combined_100k_arr()
}
samp_names = as.character(global_sig_df$Patient)
if (debug) { print(paste0(Sys.time(), " splitting test and train")) }
#test_train = test_train_random_split(samp_names, .10)
test_train = tt_stratified_split(sig_num, samp_names, .10, global_sig_df, debug=debug)
test_set = test_train[[1]]
train_set = test_train[[2]]
test_train_outfile = paste0(GLOBAL_LOGFILE_DIR, "test_train_sig", sig_num, "_", file_tag, ".rds")
if (debug) {print(paste0("saving test & train set to : ", test_train_outfile))}
saveRDS(test_train, test_train_outfile)
if (debug) { print(paste0(Sys.time(), " starting main loop")) }
#for (sig_num in 1:30) {
# if test set contains all positive or all negative examples, go to next signature
activity_vec = get_sig_activity_labels(sig_num, global_sig_df, 0.05)
test_activity = activity_vec[test_set]
check = sum(test_activity)
if (check==0 | check==length(test_set)) {
if (debug) {print("bad test set")}
next
}
if (!(eval_mode %in% c("auroc", "aupr")) ) {
stop("eval_mode must be set to either \"auroc\" or \"aupr\".")
}
if (debug) { print(paste0(Sys.time(), " sig_num: ", sig_num, " loading score_mtx_ls - mode: ", win_mode)) }
score_mtx_ls = load_ps_score_mtx_ls(sig_num, mode=win_mode)
if (debug) { print(paste0(Sys.time(), " compute_obj_score_ps() with simple_obj_fn")) }
obj_v1 = compute_obj_score_ps(sig_num, train_set, score_mtx_ls, obj_fn=simple_obj_fn, global_sig_df=global_sig_df)
if (debug) { print(paste0(Sys.time(), " compute_obj_score_ps() with obj_fn_sqrt")) }
obj_v2 = compute_obj_score_ps(sig_num, train_set, score_mtx_ls, obj_fn=obj_fn_sqrt, global_sig_df=global_sig_df)
if (debug) { print(paste0(Sys.time(), " compute_obj_score_ps() with obj_fn_class_balance")) }
obj_v3 = compute_obj_score_ps(sig_num, train_set, score_mtx_ls, obj_fn=obj_fn_class_balance, global_sig_df=global_sig_df)
if (debug) { print(paste0(Sys.time(), " getting panel windows")) }
panel_1_windows = names( top_n(obj_v1, windows_in_panel) )
panel_2_windows = names( top_n(obj_v2, windows_in_panel) )
panel_3_windows = names( top_n(obj_v3, windows_in_panel) )
if (debug) {print(paste0(Sys.time(), " generating panel dfs")) }
#panel_1_df = select_window_muts(panel_1_windows, mut_df)
#panel_2_df = select_window_muts(panel_2_windows, mut_df)
#panel_3_df = select_window_muts(panel_3_windows, mut_df)
panel_1_df = get_panel_sbs_df(panel_1_windows, sbs_array)
panel_2_df = get_panel_sbs_df(panel_2_windows, sbs_array)
panel_3_df = get_panel_sbs_df(panel_3_windows, sbs_array)
sbs_outfile_1 = paste0(GLOBAL_PANEL_SBS_DIR, "ps_obj1_sig", sig_num, "_panel_sbs_", file_tag, ".tsv")
sbs_outfile_2 = paste0(GLOBAL_PANEL_SBS_DIR, "ps_obj2_sig", sig_num, "_panel_sbs_", file_tag, ".tsv")
sbs_outfile_3 = paste0(GLOBAL_PANEL_SBS_DIR, "ps_obj3_sig", sig_num, "_panel_sbs_", file_tag, ".tsv")
if (debug) {print(paste0(Sys.time(), " saving to:")); print(sbs_outfile_1); print(sbs_outfile_2); print(sbs_outfile_3)}
#save_panel_sbs_tsv(sbs_df_from_mut_df(panel_1_df), sbs_outfile_1)
#save_panel_sbs_tsv(sbs_df_from_mut_df(panel_2_df), sbs_outfile_2)
#save_panel_sbs_tsv(sbs_df_from_mut_df(panel_3_df), sbs_outfile_3)
save_panel_sbs_tsv(panel_1_df, sbs_outfile_1)
save_panel_sbs_tsv(panel_2_df, sbs_outfile_2)
save_panel_sbs_tsv(panel_3_df, sbs_outfile_3)
sig_est_outfile_1 = paste0(GLOBAL_PANEL_SIG_EST_DIR, "ps_obj1_sig", sig_num, "_est_", file_tag, ".tsv")
sig_est_outfile_2 = paste0(GLOBAL_PANEL_SIG_EST_DIR, "ps_obj2_sig", sig_num, "_est_", file_tag, ".tsv")
sig_est_outfile_3 = paste0(GLOBAL_PANEL_SIG_EST_DIR, "ps_obj3_sig", sig_num, "_est_", file_tag, ".tsv")
state = list()
state["sbs_outfile_1"] = sbs_outfile_1
state["sbs_outfile_2"] = sbs_outfile_2
state["sbs_outfile_3"] = sbs_outfile_3
state["sig_est_outfile_1"] = sig_est_outfile_1
state["sig_est_outfile_2"] = sig_est_outfile_2
state["sig_est_outfile_3"] = sig_est_outfile_3
state["file_tag"] = file_tag
state[["test_set"]] = test_set
state["sig_num"] = sig_num
state["eval_mode"] = eval_mode
state_outfile = paste0(GLOBAL_LOGFILE_DIR, "state_", file_tag, ".rds")
saveRDS(state, file=state_outfile)
}
# these are the command line calls to run signature estimator
sig_est_shell <- function(state, debug=TRUE) {
sbs_outfile_1 = state[["sbs_outfile_1"]]
sbs_outfile_2 = state[["sbs_outfile_2"]]
sbs_outfile_3 = state[["sbs_outfile_3"]]
sig_est_outfile_1 = state[["sig_est_outfile_1"]]
sig_est_outfile_2 = state[["sig_est_outfile_2"]]
sig_est_outfile_3 = state[["sig_est_outfile_3"]]
if (debug) {print("running signature estimator for panel 1")}
system(paste0("python signature-estimation-py/signature_estimation.py -mf ", sbs_outfile_1,
" -sf ", GLOBAL_DATA_DIR, "cosmic-signatures.tsv ",
" -of ", sig_est_outfile_1)
)
if (debug) {print("running signature estimator for panel 2")}
system(paste0("python signature-estimation-py/signature_estimation.py -mf ", sbs_outfile_2,
" -sf ", GLOBAL_DATA_DIR, "cosmic-signatures.tsv ",
" -of ", sig_est_outfile_2)
)
if (debug) {print("running signature estimator for panel 3")}
system(paste0("python signature-estimation-py/signature_estimation.py -mf ", sbs_outfile_3,
" -sf ", GLOBAL_DATA_DIR, "cosmic-signatures.tsv ",
" -of ", sig_est_outfile_3)
)
}
sec_sig_specific_main <- function(file_tag, global_sig_df=NULL, debug=TRUE) {
state_infile = paste0(GLOBAL_LOGFILE_DIR, "state_", file_tag, ".rds")
state = readRDS(state_infile)
sig_est_outfile_1 = state[["sig_est_outfile_1"]]
sig_est_outfile_2 = state[["sig_est_outfile_2"]]
sig_est_outfile_3 = state[["sig_est_outfile_3"]]
test_set = state[["test_set"]]
sig_num = state[["sig_num"]]