-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity_tools.R
97 lines (81 loc) · 4.57 KB
/
activity_tools.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#' @export
#Tools for working with ORIGINS activity
ActivityFP <- function(seuratObj)
return(FeaturePlot(seuratObj, "activity") + scale_colour_gradientn(colours = wes_palette("Zissou1")))
ActivityHist <- function(seuratObj,
histTitle = "The distribution of activity scores in each experimental condition",
nBins = 100)
return (MakeHistogram(seuratObj, "activity", "orig.ident", "Activity", nBins = 100) + ggtitle(histTitle)+
theme(plot.title = element_text(hjust = 0.5)))
ActivityHistCl <- function(seuratObj, histTitle = "The distribution of activity scores in each cluster", nBins = 100)
return (MakeHistogram(seuratObj, "activity", "seurat_clusters", "Activity", nBins = 100) + ggtitle(histTitle)+
theme(plot.title = element_text(hjust = 18), axis.title.x = element_text(vjust = 1.8)))
GetActivityCorrelations <- function(seuratObj, method = "spearman")
return(GetStandardCorrelations(seuratObj, seuratObj$activity, method))
TopActivityGenes <- function(activityCorrs, cutoff = 0.2)
return(rownames(subset(activityCorrs, Correlation > cutoff)))
ActsPlotDF <- function(seuratObj, acts, colStr, df)
return (data.frame(Gene = acts, Value = df[acts, colStr],
Cluster = ClusterAvgExp(seuratObj, acts)$rowmax))
#Plotting genes with a high correlation with activity scores, arranged by the cluster of maximum avg. activity scores
ActsPlot <- function(seuratObj, actsPlotDF, geneCutoff = 50)
return (ggplot(data = actsPlotDF) + aes(y = Value, x = Cluster) + geom_point(color = "red") +
geom_text_repel(aes(label = Gene), size = 3, max.overlaps = Inf) + theme_minimal() +
ggtitle(str_c("Top ", geneCutoff,
" genes ranked by the Spearman correlation of their expression \nand ORIGINS activity, grouped by the cluster of highest expression")) +
theme(plot.title = element_text(hjust = 0.5, size = 12)) +
coord_cartesian(xlim = c(0, length(levels(seuratObj)))) +
scale_x_discrete(limits=0:length(levels(seuratObj)) - 1) +
ylab("Mean weighted activity")
)
WeightedActsPlot <- function(actsPlotDF, seuratObj, cutoff = 0.5)
return (ActsPlot(actsPlotDF, seuratObj, cutoff))
#Comparing activity scores between clusters
ActivityWilcoxCluster <- function(seuratObj){
pairs <- c()
pvalues <- c()
colLevels <- levels(seuratObj$seurat_clusters)
for (i in colLevels){
message(str_c("Comparing median activity scores for cluster: ", i, "."))
for (j in setdiff(colLevels, i)){
pairs <- c(pairs, str_c("Cluster ", i," vs. Cluster ", j))
pvalues <- c(pvalues, wilcox.test(subset(seuratObj, seurat_clusters == i)$activity, subset(seuratObj, seurat_clusters == j)$activity,
alternative = "greater")$p.value)
}
}
df <- data.frame(Grouping = pairs, pvalue = pvalues)
df <- BYCorrectDF(df)
return(df)
}
ScoreActivityWilcox <- function(seuratObj, actWilDF){
df <- data.frame(Cluster = str_c("Cluster ", levels(seuratObj)),
Wins = sapply(levels(seuratObj),
function(x)sum(sapply(actWilDF$Grouping,
function(y) strsplit(y, " vs. ")[[1]][[1]] ==
str_c("Cluster ", x)))),
Losses = sapply(levels(seuratObj),
function(x)sum(sapply(actWilDF$Grouping,
function(y) strsplit(y, " vs. ")[[1]][[2]] ==
str_c("Cluster ", x))))
)
df$Score <- df$Wins - df$Losses
return(df[order(df$Score, decreasing = T),])
}
#Find genes associated with the highest activity score relative to their expression
WAActivity <- function(seuratObj){
expression <- as.matrix(GetAssayData(seuratObj, assay = "SCT", slot = "data"))
df <- data.frame(WAActivity = unlist(lapply(rownames(seuratObj), function(x){
geneExp <- expression[x,]
return (sum(geneExp * seuratObj$activity) / sum(geneExp))
}
)), nCells = unlist(lapply(rownames(seuratObj), function(x) length(FindCellsExpressingGene(seuratObj, x)))))
rownames(df) <- rownames(seuratObj)
df <- df[order(df$WAActivity, decreasing = T), ,drop = F]
return(df)
}
#Calculates effect size using Cohen's D
ActivityCohen <- function(seuratObj, cond1, cond2){
sdev <- sd(seuratObj$activity)
diff <- mean(subset(seuratObj, orig.ident == cond1)$activity) - mean(subset(seuratObj, orig.ident == cond2)$activity)
return(diff / sdev)
}