-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSomaticVariantsReport.Rmd
2227 lines (1772 loc) · 145 KB
/
SomaticVariantsReport.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: "SUMMIT 'basket' trial biomarker genomic report"
author: "Manuel Duval / GeneCreek"
date: "Nov. 04th, 2017"
output:
html_document:
theme: spacelab
highlight: espresso
code_folding: hide
toc: true
toc_depth: 3
toc_float: true
---
version#: `r Sys.time()`
#Scopes
**Describes ErBb2 somatic variations via lolliplots.**
**Reports co-occurrence of somatic variations in oncogenes and tumor suppressor genes with oncoplots.**
**Renders change from baseline and PFS via waterfall and swim plots aligned with oncoplots**
#Data
**biomarker log data cut**
##Methods
-the report is created with the statistical computing and graphics R environment.
-the R markdown script referred to as SUMMITBioMarkerGenomicReport.Rmd reads the set of Erbb2 genotype data as well as MSK-IMPACT and non MSK-IMPACT targeted genes' genotypes form the input SUMMITbiomarkerLogDataCut23-Jun-2017.xls excel formatted file.
-In addition to general purposes packages like dplyr and DT, the two main specific packages applied by the script are [trackViewer](https://bioconductor.org/packages/release/bioc/html/trackViewer.html) and [ComplexHeatmap](https://bioconductor.org/packages/release/bioc/html/ComplexHeatmap.html): these are used to generate lollipop plots and oncoplots respectively. Detail information about the packages version are reported at the end of the document.
##New (11/03/2017)
-Oct. 20 data cut;
##Next
-Once fully parsed, render the list of variants (a QC step);
<hr />
```{r, echo = F, warning = FALSE}
library(knitr)
opts_chunk$set(tidy.opts = list(width.cutoff = 80), tidy = TRUE)
#load the libjvm lib
dyn.load('/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home/lib/server/libjvm.dylib')
```
```{r chunk_packages, echo = TRUE, results = 'hide', message = FALSE, warning = FALSE}
#Dependencies.
sapply(c("XLConnect", "XLConnectJars", "knitr", "ggplot2", "GenomicRanges", "rtracklayer", "trackViewer", "plyr", "dplyr", "DT", "stringr", "tidyr", "magrittr","ComplexHeatmap", "GetoptLong", "gridExtra", "cowplot", "grid"), library, character.only = TRUE)
```
```{r chunk_DataLoad, echo = TRUE, results = 'hide', message = FALSE, warning = FALSE, tidy = TRUE}
#Loading data and setting variables.
#loading worksheet
SummitBioMarker0818 <- loadWorkbook(paste(getwd(), "/data/SUMMITBiomarkerLogDataCut20-OCT-2017.xlsx", sep = ""))
###~~~~setting some global variables
#the Erbb2Features' GRanges object used to draw lolliplots with the Erbb2 domains.
#instantiating the Erbb2Features GenomicRanges object which holds ErBb2 protein domains.
Erbb2Features <- GRanges("ERBB2", IRanges(c(1, 652, 675, 720, 1003), width = c(651, 23, 45, 285, 405), names = c("Extracellular", "TM", "JM", "Kinase", "Tail")))
Erbb2Features$fill <- c("bisque", "coral2", "darkseagreen1","skyblue", "chartreuse2")
Erbb2Features$height <- c(0.02, 0.05, 0.035, 0.04, 0.03)
#Oct. 3rd, 2017: adjusting height of the ERBB2 domain in the context of enlarging the lolliplot
Erbb2Features2 <- Erbb2Features
Erbb2Features2$height <- c(0.01, 0.025, 0.017, 0.02, 0.015)
#instantiating the Erbb2 kinase domain GenomicRanges object which holds ErBb2 kinase domain only.
Erbb2KinaseFeature <- GRanges("ERBB2", IRanges(c(1), width = c(200)), names = c("Kinase"))
Erbb2KinaseFeature$fill <- c ("skyblue")
Erbb2KinaseFeature$height <- c(0.05)
#setting the Best Overall Response legend for the lolliplots
response <- c("ND", "PD", "SD", "PR", "CR")
response.color.set <- as.list(as.data.frame(rbind(c("gray", "black", "yellow", "green", "red"), "#FFFFFFFF"), stringsAsFactors = FALSE))
names(response.color.set) <- response
###~~~~
#function for the oncoplot
alter_fun = list(
background = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.8, "mm"), h-unit(0.1, "mm"), gp = gpar(fill = "#CCCCCC", col = NA))
},
Missense = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.8, "mm"), h-unit(0.1, "mm"), gp = gpar(fill = "green", col = NA))
},
DeepDel = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.8, "mm"), h-unit(0.1, "mm"), gp = gpar(fill = "blue", col = NA))
},
AMP = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.8, "mm"), h-unit(0.1, "mm"), gp = gpar(fill = "red", col = NA))
},
Promoter = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.8, "mm"), h-unit(0.1, "mm"), gp = gpar(fill = "purple", col = NA))
},
Nonsense = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.8, "mm"), h-unit(0.1, "mm"), gp = gpar(fill = "black", col = NA))
},
MissenseAmp = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.5, "mm"), h*0.33, gp = gpar(fill = "cyan", col = NA))
},
fusionAmp = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.5, "mm"), h*0.33, gp = gpar(fill = "cadetblue1", col = NA))
},
splice = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.5, "mm"), h-unit(0.1, "mm"), gp = gpar(fill = "coral3", col = NA))
},
Gain = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.8, "mm"), h-unit(0.1, "mm"), gp = gpar(fill = "darksalmon", col = NA))
},
rearrangement = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.8, "mm"), h-unit(0.1, "mm"), gp = gpar(fill = "khaki", col = NA))
},
Indel = function(x, y, w, h) {
grid.rect(x, y, w-unit(0.5, "mm"), h-unit(0.1, "mm"), gp = gpar(fill = "darkgreen", col = NA))
}
)
col = c("Missense" = "green", "AMP" = "red", "Promoter" = "purple", "DeepDel" = "blue", "Nonsense" = "black", "MissenseAmp" = "cyan", "Indel" = "darkgreen", "splice" = "coral3", "Gain" = "darksalmon", "rearrangement" = "khaki", "fusionAmp" = "cadetblue1")
```
#Biliary
##Distributions of ErBb2 somatic variants w.r.t. clinical endpoints.
```{r chunk_biliary, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE}
#reading biliary enrolment data set
biliary <- readWorksheet(SummitBioMarker0818, sheet = "Biliary tract", endCol = 17, endRow = 14)
biliary[biliary == ""] <- NA
#tabular views
kable(t(as.matrix(table(biliary$Mutation.code))), caption = "ErBb2 somatic variants occurences")
kable(table(biliary$Mutation.code, biliary$Primary.Cell.Type), caption = "ErbB2 mutation by Primary Cell Type")
kable(table(biliary$Mutation.code, biliary$Objective.Response.at.Week.8), caption = "ErbB2 mutation by Objective Response at Week 8")
#~Best overall response
#keep first 2 characters for the Best.Overall.Response's attribute value
biliary$Best.Overall.Response <- as.factor(str_sub(biliary$Best.Overall.Response, 1, 2))
#setting values to the Best.Overall.Response attribute
biliary$Best.Overall.Response <- factor(biliary$Best.Overall.Response, levels = c("CR", "PR", "SD", "PD", "NA"), ordered = TRUE)
#reporting it via a table
BestOverallResPerVar <- as.data.frame.matrix(table(biliary$Mutation.code, biliary$Best.Overall.Response))
datatable(BestOverallResPerVar, caption = "ErbB2 mutation by Best Overall Response")
ClinicalBenefitPerVar <- as.data.frame.matrix(table(biliary$Mutation.code, biliary$Clinical.Benefit))
datatable(ClinicalBenefitPerVar, caption = "ErbB2 mutations by Clinical Benefit Status")
#biliary$Clinical.Benefit <- revalue(biliary$Clinical.Benefit, c("pending" = "NA"))
```
<hr />
##Lolliplot: ErBb2 somatic variants annotated with Best Overall Response values
```{r chunk_biliarySNV, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE}
#adding one variable, i.e. the somatic variant coordinate w.r.t. ErbB2 aa sequence
biliary$ErBb2SomaticVarCoord <- as.numeric(str_extract(biliary$Mutation.code, "\\d+"))
#adding two additional variables: the wild type aa and the substituted aa
biliary$ErBb2WildTypeAA <- str_sub(biliary$Mutation.code, 1, 1)
biliary$ErBb2SubsAA <- gsub("^\\w{1}\\d+", "", biliary$Mutation.code, perl = TRUE)
#reordering the dataframe according to the aa coordinate
biliary <- biliary[order(biliary$ErBb2SomaticVarCoord),]
biliaryDf1 <- data.frame(biliary$ErBb2SomaticVarCoord, paste0(biliary$ErBb2WildTypeAA, biliary$ErBb2SomaticVarCoord), biliary$ErBb2SubsAA, biliary$Best.Overall.Response)
#the biliaryDf1 df Erbb2Features 4 columns, the coordinates where the variations map, the WT aa with the coordinate, the variant AA and the clinical response.
colnames(biliaryDf1) <- c("coord", "WTaaCoord", "Var", "Response")
#creating a 2nd df in order to concatenate the substituions mapping to the same coordinate
biliaryDf2 <- as.data.frame(unique(biliaryDf1[,-4]) %>% group_by(WTaaCoord, coord) %>% do(Varalleles = paste(.$Var, collapse = "/")) %>% ungroup() %>% mutate(Varalleles = unlist(Varalleles)))
#adding one variable referred to as SNV which reads the WT aa, the coordinate and the substituted aa
biliaryDf2$SNV <- paste0(biliaryDf2$WTaaCoord, biliaryDf2$Varalleles)
#adding it to the initial biliaryDf1 df
biliaryDf1 <- merge(biliaryDf1, biliaryDf2[,-c(2,3)], by = "WTaaCoord")
#adding one attribute: concatenation of the clinical response and the variant
biliaryDf1$VarRes <- paste0(biliaryDf1$Response, biliaryDf1$SNV)
# replacing NA with the 'ND' string value for Not Determined
biliaryDf1$Response <- gsub("NA", "ND", biliaryDf1$Response)
# set order for clinical response value to bottom CR, PR, SD, PD, ND
biliaryDf1$Response <- str_replace_all(str_c(biliaryDf1$Response), c(CR = "E-CR", PR = "D-PR", SD = "C-SD", PD = "B-PD", ND = "A-ND"))
# order the df
biliaryDf1 <- biliaryDf1[order(biliaryDf1$coord, biliaryDf1$Response), ]
##
biliaryDf3 <- biliaryDf1
##
biliaryDf1 <- unique(ddply(biliaryDf1[,-c(3)], "VarRes", mutate, score = length(VarRes)))
biliaryDf1 <- biliaryDf1[order(biliaryDf1$coord, biliaryDf1$VarRes),]
#instantiating the GRanges object with the coordinates and names of somatic variations
biliary.gr <- GRanges("ERBB2", IRanges(unlist(sapply(1:dim(biliaryDf1)[1], function(i) rep(biliaryDf1$coord[i], biliaryDf1$score[i]))), width = 1, names = unlist(sapply(1:dim(biliaryDf1)[1], function(i) rep(biliaryDf1$SNV[i], biliaryDf1$score[i])))))
#adding the stack.factor attribute
biliary.gr$stack.factor <- unlist(sapply(1:dim(biliaryDf1)[1], function(i) paste0(biliaryDf1$Response[i], "_", seq(1:biliaryDf1$score[i]))))
biliary.gr$value1 <- 100
biliary.gr$value2 <- 100 - biliary.gr$value1
biliary.gr$color <- response.color.set[gsub("_\\d*|\\w*-", "", biliary.gr$stack.factor)]
legend <- list(labels = response, col = "gray80", fill = sapply(response.color.set, `[`, 1))
#editing the gr object's stack.factor variable
biliaryDf3 <- ddply(biliaryDf3, .(WTaaCoord), mutate, idx = seq_along(coord))
biliaryDf3 <- biliaryDf3[order(biliaryDf3$coord, biliaryDf3$Response),]
biliaryDf3$idx <- str_replace_all(str_c(biliaryDf3$idx), c(`1` = "A", `2` = "B", `3` = "C", `4` = "D"))
biliary.gr$stack.factor <- as.character(biliaryDf3$idx)
#plotting
lolliplot(biliary.gr, Erbb2Features, type = "pie.stack", legend = legend, dashline.col = "gray", cex = .7)
```
##Oncoplot: co-occurrence of somatic variations (both IMPACT and NON-IMPACT data sets)
```{r chunkBiliaryOncoplot, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.width = 7, fig.height = 6.2}
#load co-occurring somatic variants
biliarySomatics <- readWorksheet(SummitBioMarker0818, sheet = "Biliary tract", endCol = 7, startRow = 19, endRow = 111)
#Nov3: filling out blank cells.
biliarySomatics <- biliarySomatics %>% fill(Subject.Identifier.for.the.Study)
#IMPACT data
biliarySomaticsImpact <- biliarySomatics[(biliarySomatics$Gene != "not done"),]
#subsetting
biliarySomaticsImpact <- biliarySomaticsImpact[,c(1,3,4)]
#filtering out duplication rows
biliarySomaticsImpact <- unique(biliarySomaticsImpact)
#set Subject.id to character
biliarySomaticsImpact$Subject.Identifier.for.the.Study <- as.character(biliarySomaticsImpact$Subject.Identifier.for.the.Study)
#filtering it out rows where missing values
biliarySomaticsImpact <- na.omit(biliarySomaticsImpact)
#concatenate variations' types whenever occuring in a given gene for a given subject
biliarySomaticsImpact <- biliarySomaticsImpact %>% group_by(Subject.Identifier.for.the.Study, Gene) %>% do(Alteration = paste(.$Alteration, collapse = '')) %>% ungroup() %>% mutate(Alteration = unlist(Alteration))
mat <- as.data.frame(spread(biliarySomaticsImpact, Subject.Identifier.for.the.Study, Alteration))
row.names(mat) <- mat$Gene
mat <- mat[,-1]
#Non IMPACT data
biliarySomaticsNonImpact <- biliarySomatics[(biliarySomatics$Gene == "not done"),]
biliarySomaticsNonImpact <- biliarySomaticsNonImpact[,c(1,6,7)]
biliarySomaticsNonImpact <- unique(biliarySomaticsNonImpact)
biliarySomaticsNonImpact$Subject.Identifier.for.the.Study <- as.character(paste(biliarySomaticsNonImpact$Subject.Identifier.for.the.Study, "*", sep = ""))
biliarySomaticsNonImpact <- na.omit(biliarySomaticsNonImpact)
biliarySomaticsNonImpact <- biliarySomaticsNonImpact %>% group_by(Subject.Identifier.for.the.Study, Gene.1) %>% do(Alteration.1 = paste(.$Alteration.1, collapse = '')) %>% ungroup() %>% mutate(Alteration.1 = unlist(Alteration.1))
#creating the mutations matrix for oncoprint
mat2 <- as.data.frame(spread(biliarySomaticsNonImpact, Subject.Identifier.for.the.Study, Alteration.1))
mat2 <- subset(mat2, select = colnames(mat2) != "NA*")
row.names(mat2) <- mat2$Gene.1
mat2 <- mat2[,-1]
mat <- merge(mat, mat2, by = "row.names", all = T)
row.names(mat) <- mat$Row.names
mat <- mat[,-1]
oncoPrint(mat, get_type = function(x) strsplit(x, ";")[[1]], alter_fun = alter_fun, col = col, row_names_gp = gpar(fontsize = 6), pct_gp = gpar(fontsize = 6), column_title = "Oncoplot for biliary tumor", heatmap_legend_param = list(title = "Alterations", at = c("Missense", "AMP", "Nonsense", "Indel", "splice", "Gain", "Promoter", "DeepDel", "rearrangement", "MissenseAmp"), labels = c("Missense", "Amplification", "Nonsense", "Indel", "splice", "Gain", "Promoter", "Deep Del", "rearrangement","Missense & Amp")), show_column_names = TRUE)
```
##Waterfall and swim plots with oncoplot
```{r BiliaryWF, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.align='left', fig.height = 4, fig.width = 5.3}
#Waterfalls
biliaryWF <- biliary[,c(1, 12, 13, 17)]
#selecting records for which there are IMPACT genotype data
biliaryWF <- biliaryWF[(biliaryWF$Subject.Identifier.for.the.Study %in% intersect(biliaryWF$Subject.Identifier.for.the.Study, gsub("\\*", "", colnames(mat)))),]
#biliaryWF <- biliaryWF[complete.cases(biliaryWF$Percentage.Change.of.Tumor.Measurement),]
biliaryWF <- biliaryWF[order(-biliaryWF$Percentage.Change.of.Tumor.Measurement),]
biliaryWF$Subject.Identifier.for.the.Study <- as.character(biliaryWF$Subject.Identifier.for.the.Study)
biliaryWF$Best.Overall.Response <- factor(biliaryWF$Best.Overall.Response, levels = c("CR", "PR", "SD", "PD", "NA"))
g1 <- ggplot(biliaryWF, aes(x = reorder(Subject.Identifier.for.the.Study, -Percentage.Change.of.Tumor.Measurement), y = Percentage.Change.of.Tumor.Measurement, fill = Best.Overall.Response)) + geom_col() + ylab("Change from baseline (%)") + theme_bw() + ylim(-100, 100) + theme(axis.title.x = element_blank(), axis.text.x = element_blank(), axis.title.y = element_text(size = 9), axis.text.y = element_text(size = 6), legend.text = element_text(size = 5), legend.title = element_text(size = 7), legend.position = "top", plot.margin = unit(c(0,0,0,1.6), "cm"))
#+ scale_y_continuous(position = "right")
g2 <- ggplot(biliaryWF, aes(x = reorder(Subject.Identifier.for.the.Study, -Percentage.Change.of.Tumor.Measurement), y = Progression.Free.Survival.Time..Months., fill = Best.Overall.Response)) +
geom_col() + ylab("PFS (mo)") + theme_bw() + theme(axis.title.x = element_blank(), axis.text.x = element_text(size = 6, angle = 90, hjust = 1), axis.title.y = element_text(size = 9), axis.text.y = element_text(size = 6)) + guides(fill=FALSE)
plot_grid(g1, g2, nrow = 2, align = "v")
```
```{r BiliarylOncoplot2, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.width= 6.9, fig.height = 6.0, fig.align = 'center'}
#given the occurrences of missing data for the %change of tumor measurement variable, subset out the mat matrix for the Subject.Identifier where such missing data.
d1 <- setdiff(gsub("\\*", "", colnames(mat)), biliaryWF$Subject.Identifier.for.the.Study)
mat2 <- mat[, !colnames(mat) %in% grep(paste(d1, collapse = "|"), colnames(mat), value = T)]
#creating a df for the purpose of reordering the mat object passed to the Oncoprint function
df1 <- biliaryWF[,c(1,4)]
df2<- as.data.frame(colnames(mat2))
colnames(df2) <- "subjid"
df2$Subject.Identifier.for.the.Study <- gsub("\\*", "", df2$subjid)
df3 <- merge(df1, df2, by = "Subject.Identifier.for.the.Study", sort = F)
oncoPrint(mat2[c(as.character(df3$subjid))], get_type = function(x) strsplit(x, ";")[[1]], alter_fun = alter_fun, col = col, row_names_gp = gpar(fontsize = 6), pct_gp = gpar(fontsize = 6), heatmap_legend_param = list(title = "Alterations", at = c("Missense", "AMP", "Nonsense", "Indel", "splice", "Gain", "Promoter", "DeepDel", "rearrangement", "MissenseAmp"), labels = c("Missense", "Amplification", "Nonsense", "Indel", "splice", "Gain", "Promoter", "Deep Del", "rearrangement","Missense & Amp")), show_column_names = TRUE, column_order = NULL)
```
<hr />
<hr />
#Bladder
##Distributions of ErBb2 somatic variants w.r.t. clinical endpoints.
```{r bladderDf, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE}
#reading bladder enrolment data set
bladder <- readWorksheet(SummitBioMarker0818, sheet = "bladder", endCol = 17, endRow = 17)
bladder[bladder == ""] <- NA
#tabular views
kable(t(as.matrix(table(bladder$Mutation.code))), caption = "ErBb2 somatic variants occurences")
kable(table(bladder$Mutation.code, bladder$Cancer.type), caption = "ErbB2 mutation by Primary Cell Type")
kable(table(bladder$Mutation.code, bladder$Objective.Response.at.Week.8), caption = "ErbB2 mutation by Objective Response at Week 8")
#~Best overall response
#keep first 2 characters for the Best.Overall.Response's attribute value
bladder$Best.Overall.Response <- as.factor(str_sub(bladder$Best.Overall.Response, 1, 2))
#setting values to the Best.Overall.Response attribute
bladder$Best.Overall.Response <- factor(bladder$Best.Overall.Response, levels = c("CR", "PR", "SD", "PD", "NA"), ordered = TRUE)
BestOverallResPerVar <- as.data.frame.matrix(table(bladder$Mutation.code, bladder$Best.Overall.Response))
datatable(BestOverallResPerVar, caption = "ErbB2 mutation by Best Overall Response")
ClinicalBenefitPerVar <- as.data.frame.matrix(table(bladder$Mutation.code, bladder$Clinical.Benefit))
datatable(ClinicalBenefitPerVar, caption = "ErbB2 mutations by Clinical Benefit Status")
bladder$Clinical.Benefit <- revalue(bladder$Clinical.Benefit, c("pending"="N/A"))
bladder$Clinical.Benefit <- factor(bladder$Clinical.Benefit, levels = c("YES", "NO", "N/A"), ordered = TRUE)
```
<hr />
##Lolliplot: Annotated ErBb2 somatic variants
```{r bladderLolliplot, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE}
# adding one variable, i.e. the somatic variant coordinate w.r.t. ErbB2 aa
# sequence
bladder$ErBb2SomaticVarCoord <- as.numeric(str_extract(bladder$Mutation.code, "\\d+"))
# adding two additional variables: the wild type aa and the substituted aa
bladder$ErBb2WildTypeAA <- str_sub(bladder$Mutation.code, 1, 1)
bladder$ErBb2SubsAA <- gsub("^\\w{1}\\d+", "", bladder$Mutation.code, perl = T)
# reordering the dataframe according to the aa coordinate
bladder <- bladder[order(bladder$ErBb2SomaticVarCoord), ]
#creating the data structure for the GRanges() GenomicRanges package function
bladderDf1 <- data.frame(bladder$ErBb2SomaticVarCoord, paste0(bladder$ErBb2WildTypeAA, bladder$ErBb2SomaticVarCoord), bladder$ErBb2SubsAA, bladder$Best.Overall.Response)
#the bladderDf1 df Erbb2Features 4 columns, the coordinates where the variations map, the WT aa with the coordinate, the variant AA and the clinical response.
colnames(bladderDf1) <- c("coord", "WTaaCoord", "Var", "Response")
#creating a 2nd df in order to concatenate the substitutions mapping to the same coordinate
bladderDf2 <- as.data.frame(unique(bladderDf1[,-4]) %>% group_by(WTaaCoord, coord) %>% do(Varalleles = paste(.$Var, collapse = "/")) %>% ungroup() %>% mutate(Varalleles = unlist(Varalleles)))
#adding one variable referred to as SNV which reads the WT aa, the coordinate and the substituted aa
bladderDf2$SNV <- paste0(bladderDf2$WTaaCoord, bladderDf2$Varalleles)
#adding it fo the initial bladderDf1 df
bladderDf1 <- merge(bladderDf1, bladderDf2[,-c(2,3)], by = "WTaaCoord")
#adding one attribute: concatenation of the clinical response and the variant
bladderDf1$VarRes <- paste0(bladderDf1$Response, bladderDf1$SNV)
# replacing NA with the 'ND' string value for Not Determined
bladderDf1$Response <- gsub("NA", "ND",bladderDf1$Response)
#set order for clinical response value to bottom CR, PR, SD, PD, ND
bladderDf1$Response <- str_replace_all(str_c(bladderDf1$Response), c("CR" = "E-CR", "PR" = "D-PR", "SD" = "C-SD", "PD" = "B-PD", "ND" = "A-ND"))
#order the df
bladderDf1 <- bladderDf1[order(bladderDf1$coord, bladderDf1$Response),]
##
bladderDf3 <- bladderDf1
##
bladderDf1 <- unique(ddply(bladderDf1[,-c(3)], "VarRes", mutate, score = length(VarRes)))
bladderDf1 <- bladderDf1[order(bladderDf1$coord, bladderDf1$Response),]
#instantiating the GRanges object with the coordinates and names of somatic variations
bladder.gr <- GRanges("ERBB2", IRanges(unlist(sapply(1:dim(bladderDf1)[1], function(i) rep(bladderDf1$coord[i], bladderDf1$score[i]))), width = 1, names = unlist(sapply(1:dim(bladderDf1)[1], function(i) rep(bladderDf1$SNV[i], bladderDf1$score[i])))))
#adding the stack.factor attribute
bladder.gr$stack.factor <- unlist(sapply(1:dim(bladderDf1)[1], function(i) paste0(bladderDf1$Response[i], "_", seq(1:bladderDf1$score[i]))))
bladder.gr$value1 <- 100
bladder.gr$value2 <- 100 - bladder.gr$value1
bladder.gr$color <- response.color.set[gsub("_\\d*|\\w*-", "", bladder.gr$stack.factor)]
legend <- list(labels = response, col = "gray80", fill = sapply(response.color.set, `[`, 1))
bladderDf3 <- ddply(bladderDf3, .(WTaaCoord), mutate, idx = seq_along(coord))
bladderDf3 <- bladderDf3[order(bladderDf3$coord, bladderDf3$Response),]
bladderDf3$idx <- str_replace_all(str_c(bladderDf3$idx), c("10" = "J", "11" = "K", "1" = "A", "2" = "B", "3" = "C", "4" = "D", "5" = "E", "6" = "F", "7" = "G", "8" = "H", "9" = "I"))
bladder.gr$stack.factor <- as.character(bladderDf3$idx)
#plotting
lolliplot(bladder.gr, Erbb2Features, type = "pie.stack", legend = legend, dashline.col = "gray", cex = .7)
```
##Oncoplot: co-occurrence of somatic variations (both IMPACT and NON-IMPACT data sets)
```{r chunkBladderOncoplot, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.width = 7.1, fig.height = 10.4}
#load co-occurring somatic variants
bladderSomatics <- readWorksheet(SummitBioMarker0818, sheet = "Bladder", endCol = 7, startRow = 21, endRow = 270)
#IMPACT data
bladderSomaticsImpact <- bladderSomatics[(bladderSomatics$Gene != "not done"),]
#subsetting
bladderSomaticsImpact <- bladderSomaticsImpact[,c(1,3,4)]
#filtering out duplication rows
bladderSomaticsImpact <- unique(bladderSomaticsImpact)
#set Subject.id to character
bladderSomaticsImpact$Subject.Identifier.for.the.Study <- as.character(bladderSomaticsImpact$Subject.Identifier.for.the.Study)
#filtering it out rows where missing values
bladderSomaticsImpact <- na.omit(bladderSomaticsImpact)
#concatenate variations' types whenever occuring in a given gene for a given subject
bladderSomaticsImpact <- bladderSomaticsImpact %>% group_by(Subject.Identifier.for.the.Study, Gene) %>% do(Alteration = paste(.$Alteration, collapse = '')) %>% ungroup() %>% mutate(Alteration = unlist(Alteration))
mat <- as.data.frame(spread(bladderSomaticsImpact, Subject.Identifier.for.the.Study, Alteration))
row.names(mat) <- mat$Gene
mat <- mat[,-1]
#Non IMPACT data
bladderSomaticsNonImpact <- bladderSomatics[(bladderSomatics$Gene == "not done"),]
bladderSomaticsNonImpact <- bladderSomaticsNonImpact[,c(1,6,7)]
bladderSomaticsNonImpact <- unique(bladderSomaticsNonImpact)
bladderSomaticsNonImpact$Subject.Identifier.for.the.Study <- as.character(paste(bladderSomaticsNonImpact$Subject.Identifier.for.the.Study, "*", sep = ""))
bladderSomaticsNonImpact <- na.omit(bladderSomaticsNonImpact)
bladderSomaticsNonImpact <- bladderSomaticsNonImpact %>% group_by(Subject.Identifier.for.the.Study, Gene.1) %>% do(Alteration.1 = paste(.$Alteration.1, collapse = '')) %>% ungroup() %>% mutate(Alteration.1 = unlist(Alteration.1))
#creating the mutations matrix for oncoprint
mat2 <- as.data.frame(spread(bladderSomaticsNonImpact, Subject.Identifier.for.the.Study, Alteration.1))
mat2 <- subset(mat2, select = colnames(mat2) != "NA*")
row.names(mat2) <- mat2$Gene.1
mat2 <- mat2[,-1]
mat <- merge(mat, mat2, by = "row.names", all = T)
row.names(mat) <- mat$Row.names
mat <- mat[,-1]
oncoPrint(mat, get_type = function(x) strsplit(x, ";")[[1]], alter_fun = alter_fun, col = col, row_names_gp = gpar(fontsize = 6), pct_gp = gpar(fontsize = 6), column_title = "Oncoplot for bladder tumor", heatmap_legend_param = list(title = "Alterations", at = c("Missense", "AMP", "Nonsense", "Indel", "splice", "Gain", "Promoter", "DeepDel", "rearrangement", "MissenseAmp"), labels = c("Missense", "Amplification", "Nonsense", "Indel", "splice", "Gain", "Promoter", "Deep Del", "rearrangement","Missense & Amp")), show_column_names = TRUE)
```
##Waterfall and swim plots with oncoplot
```{r BladderWF, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.align='left', fig.height= 4, fig.width= 4.8}
#Waterfalls
bladderWF <- bladder[,c(1, 12, 13, 17)]
#selecting records for which there are IMPACT and Non-IMPACT genotype data
bladderWF <- bladderWF[(bladderWF$Subject.Identifier.for.the.Study %in% intersect(bladderWF$Subject.Identifier.for.the.Study, gsub("\\*", "", colnames(mat)))),]
#bladderWF <- bladderWF[complete.cases(bladderWF$Percentage.Change.of.Tumor.Measurement),]
bladderWF <- bladderWF[order(-bladderWF$Percentage.Change.of.Tumor.Measurement),]
bladderWF$Subject.Identifier.for.the.Study <- as.character(bladderWF$Subject.Identifier.for.the.Study)
g1 <- ggplot(bladderWF, aes(x = reorder(Subject.Identifier.for.the.Study, -Percentage.Change.of.Tumor.Measurement), y = Percentage.Change.of.Tumor.Measurement, fill = Best.Overall.Response)) +
geom_col() + ylab("Change from baseline (%)") + theme_bw() + ylim(-100, 100) + theme(axis.title.x = element_blank(), axis.text.x = element_blank(), axis.title.y = element_text(size = 9), axis.text.y = element_text(size = 6), legend.text = element_text(size = 6), legend.title = element_text(size = 8), plot.margin = unit(c(0,0,0,1.6), "cm"), legend.position = "top")
g2 <- ggplot(bladderWF, aes(x = reorder(Subject.Identifier.for.the.Study, -Percentage.Change.of.Tumor.Measurement), y = Progression.Free.Survival.Time..Months., fill = Best.Overall.Response)) +
geom_col() + ylab("PFS (mo)") + theme_bw() + theme(axis.title.x = element_blank(), axis.text.x = element_text(size = 6, angle = 90, hjust = 1), axis.title.y = element_text(size = 9), axis.text.y = element_text(size = 6), legend.position = "bottom") + guides(fill=FALSE)
plot_grid(g1, g2, nrow = 2, align = "v")
```
```{r bladderOncoplot2, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.width= 6.8, fig.height = 10, fig.align = 'center'}
#given the occurrences of missing data for the %change of tumor measurement variable, subset out the mat matrix for the Subject.Identifier where such missing data.
d1 <- setdiff(gsub("\\*", "", colnames(mat)), bladderWF$Subject.Identifier.for.the.Study)
mat2 <- mat[, !colnames(mat) %in% grep(paste(d1, collapse = "|"), colnames(mat), value = T)]
#creating a df for the purpose of reordering the mat object passed to the Oncoprint function
df1 <- bladderWF[,c(1,4)]
df2<- as.data.frame(colnames(mat2))
colnames(df2) <- "subjid"
df2$Subject.Identifier.for.the.Study <- gsub("\\*", "", df2$subjid)
df3 <- merge(df1, df2, by = "Subject.Identifier.for.the.Study", sort = F)
mat2 <- mat2[c(as.character(df3$subjid))]
mat2 <- mat2[rowSums(is.na(mat2)) < dim(mat2)[2],]
oncoPrint(mat2, get_type = function(x) strsplit(x, ";")[[1]], alter_fun = alter_fun, col = col, row_names_gp = gpar(fontsize = 6), pct_gp = gpar(fontsize = 6), heatmap_legend_param = list(title = "Alterations", at = c("Missense", "AMP", "Nonsense", "Indel", "splice", "Gain", "Promoter", "DeepDel", "rearrangement", "MissenseAmp"), labels = c("Missense", "Amplification", "Nonsense", "Indel", "splice", "Gain", "Promoter", "Deep Del", "rearrangement","Missense & Amp")), show_column_names = TRUE, column_order = NULL)
```
<hr />
<hr />
#Breast mono
##Distributions of ErBb2 somatic variants w.r.t. clinical endpoints.
```{r breast_mono, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE}
#reading biliary enrolment data set
Breast_mono <- readWorksheet(SummitBioMarker0818, sheet = "Breast mono", endCol = 18, endRow = 34)
Breast_mono[Breast_mono == ""] <- NA
#tabular views
kable(t(as.matrix(table(Breast_mono$Mutation.code))), caption = "ErBb2 somatic variants occurences")
kable(table(Breast_mono$Mutation.code, Breast_mono$Primary.Cell.Type), caption = "ErbB2 mutation by Primary Cell Type")
kable(table(Breast_mono$Mutation.code, Breast_mono$Objective.Response.at.Week.8), caption = "ErbB2 mutation by Objective Response at Week 8")
#~Best overall response
#keep first 2 characters for the Best.Overall.Response's attribute value
Breast_mono$Best.Overall.Response <- as.factor(str_sub(Breast_mono$Best.Overall.Response, 1, 2))
#setting values to the Best.Overall.Response attribute
Breast_mono$Best.Overall.Response <- factor(Breast_mono$Best.Overall.Response, levels = c("CR", "PR", "SD", "PD", "NA"), ordered = TRUE)
BestOverallResPerVar <- as.data.frame.matrix(table(Breast_mono$Mutation.code, Breast_mono$Best.Overall.Response))
#kable(table(Breast_mono$Mutation.code, Breast_mono$Best.Overall.Response), caption = "ErbB2 mutation by Best Overall Response")
datatable(BestOverallResPerVar, caption = "ErbB2 mutation by Best Overall Response")
ClinicalBenefitPerVar <- as.data.frame.matrix(table(Breast_mono$Mutation.code, Breast_mono$Clinical.Benefit))
datatable(ClinicalBenefitPerVar, caption = "ErbB2 mutations by Clinical Benefit Status")
#Breast_mono$Clinical.Benefit <- revalue(Breast_mono$Clinical.Benefit, c("pending"="N/A"))
```
<hr />
##Lolliplot: ErBb2 somatic variants annotated with Best Overall Response values
```{r Breast_monoSNV, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.height = 10, fig.width = 10}
#adding one variable, i.e. the somatic variant coordinate w.r.t. ErbB2 aa sequence
Breast_mono$ErBb2SomaticVarCoord <- as.numeric(str_extract(Breast_mono$Mutation.code, "\\d+"))
#adding two additional variables: the wild type aa and the substituted aa
Breast_mono$ErBb2WildTypeAA <- str_sub(Breast_mono$Mutation.code, 1, 1)
Breast_mono$ErBb2SubsAA <- gsub("^\\w{1}\\d+", "", Breast_mono$Mutation.code, perl = TRUE)
#reordering the dataframe according to the aa coordinate
Breast_mono <- Breast_mono[order(Breast_mono$ErBb2SomaticVarCoord),]
#setting the L755_E757delinsS variant to a neighbor coordinate for lolliplot rendering
Breast_mono[13, 19] <- 754
#for Subject.Identifier.for.the.Study #1489 with 2 varianst, set the 2nd one to neighbor coordinate for lolliplot rendering
Breast_mono[27, 19] <- 779
#creating the data structure for the GRanges() GenomicRanges package function
Breast_monoDf1 <- data.frame(Breast_mono$ErBb2SomaticVarCoord, paste0(Breast_mono$ErBb2WildTypeAA, Breast_mono$ErBb2SomaticVarCoord), Breast_mono$ErBb2SubsAA, Breast_mono$Best.Overall.Response)
#the Breast_monoDf1 df Erbb2Features 4 columns, the coordinates where the variations map, the WT aa with the coordinate, the variant AA and the clinical response.
colnames(Breast_monoDf1) <- c("coord", "WTaaCoord", "Var", "Response")
#creating a 2nd df in order to concatenate the substituions mapping to the same coordinate
Breast_monoDf2 <- as.data.frame(unique(Breast_monoDf1[,-4]) %>% group_by(WTaaCoord, coord) %>% do(Varalleles = paste(.$Var, collapse = "/")) %>% ungroup() %>% mutate(Varalleles = unlist(Varalleles)))
#adding one variable referred to as SNV which reads the WT aa, the coordinate and the substituted aa
Breast_monoDf2$SNV <- paste0(Breast_monoDf2$WTaaCoord, Breast_monoDf2$Varalleles)
#adding it fo the initial Breast_monoDf1 df
Breast_monoDf1 <- merge(Breast_monoDf1, Breast_monoDf2[,-c(2,3)], by = "WTaaCoord")
#adding one attribute: concatenation of the clinical response and the variant
Breast_monoDf1$VarRes <- paste0(Breast_monoDf1$Response, Breast_monoDf1$SNV)
# replacing NA with the 'ND' string value for Not Determined
Breast_monoDf1$Response <- gsub("NA", "ND",Breast_monoDf1$Response)
#set order for clinical response value to bottom CR, PR, SD, PD, ND
Breast_monoDf1$Response <- str_replace_all(str_c(Breast_monoDf1$Response), c("CR" = "E-CR", "PR" = "D-PR", "SD" = "C-SD", "PD" = "B-PD", "ND" = "A-ND"))
#order the df
Breast_monoDf1 <- Breast_monoDf1[order(Breast_monoDf1$coord, Breast_monoDf1$Response),]
#duplicated the Breast_monoDf1 Df object for later plotting on the kinase domain
Breast_monoDf1_2 <- Breast_monoDf1
##
Breast_monoDf3 <- Breast_monoDf1
##
Breast_monoDf1 <- unique(ddply(Breast_monoDf1[,-c(3)], "VarRes", mutate, score = length(VarRes)))
Breast_monoDf1 <- Breast_monoDf1[order(Breast_monoDf1$coord, Breast_monoDf1$Response),]
#refer to line 497 and 499 respctively
Breast_monoDf1[5, 4] <- "L755_E757delinsS"
Breast_monoDf1[20, 4] <- "V777L and D769Y"
#instantiating the GRanges object with the coordinates and names of somatic variations
Breast_mono.gr <- GRanges("ERBB2", IRanges(unlist(sapply(1:dim(Breast_monoDf1)[1], function(i) rep(Breast_monoDf1$coord[i], Breast_monoDf1$score[i]))), width = 1, names = unlist(sapply(1:dim(Breast_monoDf1)[1], function(i) rep(Breast_monoDf1$SNV[i], Breast_monoDf1$score[i])))))
#adding the stack.factor attribute
Breast_mono.gr$stack.factor <- unlist(sapply(1:dim(Breast_monoDf1)[1], function(i) paste0(Breast_monoDf1$Response[i], "_", seq(1:Breast_monoDf1$score[i]))))
Breast_mono.gr$value1 <- 100
Breast_mono.gr$value2 <- 100 - Breast_mono.gr$value1
Breast_mono.gr$color <- response.color.set[gsub("_\\d*|\\w*-", "", Breast_mono.gr$stack.factor)]
legend <- list(labels = response, col = "gray80", fill = sapply(response.color.set, `[`, 1))
Breast_monoDf3 <- ddply(Breast_monoDf3, .(WTaaCoord), mutate, idx = seq_along(coord))
Breast_monoDf3 <- Breast_monoDf3[order(Breast_monoDf3$coord, Breast_monoDf3$Response),]
Breast_monoDf3$idx <- str_replace_all(str_c(Breast_monoDf3$idx), c("10" = "J", "1" = "A", "2" = "B", "3" = "C", "4" = "D", "5" = "E", "6" = "F", "7" = "G", "8" = "H", "9" = "I"))
Breast_mono.gr$stack.factor <- as.character(Breast_monoDf3$idx)
Breast_monoDf3$SNPsideID <- c(rep("top", 4), rep("bottom", 1), rep("top", 9), rep("bottom", 3), rep("top", 3), rep("bottom", 1), rep("top", 5), rep("bottom", 1), rep("top", 1), rep("bottom", 1), rep("top", 1), rep("bottom", 1), rep("top", 2))
Breast_mono.gr$SNPsideID <- Breast_monoDf3$SNPsideID
#plotting
lolliplot(Breast_mono.gr, Erbb2Features2, type = "pie.stack", legend = legend, dashline.col = "gray", cex = .7)
```
##Oncoplot: co-occurrence of somatic variations (both IMPACT and NON-IMPACT data sets)
```{r BreastMonoOncoplot, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.width = 7.2, fig.height = 10.3}
#load co-occurring somatic variants
breast_monoSomatics <- readWorksheet(SummitBioMarker0818, sheet = "Breast mono", endCol = 7, startRow = 38, endRow = 302)
#IMPACT data
breast_monoSomaticsImpact <- breast_monoSomatics[(breast_monoSomatics$Gene != "not done"),]
#subsetting
breast_monoSomaticsImpact <- breast_monoSomaticsImpact[,c(1,3,4)]
#filtering out duplication rows
breast_monoSomaticsImpact <- unique(breast_monoSomaticsImpact)
#set Subject.id to character
breast_monoSomaticsImpact$Subject.Identifier.for.the.Study <- as.character(breast_monoSomaticsImpact$Subject.Identifier.for.the.Study)
#filtering it out rows where missing values
breast_monoSomaticsImpact <- na.omit(breast_monoSomaticsImpact)
#concatenate variations' types whenever occuring in a given gene for a given subject
breast_monoSomaticsImpact <- breast_monoSomaticsImpact %>% group_by(Subject.Identifier.for.the.Study, Gene) %>% do(Alteration = paste(.$Alteration, collapse = '')) %>% ungroup() %>% mutate(Alteration = unlist(Alteration))
mat <- as.data.frame(spread(breast_monoSomaticsImpact, Subject.Identifier.for.the.Study, Alteration))
row.names(mat) <- mat$Gene
mat <- mat[,-1]
#Non IMPACT data
breast_monoSomaticsNonImpact <- breast_monoSomatics[(breast_monoSomatics$Gene == "not done"),]
breast_monoSomaticsNonImpact <- breast_monoSomaticsNonImpact[,c(1,6,7)]
breast_monoSomaticsNonImpact <- unique(breast_monoSomaticsNonImpact)
breast_monoSomaticsNonImpact$Subject.Identifier.for.the.Study <- as.character(paste(breast_monoSomaticsNonImpact$Subject.Identifier.for.the.Study, "*", sep = ""))
breast_monoSomaticsNonImpact <- na.omit(breast_monoSomaticsNonImpact)
breast_monoSomaticsNonImpact <- breast_monoSomaticsNonImpact %>% group_by(Subject.Identifier.for.the.Study, Gene.1) %>% do(Alteration.1 = paste(.$Alteration.1, collapse = '')) %>% ungroup() %>% mutate(Alteration.1 = unlist(Alteration.1))
#creating the mutations matrix for oncoprint
mat2 <- as.data.frame(spread(breast_monoSomaticsNonImpact, Subject.Identifier.for.the.Study, Alteration.1))
mat2 <- subset(mat2, select = colnames(mat2) != "NA*")
row.names(mat2) <- mat2$Gene.1
mat2 <- mat2[,-1]
mat <- merge(mat, mat2, by = "row.names", all = T)
row.names(mat) <- mat$Row.names
mat <- mat[,-1]
oncoPrint(mat, get_type = function(x) strsplit(x, ";")[[1]], alter_fun = alter_fun, col = col, row_names_gp = gpar(fontsize = 6), pct_gp = gpar(fontsize = 6), column_title = "Oncoplot for breast tumor (mono)", heatmap_legend_param = list(title = "Alterations", at = c("Missense", "AMP", "Nonsense", "Indel", "splice", "Gain", "Promoter", "DeepDel", "rearrangement", "MissenseAmp"), labels = c("Missense", "Amplification", "Nonsense", "Indel", "splice", "Gain", "Promoter", "Deep Del", "rearrangement","Missense & Amp")), show_column_names = TRUE)
```
##Waterfall and swim plots with oncoplot
```{r breast_monoWF, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.align ='left', fig.height = 4, fig.width= 5.2}
#Waterfalls
breast_monoWF <- Breast_mono[-27, c(1, 12, 13, 17)]
#breast_monoWF <- Breast_mono[, c(1, 12, 13, 17)]
#selecting records for which there are IMPACT and Non-IMPACT genotype data
breast_monoWF <- breast_monoWF[(breast_monoWF$Subject.Identifier.for.the.Study %in% intersect(breast_monoWF$Subject.Identifier.for.the.Study, gsub("\\*", "", colnames(mat)))),]
breast_monoWF1 <- breast_monoWF[complete.cases(breast_monoWF$Percentage.Change.of.Tumor.Measurement),]
#breast_monoWF <- breast_monoWF[order(-breast_monoWF$Percentage.Change.of.Tumor.Measurement),]
breast_monoWF1$Percentage.Change.of.Tumor.Measurement <- as.numeric(as.character(breast_monoWF1$Percentage.Change.of.Tumor.Measurement))
breast_monoWF1$Progression.Free.Survival.Time..Months. <- as.numeric(as.character(breast_monoWF1$Progression.Free.Survival.Time..Months.))
breast_monoWF1 <- breast_monoWF1[order(-breast_monoWF1$Percentage.Change.of.Tumor.Measurement),]
breast_monoWF1$Subject.Identifier.for.the.Study <- as.character(breast_monoWF1$Subject.Identifier.for.the.Study)
breast_monoWF2 <- rbind(breast_monoWF1, breast_monoWF[is.na(breast_monoWF$Percentage.Change.of.Tumor.Measurement),])
breast_monoWF2$Percentage.Change.of.Tumor.Measurement <- as.numeric(as.character(breast_monoWF2$Percentage.Change.of.Tumor.Measurement))
breast_monoWF2$Subject.Identifier.for.the.Study <- as.character(breast_monoWF2$Subject.Identifier.for.the.Study)
g1 <- ggplot(breast_monoWF2, aes(x = reorder(Subject.Identifier.for.the.Study, -Percentage.Change.of.Tumor.Measurement), y = Percentage.Change.of.Tumor.Measurement, fill = Best.Overall.Response)) +
geom_col() + ylab("Change from baseline (%)") + theme_bw() + ylim(-101, 101) + theme(axis.title.x = element_blank(), axis.text.x = element_blank(), axis.title.y = element_text(size = 9), axis.text.y = element_text(size = 6), legend.text = element_text(size = 6), legend.title = element_text(size = 8), plot.margin = unit(c(0,0,0,1.6), "cm"), legend.position = "top")
g2 <- ggplot(breast_monoWF2, aes(x = reorder(Subject.Identifier.for.the.Study, -Percentage.Change.of.Tumor.Measurement), y = Progression.Free.Survival.Time..Months., fill = Best.Overall.Response)) +
geom_col() + ylab("PFS (mo)") + theme_bw() + theme(axis.title.x = element_blank(), axis.text.x = element_text(size = 6, angle = 90, hjust = 1), axis.title.y = element_text(size = 9), axis.text.y = element_text(size = 6)) + guides(fill=FALSE)
plot_grid(g1, g2, nrow = 2, align = "v")
```
```{r breast_monoOncoplot2, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.width = 7, fig.height = 10.1, fig.align = "center"}
#given the occurrences of missing data for the %change of tumor measurement variable, subset out the mat matrix for the Subject.Identifier where such missing data.
breast_monoWF2 <- breast_monoWF2[order(-breast_monoWF2$Percentage.Change.of.Tumor.Measurement, breast_monoWF2$Subject.Identifier.for.the.Study),]
d1 <- setdiff(gsub("\\*", "", colnames(mat)), breast_monoWF2$Subject.Identifier.for.the.Study)
d1 <- sapply(d1, function(x) paste("^",x, "$", sep = ""))
mat2 <- mat[, !colnames(mat) %in% grep(paste(d1, collapse = "|"), colnames(mat), value = T)]
#creating a df for the purpose of reordering the mat object passed to the Oncoprint function
df1 <- breast_monoWF2[,c(1,4)]
df2<- as.data.frame(colnames(mat2))
colnames(df2) <- "subjid"
df2$Subject.Identifier.for.the.Study <- gsub("\\*", "", df2$subjid)
df3 <- merge(df1, df2, by = "Subject.Identifier.for.the.Study", sort = F)
mat2 <- mat2[,c(as.character(df3$subjid))]
mat2 <- mat2[rowSums(is.na(mat2)) < dim(mat2)[2],]
oncoPrint(mat2, get_type = function(x) strsplit(x, ";")[[1]], alter_fun = alter_fun, col = col, row_names_gp = gpar(fontsize = 6), pct_gp = gpar(fontsize = 6), heatmap_legend_param = list(title = "Alterations",at = c("Missense", "AMP", "Nonsense", "Indel", "splice", "Gain", "Promoter", "DeepDel", "rearrangement", "MissenseAmp"), labels = c("Missense", "Amplification", "Nonsense", "Indel", "splice", "Gain", "Promoter", "Deep Del", "rearrangement","Missense & Amp")), show_column_names = TRUE, column_order = NULL)
```
<hr/><hr />
#Breast combo
##Distributions of ErBb2 somatic variants w.r.t. clinical endpoints.
```{r Breast_combo, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE}
#reading biliary enrolment data set
Breast_combo <- readWorksheet(SummitBioMarker0818, sheet = "Breast combo", endCol = 18, endRow = 37)
Breast_combo[Breast_combo == ""] <- NA
Breast_combo[Breast_combo == "pending"] <- "NA"
#removing trailing white space
Breast_combo <- as.data.frame(apply(Breast_combo, 2, function(x) trimws(x)))
#tabular views
kable(t(as.matrix(table(Breast_combo$Mutation.code))), caption = "ErBb2 somatic variants occurences")
kable(table(Breast_combo$Mutation.code, Breast_combo$Primary.Cell.Type), caption = "ErbB2 mutation by Primary Cell Type")
#kable(table(Breast_combo$Mutation.code, Breast_combo$Objective.Response.at.Week.8), caption = "ErbB2 mutation by Objective Response at Week 8")
#~Best overall response
#keep first 2 characters for the Best.Overall.Response's attribute value
Breast_combo$Best.Overall.Response <- as.factor(str_sub(Breast_combo$Best.Overall.Response, 1, 2))
#setting values to the Best.Overall.Response attribute
Breast_combo$Best.Overall.Response <- factor(Breast_combo$Best.Overall.Response, levels = c("CR", "PR", "SD", "PD", "NA"), ordered = TRUE)
BestOverallResPerVar <- as.data.frame.matrix(table(Breast_combo$Mutation.code, Breast_combo$Best.Overall.Response))
#kable(table(Breast_combo$Mutation.code, Breast_combo$Best.Overall.Response), caption = "ErbB2 mutation by Best Overall Response")
datatable(BestOverallResPerVar, caption = "ErbB2 mutation by Best Overall Response")
ClinicalBenefitPerVar <- as.data.frame.matrix(table(Breast_combo$Mutation.code, Breast_combo$Clinical.Benefit))
datatable(ClinicalBenefitPerVar, caption = "ErbB2 mutations by Clinical Benefit Status")
#Breast_combo$Clinical.Benefit <- revalue(Breast_combo$Clinical.Benefit, c("pending" = "N/A", "n/a" = "N/A"))
#assessing the potential contribution of ErbB2 somatic variant on Best Overall Response
##chisq.test(table(na.omit(Breast_combo$Clinical.Benefit, Breast_combo$Mutation.code)))
```
<hr />
##Lolliplot: ErBb2 somatic variants annotated with Best Overall Response values
```{r Breast_comboSNV, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.height = 10, fig.width = 10}
#adding one variable, i.e. the somatic variant coordinate w.r.t. ErbB2 aa sequence
Breast_combo$ErBb2SomaticVarCoord <- as.numeric(str_extract(Breast_combo$Mutation.code, "\\d+"))
#adding two additional variables: the wild type aa and the substituted aa
Breast_combo$ErBb2WildTypeAA <- str_sub(Breast_combo$Mutation.code, 1, 1)
Breast_combo$ErBb2SubsAA <- gsub("^\\w{1}\\d+", "", Breast_combo$Mutation.code, perl = TRUE)
#reordering the dataframe according to the aa coordinate
Breast_combo <- Breast_combo[order(Breast_combo$ErBb2SomaticVarCoord),]
#to show the 2nd variant
Breast_combo[27,19] <- 779
#creating the data structure for the GRanges() GenomicRanges package function
Breast_comboDf1 <- data.frame(Breast_combo$ErBb2SomaticVarCoord, paste0(Breast_combo$ErBb2WildTypeAA, Breast_combo$ErBb2SomaticVarCoord), Breast_combo$ErBb2SubsAA, Breast_combo$Best.Overall.Response)
#the Breast_comboDf1 df Erbb2Features 4 columns, the coordinates where the variations map, the WT aa with the coordinate, the variant AA and the clinical response.
colnames(Breast_comboDf1) <- c("coord", "WTaaCoord", "Var", "Response")
#creating a 2nd df in order to concatenate the substituions mapping to the same coordinate
Breast_comboDf2 <- as.data.frame(unique(Breast_comboDf1[,-4]) %>% group_by(WTaaCoord, coord) %>% do(Varalleles = paste(.$Var, collapse = "/")) %>% ungroup() %>% mutate(Varalleles = unlist(Varalleles)))
#adding one variable referred to as SNV which reads the WT aa, the coordinate and the substituted aa
Breast_comboDf2$SNV <- paste0(Breast_comboDf2$WTaaCoord, Breast_comboDf2$Varalleles)
#adding it fo the initial Breast_comboDf1 df
Breast_comboDf1 <- merge(Breast_comboDf1, Breast_comboDf2[,-c(2,3)], by = "WTaaCoord")
#adding one attribute: concatenation of the clinical response and the variant
Breast_comboDf1$VarRes <- paste0(Breast_comboDf1$Response, Breast_comboDf1$SNV)
# replacing NA with the 'ND' string value for Not Determined
Breast_comboDf1$Response <- gsub("NA", "ND",Breast_comboDf1$Response)
#set order for clinical response value to bottom CR, PR, SD, PD, ND
Breast_comboDf1$Response <- str_replace_all(str_c(Breast_comboDf1$Response), c("CR" = "E-CR", "PR" = "D-PR", "SD" = "C-SD", "PD" = "B-PD", "ND" = "A-ND"))
#order the df
Breast_comboDf1 <- Breast_comboDf1[order(Breast_comboDf1$coord, Breast_comboDf1$Response),]
#duplicated the Breast_comboDf1 Df object for later plotting on the kinase domain
Breast_comboDf1_2 <- Breast_comboDf1
##
Breast_comboDf3 <- Breast_comboDf1
##
Breast_comboDf1 <- unique(ddply(Breast_comboDf1[,-c(3)], "VarRes", mutate, score = length(VarRes)))
Breast_comboDf1 <- Breast_comboDf1[order(Breast_comboDf1$coord, Breast_comboDf1$Response),]
Breast_comboDf1[25,4] <- "V777L and L755S"
#instantiating the GRanges object with the coordinates and names of somatic variations
Breast_combo.gr <- GRanges("ERBB2", IRanges(unlist(sapply(1:dim(Breast_comboDf1)[1], function(i) rep(Breast_comboDf1$coord[i], Breast_comboDf1$score[i]))), width = 1, names = unlist(sapply(1:dim(Breast_comboDf1)[1], function(i) rep(Breast_comboDf1$SNV[i], Breast_comboDf1$score[i])))))
#adding the stack.factor attribute
Breast_combo.gr$stack.factor <- unlist(sapply(1:dim(Breast_comboDf1)[1], function(i) paste0(Breast_comboDf1$Response[i], "_", seq(1:Breast_comboDf1$score[i]))))
Breast_combo.gr$value1 <- 100
Breast_combo.gr$value2 <- 100 - Breast_combo.gr$value1
Breast_combo.gr$color <- response.color.set[gsub("_\\d*|\\w*-", "", Breast_combo.gr$stack.factor)]
legend <- list(labels = response, col = "gray80", fill = sapply(response.color.set, `[`, 1))
Breast_comboDf3 <- ddply(Breast_comboDf3, .(WTaaCoord), mutate, idx = seq_along(coord))
Breast_comboDf3 <- Breast_comboDf3[order(Breast_comboDf3$coord, Breast_comboDf3$Response),]
Breast_combo.gr$stack.factor <- as.character(Breast_comboDf3$idx)
Breast_comboDf3$SNPsideID <- c(rep("top", 9), rep("bottom", 1), rep("top", 9), rep("bottom", 1), rep("top", 2), rep("bottom", 1), rep("top", 1), rep("bottom", 5), rep("top", 4), rep("bottom", 1), rep("top", 1), rep("bottom", 1))
Breast_combo.gr$SNPsideID <- Breast_comboDf3$SNPsideID
#plotting
lolliplot(Breast_combo.gr, Erbb2Features2, type = "pie.stack", legend = legend, dashline.col = "gray", cex = .7, jitter = "label")
```
##Oncoplot: co-occurrence of somatic variations (both IMPACT and NON-IMPACT data sets)
```{r chunkBreastComboOncoplot, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.width = 7.3, fig.height = 10}
#load co-occurring somatic variants
breast_comboSomatics <- readWorksheet(SummitBioMarker0818, sheet = "Breast combo", endCol = 7, startRow = 42, endRow = 320)
#IMPACT data
breast_comboSomaticsImpact <- breast_comboSomatics[(breast_comboSomatics$Gene != "not done"),]
#subsetting
breast_comboSomaticsImpact <- breast_comboSomaticsImpact[,c(1,3,4)]
#filtering out duplication rows
breast_comboSomaticsImpact <- unique(breast_comboSomaticsImpact)
#set Subject.id to character
breast_comboSomaticsImpact$Subject.Identifier.for.the.Study <- as.character(breast_comboSomaticsImpact$Subject.Identifier.for.the.Study)
#filtering it out rows where missing values
breast_comboSomaticsImpact <- na.omit(breast_comboSomaticsImpact)
#concatenate variations' types whenever occuring in a given gene for a given subject
breast_comboSomaticsImpact <- breast_comboSomaticsImpact %>% group_by(Subject.Identifier.for.the.Study, Gene) %>% do(Alteration = paste(.$Alteration, collapse = '')) %>% ungroup() %>% mutate(Alteration = unlist(Alteration))
mat <- as.data.frame(spread(breast_comboSomaticsImpact, Subject.Identifier.for.the.Study, Alteration))
row.names(mat) <- mat$Gene
mat <- mat[,-1]
#Non IMPACT data
breast_comboSomaticsNonImpact <- breast_comboSomatics[(breast_comboSomatics$Gene == "not done"),]
breast_comboSomaticsNonImpact <- breast_comboSomaticsNonImpact[,c(1,6,7)]
breast_comboSomaticsNonImpact <- unique(breast_comboSomaticsNonImpact)
breast_comboSomaticsNonImpact$Subject.Identifier.for.the.Study <- as.character(paste(breast_comboSomaticsNonImpact$Subject.Identifier.for.the.Study, "*", sep = ""))
breast_comboSomaticsNonImpact <- na.omit(breast_comboSomaticsNonImpact)
breast_comboSomaticsNonImpact <- breast_comboSomaticsNonImpact %>% group_by(Subject.Identifier.for.the.Study, Gene.1) %>% do(Alteration.1 = paste(.$Alteration.1, collapse = '')) %>% ungroup() %>% mutate(Alteration.1 = unlist(Alteration.1))
#creating the mutations matrix for oncoprint
mat2 <- as.data.frame(spread(breast_comboSomaticsNonImpact, Subject.Identifier.for.the.Study, Alteration.1))
mat2 <- subset(mat2, select = colnames(mat2) != "NA*")
row.names(mat2) <- mat2$Gene.1
mat2 <- mat2[,-1]
mat <- merge(mat, mat2, by = "row.names", all = T)
row.names(mat) <- mat$Row.names
mat <- mat[,-1]
oncoPrint(mat, get_type = function(x) strsplit(x, ";")[[1]], alter_fun = alter_fun, col = col, row_names_gp = gpar(fontsize = 6), pct_gp = gpar(fontsize = 6), column_title = "Oncoplot for breast tumor (combo)", heatmap_legend_param = list(title = "Alterations", at = c("Missense", "AMP", "Nonsense", "Indel", "splice", "Gain", "Promoter", "DeepDel", "rearrangement", "MissenseAmp"), labels = c("Missense", "Amplification", "Nonsense", "Indel", "splice", "Gain", "Promoter", "Deep Del", "rearrangement","Missense & Amp")), show_column_names = TRUE)
```
##Waterfall and swim plots with oncoplot
```{r breast_comboWF, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.align ='left', fig.height = 4, fig.width = 5.15}
#Waterfalls
breast_comboWF <- Breast_combo[-27,c(1, 12, 13, 17)]
#selecting records for which there are IMPACT and Non-IMPACT genotype data
breast_comboWF <- breast_comboWF[(breast_comboWF$Subject.Identifier.for.the.Study %in% intersect(breast_comboWF$Subject.Identifier.for.the.Study, gsub("\\*", "", colnames(mat)))),]
breast_comboWF1 <- breast_comboWF[complete.cases(breast_comboWF$Percentage.Change.of.Tumor.Measurement),]
breast_comboWF1$Percentage.Change.of.Tumor.Measurement <- as.numeric(as.character(breast_comboWF1$Percentage.Change.of.Tumor.Measurement))
breast_comboWF1$Progression.Free.Survival.Time..Months. <- as.numeric(as.character(breast_comboWF1$Progression.Free.Survival.Time..Months.))
breast_comboWF1 <- breast_comboWF1[order(-breast_comboWF1$Percentage.Change.of.Tumor.Measurement),]
breast_comboWF1$Subject.Identifier.for.the.Study <- as.character(breast_comboWF1$Subject.Identifier.for.the.Study)
breast_comboWF2 <- rbind(breast_comboWF1, breast_comboWF[is.na(breast_comboWF$Percentage.Change.of.Tumor.Measurement),])
breast_comboWF2$Percentage.Change.of.Tumor.Measurement <- as.numeric(as.character(breast_comboWF2$Percentage.Change.of.Tumor.Measurement))
breast_comboWF2$Progression.Free.Survival.Time..Months. <- as.numeric(breast_comboWF2$Progression.Free.Survival.Time..Months.)
g1 <- ggplot(breast_comboWF2, aes(x = reorder(Subject.Identifier.for.the.Study, -Percentage.Change.of.Tumor.Measurement), y = Percentage.Change.of.Tumor.Measurement, fill = Best.Overall.Response)) +
geom_col() + ylab("Change from baseline (%)") + theme_bw() + ylim(-100, 100) + theme(axis.title.x = element_blank(), axis.text.x = element_blank(), axis.title.y = element_text(size = 9), axis.text.y = element_text(size = 6), legend.text = element_text(size = 6), legend.title = element_text(size = 8), plot.margin = unit(c(0,0,0,1.6), "cm"), legend.position = "top")
g2 <- ggplot(breast_comboWF2, aes(x = reorder(Subject.Identifier.for.the.Study, -Percentage.Change.of.Tumor.Measurement), y = Progression.Free.Survival.Time..Months., fill = Best.Overall.Response)) + geom_col() + ylab("PFS (mo)") + theme_bw() + theme(axis.title.x = element_blank(), axis.text.x = element_text(size = 6, angle = 90, hjust = 1), axis.title.y = element_text(size = 9), axis.text.y = element_text(size = 6)) + guides(fill=FALSE)
plot_grid(g1, g2, nrow = 2, align = "v")
```
```{r breast_comboOncoplot2, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.width = 6.9, fig.height = 10.1, fig.align = "center"}
#given the occurrences of missing data for the %change of tumor measurement variable, subset out the mat matrix for the Subject.Identifier where such missing data.
breast_comboWF2 <- breast_comboWF2[order(-breast_comboWF2$Percentage.Change.of.Tumor.Measurement, breast_comboWF2$Subject.Identifier.for.the.Study),]
d1 <- setdiff(gsub("\\*", "", colnames(mat)), breast_comboWF2$Subject.Identifier.for.the.Study)
d1 <- sapply(d1, function(x) paste("^",x, "$", sep = ""))
mat2 <- mat[, !colnames(mat) %in% grep(paste(d1, collapse = "|"), colnames(mat), value = T)]
#creating a df for the purpose of reordering the mat object passed to the Oncoprint function
df1 <- breast_comboWF2[,c(1,4)]
df2<- as.data.frame(colnames(mat2))
colnames(df2) <- "subjid"
df2$Subject.Identifier.for.the.Study <- gsub("\\*", "", df2$subjid)
df3 <- merge(df1, df2, by = "Subject.Identifier.for.the.Study", sort = F)
mat2 <- mat2[c(as.character(df3$subjid))]
mat2 <- mat2[rowSums(is.na(mat2)) < dim(mat2)[2],]
oncoPrint(mat2, get_type = function(x) strsplit(x, ";")[[1]], alter_fun = alter_fun, col = col, row_names_gp = gpar(fontsize = 6), pct_gp = gpar(fontsize = 6), heatmap_legend_param = list(title = "Alterations", at = c("Missense", "AMP", "Nonsense", "Indel", "splice", "Gain", "Promoter", "DeepDel", "rearrangement", "MissenseAmp"), labels = c("Missense", "Amplification", "Nonsense", "Indel", "splice", "Gain", "Promoter", "Deep Del", "rearrangement","Missense & Amp")), show_column_names = TRUE, column_order = NULL)
```
<hr /><hr />
#Cervical
##Distributions of ErBb2 somatic variants w.r.t. clinical endpoints.
```{r chunk9, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE}
#reading cervical enrolment data set
cervical <- readWorksheet(SummitBioMarker0818, sheet = "cervical", endCol = 17, endRow = 7)
cervical[cervical == ""] <- NA
cervical[cervical == "pending"] <- "NA"
#tabular views
kable(t(as.matrix(table(cervical$Mutation.code))), caption = "ErBb2 somatic variants occurences")
kable(table(cervical$Mutation.code, cervical$Primary.Cell.Type
), caption = "ErbB2 mutation by Primary Cell Type")
kable(table(cervical$Mutation.code, cervical$Objective.Response.at.Week.8), caption = "ErbB2 mutation by Objective Response at Week 8")
#~Best overall response
#keep first 2 characters for the Best.Overall.Response's attribute value
cervical$Best.Overall.Response <- as.factor(str_sub(cervical$Best.Overall.Response, 1, 2))
#setting values to the Best.Overall.Response attribute
cervical$Best.Overall.Response <- factor(cervical$Best.Overall.Response, levels = c("CR", "PR", "SD", "PD", "NA"), ordered = TRUE)
BestOverallResPerVar <- as.data.frame.matrix(table(cervical$Mutation.code, cervical$Best.Overall.Response))
datatable(BestOverallResPerVar, caption = "ErbB2 mutation by Best Overall Response")
#plotting
ClinicalBenefitPerVar <- as.data.frame.matrix(table(cervical$Mutation.code, cervical$Clinical.Benefit))
datatable(ClinicalBenefitPerVar, caption = "ErbB2 mutations by Clinical Benefit Status")
#cervical$Clinical.Benefit <- revalue(cervical$Clinical.Benefit, c("pending"="N/A"))
```
<hr />
##Lolliplot: Annotated ErBb2 somatic variants
```{r chunk10, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE}
# adding one variable, i.e. the somatic variant coordinate w.r.t. ErbB2 aa
# sequence
cervical$ErBb2SomaticVarCoord <- as.numeric(str_extract(cervical$Mutation.code, "\\d+"))
# adding two additional variables: the wild type aa and the substituted aa
cervical$ErBb2WildTypeAA <- str_sub(cervical$Mutation.code, 1, 1)
cervical$ErBb2SubsAA <- gsub("^\\w{1}\\d+", "", cervical$Mutation.code,
perl = T)
# reordering the dataframe according to the aa coordinate
cervical <- cervical[order(cervical$ErBb2SomaticVarCoord), ]
#creating the data structure for the GRanges() GenomicRanges package function
cervicalDf1 <- data.frame(cervical$ErBb2SomaticVarCoord, paste0(cervical$ErBb2WildTypeAA, cervical$ErBb2SomaticVarCoord), cervical$ErBb2SubsAA, cervical$Best.Overall.Response)
#the cervicalDf1 df Erbb2Features 4 columns, the coordinates where the variations map, the WT aa with the coordinate, the variant AA and the clinical response.
colnames(cervicalDf1) <- c("coord", "WTaaCoord", "Var", "Response")
#creating a 2nd df in order to concatenate the substituions mapping to the same coordinate
cervicalDf2 <- as.data.frame(unique(cervicalDf1[,-4]) %>% group_by(WTaaCoord, coord) %>% do(Varalleles = paste(.$Var, collapse = "/")) %>% ungroup() %>% mutate(Varalleles = unlist(Varalleles)))
#adding one variable referred to as SNV which reads the WT aa, the coordinate and the substituted aa
cervicalDf2$SNV <- paste0(cervicalDf2$WTaaCoord, cervicalDf2$Varalleles)
#adding it fo the initial cervicalDf1 df
cervicalDf1 <- merge(cervicalDf1, cervicalDf2[,-c(2,3)], by = "WTaaCoord")
#adding one attribute: concatenation of the clinical response and the variant
cervicalDf1$VarRes <- paste0(cervicalDf1$Response, cervicalDf1$SNV)
# replacing NA with the 'ND' string value for Not Determined
cervicalDf1$Response <- gsub("NA", "ND",cervicalDf1$Response)
#set order for clinical response value to bottom CR, PR, SD, PD, ND
cervicalDf1$Response <- str_replace_all(str_c(cervicalDf1$Response), c("CR" = "E-CR", "PR" = "D-PR", "SD" = "C-SD", "PD" = "B-PD", "ND" = "A-ND"))
#order the df
cervicalDf1 <- cervicalDf1[order(cervicalDf1$coord, cervicalDf1$Response),]
##
cervicalDf3 <- cervicalDf1
##
cervicalDf1 <- unique(ddply(cervicalDf1[,-c(3)], "VarRes", mutate, score = length(VarRes)))
cervicalDf1 <- cervicalDf1[order(cervicalDf1$coord, cervicalDf1$Response),]
#instantiating the GRanges object with the coordinates and names of somatic variations
cervical.gr <- GRanges("ERBB2", IRanges(unlist(sapply(1:dim(cervicalDf1)[1], function(i) rep(cervicalDf1$coord[i], cervicalDf1$score[i]))), width = 1, names = unlist(sapply(1:dim(cervicalDf1)[1], function(i) rep(cervicalDf1$SNV[i], cervicalDf1$score[i])))))
#adding the stack.factor attribute
cervical.gr$stack.factor <- unlist(sapply(1:dim(cervicalDf1)[1], function(i) paste0(cervicalDf1$Response[i], "_", seq(1:cervicalDf1$score[i]))))
cervical.gr$value1 <- 100
cervical.gr$value2 <- 100 - cervical.gr$value1
cervical.gr$color <- response.color.set[gsub("_\\d*|\\w*-", "", cervical.gr$stack.factor)]
legend <- list(labels = response, col = "gray80", fill = sapply(response.color.set, `[`, 1))
cervicalDf3 <- ddply(cervicalDf3, .(WTaaCoord), mutate, idx = seq_along(coord))
cervicalDf3 <- cervicalDf3[order(cervicalDf3$coord, cervicalDf3$Response),]
cervical.gr$stack.factor <- as.character(cervicalDf3$idx)
#plotting
lolliplot(cervical.gr, Erbb2Features, type = "pie.stack", legend = legend, dashline.col = "gray", cex = .7, jitter = "label")
```
##Oncoplot: co-occurrence of somatic variations (both IMPACT and NON-IMPACT data sets)
```{r CervicalOncoplot, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE}
#load co-occurring somatic variants
CervicalSomatics <- readWorksheet(SummitBioMarker0818, sheet = "Cervical", endCol = 7, startRow = 10, endRow = 52)
#IMPACT data
CervicalSomaticsImpact <- CervicalSomatics[(CervicalSomatics$Gene != "not done"),]
#subsetting
CervicalSomaticsImpact <- CervicalSomaticsImpact[,c(1,3,4)]
#filtering out duplication rows
CervicalSomaticsImpact <- unique(CervicalSomaticsImpact)
#set Subject.id to character
CervicalSomaticsImpact$Subject.Identifier.for.the.Study <- as.character(CervicalSomaticsImpact$Subject.Identifier.for.the.Study)
#filtering it out rows where missing values
CervicalSomaticsImpact <- na.omit(CervicalSomaticsImpact)
#concatenate variations' types whenever occuring in a given gene for a given subject
CervicalSomaticsImpact <- CervicalSomaticsImpact %>% group_by(Subject.Identifier.for.the.Study, Gene) %>% do(Alteration = paste(.$Alteration, collapse = '')) %>% ungroup() %>% mutate(Alteration = unlist(Alteration))
mat <- as.data.frame(spread(CervicalSomaticsImpact, Subject.Identifier.for.the.Study, Alteration))
row.names(mat) <- mat$Gene
mat <- mat[,-1]
#Non IMPACT data
CervicalSomaticsNonImpact <- CervicalSomatics[(CervicalSomatics$Gene == "not done"),]
CervicalSomaticsNonImpact <- CervicalSomaticsNonImpact[,c(1,6,7)]
CervicalSomaticsNonImpact <- unique(CervicalSomaticsNonImpact)
CervicalSomaticsNonImpact$Subject.Identifier.for.the.Study <- as.character(paste(CervicalSomaticsNonImpact$Subject.Identifier.for.the.Study, "*", sep = ""))
CervicalSomaticsNonImpact <- na.omit(CervicalSomaticsNonImpact)
CervicalSomaticsNonImpact <- CervicalSomaticsNonImpact %>% group_by(Subject.Identifier.for.the.Study, Gene.1) %>% do(Alteration.1 = paste(.$Alteration.1, collapse = '')) %>% ungroup() %>% mutate(Alteration.1 = unlist(Alteration.1))
#creating the mutations matrix for oncoprint
mat2 <- as.data.frame(spread(CervicalSomaticsNonImpact, Subject.Identifier.for.the.Study, Alteration.1))
mat2 <- subset(mat2, select = colnames(mat2) != "NA*")
row.names(mat2) <- mat2$Gene.1
mat2 <- mat2[,-1]
mat <- merge(mat, mat2, by = "row.names", all = T)
row.names(mat) <- mat$Row.names
mat <- mat[,-1]
oncoPrint(mat, get_type = function(x) strsplit(x, ";")[[1]], alter_fun = alter_fun, col = col, row_names_gp = gpar(fontsize = 6), pct_gp = gpar(fontsize = 6), column_title = "Oncoplot for cervical tumor", heatmap_legend_param = list(title = "Alterations", at = c("Missense", "AMP", "Nonsense", "Indel", "splice", "Gain", "Promoter", "DeepDel", "rearrangement", "MissenseAmp"), labels = c("Missense", "Amplification", "Nonsense", "Indel", "splice", "Gain", "Promoter", "Deep Del", "rearrangement","Missense & Amp")), show_column_names = TRUE)
```
##Waterfall and swim plots with oncoplot
```{r cervicalWF, echo = TRUE, message = FALSE, warning = FALSE, tidy = TRUE, fig.align = 'left', fig.height= 4, fig.width= 5.1}
#Waterfalls
cervicalWF <- cervical[,c(1, 12, 13, 17)]