Seurat 一般流程处理方法记录

2022-04-18  本文已影响0人  倪桦
###############################################################################################
stdNor <- function(project,res=0.5,regress=F){
    library(Seurat)
    set.seed(123)
    all <- NormalizeData(project, normalization.method = "LogNormalize", scale.factor = 10000)
    all <- FindVariableFeatures(all, selection.method = "vst", nfeatures = 3000)
    all.genes <- unique(rownames(all))
    if (regress == T) {
        all <- ScaleData(all, features = all.genes,vars.to.regress = "nCount_RNA" )
    }else{ 
        all <- ScaleData(all, features = all.genes) 
    }
    all <- RunPCA(all, features = VariableFeatures(all), npcs = 40, verbose = F)
    all <- FindNeighbors(all, dims = 1:20)
    all <- FindClusters(all, resolution = res)
    #all <- FindClusters(all, resolution = c(0,.2,.5,.8,1,1.3,1.6))
    all <- RunUMAP(all, dims = 1:20)
    all <- AddMetaData(all,all@reductions$umap@cell.embeddings,col.name = colnames(all@reductions$umap@cell.embeddings))
    ggsave(plot = DimPlot(all ,reduction = 'umap', label = T,label.size = 3), "cluster.png", width = 23, height = 18,units="cm",dpi=600)
    return(all)
}
###############################################################################################
stdSct <- function(project,res=0.5,regress=F){
    library(Seurat)
    set.seed(123)
    all <- SCTransform(all, assay = "RNA", verbose = FALSE, variable.features.n=3000)
    all <- RunPCA(all,assay = "SCT",verbose = F)
    all <- FindNeighbors(all, reduction = "pca", dims = 1:20)
    all <- RunUMAP(all, reduction = "pca", dims = 1:20)
    all <- FindClusters(all, verbose = FALSE,resolution = res)
    all <- AddMetaData(all,all@reductions$umap@cell.embeddings,col.name = colnames(all@reductions$umap@cell.embeddings))
    ggsave(plot = DimPlot(all ,reduction = 'umap', label = T,label.size = 3), "cluster.png", width = 23, height = 18,units="cm",dpi=600)
    return(all)
}
###############################################################################################
std_rmDoublet <- function(project){
        set.seed(123)
    library(DoubletFinder)
    library(Seurat)
    EC <- NormalizeData(project)
    EC <- FindVariableFeatures(EC, selection.method = "vst", nfeatures = 3000)
    EC <- ScaleData(EC)
    EC <- RunPCA(EC)
    EC <- RunUMAP(EC, dims = 1:20)
    Find_doublet <- function(data){
        sweep.res.list <- paramSweep_v3(data, PCs = 1:20, sct = FALSE)
        sweep.stats <- summarizeSweep(sweep.res.list, GT = FALSE)
        bcmvn <- find.pK(sweep.stats)
        nExp_poi <- round(0.05*ncol(data))
        p <- as.numeric(as.vector(bcmvn[bcmvn$MeanBC==max(bcmvn$MeanBC),]$pK))
        data <- doubletFinder_v3(data, PCs = 1:20, pN = 0.25, pK = p, nExp = nExp_poi, reuse.pANN = FALSE, sct = FALSE)
        colnames(data@meta.data)[ncol(data@meta.data)] = "doublet_info"
        return(data)
    }
    EC <- Find_doublet(EC)
    EC <- subset(EC,subset=  doublet_info=="Singlet")
    return(EC)
}
###############################################################################################
std_harmony <- function(project,group="orig.ident",res=F){
    set.seed(123)
    library(Seurat)
    library(harmony)
    scRNA_harmony <- NormalizeData(project, normalization.method = "LogNormalize", scale.factor = 10000)
    scRNA_harmony <- FindVariableFeatures(scRNA_harmony, selection.method = "vst", nfeatures = 3000)
    if (regree == T) {
            scRNA_harmony <- ScaleData(scRNA_harmony,features = unique(rownames(scRNA_harmony)),vars.to.regress = "nCount_RNA" )
    }else{
            scRNA_harmony <- ScaleData(scRNA_harmony,features = unique(rownames(scRNA_harmony)) )
    }
    scRNA_harmony <- RunPCA(scRNA_harmony, npcs = 40, verbose = F)
    scRNA_harmony <- RunHarmony(scRNA_harmony, group , plot_convergence = F,dims.use = 1:20)
    scRNA_harmony = RunUMAP(scRNA_harmony, reduction = "harmony", dims = 1:20)
    scRNA_harmony = FindNeighbors(scRNA_harmony, reduction = "harmony",dims = 1:20)
    scRNA_harmony = FindClusters(scRNA_harmony, resolution =res)
    return(scRNA_harmony)
}

上一篇下一篇

猜你喜欢

热点阅读