首页 > Python资料 博客日记
单细胞Seruat和h5ad数据格式互换(R与python)方法学习和整理
2024-10-29 18:00:05Python资料围观47次
这篇文章介绍了单细胞Seruat和h5ad数据格式互换(R与python)方法学习和整理,分享给大家做个参考,收藏Python资料网收获更多编程知识
SeruatV4数据转化为h5ad格式数据
1、导入(R)
rm(list = ls())
library(Seurat)
library(qs)
library(reticulate)
library(hdf5r)
library(sceasy)
library(BiocParallel)
register(MulticoreParam(workers = 4, progressbar = TRUE))
scRNA <- qread("sc_dataset.qs")
scRNA
# An object of class Seurat
# 30269 features across 44651 samples within 2 assays
# Active assay: integrated (2000 features, 2000 variable features)
# 1 other assay present: RNA
# 3 dimensional reductions calculated: pca, umap, tsne
2、配置python环境(终端/linux)
# 配置环境
conda create -n sceasy python=3.9
conda activate sceasy
conda install loompy
# 可选安装
conda install anndata
conda install scipy
3、开始转换(R)
# 在R语言中加载python环境
use_condaenv('sceasy')
loompy <- reticulate::import('loompy')
# Seurat to AnnData
sceasy::convertFormat(scRNA, from="seurat", to="anndata",
outFile='scRNA.h5ad')
# AnnData object with n_obs × n_vars = 44651 × 28269
# obs: 'orig.ident', 'nCount_RNA', 'nFeature_RNA', 'GSE_num', 'Gender', 'Age', 'subsite', 'hpv', 'percent.mt', 'percent.rp', 'percent.hb', 'RNA_snn_res.0.1', 'RNA_snn_res.0.2', 'RNA_snn_res.0.3', 'RNA_snn_res.0.4', 'RNA_snn_res.0.5', 'RNA_snn_res.0.6', 'RNA_snn_res.0.7', 'RNA_snn_res.0.8', 'RNA_snn_res.0.9', 'RNA_snn_res.1', 'RNA_snn_res.1.1', 'RNA_snn_res.1.2', 'RNA_snn_res.1.3', 'RNA_snn_res.1.4', 'RNA_snn_res.1.5', 'RNA_snn_res.1.6', 'RNA_snn_res.1.7', 'RNA_snn_res.1.8', 'RNA_snn_res.1.9', 'RNA_snn_res.2', 'seurat_clusters', 'celltype', 'integrated_snn_res.0.1', 'integrated_snn_res.0.2', 'integrated_snn_res.0.3', 'integrated_snn_res.0.4', 'integrated_snn_res.0.5', 'integrated_snn_res.0.6', 'integrated_snn_res.0.7', 'integrated_snn_res.0.8', 'integrated_snn_res.0.9', 'integrated_snn_res.1', 'integrated_snn_res.1.1', 'integrated_snn_res.1.2', 'integrated_snn_res.1.3', 'integrated_snn_res.1.4', 'integrated_snn_res.1.5', 'integrated_snn_res.1.6', 'integrated_snn_res.1.7', 'integrated_snn_res.1.8', 'integrated_snn_res.1.9', 'integrated_snn_res.2'
# var: 'name'
# obsm: 'X_pca', 'X_umap', 'X_tsne'
#Seurat to SingleCellExperiment
sceasy::convertFormat(scRNA, from="seurat", to="sce",
outFile='scRNA.rds')
4、IDE中确认一下(python)
# 加载库
import scanpy as sc
import os
# 确认路径
os.getcwd()
# 读取数据
adata = sc.read_h5ad('scRNA.h5ad')
adata
# AnnData object with n_obs × n_vars = 44651 × 28269
# obs: 'orig.ident', 'nCount_RNA', 'nFeature_RNA', 'GSE_num', 'Gender', 'Age', 'subsite', 'hpv', 'percent.mt', 'percent.rp', 'percent.hb', 'RNA_snn_res.0.1', 'RNA_snn_res.0.2', 'RNA_snn_res.0.3', 'RNA_snn_res.0.4', 'RNA_snn_res.0.5', 'RNA_snn_res.0.6', 'RNA_snn_res.0.7', 'RNA_snn_res.0.8', 'RNA_snn_res.0.9', 'RNA_snn_res.1', 'RNA_snn_res.1.1', 'RNA_snn_res.1.2', 'RNA_snn_res.1.3', 'RNA_snn_res.1.4', 'RNA_snn_res.1.5', 'RNA_snn_res.1.6', 'RNA_snn_res.1.7', 'RNA_snn_res.1.8', 'RNA_snn_res.1.9', 'RNA_snn_res.2', 'seurat_clusters', 'celltype', 'integrated_snn_res.0.1', 'integrated_snn_res.0.2', 'integrated_snn_res.0.3', 'integrated_snn_res.0.4', 'integrated_snn_res.0.5', 'integrated_snn_res.0.6', 'integrated_snn_res.0.7', 'integrated_snn_res.0.8', 'integrated_snn_res.0.9', 'integrated_snn_res.1', 'integrated_snn_res.1.1', 'integrated_snn_res.1.2', 'integrated_snn_res.1.3', 'integrated_snn_res.1.4', 'integrated_snn_res.1.5', 'integrated_snn_res.1.6', 'integrated_snn_res.1.7', 'integrated_snn_res.1.8', 'integrated_snn_res.1.9', 'integrated_snn_res.2'
# var: 'name'
# obsm: 'X_pca', 'X_tsne', 'X_umap'
SeruatV5数据转化为h5ad格式数据
1、导入(R)
rm(list = ls())
V5_path = "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/seurat5/"
.libPaths(V5_path)
.libPaths()
library(Seurat)
library(qs)
library(reticulate)
library(hdf5r)
library(sceasy)
library(BiocParallel)
register(MulticoreParam(workers = 4, progressbar = TRUE))
scRNA_V5 <- readRDS("scRNA_tumor.rds")
scRNA_V5
# An object of class Seurat
# 20124 features across 5042 samples within 1 assay
# Active assay: RNA (20124 features, 2000 variable features)
# 3 layers present: counts, data, scale.data
# 3 dimensional reductions calculated: pca, harmony, umap
2、配置python环境(终端/linux)
# 配置环境
conda create -n sceasy python=3.9
conda activate sceasy
conda install loompy
# 可选安装
conda install anndata
conda install scipy
3、R语言转换(R)
# 在R语言中加载python环境
use_condaenv('sceasy')
loompy <- reticulate::import('loompy')
# Seurat to AnnData
scRNA_V5[["RNA"]] <- as(scRNA_V5[["RNA"]], "Assay")
sceasy::convertFormat(scRNA_V5, from="seurat", to="anndata",
outFile='scRNA_V5.h5ad')
# AnnData object with n_obs × n_vars = 5042 × 20124
# obs: 'nCount_RNA', 'nFeature_RNA', 'Sample', 'Cell.Barcode', 'Type', 'RNA_snn_res.0.1', 'RNA_snn_res.0.2', 'RNA_snn_res.0.3', 'RNA_snn_res.0.4', 'RNA_snn_res.0.5', 'RNA_snn_res.0.6', 'RNA_snn_res.0.7', 'RNA_snn_res.0.8', 'RNA_snn_res.0.9', 'RNA_snn_res.1', 'RNA_snn_res.1.1', 'RNA_snn_res.1.2', 'seurat_clusters', 'celltype', 'seurat_annotation'
# var: 'vf_vst_counts_mean', 'vf_vst_counts_variance', 'vf_vst_counts_variance.expected', 'vf_vst_counts_variance.standardized', 'vf_vst_counts_variable', 'vf_vst_counts_rank', 'var.features', 'var.features.rank'
# obsm: 'X_pca', 'X_harmony', 'X_umap'
# Warning message:
# In .regularise_df(obj@meta.data, drop_single_values = drop_single_values) :
# Dropping single category variables:orig.ident
先将 Seurat V5 对象中的 Assay5 类型转换为 Seurat 旧版本中的 Assay 类型,然后再进行转化
4、IDE中确认一下(python)
# 加载库
import scanpy as sc
import os
# 确认路径
os.getcwd()
# 读取数据
adata = sc.read_h5ad('scRNA.h5ad')
adata
# AnnData object with n_obs × n_vars = 5042 × 20124
# obs: 'nCount_RNA', 'nFeature_RNA', 'Sample', 'Cell.Barcode', 'Type', 'RNA_snn_res.0.1', 'RNA_snn_res.0.2', 'RNA_snn_res.0.3', 'RNA_snn_res.0.4', 'RNA_snn_res.0.5', 'RNA_snn_res.0.6', 'RNA_snn_res.0.7', 'RNA_snn_res.0.8', 'RNA_snn_res.0.9', 'RNA_snn_res.1', 'RNA_snn_res.1.1', 'RNA_snn_res.1.2', 'seurat_clusters', 'celltype', 'seurat_annotation'
# var: 'vf_vst_counts_mean', 'vf_vst_counts_variance', 'vf_vst_counts_variance.expected', 'vf_vst_counts_variance.standardized', 'vf_vst_counts_variable', 'vf_vst_counts_rank', 'var.features', 'var.features.rank'
# obsm: 'X_harmony', 'X_pca', 'X_umap'
ha5d格式数据转化成seruat对象
1.导入
rm(list = ls())
library(sceasy)
library(reticulate)
library(Seurat)
library(BiocParallel)
register(MulticoreParam(workers = 4, progressbar = TRUE))
2、R语言转换
# h5ad转为Seurat
sceasy::convertFormat(obj = "scRNA.h5ad",
from="anndata",to="seurat",
outFile = 'scRNA.rds')
# X -> counts
# An object of class Seurat
# 28269 features across 44651 samples within 1 assay
# Active assay: RNA (28269 features, 0 variable features)
# 2 layers present: counts, data
# 3 dimensional reductions calculated: pca, tsne, umap
这种方法得到的数据是SeruatV4版本的,所以如果要用于SeruatV5的话还需要再转化一下。
还有细胞数很多的话sceasy就不好用了,这个时候可以用dior包。
参考资料:
-
sceasy: https://github.com/cellgeni/sceasy
-
dior: https://github.com/JiekaiLab/dior
-
单细胞天地: https://mp.weixin.qq.com/s/qHBeQnYJdK0ATGlTOROPeA
-
生信菜鸟团: https://mp.weixin.qq.com/s/8fwJSc9Dnp8h_Suv76oXVA
-
KS科研分享与服务:https://mp.weixin.qq.com/s/Wt9TU5Qk3yqPDlRlXr6BfQ
注:若对内容有疑惑或者有发现明确错误的朋友,请联系后台(欢迎交流)。更多内容可关注公众号:生信方舟
- END -
版权声明:本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签:
相关文章
最新发布
- 【Python】selenium安装+Microsoft Edge驱动器下载配置流程
- Python 中自动打开网页并点击[自动化脚本],Selenium
- Anaconda基础使用
- 【Python】成功解决 TypeError: ‘<‘ not supported between instances of ‘str’ and ‘int’
- manim边学边做--三维的点和线
- CPython是最常用的Python解释器之一,也是Python官方实现。它是用C语言编写的,旨在提供一个高效且易于使用的Python解释器。
- Anaconda安装配置Jupyter(2024最新版)
- Python中读取Excel最快的几种方法!
- Python某城市美食商家爬虫数据可视化分析和推荐查询系统毕业设计论文开题报告
- 如何使用 Python 批量检测和转换 JSONL 文件编码为 UTF-8
点击排行
- 版本匹配指南:Numpy版本和Python版本的对应关系
- 版本匹配指南:PyTorch版本、torchvision 版本和Python版本的对应关系
- Python 可视化 web 神器:streamlit、Gradio、dash、nicegui;低代码 Python Web 框架:PyWebIO
- 相关性分析——Pearson相关系数+热力图(附data和Python完整代码)
- Python与PyTorch的版本对应
- Anaconda版本和Python版本对应关系(持续更新...)
- Python pyinstaller打包exe最完整教程
- Could not build wheels for llama-cpp-python, which is required to install pyproject.toml-based proj