【单细胞转录组】使用Seurat的Read10X函数读取10x文件时为什么有格式要求

2023-12-26 14:20

本文主要是介绍【单细胞转录组】使用Seurat的Read10X函数读取10x文件时为什么有格式要求,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

非模式物种在分析时可能会遇到各种各样的问题,但是也能学习到一些不太会注意到的小细节。比如我在分析数据的时候,使用seurat查找差异基因后绘图,这里展示一下某个cluster的结果:

可以注意到出来Os号外还有一些看起来奇奇怪怪的基因,我本来以为是注释文件的问题。经师姐提醒,我去检查了一下features.tsv这个文件中的信息,发现这些长得奇奇怪怪的基因确实是存在的,而且是在第二列

也可以在矩阵中检查。比如

matrix <- PRO@assays$RNA@data
tail(rownames(matrix),200)

说明在创建seurat对象的时候features信息自动读取的是第二列

使用Read10X函数,可以直接读取三个文件,比如

pbmc.data <- Read10X(data.dir = "../data/pbmc3k/filtered_gene_bc_matrices/hg19/")

去看Read10X这个函数,同样会注意到gene.column这里默认是2,再次说明读取的是features.tsv这个文件的第二列。

这里要回顾一下cellranger(我目前是v7,提到版本是如果是很早的版本结果可能还不一样)输出的三个文件都是什么:

  • barcodes.tsv.gz
    一列,细胞信息(列号)

  • features.tsv.gz
    三列,基因信息(行号)

  • matrix.mtx.gz
    三列,
    第一列:行号
    第二列:列号
    第三列:表达量
    代表某一行的基因在某一列的细胞中表达量是多少

了解到这些信息后,可以知道Read10X的功能,其实就是读取了一个矩阵,只是这个矩阵分成了三个部分:所有的细胞、所有的基因,以及它们的表达量信息。使用Read10X后这三个文件被还原成类似以下的样子:

并且要注意,

  • 基因默认是读取features.tsv.gz文件中的第二列(gene.column)
  • 默认会过滤重复基因,只保留一个高丰度的(unique.features)

所以对于一些特殊物种,为了其它下游分析不出现bug(比如富集分析默认用的是geneid,但你在这里可能读的是symbol这一列),因此可以默认在读取矩阵的时候注意一下gene.column读的是哪一列,比如可以默认读第一列

reader_10x = Read10X(data.dir=dir, gene.column = 1)

这里再贴一下Read10X的源码

