-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeurat_pseudobulk_DEA.R
202 lines (180 loc) · 8.59 KB
/
Seurat_pseudobulk_DEA.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
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
#' Function to separate single-cell data by a factor of interest and create
#' pseudo-bulk datasets by combining matrices for each factor
#' followed by DEA with DESeq2
#'
#' currently supports only single-factor designs
#' @param object
#' @param design
#' @param splitCells
#' @param skip
#' @param cellFactor
#' @param organism
#' @param geneVar
#' @param outDir
#' @param outPrefix
#' @param sortDirection
#' @param minCells
#' @param cores
#' @param fit_type
#' @param genomePrefix
#' @param cell_mappings
#' @return
#' @import Seurat
#' @import DESeq2
#' @import biomaRt
#' @import future
#' @import BiocParallel
#' @export
bulkDEA = function(object, #seurat object
metaData, #metadata mapping samples to factors. Rownames are sample names.
design, #design string for DESeq
splitCells = T, #whether or not to split by cell type (or whatever)
skip = NA, #if splitting cells, which factors should be skipped? Defaults to none.
cellFactor = NULL, #name of the cell-type column
organism, #in ensembl format, e.g. mmusculus
geneVar, #type of gene ID used, in biomart format e.g. ensembl_gene_id
outDir, #directory to output results
outPrefix, #file prefix for results files
sortDirection = c("descending", "ascending"), #direction to sort comparisons: alphabetical "ascending" or reverse "descending"
minCells = 50, #whether or not to override the minimum number of cells/samples
cores = 1, #cores to run in parallel
fit_type = "parametric", #to control dispersion fitting
genomePrefix = NA, # regular expression of genome prefixe(s) on gene names for removal and better binding downstream
cell_mappings = NULL) #optional data frame to redefine samples from individual cell IDs rather than just prefix. Rownames are cell IDs.
{
register(BPPARAM = MulticoreParam(cores))
dateString = format(Sys.Date(), "%y%m%d")
if(!dir.exists(outDir))
dir.create(outDir)
setwd(outDir)
compFactor = colnames(metaData)[1] # the factor we will be comparing by
sortDirection = match.arg(sortDirection)
shouldDecrease = (sortDirection == "descending")
#make mart for conversion
mart = useMart("ensembl", paste0(organism, "_gene_ensembl"))
conv = getBM(attributes = c("ensembl_gene_id", "entrezgene_id", "external_gene_name"), mart = mart)
# keep track of number of samples per condition
completeMetadata = metaData
#handle sample IDs
if(is.null(cell_mappings))
{
allSamples = sort(unique(substr(rownames([email protected]), 1, (regexpr("\\_+[ATCG]", rownames([email protected])) - 1))))
} else
{
allSamples = unique(cell_mappings[, 1])
}
sampleData = matrix(nrow = 0, ncol = (length(allSamples) + 1)) # keeps track of number of cells per condition
sampleDataCols = c("cellType", paste0("Ncells_", allSamples))
colnames(sampleData) = sampleDataCols
sampleData = as.data.frame(sampleData)
bulkSampleData = matrix(nrow = 0, ncol = (length(unique(metaData[ , 1])) + 1)) #keep track of number of biological replicates per condition
bulkSampleDataCols = c("cellType", paste0("Nsamples_", sort(unique(as.character(metaData[ , 1])))))
colnames(bulkSampleData) = bulkSampleDataCols
bulkSampleData = as.data.frame(bulkSampleData)
cellTypes = c(levels(factor([email protected][ , cellFactor])))
cellTypes = setdiff(cellTypes, skip)
for(cell in cellTypes)
{
message(cell)
metaData = completeMetadata #reset for each round
cell_sub = object
if(cell != "all_cells" && splitCells) #for all cells, keep whole dataset
cell_sub = cell_sub[ , [email protected][ , cellFactor] == cell]
#now get joined counts matrices
counts = round(as.matrix(cell_sub@assays$RNA@counts)) # raw counts
sd = cell
for(sample in allSamples) #get number of cells for each (zero therefore means not present)
{
if(is.null(cell_mappings))
{
sd = c(sd, sum(grepl(sample, colnames(cell_sub))))
} else
{
sd = c(sd, sum([email protected][, colnames(cell_mappings)[1]] == sample))
}
}
samples = allSamples[as.numeric(sd[2:length(sd)]) >= minCells] # need at least 50 cells to get a real picture
sampleData$cellType = as.character(sampleData$cellType)
sampleData = rbind(sampleData, sd)
colnames(sampleData) = sampleDataCols #necessary on first pass (but harmless afterward)
# make sure comparison is even worth it
metaData = subset(metaData, rownames(metaData) %in% samples)
if(length(unique(metaData[, 1])) < 2) # skip if we can't make any comparisons
{
warning("Skipped -- not enough samples")
next
}
#remove factors with fewer than 2 samples
bulkN = table(metaData[, 1])
bulkN = data.frame(sample = names(bulkN), N = as.numeric(bulkN))
bulkN = bulkN[order(bulkN$sample), ]
keep = bulkN[bulkN$N >= 2, ]$sample
if(length(keep) > 1)
{
cell_sub = cell_sub[ , [email protected][ , compFactor] %in% keep]
} else
{
warning("Skipped -- not enough samples")
next #no need to continue with analysis in this case
}
#update sample Ns
bulkN$N[bulkN$N < 2] = NA
bulkSampleData$cellType = as.character(bulkSampleData$cellType) #necessary on first pass (but harmless afterward)
bulkSampleData = rbind(bulkSampleData, c(cell, bulkN$N))
colnames(bulkSampleData) = bulkSampleDataCols #necessary on first pass (but harmless afterward)
joinedMat = matrix(data = NA, nrow = nrow(counts), ncol = length(samples))
rownames(joinedMat) = rownames(counts)
colnames(joinedMat) = samples
for(sample in samples)
{
if(is.null(cell_mappings))
{
sampleMat = counts[ , grepl(sample, colnames(counts))]
} else
{
sampleCells = colnames(cell_sub)[[email protected][, colnames(cell_mappings)[1]] == sample]
sampleMat = counts[, sampleCells]
}
sampleCounts = rowSums(sampleMat)
joinedMat[, sample] = sampleCounts
}
#now for DEA
stopifnot(class(design) == "formula") #calling formula in function causes massive problems. Avoid by doing externally!
joinedMat = joinedMat[, match(rownames(metaData), colnames(joinedMat))]
des = DESeqDataSetFromMatrix(countData = joinedMat, colData = metaData, design = design)
#safe for symbols
outPath = paste(dateString, outPrefix, gsub("\\W", replacement = "-", cell), "subset", "des.rds", sep = "_")
saveRDS(des, outPath)
#if there is a zero in every gene, data become unreliable. Discard.
if(all(rowSums(counts(des, normalized = F) == 0) > 0))
{
warning("Skipped -- Every gene contains at least one zero, cannot compute log geometric means")
next
}
dge = DESeq(des, parallel = T, fitType = fit_type)
allFactors = sort(unique(as.character(metaData[, 1])), decreasing = shouldDecrease)
allComps = combn(x = allFactors, m = 2, simplify = F) # every possible combination of ages; list of older, younger for each comp
allComps = lapply(allComps, as.character) #otherwise we get factor levels!
for(comp in allComps)
{
res = as.data.frame(results(dge, contrast = c(colnames(metaData)[1], comp[1], comp[2]), alpha = 0.05, parallel = T))
res = rownames_to_column(res, var = geneVar)
if(!is.null(genomePrefix) && geneVar == "external_gene_name")
{
res[, geneVar] = gsub(genomePrefix, "", res[, geneVar])
}
if(geneVar == "ensembl_gene_id")
{
res$ensembl_gene_id = ifelse(grepl("\\.", res$ensembl_gene_id),
yes = substr(res$ensembl_gene_id, 1, (regexpr("\\.", res$ensembl_gene_id) - 1)),
no = res$ensembl_gene_id) #remove version numbers
}
res = merge(res, conv, all.x = T)
compName = paste(comp[1], "vs", comp[2], sep = "_")
write.csv(res, paste(dateString, outPrefix, gsub("\\W", replacement = "-", cell), "subset", compName, "dge.csv", sep = "_"))
}
}
#output sample statistics
write.csv(sampleData, paste(dateString, outPrefix, "sampleData.csv", sep = "_"))
write.csv(bulkSampleData, paste(dateString, outPrefix, "bulkSampleData.csv", sep = "_"))
}