R读取单细胞数据

setwd('~/Desktop')
Path=getwd()
if(dir.exists(Path)){
  path=paste(Path,"/00_scRNA-seq_analysis/00_rawdt",sep='')
  if(!dir.exists(path)){
    dir.create(path,recursive=TRUE)
  }
}

# R1 ** read 10X **
# 10Xgenomics:barcodes.tsv.gz;features.tsv.gz;matrix.mtx.gz

#*----------------------multiple file-----------------------*#
dt.dir=paste(Path,"/00_scRNA-seq_analysis/00_rawdt",sep='')
#dirs=list.dirs(dt.dir, recursive = FALSE)
sam_name=list.files(dt.dir)

sce_List=lapply(sam_name,function(proj){ 
  print(proj)
  folder=file.path(dt.dir, proj) 
  sce=CreateSeuratObject(counts=Read10X(data.dir=folder), 
                         project=proj,
                         min.cells=5, 
                         min.features=200)
  return(sce)
})

#sam_name = c('Control1','Treat1','Treat2')
sce_all = merge(x = sce_List[[1]], 
                y = sce_List[-1], 
                add.cell.ids = sam_name,
                merge.data = TRUE)
sce_combined <- JoinLayers(sce_all)

filename=paste(Path,"/00_scRNA-seq_analysis/00_rawdt/sce_all_rawdt.rds",sep='')
saveRDS(sce_combined ,file=filename)

#*----------------------single file-----------------------*#
dt.dir=paste(Path,"/00_scRNA-seq_analysis/00_rawdt",sep='')
sam_name=list.files(dt.dir)

sce = Read10X(data.dir = paste(dt.dir,sam_name,sep = '/'))
sce_all = CreateSeuratObject(counts = sce, 
                              project = sam_name,
                              min.cells = 20, 
                              min.features = 200)

filename=paste(Path,"/00_scRNA-seq_analysis/00_rawdt/sce.",sam_name,".rawdt.rds",sep='')
saveRDS(sce_all,file=filename)

# R2 ** Expression matrix **
# expression_matrix.txt.gz
dt.dir=paste(Path,"/00_scRNA-seq_analysis/00_rawdt",sep='')
#dirs=list.dirs(dt.dir, recursive = FALSE)
sam_name=list.files(dt.dir)

sce_List = lapply(sam_name,function(proj){
  print(proj) 
  folder=file.path(dt.dir, proj) 
  exp_mat=fread(folder,data.table = F)
  EM=exp_mat[,-1]
  rownames(EM)=exp_mat[,1]
  sce=CreateSeuratObject(EM, project=gsub('_.*$','',proj))
  
  return(sce)
})

#names(sceList)  
sce_all = merge(x = sceList[[1]], 
                y = sceList[ -1 ],
                add.cell.ids = sam_name) 

filename=paste(Path,"/00_scRNA-seq_analysis/00_rawdt/sce_all_rawdt.rds",sep='')
saveRDS(sce_all,file=filename)

#*----------------------single file-----------------------*#
dt.dir=paste(Path,"/00_scRNA-seq_analysis/00_rawdt",sep='')
#dirs=list.dirs(dt.dir, recursive = FALSE)
sam_name=list.files(dt.dir)

folder=file.path(dt.dir, sam_name) 
exp_mat=fread(folder,data.table = F)
EM=exp_mat[,-1]
rownames(EM)=exp_mat[,1]
sce_all=CreateSeuratObject(EM, project=gsub('_.*$','',sam_name))
  
filename=paste(Path,"/00_scRNA-seq_analysis/00_rawdt/sce_all_rawdt.rds",sep='')
saveRDS(sce_all,file=filename)

# R3 ** h5 **
dt.file = paste(Path,"/00_scRNA-seq_analysis/*_h5.h5",sep='')
# list.files(dt.file)
dt = Read10X_h5(filename = dt.file)
sce_seurat = CreateSeuratObject(counts = dt)

# R4 ** h5ad **
library(SeuratDisk)
dt.file = paste(Path,"/00_scRNA-seq_analysis/*_raw_counts.h5ad",sep='')
Convert(dt.file, "h5seurat", overwrite = TRUE, assay = "RNA")
sce_seurat=LoadH5Seurat(paste(Path,"/00_scRNA-seq_analysis/*_raw_counts.h5seurat",sep=''))
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容