-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcitefunction.R
More file actions
501 lines (464 loc) · 18 KB
/
citefunction.R
File metadata and controls
501 lines (464 loc) · 18 KB
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
# Install required packages
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
if (!requireNamespace("devtools", quietly = TRUE))
install.packages("devtools")
if (!requireNamespace("Seurat", quietly = TRUE))
install.packages('Seurat')
if (!requireNamespace("plyr", quietly = TRUE))
install.packages('plyr')
if (!requireNamespace("dsb", quietly = TRUE))
install.packages('dsb')
if (!requireNamespace("ComplexHeatmap", quietly = TRUE))
BiocManager::install("ComplexHeatmap")
if (!requireNamespace("RColorBrewer", quietly = TRUE))
install.packages('RColorBrewer')
if (!requireNamespace("reticulate", quietly = TRUE))
install.packages("reticulate")
if (!requireNamespace("CellChat", quietly = TRUE))
devtools::install_github("sqjin/CellChat")
if (!requireNamespace("patchwork", quietly = TRUE))
devtools::install_github("thomasp85/patchwork")
if (!requireNamespace("scater", quietly = TRUE))
BiocManager::install("scater")
# Creat a seuart object
# input:
# h5Path: address of h5 file to read. (When dataFormat is 'h5', it couldn't be NULL.)
# rna_matrix: a RNA matrix where the rows correspond to genes and the columns correspond to cells. (When dataFormat is 'matrixs', it couldn't be NULL.)
# atac_matrix: a matrix where the rows correspond to peaks and the columns correspond to cells. (When data_type is 'scRNA_scATAC'and dataFormat is 'matrixs', it couldn't be NULL.)
# adt_matrix: a matrix where the rows correspond to proteins and the columns correspond to cells. (When data_type is 'CITE' and dataFormat is 'matrixs', it couldn't be NULL.)
# data_type: the type of your input data('CITE' or 'scRNA_scATAC')
# dataFormat: the format of your input data ('matrixs' or 'h5')
# min_cell: the peak / gene will be removed if the value in the gene / peak with more than min_cell cell is equal to zero
# nmad: a numeric scalar, specifying the minimum number of MADs away from median required for a value to be called an outlier
# gene.filter: if do gene filtering
# gene.filter: if do cell filtering
# output:
# - obj: a seuart object for cite-seq data after normalizing and scaling (When data_type is 'CITE'); a seuart object for scRNA-seq and scATAC-seq (When data_type is 'scRNA_scATAC')
ReadData <- function(h5Path = NULL, rna_matrix = NULL, atac_matrix = NULL, adt_matrix = NULL, data_type = NULL, dataFormat = NULL, min_cell=0.1, nmad=3, gene.filter=TRUE, cell.filter=TRUE){
if (data_type=='CITE'){
# obtain RNA and ADT matrixs
if (dataFormat=='matrixs'){
rna <- rna_matrix
adt <- adt_matrix
}else if(dataFormat=='h5'){
h5 <- Read10X_h5(h5Path)
rna <- h5$`Gene Expression`
adt <- h5$`Antibody Capture`
}
# gene filtering
if (gene.filter==TRUE){
binaryrna <- rna
binaryadt <- adt
binaryrna[binaryrna>0] <-1
binaryadt[binaryadt>0] <-1
rna <- rna[which(rowSums(binaryrna) > ncol(binaryrna)*min_cell),]
adt <- adt[which(rowSums(binaryadt) > ncol(binaryadt)*min_cell),]
}
#Setup a Seurat object, add the RNA and protein data
obj <- CreateSeuratObject(counts = rna)
obj [["percent.mt"]] <- PercentageFeatureSet(obj, pattern = "^MT-")
adt_assay <- CreateAssayObject(counts = adt)
obj[["ADT"]] <- adt_assay
# cell filtering
if (cell.filter==TRUE){
adtn<-isOutlier(
obj$nCount_ADT,
nmads = nmad,
log = F,
type = "both"
)
rnan<-isOutlier(
obj$nCount_RNA,
nmads = nmad,
log = F,
type = "both"
)
mito<-isOutlier(
obj$percent.mt,
nmads = nmad,
log = F,
type = "both"
)
obj <-
AddMetaData(obj, adtn, col.name = "adtn")
obj <-
AddMetaData(obj, rnan, col.name = "rnan")
obj <-
AddMetaData(obj, mito, col.name = "mito")
obj<-subset(
x = obj,
subset = adtn == F &
rnan == F &
mito == F
)
}
# normalization and scaling
DefaultAssay(obj) <- 'RNA'
obj <- NormalizeData(obj) %>% FindVariableFeatures()
all.genes <- rownames(obj)
obj <- ScaleData(obj, features = all.genes)
DefaultAssay(obj) <- 'ADT'
VariableFeatures(obj) <- rownames(obj[["ADT"]])
obj <- NormalizeData(obj, normalization.method = 'CLR', margin = 2) %>%
ScaleData()
DefaultAssay(obj) <- 'RNA'
}else if (data_type=='scRNA_scATAC'){
if (gene.filter==FALSE){
min_cell = 0
}
if (dataFormat == "h5") {
obj <- Read10Xdata(h5Path = h5Path, min_cell = min_cell)
}else{
obj <- readmatrix(rna_matrix = rna_matrix, atac_matrix = atac_matrix, min_cell = min_cell)
}
if (cell.filter==TRUE){
obj <- filterCell(obj, nmad= nmad)
}
}
return(obj)
}
# splice togethor the RNA matrix and ADT matrix and do normalization
# input:
# obj: a seuart object obtained from creatobject fuction
# output:
# GAS: the spliced and normalized matrix as the input of HGT model
CLR <- function(obj){
m1 <- obj@assays$RNA@counts[obj@assays$RNA@var.features,]
m2 <- obj@assays$ADT@counts
m3 <- m2
# Add 'p_' to the name of the protein
rownames(m3) <- paste0('p_',rownames(m2))
m <- NormalizeData(rbind( m1, m3), normalization.method = 'CLR', margin = 2)
return(m)
}
# Cluster cells based on HGT embedding
# input:
# cell_hgt_matrixPath: the path of cell_embedding matrix
# nhid: hyper-parameter of HGT model
# resolution: resolution of cell clustering
# obj: a seurat object obtained from creatobject fuction
# output:
# obj: a seurat object containing cell clustering result
HGT_cluster <- function(obj, cell_hgt_matrix, nhid, resolution){
cell_hgt_matrix<-as.matrix(cell_hgt_matrix)
rownames(cell_hgt_matrix) <-colnames(rna)
HGT_embedding <-
CreateDimReducObject(
embeddings = cell_hgt_matrix,
key = "HGT_",
assay = "RNA"
)
obj<-obj[,colnames(rna)]
obj@reductions[['HGT']] <- HGT_embedding
for(j in 1:ncol(obj@reductions$HGT@cell.embeddings)){
obj[[paste0('HGT_',j)]] <- obj@reductions$HGT@cell.embeddings[,paste0('HGT_',j)]
}
obj <- FindNeighbors(obj, reduction = "HGT",dims=1:nhid)
obj <- FindClusters(obj, resolution = resolution)
return(obj)
}
# Draw a heatmap of marker proteins or genes
# required packages: dsb, ComplexHeatmap
# input:
# obj: a seurat object obtained from HGT_cluster fuction
# marker: character of markers(the order of markers corresponding to ctorder)
# ctorder: the order of celltypes to display
# assays: RNA or ADT
# output:
# a heatmap of marker proteins or genes
MarkerHeatmap <- function(obj,marker,ctorder,assays){
sortmarker <- 1:length(marker)
names(sortmarker) <- marker
if (assays == 'RNA'){
rnamarker <- intersect(rownames(obj@assays$RNA@counts),marker)
rnamarker <- rnamarker[order(sortmarker[rnamarker])]
rnam <- AverageExpression(obj,assays = 'RNA', slot='counts',group.by = 'cell_type',features = rnamarker)
rnam <- rnam[[1]][unique(rnamarker),ctorder]
t <- DSBNormalizeProtein(rnam,rnam,use.isotype.control =FALSE,denoise.counts =FALSE)
}else if(assays == 'ADT'){
adtmarker <- intersect(rownames(obj@assays$ADT@counts),marker)
adtmarker <- adtmarker[order(sortmarker[adtmarker])]
adtm <- AverageExpression(obj,assays = 'ADT', slot='counts',group.by = 'cell_type',features = adtmarker)
adtm <- adtm[[1]][unique(adtmarker),ctorder]
t <- DSBNormalizeProtein(adtm,adtm,use.isotype.control =FALSE,denoise.counts =FALSE)
}
Heatmap(t(t),c("white","blue"),cluster_rows = F,cluster_columns = F,
heatmap_legend_param = list( title = "normalized.expression",title_position = "leftcenter-rot" ))
}
# take a subset of obj and preprocess data again
# input:
# obj: a seurat object obtained from HGT_cluster fuction
# I: cell names subset
# output:
# obj: a subset of obj after preprocessing
subobject <- function(obj,I){
obj <- obj[,I]
DefaultAssay(obj) <- 'RNA'
obj <- NormalizeData(obj) %>% FindVariableFeatures()
all.genes <- rownames(obj)
obj <- ScaleData(obj, features = all.genes)
DefaultAssay(obj) <- 'ADT'
VariableFeatures(obj) <- rownames(obj[["ADT"]])
obj <- NormalizeData(obj, normalization.method = 'CLR', margin = 2) %>%
ScaleData()
return(obj)
}
# find features mostly associated with a given feature based on the expression of a type of features and two types of cells.
# input:
# feature: a given feature (gene or protein)
# ident: an expression matrix of a type of features and two types of cells.
# output:
# co: features mostly associated with the given feature
Findcofeatures <- function(feature,ident){
y <- ident[feature,]
co <- c()
for (f in rownames(ident)){
co <- rbind(co, c(f,cor(ident[f,],y,method = "spearman")))
if (is.na(cor(ident[f,],y,method = "spearman"))){
print(f)
}
}
co <- co[order(co[,2],decreasing =TRUE),]
print(which(co[,1]==feature))
co <- co[which(co[,1]!=feature),]
co <- co[which(co[,2]>0),]
return(co)
}
# find all features mostly associated with top DE features based on the expression of two types of cells.
# input:
# rna.markers: the result of DE genes analysis between two cell types from FindMarkers function
# rna_ident: an RNA expression matrix of two types of cells after discarding zero expression genes.
# adt.markers: the result of DE proteins analysis between two cell types from FindMarkers function
# adt_ident: an ADT abundance matrix of two types of cells after discarding zero abundance proteins.
# output:
# allcofeatures: features mostly associated with the given features
Findallcofeatures <- function(rna.markers,rna_ident,adt.markers,adt_ident){
allcofeatures <- list()
options(scipen = 100)
corna1 <- list()
corna2 <- list()
coadt1 <- list()
coadt2 <- list()
for (i in 1:10){
feature <- rownames(rna.markers)[i]
print(feature)
co <- Findcofeatures(feature,rna_ident)
co <- co[which(co[,1] %in% rownames(rna.markers)[1:10]==FALSE),]
if (rna.markers[feature,"avg_log2FC"]>0){
corna1[[feature]] <- co[1:5,1]
}else{
corna2[[feature]] <- co[1:5,1]
}
}
for (i in 1:3){
feature <- rownames(adt.markers)[i]
print(feature)
co <- Findcofeatures(feature,adt_ident)
co <- co[which(co[,1] %in% rownames(adt.markers)[1:3]==FALSE),]
if (adt.markers[feature,"avg_log2FC"]>0){
coadt1[[feature]] <- co[1:5,1]
}else{
coadt2[[feature]] <- co[1:5,1]
}
}
allcofeatures[['corna1']] <- corna1
allcofeatures[['corna2']] <- corna2
allcofeatures[['coadt1']] <- coadt1
allcofeatures[['coadt2']] <- coadt2
return(allcofeatures)
}
# calculate subset of obj under two cell types, the DE features and top associated features
# input:
# obj: a seurat object obtained from HGT_cluster fuction
# ident.1, ident.2: two cell types
# output:
# obj_co: a list of the subset of obj and selected features
subobjco <- function(obj, ident.1, ident.2){
obj_co <- list()
# DEG analysis between ident.1 and ident.2
DefaultAssay(obj) <- 'RNA'
rna.markers <- FindMarkers(obj, ident.1 = ident.1 ,ident.2 = ident.2)
DefaultAssay(obj) <- 'ADT'
adt.markers <- FindMarkers(obj, ident.1 = ident.1,ident.2 = ident.2)
I0 <- colnames(obj)[which(obj@meta.data$cell_type %in% c(ident.1,ident.2))]
obj0 <- subobject(obj,I0)
# expression data for rnas and adts of two cell types
rna_ident <- obj0@assays$RNA@data
adt_ident <- obj0@assays$ADT@data
rna_ident <- rna_ident[which(rowSums(rna_ident)!=0),]
adt_ident <- adt_ident[which(rowSums(adt_ident)!=0),]
cofeatures <- Findallcofeatures(rna.markers = rna.markers, rna_ident = rna_ident, adt.markers = adt.markers, adt_ident = adt_ident)
corna1 <- cofeatures[['corna1']]
corna2 <- cofeatures[['corna2']]
coadt1 <- cofeatures[['coadt1']]
coadt2 <- cofeatures[['coadt2']]
obj_co[['cofeatures']] <- cofeatures
obj_co[['obj']] <- obj0
obj_co[['I']] <- I0
obj_co[['rna.markers']] <- rna.markers
obj_co[['adt.markers']] <- adt.markers
return(obj_co)
}
# draw heatmap on correlation matrix between selected features
# required packages: RColorBrewer
# input:
# obj_co: a list obtained from subbmco function
# output:
# heatmap: heatmap on correlation matrix between selected features
#library(RColorBrewer)
DEfeaturesheatmap <- function(obj_co){
obj0 <- obj_co$obj
corna1 <- obj_co$cofeatures$corna1
corna2 <- obj_co$cofeatures$corna2
coadt1 <- obj_co$cofeatures$coadt1
coadt2 <- obj_co$cofeatures$coadt2
corna <- c(corna1,corna2)
coadt <- c(coadt1,coadt2)
# mm - correlations matrix as input of Heatmap
m <-rbind(obj0@assays$RNA@scale.data[unique(c(unlist(corna), names(corna))),], obj0@assays$ADT@scale.data[unique(c(unlist(coadt), names(coadt))),])
rownames(m) <- c(unique(c(unlist(corna), names(corna))),unique(c(unlist(coadt), names(coadt))))
mm <- cor(t(m))
# lab - the coressponding cel types of features
lab <- list()
for (f in rownames(m)){
if (f %in% unique(c(unlist(corna1), names(corna1),unlist(coadt1), names(coadt1)))){
lab[[f]] <- color1
}else{
lab[[f]] <- color2
}
}
p <- heatmap(mm,RowSideColors=unlist(lab),col=colorRampPalette(c(rep("royalblue",1),'white','#F6DBDB',rep("firebrick3",2)))(56))
return(p)
}
# draw a line representing a given feature
# input:
# f: a given feature
# dff: a dataframe containing expression of selected features and a dimension values of HGT embedding.
# color : color of a line
# output:
# draw a line representing a given feature
line <- function(f,dff,color){
df <- data.frame(cbind(dff[,f],dff$index))
model1=loess(X1 ~ X2,data=df,span=1 )
model <- stats::predict(model1)
lines(model, x=df$X2, col=color)
}
# draw the lines by a loess smoothing function based on the corresponding embedding and scaled gene expressions in cells
# input:
# obj: a seurat object obtained from HGT_cluster fuction
# obj_co: a list obtained from subbmco function
# HGT.dim: a dimension of HGT embedding
# color1, color2: two colors coressponding to two cell types
# output:
# a plot show the relationship between HGT embedding and feature expression
DEfeaturesloess <- function(obj, obj_co, HGT.dim = n, color1, color2){
obj0 <- obj_co$obj
I0 <- obj_co$I
corna1 <- obj_co$cofeatures$corna1
corna2 <- obj_co$cofeatures$corna2
coadt1 <- obj_co$cofeatures$coadt1
coadt2 <- obj_co$cofeatures$coadt2
corna <- c(corna1,corna2)
coadt <- c(coadt1,coadt2)
m <-obj0@assays$RNA@scale.data[unique(c(unlist(corna), names(corna))),]
df0 <- data.frame(t(m))
colnames(df0) <- rownames(m)
df0$index <-unlist(obj[[paste0('HGT_', HGT.dim)]][I0,])
df0 <- df0[order(df0$index),]
m <-obj0@assays$ADT@scale.data[unique(c(unlist(coadt), names(coadt))),]
df1 <- data.frame(t(m))
colnames(df1) <- rownames(m)
df1$index <-unlist(obj[[paste0('HGT_', HGT.dim)]][I0,])
df1 <- df1[order(df1$index),]
rna.markers <- obj_co$rna.markers
adt.markers <- obj_co$adt.markers
par(pin = c(3,3))
y=seq(-2,2,0.1)
x=seq(min(df0$index),max(df0$index),length.out=length(y))
#x=seq(-0.1,0.05,length.out=length(y))
p <- plot(x,y,col="white",xlab = paste0('HGT_', HGT.dim), ylab="scaled experssion",type="l",main="Loess Smoothing and Prediction")
for (j in 1:10){
feature <- rownames(rna.markers)[j]
if (rna.markers[feature,'avg_log2FC']>0){
color <- color1
}else{
color <- color2
}
line (feature,df0,color)
for (cof in unlist(corna[[feature]])[1:4]){
line (cof,df0,color)
}
}
for (j in 1:3){
feature <- rownames(adt.markers)[j]
if (adt.markers[feature,'avg_log2FC']>0){
color <- color1
}else{
color <- color2
}
line (feature,df1,color)
for (cof in unlist(coadt[[feature]])[1:4]){
line (cof,df1,color)
}
}
return(p)
}
# required package -- reticulate
# input:
# GAS: the spliced and normalized matrix obtained from CLR function
# result_dir: The address for storing the models and optimization results(Type:str)
# epoch:(Type:int)
# lr: learning rate(Type:float)
# n_hid: Number of hidden dimension(Type:int)
# n_heads: Number of attention head(Type:int)
# cuda: 0 use GPU0 else cpu(Type:int)
# data_type: 'CITE', 'scRNA_scATAC', or 'multipleRNA'
# envPath: The address for environment to use if use.env is TRUE(Type:str)
# output:
# HGT_result: a list containing requried results of HGT model as follows:
# parameters: given parameters from user --epoch, lr, n_hid, n_heads, cuda
# cell_hgt_matrix: cell embedding matrix
# feature_hgt_matrix : gene embedding matrix and protein embedding matrix when data_type is 'CITE';
# attention: attention meassage for features and cells
# data_type: 'CITE', 'scRNA_scATAC', or 'multipleRNA'
# result_dir: The address for storing the models and optimization results
# GAS: the spliced and normalized matrix obtained from CLR function
run_HGT <- function(GAS,result_dir,data_type,envPath=NULL,lr=NULL, epoch=NULL, n_hid=NULL, n_heads=NULL,cuda=0){
if (data_type == 'CITE') {
if (is.null(lr)){lr = 0.1}
if (is.null(epoch)){epoch = 50}
if (is.null(n_hid)){n_hid = 104}
if (is.null(n_heads)){n_heads = 13}
}
if (data_type == 'scRNA_scATAC') {
if (is.null(lr)){lr = 0.1}
if (is.null(epoch)){epoch = 100}
if (is.null(n_hid)){n_hid = 128}
if (is.null(n_heads)){n_heads = 16}
}
if (data_type == 'multipleRNA') {
if (is.null(lr)){lr = 0.1}
if (is.null(epoch)){epoch = 100}
if (is.null(n_hid)){n_hid = 104}
if (is.null(n_heads)){n_heads = 13}
}
print(epoch)
if (!is.null(envPath)){use_condaenv(envPath)}
list_in <- assign("list_in", list(lr=lr, epoch=epoch, n_hid=n_hid, n_heads=n_heads, result_dir=result_dir, cuda=cuda, data_type=data_type, cell_gene=GAS, gene_name=rownames(GAS), cell_name=colnames(GAS)), envir = .GlobalEnv)
source_python('./arg.py')
cell_hgt_matrix <- py$cell_matrix
gene_hgt_matrix <- py$gene_matrix
attention <- py$df2
rownames(cell_hgt_matrix) <- list_in$cell_name
rownames(gene_hgt_matrix) <- list_in$gene_name
HGT_result <- list()
HGT_result[['parameters']] <- data.frame(lr,epoch,n_hid,n_heads,cuda)
HGT_result[['GAS']] <- GAS
HGT_result[['cell_hgt_matrix']] <- cell_hgt_matrix
HGT_result[['feature_hgt_matrix']] <- gene_hgt_matrix
HGT_result[['attention']] <- attention
HGT_result[['result_dir']] <- result_dir
HGT_result[['data_type']] <- data_type
return(HGT_result)
}