-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.R
More file actions
636 lines (529 loc) · 24.1 KB
/
Copy pathutils.R
File metadata and controls
636 lines (529 loc) · 24.1 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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
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
629
630
631
632
633
634
635
636
#library(Seurat, quietly = T)
#library(Signac, quietly = T)
#library(GenomicRanges, quietly = T)
#library(dplyr, quietly = T)
#library(ggplot2, quietly = T)
#library(Matrix, quietly = T)
#library(preprocessCore)
#library(harmony)
#library(openxlsx)
#library(chromVAR, quietly = TRUE)
#library(motifmatchr, quietly = TRUE)
#library(SummarizedExperiment, quietly = TRUE)
#library(JASPAR2020)
#library(TFBSTools)
#library(BSgenome.Mmusculus.UCSC.mm10)
#library(BSgenome.Hsapiens.UCSC.hg38)
#library(EnsDb.Mmusculus.v79)
#library(EnsDb.Hsapiens.v86)
# Read seurat objects. Returns list of objects
# add functionality to take 2d path
read_objects <- function(ipath1, ipath2=NULL){
objs <- list.files(ipath1)
obj_list <- c()
for(o in objs){
temp_obj <- readRDS(paste0(ipath1,o))
obj_list <- append(obj_list, temp_obj)
}
if (is.null(ipath2) == FALSE){
objs2 <- list.files(ipath2)
for(o in objs2){
temp_obj2 <- readRDS(paste0(ipath2,o))
obj_list <- append(obj_list, temp_obj2)
}
}
return(obj_list)
}
# Writes spreadsheet of top DE genes
write.top.n.xlsx <- function(markers, outdir, group.by = "cluster", n = 100){
wb <- createWorkbook("TopMarkers")
for (c in unique(markers[[group.by]])){
addWorksheet(wb, c)
tdf <- head(markers[markers[[group.by]] == c, ], n = n)
writeData(wb, sheet = c, x = tdf)
}
saveWorkbook(wb = wb, file = outdir, overwrite = TRUE)
}
peak_calling <- function(sobj, output_assay, species, group.by = "seurat_clusters", outdir = tempdir()){
print("Calling Peaks")
######## Changed to group.by experiment ###########
peaks <- CallPeaks(object = sobj, group.by = group.by, outdir = outdir, cleanup = FALSE)
frags <- Fragments(object = sobj)
peak_counts <- FeatureMatrix(fragments = c(frags), cells = rownames(sobj@meta.data), features = peaks)
# Define new assay for new peak calls
if (species == "mouse"){
peak_assay <- CreateChromatinAssay(
counts = peak_counts,
sep = c(":", "-"),
genome = "mm10",
fragments = frags,
min.cells = 1
)
}
else if(species == "human"){
peak_assay <- CreateChromatinAssay(
counts = peak_counts,
sep = c(":", "-"),
genome = "hg38",
fragments = frags,
min.cells = 1
)
}
# Add new peak assay
sobj[[paste0(output_assay)]] <- peak_assay
return(sobj)
}
chromvar <- function(sobj, species){
pfm <- getMatrixSet(
x = JASPAR2020,
opts = list(collection = "CORE", tax_group = 'vertebrates', all_versions = FALSE)
)
if (species == "mouse"){
# add motif information
sobj <- AddMotifs(
object = sobj,
genome = BSgenome.Mmusculus.UCSC.mm10,
pfm = pfm)
sobj <- RunChromVAR(
object = sobj,
genome = BSgenome.Mmusculus.UCSC.mm10)
} else if (species == "human"){
sobj <- AddMotifs(
object = sobj,
genome = BSgenome.Hsapiens.UCSC.hg38,
pfm = pfm)
sobj <- RunChromVAR(
object = sobj,
genome = BSgenome.Hsapiens.UCSC.hg38)
} else {print("incorrect species input for chromvar: choose 'mouse' or 'human'")}
return(sobj)
}
activity <- function(atac.sobj, species){
if (species == "mouse"){
annotations <- GetGRangesFromEnsDb(ensdb = EnsDb.Mmusculus.v79)
seqlevelsStyle(annotations) <- "UCSC"
genome(annotations) <- "mm10"
Annotation(atac.sobj) <- annotations
} else if (species == "human"){
print("human data is not currently supported. I forget why. Probably cause Ryan is lazy")
}
gene.activities <- GeneActivity(atac.sobj)
# add gene activities as a new assay
atac.sobj[["ACTIVITY"]] <- CreateAssayObject(counts = gene.activities)
# normalize gene activities
DefaultAssay(atac.sobj) <- "ACTIVITY"
atac.sobj <- NormalizeData(atac.sobj)
atac.sobj <- ScaleData(atac.sobj, features = rownames(atac.sobj))
return(atac.sobj)
}
annotation <- function(label_transfer=FALSE, atac.sobj, rna.sobj, anno_column){
if (label_transfer == TRUE){
gene.activities <- GeneActivity(atac.sobj, features = VariableFeatures(rna.sobj))
transfer.anchors <- FindTransferAnchors(reference = rna.sobj, query = atac.sobj, features = VariableFeatures(object = rna.sobj),
reference.assay = "RNA", query.assay = "ACTIVITY", reduction = "cca")
atac_labels <- TransferData(anchorset = transfer.anchors, refdata = rna.sobj[[anno_column]], weight.reduction = mwt[["lsi"]],
dims = 2:30)
atac.sobj <- AddMetaData(atac.sobj, atac_labels, col.name = NULL)
return(atac.sobj)
} else if(label_transfer == FALSE) {
print("skipping label transfer")
}
}
norm_cellquant_bplot <- function(sobj, group.by = "conditions", invert = FALSE, x.scale = 2){
if (invert == FALSE){
Idents(sobj) <- "seurat_clusters"
#table <- table(Idents(sobj), sobj$conditions)
table <- table(Idents(sobj), sobj@meta.data[[group.by]])
nmax <- max(table(Idents(sobj)))
#norm_df <- data.frame(row.names = seq(0, length(unique(sobj$conditions))))
norm_df <- data.frame(row.names = seq(0, length(unique(sobj@meta.data[[group.by]]))))
} else if (invert == TRUE){ # invert conditions to x-axis. Cluster contribution as y.
Idents(sobj) <- "conditions"
table <- table(Idents(sobj), sobj$seurat_clusters)
nmax <- max(table(Idents(sobj)))
norm_df <- data.frame(row.names = seq(0, length(unique(sobj$seurat_clusters))))
}
# Buld normalized cell quantity table
for (r in seq(1,nrow(table))){
#print(r)
rsum <- sum(table[r,])
cfactor <- nmax / rsum
trow <- table[r,] * cfactor
trow <- trow / sum(trow) * 100
norm_df <- rbind(norm_df, trow)
}
colnames(norm_df) <- colnames(table)
row.names(norm_df ) <- seq(1, nrow(norm_df))
#norm_df
cols <- c("cadetblue3", "coral3", "darkolivegreen3", "darkcyan", "mediumpurple1",
"lightgreen", "lightgoldenrod", "lightslateblue", "mistyrose", "lightblue4",
"navajowhite1", "magenta", "lightsalmon", "mediumorchid1", "midnightblue",
"lightskyblue", "lightgoldenrodyellow", "black", "lightgrey", "mistyrose4")
ncols <- sample(x = cols, size = ncol(norm_df), replace = F)
#ncols
# pdf(file = "Plots/ATAC_all_barplot.pdf", width = 14, height = 10)
#barplot(height = as.matrix(t(norm_df)), col = cols, legend = T, , xlim = c(0, 12),
# names.arg = seq(0,length(unique(sobj$seurat_clusters)) -1))
# dev.off()
if (invert == FALSE){
return(barplot(height = as.matrix(t(norm_df)), col = cols, legend = T,
xlim = c(0, (length(unique(sobj$seurat_clusters)) * x.scale)),
names.arg = seq(0,length(unique(sobj$seurat_clusters)) -1)))
} else if(invert == TRUE){
return(barplot(height = as.matrix(t(norm_df)), col = cols, legend = T,
xlim = c(0, (length(unique(sobj$conditions)) * x.scale)),
names.arg = row.names(table)))
}
}
# Function to add conditions using a csv
add.conditions <- function(sobj, dset.col = "orig.ident", cond.csv.path){
# read 2 col csv. C1 = dset. C2 = condition
csv <- read.csv2(cond.csv.path, sep = ",", header = F)
# Create function to return condition for each dset by referencing csv
cond_list <- function(dataset){
csv.row <- csv[csv$V1 == dataset,]
condition <- csv.row[,2]
return(condition)
}
# Apply func to each row of metadata dset col.
conditions <- apply(X = as.data.frame(sobj@meta.data[[dset.col]]), MARGIN = 1, FUN = cond_list)
# Store new vector of conditions in object
sobj$conditions <- conditions
# Return new object with condition column
return(sobj)
}
qt.normalize <- function(object_list){
# Find max ncells out of object list. Need that to create correct size matrix max ncells will be nrows
# matrix is max ncells by n objects in list
ncells.l <- sapply(object_list, FUN = function(object){
return(nrow(object@meta.data))
})
max.c <- max(ncells.l)
# initialize matrix
dmat <- matrix(data = as.numeric("NA"), nrow = max.c, ncol = length(object_list))
# for o in obj list: get scores, append to matrix
cntr <- 1
for (o in object_list){
# extract doublet scores
d.scores <- o@meta.data$DF.scores
# enter scores into matrix to be normalized
dmat[1:length(d.scores),cntr] <- d.scores
cntr <- cntr + 1
}
# quantile normalize matrix
n.dmat <- normalize.quantiles(x = dmat)
#print(head(n.dmat))
# for o in obj list: add col of matrix to metadata, replace itself in object list
cntr <- 1
object_list2 <- c()
for (o in object_list){
# extract normalized scores
n.scores <- n.dmat[,cntr]
# apply them to objects with NA's stripped
o@meta.data$qn.DF.scores <- na.omit(n.scores)
# add object with normalized scores to new list
object_list2[[cntr]] <- o
cntr <- cntr + 1
}
return(object_list2)
}
qt.norm.drm.scoring <- function(object, normalize = TRUE, outdir = NULL, dset_col = "orig.ident"){
# If single object is submitted with no qt normalization. Run All
if(length(object) == 1 & normalize == TRUE){
# 1.Subset obj by dataset
# 2.create list of objects
#Idents(object) <- "orig.ident"
#dsets <- unique(object@meta.data$orig.ident)
# changed for atac flexibility
Idents(object) <- dset_col
dsets <- unique(Idents(object))
obj_list <- c()
for (d in dsets){
t.obj <- subset(object, ident = d)
obj_list <- append(obj_list, t.obj)
}
# 3.apply qt.normalize function
n.obj.list <- qt.normalize(obj_list)
# 4.merge object list with norm doub scores
mobj <- merge(x= n.obj.list[[1]],
y= n.obj.list[2:length(n.obj.list)])
} else if(length(object) > 1 & normalize == TRUE){
# 3.apply qt.normalize function
n.obj.list <- qt.normalize(obj_list)
# 4.merge object list with norm doub scores
mobj <- merge(x= n.obj.list[[1]],
y= n.obj.list[2:length(n.obj.list)])
} else if(length(object) > 1 & normalize == FALSE){
# 4.merge object list with norm doub scores
mobj <- merge(x= n.obj.list[[1]],
y= n.obj.list[2:length(n.obj.list)])
}
# If object length = 1 and norm = False. Start here
# 5.For each cluster extract the norm doublet scores
Idents(mobj) <- "seurat_clusters"
clusters <- seq(0, length(unique(mobj@meta.data$seurat_clusters)) - 1 )
clust.qn.dfscores <- lapply(X = clusters, FUN = function(c){
# For each cluster; do this
t.obj <- subset(mobj, idents = c)
scores <- t.obj$qn.DF.scores
return(scores)
})
# 6.Take the summary() of each distribution of doub scores
# 7.Print summary of clust distributions to a file
cntr <- 1
doub.table <- data.frame()
for (c in clust.qn.dfscores){
s <- summary(c)
# Row is clust #, ncells, summary scores(min, q1, median, mean, q3, max)
t.row <- c(cntr - 1, length(c), s)
doub.table <- rbind(doub.table, t.row)
cntr <- cntr + 1
}
colnames(doub.table) <- c("Cluster", "nCells", "MinScore", "Q1", "Median", "Mean", "Q3", "MaxScore")
dt.sort <- doub.table[order(doub.table$Mean),]
# transfer qn.scores to original object. Create feature plot of qn.scores on orig embedding
object[["qn.DF.scores"]] <- mobj$qn.DF.scores
# ****** Plotting ******
# 8.Create violin plots, kelseys line plot, write doub table to file
if (is.null(outdir) == FALSE){
# Save doub.table as tsv
write.table(x = dt.sort, file = paste0(outdir, "normalized_doublet_summary.txt"), row.names = F)
# Boxplots
bxp1 <- ggplot(mobj@meta.data, aes(seurat_clusters, DF.scores)) +
geom_boxplot(varwidth=T, fill="plum") +
labs(title="Liver RNA Box plot",
subtitle="Doublet Score by Cluster",
x="Cluster",
y="Doublet Score")
bxp2 <- ggplot(mobj@meta.data, aes(seurat_clusters, qn.DF.scores)) +
geom_boxplot(varwidth=T, fill="plum") +
labs(title="Liver RNA Box plot",
subtitle="Normalized Doublet Score by Cluster",
x="Cluster",
y="Normalized Doublet Score")
# Kelseys line plot
min.mean <- min(dt.sort$Mean)
max.mean <- max(dt.sort$Mean)
klp1 <- ggplot(dt.sort, aes(x=1:nrow(dt.sort), y=Mean, label=Cluster)) +
geom_point() + geom_line() +
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank()) +
ylab("Mean Normalized Doublet Score") + xlab("Clusters") +
geom_text(hjust=-0.2, vjust=-2) +
lims(x= c(1,nrow(dt.sort)),y=c(min.mean - .01, max.mean + .02)) +
ggtitle("Mean Normalized Doublet Score per Cluster")
#geom_text_repel(aes(x=1:nrow(dt.sort), y=Mean, label=Cluster))
dt.sort.2 <- doub.table[order(doub.table$Median),]
min.median <- min(dt.sort$Median)
max.median <- max(dt.sort$Median)
klp2 <- ggplot(dt.sort.2, aes(x=1:nrow(dt.sort.2), y=Median, label=Cluster)) +
geom_point() +
geom_line() +
theme(axis.text.x=element_blank(),
axis.ticks.x=element_blank()) +
ylab("Median Normalized Doublet Score") +
xlab("Clusters") +
geom_text(hjust=0, vjust=-2) +
lims(x= c(1,nrow(dt.sort.2)),y=c(min.median - .01, max.median + .02)) +
ggtitle("Median Normalized Doublet Score per Cluster")
Idents(object) <- "seurat_clusters"
Idents(mobj) <- "seurat_clusters"
vp1 <- VlnPlot(object, features = c("DF.scores"), ncol = 1)
vp2 <- VlnPlot(mobj, features = c("qn.DF.scores"), ncol = 1)
Idents(object) <- dset_col
Idents(mobj) <- dset_col
vp3 <- VlnPlot(object, features = c("DF.scores"), ncol = 1)
vp4 <- VlnPlot(mobj, features = c("qn.DF.scores"), ncol = 1)
# Umap of high res object
Idents(object) <- "seurat_clusters"
ump1 <- DimPlot(object, label = T)
# Feature plot of normalized scores on original object
fplt1 <- FeaturePlot(object, features = "qn.DF.scores")
# Write plots to outdir
ggsave(filename = paste0(outdir, "mean_ndoub_lplot.pdf"), plot = klp1, device = "pdf", width = 13, height = 9)
ggsave(filename = paste0(outdir, "median_ndoub_ndoub_lplot.pdf"), plot = klp2, device = "pdf", width = 13, height = 9)
ggsave(filename = paste0(outdir, "doub_bxplt.pdf"), plot = bxp1, device = "pdf", width = 20, height = 9)
ggsave(filename = paste0(outdir, "ndoub_bxplt.pdf"), plot = bxp2, device = "pdf", width = 20, height = 9)
ggsave(filename = paste0(outdir, "doub_clust_vln.pdf"), plot = vp1, device = "pdf", width = 14, height = 9)
ggsave(filename = paste0(outdir, "ndoub__clust_vln.pdf"), plot = vp2, device = "pdf", width = 14, height = 9)
ggsave(filename = paste0(outdir, "doub_dset_vln.pdf"), plot = vp3, device = "pdf", width = 14, height = 9)
ggsave(filename = paste0(outdir, "ndoub__dset_vln.pdf"), plot = vp4, device = "pdf", width = 14, height = 9)
ggsave(filename = paste0(outdir, "ndoub__dset_vln.pdf"), plot = vp4, device = "pdf", width = 14, height = 9)
ggsave(filename = paste0(outdir, "high_res_umap.pdf"), plot = ump1, device = "pdf", width = 11, height = 9)
ggsave(filename = paste0(outdir, "ndoub_fplt.pdf"), plot = fplt1, device = "pdf", width = 11, height = 9)
}
#return(mobj)
return(object)
}
sct.harm.processing <- function(sobj, dims = 1:20, res = 0.05, n.neigh = 30L, min.dist = 0.3,
spread = 1, SCT = TRUE, harmony = FALSE, harm.group = "org.ident", CM = TRUE){
if (SCT){
sobj <- SCTransform(sobj, vst.flavor = "v2", conserve.memory = CM,
verbose = FALSE)
}
if (harmony) {
sobj <- RunPCA(object = sobj, verbose = FALSE) %>%
RunHarmony(reduction.use = "pca", group.by.vars = harm.group,
assay.use = "SCT", project.dim = FALSE)
sobj <- FindNeighbors(sobj, dims = dims, verbose = FALSE,
reduction = "harmony") %>% FindClusters(resolution = res,
verbose = FALSE) %>% RunUMAP(dims = dims, n.neighbors = n.neigh,
min.dist = min.dist, spread = spread, verbose = FALSE,
reduction = "harmony")
}
else {
sobj <- RunPCA(object = sobj, verbose = FALSE) %>%
FindNeighbors(sobj, dims = dims, verbose = FALSE,
reduction = "pca") %>% FindClusters(resolution = res,
verbose = FALSE) %>% RunUMAP(dims = dims, n.neighbors = n.neigh,
min.dist = min.dist, spread = spread, verbose = FALSE,
reduction = "pca")
}
return(sobj)
}
lsi.processing <- function(sobj, TF.method = 1, dims = 2:30, res = .05, assay = "ATAC", harmony = FALSE){
DefaultAssay(sobj) <- assay
sobj <- RunTFIDF(sobj, method = TF.method, assay = assay) %>%
FindTopFeatures(min.cutoff = 'q0') %>%
RunSVD()
if(harmony){
sobj <- RunHarmony(object = sobj,
reduction = "lsi",
group.by.vars = "experiment",
assay.use = assay,
project.dim = FALSE)
sobj <- FindNeighbors(sobj, dims = dims, verbose = FALSE, reduction = "harmony") %>%
FindClusters(resolution = res, verbose = FALSE, algorithm = 3) %>%
RunUMAP(dims = dims , verbose = FALSE, reduction = "harmony")
} else{
sobj <- FindNeighbors(sobj, dims = dims, verbose = FALSE, reduction = "lsi") %>%
FindClusters(resolution = res, verbose = FALSE, algorithm = 3) %>%
RunUMAP(dims = dims , verbose = FALSE, reduction = "lsi")
}
return(sobj)
}
norm_cellquant_bplot2 <- function (sobj, dset.col = "orig.ident", xlab = "seurat_clusters", stack.by = "condition", rand.cols = FALSE){
### This function needs to do 2 things
# 1. Normalize the cell quanities by dataset.
# 2. Plot the normized values using specified variables
# Create table of cells objserved for our dataset and xaxis variable
table <- table(sobj@meta.data[[dset.col]], sobj@meta.data[[xlab]])
# Normaize this table by dataset cell quanity, so all datasets are scaled by largest.
# Coult use any value but largest of the group works
nmax <- max(rowSums(table))
norm.df <- data.frame(row.names = rownames(table))
for (r in seq(1, nrow(table))) {
rsum <- sum(table[r, ])
cfactor <- nmax/rsum
trow <- table[r, ] * cfactor
# the normaized quanitites are then expresed as a percent of the total accross the x-axis groups
trow <- trow/sum(trow) * 100
norm.df <- rbind(norm.df, trow)
}
colnames(norm.df) <- colnames(table)
rownames(norm.df) <- rownames(table)
### Normalization is done.
# Next step is to add our stack.by variable
# If it is not dataset... in which case skip
vars <- c()
for (r in rownames(norm.df)){
# draw the index associated with the dataset in the stack.by column
var <- sobj@meta.data[sobj@meta.data[[dset.col]] == r, stack.by][1]
vars <- append(vars, var)
}
norm.df[[stack.by]] <- vars
# melt dataframe for ggplot
norm.df.m <- melt(norm.df, id = stack.by)
# print(norm.df)
### plotting
cols <- c("cadetblue4", "lightgoldenrod", "salmon",
"paleturquoise3","palegreen3", "mediumpurple1", "salmon",
"lightblue4", "navajowhite1", "magenta", "coral2",
"mediumorchid1", "midnightblue", "lightgoldenrodyellow",
"black", "lightgrey", "mistyrose4","darkcyan", "steelblue2",
"darkolivegreen3", "mediumpurple1", "lightskyblue")
# randomize colors for fun
if (rand.cols == TRUE){
cols <- sample(x = cols, size = ncol(norm.df), replace = F)
}
# Create ggplot ****** note the bizzare ass method for using variables in ggplot... nice
bp <- ggplot(norm.df.m, aes(fill = .data[[stack.by]] , y = value, x = variable)) +
geom_bar(position="fill", stat="identity") + scale_fill_manual(values = cols) +
theme(axis.text.x = element_text(angle = 40, vjust = 0.5, hjust=1)) +
ggtitle("Normalized Stacked Barplot") + xlab(xlab) + ylab("pct.cell.quantity")
return(bp)
}
dset_barplot <- function(sobj, dset.col = "orig.ident", stack.by = "seurat_clusters", rand.cols = FALSE){
table <- table(sobj@meta.data[[dset.col]], sobj@meta.data[[stack.by]])
### express columns as percent of a total for each dataset
for (r in seq(1, nrow(table))) {
trow <- table[r, ]
table[r,] <- trow/sum(trow) * 100
}
# print(table)
m.table <- melt(table, id = rownames(table))
colnames(m.table) <- c("dataset", "cluster", "pct.quantity")
m.table$cluster <- as.factor(m.table$cluster)
### plotting
cols <- c("cadetblue4", "lightgoldenrod", "salmon",
"paleturquoise3","palegreen3", "mediumpurple1", "salmon",
"lightblue4", "navajowhite1", "magenta", "coral2",
"mediumorchid1", "midnightblue", "lightgoldenrodyellow",
"black", "lightgrey", "mistyrose4","darkcyan", "steelblue2",
"darkolivegreen3", "mediumpurple1", "lightskyblue")
# randomize colors for fun
if (rand.cols == TRUE){
cols <- sample(x = cols, size = ncol(norm.df), replace = F)
}
bp <- ggplot(m.table, aes(fill = cluster , y = pct.quantity, x = dataset)) +
geom_bar(position="fill", stat="identity") + scale_fill_manual(values = cols) +
theme(axis.text.x = element_text(angle = 40, vjust = 0.5, hjust=1)) +
ggtitle("Stacked Barplot") + xlab("dataset") + ylab("pct.cell.quantity")
return(bp)
}
comp_umap <- function(sobj, comparison, cond_col, anno_col, reduction = "umap"){
cond1 <- comparison[[1]]
cond2 <- comparison[[2]]
# Divide total cells in cond1 by total cells in cond2
base.ratio <- table(sobj[[cond_col]])[cond1] / table(sobj[[cond_col]])[cond2]
# table of anno col * cond col
comp.table <- table(sobj@meta.data[,anno_col], sobj@meta.data[,cond_col])
# extract cond1 and cond2 columns as a df
comp.df <- data.frame(cond1 = comp.table[,cond1], cond2 = comp.table[,cond2])
comp.df$ratio <- comp.df[,"cond1"] / comp.df[,"cond2"]
comp.df$ratio.norm <- comp.df[,"ratio"] / base.ratio
comp.df$foldChange <- log2(comp.df$ratio.norm)
### Add the ratio to each cell in the object pertaining to that cluster
ratio.list <- c()
for (c in sobj@meta.data[[anno_col]]){
# print(c)
t.row <- comp.df[rownames(comp.df) == c,]
ratio.list <- append(ratio.list, t.row[,"foldChange"])
}
sobj[[paste0(cond1, "_", cond2, "_foldChange")]] <- ratio.list
FeaturePlot(object = sobj, reduction = reduction, features = paste0(cond1, "_", cond2, "_foldChange")) +
scale_color_gradient2(midpoint=0, low="blue", mid="cornsilk", high="red", space ="Lab", name = "FoldChange") +
ggtitle(paste0(cond1))
}
update_frag_paths <- function(sobj, new_frags){
opaths <- c()
for (i in seq(1, length(Fragments(sobj)))){
opath <- Fragments(sobj)[[i]]@path
opaths <- append(opaths, opath)
}
### Create new frags objects with replaced paths
nfiles <- list.files(new_frags)
frags <- c()
for (i in seq(1, length(opaths))){
opath <- opaths[i]
print(paste0("Old path: ", opath))
nfile <- grep(pattern = paste0(basename(opath), "$"), x = nfiles, value = TRUE)
npath <- paste0(new_frags, nfile)
print(paste0("New path: ", npath))
frag_obj <- UpdatePath(Fragments(sobj)[[i]], new.path = npath)
frags <- append(frags, frag_obj)
}
Fragments(sobj) <- frags
return(sobj)
}
### Add new functions here