Compare commits
3 Commits
f4b6748210
...
55b27c57a6
Author | SHA1 | Date | |
---|---|---|---|
|
55b27c57a6 | ||
|
a620b4e5d2 | ||
|
0427dcc6ad |
134
R/FastCAR_Base.R
134
R/FastCAR_Base.R
@ -13,6 +13,9 @@
|
|||||||
library(Matrix)
|
library(Matrix)
|
||||||
library(Seurat)
|
library(Seurat)
|
||||||
library(qlcMatrix)
|
library(qlcMatrix)
|
||||||
|
library(pheatmap)
|
||||||
|
library(ggplot2)
|
||||||
|
library(gridExtra)
|
||||||
|
|
||||||
###############################################################################
|
###############################################################################
|
||||||
|
|
||||||
@ -46,12 +49,11 @@ remove.background = function(geneCellMatrix, ambientRNAprofile){
|
|||||||
}
|
}
|
||||||
##############
|
##############
|
||||||
|
|
||||||
determine.background.to.remove = function(fullCellMatrix, cellMatrix, emptyDropletCutoff, contaminationChanceCutoff){
|
determine.background.to.remove = function(fullCellMatrix, emptyDropletCutoff, contaminationChanceCutoff){
|
||||||
|
|
||||||
# determines the highest expression value found for every gene in the droplets that we're sure don't contain cells
|
# determines the highest expression value found for every gene in the droplets that we're sure don't contain cells
|
||||||
backGroundMax = as.vector(rowMax(fullCellMatrix[,Matrix::colSums(fullCellMatrix) < emptyDropletCutoff]))
|
backGroundMax = as.vector(qlcMatrix::rowMax(fullCellMatrix[,Matrix::colSums(fullCellMatrix) < emptyDropletCutoff]))
|
||||||
names(backGroundMax) = rownames(fullCellMatrix)
|
names(backGroundMax) = rownames(fullCellMatrix)
|
||||||
nCell = ncol(cellMatrix)
|
|
||||||
|
|
||||||
# droplets that are empty but not unused barcodes, unused barcodes have zero reads assigned to them.
|
# droplets that are empty but not unused barcodes, unused barcodes have zero reads assigned to them.
|
||||||
nEmpty = table((Matrix::colSums(fullCellMatrix) < emptyDropletCutoff) &(Matrix::colSums(fullCellMatrix) > 0))[2]
|
nEmpty = table((Matrix::colSums(fullCellMatrix) < emptyDropletCutoff) &(Matrix::colSums(fullCellMatrix) > 0))[2]
|
||||||
@ -80,6 +82,93 @@ read.full.matrix = function(fullFolderLocation){
|
|||||||
return(fullMatrix)
|
return(fullMatrix)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
getExpressionThreshold = function(gene, expMat, percentile){
|
||||||
|
orderedExpression = expMat[gene, order(expMat[gene,], decreasing = TRUE)]
|
||||||
|
CS = cumsum(orderedExpression)
|
||||||
|
return(orderedExpression[which(CS/max(CS) > percentile)[1]])
|
||||||
|
}
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
celltypeSpecificityScore = function(gene, expMat){
|
||||||
|
CS = cumsum(expMat[gene, order(expMat[gene,], decreasing = TRUE)])
|
||||||
|
return((sum(CS/max(CS))/ncol(expMat)) )
|
||||||
|
}
|
||||||
|
|
||||||
|
###############################################################################
|
||||||
|
describe.correction.effect = function (allExpression, cellExpression, startPos, stopPos, byLength, contaminationChanceCutoff){
|
||||||
|
|
||||||
|
# Make somewhere to store all the data that needs to be returned to the user
|
||||||
|
ambientScoreProfileOverview = data.frame(row.names = rownames(cellExpression))
|
||||||
|
|
||||||
|
# do a quick first run to see which genes get corrected at the highest setting
|
||||||
|
ambientProfile = determine.background.to.remove(allExpression, cellExpression, stopPos, contaminationChanceCutoff)
|
||||||
|
genelist = names(ambientProfile[ambientProfile > 0])
|
||||||
|
|
||||||
|
print(paste0("Calculating cell expression score for ", length(genelist), " genes"))
|
||||||
|
ctsScores = vector(mode = "numeric", length = nrow(cellExpression))
|
||||||
|
names(ctsScores) = rownames(cellExpression)
|
||||||
|
|
||||||
|
for(gene in genelist){
|
||||||
|
ctsScores[gene] = celltypeSpecificityScore(gene, cellExpression)
|
||||||
|
}
|
||||||
|
|
||||||
|
# loop over every threshold to test
|
||||||
|
# Starts at the highest value so
|
||||||
|
for(emptyDropletCutoff in seq(from = startPos, to = stopPos, by = byLength)){
|
||||||
|
ambientProfile = determine.background.to.remove(allExpression, cellExpression, emptyDropletCutoff, contaminationChanceCutoff)
|
||||||
|
|
||||||
|
print(paste0("Profiling at cutoff ", emptyDropletCutoff))
|
||||||
|
|
||||||
|
ambientScoreProfile = data.frame(ambientProfile, ctsScores)
|
||||||
|
#ambientScoreProfile = ambientScoreProfile[ambientScoreProfile$ctsScores > 0.85, ]
|
||||||
|
ambientScoreProfile$stillOverAmbient = 0
|
||||||
|
ambientScoreProfile$belowCellexpression = 0
|
||||||
|
|
||||||
|
genesToCheck = names(ambientProfile[ambientProfile > 0])
|
||||||
|
if(exists("overAmbientGenes")){
|
||||||
|
genesToCheck = overAmbientGenes
|
||||||
|
}
|
||||||
|
|
||||||
|
print(paste0("Calculating profiles for ", length(genesToCheck), " genes"))
|
||||||
|
|
||||||
|
for(gene in genesToCheck){
|
||||||
|
expThresh = getExpressionThreshold(gene, cellExpression, 0.95)
|
||||||
|
|
||||||
|
if(emptyDropletCutoff == startPos){
|
||||||
|
ambientScoreProfile[gene, "belowCellexpression"] = table(cellExpression[gene,] > 0 & cellExpression[gene,] < expThresh)["TRUE"]
|
||||||
|
}
|
||||||
|
ambientScoreProfile[gene, "stillOverAmbient"] = table(cellExpression[gene,] > ambientScoreProfile[gene, "ambientProfile"] & cellExpression[gene,] < expThresh)["TRUE"]
|
||||||
|
}
|
||||||
|
|
||||||
|
ambientScoreProfile[is.na(ambientScoreProfile)] = 0
|
||||||
|
ambientScoreProfile$contaminationChance = ambientScoreProfile$stillOverAmbient / ambientScoreProfile$belowCellexpression
|
||||||
|
ambientScoreProfile[is.na(ambientScoreProfile)] = 0
|
||||||
|
|
||||||
|
# Genes that have already been completely removed don't need to be checked at higher resolution
|
||||||
|
overAmbientGenes = rownames(ambientScoreProfile[ambientScoreProfile$stillOverAmbient > 0,])
|
||||||
|
|
||||||
|
ambientScoreProfile[genelist,"AmbientCorrection"]
|
||||||
|
|
||||||
|
ambientScoreProfileOverview[names(ctsScores), "ctsScores"] = ctsScores
|
||||||
|
if(emptyDropletCutoff == startPos){
|
||||||
|
ambientScoreProfileOverview[rownames(ambientScoreProfile), "belowCellexpression"] = ambientScoreProfile$belowCellexpression
|
||||||
|
}
|
||||||
|
ambientScoreProfileOverview[rownames(ambientScoreProfile), paste0("stillOverAmbient", as.character(emptyDropletCutoff))] = ambientScoreProfile$stillOverAmbient
|
||||||
|
ambientScoreProfileOverview[rownames(ambientScoreProfile), paste0("AmbientCorrection", as.character(emptyDropletCutoff))] = ambientScoreProfile$ambientProfile
|
||||||
|
}
|
||||||
|
ambientScoreProfileOverview = ambientScoreProfileOverview[!is.na(ambientScoreProfileOverview$ctsScores),]
|
||||||
|
ambientScoreProfileOverview[is.na(ambientScoreProfileOverview)] = 0
|
||||||
|
|
||||||
|
ambientScoreProfileOverview[,paste0("Threshold_", seq(from = startPos, to = stopPos, by = byLength))] = ambientScoreProfileOverview[,paste0("stillOverAmbient", as.character(seq(from = startPos, to = stopPos, by = byLength)))] / ambientScoreProfileOverview$belowCellexpression
|
||||||
|
ambientScoreProfileOverview[is.na(ambientScoreProfileOverview)] = 0
|
||||||
|
|
||||||
|
ambientScoreProfileOverview[,paste0("contaminationChance", as.character(seq(from = startPos, to = stopPos, by = byLength)))] = ambientScoreProfileOverview[,paste0("stillOverAmbient", as.character(seq(from = startPos, to = stopPos, by = byLength)))] / ambientScoreProfileOverview$belowCellexpression
|
||||||
|
|
||||||
|
return(ambientScoreProfileOverview)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
##############
|
##############
|
||||||
# Turns out that cellranger output looks different from WriteMM output and Read10X can't read the latter
|
# Turns out that cellranger output looks different from WriteMM output and Read10X can't read the latter
|
||||||
# TODO
|
# TODO
|
||||||
@ -100,14 +189,15 @@ read.full.matrix = function(fullFolderLocation){
|
|||||||
# }
|
# }
|
||||||
|
|
||||||
# describe the number of genes identified in the background
|
# describe the number of genes identified in the background
|
||||||
# and the number of genes failing the contaminiation chance threshold
|
# and the number of genes failing the contamination chance threshold
|
||||||
#
|
#
|
||||||
describe.ambient.RNA.sequence = function(fullCellMatrix, start, stop, by, contaminationChanceCutoff){
|
describe.ambient.RNA.sequence = function(fullCellMatrix, start, stop, by, contaminationChanceCutoff){
|
||||||
|
cutoffValue = seq(start, stop, by)
|
||||||
genesInBackground = vector(mode = "numeric", length = length(seq(start, stop, by)))
|
genesInBackground = vector(mode = "numeric", length = length(seq(start, stop, by)))
|
||||||
genesContaminating = vector(mode = "numeric", length = length(seq(start, stop, by)))
|
genesContaminating = vector(mode = "numeric", length = length(seq(start, stop, by)))
|
||||||
nEmptyDroplets = vector(mode = "numeric", length = length(seq(start, stop, by)))
|
nEmptyDroplets = vector(mode = "numeric", length = length(seq(start, stop, by)))
|
||||||
|
|
||||||
ambientDescriptions = data.frame(nEmptyDroplets, genesInBackground, genesContaminating)
|
ambientDescriptions = data.frame(nEmptyDroplets, genesInBackground, genesContaminating, cutoffValue)
|
||||||
rownames(ambientDescriptions) = seq(start, stop, by)
|
rownames(ambientDescriptions) = seq(start, stop, by)
|
||||||
for(emptyCutoff in seq(start, stop, by)){
|
for(emptyCutoff in seq(start, stop, by)){
|
||||||
nEmpty = table((Matrix::colSums(fullCellMatrix) < emptyCutoff) &(Matrix::colSums(fullCellMatrix) > 0))[2]
|
nEmpty = table((Matrix::colSums(fullCellMatrix) < emptyCutoff) &(Matrix::colSums(fullCellMatrix) > 0))[2]
|
||||||
@ -125,23 +215,31 @@ describe.ambient.RNA.sequence = function(fullCellMatrix, start, stop, by, contam
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
plot.correction.effect.chance = function(correctionProfile){
|
||||||
|
pheatmap(correctionProfile[correctionProfile[,3] > 0, colnames(correctionProfile)[grep("contaminationChance", colnames(correctionProfile))]],
|
||||||
|
cluster_cols = FALSE,
|
||||||
|
treeheight_row = 0,
|
||||||
|
main = "Chance of affecting DE analyses")
|
||||||
|
}
|
||||||
|
|
||||||
|
plot.correction.effect.removal = function(correctionProfile){
|
||||||
|
pheatmap(correctionProfile[(correctionProfile[,3] > 0) ,colnames(correctionProfile)[grep("AmbientCorrection", colnames(correctionProfile))]],
|
||||||
|
cluster_cols = FALSE, treeheight_row = 0,
|
||||||
|
main = "Counts removed from each cell")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
plot.ambient.profile = function(ambientProfile){
|
plot.ambient.profile = function(ambientProfile){
|
||||||
par(mfrow = c(3,1))
|
|
||||||
plot(as.numeric(rownames(ambientProfile)), ambientProfile[,1],
|
|
||||||
main = "Total number of empty droplets at cutoffs",
|
|
||||||
xlab = "empty droplet UMI cutoff",
|
|
||||||
ylab = "Number of empty droplets")
|
|
||||||
|
|
||||||
plot(as.numeric(rownames(ambientProfile)), ambientProfile[,2],
|
p1 = ggplot(ambientProfile, aes(x=cutoffValue, y=genesInBackground)) + geom_point()
|
||||||
main = "Number of genes in ambient RNA",
|
|
||||||
xlab = "empty droplet UMI cutoff",
|
|
||||||
ylab = "Genes in empty droplets")
|
p2= ggplot(ambientProfile, aes(x=cutoffValue, y=genesContaminating)) + geom_point()
|
||||||
|
|
||||||
|
p3 = ggplot(ambientProfile, aes(x=cutoffValue, y=nEmptyDroplets)) + geom_point()
|
||||||
|
|
||||||
|
grid.arrange(p1, p2, p3, nrow = 3)
|
||||||
|
|
||||||
plot(as.numeric(rownames(ambientProfile)), ambientProfile[,3],
|
|
||||||
main = "number of genes to correct",
|
|
||||||
xlab = "empty droplet UMI cutoff",
|
|
||||||
ylab = "Genes identified as contamination")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# I noticed that the number of genes removed tends to even out over time
|
# I noticed that the number of genes removed tends to even out over time
|
||||||
|
Loading…
Reference in New Issue
Block a user