-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPart4.Rmd
2439 lines (2113 loc) · 90.7 KB
/
Part4.Rmd
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
---
title: "Feature importance using DREMI scores - Application to Benedicte's cohort"
author: "Dimitrios Kleftogiannis"
date: "2024-03-15"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
### Utility
This code is part of the study titled "Automated cell type annotation and exploration of single-cell signalling dynamics using mass cytometry".
The utility of this code is use XGBoost feature importance and identify the most stable features, meaning that we estimate the frequency of selection and tha gain metric returned by the algorithm. The data used here are described at "Early response evaluation by single cell signaling profiling in acute myeloid leukemia", doi: 10.1038/s41467-022-35624-4
### Contact
Comments and bug reports are welcome, please email: Dimitrios Kleftogiannis ([email protected])
We are also interested to know about how you have used our framework, including any improvements that you have implemented.
You are free to modify, extend or distribute our source codes, as long as our copyright notice remains unchanged and included in its entirety.
### License
This code is licensed under the MIT License.
Copyright 2023, University of Bergen (UiB) and NeuroSysMed, Norway
## Load R libraries.
We also define functions that are required
```{r load packages, echo=TRUE, eval=TRUE, error=TRUE, warning=FALSE,cache=TRUE}
library(ggplot2)
library(readxl)
library(dplyr)
library(ggridges)
library(MASS)
library(matrixStats)
library(reshape2)
library(ggthemes)
library(RColorBrewer)
library(ggbeeswarm)
library(DMwR)
library(xgboost)
library(pROC)
```
## Setting workspace and loading in-house functions
```{r initialise workspace,echo=TRUE,eval=TRUE,error=TRUE, warning=FALSE,cache=TRUE}
#load the functions
setwd("/Users/kleftogi/Desktop/CyTOF_paper_corrections")
set.seed(1234)
```
## Use XGBoost feauture importance
The input would be DREMI scores with SMOTE 1:2 ratios between positive and negative classes.
We will assess the most stable features
```{r predicting survival using XGBoost and SMOTE on DREMI,echo=TRUE,eval=TRUE,error=TRUE, warning=FALSE,cache=TRUE}
#load the clinical info from external files --> we are not allowed to share this file; please contact the authors
filename <- "AML_Benedicte_cohort/barcode_metadata_leukemia_cohort.xlsx"
#read the samples
md <- read_excel(filename)
patient_samples <- md[md$condition=='0h',]
file_names_patients <- patient_samples$file_name
file_names_patients <- paste('AML_Benedicte_cohort/raw_data/',file_names_patients,sep='')
file_barcode_patients <- patient_samples$barcode
metadata_filename <- "AML_Benedicte_cohort/leukemia_cohort_info.xlsx"
md_0h <- read_excel(metadata_filename)
load('DREMI_feature_vector_v2.RDa')
#retrieve the markers of interest - for this example we focus on signaling markers only
signal.col.names <- c("pAxl","CyclinB1","pNFkB","pErk","pSTAT1",
"pP38","pSTAT3","pCREB","pHist3","Casp3",
"pSTAT5","p4EBP1","pAkt","pRB","pS6")
#we generate all possible pairs of signaling markers in the panel
x = signal.col.names
testRelation <- with(subset(expand.grid(x,x),Var1!=Var2),paste0(Var1,'-',Var2))
#perform cell type specific modeling
cellTypes <- c('B','CD4_T','CD8_T','HSCs_MPPs', 'Monocytes','NK','pDCs')
####################################################################################################
#SMOTE XGBoost 1:1 ratio - save the feature importance
totalPerformanceXGBoostDREMI_1_1 <- data.frame()
N <- 1000
#initiate cell type specific matrices
featureImportanceSummaryGain_B <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryGain_B <- as.data.frame(featureImportanceSummaryGain_B)
featureImportanceSummaryGain_B$Feature <- testRelation
featureImportanceSummaryFrequency_B <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryFrequency_B <- as.data.frame(featureImportanceSummaryFrequency_B)
featureImportanceSummaryFrequency_B$Feature <- testRelation
featureImportanceSummaryGain_CD4T <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryGain_CD4T <- as.data.frame(featureImportanceSummaryGain_CD4T)
featureImportanceSummaryGain_CD4T$Feature <- testRelation
featureImportanceSummaryFrequency_CD4T <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryFrequency_CD4T <- as.data.frame(featureImportanceSummaryFrequency_CD4T)
featureImportanceSummaryFrequency_CD4T$Feature <- testRelation
featureImportanceSummaryGain_CD8T <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryGain_CD8T <- as.data.frame(featureImportanceSummaryGain_CD8T)
featureImportanceSummaryGain_CD8T$Feature <- testRelation
featureImportanceSummaryFrequency_CD8T <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryFrequency_CD8T <- as.data.frame(featureImportanceSummaryFrequency_CD8T)
featureImportanceSummaryFrequency_CD8T$Feature <- testRelation
featureImportanceSummaryGain_HSCsMPPs <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryGain_HSCsMPPs <- as.data.frame(featureImportanceSummaryGain_HSCsMPPs)
featureImportanceSummaryGain_HSCsMPPs$Feature <- testRelation
featureImportanceSummaryFrequency_HSCsMPPs <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryFrequency_HSCsMPPs <- as.data.frame(featureImportanceSummaryFrequency_HSCsMPPs)
featureImportanceSummaryFrequency_HSCsMPPs$Feature <- testRelation
featureImportanceSummaryGain_Monocytes <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryGain_Monocytes <- as.data.frame(featureImportanceSummaryGain_Monocytes)
featureImportanceSummaryGain_Monocytes$Feature <- testRelation
featureImportanceSummaryFrequency_Monocytes <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryFrequency_Monocytes <- as.data.frame(featureImportanceSummaryFrequency_Monocytes)
featureImportanceSummaryFrequency_Monocytes$Feature <- testRelation
featureImportanceSummaryGain_pDCs <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryGain_pDCs <- as.data.frame(featureImportanceSummaryGain_pDCs)
featureImportanceSummaryGain_pDCs$Feature <- testRelation
featureImportanceSummaryFrequency_pDCs <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryFrequency_pDCs <- as.data.frame(featureImportanceSummaryFrequency_pDCs)
featureImportanceSummaryFrequency_pDCs$Feature <- testRelation
featureImportanceSummaryGain_NK <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryGain_NK <- as.data.frame(featureImportanceSummaryGain_NK)
featureImportanceSummaryGain_NK$Feature <- testRelation
featureImportanceSummaryFrequency_NK <- matrix(0,nrow = length(testRelation),ncol = N )
featureImportanceSummaryFrequency_NK <- as.data.frame(featureImportanceSummaryFrequency_NK)
featureImportanceSummaryFrequency_NK$Feature <- testRelation
start_time = Sys.time()
for(iter in 1:N){
for(currentCellType in cellTypes){
#fetch the data
currentDataValues <- DREMI_feature_vector[DREMI_feature_vector$CellType==currentCellType,]
#first we check the the cases where there are -1 in the dataset.this means that DREMI was not computed so we filter out those samples
a <- which(currentDataValues[,1]==-1)
if(length(a)<=0){
a <- 1
}else{
currentDataValues <- currentDataValues[-a,]
}
#fetch the data and filter to perform SMOTE sampling
currentDataValues_filt <- currentDataValues[,c(1:210,218)]
currentDataValues_filt$SurvivalGroup <- as.factor(currentDataValues_filt$SurvivalGroup)
#1000 vs 110 gives balanced data
currentDataValues_new <- SMOTE(SurvivalGroup ~ ., currentDataValues_filt, perc.over = 1000, perc.under = 120)
currentDataValues_new$SurvivalStatus <- ifelse(currentDataValues_new$SurvivalGroup=='STS',1,0)
#generate random disjoint sets for training, validation and testing
cut_point <- round(nrow(currentDataValues_new)*0.8)
a <- sample(nrow(currentDataValues_new))
asel <- a[1:cut_point]
train_valid_Set <- currentDataValues_new[asel,]
asel <- a[(cut_point+1):length(a)]
testSet <- currentDataValues_new[asel,]
#if the test set has only CellType from one class AUC cannot be built, thus we ship
check1 <- which(testSet$SurvivalStatus==0)
check2 <- which(testSet$SurvivalStatus==1)
if(length(check1)>0 & length(check2)>0){
#split the train_valid_Set to validation and actual train with ratios again 60-40
cut_point <- round(nrow(train_valid_Set)*0.6)
a <- sample(nrow(train_valid_Set))
asel <- a[1:cut_point]
trainSet <- train_valid_Set[asel,]
asel <- a[(cut_point+1):length(a)]
validationSet <- train_valid_Set[asel,]
depthTunning <- data.frame()
for(depthVal in c(2,4,6,8,10,12,14,16,18,20,30,40,50,60)){
Model <- xgboost(data = as.matrix(trainSet[,testRelation]),
label = trainSet$SurvivalStatus,
max_depth = depthVal,
eta = 0.3,
nthread = 2,
nrounds = 4,
objective = "binary:logistic",
verbose = 0)
pred <- predict(Model, as.matrix(validationSet[,testRelation]))
prediction <- as.numeric(pred > 0.5)
#if you want to estimate error
#err <- mean(as.numeric(pred > 0.5) != validationSet$SurvivalStatus)
#print(paste("test-error=", err))
#assess performance
prediction <- factor(prediction,levels = c(0,1))
validationSet$SurvivalStatus <- factor(validationSet$SurvivalStatus,levels = c(0,1))
perf <- confusionMatrix(prediction, validationSet$SurvivalStatus, positive = c('1'))
perf <- as.data.frame(t(perf$byClass))
perf$depthVal <- depthVal
depthTunning <- rbind(depthTunning,perf)
}
#find the depthVal that maximises F1 in the validation set
a <- which.max(depthTunning$F1)
if(length(a)>0){
bestdepthVal <- depthTunning[a,'depthVal']
}else{
bestdepthVal <- 4
}
#final model after tuning, tested on the test set
bstModel <- xgboost(data = as.matrix(train_valid_Set[,testRelation]),
label = train_valid_Set$SurvivalStatus,
max_depth = bestdepthVal,
eta = 0.3,
nthread = 2,
nrounds = 4,
objective = "binary:logistic",
verbose = 0)
importance_matrix <- xgb.importance(model = bstModel)
#a bit ugly part of code, but it was easy to control it and avoid nested lists
if(currentCellType=='B'){
dr <- match(importance_matrix$Feature,featureImportanceSummaryGain_B$Feature)
featureImportanceSummaryGain_B[dr,iter] <- importance_matrix$Gain
featureImportanceSummaryFrequency_B[dr,iter] <- importance_matrix$Frequency
}
if(currentCellType=='CD4_T'){
dr <- match(importance_matrix$Feature,featureImportanceSummaryGain_CD4T$Feature)
featureImportanceSummaryGain_CD4T[dr,iter] <- importance_matrix$Gain
featureImportanceSummaryFrequency_CD4T[dr,iter] <- importance_matrix$Frequency
}
if(currentCellType=='CD8_T'){
dr <- match(importance_matrix$Feature,featureImportanceSummaryGain_CD8T$Feature)
featureImportanceSummaryGain_CD8T[dr,iter] <- importance_matrix$Gain
featureImportanceSummaryFrequency_CD8T[dr,iter] <- importance_matrix$Frequency
}
if(currentCellType=='HSCs_MPPs'){
dr <- match(importance_matrix$Feature,featureImportanceSummaryGain_HSCsMPPs$Feature)
featureImportanceSummaryGain_HSCsMPPs[dr,iter] <- importance_matrix$Gain
featureImportanceSummaryFrequency_HSCsMPPs[dr,iter] <- importance_matrix$Frequency
}
if(currentCellType=='Monocytes'){
dr <- match(importance_matrix$Feature,featureImportanceSummaryGain_Monocytes$Feature)
featureImportanceSummaryGain_Monocytes[dr,iter] <- importance_matrix$Gain
featureImportanceSummaryFrequency_Monocytes[dr,iter] <- importance_matrix$Frequency
}
if(currentCellType=='NK'){
dr <- match(importance_matrix$Feature,featureImportanceSummaryGain_NK$Feature)
featureImportanceSummaryGain_NK[dr,iter] <- importance_matrix$Gain
featureImportanceSummaryFrequency_NK[dr,iter] <- importance_matrix$Frequency
}
if(currentCellType=='pDCs'){
dr <- match(importance_matrix$Feature,featureImportanceSummaryGain_pDCs$Feature)
featureImportanceSummaryGain_pDCs[dr,iter] <- importance_matrix$Gain
featureImportanceSummaryFrequency_pDCs[dr,iter] <- importance_matrix$Frequency
}
pred <- predict(bstModel, as.matrix(testSet[,testRelation]))
prediction <- as.numeric(pred > 0.5)
prediction <- factor(prediction,levels = c(0,1))
testSet$SurvivalStatus <- factor(testSet$SurvivalStatus,levels = c(0,1))
perf <- confusionMatrix(prediction, testSet$SurvivalStatus, positive = c('1'))
perf <- as.data.frame(t(perf$byClass))
#ROC using pROC package
tS <- as.numeric(testSet$SurvivalStatus)-1
pR <- as.numeric(prediction)-1
a <- roc(tS, pR,levels=c("1","0"))
perf$AUC <- a$auc
perf$depthVal <- bestdepthVal
perf$CellType <- currentCellType
perf$Iter <- iter
totalPerformanceXGBoostDREMI_1_1 <- rbind(totalPerformanceXGBoostDREMI_1_1,perf)
}
}
if(iter%%50==0){
str <- paste('Processed iter: ',iter,' /',N,sep='')
cat(str)
cat('\n')
gc()
}
}
end_time = Sys.time()
end_time-start_time
gc()
#summarise selected features per cell type
library(ggrepel)
db13 <- c('#e6194b', '#3cb44b', '#ffe119', '#4363d8', '#f58231', '#911eb4', '#46f0f0', '#f032e6',
'#bcf60c', '#fabebe', '#008080', '#e6beff', '#9a6324', '#fffac8', '#800000', 'slategray3',
'khaki3','bisque3','coral1','mediumaquamarine',
'#aaffc3', '#808000', '#ffd8b1', '#000075', '#808080','gray88' ,'#ffffff', '#000000')
############################################################################################
############################################################################################
#B
n <- 1
dataGain <- featureImportanceSummaryGain_B
dataFreq <- featureImportanceSummaryFrequency_B
dataGain$TotalGain <- rowSums(dataGain[,c(1:N)])
dataGain$AverageGain <- rowMeans(dataGain[,c(1:N)])
dataFreq$AverageFreq <- rowMeans(dataFreq[,c(1:N)])
for(i in 1:nrow(dataFreq)){
for(j in 1:N){
if(dataFreq[i,j]!=0){
dataFreq[i,j] <- 1
}
}
}
dataFreq$TotalCount <- rowSums(dataFreq[,c(1:N)])
plotData <- data.frame(Features=dataGain$Feature,
TotalGain=dataGain$TotalGain,
AvgGain=dataGain$AverageGain,
AvgFreq=dataFreq$AverageFreq,
Counts=dataFreq$TotalCount)
plotData <- arrange(plotData,desc(AvgGain))
#plot the first 10 total gain features
tmp <- plotData[1:10,]
str <- paste('B cells - top10 features',sep='')
o1 <- ggplot(tmp, aes(x = reorder(Features,AvgGain), y = AvgGain)) +
geom_bar(stat='identity',width = 0.58,alpha=0.68,size=0.2,color='gray8',fill=db13[n])+
ylab('Avg. Gain (1000 iter.)')+
ggtitle(str)+
theme_bw()+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_blank(),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)+
guides(fill = guide_legend(override.aes = list(size=3),nrow=1,title=""))+
coord_flip()
tmp <- arrange(plotData,desc(AvgGain),Counts)
str <- paste('B cells',sep='')
o4 <- ggplot(tmp,aes(y=AvgGain,x=Counts))+
geom_point(size = 2,color=db13[n])+
geom_label_repel(aes(label = Features),
box.padding = 0.1,
point.padding = 0.2,
size=3,
segment.color = 'grey50')+
ggtitle(str)+
ylab('Avg. Gain (1000 iter.)')+
xlab('#Times selected')+
theme_bw()+
xlim(0,1000)+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_text( size = 12 ),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)
myfile <- paste('B_Gain.pdf',sep ='')
pdf(myfile)
print(o1)
dev.off()
myfile <- paste('B_Gain_Count.pdf',sep ='')
pdf(myfile)
print(o4)
dev.off()
############################################################################################
############################################################################################
#CD4_T
n <- 2
dataGain <- featureImportanceSummaryGain_CD4T
dataFreq <- featureImportanceSummaryFrequency_CD4T
dataGain$TotalGain <- rowSums(dataGain[,c(1:N)])
dataGain$AverageGain <- rowMeans(dataGain[,c(1:N)])
dataFreq$AverageFreq <- rowMeans(dataFreq[,c(1:N)])
for(i in 1:nrow(dataFreq)){
for(j in 1:N){
if(dataFreq[i,j]!=0){
dataFreq[i,j] <- 1
}
}
}
dataFreq$TotalCount <- rowSums(dataFreq[,c(1:N)])
plotData <- data.frame(Features=dataGain$Feature,
TotalGain=dataGain$TotalGain,
AvgGain=dataGain$AverageGain,
AvgFreq=dataFreq$AverageFreq,
Counts=dataFreq$TotalCount)
plotData <- arrange(plotData,desc(AvgGain))
#plot the first 10 total gain features
tmp <- plotData[1:10,]
str <- paste('CD4_T cells - top10 features',sep='')
o1 <- ggplot(tmp, aes(x = reorder(Features,AvgGain), y = AvgGain)) +
geom_bar(stat='identity',width = 0.58,alpha=0.68,size=0.2,color='gray8',fill=db13[n])+
ylab('Avg. Gain (1000 iter.)')+
ggtitle(str)+
theme_bw()+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_blank(),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)+
guides(fill = guide_legend(override.aes = list(size=3),nrow=1,title=""))+
coord_flip()
tmp <- arrange(plotData,desc(AvgGain),Counts)
str <- paste('CD4_T cells',sep='')
o4 <- ggplot(tmp,aes(y=AvgGain,x=Counts))+
geom_point(size = 2,color=db13[n])+
geom_label_repel(aes(label = Features),
box.padding = 0.1,
point.padding = 0.2,
size=3,
segment.color = 'grey50')+
ggtitle(str)+
ylab('Avg. Gain (1000 iter.)')+
xlab('#Times selected')+
theme_bw()+
xlim(0,1000)+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_text( size = 12 ),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)
myfile <- paste('CD4T_Gain.pdf',sep ='')
pdf(myfile)
print(o1)
dev.off()
myfile <- paste('CD4T_Gain_Count.pdf',sep ='')
pdf(myfile)
print(o4)
dev.off()
############################################################################################
############################################################################################
#CD8_T
n <- 3
dataGain <- featureImportanceSummaryGain_CD8T
dataFreq <- featureImportanceSummaryFrequency_CD8T
dataGain$TotalGain <- rowSums(dataGain[,c(1:N)])
dataGain$AverageGain <- rowMeans(dataGain[,c(1:N)])
dataFreq$AverageFreq <- rowMeans(dataFreq[,c(1:N)])
for(i in 1:nrow(dataFreq)){
for(j in 1:N){
if(dataFreq[i,j]!=0){
dataFreq[i,j] <- 1
}
}
}
dataFreq$TotalCount <- rowSums(dataFreq[,c(1:N)])
plotData <- data.frame(Features=dataGain$Feature,
TotalGain=dataGain$TotalGain,
AvgGain=dataGain$AverageGain,
AvgFreq=dataFreq$AverageFreq,
Counts=dataFreq$TotalCount)
plotData <- arrange(plotData,desc(AvgGain))
#plot the first 10 total gain features
tmp <- plotData[1:10,]
str <- paste('CD8_T cells - top10 features',sep='')
o1 <- ggplot(tmp, aes(x = reorder(Features,AvgGain), y = AvgGain)) +
geom_bar(stat='identity',width = 0.58,alpha=0.68,size=0.2,color='gray8',fill=db13[n])+
ylab('Avg. Gain (1000 iter.)')+
ggtitle(str)+
theme_bw()+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_blank(),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)+
guides(fill = guide_legend(override.aes = list(size=3),nrow=1,title=""))+
coord_flip()
tmp <- arrange(plotData,desc(AvgGain),Counts)
str <- paste('CD8_T cells',sep='')
o4 <- ggplot(tmp,aes(y=AvgGain,x=Counts))+
geom_point(size = 2,color=db13[n])+
geom_label_repel(aes(label = Features),
box.padding = 0.1,
point.padding = 0.2,
size=3,
segment.color = 'grey50')+
ggtitle(str)+
ylab('Avg. Gain (1000 iter.)')+
xlab('#Times selected')+
xlim(0,1000)+
theme_bw()+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_text( size = 12 ),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)
myfile <- paste('CD8T_Gain.pdf',sep ='')
pdf(myfile)
print(o1)
dev.off()
myfile <- paste('CD8T_Gain_Count.pdf',sep ='')
pdf(myfile)
print(o4)
dev.off()
############################################################################################
############################################################################################
#HSCs_MPPs
n <- 4
dataGain <- featureImportanceSummaryGain_HSCsMPPs
dataFreq <- featureImportanceSummaryFrequency_HSCsMPPs
dataGain$TotalGain <- rowSums(dataGain[,c(1:N)])
dataGain$AverageGain <- rowMeans(dataGain[,c(1:N)])
dataFreq$AverageFreq <- rowMeans(dataFreq[,c(1:N)])
for(i in 1:nrow(dataFreq)){
for(j in 1:N){
if(dataFreq[i,j]!=0){
dataFreq[i,j] <- 1
}
}
}
dataFreq$TotalCount <- rowSums(dataFreq[,c(1:N)])
plotData <- data.frame(Features=dataGain$Feature,
TotalGain=dataGain$TotalGain,
AvgGain=dataGain$AverageGain,
AvgFreq=dataFreq$AverageFreq,
Counts=dataFreq$TotalCount)
plotData <- arrange(plotData,desc(AvgGain))
#plot the first 10 total gain features
tmp <- plotData[1:10,]
str <- paste('HSCs & MPPs cells - top10 features',sep='')
o1 <- ggplot(tmp, aes(x = reorder(Features,AvgGain), y = AvgGain)) +
geom_bar(stat='identity',width = 0.58,alpha=0.68,size=0.2,color='gray8',fill=db13[n])+
ylab('Avg. Gain (1000 iter.)')+
ggtitle(str)+
theme_bw()+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_blank(),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)+
guides(fill = guide_legend(override.aes = list(size=3),nrow=1,title=""))+
coord_flip()
tmp <- arrange(plotData,desc(AvgGain),Counts)
str <- paste('HSCs & MPPs cells',sep='')
o4 <- ggplot(tmp,aes(y=AvgGain,x=Counts))+
geom_point(size = 2,color=db13[n])+
geom_label_repel(aes(label = Features),
box.padding = 0.1,
point.padding = 0.2,
size=3,
segment.color = 'grey50')+
ggtitle(str)+
ylab('Avg. Gain (1000 iter.)')+
xlab('#Times selected')+
xlim(0,1000)+
theme_bw()+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_text( size = 12 ),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)
myfile <- paste('HSCsMPP_Gain.pdf',sep ='')
pdf(myfile)
print(o1)
dev.off()
myfile <- paste('HSCsMPP_Gain_Count.pdf',sep ='')
pdf(myfile)
print(o4)
dev.off()
############################################################################################
############################################################################################
#Monocytes
n <- 5
dataGain <- featureImportanceSummaryGain_Monocytes
dataFreq <- featureImportanceSummaryFrequency_Monocytes
dataGain$TotalGain <- rowSums(dataGain[,c(1:N)])
dataGain$AverageGain <- rowMeans(dataGain[,c(1:N)])
dataFreq$AverageFreq <- rowMeans(dataFreq[,c(1:N)])
for(i in 1:nrow(dataFreq)){
for(j in 1:N){
if(dataFreq[i,j]!=0){
dataFreq[i,j] <- 1
}
}
}
dataFreq$TotalCount <- rowSums(dataFreq[,c(1:N)])
plotData <- data.frame(Features=dataGain$Feature,
TotalGain=dataGain$TotalGain,
AvgGain=dataGain$AverageGain,
AvgFreq=dataFreq$AverageFreq,
Counts=dataFreq$TotalCount)
plotData <- arrange(plotData,desc(AvgGain))
#plot the first 10 total gain features
tmp <- plotData[1:10,]
str <- paste('Monocytes - top10 features',sep='')
o1 <- ggplot(tmp, aes(x = reorder(Features,AvgGain), y = AvgGain)) +
geom_bar(stat='identity',width = 0.58,alpha=0.68,size=0.2,color='gray8',fill=db13[n])+
ylab('Avg. Gain (1000 iter.)')+
ggtitle(str)+
theme_bw()+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_blank(),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)+
guides(fill = guide_legend(override.aes = list(size=3),nrow=1,title=""))+
coord_flip()
tmp <- arrange(plotData,desc(AvgGain),Counts)
str <- paste('Monocytes',sep='')
o4 <- ggplot(tmp,aes(y=AvgGain,x=Counts))+
geom_point(size = 2,color=db13[n])+
geom_label_repel(aes(label = Features),
box.padding = 0.1,
point.padding = 0.2,
size=3,
segment.color = 'grey50')+
ggtitle(str)+
ylab('Avg. Gain (1000 iter.)')+
xlab('#Times selected')+
xlim(0,1000)+
theme_bw()+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_text( size = 12 ),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)
myfile <- paste('Monocytes_Gain.pdf',sep ='')
pdf(myfile)
print(o1)
dev.off()
myfile <- paste('Monocytes_Gain_Count.pdf',sep ='')
pdf(myfile)
print(o4)
dev.off()
############################################################################################
############################################################################################
#NK
n <- 6
dataGain <- featureImportanceSummaryGain_NK
dataFreq <- featureImportanceSummaryFrequency_NK
dataGain$TotalGain <- rowSums(dataGain[,c(1:N)])
dataGain$AverageGain <- rowMeans(dataGain[,c(1:N)])
dataFreq$AverageFreq <- rowMeans(dataFreq[,c(1:N)])
for(i in 1:nrow(dataFreq)){
for(j in 1:N){
if(dataFreq[i,j]!=0){
dataFreq[i,j] <- 1
}
}
}
dataFreq$TotalCount <- rowSums(dataFreq[,c(1:N)])
plotData <- data.frame(Features=dataGain$Feature,
TotalGain=dataGain$TotalGain,
AvgGain=dataGain$AverageGain,
AvgFreq=dataFreq$AverageFreq,
Counts=dataFreq$TotalCount)
plotData <- arrange(plotData,desc(AvgGain))
#plot the first 10 total gain features
tmp <- plotData[1:10,]
str <- paste('NK cells - top10 features',sep='')
o1 <- ggplot(tmp, aes(x = reorder(Features,AvgGain), y = AvgGain)) +
geom_bar(stat='identity',width = 0.58,alpha=0.68,size=0.2,color='gray8',fill=db13[n])+
ylab('Avg. Gain (1000 iter.)')+
ggtitle(str)+
theme_bw()+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_blank(),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)+
guides(fill = guide_legend(override.aes = list(size=3),nrow=1,title=""))+
coord_flip()
tmp <- arrange(plotData,desc(AvgGain),Counts)
str <- paste('NK cells',sep='')
o4 <- ggplot(tmp,aes(y=AvgGain,x=Counts))+
geom_point(size = 2,color=db13[n])+
geom_label_repel(aes(label = Features),
box.padding = 0.1,
point.padding = 0.2,
size=3,
segment.color = 'grey50')+
ggtitle(str)+
ylab('Avg. Gain (1000 iter.)')+
xlab('#Times selected')+
xlim(0,1000)+
theme_bw()+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_text( size = 12 ),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)
myfile <- paste('NK_Gain.pdf',sep ='')
pdf(myfile)
print(o1)
dev.off()
myfile <- paste('NK_Gain_Count.pdf',sep ='')
pdf(myfile)
print(o4)
dev.off()
############################################################################################
############################################################################################
#pDC
n <- 7
dataGain <- featureImportanceSummaryGain_pDCs
dataFreq <- featureImportanceSummaryFrequency_pDCs
dataGain$TotalGain <- rowSums(dataGain[,c(1:N)])
dataGain$AverageGain <- rowMeans(dataGain[,c(1:N)])
dataFreq$AverageFreq <- rowMeans(dataFreq[,c(1:N)])
for(i in 1:nrow(dataFreq)){
for(j in 1:N){
if(dataFreq[i,j]!=0){
dataFreq[i,j] <- 1
}
}
}
dataFreq$TotalCount <- rowSums(dataFreq[,c(1:N)])
plotData <- data.frame(Features=dataGain$Feature,
TotalGain=dataGain$TotalGain,
AvgGain=dataGain$AverageGain,
AvgFreq=dataFreq$AverageFreq,
Counts=dataFreq$TotalCount)
plotData <- arrange(plotData,desc(AvgGain))
#plot the first 10 total gain features
tmp <- plotData[1:10,]
str <- paste('pDC cells - top10 features',sep='')
o1 <- ggplot(tmp, aes(x = reorder(Features,AvgGain), y = AvgGain)) +
geom_bar(stat='identity',width = 0.58,alpha=0.68,size=0.2,color='gray8',fill=db13[n])+
ylab('Avg. Gain (1000 iter.)')+
ggtitle(str)+
theme_bw()+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_blank(),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)+
guides(fill = guide_legend(override.aes = list(size=3),nrow=1,title=""))+
coord_flip()
tmp <- arrange(plotData,desc(AvgGain),Counts)
str <- paste('pDC cells',sep='')
o4 <- ggplot(tmp,aes(y=AvgGain,x=Counts))+
geom_point(size = 2,color=db13[n])+
geom_label_repel(aes(label = Features),
box.padding = 0.1,
point.padding = 0.2,
size=3,
segment.color = 'grey50')+
ggtitle(str)+
ylab('Avg. Gain (1000 iter.)')+
xlab('#Times selected')+
xlim(0,1000)+
theme_bw()+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, size = 12),
axis.title.x = element_text( size = 12,face = 'bold' ),
axis.title.y = element_text( size = 12 ),
strip.text = element_text(size = 12,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1)
myfile <- paste('pDC_Gain.pdf',sep ='')
pdf(myfile)
print(o1)
dev.off()
myfile <- paste('pDC_Gain_Count.pdf',sep ='')
pdf(myfile)
print(o4)
dev.off()
############################################################################################
############################################################################################
#finally we estimate the average performance per cell type
meanPerformances <- data.frame()
#compute the median per cell type and per method
for(currentCellType in cellTypes){
tmp <- totalPerformanceXGBoostDREMI_1_1[totalPerformanceXGBoostDREMI_1_1$CellType==currentCellType,]
#compute the medians
tmp <- tmp %>%
group_by(CellType) %>%
dplyr::summarize(meanSEN = mean(Sensitivity, na.rm=TRUE),
meanSPE = mean(Specificity, na.rm=TRUE),
meanF1 = mean(F1, na.rm=TRUE),
meanAUC = mean(AUC, na.rm=TRUE))
tmp$CellType <- currentCellType
meanPerformances <- rbind(meanPerformances,tmp)
}
tmp <- melt(meanPerformances,id.vars = c("CellType"))
tmp$CellType <- factor(tmp$CellType,levels = cellTypes)
colnames(tmp)[2] <- 'Metric'
tmp$Metric <- factor(tmp$Metric,levels = c('meanSEN','meanSPE','meanF1','meanAUC'))
performance_summary <- ggplot(tmp, aes(x = CellType, y = value, fill = CellType)) +
geom_bar(stat="identity",size=0.3,width = 0.5,alpha=0.88,color='black')+
facet_wrap(~Metric,ncol = 2)+
#geom_hline(yintercept = 0.9,color='red')+
ylab('Mean performance per cell type')+
theme_bw() +
scale_fill_manual(values = db13,name='')+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 0.05, hjust = 0.95, size = 12),
axis.title.x =element_text( size = 12 ) ,
axis.title.y = element_blank(),
strip.text = element_text(size = 10,face='bold',lineheight=1),
legend.position = "none",aspect.ratio = 1,
legend.text=element_text(size=12))+coord_flip()
myfile <- paste('best_DREMI_avg_performance.pdf',sep ='')
pdf(myfile)
print(performance_summary)
dev.off()
```
## Use the clinical data to find possible associations of selected DREMI features with clinical outcome
In this subsection we use the features from the previous analysis to investigate possible associations with survival.
```{r assocation with survival,echo=TRUE,eval=TRUE,error=TRUE, warning=FALSE,cache=TRUE}
library(survival)
library(survminer)
#clinical info
#load the clinical info --> not allowed to share this data; please contact the authors
metadata_filename <- "AML_Benedicte_cohort/leukemia_cohort_info.xlsx"
md_0h <- read_excel(metadata_filename)
#initialise the top selected features based on the feature the achieves the highest gain and frequency of selection
# B cells -->
myCurrentCellType <- 'B'
mySelectedFeatures <- c('pP38-pSTAT3')
summary_patient_data <- data.frame()
for(idx in 1:nrow(patient_samples)){
myPatientID <- patient_samples[idx,'patient_id']
a <- which(md_0h$Patient_nr_cytobank==myPatientID$patient_id)
if(length(a)>0){
survivalTime <- md_0h[a,"5-year survival (days)"]
chromAbber <- md_0h[a,'Karyotype']
chromAbber <- chromAbber$Karyotype
ELN <- md_0h[a,'ELN 2017 risk']
ELN <- ELN$`ELN 2017 risk`
if(ELN=='NA'){
ELN <- NA
}
if(chromAbber=='NA'){
chromAbber <- NA
}
mutFLT3 <- md_0h[a,'FLT3-ITD']
mutFLT3 <- mutFLT3$`FLT3-ITD`
if(mutFLT3=='Present'){
mutFLT3 <- 'Mutated'
}else{
mutFLT3 <- 'Wt'
}
#fetch the patient cells
promptKey <- paste('P',idx,sep='')
data <- DREMI_feature_vector[DREMI_feature_vector$PatientNr==promptKey &
DREMI_feature_vector$CellType==myCurrentCellType,]
#check for -1 that means no DREMI score available
if(data[1,1]==-1){
a <- 'skip'
}else{
data <- data[,c(mySelectedFeatures,
'PatientNr','PatientID','SurvivalTime','karyotype','FLT3',
'CellType','SurvivalStatus','SurvivalGroup')]
data$ELN <- ELN
summary_patient_data <- rbind(summary_patient_data,data)
}
}
}
#plot 1 based on survival
plot_data <- summary_patient_data[,c(mySelectedFeatures,'SurvivalGroup')]
plot_data <- melt(plot_data,id.vars = 'SurvivalGroup')
plot_data$SurvivalGroup <- factor(plot_data$SurvivalGroup,levels = c('STS','LTS'))
DREMI_B_STS_LTS <- ggplot(plot_data,aes(x=SurvivalGroup,y=(value),fill=SurvivalGroup),color=SurvivalGroup)+
geom_beeswarm(size=0.78,alpha=0.8)+
geom_boxplot(width=0.38 ,size=0.58,alpha=0.8,fatten=TRUE,
position=position_dodge(width=0.78),
outlier.colour = "black",
outlier.shape = 21,
outlier.fill = "gray",
outlier.size = 0.4)+
facet_wrap(~variable,scales = "free_x",ncol = 1)+
theme_bw()+
ggtitle('B cells')+
theme(axis.text.y = element_text( size = 12 ),
axis.text.x = element_text(angle = 0, vjust = 1, hjust = 1, size = 12),
axis.title.y = element_blank(),
axis.title.x = element_text( size = 12 ),
strip.text = element_text(size = 14,face='bold',lineheight=1),
legend.position = "none",
legend.text = element_text(size=12),
strip.background = element_rect(colour = "black", fill = "white"),
plot.title = element_text(size = 14, face = "bold",hjust = 0.5),aspect.ratio = 0.6)+
ylab('DREMI scores')+
scale_fill_manual(values=c('#e6194b', '#3cb44b'),name='')+
theme(panel.spacing = unit(1.8, "lines"))+coord_flip()
#save the abundance plot
myfile <- paste('DREMI_B_STS_LTS.pdf',sep ='')
pdf(myfile)
print(DREMI_B_STS_LTS)
dev.off()
#then we perfrom KP survival analysis by discretising the DREMI values
test <- summary_patient_data
a1 <- quantile(test$`pP38-pSTAT3`)
test$`pP38-pSTAT3` <- ifelse(test$`pP38-pSTAT3`>a1[3],'High','Low')
test$`pP38-pSTAT3` <- factor(test$`pP38-pSTAT3`,levels = c('Low','High'))
data_B <- test[,c(1,2)]
colnames(data_B)[1] <- paste('B_',colnames(data_B)[1],sep='')
colnames(test)[1] <- 'D1'