Five masking schemes that introduce zeros to scRNA-seq data
ruochenj / five_masking_schemes Goto Github PK
View Code? Open in Web Editor NEWFive masking schemes that introduce zeros to scRNA-seq data
Five masking schemes that introduce zeros to scRNA-seq data
`# five ways of introducing zeros to a matrix:
set.seed(1234)
zero_prop = 0.5
complete_mat <- matrix(rnorm(120,2,2), 3, 4)
dim(complete_mat)
complete_mat[complete_mat < 0.1] = 0
sce_ct_nzi <- complete_mat
zi_idx <- matrix(rbinom(dim(sce_ct_nzi)[1] * dim(sce_ct_nzi)[2], size = 1, prob = (1-zero_prop)), nrow = dim(sce_ct_nzi)[1], ncol = dim(sce_ct_nzi)[2])
sce_ct_zi <- sce_ct_nzi * zi_idx
sce_ct_zi1 <- sce_ct_zi
sum(sce_ct_zi1 == 0) / (dim(sce_ct_zi)[1] * dim(sce_ct_zi)[2])
sce_ct_nzi2 <- complete_mat
sce_ct_zi2 <- sce_ct_nzi2
idx_nz1 <- which(sce_ct_nzi2 > 0)
cutoff <- quantile(sce_ct_nzi2[idx_nz1], zero_prop)
idx_zero2 <- which(sce_ct_nzi2[idx_nz1] <= cutoff)
idx_zero2 <- sample(idx_nz1[idx_zero2], floor(zero_prop * length(idx_nz1)))
sce_ct_zi2[idx_zero2] <- 0
sum(sce_ct_zi2 == 0) / (dim(sce_ct_zi2)[1] * dim(sce_ct_zi2)[2])
sum(complete_mat == 0) / (dim(sce_ct_nzi)[1] * dim(sce_ct_nzi)[2])
zp_rec <- sum(sce_ct_zi2 == 0) / (dim(sce_ct_nzi)[1] * dim(sce_ct_zi)[2])
sce_ct_nzi3 <- complete_mat
sce_ct_zi3 <- sce_ct_nzi3
ZC_avg <- apply(sce_ct_nzi3, 1, FUN = function(x){
if(sum(x == 0) == length(x)){
return(c(0,0))
}else{
return(c(sum(x > 0), mean(x[x>0])))
}
})
f <- function(lambda){
sce_ct_zi3 <- apply(sce_ct_nzi3, 1, FUN = function(x){
if(sum(x == 0) == length(x)){
return(x)
}else{
nz_idx <- which(x > 0)
x[nz_idx] <- x[nz_idx] * (1-rbinom(length(nz_idx), size = 1, prob = exp(-lambdalog(mean(x[nz_idx]) + 1.01)^2)))
return(x)
}
})
return(sum(sce_ct_zi3 == 0) / (dim(sce_ct_nzi3)[1] * dim(sce_ct_nzi3)[2]) - zp_rec)
}
lambda = uniroot(f, c(0,20))$root
sce_ct_zi3 <- t(apply(sce_ct_nzi3, 1, FUN = function(x){
if(sum(x == 0) == length(x)){
return(x)
}else{
nz_idx <- which(x > 0)
x[nz_idx] <- x[nz_idx] * (1-rbinom(length(nz_idx), size = 1, prob = exp(-lambdalog(mean(x[nz_idx]) + 1.01)^2)))
return(x)
}
}))
sum(sce_ct_zi3 == 0) / (dim(sce_ct_nzi3)[1] * dim(sce_ct_nzi3)[2])
sce_ct_nzi4 <- complete_mat
sum(sce_ct_nzi4 == 0) / (dim(sce_ct_nzi4)[1] * dim(sce_ct_nzi4)[2])
sce_ct_zi4 <- t(apply(sce_ct_nzi4, 1, FUN = function(x){
idx_nz1 <- which(x > 0)
cutoff <- quantile(x[idx_nz1], zero_prop)
idx_zero2 <- which(x[idx_nz1] <= cutoff)
idx_zero2 <- sample(idx_nz1[idx_zero2], floor(zero_prop * length(idx_nz1)))
x[idx_zero2] <- 0
return(x)
}))
sum(sce_ct_zi4 == 0) / (dim(sce_ct_zi4)[1] * dim(sce_ct_zi4)[2])
sce_ct_nzi5 <- complete_mat
g <- function(lambda){
sce_ct_zi5 <- apply(sce_ct_nzi5, 1, FUN = function(x){
if(sum(x == 0) == length(x)){
return(x)
}else{
x[rank(x) <= (length(x) * exp(-lambdalog(mean(x[x > 0]))^2))] = 0
return(x)
}
})
return(sum(sce_ct_zi5 == 0) / (dim(sce_ct_nzi5)[1] * dim(sce_ct_nzi5)[2]) - zp_rec)
}
lambda = uniroot(g, c(0,200))$root
sce_ct_zi5 <- t(apply(sce_ct_nzi5, 1, FUN = function(x){
if(sum(x == 0) == length(x)){
return(x)
}else{
x[rank(x) <= (length(x) * exp(-lambdalog(mean(x[x > 0]))^2))] = 0
return(x)
}
}))
sum(sce_ct_zi5 == 0) / (dim(sce_ct_nzi5)[1] * dim(sce_ct_nzi5)[2])
sum(sce_ct_zi1 == 0) / (dim(sce_ct_nzi)[1] * dim(sce_ct_nzi)[2])
sum(sce_ct_zi2 == 0) / (dim(sce_ct_nzi)[1] * dim(sce_ct_nzi)[2])
sum(sce_ct_zi3 == 0) / (dim(sce_ct_nzi)[1] * dim(sce_ct_nzi)[2])
sum(sce_ct_zi4 == 0) / (dim(sce_ct_nzi)[1] * dim(sce_ct_nzi)[2])
sum(sce_ct_zi5 == 0) / (dim(sce_ct_nzi)[1] * dim(sce_ct_nzi)[2])
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.