forked from sdparekh/zUMIs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbarcodeIDFUN.R
More file actions
executable file
·170 lines (151 loc) · 5.85 KB
/
barcodeIDFUN.R
File metadata and controls
executable file
·170 lines (151 loc) · 5.85 KB
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
setDownSamplingOption<-function( down ,bccount, filename=NULL){
if(down!="0") {
subsample=TRUE
if(grepl(pattern = ",",x = down)==TRUE){
subsample.splits <- t(sapply(strsplit(down,split = ",")[[1]],
function(x){
if(grepl("-",x)){
as.numeric(strsplit(x,split="-")[[1]])
}else{ as.numeric(rep(x,2)) }
}))
}else{
subn=as.numeric(down)
subsample.splits <- matrix( c(subn,subn),1,2 )
}
if(any(subsample.splits[,1] > max(bccount$n))){
print("Warning! None of the barcodes had enough reads for the following requested downsampling:")
print(row.names(subsample.splits)[which(subsample.splits[,1] > max(bccount$n))])
subsample.splits <- subsample.splits[which(subsample.splits[,1] <= max(bccount$n)),]
}
}else{
mads<-calcMADS(bccount)
plotReadCountSelection(bccount,mads, filename=filename)
subsample.splits <- matrix( mads, 1, 2)
}
colnames(subsample.splits)<-c("minR","maxR")
return( subsample.splits )
}
##cellbarcode ordering functions
.FindBCcut <- function(bccount){
suppressMessages(require(mclust))
tmp<-mclust::Mclust(log10(bccount$n), modelNames = c("E","V"))
ss <- ifelse(tmp$modelName=="E",1,tmp$G)
mm<-tmp$parameters$mean[tmp$G]
va<-tmp$parameters$variance$sigmasq[ss]
cut<-10^(qnorm(0.01, m=mm,sd=sqrt(va)))
return(cut)
}
.cellBarcode_unknown <- function( bccount, outfilename=NULL) {
bccount[ ,cs:=cumsum(as.numeric(n))]
cut <- .FindBCcut(bccount)
nkeep<-bccount[n>=cut][,list(s=.N)]
if(nkeep<10){
print("Warning! Adaptive BC selection gave < 10 cells so I will try to use top 100 cells!")
if(nrow(bccount)<100){
print("Less than 100 barcodes present, will continue with all barcodes...")
bccount[1:nrow(bccount),keep:=TRUE]
}else{
bccount[1:100,keep:=TRUE]
}
}else{
bccount[n>=cut,keep:=TRUE]
print(paste(nkeep," barcodes detected.",sep=""))
}
#Plotting
if(is.null(outfilename)==FALSE){
p_dens<-ggplot2::ggplot(bccount,aes(x=log10(n)))+
geom_density()+theme_classic()+
geom_vline(xintercept = log10(min(bccount$n[bccount$keep])),col="#56B4E9",size=1.5)+
xlab("log10(Number of reads per cell)")+ylab("Density")+
ggtitle("Cells right to the blue line are selected")+
theme(axis.text = element_text(size=12),axis.title = element_text(size=13),
plot.title = element_text(hjust=0.5,vjust=0.5,size=13))
p_bc<-ggplot2::ggplot(bccount,aes(y=cs,x=cellindex,color=keep))+
geom_point(size=2)+xlab("Cell Index")+
ylab("Cumulative number of reads")+
ggtitle("Detected cells are highlighted in blue")+
theme_classic()+theme(legend.position = "none",legend.text = element_text(size=15),
legend.title = element_blank(),axis.text = element_text(size=12),
axis.title = element_text(size=13),
plot.title = element_text(hjust=0.5,vjust=0.5,size=13))
bcplot <- cowplot::plot_grid(p_dens,p_bc,labels = c("a","b"))
ggplot2::ggsave(bcplot,filename=outfilename,
width = 10,height = 4)
}
bccount[,cs:=NULL]
return( bccount[keep==TRUE,XC] )
}
.cellBarcode_number <- function( bccount, bcNumber){
if(bcNumber <= nrow(bccount)){
bccount[1:bcNumber,keep:=TRUE]
}else{
print("Warning! The data contains fewer barcodes than requested so I will try to use top 100 cells!")
if(nrow(bccount)<100){
print("Less than 100 barcodes present, will continue with all barcodes...")
bccount[1:nrow(bccount),keep:=TRUE]
}else{
bccount[1:100,keep:=TRUE]
}
}
return(bccount[keep==TRUE,XC])
}
.cellBarcode_known <- function( bccount, bcfile ){
bc<-read.table(bcfile,header = F,stringsAsFactors = F)$V1
if( any( bc %in% bccount$XC ) ){
bccount[XC %in% bc,keep:=TRUE]
}else{
print("Warning! None of the annotated barcodes were detected.")
if(nrow(bccount)<100){
print("Less than 100 barcodes present, will continue with all barcodes...")
bccount[1:nrow(bccount),keep:=TRUE]
}else{
print("Continuing with top 100 barcodes instead...")
bccount[1:100,keep:=TRUE]
}
}
return(bccount[keep==TRUE,XC])
}
#bccount needs to be read
cellBC<-function(bcfile, bcnum, bccount_file, outfilename=NULL){
bccount<-data.table::fread( bccount_file )
names(bccount)<-c("XC","n")
bccount <- bccount[,list(n=sum(n)),by=XC]
bccount<-bccount[n>=opt$barcodes$nReadsperCell][order(-n)][,cellindex:=1:(.N)][,keep:=FALSE]
if(is.null(bcfile)==FALSE){
bc <- .cellBarcode_known( bccount, bcfile=bcfile )
}else if (is.null(bcnum)==FALSE){
bc <- .cellBarcode_number(bccount ,bcNumber=bcnum )
}else{
bc <- .cellBarcode_unknown( bccount, outfilename = outfilename )
}
bc[is.na(bc)==F]
noReads<-bccount[,list(sum(n)),by=keep][keep==FALSE,V1]
print(paste(noReads, "reads were assigned to barcodes that do not correspond to intact cells." ))
return(bccount[XC %in% bc][,keep:=NULL][order(-n)])
}
calcMADS<-function(bccount){
mr <- round(median(bccount$n),digits = 0)
dev<- 3*median(abs(log10(bccount$n)-median(log10(bccount$n))))
MAD_up <- 10^(log10(mr) + dev)
MAD_low <- 10^(log10(mr) - dev)
#check that low is not under 0
if(MAD_low<0){
MAD_low <- 0
}
MAD_up <- round(MAD_up,digits = 0)
MAD_low <- round(MAD_low,digits = 0)
retmat<-matrix( c(MAD_low,MAD_up),1,2)
colnames(retmat)<-c("minR","maxR")
rownames(retmat)<-"MADs"
return(retmat)
}
plotReadCountSelection<-function(bccount , mads, filename){
MAD_up=mads[1,2]
MAD_low=mads[1,1]
pdf(file=filename)
barplot(bccount$n,ylab="Number of reads",xlab="Cell Barcodes",
ylim = c(0,1.1*max(c(bccount$n,MAD_up))))
abline(h=MAD_low,col="red")
abline(h=MAD_up,col="red")
dev.off()
}