sc-TissueMapper_functions.R 42.9 KB
Newer Older
1
2
3
4
5
6
7
#sc-TissueMapper
#Author: Gervaise H. Henry
#Email: gervaise.henry@utsouthwestern.edu
#Lab: Strand Lab, Deparment of Urology, University of Texas Southwestern Medical Center


scFolders <- function(){
Gervaise Henry's avatar
Gervaise Henry committed
8
9
  #Create analysis output folders
  
10
11
12
  if (!dir.exists("./analysis/qc/")){
    dir.create("./analysis/qc/")
  }
13
14
15
16
17
18
  if (!dir.exists("./analysis/qc/")){
    dir.create("./analysis/qc/")
  }
  if (!dir.exists("./analysis/qc/cutoffs/")){
    dir.create("./analysis/qc/cutoffs/")
  }
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  if (!dir.exists("./analysis/qc/cellcycle")){
    dir.create("./analysis/qc/cellcycle")
  }
  if (!dir.exists("./analysis/vis")){
    dir.create("./analysis/vis")
  }
  if (!dir.exists("./analysis/score_id")){
    dir.create("./analysis/score_id")
  }
  if (!dir.exists("./analysis/cor")){
    dir.create("./analysis/cor")
  }
}


Gervaise Henry's avatar
Gervaise Henry committed
34
scLoad <- function(p,cellranger=3,aggr=TRUE,ncell=0,nfeat=0,seurat=FALSE){
35
36
37
38
  #Load and prefilter filtered_gene_bc_matrices_mex output from cellranger
  
  #Inputs:
  #p = project name
Gervaise Henry's avatar
Gervaise Henry committed
39
40
41
42
43
  #cellranger = cellranger version number used for count/aggr, 2, 3, or 4
  #aggr = if the samples are already aggregated, TRUE if using the output of aggr, FALSE if using outputs of each count
  #ncell = minimum number of cells for initial feature filter
  #nfeat = minimum number of features for initial cell filter
  #seurat = if Seurat R objects per sample are already created, TRUE if using Seurat objects, FALSE if using output of count
44
45
  
  #Outputs:
46
47
  #sc10x = Seurat object list
  #sc10x.groups = group labels for each sample
48
49
  
  
50
51
52
53
  sc10x.groups <- read_csv(paste0("./analysis/DATA/",p,"-demultiplex.csv"))
  
  #Load filtered_gene_bc_matrices output from cellranger
  sc10x <- list()
Gervaise Henry's avatar
Gervaise Henry committed
54
55
56
57
58
59
60
61
62
  if (seurat==FALSE) {
    sc10x.data <- list()
    if (aggr==TRUE){
      if (cellranger==2){
        sc10x.data[aggr] <- Read10X(data.dir=paste0("./analysis/DATA/10x/filtered_gene_bc_matrices_mex/"))
      } else {
        sc10x.data[aggr] <- Read10X(data.dir=paste0("./analysis/DATA/10x/filtered_feature_bc_matrix/"))
      }
      sc10x[aggr] <- new("seurat",raw.data=sc10x.data[aggr])
63
    } else {
Gervaise Henry's avatar
Gervaise Henry committed
64
65
66
67
68
69
70
71
72
      for (i in sc10x.groups$Samples){
        if (cellranger==2){
          sc10x.data[i] <- Read10X(data.dir=paste0("./analysis/DATA/10x/",i,"/filtered_gene_bc_matrices/"))
        } else {
          sc10x.data[i] <- Read10X(data.dir=paste0("./analysis/DATA/10x/",i,"/filtered_feature_bc_matrix/"))
        }
        sc10x[i] <- CreateSeuratObject(counts=sc10x.data[[i]],project=p,min.cells=ncell,min.features=nfeat)
        sc10x[[i]]$samples <- i
      }
73
    }
74
  } else {
75
    for (i in sc10x.groups$Samples){
Gervaise Henry's avatar
Gervaise Henry committed
76
77
      sc10x[i] <- readRDS(paste0("./analysis/DATA/10x/",i,"/",i,".rds"))
      sc10x[[i]] <- sc10x[[i]]
78
      sc10x[[i]]$samples <- i
Gervaise Henry's avatar
Gervaise Henry committed
79
80
81
82
83
84
85
86
87
88
    }
  }
  
  if (length(colnames(sc10x.groups)[!(colnames(sc10x.groups) %in% c("Samples","ALL","Keep"))])!=0){
    for (i in sc10x.groups$Samples){
      Idents(sc10x[[i]],cells=1:ncol(sc10x[[i]])) <- i
      sc10x[[i]]$samples <- Idents(sc10x[[i]])
      for (j in colnames(sc10x.groups)[!(colnames(sc10x.groups) %in% c("Samples","ALL"))]){
        Idents(sc10x[[i]],cells=1:ncol(sc10x[[i]])) <- sc10x.groups[sc10x.groups$Samples==i,colnames(sc10x.groups)==j]
        sc10x[[i]]@meta.data <- sc10x[[i]]@meta.data[,c("nCount_RNA","nFeature_RNA","samples")]
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
  # #Label sample names from aggregation_csv.csv
  # if (sub==FALSE){
  #   if (cellranger==2){
  #     sc10x.aggr <- read_csv("./analysis/DATA/10x/aggregation_csv.csv")
  #   } else {
  #     sc10x.aggr <- read_csv("./analysis/DATA/10x/aggregation.csv")
  #   }
  # } else {
  #   if (cellranger==2){
  #     sc10x.aggr <- read_csv(paste0("./analysis/DATA/",p,"/10x/aggregation_csv.csv"))
  #   } else {
  #     sc10x.aggr <- read_csv(paste0("./analysis/DATA/",p,"/10x/aggregation.csv"))
  #   }
  # }
  # cell.codes <- as.data.frame(sc10x@raw.data@Dimnames[[2]])
  # colnames(cell.codes) <- "barcodes"
  # rownames(cell.codes) <- cell.codes$barcodes
  # cell.codes$lib.codes <- as.factor(gsub(pattern=".+-",replacement="",cell.codes$barcodes))
  # cell.codes$samples <- sc10x.aggr$library_id[match(cell.codes$lib.codes,as.numeric(rownames(sc10x.aggr)))]
  # sc10x <- CreateSeuratObject(counts=sc10x.data,project=p,assay="RNA",min.cells=mc,min.features=mg,meta.data=cell.codes["samples"])
  # 
  # #Create groups found in demultiplex.csv
  # for (i in 2:ncol(sc10x.demultiplex)){
  #   Idents(sc10x) <- "samples"
  #   merge.cluster <- apply(sc10x.demultiplex[,i],1,as.character)
  #   merge.cluster[merge.cluster==1] <- colnames(sc10x.demultiplex[,i])
  #   
  #   Idents(sc10x) <- plyr::mapvalues(x=Idents(sc10x),from=sc10x.demultiplex$Samples,to=merge.cluster)
  #   sc10x@meta.data[,colnames(sc10x.demultiplex[,i])] <- Idents(sc10x)
  # }
  
  
  results <- list(
    sc10x=sc10x,
    sc10x.groups=sc10x.groups
  )
  return(results)
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
}


scSubset <- function(sc10x,i="ALL",g="ALL"){
  #Subset cells based on an identity
  
  #Inputs:
  #sc10x = seruat object
  #i = identity to use
  #g = group to subset by
  
  #Outputs:
  #Seurat object
  
  
145
  Idents(sc10x) <- i
146
147
  sc10x.sub <- subset(x=sc10x,idents=g)
  
148
  
149
150
151
152
  return(sc10x.sub)
}


153
scQC <- function(sc10x,sp="hu",feature="nFeature_RNA"){
154
155
156
157
158
159
160
  #QC and filter Seurat object
  
  #Inputs:
  #sc10x = Seruat object
  #sub = Subfolder to save output files
  
  #Outputs:
161
  #result[1] = filtered Seurat object
162
163
164
165
166
167
  #result[2] = raw cell count
  #result[3] = raw gene count
  #result[4] = filtered cell count
  #result[5] = filtered gene count
  
  
168
  #Calculate percent mitochondrea
169
  if (sp=="hu"){
170
    mito.pattern <- "^MT-"
171
    ribo.pattern <- "^(RPL|RPS)"
172
173
  } else if (sp=="mu"){
    mito.pattern <- "^mt-"
174
    ribo.pattern <- "^(Rpl|Rps)"
175
  }
176
177
178
  for (i in names(sc10x)){
    sc10x.temp <- sc10x[[i]]
    sc10x.temp[["percent.mito"]] <- PercentageFeatureSet(object=sc10x.temp,pattern=mito.pattern)
179
    sc10x.temp[["percent.ribo"]] <- PercentageFeatureSet(object=sc10x.temp,pattern=ribo.pattern)
180
    #sc10x.temp <- subset(sc10x.temp,cell=names(which(is.na(sc10x.temp$percent.mito))),invert=TRUE)
181
    sc10x[i] <- sc10x.temp
182
  }
183
  
184
185
  #Calculate cutoffs
  thresh <- list()
186
  for (i in feature){
Gervaise Henry's avatar
Gervaise Henry committed
187
    if (i == "nFeature_RNA"){
188
189
      sc10x.temp <- list()
      for (j in names(sc10x)){
Gervaise Henry's avatar
Gervaise Henry committed
190
191
192
193
194
195
196
        h <- NULL
        cutoff.temp <- NULL
        cells.remove <- NULL
        h <- hist(data.frame(sc10x[[j]][[i]])$nFeature_RNA,breaks=10,plot=FALSE)
        cutoff.temp <- mean(c(h$mids[which.max(h$counts)],h$mids[-which.max(h$counts)][which.max(h$counts[-which.max(h$counts)])]))
        cells.remove <- c(cells.remove,rownames(sc10x[[j]][["nFeature_RNA"]])[sc10x[[j]][[i]][,1] < cutoff.temp])
        sc10x.temp[[j]] <- subset(sc10x[[j]],cells=cells.remove,invert=TRUE)
197
      }
Gervaise Henry's avatar
Gervaise Henry committed
198
      thresh[[i]] <- scThresh(sc10x.temp,feature=i,sub="higher")
Gervaise Henry's avatar
Gervaise Henry committed
199
    }
200
    if (i == "percent.mito"){
Gervaise Henry's avatar
Gervaise Henry committed
201
      sc10x.temp <- list()
202
      for (j in names(sc10x)){
Gervaise Henry's avatar
Gervaise Henry committed
203
204
205
206
207
208
209
        h <- NULL
        cutoff.temp <- NULL
        cells.remove <- NULL
        h <- hist(data.frame(sc10x[[j]][[i]])$percent.mito,breaks=100,plot=FALSE)
        cutoff.temp <- mean(c(h$mids[which.max(h$counts)],h$mids[-which.max(h$counts)][which.max(h$counts[-which.max(h$counts)])]))
        cells.remove <- c(cells.remove,rownames(sc10x[[j]][["percent.mito"]])[sc10x[[j]][[i]][,1] < cutoff.temp])
        sc10x.temp[[j]] <- subset(sc10x[[j]],cells=cells.remove,invert=TRUE)
210
      }
Gervaise Henry's avatar
Gervaise Henry committed
211
      thresh[[i]] <- scThresh(sc10x.temp,feature=i,sub="higher")
212
    }
213
    if (i == "percent.ribo"){
Gervaise Henry's avatar
Gervaise Henry committed
214
      thresh[[i]] <- scThresh(sc10x,feature=i,sub="all")
215
216
    }
    if (i == "nCount_RNA"){
217
218
      sc10x.temp <- list()
      for (j in names(sc10x)){
Gervaise Henry's avatar
Gervaise Henry committed
219
220
221
222
223
        h <- NULL
        cutoff.temp <- NULL
        cells.remove <- NULL
        h <- hist(data.frame(sc10x[[j]][[i]])$nCount_RNA,breaks=100,plot=FALSE)
        cutoff.temp <- mean(c(h$mids[which.max(h$counts)],h$mids[-which.max(h$counts)][which.max(h$counts[-which.max(h$counts)])]))
Gervaise Henry's avatar
Gervaise Henry committed
224
        cells.remove <- c(cells.remove,rownames(sc10x[[j]][["nCount_RNA"]])[sc10x[[j]][[i]][,1] > cutoff.temp])
Gervaise Henry's avatar
Gervaise Henry committed
225
        sc10x.temp[[j]] <- subset(sc10x[[j]],cells=cells.remove,invert=TRUE)
226
      }
Gervaise Henry's avatar
Gervaise Henry committed
227
      thresh[[i]] <- scThresh(sc10x.temp,feature=i,sub="lower")
228
    }
229
230
231
232
233
234
  }
  
  #Plot raw stats
  max.ct <- 0
  max.ft <- 0
  max.mt <- 0
235
  max.rb <- 0
236
237
238
239
240
241
242
243
244
245
  for (i in names(sc10x)){
    if (max.ct < max(sc10x[[i]][["nCount_RNA"]])){
      max.ct <- max(sc10x[[i]][["nCount_RNA"]])
    }
    if (max.ft < max(sc10x[[i]][["nFeature_RNA"]])){
      max.ft <- max(sc10x[[i]][["nFeature_RNA"]])
    }
    if (max.mt < max(sc10x[[i]][["percent.mito"]])){
      max.mt <- max(sc10x[[i]][["percent.mito"]])
    }
246
247
248
    if (max.rb < max(sc10x[[i]][["percent.ribo"]])){
      max.rb <- max(sc10x[[i]][["percent.ribo"]])
    }
249
250
251
252
  }
  max.ct <- max.ct*1.1
  max.ft <- max.ft*1.1
  max.mt <- max.mt*1.1
253
  max.rb <- max.rb*1.1
254
  cells.remove <- list()
255
  for (i in feature){
256
257
258
259
260
261
262
    max.y <- 0
    if (i == "nCount_RNA"){
      max.y <- max.ct
    } else if (i == "nFeature_RNA"){
      max.y <- max.ft
    } else if (i == "percent.mito"){
      max.y <- max.mt
263
264
    } else if (i == "percent.ribo"){
      max.y <- max.rb
265
266
267
268
    }
    plots.v <- list()
    densities.s <- list()
    plots.s <- list()
Gervaise Henry's avatar
Gervaise Henry committed
269
    sc10x.temp <- NULL
270
271
272
    for (j in names(sc10x)){
      sc10x.temp <- sc10x[[j]]
      plots.v[[j]] <- VlnPlot(object=sc10x.temp,features=i,pt.size=0.1,)+scale_x_discrete(labels=j)+scale_y_continuous(limits=c(0,max.y))+theme(legend.position="none",axis.text.x=element_text(hjust=0.5,angle=0))
273
      if (i %in% c("nFeature_RNA","percent.mito","percent.ribo","nCount_RNA")){
274
275
        if (i == "nFeature_RNA"){
          cutoff.h <- thresh[[i]][[j]]$threshold[thresh[[i]][[j]]$method=="RenyiEntropy"]
Gervaise Henry's avatar
Gervaise Henry committed
276
          cutoff.l <- 200
277
        } else if (i == "percent.mito") {
Gervaise Henry's avatar
Gervaise Henry committed
278
          cutoff.h <- thresh[[i]][[j]]$threshold[thresh[[i]][[j]]$method=="Triangle"]
Gervaise Henry's avatar
Gervaise Henry committed
279
          cutoff.l <- 0
280
281
282
283
284
        } else if (i == "percent.ribo") {
          cutoff.h <- thresh[[i]][[j]]$threshold[thresh[[i]][[j]]$method=="RenyiEntropy"]
          cutoff.l <- 0
        } else if (i == "nCount_RNA") {
          cutoff.h <- max(sc10x[[j]][[i]])
Gervaise Henry's avatar
Gervaise Henry committed
285
          cutoff.l <- thresh[[i]][[j]]$threshold[thresh[[i]][[j]]$method=="RenyiEntropy"]
286
287
        }
        plots.v[[j]] <- plots.v[[j]]+geom_hline(yintercept=cutoff.l,size=0.5,color="red")+geom_hline(yintercept=cutoff.h,size=0.5,color="red")
288
289
290
291
292
293
294
        if (i != "nCount_RNA"){
          densities.s[[j]] <- density(sc10x.temp$nCount_RNA,sc10x.temp[[i]][,1],n=1000)
          plots.s[[j]] <- ggplotGrob(ggplot(data.frame(cbind(sc10x.temp$nCount_RNA,sc10x.temp[[i]][,1])))+geom_point(aes(x=X1,y=X2,color=densities.s[[j]]),size=0.1)+scale_x_continuous(limits=c(0,max.ct))+scale_y_continuous(limits=c(0,max.y))+scale_color_viridis(option="inferno")+labs(x="nCount_RNA",y=i,color="Density")+ggtitle(j)+cowplot::theme_cowplot()+theme(plot.title=element_text(size=7.5),axis.title=element_text(size=7.5),axis.text=element_text(size=5,angle=45),legend.position="bottom",legend.title=element_text(size=5,face="bold",vjust=1),legend.text=element_text(size=5,angle=45))+guides(color=guide_colourbar(barwidth=5,barheight=0.5))+geom_hline(yintercept=cutoff.l,size=0.1,color="red")+geom_hline(yintercept=cutoff.h,size=0.1,color="red"))
        } else {
          densities.s[[j]] <- density(sc10x.temp$nFeature_RNA,sc10x.temp[[i]][,1],n=1000)
          plots.s[[j]] <- ggplotGrob(ggplot(data.frame(cbind(sc10x.temp$nFeature_RNA,sc10x.temp[[i]][,1])))+geom_point(aes(x=X1,y=X2,color=densities.s[[j]]),size=0.1)+scale_x_continuous(limits=c(0,max.ct))+scale_y_continuous(limits=c(0,max.y))+scale_color_viridis(option="inferno")+labs(x="nFeature_RNA",y=i,color="Density")+ggtitle(j)+cowplot::theme_cowplot()+theme(plot.title=element_text(size=7.5),axis.title=element_text(size=7.5),axis.text=element_text(size=5,angle=45),legend.position="bottom",legend.title=element_text(size=5,face="bold",vjust=1),legend.text=element_text(size=5,angle=45))+guides(color=guide_colourbar(barwidth=5,barheight=0.5))+geom_hline(yintercept=cutoff.l,size=0.1,color="red")+geom_hline(yintercept=cutoff.h,size=0.1,color="red"))
        }
295
        cells.remove[[j]] <- c(cells.remove[[j]],rownames(sc10x[[j]][[i]])[sc10x[[j]][[i]][,1] < cutoff.l | sc10x[[j]][[i]][,1] > cutoff.h])
296
      }
Gervaise Henry's avatar
Gervaise Henry committed
297
      ggsave(paste0("./analysis/qc/Violin_qc.raw.",i,".",j,".eps"),plot=plots.v[[j]])
298
      if (i %in% c("nFeature_RNA","percent.mito","percent.ribo","nCount_RNA")){
Gervaise Henry's avatar
Gervaise Henry committed
299
300
        ggsave(paste0("./analysis/qc/Plot_qc.raw.",i,".",j,".eps"),plot=plots.s[[j]])
      }
301
    }
302
  }
303
  
304
305
306
307
308
309
310
311
312
  #Record cell/gene counts pre and post filtering
  counts.cell.raw <- list()
  counts.gene.raw <- list()
  sc10x.sub <- list()
  counts.cell.filtered <- list()
  counts.gene.filtered <- list()
  for (i in names(sc10x)){
    counts.cell.raw[i] <- ncol(GetAssayData(object=sc10x[[i]],slot="counts"))
    counts.gene.raw[i] <- nrow(GetAssayData(object=sc10x[[i]],slot="counts"))
313
    sc10x.sub[[i]] <- subset(sc10x[[i]],cells=setdiff(colnames(sc10x[[i]]),cells.remove[[i]]))
314
315
316
    counts.cell.filtered[i] <- ncol(GetAssayData(object=sc10x.sub[[i]],slot="counts"))
    counts.gene.filtered[i] <- nrow(GetAssayData(object=sc10x.sub[[i]],slot="counts"))
  }
317
318
  
  #Plot filtered stats
319
320
321
  max.ct <- 0
  max.ft <- 0
  max.mt <- 0
322
  max.rb <- 0
323
324
325
326
327
328
329
330
331
332
  for (i in names(sc10x)){
    if (max.ct < max(sc10x.sub[[i]][["nCount_RNA"]])){
      max.ct <- max(sc10x.sub[[i]][["nCount_RNA"]])
    }
    if (max.ft < max(sc10x.sub[[i]][["nFeature_RNA"]])){
      max.ft <- max(sc10x.sub[[i]][["nFeature_RNA"]])
    }
    if (max.mt < max(sc10x.sub[[i]][["percent.mito"]])){
      max.mt <- max(sc10x.sub[[i]][["percent.mito"]])
    }
333
334
335
    if (max.rb < max(sc10x.sub[[i]][["percent.ribo"]])){
      max.rb <- max(sc10x.sub[[i]][["percent.ribo"]])
    }
336
  }
337
338
339
  max.ct <- max.ct*1.1
  max.ft <- max.ft*1.1
  max.mt <- max.mt*1.1
340
341
  max.rb <- max.rb*1.1
  for (i in feature){
342
343
344
345
346
347
348
    max.y <- 0
    if (i == "nCount_RNA"){
      max.y <- max.ct
    } else if (i == "nFeature_RNA"){
      max.y <- max.ft
    } else if (i == "percent.mito"){
      max.y <- max.mt
349
350
    } else if (i == "percent.ribo"){
      max.y <- max.rb
351
352
353
354
355
356
357
    }
    plots.v <- list()
    densities.s <- list()
    plots.s <- list()
    for (j in names(sc10x.sub)){
      sc10x.temp <- sc10x.sub[[j]]
      plots.v[[j]] <- VlnPlot(object=sc10x.temp,features=i,pt.size=0.1,)+scale_x_discrete(labels=j)+scale_y_continuous(limits=c(0,max.y))+theme(legend.position="none",axis.text.x=element_text(hjust=0.5,angle=0))
358
      if (i != "nCount_RNA"){
359
        densities.s[[j]] <- density(sc10x.temp$nCount_RNA,sc10x.temp[[i]][,1],n=1000)
360
361
362
363
        plots.s[[j]] <- ggplotGrob(ggplot(data.frame(cbind(sc10x.temp$nCount_RNA,sc10x.temp[[i]][,1])))+geom_point(aes(x=X1,y=X2,color=densities.s[[j]]),size=0.1)+scale_x_continuous(limits=c(0,max.ct))+scale_y_continuous(limits=c(0,max.y))+scale_color_viridis(option="inferno")+labs(x="nCount_RNA",y=i,color="Density")+ggtitle(j)+cowplot::theme_cowplot()+theme(plot.title=element_text(size=7.5),axis.title=element_text(size=7.5),axis.text=element_text(size=5,angle=45),legend.position="bottom",legend.title=element_text(size=5,face="bold",vjust=1),legend.text=element_text(size=5,angle=45))+guides(color=guide_colourbar(barwidth=5,barheight=0.5)))
      } else {
        densities.s[[j]] <- density(sc10x.temp$nFeature_RNA,sc10x.temp[[i]][,1],n=1000)
        plots.s[[j]] <- ggplotGrob(ggplot(data.frame(cbind(sc10x.temp$nFeature_RNA,sc10x.temp[[i]][,1])))+geom_point(aes(x=X1,y=X2,color=densities.s[[j]]),size=0.1)+scale_x_continuous(limits=c(0,max.ct))+scale_y_continuous(limits=c(0,max.y))+scale_color_viridis(option="inferno")+labs(x="nFeature_RNA",y=i,color="Density")+ggtitle(j)+cowplot::theme_cowplot()+theme(plot.title=element_text(size=7.5),axis.title=element_text(size=7.5),axis.text=element_text(size=5,angle=45),legend.position="bottom",legend.title=element_text(size=5,face="bold",vjust=1),legend.text=element_text(size=5,angle=45))+guides(color=guide_colourbar(barwidth=5,barheight=0.5)))
364
      }
365
      ggsave(paste0("./analysis/qc/Violin_qc.filtered.",i,".",j,".eps"),plot=plots.v[[j]])
366
      if (i %in% c("nFeature_RNA","percent.mito","percent.ribo","nCount_RNA")){
367
368
369
        ggsave(paste0("./analysis/qc/Plot_qc.filtered.",i,".",j,".eps"),plot=plots.s[[j]])
      }

370
371
372
    }
  }
  
373
374
375
376
377
378
379
380
381
382
383
  
  results <- list(
    sc10x=sc10x.sub,
    counts.cell.raw=counts.cell.raw,
    counts.gene.raw=counts.gene.raw,
    counts.cell.filtered=counts.cell.filtered,
    counts.gene.filtered=counts.gene.filtered
  )
  return(results)
}

384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
scThresh <- function(sc10x,feature,sub=FALSE){
  #Calculate thresholds and cutoffs
  
  #Inputs:
  #sc10x = Seruat object
  #feature = feature to threshold
  #sub = Subfolder to save output files
  
  #Outputs:
  #result = Threshold data
  
  
  #Make folders
  if (sub==FALSE){
    folder <- "./analysis/qc/cutoffs/"
  } else {
    folder <- paste0("./analysis/qc/cutoffs/",sub,"/")
    if (!dir.exists(folder)){
      dir.create(folder)
    }
404
405
  }
  
406
407
408
409
410
411
412
  #Calculate range of histogram based threholding and manually select methods for cutoffs
  scale <- list()
  scale.scaled <- list()
  h <- list()
  thresh <-list()
  cutoff.l <- list()
  cutoff.h <- list()
413
  thresh_methods <- c("IJDefault","Huang","Huang2","IsoData","Li","Mean","MinErrorI","Moments","Otsu","Percentile","RenyiEntropy","Shanbhag","Triangle")#,"Intermodes"
414
415
416
417
  for (i in names(sc10x)){
    scale[[i]] <- data.frame(Score=sc10x[[i]][[feature]])
    colnames(scale[[i]]) <- "Score"
    scale[[i]] <- data.frame(Score=scale[[i]]$Score[!is.na(scale[[i]]$Score)])
Gervaise Henry's avatar
Gervaise Henry committed
418
419
420
    scale.scaled[[i]] <- as.integer((scale[[i]]$Score-min(scale[[i]]$Score))/(max(scale[[i]]$Score)-min(scale[[i]]$Score))*360)
    #scale.scaled[[i]] <- as.integer(scales::rescale(scale[[i]]$Score,to=c(0,1))*360)
    h[[i]] <- hist(scale[[i]]$Score,breaks=100,plot=FALSE)
Gervaise Henry's avatar
Gervaise Henry committed
421
    thresh[[i]] <- purrr::map_chr(thresh_methods,~auto_thresh(scale.scaled[[i]],.)) %>% tibble(method = thresh_methods, threshold = .)
422
    thresh[[i]]$threshold <- as.numeric(thresh[[i]]$threshold)
Gervaise Henry's avatar
Gervaise Henry committed
423
424
    thresh[[i]]$threshold <- ((thresh[[i]]$threshold/360)*(max(scale[[i]]$Score)-min(scale[[i]]$Score)))+min(scale[[i]]$Score)
    #thresh[[i]] <- thresh[[i]] %>% mutate(threshold=(scales::rescale(as.numeric(threshold)/360,to=range(scale[[i]]$Score))))
425
426
427
428
429
430
431
432
433
    postscript(paste0(folder,"Hist_qc.",i,".",feature,".eps"))
    plot(h[[i]],main=paste0("Histogram of ",feature," of sample ",i),xlab=feature)
    abline(v=thresh[[i]]$threshold)
    mtext(thresh[[i]]$method,side=1,line=2,at=thresh[[i]]$threshold,cex=0.5)
    dev.off()
  }
  
  
  return(thresh)
434
}
435

436
scCellCycle <- function(sc10x,sub=FALSE,sp="hu"){
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
  #Runs Seurat based PCA analysis for cell cycle ID
  
  #Inputs:
  #sc10x = Seruat object
  #sub = Subfolder to save output files
  
  #Outputs:
  #results[1] = Seurat object
  #results[2] = s genes
  #results[3] = g2m genes
  
  #Make sub-folders if necessary
  if (sub==FALSE){
    folder <- "./analysis/qc/cellcycle/"
  } else {
    folder <- paste0("./analysis/qc/cellcycle/",sub,"/")
    if (!dir.exists(folder)){
      dir.create(folder)
    }}
  
  #score cell cycle
  genes.cc <- readLines(con="./genesets/regev_lab_cell_cycle_genes.txt")
  genes.s <- genes.cc[1:43]
  genes.g2m <- genes.cc[44:97]
  sc10x <- NormalizeData(object=sc10x,verbose=FALSE)
  sc10x <- ScaleData(object=sc10x,do.par=TRUE,num.cores=45,verbose=FALSE)
  sc10x <- CellCycleScoring(object=sc10x,s.features=genes.s,g2m.features=genes.g2m,set.ident=TRUE)
  
  #plot cell cycle specific genes
466
467
468
469
470
471
472
  if (sp=="hu"){
    genes=c("PCNA","TOP2A","MCM6","MKI67")
    postscript(paste0(folder,"Violin_cc.Raw.eps"))
    plot <- VlnPlot(object=sc10x,features=genes,ncol=2,pt.size=1)
    plot(plot)
    dev.off()
  }
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  
  # sc10x <- RunPCA(object=sc10x,features=c(genes.s,genes.g2m),npcs=2,verbose=FALSE)
  # postscript(paste0(folder,"PCA_cc.Raw.eps"))
  # plot <- DimPlot(object=sc10x,reduction="pca")
  # plot(plot)
  # dev.off()
  # gc()
  # sc10x <- ScaleData(object=sc10x,vars.to.regress=c("S.Score","G2M.Score"),do.par=TRUE,num.cores=45,verbose=TRUE)
  # gc()
  # sc10x <- RunPCA(object=sc10x,features=c(genes.s,genes.g2m),npcs=2,verbose=FALSE)
  # postscript(paste0(folder,"PCA_cc.Norm.eps"))
  # plot <- DimPlot(object=sc10x,reduction="pca")
  # plot(plot)
  # dev.off()
  
  results <- list(
    sc10x=sc10x,
    genes.s=genes.s,
    genes.g2m=genes.g2m
  )
  return(results)
}


497
scPC <- function(sc10x,pc=50,hpc=0.9,file="pre.stress",print="tsne",assay="integrated"){
498
499
500
501
502
503
504
505
506
507
508
509
510
511
  #Scale Seurat object & calculate # of PCs to use
  
  #Inputs:
  #sc10x = Seruat object
  #pc = number of PCs to cacluate
  #hpc = max variance cutoff for PCs to use"
  #file = file for output
  
  #Outputs:
  #result[1] = Seurat object
  #result[2] = # of PCs to use
  
  #Run PCA
  Idents(object=sc10x) <- "ALL"
512
  sc10x <- RunPCA(object=sc10x,npcs=pc,verbose=FALSE,assay=assay)
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
  
  #Calculate PCs to use
  pc.use <- sc10x[["pca"]]@stdev^2
  pc.use <- pc.use/sum(pc.use)
  pc.use <- cumsum(pc.use)
  pc.use <- min(which(pc.use>=hpc))
  
  postscript(paste0("./analysis/qc/Plot_PCElbow_",file,".eps"))
  plot <- ElbowPlot(object=sc10x,ndims=pc)
  plot <- plot+geom_vline(xintercept=pc.use,size=1,color="red")
  plot(plot)
  dev.off()
  
  results <- list(
    sc10x=sc10x,
    pc.use=pc.use
  )
  return(results)
}


Gervaise Henry's avatar
Gervaise Henry committed
534
scAlign <-  function(sc10x.l){
535
  for (i in 1:length(sc10x.l)){
536
    #sc10x.l[[i]] <- NormalizeData(sc10x.l[[i]],verbose=FALSE)
537
    gc()
538
    #sc10x.l[[i]] <- ScaleData(sc10x.l[[i]],vars.to.regress=c("nFeature_RNA","percent.mito"),verbose = FALSE)
539
    sc10x.l[[i]] <- SCTransform(sc10x.l[[i]],vars.to.regress=c("nFeature_RNA","percent.mito","Stress1"),verbose=FALSE,assay="RNA")
540
    gc()
541
    #sc10x.l[[i]] <- FindVariableFeatures(sc10x.l[[i]],selection.method="vst",nfeatures=2000,verbose=FALSE)
542
543
  }
  
544
  sc10x.features <- SelectIntegrationFeatures(object.list=sc10x.l,nfeatures=5000)
545
  sc10x.l <- PrepSCTIntegration(object.list=sc10x.l,anchor.features=sc10x.features,verbose=FALSE)
546
547
548
549

  sc10x.l <- lapply(sc10x.l,FUN=function(x) { RunPCA(x,features=sc10x.features,verbose=FALSE) })
  
  sc10x.anchors <- FindIntegrationAnchors(object.list=sc10x.l,normalization.method="SCT",anchor.features=sc10x.features,verbose=FALSE,reduction="rpca",dims=1:30)
550
  sc10x <- IntegrateData(anchorset=sc10x.anchors,normalization.method="SCT",verbose=FALSE)
551
  
552
553
554
555
556
557
  #sc10x <- FindIntegrationAnchors(object.list=sc10x.l,dims=1:30,scale=FALSE)
  #sc10x <- IntegrateData(anchorset=sc10x,dims=1:30)
  
  #gc()
  #sc10x <- ScaleData(object=sc10x,do.par=TRUE,num.cores=45,verbose=FALSE,assay="integrated")
  #gc()
558
  
559
  gc()
560
  sc10x <- SCTransform(sc10x,vars.to.regress=c("nFeature_RNA","percent.mito","Stress1"),verbose=FALSE,return.only.var.genes=FALSE,assay="RNA")
561
  gc()
562
563
564
565
566
  
  return(sc10x)
}


567
scCluster <- function(sc10x,res=0.1,red="pca",dim,print="tsne",folder=FALSE,assay="integrated"){
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
  #Cluster Seurat object and produce visualizations
  
  #Inputs:
  #sc10x = Seruat object
  #res = resolution to calculate clustering
  #red = rediction type to use for clustering
  #dim = number of dimentions to use for display
  #print = dimentionality reduction to use for display
  #folder = folder for output
  
  #Outputs:
  #result = Seurat object
  
  #Create subfolder if necessary
  if (folder==FALSE){
    sub <- ""
Gervaise Henry's avatar
Gervaise Henry committed
584
  } else if (print != "0") {
585
586
587
588
589
590
    if (!dir.exists(paste0("./analysis/vis/",folder))){
      dir.create(paste0("./analysis/vis/",folder))
    }
    sub <- paste0(folder,"/")
  }
  
591
  DefaultAssay(sc10x) <- assay
592

593
  #Calculste Vis
594
595
  sc10x <- RunTSNE(sc10x,dims=1:dim,reduction="pca",assay=assay)
  sc10x <- RunUMAP(sc10x,dims=1:dim,reduction="pca",assay=assay)
596
  
Gervaise Henry's avatar
Gervaise Henry committed
597
  sc10x <- FindNeighbors(sc10x,reduction=red,verbose=FALSE,assay=assay)
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
  
  for (i in res){
    sc10x <- FindClusters(sc10x,resolution=i,verbose=FALSE)
    
    plot1 <- DimPlot(sc10x,reduction="pca",label=TRUE,repel=TRUE)+theme(legend.position="none")
    plot2 <- DimPlot(sc10x,reduction="tsne",label=TRUE,repel=TRUE)+theme(legend.position="none")
    plot3 <- DimPlot(sc10x,reduction="umap",label=TRUE,repel=TRUE)+theme(legend.position="none")
    
    if (print=="tsne"){
      postscript(paste0("./analysis/vis/",sub,"tSNE_",i,".eps"))
      print(print2)
      dev.off()
    } else if (print=="umap"){
      postscript(paste0("./analysis/vis/",sub,"UMAP_",i,".eps"))
      print(print3)
      dev.off()
    } else if (print=="2"){
      plot2 <- plot2+theme(legend.position="none")
      plot3 <- plot3+theme(legend.position="none")
      postscript(paste0("./analysis/vis/",sub,"2Vis_",i,".eps"))
      grid.arrange(plot2,plot3,ncol=1)
      dev.off()
    } else if (print=="3"){
      plot1 <- plot1+theme(legend.position="none")
      plot2 <- plot2+theme(legend.position="none")
      plot3 <- plot3+theme(legend.position="none")
      postscript(paste0("./analysis/vis/",sub,"3Vis_",i,".eps"))
      grid.arrange(plot1,plot2,plot3,ncol=1)
      dev.off()
    }}
  
Gervaise Henry's avatar
Gervaise Henry committed
629
  for (i in c("samples","HTO_maxID","hash.ID")[c("samples","HTO_maxID","hash.ID") %in% colnames(sc10x@meta.data)]){
Gervaise Henry's avatar
Gervaise Henry committed
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
    plot1 <- DimPlot(sc10x,reduction="pca",group.by=i)
    plot2 <- DimPlot(sc10x,reduction="tsne",group.by=i)
    plot3 <- DimPlot(sc10x,reduction="umap",group.by=i)
    legend <- cowplot::get_legend(plot1)
    
    if (print=="tsne"){
      postscript(paste0("./analysis/vis/",sub,"tSNE_",i,".eps"))
      grid.arrange(plot2,legend,ncol=1)
      dev.off()
    } else if (print=="umap"){
      postscript(paste0("./analysis/vis/",sub,"UMAP_",i,".eps"))
      grid.arrange(plot3,legend,ncol=1)
      dev.off()
    } else if (print=="2"){
      plot2 <- plot2+theme(legend.position="none")
      plot3 <- plot3+theme(legend.position="none")
      postscript(paste0("./analysis/vis/",sub,"2Vis_",i,".eps"))
      grid.arrange(plot2,plot3,legend,ncol=1)
      dev.off()
    } else if (print=="3"){
      plot1 <- plot1+theme(legend.position="none")
      plot2 <- plot2+theme(legend.position="none")
      plot3 <- plot3+theme(legend.position="none")
      postscript(paste0("./analysis/vis/",sub,"3Vis_",i,".eps"))
      grid.arrange(plot1,plot2,plot3,legend,ncol=1)
      dev.off()
    }
657
  }
Gervaise Henry's avatar
Gervaise Henry committed
658
  
659
  DefaultAssay(sc10x) <- "SCT"
660
661
662
663
664
  
  return(sc10x)
}


665
scScore <- function(sc10x.l,score,geneset,cut.pt=0.9,anchor=FALSE){
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
  #Runs custom PCA analysis for stress ID
  
  #Inputs:
  #sc10x = Seruat object
  #score = name of geneset for scoring
  #geneset = geneset to use for ID
  #cut.pt = % of cells to keep
  
  #Outputs:
  #results[1] = Seurat object (original + score)
  #results[2] = Seurat object (negatively filtered)
  #results[3] = Seurat object (positively filtered)
  
  #Make subdirectory
  if (!dir.exists(paste0("./analysis/score_id/",score))){
    dir.create(paste0("./analysis/score_id/",score))
  }
  if (!dir.exists(paste0("./analysis/vis/",score))){
    dir.create(paste0("./analysis/vis/",score))
  }
  
687
688
689
690
691
  sc10x.l.negative <- list()
  sc10x.l.positive <- list()
  
  for (i in names(sc10x.l)){
    sc10x <- sc10x.l[[i]]
692
    
693
694
695
    #Score geneset
    sc10x <- AddModuleScore(object=sc10x,features=geneset,name=score,assay="SCT")
    Idents(object=sc10x) <- paste0(score,"1")
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
    #CDF
    cdf <- ecdf(as.numeric(levels(sc10x)))
    if (cut.pt == "renyi"){
      h <- hist(data.frame(sc10x[[paste0(score,"1")]])[,paste0(score,"1")],breaks=1000,plot=FALSE)
      cutoff.temp <- mean(c(h$mids[which.max(h$counts)],h$mids[-which.max(h$counts)][which.max(h$counts[-which.max(h$counts)])]))
      cells.remove <- rownames(sc10x[[paste0(score,"1")]])[sc10x[[paste0(score,"1")]][,1] < cutoff.temp]
      sc10x.temp <- subset(sc10x,cells=cells.remove,invert=TRUE)
   
      thresh <- list()
      thresh[["all"]] <- scThresh(list(all=sc10x.temp),feature=paste0(score,"1"),sub=score)
      cut.x <- thresh$all$all$threshold[thresh$all$all$method=="RenyiEntropy"]
    } else if (cut.pt == "triangle"){
      h <- hist(data.frame(sc10x[[paste0(score,"1")]])[,paste0(score,"1")],breaks=1000,plot=FALSE)
      cutoff.temp <- mean(c(h$mids[which.max(h$counts)],h$mids[-which.max(h$counts)][which.max(h$counts[-which.max(h$counts)])]))
      cells.remove <- rownames(sc10x[[paste0(score,"1")]])[sc10x[[paste0(score,"1")]][,1] < cutoff.temp]
      sc10x.temp <- subset(sc10x,cells=cells.remove,invert=TRUE)
      
      thresh <- list()
      thresh[["all"]] <- scThresh(list(all=sc10x.temp),feature=paste0(score,"1"),sub=score)
      cut.x <- thresh$all$all$threshold[thresh$all$all$method=="Triangle"]
    } else if (cut.pt == "minerror"){
      h <- hist(data.frame(sc10x[[paste0(score,"1")]])[,paste0(score,"1")],breaks=1000,plot=FALSE)
      cutoff.temp <- mean(c(h$mids[which.max(h$counts)],h$mids[-which.max(h$counts)][which.max(h$counts[-which.max(h$counts)])]))
      cells.remove <- rownames(sc10x[[paste0(score,"1")]])[sc10x[[paste0(score,"1")]][,1] < cutoff.temp]
      sc10x.temp <- subset(sc10x,cells=cells.remove,invert=TRUE)
      
      thresh <- list()
      thresh[["all"]] <- scThresh(list(all=sc10x.temp),feature=paste0(score,"1"),sub=score)
      cut.x <- thresh$all$all$threshold[thresh$all$all$method=="MinErrorI"]
    } else {
      cut.x <- quantile(cdf,probs=cut.pt)
      cut.x <- unname(cut.x)
    }
    postscript(paste0("./analysis/score_id/",score,"/CDF_",i,".",score,".eps"))
    plot(cdf,main=paste0("Cumulative Distribution of ",score," Score"),xlab=paste0(score," Score"),ylab="CDF")
    abline(v=cut.x,col="red")
    dev.off()  
    
    #KDE
    postscript(paste0("./analysis/score_id/",score,"/Histo_",i,".",score,".eps"))
    plot(ggplot(data.frame(Score=as.numeric(levels(sc10x))),aes(x=Score))+geom_histogram(bins=100,aes(y=..density..))+geom_density()+geom_vline(xintercept=cut.x,size=1,color="red")+ggtitle(paste0(score," Score"))+cowplot::theme_cowplot())
738
    dev.off()
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
    
    Idents(object=sc10x) <- "ALL"
    predicate <- paste0(score,"1 >= ",cut.x)
    Idents(object=sc10x, cells = rownames(sc10x[[paste0(score,"1")]])[sc10x[[paste0(score,"1")]] >= cut.x]) <- score
    sc10x[[score]] <- Idents(object=sc10x)
    Idents(sc10x) <- score
    sc10x.negative <- subset(x=sc10x,idents="ALL")
    sc10x.positive <- subset(x=sc10x,idents=score)
    
    #Generate plots
    postscript(paste0("./analysis/vis/",score,"/3Vis_",i,".",score,".eps"))
    plot1 <- DimPlot(sc10x,reduction="pca",label=TRUE,repel=TRUE)+theme(legend.position="none")+ggtitle("ALL")
    plot2 <- DimPlot(sc10x.negative,reduction="pca",label=TRUE,repel=TRUE)+theme(legend.position="none")
    plot3 <- DimPlot(sc10x.positive,reduction="pca",label=TRUE,repel=TRUE)+theme(legend.position="none")
    plot4 <- DimPlot(sc10x,reduction="tsne",label=TRUE,repel=TRUE)+theme(legend.position="none")+ggtitle("Negative")
    plot5 <- DimPlot(sc10x.negative,reduction="tsne",label=TRUE,repel=TRUE)+theme(legend.position="none")
    plot6 <- DimPlot(sc10x.positive,reduction="tsne",label=TRUE,repel=TRUE)+theme(legend.position="none")
    plot7 <- DimPlot(sc10x,reduction="umap",label=TRUE,repel=TRUE)+theme(legend.position="none")+ggtitle("Positive")
    plot8 <- DimPlot(sc10x.negative,reduction="umap",label=TRUE,repel=TRUE)+theme(legend.position="none")
    plot9 <- DimPlot(sc10x.positive,reduction="umap",label=TRUE,repel=TRUE)+theme(legend.position="none")
    grid.arrange(plot1,plot2,plot3,plot4,plot5,plot6,plot7,plot8,plot9,ncol=3)
    dev.off()
    
    #Generate violin plot of gene exvpression
    if (anchor!=FALSE){
      postscript(paste0("./analysis/score_id/",score,"/Violin_",i,".",score,".eps"))
      plot <- VlnPlot(object=sc10x,features=anchor,pt.size=0.1,assay="SCT")
      plot(plot)
      dev.off()
    }
    sc10x.l[[i]] <- sc10x
    sc10x.l.negative[[i]] <- sc10x.negative
    sc10x.l.positive[[i]] <- sc10x.positive
772
773
774
  }
  
  results <- list(
775
776
777
    sc10x <- sc10x.l,
    sc10x.negative <- sc10x.l.negative,
    sc10x.positive <- sc10x.l.positive
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
  )
  return(results)
}


scQuSAGE <- function(sc10x,gs,save=FALSE,type,id,ds=0,nm="pops",print="tsne"){
  #Runs QuSAGE
  
  #Inputs:
  #sc10x = Seruat object
  #gs = geneset to use for correlation
  #save = save ID
  #type = type of qusage to run (id: create ID's based on run, sm: cor only using small genesets, lg: cor only with large genesets)
  #id = ident to use
  #nm = name of test
  #print = dimentionality reduction to use for display
  
  #Outputs:
  #results[1] = Seurat object
  #results[2] = correlation table
  #results[3] = correlation results
  
  if (!dir.exists(paste0("./analysis/cor/",nm))){
    dir.create(paste0("./analysis/cor/",nm))
  }
  if (!dir.exists(paste0("./analysis/cor/",nm,"/geneset"))){
    dir.create(paste0("./analysis/cor/",nm,"/geneset"))
  }
  if (!dir.exists(paste0("./analysis/cor/",nm,"/cluster"))){
    dir.create(paste0("./analysis/cor/",nm,"/cluster"))
  }
  if (!dir.exists(paste0("./analysis/vis/",nm))){
    dir.create(paste0("./analysis/vis/",nm))
  }
  
  Idents(object=sc10x) <- id
  number.clusters <- length(unique(levels(x=sc10x)))
  
  labels <- paste0("Cluster_",as.vector(Idents(object=sc10x)))
  
  cell.sample <- NULL
  for (i in unique(labels)){
    cell <- WhichCells(sc10x,ident=sub("Cluster_","",i))
    if (length(cell)>ds & ds!=0){
822
      set.seed(71682)
823
824
825
826
827
      rnd <- sample(1:length(cell),ds)
      cell <- cell[rnd]
    }
    cell.sample <- c(cell.sample,cell)
  }
828
  data <- as.data.frame(as.matrix(GetAssayData(sc10x[,colnames(sc10x) %in% cell.sample],assay="SCT",slot="scale.data")))
829
  labels <- labels[colnames(sc10x) %in% cell.sample]
Gervaise Henry's avatar
Gervaise Henry committed
830
831
  #groups <- sort(unique(labels))
  groups <- paste0("Cluster_",levels(sc10x@active.ident))
832
  
Gervaise Henry's avatar
Gervaise Henry committed
833
834
  #col <- hcl(h=(seq(15,375-375/length(groups),length=length(groups))),c=100,l=65)
  col <- brewer.pal(n=length(groups),name="Dark2")
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
  
  #Make labels for QuSAGE
  clust <- list()
  clust.comp <- list()
  for (i in groups){
    t <- labels
    t[t != i] <- "REST"
    clust[i] <- list(i=t)
    rm(t)
    clust.comp[i] <- paste0(i,"-REST")
  }
  
  #Run QuSAGE
  for (i in groups){
    assign(paste0("results.",i),qusage(data,unlist(clust[i]),unlist(clust.comp[i]),gs))
  }
  
  #Generate ID table
  results.cor <- NULL
  results.cor <- qsTable(get(paste0("results.",groups[1])),number=length(gs))
  results.cor$Cluster <- groups[1]
  for (i in groups[-1]){
    qs <- qsTable(get(paste0("results.",i)),number=length(gs))
    qs$Cluster <- i
    results.cor <- rbind(results.cor,qs)
  }
  results.cor <- results.cor[,-3]
  rownames(results.cor) <- NULL
  
  results.clust.id <- NULL
865
866
867
868
  #if (max(results.cor[results.cor[,4]==groups[1] & results.cor[,3]<=0.05,][,2],na.rm=TRUE)>=0){
  #  results.clust.id <- results.cor[results.cor[,4]==groups[1] & results.cor[,3]<=0.05,][which.max(results.cor[results.cor[,4]==groups[1] & results.cor[,3]<=0.05,][,2]),]
  if (max(results.cor[results.cor[,4]==groups[1],][,2],na.rm=TRUE)>=0){
    results.clust.id <- results.cor[results.cor[,4]==groups[1],][which.max(results.cor[results.cor[,4]==groups[1],][,2]),]
869
870
871
872
873
874
875
876
  } else {
    results.clust.id$pathway.name <- "Unknown"
    results.clust.id$log.fold.change <- 0
    results.clust.id$FDR <- 0
    results.clust.id$Cluster <- groups[1]
    results.clust.id <- as.data.frame(results.clust.id)
  }
  for (i in groups[-1]){
877
878
879
880
    #if (max(results.cor[results.cor[,4]==i & results.cor[,3]<=0.05,][,2],na.rm=TRUE)>=0){
    #  results.clust.id <- rbind(results.clust.id,results.cor[results.cor[,4]==i & results.cor[,3]<=0.05,][which.max(results.cor[results.cor[,4]==i & results.cor[,3]<=0.05,][,2]),])
    if (max(results.cor[results.cor[,4]==i,][,2],na.rm=TRUE)>=0){
      results.clust.id <- rbind(results.clust.id,results.cor[results.cor[,4]==i,][which.max(results.cor[results.cor[,4]==i,][,2]),])
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
    } else {
      results.clust.id <- rbind(results.clust.id,data.frame(pathway.name="Unknown",log.fold.change=0,FDR=0,Cluster=i))
    }}
  rownames(results.clust.id) <- NULL
  
  #Determine axes for correlation plots
  max.x.rg <- 0
  min.x.rg <- 0
  max.y.rg <- 0
  for (i in groups){
    qs <- get(paste0("results.",i))
    if (max(qs$path.mean)>max.x.rg){
      max.x.rg <- max(qs$path.mean)
    }
    if (min(qs$path.mean)<min.x.rg){
      min.x.rg <- min(qs$path.mean)
    }
    if (max(qs$path.PDF)>max.y.rg){
      max.y.rg <- max(qs$path.PDF)
    }}
  if (type=="sm"){
902
903
904
905
906
907
908
909
910
911
912
913
914
915
    #Plot correlation plots by geneset
    for (i in 1:length(gs)){
      postscript(paste0("./analysis/cor/",nm,"/geneset/QuSAGE_",nm,".",names(gs)[i],".eps"))
      for (j in groups){
        qs <- get(paste0("results.",j))
        if (j==groups[1]){
          plotDensityCurves(qs,path.index=i,col=col[match(j,groups)],main=names(gs)[i],xlim=c(min.x.rg-0.05,max.x.rg+0.05),ylim=c(0,50*ceiling(max.y.rg/50)),xlab="Gene Set Activation",lwd=5,cex.main=2.5,cex.axis=1.5,cex.lab=2)
        } else {
          plotDensityCurves(qs,path.index=i,add=TRUE,col=col[match(j,groups)],lwd=5)
        }}
      legend("topright",col=col,legend=groups,lty=1,lwd=5,cex=2,ncol=1,bty="n",pt.cex=2)
      box(lwd=5)
      dev.off()
    }
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
    #Plot correlation plots by cluster
    for (i in groups){
      qs <- get(paste0("results.",i))
      postscript(paste0("./analysis/cor/",nm,"/cluster/QuSAGE_",nm,"_",i,".eps"))
      for (j in 1:length(gs)){
        if (j==1){
          plotDensityCurves(qs,path.index=j,col=viridis(length(gs))[j],main=i,xlim=c(min.x.rg-0.05,max.x.rg+0.05),ylim=c(0,50*ceiling(max.y.rg/50)),xlab="Gene Set Activation",lwd=5,cex.main=2.5,cex.axis=1.5,cex.lab=2)
        } else {
          plotDensityCurves(qs,path.index=j,add=TRUE,col=viridis(length(gs))[j],lwd=5)
        }}
      legend("topright",col=viridis(length(gs)),legend=names(gs),lty=1,lwd=5,cex=1,ncol=2,bty="n",pt.cex=2)
      box(lwd=5)
      dev.off()
    }} else {
      for (i in groups){
        qs <- get(paste0("results.",i))
        postscript(paste0("./analysis/cor/",nm,"/cluster/QuSAGE_",nm,"_",i,".eps"))
        plotCIs(qs,path.index=1:numPathways(qs),cex.lab=1.5)
        dev.off()
      }}
  
  if (save==TRUE){
    merge.cluster <- NULL
    for (i in groups){
940
941
942
943
      #if (max(qsTable(get(paste0("results.",i)),number=length(gs))[qsTable(get(paste0("results.",i)),number=length(gs))[,4]<=0.05,][,2],na.rm=TRUE)>=0){
      #  sc10x<-eval(parse(text=paste0("RenameIdents(object=sc10x,'",sub("Cluster_","",i),"' = '",qsTable(get(paste0("results.",i)),number=length(gs))[qsTable(get(paste0("results.",i)),number=length(gs))[2]==max(qsTable(get(paste0("results.",i)),number=length(gs))[qsTable(get(paste0("results.",i)),number=length(gs))[,4]<=0.05,][,2],na.rm=TRUE)][1],"')")))
      if (max(qsTable(get(paste0("results.",i)),number=length(gs))[,2],na.rm=TRUE)>=0){
        sc10x<-eval(parse(text=paste0("RenameIdents(object=sc10x,'",sub("Cluster_","",i),"' = '",qsTable(get(paste0("results.",i)),number=length(gs))[qsTable(get(paste0("results.",i)),number=length(gs))[2]==max(qsTable(get(paste0("results.",i)),number=length(gs))[,2],na.rm=TRUE)][1],"')")))
944
945
946
947
948
949
950
951
952
953
954
      } else {
        sc10x<-eval(parse(text=paste0("RenameIdents(object=sc10x,'",sub("Cluster_","",i),"' = 'Unknown')")))
      }}
    sc10x[[nm]] <- Idents(object=sc10x)
  }
  
  plot1 <- DimPlot(sc10x,reduction="pca",label=TRUE,repel=TRUE)+theme(legend.position="none")
  plot2 <- DimPlot(sc10x,reduction="tsne",label=TRUE,repel=TRUE)+theme(legend.position="none")
  plot3 <- DimPlot(sc10x,reduction="umap",label=TRUE,repel=TRUE)+theme(legend.position="none")
  if (print=="tsne"){
    postscript(paste0("./analysis/vis/",nm,"/tSNE_",id,"_",nm,".eps"))
Gervaise Henry's avatar
Gervaise Henry committed
955
    print(plot2)
956
957
958
    dev.off()
  } else if (print=="umap"){
    postscript(paste0("./analysis/vis/",nm,"/UMAP_",id,"_",nm,".eps"))
Gervaise Henry's avatar
Gervaise Henry committed
959
    print(plot3)
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
    dev.off()
  } else if (print=="2"){
    postscript(paste0("./analysis/vis/",nm,"/2Vis_",id,"_",nm,".eps"))
    grid.arrange(plot2,plot3,ncol=1)
    dev.off()
  } else if (print=="3"){
    postscript(paste0("./analysis/vis/",nm,"/3Vis_",id,"_",nm,".eps"))
    grid.arrange(plot1,plot2,plot3,ncol=1)
    dev.off()
  }
  
  results <- list(
    sc10x=sc10x,
    results.cor=results.cor,
    results.clust.id=results.clust.id
  )
  names(results)=c("sc10x",paste0("results.cor.",nm),paste0("results.clust.",nm,".id"))
  return(results)
}
979

980
981
982
983
984
985
986
987
988
989
990
991
992
993
scShinyOutput <- function(sc10x,anal="raw"){
  write_delim(as.data.frame(colnames(sc10x)),path=paste0("./analysis/shiny/",anal,"/outs/filtered_feature_bc_matrix/barcodes.tsv.gz"),delim="\t",col_names=FALSE)
  features <- rownames(sc10x)
  features <- c(features,c("nFeature","nCount","percent.mito","percent.ribo","Stress.score"))
  features <- data.frame(ENSG=features,Feature=features,Label="feature")
  write_delim(features,path=paste0("./analysis/shiny/",anal,"/outs/filtered_feature_bc_matrix/features.tsv.gz"),delim="\t",col_names=FALSE)
  exp <- GetAssayData(sc10x,slot="scale.data")
  exp.extra <- matrix(nrow=5,ncol=ncol(sc10x))
  exp.extra[1,] <- as.numeric(sc10x$nFeature_RNA)
  exp.extra[2,] <- as.numeric(sc10x$nCount_RNA)
  exp.extra[3,] <- as.numeric(sc10x$percent.mito)
  exp.extra[4,] <- as.numeric(sc10x$percent.ribo)
  exp.extra[5,] <- as.numeric(sc10x$Stress1)
  exp <- rbind(exp,exp.extra)
Gervaise Henry's avatar
Gervaise Henry committed
994
  Matrix::writeMM(as(exp,"dgCMatrix"),file=paste0("./analysis/shiny/",anal,"/outs/filtered_feature_bc_matrix/matrix.mtx.gz"))
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
  for (i in c("pca","tsne","umap")){
    dr <- Embeddings(sc10x,i)
    if (i != "pca"){
      colnames(dr) <- c(paste0(toupper(i),"-",1:2))
    } else {
      dr <- dr[,1:10]
      colnames(dr) <- c(paste0(toupper(i),"-",1:10))
    }
    dr <- cbind(dr,Barcode=rownames(dr))
    dr <- dr[,c(3,1,2)]
    dr <- as.data.frame(dr,row.names=FALSE)
    if (i != "pca"){
1007
      write_csv(dr,paste0("./analysis/shiny/",anal,"/outs/analysis/",i,"/2_components/projection.csv"),col_names=TRUE)
1008
    } else {
1009
      write_csv(dr,paste0("./analysis/shiny/",anal,"/outs/analysis/",i,"/10_components/projection.csv"),col_names=TRUE)
1010
1011
    }
  }
1012
  sc10x <- NormalizeData(sc10x,assay="RNA")
Gervaise Henry's avatar
Gervaise Henry committed
1013
  clusters <- c("samples","samples_HTO",paste0("integrated_snn_res.",res),"lin","pops","leu","scDWSpr","HTO_maxID","hash.ID")
1014
1015
  clusters <- intersect(clusters,names(sc10x@meta.data))
  for (i in clusters){
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
    if (nrow(unique(sc10x[[i]]))>1){
      if (!dir.exists(paste0("./analysis/shiny/",anal,"/outs/analysis/clustering/",gsub("integrated_snn_res.","res_",i)))){
        dir.create(paste0("./analysis/shiny/",anal,"/outs/analysis/clustering/",gsub("integrated_snn_res.","res_",i)))
      }
      clust <- as.matrix(sc10x[[i]])
      colnames(clust) <- "Cluster"
      clust <- cbind(clust,Barcode=rownames(clust))
      clust <- clust[,c(2,1)]
      clust <- as.data.frame(clust,row.names=FALSE)
      clust[,2] <- paste0("Cluster ",clust[,2])
      write_csv(clust,paste0("./analysis/shiny/",anal,"/outs/analysis/clustering/",gsub("integrated_snn_res.","res_",i),"/clusters.csv"),col_names=TRUE)
  
      if (!dir.exists(paste0("./analysis/shiny/",anal,"/outs/analysis/diffexp/",gsub("integrated_snn_res.","res_",i)))){
        dir.create(paste0("./analysis/shiny/",anal,"/outs/analysis/diffexp/",gsub("integrated_snn_res.","res_",i)))
      }
      Idents(sc10x) <- i
      deg <- FindAllMarkers(sc10x,assay="RNA",slot="data",logfc.threshold=0,test.use="MAST",min.pct=0.25,min.diff.pct=0.25,max.cells.per.ident=500)
      dexp <- data.frame("Feature ID"=unique(deg$gene),"Feature Name"=unique(deg$gene))
      for (cluster in unique(deg$cluster)){
        dexp[,paste0("Cluster.",cluster,".Mean.Counts")] <- deg$pct.1[deg$cluster==cluster][match(dexp$Feature.ID,deg$gene[deg$cluster==cluster])]
        dexp[,paste0("Cluster.",cluster,".Log2.fold.change")] <- deg$avg_logFC[deg$cluster==cluster][match(dexp$Feature.ID,deg$gene[deg$cluster==cluster])]
        dexp[,paste0("Cluster.",cluster,".Adjusted.p.value")] <- deg$p_val_adj[deg$cluster==cluster][match(dexp$Feature.ID,deg$gene[deg$cluster==cluster])]
      }
      colnames(dexp) <- gsub("\\."," ",colnames(dexp))
      write_csv(dexp,paste0("./analysis/shiny/",anal,"/outs/analysis/diffexp/",gsub("integrated_snn_res.","res_",i),"/differential_expression.csv"),col_names=TRUE)
1041
    }
1042
  }
1043
}