function (data.dir, gene.column = 2, cell.column = 1, unique.features = TRUE, strip.suffix = FALSE) 
{full.data <- list()has_dt <- requireNamespace("data.table", quietly = TRUE) && requireNamespace("R.utils", quietly = TRUE)for (i in seq_along(along.with = data.dir)) {run <- data.dir[i]if (!dir.exists(paths = run)) {stop("Directory provided does not exist")}
#在data.dir路径下获得barcode、gene、features、matrix地址,并检查是否存在genes.tsv这个文件barcode.loc <- file.path(run, "barcodes.tsv")gene.loc <- file.path(run, "genes.tsv")features.loc <- file.path(run, "features.tsv.gz")matrix.loc <- file.path(run, "matrix.mtx")pre_ver_3 <- file.exists(gene.loc)if (!pre_ver_3) {addgz <- function(s) {return(paste0(s, ".gz"))}barcode.loc <- addgz(s = barcode.loc)matrix.loc <- addgz(s = matrix.loc)}if (!file.exists(barcode.loc)) {stop("Barcode file missing. Expecting ", basename(path = barcode.loc))}if (!pre_ver_3 && !file.exists(features.loc)) {stop("Gene name or features file missing. Expecting ", basename(path = features.loc))}if (!file.exists(matrix.loc)) {stop("Expression matrix file missing. Expecting ", basename(path = matrix.loc))}
#读取表达量data <- readMM(file = matrix.loc)if (has_dt) {
#读取细胞信息cell.barcodes <- as.data.frame(data.table::fread(barcode.loc, header = FALSE))}else {cell.barcodes <- read.table(file = barcode.loc, header = FALSE, sep = "\t", row.names = NULL)}if (ncol(x = cell.barcodes) > 1) {cell.names <- cell.barcodes[, cell.column]}else {cell.names <- readLines(con = barcode.loc)}if (all(grepl(pattern = "\\-1$", x = cell.names)) & strip.suffix) {
#对细胞信息进行修改,矩阵的colnames添加上细胞信息cell.names <- as.vector(x = as.character(x = sapply(X = cell.names, FUN = ExtractField, field = 1, delim = "-")))}if (is.null(x = names(x = data.dir))) {if (length(x = data.dir) < 2) {colnames(x = data) <- cell.names}else {colnames(x = data) <- paste0(i, "_", cell.names)}}else {colnames(x = data) <- paste0(names(x = data.dir)[i], "_", cell.names)}if (has_dt) {
#读取基因信息,并且命名genes.tsv/features.tsv.gz都是可以读取的feature.names <- as.data.frame(data.table::fread(ifelse(test = pre_ver_3, yes = gene.loc, no = features.loc), header = FALSE))}else {feature.names <- read.delim(file = ifelse(test = pre_ver_3, yes = gene.loc, no = features.loc), header = FALSE, stringsAsFactors = FALSE)}if (any(is.na(x = feature.names[, gene.column]))) {warning("Some features names are NA. Replacing NA names with ID from the opposite column requested", call. = FALSE, immediate. = TRUE)na.features <- which(x = is.na(x = feature.names[, gene.column]))replacement.column <- ifelse(test = gene.column == 2, yes = 1, no = 2)
#删除NA,并且选择读取第一列还是第二列feature.names[na.features, gene.column] <- feature.names[na.features, replacement.column]}if (unique.features) {fcols = ncol(x = feature.names)if (fcols < gene.column) {stop(paste0("gene.column was set to ", gene.column, " but feature.tsv.gz (or genes.tsv) only has ", fcols, " columns.", " Try setting the gene.column argument to a value <= to ", fcols, "."))}
#去重,矩阵的rownames添加上基因信息rownames(x = data) <- make.unique(names = feature.names[, gene.column])}if (ncol(x = feature.names) > 2) {data_types <- factor(x = feature.names$V3)lvls <- levels(x = data_types)if (length(x = lvls) > 1 && length(x = full.data) == 0) {message("10X data contains more than one type and is being returned as a list containing matrices of each type.")}expr_name <- "Gene Expression"if (expr_name %in% lvls) {lvls <- c(expr_name, lvls[-which(x = lvls == expr_name)])}data <- lapply(X = lvls, FUN = function(l) {return(data[data_types == l, , drop = FALSE])})names(x = data) <- lvls}else {data <- list(data)}full.data[[length(x = full.data) + 1]] <- data}list_of_data <- list()for (j in 1:length(x = full.data[[1]])) {list_of_data[[j]] <- do.call(cbind, lapply(X = full.data, FUN = `[[`, j))list_of_data[[j]] <- as.sparse(x = list_of_data[[j]])}names(x = list_of_data) <- names(x = full.data[[1]])if (length(x = list_of_data) == 1) {return(list_of_data[[1]])}else {return(list_of_data)}
}

这里还可以看到seurat为什么对命名有要求,如果未来遇到不是这样命名的,改这里就好。

    barcode.loc <- file.path(run, "barcodes.tsv")gene.loc <- file.path(run, "genes.tsv")features.loc <- file.path(run, "features.tsv.gz")matrix.loc <- file.path(run, "matrix.mtx")

完~


本文由mdnice多平台发布

这篇关于【单细胞转录组】使用Seurat的Read10X函数读取10x文件时为什么有格式要求的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/539745

相关文章

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

SpringBoot3应用中集成和使用Spring Retry的实践记录

《SpringBoot3应用中集成和使用SpringRetry的实践记录》SpringRetry为SpringBoot3提供重试机制,支持注解和编程式两种方式,可配置重试策略与监听器,适用于临时性故... 目录1. 简介2. 环境准备3. 使用方式3.1 注解方式 基础使用自定义重试策略失败恢复机制注意事项

nginx启动命令和默认配置文件的使用

《nginx启动命令和默认配置文件的使用》:本文主要介绍nginx启动命令和默认配置文件的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录常见命令nginx.conf配置文件location匹配规则图片服务器总结常见命令# 默认配置文件启动./nginx

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

Windows下C++使用SQLitede的操作过程

《Windows下C++使用SQLitede的操作过程》本文介绍了Windows下C++使用SQLite的安装配置、CppSQLite库封装优势、核心功能(如数据库连接、事务管理)、跨平台支持及性能优... 目录Windows下C++使用SQLite1、安装2、代码示例CppSQLite:C++轻松操作SQ

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python中help()和dir()函数的使用

《Python中help()和dir()函数的使用》我们经常需要查看某个对象(如模块、类、函数等)的属性和方法,Python提供了两个内置函数help()和dir(),它们可以帮助我们快速了解代... 目录1. 引言2. help() 函数2.1 作用2.2 使用方法2.3 示例(1) 查看内置函数的帮助(

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删