Environment: R
BEER's latest version: https://github.com/jumphone/BEER/releases
-
Mar. 2021 ( V0.1.9 ): First version for Seurat 4.0.0
-
Feb. 2021 ( V0.1.8 ): Last version for Seurat 3.0.0
-
Nov. 2019 ( v0.1.7 ): In ".simple_combine(D1, D2, FILL=TRUE)", "FILL" can help users to keep genes that are expressed in only one condition (fill the matrix with “0”). Default "FILL" is FALSE
-
July 2019 ( v0.1.6 ): BEER can automatically adjust "GNUM" when cell number is small in some batch
-
July 2019 ( v0.1.5 ): "ComBat" is used to replace "regression" of "ScaleData" (ComBat is much faster)
-
July 2019 ( v0.1.4 ): Users can provide genes which need to be removed.
-
July 2019 ( v0.1.3 ): Users can use VISA to extract peaks of scATAC-seq.
-
...
Please see V. Batch-effect Removal Enhancement for details of "Enhancement".
#R >=3.5
install.packages('Seurat') # ==4.0.0
# Install ComBat:
if (!requireNamespace("BiocManager", quietly = TRUE))
install.packages("BiocManager")
BiocManager::install("sva")
BiocManager::install("limma")
# Users can use "BEER" by directly importing "BEER.R" on the github webpage:
source('https://raw.githubusercontent.com/jumphone/BEER/master/BEER.R')
# Or, download and import it:
source('BEER.R')
For batch-effect removal enhancement, please install BBKNN: https://github.com/Teichlab/bbknn
- I. Combine Two Batches
- II. Combine Multiple Batches
- III. UMAP-based Clustering
- IV. Combine scATAC-seq & scRNA-seq
- V. Batch-effect Removal Enhancement
- VI. Transfer Labels
- VII. Biological Interpretation
- VIII. QC before using BEER
library(reticulate)
use_python("/home/toolkit/local/bin/python3",required=T)
py_config()
Download demo data: https://github.com/jumphone/BEER/raw/master/DATA/demodata.zip
Please do basic quality control before using BEER (e.g. remove low-quality cells & genes).
For QC, please see: https://satijalab.org/seurat/v3.0/pbmc3k_tutorial.html
library(Seurat)
source('https://raw.githubusercontent.com/jumphone/BEER/master/BEER.R')
#source('BEER.R')
#Read 10X data: pbmc.data <- Read10X(data.dir = "../data/pbmc3k/filtered_gene_bc_matrices/hg19/")
#Load Demo Data (subset of GSE70630: MGH53 & MGH54)
#Download: https://github.com/jumphone/BEER/raw/master/DATA/demodata.zip
D1 <- read.table(unz("demodata.zip","DATA1_MAT.txt"), sep='\t', row.names=1, header=T)
D2 <- read.table(unz("demodata.zip","DATA2_MAT.txt"), sep='\t', row.names=1, header=T)
# "D1" & "D2" are UMI matrix (or FPKM, RPKM, TPM, PKM ...; Should not be gene-centric scaled data)
# Rownames of "D1" & "D2" are gene names
# Colnames of "D1" & "D2" are cell names
# There shouldn't be duplicated colnames in "D1" & "D2":
colnames(D1)=paste0('D1_', colnames(D1))
colnames(D2)=paste0('D2_', colnames(D2))
DATA=.simple_combine(D1,D2)$combine
# Users can use "DATA=.simple_combine(D1,D2, FILL=TRUE)$combine" to keep genes that are expressed in only one condition.
BATCH=rep('D2',ncol(DATA))
BATCH[c(1:ncol(D1))]='D1'
# Simple Quality Control (QC): check the number of sequenced genes
# PosN=apply(DATA,2,.getPos)
# USED=which(PosN>500 & PosN<4000)
# DATA=DATA[,USED]; BATCH=BATCH[USED]
mybeer=BEER(DATA, BATCH, GNUM=30, PCNUM=50, ROUND=1, GN=2000, SEED=1, COMBAT=TRUE, RMG=NULL)
# DATA: Expression matrix. Rownames are genes. Colnames are cell names.
# BATCH: A character vector. Length is equal to the "ncol(DATA)".
# GNUM: the number of groups in each batch (default: 30)
# PCNUM: the number of computated PCA subspaces (default: 50)
# ROUND: batch-effect removal strength, positive integer (default: 1)
# GN: the number of variable genes in each batch (default: 2000)
# RMG: genes need to be removed (default: NULL)
# COMBAT: use ComBat to adjust expression value(default: TRUE)
# Users can use "ReBEER" to adjust GNUM, PCNUM, ROUND, and RMG (it's faster than directly using BEER).
# mybeer <- ReBEER(mybeer, GNUM=30, PCNUM=50, ROUND=1, SEED=1, RMG=NULL)
# Check selected PCs
PCUSE=mybeer$select
COL=rep('black',length(mybeer$cor))
COL[PCUSE]='red'
plot(mybeer$cor,mybeer$lcor,pch=16,col=COL,
xlab='Rank Correlation',ylab='Linear Correlation',xlim=c(0,1),ylim=c(0,1))
Users can select PCA subspaces based on the distribution of "Rank Correlation" and "Linear Correlation".
# PCUSE=.selectUSE(mybeer, CUTR=0.7, CUTL=0.7, RR=0.5, RL=0.5)
pbmc_batch=CreateSeuratObject(counts = DATA, min.cells = 0, min.features = 0, project = "ALL")
[email protected]$batch=BATCH
pbmc_batch=FindVariableFeatures(object = pbmc_batch, selection.method = "vst", nfeatures = 2000)
VariableFeatures(object = pbmc_batch)
pbmc_batch <- NormalizeData(object = pbmc_batch, normalization.method = "LogNormalize", scale.factor = 10000)
pbmc_batch <- ScaleData(object = pbmc_batch, features = VariableFeatures(object = pbmc_batch))
pbmc_batch <- RunPCA(object = pbmc_batch, seed.use=123, npcs=50, features = VariableFeatures(object = pbmc_batch), ndims.print=1,nfeatures.print=1)
pbmc_batch <- RunUMAP(pbmc_batch, dims = 1:50, seed.use = 123,n.components=2)
DimPlot(pbmc_batch, reduction='umap', group.by='batch', pt.size=0.1)
pbmc <- mybeer$seurat
PCUSE <- mybeer$select
pbmc <- RunUMAP(object = pbmc, reduction='pca',dims = PCUSE, check_duplicates=FALSE)
DimPlot(pbmc, reduction='umap', group.by='batch', pt.size=0.1)
Download demo data: https://sourceforge.net/projects/beergithub/files/
source('https://raw.githubusercontent.com/jumphone/BEER/master/BEER.R')
#Load Demo Data (Oligodendroglioma, GSE70630)
#Download: https://sourceforge.net/projects/beergithub/files/
D1=readRDS('MGH36.RDS')
D2=readRDS('MGH53.RDS')
D3=readRDS('MGH54.RDS')
D4=readRDS('MGH60.RDS')
D5=readRDS('MGH93.RDS')
D6=readRDS('MGH97.RDS')
BATCH=c(rep('D1',ncol(D1)),
rep('D2',ncol(D2)),
rep('D3',ncol(D3)),
rep('D4',ncol(D4)),
rep('D5',ncol(D5)),
rep('D6',ncol(D6)) )
D12=.simple_combine(D1,D2)$combine
D34=.simple_combine(D3,D4)$combine
D56=.simple_combine(D5,D6)$combine
D1234=.simple_combine(D12,D34)$combine
D123456=.simple_combine(D1234,D56)$combine
DATA=D123456
rm(D1);rm(D2);rm(D3);rm(D4);rm(D5);rm(D6)
rm(D12);rm(D34);rm(D56);rm(D1234);rm(D123456)
# Simple Quality Control (QC): check the number of sequenced genes
# PosN=apply(DATA,2,.getPos)
# USED=which(PosN>500 & PosN<4000)
# DATA=DATA[,USED]; BATCH=BATCH[USED]
mybeer=BEER(DATA, BATCH, GNUM=30, PCNUM=50, ROUND=1, GN=2000, SEED=1, COMBAT=TRUE )
# Check selected PCs
PCUSE=mybeer$select
COL=rep('black',length(mybeer$cor))
COL[PCUSE]='red'
plot(mybeer$cor,mybeer$lcor,pch=16,col=COL,
xlab='Rank Correlation',ylab='Linear Correlation',xlim=c(0,1),ylim=c(0,1))
pbmc_batch=CreateSeuratObject(counts = DATA, min.cells = 0, min.features = 0, project = "ALL")
[email protected]$batch=BATCH
pbmc_batch=FindVariableFeatures(object = pbmc_batch, selection.method = "vst", nfeatures = 2000)
VariableFeatures(object = pbmc_batch)
pbmc_batch <- NormalizeData(object = pbmc_batch, normalization.method = "LogNormalize", scale.factor = 10000)
pbmc_batch <- ScaleData(object = pbmc_batch, features = VariableFeatures(object = pbmc_batch))
pbmc_batch <- RunPCA(object = pbmc_batch, seed.use=123, npcs=50, features = VariableFeatures(object = pbmc_batch), ndims.print=1,nfeatures.print=1)
pbmc_batch <- RunUMAP(pbmc_batch, dims = 1:50, seed.use = 123,n.components=2)
DimPlot(pbmc_batch, reduction='umap', group.by='batch', pt.size=0.1)
pbmc <- mybeer$seurat
PCUSE <- mybeer$select
pbmc <- RunUMAP(object = pbmc, reduction='pca',dims = PCUSE, check_duplicates=FALSE)
DimPlot(pbmc, reduction='umap', group.by='batch', pt.size=0.1)
VEC=pbmc@[email protected]
# Here, we use K-means to do the clustering
N=20
set.seed(123)
K=kmeans(VEC,centers=N)
CLUST=K$cluster
[email protected]$clust=as.character(CLUST)
DimPlot(pbmc, reduction='umap', group.by='clust', pt.size=0.5,label=TRUE)
# Or, manually select some cells
ppp=DimPlot(pbmc, reduction='umap', pt.size=0.5)
used.cells <- CellSelector(plot = ppp)
# Press "ESC"
markers <- FindMarkers(pbmc, ident.1=used.cells,only.pos=T)
head(markers, n=20)
Please install "Signac": https://satijalab.org/signac/
Download DEMO data: https://sourceforge.net/projects/beer-file/files/ATAC/ & https://satijalab.org/signac/articles/pbmc_vignette.html
source('https://raw.githubusercontent.com/jumphone/BEER/master/BEER.R')
#source('BEER.R')
library(Seurat)
library(Signac)
library(EnsDb.Hsapiens.v75)
counts <- Read10X_h5(filename = "./data/atac_v1_pbmc_10k_filtered_peak_bc_matrix.h5")
metadata <- read.csv(
file = "./data/atac_v1_pbmc_10k_singlecell.csv",
header = TRUE,
row.names = 1
)
chrom_assay <- CreateChromatinAssay(
counts = counts,
sep = c(":", "-"),
genome = 'hg19',
fragments = './data/atac_v1_pbmc_10k_fragments.tsv.gz',
min.cells = 10,
min.features = 200
)
pbmc.atac <- CreateSeuratObject(
counts = chrom_assay,
assay = "peaks",
meta.data = metadata
)
annotations <- GetGRangesFromEnsDb(ensdb = EnsDb.Hsapiens.v75)
seqlevelsStyle(annotations) <- "UCSC"
genome(annotations) <- "hg19"
Annotation(pbmc.atac) <- annotations
gene.activities <- GeneActivity(pbmc.atac)
pbmc.rna <- readRDS("./data/pbmc_10k_v3.rds")
D1=as.matrix(gene.activities)
D2=as.matrix(pbmc.rna@assays$RNA@counts)
colnames(D1)=paste0('ATAC_', colnames(D1))
colnames(D2)=paste0('RNA_', colnames(D2))
D1=.check_rep(D1)
D2=.check_rep(D2)
DATA=.simple_combine(D1,D2)$combine
BATCH=rep('RNA',ncol(DATA))
BATCH[c(1:ncol(D1))]='ATAC'
mybeer <- BEER(DATA, BATCH, GNUM=30, PCNUM=50, ROUND=1, GN=5000, SEED=1, COMBAT=TRUE)
saveRDS(mybeer, file='mybeer')
# Users can use "ReBEER" to adjust parameters
mybeer <- ReBEER(mybeer, GNUM=100, PCNUM=100, ROUND=3, SEED=1)
PCUSE=mybeer$select
#PCUSE=.selectUSE(mybeer, CUTR=0.8, CUTL=0.8, RR=0.5, RL=0.5)
COL=rep('black',length(mybeer$cor))
COL[PCUSE]='red'
plot(mybeer$cor,mybeer$lcor,pch=16,col=COL,
xlab='Rank Correlation',ylab='Linear Correlation',xlim=c(0,1),ylim=c(0,1))
pbmc_batch=CreateSeuratObject(counts = DATA, min.cells = 0, min.features = 0, project = "ALL")
[email protected]$batch=BATCH
pbmc_batch=FindVariableFeatures(object = pbmc_batch, selection.method = "vst", nfeatures = 2000)
VariableFeatures(object = pbmc_batch)
pbmc_batch <- NormalizeData(object = pbmc_batch, normalization.method = "LogNormalize", scale.factor = 10000)
pbmc_batch <- ScaleData(object = pbmc_batch, features = VariableFeatures(object = pbmc_batch))
pbmc_batch <- RunPCA(object = pbmc_batch, seed.use=123, npcs=50, features = VariableFeatures(object = pbmc_batch), ndims.print=1,nfeatures.print=1)
pbmc_batch <- RunUMAP(pbmc_batch, dims = 1:50, seed.use = 123,n.components=2)
DimPlot(pbmc_batch, reduction='umap', group.by='batch', pt.size=0.1)
pbmc <- mybeer$seurat
PCUSE=mybeer$select
pbmc <- RunUMAP(object = pbmc, reduction='pca',dims = PCUSE, check_duplicates=FALSE)
DimPlot(pbmc, reduction='umap', group.by='batch', pt.size=0.1)
[email protected]$celltype=rep(NA,length([email protected]$batch))
[email protected]$celltype[which([email protected]$batch=='RNA')][email protected]$celltype
DimPlot(pbmc, reduction='umap', group.by='celltype', pt.size=0.1,label=T)
saveRDS(mybeer, file='mybeer.final.RDS')
For further enhancement, please see V. Batch-effect Removal Enhancement.
Please install BBKNN: https://github.com/Teichlab/bbknn
This DEMO follows IV. Combine scATAC-seq & scRNA-seq
source('https://raw.githubusercontent.com/jumphone/BEER/master/BEER.R')
#source('BEER.R')
mybeer=readRDS('mybeer.final.RDS')
pbmc.rna <- readRDS("./data/pbmc_10k_v3.rds")
pbmc <- mybeer$seurat
PCUSE=c(1:ncol(pbmc@[email protected]))
pbmc=BEER.combat(pbmc) #Adjust PCs using ComBat
umap=BEER.bbknn(pbmc, PCUSE, NB=3, NT=10)
pbmc@[email protected]=umap
DimPlot(pbmc, reduction='umap', group.by='batch', pt.size=0.1,label=F)
pbmc <- mybeer$seurat
PCUSE=mybeer$select
pbmc=BEER.combat(pbmc) #Adjust PCs using ComBat
umap=BEER.bbknn(pbmc, PCUSE, NB=3, NT=10)
pbmc@[email protected]=umap
DimPlot(pbmc, reduction='umap', group.by='batch', pt.size=0.1,label=F)
saveRDS(pbmc, file='seurat.enh.RDS')
[email protected]$celltype=rep(NA,length([email protected]$batch))
[email protected]$celltype[which([email protected]$batch=='RNA')][email protected]$celltype
DimPlot(pbmc, reduction='umap', group.by='celltype', pt.size=0.1,label=T)
Please download beer_bbknn.py.
source('https://raw.githubusercontent.com/jumphone/BEER/master/BEER.R')
#source('BEER.R')
pbmc <- mybeer$seurat
pbmc=BEER.combat(pbmc) #Adjust PCs using ComBat
PCUSE = mybeer$select
used.pca = pbmc@[email protected][,PCUSE]
.writeTable(DATA=used.pca, PATH='used.pca.txt',SEP=',')
.writeTable([email protected]$batch, PATH='batch.txt',SEP=',')
Then, use "beer_bbknn.py" in your command line (please modify parameters in beer_bbknn.py):
python beer_bbknn.py
Finally, load the output of beer_bbknn.py and draw UMAP:
umap=read.table('bbknn_umap.txt',sep='\t',header=FALSE)
umap=as.matrix(umap)
rownames(umap)=rownames(pbmc@[email protected])
colnames(umap)=colnames(pbmc@[email protected])
pbmc@[email protected]=umap
DimPlot(pbmc, reduction='umap', group.by='batch', pt.size=0.1,label=F)
This DEMO follows V. Batch-effect Removal Enhancement
[email protected]$celltype=rep(NA,length([email protected]$batch))
[email protected]$celltype[which([email protected]$batch=='RNA')][email protected]$celltype
#DimPlot(pbmc, reduction='umap', group.by='celltype', pt.size=0.1,label=T)
#######
VEC=pbmc@[email protected]
set.seed(123)
N=150
K=kmeans(VEC,centers=N)
[email protected]$kclust=K$cluster
#DimPlot(pbmc, reduction='umap', group.by='kclust', pt.size=0.1,label=T)
[email protected]$transfer=rep(NA, length([email protected]$celltype))
TMP=cbind([email protected]$celltype, [email protected]$kclust)
KC=unique([email protected]$kclust)
i=1
while(i<=length(KC)){
this_kc=KC[i]
this_index=which([email protected]$kclust==this_kc)
this_tb=table([email protected]$celltype[this_index])
if(length(this_tb)!=0){
this_ct=names(this_tb)[which(this_tb==max(this_tb))[1]]
[email protected]$transfer[this_index]=this_ct}
i=i+1}
[email protected][email protected]$celltype
NA.index=which(is.na([email protected]$celltype))
[email protected]$tf.ct[NA.index][email protected]$transfer[NA.index]
######
RNA.cells=colnames(pbmc)[which([email protected]$batch=='RNA')]
ATAC.cells=colnames(pbmc)[which([email protected]$batch=='ATAC')]
library(ggplot2)
plot.all <- DimPlot(pbmc, reduction='umap', group.by='batch',
pt.size=0.1,label=F) + labs(title = "Batches")
plot.ct <- DimPlot(pbmc,reduction='umap', group.by='tf.ct',
pt.size=0.1,label=T) + labs(title = "CellType")
plot.rna <- DimPlot(pbmc, cells=RNA.cells,reduction='umap',
group.by='tf.ct', pt.size=0.1,label=T,plot.title='RNA.transfer') + labs(title = "RNA")
plot.atac <- DimPlot(pbmc, cells=ATAC.cells,reduction='umap',
group.by='tf.ct', pt.size=0.1,label=T,plot.title='ATAC.transfer') + labs(title = "ATAC")
CombinePlots(list(all=plot.all, ct=plot.ct, rna=plot.rna, atac=plot.atac))
If you want to visualize peak signals of any given cluster, please go to https://github.com/jumphone/VISA.
Please install "RITANdata" and "RITAN".
RITAN: https://bioconductor.org/packages/devel/bioc/vignettes/RITAN/inst/doc/enrichment.html
This DEMO follows IV. Combine scATAC-seq & scRNA-seq
library(RITANdata)
library(RITAN)
PCUSE <- mybeer$select
PCALL <- c(1:length(mybeer$cor))
PCnotUSE <- PCALL[which(!PCALL %in% PCUSE)]
LD=mybeer$seurat@[email protected]
GNAME=rownames(LD)
N=100
getPosAndNegTop <- function(x){
O=c(order(x)[1:N],order(x)[(length(x)-(N-1)):length(x)])
G=GNAME[O]
return(G)
}
GMAT=apply(LD,2,getPosAndNegTop)
colnames(GMAT)=paste0(colnames(GMAT),'_R_',round(mybeer$cor,1),"_L_",round(mybeer$lcor,1))
GMAT=toupper(GMAT)
GMAT=GMAT[,PCnotUSE]
#GMAT=GMAT[,PCUSE]
study_set=list()
TAG=colnames(GMAT)
i=1
while(i<=ncol(GMAT)){
study_set=c(study_set,list(GMAT[,i]))
i=i+1
}
names(study_set)=TAG
#names(geneset_list)
resources=c('KEGG_filtered_canonical_pathways','MSigDB_Hallmarks')
e <- term_enrichment_by_subset( study_set, q_value_threshold = 1e-5,
resources = resources,
all_symbols = cached_coding_genes )
plot( e, show_values = FALSE, label_size_y = 7, label_size_x = 7, cap=10 )
Download demo data: https://sourceforge.net/projects/beergithub/files/
source('https://raw.githubusercontent.com/jumphone/BEER/master/BEER.R')
#Load Demo Data (Oligodendroglioma, GSE70630)
#Download: https://sourceforge.net/projects/beergithub/files/
D1=readRDS('MGH36.RDS')
D2=readRDS('MGH53.RDS')
D3=readRDS('MGH54.RDS')
D4=readRDS('MGH60.RDS')
D5=readRDS('MGH93.RDS')
D6=readRDS('MGH97.RDS')
BATCH=c(rep('D1',ncol(D1)),
rep('D2',ncol(D2)),
rep('D3',ncol(D3)),
rep('D4',ncol(D4)),
rep('D5',ncol(D5)),
rep('D6',ncol(D6)) )
D12=.simple_combine(D1,D2)$combine
D34=.simple_combine(D3,D4)$combine
D56=.simple_combine(D5,D6)$combine
D1234=.simple_combine(D12,D34)$combine
D123456=.simple_combine(D1234,D56)$combine
DATA=D123456
rm(D1);rm(D2);rm(D3);rm(D4);rm(D5);rm(D6)
rm(D12);rm(D34);rm(D56);rm(D1234);rm(D123456)
pbmc <- CreateSeuratObject(counts = DATA, project = "pbmc3k", min.cells = 0, min.features = 0)
Idents(pbmc)=BATCH
[email protected]$batch=BATCH
pbmc <- subset(pbmc, subset = nFeature_RNA > 200 & nFeature_RNA < 2500 & percent.mt < 5)
Please fllow https://satijalab.org/seurat/v3.1/pbmc3k_tutorial.html to do Quality Control.
[email protected]$batch
DATA=as.matrix(pbmc@assays$RNA@counts[,which(colnames(pbmc@assays$RNA@counts) %in% colnames(pbmc@assays$RNA@data))])
Refer to II. Combine Multiple Batches
Feng Zhang, Yu Wu, Weidong Tian*; A novel approach to remove the batch effect of single-cell data, Cell Discovery, 2019, https://doi.org/10.1038/s41421-019-0114-x
Latest version: https://github.com/jumphone/BEER/releases
Manuscript version: https://github.com/jumphone/BEER/archive/0.0.2.zip
More tools & studies: https://fzhang.bioinfo-lab.com/
MIT License
Copyright (c) 2019 Zhang, Feng
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.