forked from CCBR/CPTR_5_DSP_Krug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CPTR5_analysis_markdown.Rmd
4967 lines (3594 loc) · 154 KB
/
CPTR5_analysis_markdown.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: "CPTR-5 Krug: DSP Analysis of KS samples"
output: html_document
date: "2024-July-31"
---
# Setup
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,
warning = FALSE,
message = FALSE)
# Increase the time out for downloading the DSP package
options(timeout = max(300, getOption("timeout")))
# Global parameters
# Knitting Markdown
include.qc <- FALSE
include.DE <- FALSE
include.PCA <- FALSE
# Exporting Results
global.export.deg <- FALSE
global.export.volcano <- FALSE
global.export.heatmap <- FALSE
export.gsea.input <- FALSE
```
```{r, include=FALSE}
library(GeomxTools)
library(dplyr)
library(limma)
library(edgeR)
library(ggplot2)
library(ggrepel)
library(stringr)
library(PCAtools)
library(readxl)
library(gridExtra)
library(grid)
# Source the helper functions
source("DSP_functions.R")
# Results folder where the results should be exported
results.folder <- "results/"
run.folder <- "7_31_2024/"
```
```{r Load DSPWorkflow, include=include.qc}
# Install DSPWorkflow package
install.DSP <- FALSE
if(install.DSP == TRUE){
library(devtools)
install_github("NIDAP-Community/DSPWorkflow", ref = "dev")
}
library(DSPWorkflow)
```
``` {r Load Data, include=include.qc}
# Load all inputs
dcc.files <- list.files(file.path("dcc"),
pattern = ".dcc$",
full.names = TRUE,
recursive = TRUE
)
pkc.files <- "Hs_R_NGS_WTA_v1.0.pkc"
pheno.data.file <- "annotation_Krug_CPTR_5_July2024_NC_edit.xlsx"
```
# Study Design
```{r Study Design, include=include.qc}
# Save the output from the study design function into a list
sdesign.list <- studyDesign(dcc.files = dcc.files,
pkc.files = pkc.files,
pheno.data.file = pheno.data.file,
pheno.data.sheet = "annotation",
pheno.data.dcc.col.name = "Sample_ID",
protocol.data.col.names = c("ROI"),
experiment.data.col.names = c("panel"),
slide.name.col = "slide name",
class.col = "class",
region.col = "Region",
segment.col = "segment",
area.col = "area",
nuclei.col = "nuclei",
sankey.exclude.slide = FALSE,
segment.id.length = 10)
# The output of the study design function is a Geomxset Object and a Plot
# Print out a summary of the object
print(sdesign.list$object)
# Print out the Sankey Plot
print(sdesign.list$sankey.plot)
```
# QC
```{r QC Preprocessing, include=include.qc}
qc.output <- qcProc(object = sdesign.list$object,
min.segment.reads = 1000,
percent.trimmed = 80,
percent.stitched = 80,
percent.aligned = 80,
percent.saturation = 50,
min.negative.count = 3,
max.ntc.count = 1000,
min.nuclei = 200,
min.area = 1000,
print.plots = TRUE)
print(qc.output$segments.qc)
print(qc.output$segment.flags)
print(qc.output$probe.flags)
# Export the flags table
export.flags <- FALSE
if(export.flags == TRUE){
write.csv(qc.output$segment.flags, file = "qc/segment_qc_flags.csv")
write.csv(qc.output$probe.flags, file = "qc/probe_qc_flags.csv")
}
```
# Filtering
```{r Segment Filtering by Gene Detection, include=include.qc}
library(GeomxTools)
library(dplyr)
library(knitr)
object <- qc.output$object
# Set up lists of segment IDs
segment.list.total <- pData(object)$segmentID
# Define Modules
modules <- gsub(".pkc", "", pkc.files)
# Calculate limit of quantification (LOQ) in each segment
# LOQ = geomean(NegProbes) * geoSD(NegProbes)^(LOQ cutoff)
# LOQ is calculated for each module (pkc file)
loq <- data.frame(row.names = colnames(object))
loq.min <- 2
loq.cutoff <- 2
for(module in modules) {
vars <- paste0(c("NegGeoMean_", "NegGeoSD_"),
module)
if(all(vars[1:2] %in% colnames(pData(object)))) {
neg.geo.mean <- vars[1]
neg.geo.sd <- vars[2]
loq[, module] <-
pmax(loq.min,
pData(object)[, neg.geo.mean] *
pData(object)[, neg.geo.sd] ^ loq.cutoff)
}
}
# Store the loq df in the annotation df
pData(object)$loq <- loq
# Setup a master loq matrix
loq.mat <- c()
for(module in modules) {
# Gather rows with the given module
ind <- fData(object)$Module == module
# Check if each feature has counts above the LOQ
mat.i <- t(esApply(object[ind, ], MARGIN = 1,
FUN = function(x) {
x > loq[, module]
}))
# Store results in the master loq matrix
loq.mat <- rbind(loq.mat, mat.i)
}
# ensure ordering since this is stored outside of the geomxSet
loq.mat <- loq.mat[fData(object)$TargetName, ]
# Evaluate and Filter Segment Gene Detection Rate
# Save detection rate information to pheno data
pData(object)$GenesDetected <- colSums(loq.mat, na.rm = TRUE)
pData(object)$GeneDetectionRate <- 100*(pData(object)$GenesDetected / nrow(object))
# Establish detection bins
detection.bins <- c("less_than_1", "1_5", "5_10", "10_15", "greater_than_15")
# Determine detection thresholds: 1%, 5%, 10%, 15%, >15%
pData(object)$DetectionThreshold <-
cut(pData(object)$GeneDetectionRate,
breaks = c(0, 1, 5, 10, 15, 100),
labels = detection.bins)
# stacked bar plot of different cut points (1%, 5%, 10%, 15%)
segment.stacked.bar.plot<- ggplot(pData(object),
aes(x = DetectionThreshold)) +
geom_bar(aes(fill = region)) +
geom_text(stat = "count", aes(label = ..count..), vjust = -0.5) +
theme_bw() +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
labs(x = "Gene Detection Rate",
y = "Segments, #",
fill = "Segment Type")
# cut percent genes detected at 1, 5, 10, 15
segment.table <- kable(table(pData(object)$DetectionThreshold,
pData(object)$class))
# Make a list of segments with low detection
low.detection.segments <- pData(object) %>%
filter(GeneDetectionRate < 5) %>%
select(any_of(c("segmentID", "GeneDetectionRate")))
print(low.detection.segments)
# Export a summary of the segment gene detection
segment.detection.summary <- pData(object) %>%
select(any_of(c("segmentID", "GeneDetectionRate", "DetectionThreshold")))
export.segment.detection.summary <- FALSE
if(export.segment.detection.summary == TRUE){
write.csv(segment.detection.summary, "qc/segment_detection_summary.csv")
}
```
```{r Remove Segments, include=include.qc}
# Filter the data using the cutoff for gene detection rate
segment.gene.rate.cutoff <- 1
object.segment.filtered <-
object[, pData(object)$GeneDetectionRate >= segment.gene.rate.cutoff]
```
```{r Gene Filtering by Detection per Segment, include=include.qc}
library(scales)
# Evaluate and Filter Study-wide Gene Detection Rate
# Calculate detection rate:
loq.mat <- loq.mat[, colnames(object.segment.filtered)]
fData(object.segment.filtered)$DetectedSegments <- rowSums(loq.mat, na.rm = TRUE)
fData(object.segment.filtered)$DetectionRate <-
100*(fData(object.segment.filtered)$DetectedSegments / nrow(pData(object)))
# Establish detection bins
detection.bins <- c("0", "less_than_1", "1_5", "5_10", "10_20", "20_30", "30_40", "40_50", "greater_than_50")
# Determine detection thresholds: 1%, 5%, 10%, 15%, >15%
fData(object.segment.filtered)$DetectionThreshold <-
cut(fData(object.segment.filtered)$DetectionRate,
breaks = c(-1, 0, 1, 5, 10, 20, 30, 40, 50, 100),
labels = detection.bins)
gene.stacked.bar.plot <- ggplot(fData(object.segment.filtered),
aes(x = DetectionThreshold)) +
geom_bar(aes(fill = Module)) +
geom_text(stat = "count", aes(label = ..count..), vjust = -0.5) +
theme_bw() +
scale_y_continuous(expand = expansion(mult = c(0, 0.1))) +
labs(x = "Gene Detection Rate",
y = "Genes, #",
fill = "Probe Set")
# Gene of interest detection table
goi <- c("A2M", "CD44")
goi.table <- data.frame(Gene = goi,
Number = fData(object.segment.filtered)[goi, "DetectedSegments"],
DetectionRate = percent(fData(object.segment.filtered)[goi, "DetectionRate"]))
print(goi.table)
# Plot detection rate:
plot.detect <- data.frame(Freq = c(1, 5, 10, 20, 30, 50))
plot.detect$Number <-
unlist(lapply(c(1, 5, 10, 20, 30, 50),
function(x) {sum(fData(object.segment.filtered)$DetectionRate >= x)}))
plot.detect$Rate <- plot.detect$Number / nrow(fData(object.segment.filtered))
rownames(plot.detect) <- plot.detect$Freq
genes.detected.plot <- ggplot(plot.detect, aes(x = as.factor(Freq), y = Rate, fill = Rate)) +
geom_bar(stat = "identity") +
geom_text(aes(label = formatC(Number, format = "d", big.mark = ",")),
vjust = 1.6, color = "black", size = 4) +
scale_fill_gradient2(low = "orange2", mid = "lightblue",
high = "dodgerblue3", midpoint = 0.65,
limits = c(0,1),
labels = scales::percent) +
theme_bw() +
scale_y_continuous(labels = scales::percent, limits = c(0,1),
expand = expansion(mult = c(0, 0))) +
labs(x = "% of Segments",
y = "Genes Detected, % of Panel > loq")
# Export a summary of the gene detection
gene.detection.summary <- fData(object.segment.filtered) %>%
select(any_of(c("segmentID", "DetectionRate", "DetectionThreshold")))
export.gene.detection.summary <- FALSE
if(export.gene.detection.summary == TRUE){
write.csv(gene.detection.summary, "qc/gene_detection_summary.csv")
}
```
```{r Remove Genes, include=include.qc}
# Set the cutoff for gene detection
study.gene.rate.cutoff <- 0.01
# Subset for genes above the study gene detection rate cutoff
# Manually include the negative control probe, for downstream use
negative.probe.fData <- subset(fData(object.segment.filtered), CodeClass == "Negative")
neg.probes <- unique(negative.probe.fData$TargetName)
object.gene.filtered <- object.segment.filtered[fData(object.segment.filtered)$DetectionRate >= study.gene.rate.cutoff |
fData(object.segment.filtered)$TargetName %in% neg.probes, ]
```
# Normalization
```{r Normalization, include=include.qc}
q3.normalization.output <- geomxNorm(
object = object.gene.filtered,
norm = "q3")
print(q3.normalization.output$multi.plot)
print(q3.normalization.output$boxplot.raw)
print(q3.normalization.output$boxplot.norm)
neg.normalization.output <- geomxNorm(
object = object.gene.filtered,
norm = "neg")
print(neg.normalization.output$multi.plot)
print(neg.normalization.output$boxplot.raw)
print(neg.normalization.output$boxplot.norm)
# Export normalized counts
export.counts <- TRUE
if(export.counts == TRUE){
# Gather the counts
norm.counts <- q3.normalization.output$object@assayData$q_norm
# Write to csv
write.csv(norm.counts,
file = paste0(results.folder, "q3_norm_counts.csv"))
}
```
```{r Heatmap of Variable Genes, include=include.PCA}
# Create a slot for log2 counts
assayDataElement(object = q3.normalization.output$object, elt = "log_q") <-
assayDataApply(q3.normalization.output$object, 2, FUN = log, base = 2, elt = "q_norm")
# Make a df from the log counts
q3.norm.log.counts <- q3.normalization.output$object@assayData$log_q
# create Coefficient of Variation (CV) function and apply to the log counts
calc_CV <- function(x) {sd(x) / mean(x)}
cv.df <- data.frame(CV = apply(q3.norm.log.counts, 1, calc_CV))
# Take the top 500 most variable genes by CV score
cv.df.top <- cv.df %>% arrange(desc(CV)) %>% slice(1:500)
# Get the list of top CV genes
top.cv.gene.list <- rownames(cv.df.top)
# Subset the counts for the top CV genes
top.cv.heatmap.counts <- q3.norm.log.counts[rownames(q3.norm.log.counts) %in% top.cv.gene.list, ]
# Order the counts by top CV
top.cv.heatmap.counts <- top.cv.heatmap.counts[match(top.cv.gene.list, rownames(top.cv.heatmap.counts)), ]
anno.colors = list(
Tumor = c(Biopsy = "slateblue",
PDX = "coral"),
region = c(tumor = "pink",
vessel = "seagreen1",
immune = "lightblue",
'immune/stroma' = "maroon"),
segment = c(full_ROI = "lightsalmon",
LANA_pos = "aquamarine"),
Source = c(Skin = "steelblue1",
GI = "indianred")
)
# Set up the annotation
annotation <- pData(q3.normalization.output$object)
annotation.heatmap <- annotation %>%
select(names(anno.colors))
cv.heatmap.all.samples <- pheatmap(top.cv.heatmap.counts,
main = "Top 500 Variable Genes - All Samples",
scale = "row",
show_colnames = FALSE,
show_rownames = FALSE,
border_color = NA,
cluster_rows = TRUE,
cluster_cols = TRUE,
clustering_method = "average",
clustering_distance_rows = "correlation",
clustering_distance_cols = "correlation",
color = colorRampPalette(c("blue", "white", "red"))(120),
annotation_row = NULL,
annotation_col = annotation.heatmap,
annotation_colors = anno.colors)
# Skin
# Subset the annotation
annotation.heatmap.skin <- annotation.heatmap %>%
filter(Source == "Skin")
# Get the sample IDs for skin
skin.sample.IDs <- rownames(annotation.heatmap.skin)
# Subset the counts files for only the skin samples
top.cv.heatmap.counts.skin <- as.data.frame(top.cv.heatmap.counts) %>%
select(all_of(skin.sample.IDs))
cv.heatmap.skin <- pheatmap(top.cv.heatmap.counts.skin,
main = "Top 500 Variable Genes - Skin Only",
scale = "row",
show_colnames = FALSE,
show_rownames = FALSE,
border_color = NA,
cluster_rows = TRUE,
cluster_cols = TRUE,
clustering_method = "average",
clustering_distance_rows = "correlation",
clustering_distance_cols = "correlation",
color = colorRampPalette(c("blue", "white", "red"))(120),
annotation_row = NULL,
annotation_col = annotation.heatmap.skin,
annotation_colors = anno.colors)
# GI
# Subset the annotation
annotation.heatmap.gi <- annotation.heatmap %>%
filter(Source == "GI")
# Get the sample IDs for skin
gi.sample.IDs <- rownames(annotation.heatmap.gi)
# Subset the counts files for only the skin samples
top.cv.heatmap.counts.gi <- as.data.frame(top.cv.heatmap.counts) %>%
select(all_of(gi.sample.IDs))
cv.heatmap.gi <- pheatmap(top.cv.heatmap.counts.gi,
main = "Top 500 Variable Genes - GI Only",
scale = "row",
show_colnames = FALSE,
show_rownames = FALSE,
border_color = NA,
cluster_rows = TRUE,
cluster_cols = TRUE,
clustering_method = "average",
clustering_distance_rows = "correlation",
clustering_distance_cols = "correlation",
color = colorRampPalette(c("blue", "white", "red"))(120),
annotation_row = NULL,
annotation_col = annotation.heatmap.gi,
annotation_colors = anno.colors)
# Export all CV heatmaps
export.cv.heatmaps <- FALSE
if(export.cv.heatmaps == TRUE){
ggsave(paste0(results.folder,
run.folder,
"cv_heatmap_all_samples.png"),
cv.heatmap.all.samples,
height = 10,
width = 14)
ggsave(paste0(results.folder,
run.folder,
"cv_heatmap_skin.png"),
cv.heatmap.skin,
height = 10,
width = 14)
ggsave(paste0(results.folder,
run.folder,
"cv_heatmap_gi.png"),
cv.heatmap.gi,
height = 10,
width = 14)
}
```
# Count AOIs per annotation
```{r Count AOIs per annotation, include=TRUE}
# Subset for annotation types to count
annotation.subset <- annotation %>%
select(segment, region, class, Tumor, Source, 'Corrections MOH')
aoi.counts <- lapply(annotation.subset, table)
print(aoi.counts)
```
```{r PCA Setup, include=include.qc}
object <- q3.normalization.output$object
# Load the normalized counts
norm.counts <- object@assayData$q_norm
log.counts <- as.data.frame(log(norm.counts, base = 2))
# Load and clean up the annotation
annotation <- pData(object)
# Order of rownames of annotation need to match columns of count data
cleaned.annotation.df <- annotation[order(rownames(annotation)), ]
log.counts.cleaned <- as.data.frame(log.counts[, order(colnames(log.counts))])
# Create a function for the main PCA annotations
main_annotation_PCA <- function(pca.table){
# Create a PCA for the main annotations, then group together
pca.plot.slide <- biplot(pca.table,
colby = "slide_name",
legendPosition = "right",
legendLabSize = 10,
legendIconSize = 5,
lab = NULL,
title = NULL,
subtitle = "Slide Analysis")
pca.plot.region <- biplot(pca.table,
colby = "region",
legendPosition = "right",
legendLabSize = 10,
legendIconSize = 5,
lab = NULL,
title = NULL,
subtitle = "Region analysis")
pca.plot.segment <- biplot(pca.table,
colby = "segment",
legendPosition = "right",
legendLabSize = 10,
legendIconSize = 5,
lab = NULL,
title = NULL,
subtitle = "Segment Analysis")
pca.plot.class <- biplot(pca.table,
colby = "class",
legendPosition = "right",
legendLabSize = 10,
legendIconSize = 5,
lab = NULL,
title = NULL,
subtitle = "Class Analysis")
combined.plot <- arrangeGrob(ggplotGrob(pca.plot.slide),
ggplotGrob(pca.plot.region),
ggplotGrob(pca.plot.segment),
ggplotGrob(pca.plot.class),
nrow = 2, ncol = 2)
return(combined.plot)
}
```
```{r PCA analysis for all AOIs, include = include.PCA}
# Generate a PCA table for all samples
pca.table <- pca(log.counts.cleaned,
metadata = cleaned.annotation.df,
removeVar = 0.1)
all.aoi.pca <- main_annotation_PCA(pca.table = pca.table)
ggsave("results/April23_2024/all_aoi_pca.pdf", all.aoi.pca, width = 14, height = 12)
# Extra PCAs
pca.plot.gene.detect <- biplot(pca.table,
colby = "DetectionThreshold",
legendPosition = "right",
legendLabSize = 10,
legendIconSize = 5,
lab = NULL,
title = "All AOIs",
subtitle = "by Gene Detection")
print(pca.plot.gene.detect)
```
```{r ## PCA analysis for only immune AOIs, include = include.PCA}
# Subset annotation
annotation.immune <- cleaned.annotation.df %>%
filter(region %in% c("immune", "immune/stroma"))
# Subset counts
log.counts.immune <- log.counts.cleaned %>%
select(rownames(annotation.immune))
pca.immune <- pca(log.counts.immune,
metadata = annotation.immune,
removeVar = 0.1)
all.aoi.pca <- main_annotation_PCA(pca.table = pca.immune)
grid.draw(all.aoi.pca)
#ggsave("results/April23_2024/immune_pca.pdf", all.aoi.pca, width = 14, height = 12)
```
```{r PCA analysis with immune/stroma removed, include = include.PCA}
# Subset annotation
annotation.stroma.removed <- cleaned.annotation.df %>%
filter(region != "immune/stroma")
# Subset counts
log.counts.stroma.removed <- log.counts.cleaned %>%
select(rownames(annotation.stroma.removed))
pca.stroma.removed <- pca(log.counts.stroma.removed,
metadata = annotation.stroma.removed,
removeVar = 0.1)
pca.plot <- main_annotation_PCA(pca.table = pca.stroma.removed)
grid.draw(pca.plot)
#ggsave("results/April23_2024/stroma_removed_pca.pdf", pca.plot, width = 14, height = 12)
```
```{r PCA analysis for tumor only, include = include.PCA}
# Subset annotation
annotation.tumor <- cleaned.annotation.df %>%
filter(region == "tumor")
# Subset counts
log.counts.tumor <- log.counts.cleaned %>%
select(rownames(annotation.tumor))
pca.tumor <- pca(log.counts.tumor,
metadata = annotation.tumor,
removeVar = 0.1)
pca.plot <- main_annotation_PCA(pca.table = pca.tumor)
grid.draw(pca.plot)
#ggsave("results/April23_2024/tumor_only_pca.pdf", pca.plot, width = 14, height = 12)
```
```{r PCA analysis for skin only, include = include.PCA}
# Subset annotation
annotation.skin <- cleaned.annotation.df %>%
filter(Source == "Skin")
# Subset counts
log.counts.skin <- log.counts.cleaned %>%
select(rownames(annotation.skin))
pca.skin <- pca(log.counts.skin,
metadata = annotation.skin,
removeVar = 0.1)
pca.plot <- main_annotation_PCA(pca.table = pca.skin)
grid.draw(pca.plot)
#ggsave("results/April23_2024/skin_only_pca.pdf", pca.plot, width = 14, height = 12)
```
```{r PCA analysis for GI only, include = include.PCA}
# Subset annotation
annotation.gi <- cleaned.annotation.df %>%
filter(Source == "GI")
# Subset counts
log.counts.gi <- log.counts.cleaned %>%
select(rownames(annotation.gi))
pca.gi <- pca(log.counts.gi,
metadata = annotation.gi,
removeVar = 0.1)
pca.plot <- main_annotation_PCA(pca.table = pca.gi)
grid.draw(pca.plot)
#ggsave("results/April23_2024/gi_only_pca.pdf", pca.plot, width = 14, height = 12)
```
```{r PCA analysis for Full ROI only, include = include.PCA}
# Subset annotation
annotation.full_roi <- cleaned.annotation.df %>%
filter(segment == "full_ROI")
# Subset counts
log.counts.full_roi <- log.counts.cleaned %>%
select(rownames(annotation.full_roi))
pca.full_roi <- pca(log.counts.full_roi,
metadata = annotation.full_roi,
removeVar = 0.1)
pca.plot <- main_annotation_PCA(pca.table = pca.full_roi)
grid.draw(pca.plot)
#ggsave("results/April23_2024/full_roi_only_pca.pdf", pca.plot, width = 14, height = 12)
```
```{r PCA analysis for LANA+ only, include = include.PCA}
# Subset annotation
annotation.lana <- cleaned.annotation.df %>%
filter(segment == "LANA_pos")
# Subset counts
log.counts.lana <- log.counts.cleaned %>%
select(rownames(annotation.lana))
pca.lana <- pca(log.counts.lana,
metadata = annotation.lana,
removeVar = 0.1)
pca.plot <- main_annotation_PCA(pca.table = pca.lana)
grid.draw(pca.plot)
#ggsave("results/April23_2024/lana_only_pca.pdf", pca.plot, width = 14, height = 12)
```
# Differential Expression
```{r DE Setup, include=FALSE}
# Set up annotation colors for the heatmaps
anno.colors = list(
Tumor = c(Biopsy = "slateblue",
PDX = "coral"),
region = c(tumor = "pink",
vessel = "seagreen1",
immune = "lightblue",
'immune/stroma' = "maroon"),
segment = c(full_ROI = "lightsalmon",
LANA_pos = "aquamarine"),
Source = c(Skin = "steelblue1",
GI = "indianred")
)
DE.results.folder <- paste0(results.folder, run.folder, "DE/")
DE.results.files <- list.files(DE.results.folder)
```
### DE contrast: Skin Biopsy, Tumor (infected) & Vessel (uninfected), Full ROI
```{r, include=include.DE}
# Setup annotation groups for the contrast
tumor.types <- c("Biopsy")
region.types <- c("tumor", "vessel")
source.types <- c("Skin")
segment.types <- c("full_ROI")
# Define the name of the contrast
contrast.name <- paste0(tumor.types[[1]],
"_",
source.types[[1]],
"_",
segment.types[[1]],
"_",
region.types[[1]],
"_",
region.types[[2]])
# Gather the column names to be used in defining contrasts
contrast.groups.list <- list(
"Tumor" = tumor.types,
"region" = region.types,
"segment" = segment.types,
"Source" = source.types)
# Filter data for the chosen annotations
object <- q3.normalization.output$object
# Generate the counts, annotation, and subset object for lmm
lmm.input <- subset_for_lmm(object = object,
subset.list = contrast.groups.list)
# Create summary table of group counts
summary.table.df <- pData(lmm.input$subset.object) %>%
select(c(names(contrast.groups.list)))
summary.table <- table(summary.table.df)
print("Sample Numbers per Annotation Group")
print("-----------------------------------")
print(summary.table)
# Check if the DE results have already been generated
DE.result.file <- grepl(contrast.name, DE.results.files)
if(sum(DE.result.file) >= 1) {
# Load the previously generated DE results
results.df <- as.data.frame(read.csv(paste0(results.folder, run.folder, "DE/", contrast.name, "_de.results.csv")))
annotation.df <- lmm.input$annotation
annotation.df$loq <- annotation.df$loq[, 1]
write.csv(annotation.df, paste0(results.folder, run.folder, "DE/", contrast.name, "_annotation.csv"), row.names = FALSE)
} else {
# Use the function from the DSPWorkflow package
# Within slide analysis
# Listed contrasts are condition, reference
results.list <- diffExpr(object = lmm.input$subset.object,
analysis.type = "Within Groups",
region.col = "region",
regions = c("tumor", "vessel"),
group.col = "Tumor",
groups = c("Biopsy"),
n.cores = parallel::detectCores())
# Create the results df
results.df <- results.list$results
write.csv(results.df, paste0(results.folder, run.folder, "DE/", contrast.name, "_de.results.csv"))
}
# Adjust column names
logfc.column <- colnames(results.df[grepl("logFC",colnames(results.df))])
results.df$logfc <- results.df[[logfc.column]]
pval.column <- colnames(results.df[grepl("_pval",colnames(results.df))])
results.df$pval <- results.df[[pval.column]]
adj.pval.column <- colnames(results.df[grepl("adjpval",colnames(results.df))])
results.df$padj <- results.df[[adj.pval.column]]
results.df$gene <- results.df$Gene
# Keep only the necessary columns
results.df <- results.df %>% select(c("gene",
"logfc",
"pval",
"padj"))
# Export the results
export.deg.list <- global.export.deg
if(export.deg.list == TRUE){
write.csv(results.df,
file = paste0(results.folder, run.folder, "DE/DEG_lists/", contrast.name, "_deg_list.csv"),
row.names = FALSE)
}
# Make the volcano plot
volcano.output <- make_volcano(lmm.results = results.df,
title = contrast.name,
legend.title = "DE in Tumor",
x.axis.title = "Infected (Tumor) vs. Uninfected (Vessel)")
# Export the volcano
export.volcano <- global.export.volcano
if(export.volcano == TRUE){
ggsave(filename = paste0(results.folder, run.folder, "DE/volcano/", contrast.name, "_volcano_plot.png"),
width = 14,
height = 10)
}
print(volcano.output$volcano.plot)
# Make the heatmap
# Subset the annotation just for the heatmap annotations of interest
annotation.heatmap <- lmm.input$annotation %>%
select(names(anno.colors))
heatmap.plot <- make_heatmap(
normalized.log.counts.df = lmm.input$log.counts,
de.results = results.df,
top.degs = TRUE,
annotation.column = annotation.heatmap,
annotation.row = NULL,
anno.colors = anno.colors,
cluster.rows = FALSE,
cluster.columns = TRUE,
main.title = contrast.name,
row.gaps = NULL,
column.gaps = NULL)
print(heatmap.plot)
export.heatmap <- global.export.heatmap
if(global.export.heatmap == TRUE){
ggsave(heatmap.plot,
filename = paste0(results.folder, run.folder, "DE/heatmap/", contrast.name, "_heatmap_plot.png"),
width = 14,
height = 10)
}
```