-
Notifications
You must be signed in to change notification settings - Fork 1
/
fig4cd_tumorDeconv.R
154 lines (118 loc) · 5.2 KB
/
fig4cd_tumorDeconv.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
##run tumor deconvolution on organoids
library(immunedeconv)
library(dplyr)
library(tidyr)
library(pheatmap)
source("orgPlottingFunctions.R")
source("loadExpFromCounts.R")
#source("loadOrganoidData.R")
library(ggplot2)
library(GSVA)
##load up one more annotation file
bannotes[which(bannotes$individualID=='NF0007'),'individualID']<-
sapply(rownames(bannotes[which(bannotes$individualID=='NF0007'),]),function(x)
unlist(stringr::str_split(x,pattern='[ |-][M|D|S]'))[1])
#' runImmuneDeconv
#' @param tab
#' @param method
#' @prefix
#' @return Tidied data frame
runImmuneDeconv<-function(tab,method,prefix='organoids'){
#run MCP counter
mat<-reshape2::acast(tab,Symbol~specimenID,value.var='totalCounts',fun.aggregate=mean,na.rm=T)
nas<-which(apply(mat,1,function(x) any(is.na(x))))
if(length(nas)>0)
mat<-mat[-nas,]
res<-deconvolute(mat,method)
df<-tab|>
as.data.frame()|>
dplyr::select(c(specimenID,experimentalCondition))%>%
unique()#%>%
# rename(study='studyName')
rownames(df)<-df$specimenID
#save as heatmap with metadata
mtab<-res%>%
as.data.frame()|>
dplyr::select(-cell_type)
rownames(mtab)<-res$cell_type
##now tidy up data to table
td<-tidyr::gather(res,key="specimenID",value="score",-cell_type )%>%
left_join(df,by='specimenID')
td$method=method
return(td)
}
#if(FALSE){
##NEED TO DOWNLOD FILES MANUALLY I CANNOT DISTRIBUTE
set_cibersort_binary('./CIBERSORT.R')
set_cibersort_mat('./LM22.txt')
xc<-runImmuneDeconv(rnaseq,'xcell')
mc<-runImmuneDeconv(rnaseq,'mcp_counter')
cs<-runImmuneDeconv(rnaseq,'epic')
#cs<-runImmuneDeconv(rnaseq,'cibersort')
full.tab<-rbind(xc,mc,cs)
write.table(full.tab,'deconvfile.tsv',sep='\t',quote=FALSE,row.names=F)
##MCP Counter
mat<-subset(full.tab,method=='mcp_counter')%>%
pivot_wider(id_cols=-c(method,experimentalCondition),
values_from=score,names_from=specimenID)%>%
tibble::column_to_rownames('cell_type')
pheatmap(log10(1+mat),annotation_col=bannotes,cellheight=10,
clustering_distance_rows = 'correlation',
clustering_distance_cols='correlation',method='ward.D2',
cellwidth=10,annotation_colors=annote.colors,filename = 'all_mcpTypes.pdf',width=18)
mat<-subset(full.tab,method=='mcp_counter')%>%
subset(!specimenID%in%pats)%>%
pivot_wider(id_cols=-c(method,experimentalCondition),values_from=score,names_from=specimenID)%>%
tibble::column_to_rownames('cell_type')
pheatmap(log10(1+mat),annotation_col=bannotes,cellheight=10,method='ward.D2',
#clustering_distance_rows = 'correlation',clustering_distance_cols='correlation',
cellwidth=10,annotation_colors=annote.colors,filename = 'fig4c_mcpTypes.pdf',width=18)
#mcp.cor<-plotCorrelationBetweenSamps(mat,annotes,'mcpCounter')
##CIBERSORT
mat<-subset(full.tab,method=='epic')%>%
pivot_wider(id_cols=-c(method,experimentalCondition),values_from=score,names_from=specimenID)%>%
tibble::column_to_rownames('cell_type')
#all.c<-lapply(condlist,function(x){
# getDifferencesInCondition(mat,biga,x,'Cibersort',doPlot=FALSE)
#})
#names(all.c)<-condlist
#cellCors<-data.frame(all.c)%>%mutate(dataType='cibersortPreds')%>%rbind(cellCors)
#cs.cor<-plotCorrelationBetweenSamps(mat,annotes,'cibersort')
pheatmap(log10(1+mat),annotation_col=bannotes,cellheight=10,clustering_distance_rows = 'correlation',
clustering_distance_cols='correlation',method='ward.D2',
cellwidth=10,annotation_colors=annote.colors,
filename = 'all_epicTypes.pdf',width=18)
mat<-subset(full.tab,method=='epic')%>%
subset(!specimenID%in%pats)%>%
pivot_wider(id_cols=-c(method,experimentalCondition),values_from=score,names_from=specimenID)%>%
tibble::column_to_rownames('cell_type')
pheatmap(log10(1+mat),annotation_col=bannotes,cellheight=10,method='ward.D2',
#clustering_distance_rows = 'correlation',clustering_distance_cols='correlation',
cellwidth=10,annotation_colors=annote.colors,
filename = 'epicTypes.pdf',width=18)
#XCELL
mat<-subset(full.tab,method=='xcell')%>%
subset(!specimenID%in%pats)%>%
pivot_wider(id_cols=-c(method,experimentalCondition),values_from=score,names_from=specimenID)%>%
tibble::column_to_rownames('cell_type')
pheatmap(log10(1+mat),annotation_col=bannotes,cellheight=10,#clustering_distance_rows = 'correlation',
#clustering_distance_cols='correlation',
cellwidth=10,annotation_colors=annote.colors,method='ward.D2',
filename = 'xcellTypes.pdf',width=18)
#rbind(cs.cor,mcp.cor,xc.cor)%>%
# ggplot(aes(x=Media,y=Similarity,fill=dataType,shape=Cytokines))+geom_boxplot()+
# geom_jitter()+scale_fill_manual(values=pal)
######last part of figure is matrisome
mat.rest <-rnaseq|>
subset(!specimenID%in%pats)|>
dplyr::select(specimenID,Symbol,totalCounts)|>
dplyr::distinct()|>
tidyr::pivot_wider(names_from='specimenID',values_from='totalCounts')|>
tibble::column_to_rownames('Symbol')|>
as.matrix()
GSET <- apply(gset,2,function(x) setdiff(x,""))
res <- gsva(mat.rest,GSET,method="ssgsea",min.sz=1,max.sz=Inf,mx.diff=TRUE,parallel.sz=1,verbose=TRUE)
#res <- t(as.data.frame(res))
print(res)
pheatmap(res,annotation_col = bannotes,annotation_colors=annote.colors,
cellheight=10,cellwidth=10,filename='fig4d_matrisomeGSVA.pdf')