-
Notifications
You must be signed in to change notification settings - Fork 1
/
functions.R
233 lines (202 loc) · 8.51 KB
/
functions.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
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
require(dplyr)
library(slingshot)
getGeneRange <- function(scrna,gene_probes){
gene_values=as.data.frame(FetchData(scrna,gene_probes[1]))
minr<- round(min(gene_values),2)
maxr<- round(max(gene_values),2)
return(c(ifelse(minr==0,.1,minr-.1),maxr))
#return(c(1,12))
}
bigene_getValues <- function(scrna,gene_probes,limita,limitb){
gene_values=FetchData(scrna,c(gene_probes[1],gene_probes[2]))
colnames(gene_values) <- c('genea','geneb')
# gene_values=as.data.frame(gene_values) %>%
# mutate(value =ifelse(genea>=limita[1] & geneb <limitb[1], gene_probes[1], ifelse(genea<limita[1] & geneb >=limitb[1],
# gene_probes[2],ifelse(genea>=limita[1] & geneb >=limitb[1],"DoublePos","NULL"))))
as.data.frame(gene_values) %>%
mutate(value = ifelse(genea>=limita[1] & geneb>=limitb[1],
'both',
ifelse(genea>=limita[1] & geneb<limitb[1],
gene_probes[1],
ifelse(genea<=limita[1] & geneb>=limitb[1],
gene_probes[2],
'none')))
) #%>% select(value)
}
monocle_theme_opts <- function()
{
theme(strip.background = element_rect(colour = 'white', fill = 'white')) +
theme(panel.border = element_blank()) +
theme(axis.line.x = element_line(size=0.25, color="black")) +
theme(axis.line.y = element_line(size=0.25, color="black")) +
theme(panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank()) +
theme(panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank()) +
theme(panel.background = element_rect(fill='white')) +
theme(legend.key=element_blank())
}
bigene_plot <- function (scrna, gene_probes, x=1,y=2, limita=c(1,100), limitb=c(1,100), marker_size = 0.1,
title = NULL,type="tsne")
{
gene_values <- bigene_getValues(scrna,gene_probes,limita,limitb)
projection=as.data.frame(eval(parse(text=paste("Embeddings(scrna, reduction =\"",type,"\")",sep=""))))
colnames(projection) <- c("Component.1", "Component.2")
proj_gene <- data.frame(cbind(projection, gene_values))
#proj_gene$value = factor(proj_gene$value,levels=unique(proj_gene$value))
proj_gene$value = factor(proj_gene$value,levels=c('both',gene_probes[1],gene_probes[2],'none'))
proj_gene <- arrange(proj_gene, desc(value))
p <- ggplot(proj_gene, aes(Component.1, Component.2)) +
geom_point(aes(colour = value), size = marker_size) +
scale_color_manual(values=c("#E41A1C","#377EB8","#4DAF4A", 'grey75'),drop=F) +
theme(legend.key.size = unit(10,"point")) + xlab(paste("Component", x)) +
ylab(paste("Component", y))
if (!is.null(title)) {
p <- p + ggtitle(title)
}
p <- p + monocle_theme_opts() + theme(plot.title = element_text(hjust = 0.5),
legend.position="bottom",
legend.title=element_blank(),
legend.text=element_text(size=14),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
return(p)
}
#given scrna data, variable/ ident and project name, find all lig rec pairs
ligrec <- function(scrna,pair,prj,perc,filetype){
#get grouping variable
var=as.character(pair)
tt=rownames(GetAssayData(object = scrna, slot = "counts"))
#Read ligrec file based on organism
file = read.csv("data/param.csv")
if (filetype=="list"){
org=as.character(file$organism[file$projects==prj])}
else if(filetype == "upload"){
org = unique([email protected]$org)
}
#genes=fread("data/ligrecgenes.txt",header = TRUE)
if(org=="mouse"){rl=read.csv("data/Mm_PairsLigRec.csv")}else if(org=="human"){rl=read.csv("data/Hs_PairsLigRec.csv")}
genes=unique(c(as.character(rl$ligand),as.character(rl$receptor)))
# if(org=="human"){
# genes$genes=toupper(genes$genes)
# }
genes2=tt[tt %in% genes]
#For all unique genes in the ligrec list, get their expression value for all cells and the groups the cells belong to
my.data=FetchData(scrna,c(var,genes2))
colnames(my.data)[1]= "clust"
#my.data$clust=factor(my.data$clust,levels=unique(my.data$clust))
perc=perc/100
#if(org=="mouse"){rl=read.csv("data/Mm_PairsLigRec.csv")}else if(org=="human"){rl=read.csv("data/Hs_PairsLigRec.csv")}
result=data.frame()
res=data.frame()
#loop over each cluster to find pairs
for(i in 1:(length(levels(my.data$clust)))){
for(j in 1:(length(levels(my.data$clust)))){
# for(i in levels(my.data$clust)){
# for(j in levels(my.data$clust)){
#if(i!=j){
#from the large martix, subselect receptor and lig subgoups (if i=1 and j=2, keep cells in grps 1 and 2)
test=my.data[my.data$clust==levels(my.data$clust)[i] | my.data$clust==levels(my.data$clust)[j],]
#Subselect genes in receptor list in cells in rec subgroup (say 1)
R_c1=test[test$clust==levels(my.data$clust)[i] ,(colnames(test) %in% rl$receptor)]
#Subselect genes in ligand list in cells in lig subgroup (say 2)
L_c2=test[test$clust==levels(my.data$clust)[j] , (colnames(test) %in% rl$ligand)]
if(nrow(R_c1)!=0 &nrow(L_c2)!=0){
#keep genes that are expressed in more than user-input percent of the cells
keep1 = colSums(R_c1>0)>=perc*dim(R_c1)[1]
keep2 = colSums(L_c2>0)>=perc*dim(L_c2)[1]
R_c1=R_c1[,keep1]
L_c2=L_c2[,keep2]
#get list of lig-rec pairs
res=rl[(rl$ligand %in% colnames(L_c2)) & (rl$receptor %in% colnames(R_c1)),]
}else{}
# }
# else{}
if(nrow(res)!=0){
res$Receptor_cluster=levels(my.data$clust)[i]
res$Lig_cluster=levels(my.data$clust)[j]
result=rbind(result,res)
}else{result=result}
}
}
# get final list of all lig-rec pairs
#result=result[result$Receptor_cluster!=result$Lig_cluster,]
return(result)
}
#function to curve graph edges in igraph
autocurve.edges2 <-function (graph, start = 0.5)
{
cm <- count.multiple(graph)
mut <-is.mutual(graph) #are connections mutual?
el <- apply(get.edgelist(graph, names = FALSE), 1, paste,
collapse = ":")
ord <- order(el)
res <- numeric(length(ord))
p <- 1
while (p <= length(res)) {
m <- cm[ord[p]]
mut.obs <-mut[ord[p]] #are the connections mutual for this point?
idx <- p:(p + m - 1)
if (m == 1 & mut.obs==FALSE) { #no mutual conn = no curve
r <- 0
}
else {
r <- seq(-start, start, length = m)
}
res[ord[idx]] <- r
p <- p + m
}
res
}
#Get maximum dimensions run
getMaxDim <- function(object){
object@reductions$pca@jackstraw$overall.p.values %>%
as.data.frame(.) %>%
mutate(adj = p.adjust(Score,method='bonferroni')) %>%
filter(adj <0.05) %>%
summarise(max=max(PC)) %>%
pull(max)
}
#Seurat Extras functions
CurvePlot = function(object,
sds=NULL,
group.by = NULL,
reduction = 'umap',
dims = 1:2,
cols=NULL,
label=T
) {
object[['ident']] <- Idents(object = object)
group.by <- group.by %||% 'ident'
dims <- paste0(Key(object = object[[reduction]]), dims)
curved <-
bind_rows(lapply(names(slingCurves(sds)), function(x) {
c <- slingCurves(sds)[[x]]
d <- as.data.frame(c$s[c$ord, dims])
d$curve <- x
return(d)
}))
DimPlot(object,cols=cols,label = label,group.by = group.by,reduction = reduction) +
geom_path(aes_string(dims[1], dims[2], linetype = "curve"), curved, size =1)
}
`%||%` <- function(lhs, rhs) {
if (!is.null(x = lhs)) {
return(lhs)
} else {
return(rhs)
}
}
runSlingshot_int <- function(object,sds.name='sds',reduction='dm',group.by=NULL, start.clus=NULL,end.clus=NULL, allow.breaks=TRUE, extend='n',stretch=0){
rd <- Embeddings(object,reduction)
#Set ident to groupby option
if(!is.null(group.by)){
Idents(object)<- group.by
}
cl <- Idents(object = object)
### Need to fix this not working
#group.by <- group.by %||% 'ident'
#cl <- FetchData(object = object, vars = group.by) %>% pull(`group.by`)
object@misc[[sds.name]] <- list("dr"=reduction,"data"=slingshot(rd,cl,start.clus=start.clus,end.clus=end.clus,allow.breaks=allow.breaks,extend=extend,stretch=stretch))
#ps <- slingPseudotime(object@misc[['sds']]$data)
#[email protected][,colnames(ps)] <- as.data.frame(ps)
object <- LogSeuratCommand(object = object)
return(object)
}