-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpseudobulk.R
69 lines (62 loc) · 3.19 KB
/
pseudobulk.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
#' @export
SignatureRepresentation <- function(seuratObj, signature){
seuratObj$orig.ident <- droplevels(seuratObj$orig.ident)
sigCells <- FindCellsCoexpressingGenes(seuratObj, signature)
sigSeurat <- subset(seuratObj, cells = sigCells)
conditionSeurat <- dplyr::count([email protected], orig.ident)$n
conditionSig <- dplyr::count([email protected], orig.ident, .drop = FALSE)$n
print(dplyr::count([email protected], orig.ident, .drop = FALSE))
df <- data.frame(Grouping = levels(seuratObj$orig.ident), pvalue = sapply(1:length(levels(seuratObj$orig.ident)),
function(i) phyper(conditionSig[i], conditionSeurat[i], sum(conditionSeurat) - conditionSeurat[i],
sum(conditionSig))))
df <- BYCorrectDF(df)
return(df)
}
ClusterSignatureRepresentation <- function(seuratObj, signature){
seuratObj$seurat_clusters <- droplevels(seuratObj$seurat_clusters)
sigCells <- FindCellsCoexpressingGenes(seuratObj, signature)
sigSeurat <- subset(seuratObj, cells = sigCells)
groupingSeurat <- dplyr::count([email protected], seurat_clusters)$n
groupingSig <- dplyr::count([email protected], seurat_clusters, .drop = FALSE)$n
print(dplyr::count([email protected], seurat_clusters, .drop = FALSE))
df <- data.frame(Grouping = levels(seuratObj$seurat_clusters),
pvalue = sapply(1:length(levels(seuratObj$seurat_clusters)),
function(i) phyper(groupingSig[i], groupingSeurat[i], sum(groupingSeurat) - groupingSeurat[i], sum(groupingSig))))
df <- BYCorrectDF(df)
return(df)
}
GroupingGenes <- function(singleGenes, markers){
pairs <- c()
pvalues <- c()
for (x in singleGenes)
if (x %in% rownames(markers)){
pairs <- c(pairs, x)
pvalues <- c(pvalues, markers[x, ]$p_val_adj)
}
#Markers are called Grouping just to be consistent with PrintOverlap2
df <- data.frame(Grouping = pairs, pvalue = pvalues)
if (length(df$pvalue) > 0)df <- df[order(df$pvalue),]
return(df)
#It is already corrected with Bonferroni - no need for BY!
}
AllGroupingsGenes <- function(singleGenes, markerList)
return(lapply(markerList, function(x) GroupingGenes(singleGenes, x)))
PrintGenesInGroupings <- function(singleGenes, markerList)
return(invisible(lapply(AllGroupingsGenes(singleGenes, markerList), PrintOverlap)))
SingleGeneSelections <- function(singleGenes, selectionMarkers, RownamesCML, index){
print(singleGenes[index])
pairs <- c()
pvalues <- c()
for (i in 1:length(RownamesCML))
if (singleGenes[[index]] %in% rownames(selectionMarkers[[i]])){
pairs <- c(pairs, RownamesCML[i])
pvalues <- c(pvalues, selectionMarkers[[i]][singleGenes[index], ]$p_val_adj)
}
df <- data.frame(Grouping = pairs, pvalue = pvalues)
if (dim(df)[1] > 0)df <- df[order(df$pvalue),]
return(df)
#It is already corrected with Bonferroni - no need for BY!
}
PrintAllSGSelections <- function(singleGenes, selectionMarkers, RownamesCML)
invisible(lapply(1:length(singleGenes), function(x)
PrintOverlap(SingleGeneSelections(singleGenes, selectionMarkers, RownamesCML, x))))