forked from gabrielakinker/CCLE_heterogeneity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module4-DRT.Rmd
189 lines (138 loc) · 10.1 KB
/
Module4-DRT.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
---
title: "Kinker test"
output: html_notebook
author: Darren Tyson
date: 2023-12-04
---
## Kinker et al. 2021
Reference here
Working on figure 4. Pulling code from `module4_vitro_vs_vivo_II.R`.
### Module 4
Evaluating expression heterogeneity in HNSCC and melanoma by analyzing human tumors and cell lines as a single dataset
#### Basic setup
Load necessary R packages and functions
```{r}
library(reshape2)
library(ggplot2)
library(scales)
```
```{r}
# path to where output should be saved
OUTPUT_PATH <- "~/dropbox-vu/temp/kinker_output/Output"
# path to where required data are
DATAPATH <- "~/dropbox-vu/temp/Kinker_data/"
# whether to overwrite output files is already exist
OVERWRITE <- FALSE
```
```{r Load data}
# read scRNA-seq data from cell lines and tumors
expr_ccle <- readRDS(file.path(DATAPATH,"CCLE_heterogeneity_Rfiles/CCLE_scRNAseq_CPM.RDS")) # CCLE cell lines
expr_tumor <- readRDS(file.path(DATAPATH,"CCLE_heterogeneity_Rfiles/tumors_scRNAseq_logTPM.RDS")) # human tumors
# calculate average gene expression
rowmeans_ccle <- lapply(expr_ccle, rowMeans)
rowmeans_tumor <- lapply(expr_tumor, function(x) rowMeans(10*((2^x)-1)))
# process data
expr_ccle<- lapply(expr_ccle, function(x) {log2((x/10) + 1)})
expr_ccle <- lapply(expr_ccle, function(x) {x-rowMeans(x)})
expr_tumor <- lapply(expr_tumor, function(x) {x-rowMeans(x)})
```
```{r Load NMF programs}
nmf_metaprograms_programs_nc_ccle <- readRDS(file.path(OUTPUT_PATH,"module2/nmf_metaprograms_programs_nc_ccle.RDS"))
nmf_metaprograms_programs_nc_tumor<- readRDS(file.path(OUTPUT_PATH,"module2/nmf_metaprograms_programs_nc_tumor.RDS")) # programs in each tumor metaprogram
```
```{r Processing}
episen_emt_ccle <- intersect(gsub(".{4}$", "",nmf_metaprograms_programs_nc_ccle$episen), gsub(".{4}$", "",nmf_metaprograms_programs_nc_ccle$emtII))
episen_emt_ccle <- unique(episen_emt_ccle[grep("UPPER", episen_emt_ccle)]) # HNCSS cell lines harboring both EpiSen and EMTII
episen_emt_tumor <- unique(intersect(gsub(".{4}$", "",nmf_metaprograms_programs_nc_tumor$episen), gsub(".{4}$", "",nmf_metaprograms_programs_nc_tumor$emt))) # HNCSS tumors harboring both EpiSen and EMTII
skinpig_emt_ccle <- intersect(gsub(".{4}$", "",nmf_metaprograms_programs_nc_ccle$skinpig), gsub(".{4}$", "",nmf_metaprograms_programs_nc_ccle$emtI)) # melanoma cell lines harboring both skinpig and EMTI
skinpig_emt_tumor <- names(expr_tumor)[grep("mel", names(expr_tumor))] # melanoma tumors harboring both skinpig and EMTI
# read metaprogram signatures from cell lines
# meta_sig_ccle <- readRDS("Expected_results/module2/nmf_metaprograms_sig_nc_ccle.RDS")
meta_sig_ccle <- readRDS(file.path(OUTPUT_PATH,"module2/nmf_metaprograms_sig_nc_ccle.RDS"))
meta_sig_ccle <- meta_sig_ccle[c("skinpig", "emtI", "episen", "emtII")]
names(meta_sig_ccle) <- paste0(names(meta_sig_ccle), "_vitro")
meta_sig_ccle <- lapply(meta_sig_ccle, function(x) names(x[1:100]))
```
```{r}
meta_sig_tumor <- unlist(list(read.table(file.path(DATAPATH,"CCLE_heterogeneity_Rfiles/metaprograms_tumors_literature.txt"), sep = "\t", header = T,stringsAsFactors = F)), recursive=F)
meta_sig_tumor <- meta_sig_tumor[c("melanoma.MITF.program", "melanoma.AXL.program", "HNSCC.PEMT","HNSCC.Epidif.1" )]
meta_sig_tumor <- lapply(meta_sig_tumor, function(x) x[x!=""])
# get genes shared between programs in tumors and cell lines
meta_intersect <- lapply(meta_sig_ccle, function(x) lapply(meta_sig_tumor, function(y) intersect(x,y)))
```
### Combine datasets and run PCA - HNSCC
```{r}
# get expression data from selected cell lines
expr_emt_episen_vitro <- expr_ccle[episen_emt_ccle]
expr_emt_episen_vivo <- expr_tumor[episen_emt_tumor]
# select common genes in the cell line and tumor datasets
expr_emt_episen_vitro <- sapply(expr_emt_episen_vitro, function(x) x[Reduce(intersect, c(lapply( expr_emt_episen_vitro, rownames), lapply(expr_emt_episen_vivo, rownames))),])
expr_emt_episen_vivo <- sapply(expr_emt_episen_vivo, function(x) x[Reduce(intersect, c(lapply(expr_emt_episen_vitro, rownames), lapply(expr_emt_episen_vivo, rownames))),])
# unlist datasets
expr_emt_episen_vitro <- do.call(cbind,expr_emt_episen_vitro )
expr_emt_episen_vivo <- do.call(cbind, expr_emt_episen_vivo)
# select top expressed genes among cell lines and tumors
rowmeans_ccle_hnscc <-rowMeans(sapply(rowmeans_ccle[episen_emt_ccle], function(x) x[rownames(expr_emt_episen_vitro)]))
rowmeans_tumor_hnscc <- rowMeans(sapply(rowmeans_tumor[episen_emt_tumor], function(x) x[rownames(expr_emt_episen_vitro)]))
expr_emt_episen_vitro_filt <- expr_emt_episen_vitro[order(rowMeans(cbind(rowmeans_ccle_hnscc, rowmeans_tumor_hnscc)), decreasing=T)[1:4500] ,]
expr_emt_episen_vivo_filt <- expr_emt_episen_vivo[order(rowMeans(cbind(rowmeans_ccle_hnscc, rowmeans_tumor_hnscc)), decreasing=T)[1:4500] ,]
# combine the two datasets
expr_emt_episen_final <- cbind(expr_emt_episen_vitro_filt,expr_emt_episen_vivo_filt)
# run pca
expr_emt_episen_pca <- prcomp(t(expr_emt_episen_final))
```
```{r}
# **************************************************************************
# Combine datasets and run PCA - melanoma
# get expression data from selected cell lines
expr_skinpig_emt_vitro <- expr_ccle[skinpig_emt_ccle]
expr_skinpig_emt_vivo <- expr_tumor[skinpig_emt_tumor]
# select common genes in the cell line and tumor datasets
expr_skinpig_emt_vitro <- sapply(expr_skinpig_emt_vitro, function(x) x[Reduce(intersect, c(lapply( expr_skinpig_emt_vitro, rownames), lapply(expr_skinpig_emt_vivo, rownames))),])
expr_skinpig_emt_vivo <- sapply(expr_skinpig_emt_vivo, function(x) x[Reduce(intersect, c(lapply(expr_skinpig_emt_vitro, rownames), lapply(expr_skinpig_emt_vivo, rownames))),])
# unlist datasets
expr_skinpig_emt_vitro <- do.call(cbind, expr_skinpig_emt_vitro)
expr_skinpig_emt_vivo <- do.call(cbind, expr_skinpig_emt_vivo)
# select top expressed genes among cell lines and tumors
rowmeans_ccle_melanoma <-rowMeans(sapply(rowmeans_ccle[skinpig_emt_ccle], function(x) x[rownames(expr_skinpig_emt_vitro)]))
rowmeans_tumor_melanoma<- rowMeans(sapply(rowmeans_tumor[skinpig_emt_tumor], function(x) x[rownames(expr_skinpig_emt_vitro)]))
expr_skinpig_emt_vitro_filt <- expr_skinpig_emt_vitro[order(rowMeans(cbind(rowmeans_ccle_melanoma, rowmeans_tumor_melanoma)), decreasing=T)[1:4500] ,]
expr_skinpig_emt_vivo_filt <- expr_skinpig_emt_vivo[order(rowMeans(cbind(rowmeans_ccle_melanoma, rowmeans_tumor_melanoma)), decreasing=T)[1:4500] ,]
# combine the two datasets
expr_skinpig_emt_final <- cbind(expr_skinpig_emt_vitro_filt, expr_skinpig_emt_vivo_filt)
# run PCA
expr_skinpig_emt_pca <- prcomp(t(expr_skinpig_emt_final))
```
```{r}
# **************************************************************************
# Plot PCA results - HNSCC
# store pca coordinates in dataframe
expr_emt_episen_plot <- data.frame(expr_emt_episen_pca$x[,1:5])
# calculate program scores
expr_emt_episen_plot$emtII <- colMeans(expr_emt_episen_final[is.element(rownames(expr_emt_episen_final), meta_intersect$emtII_vitro$HNSCC.PEMT),])
expr_emt_episen_plot$episen=colMeans(expr_emt_episen_final[is.element(rownames(expr_emt_episen_final), meta_intersect$episen_vitro$`HNSCC.Epidif.1`),])
# add metadata
expr_emt_episen_plot$type <- rep(c("in vitro", "in vivo"), c(ncol(expr_emt_episen_vitro), ncol(expr_emt_episen_vivo)))
expr_emt_episen_plot$names <- c(rep(episen_emt_ccle, sapply(expr_ccle[episen_emt_ccle],ncol)),rep(episen_emt_tumor, sapply(expr_tumor[episen_emt_tumor],ncol)) )
expr_emt_episen_plot$cell_line <- NA
expr_emt_episen_plot$cell_line[expr_emt_episen_plot$type=="in vitro"] <- expr_emt_episen_plot$names[expr_emt_episen_plot$type=="in vitro"]
expr_emt_episen_plot$cell_line <- gsub("_UPPER_AERODIGESTIVE_TRACT", "", expr_emt_episen_plot$cell_line)
expr_emt_episen_plot$tumor <- NA
expr_emt_episen_plot$tumor[expr_emt_episen_plot$type=="in vivo"] <- expr_emt_episen_plot$names[expr_emt_episen_plot$type=="in vivo"]
```
```{r}
# plot pca coordinates
fn1 <- file.path(OUTPUT_PATH,"module4/HNSCC_pca_program.pdf")
if(OVERWRITE | !file.exists(fn1))
pdf(fn1, width = 3.9, height = 3.8)
ggplot(expr_emt_episen_plot, aes(x=PC2, y=PC4+PC5, color=emtII-episen)) +
geom_point( size=0.5) +
scale_color_gradient2(limits=c(-3, 3), midpoint = 0, low= c("#053061" ,"#2166AC" ,"#4393C3" ,"#92C5DE" ), mid= c("#D1E5F0", "gray95", "#FDDBC7"), high = c( "#F4A582", "#D6604D" ,"#B2182B" ,"#67001F") , oob=squish, name="Relative score\n(EMT II - Episen)") +
theme(panel.background = element_blank(), panel.border = element_rect(fill=F), legend.text.align = 0.5, axis.text=element_text(size=12), axis.title = element_text(size=13), legend.title.align = 0.5, legend.position = "top", legend.title = element_text(size=12, vjust = 1), legend.text = element_text(size=11), plot.margin=unit(c(0.1,2,0.1,0.1), "cm"))
if(OVERWRITE | !file.exists(fn1)) {
message(cat("writing file ",fn1,"\n"))
dev.off()
} else {
message(paste("File exists; not overwriting ",fn1))
}
```