空间转录组-生信流程-使用scanpy读入数据

探序基因肿瘤研究院

import scanpy as sc

>>> adata = sc.read_10x_h5('/xxx/filtered_feature_bc_matrix.h5')

/root/miniconda3/lib/python3.12/site-packages/anndata/_core/anndata.py:1756: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.

utils.warn_names_duplicates("var")

/root/miniconda3/lib/python3.12/site-packages/anndata/_core/anndata.py:1756: UserWarning: Variable names are not unique. To make them unique, call `.var_names_make_unique`.

utils.warn_names_duplicates("var")

不知道这个报错代表啥意思

>>> print(adata)

AnnData object with n_obs × n_vars = 3793 × 18085

var: 'gene_ids', 'feature_types', 'genome'

保存变量:

import pickle

with open('processed_data.pkl', 'wb') as f:

pickle.dump(adata, f)

读取变量:

import pickle

with open('processed_data.pkl', 'rb') as f:

adata = pickle.load(f)

查看adata.var:

下一步,要将基因组位置存储在 adata.var 中。 "chromosome", "start", "end"分别保存每个基因的染色体以及在该染色体上的开始和结束位置。可以使用infercnvpy.io.genomic_position_from_gtf()函数。

下载了一个Homo_sapiens.GRCh38.99.gtf.gz文件,

infercnvpy.io.genomic_position_from_gtf("Homo_sapiens.GRCh38.99.gtf.gz",adata)

出现报错信息:

INFO:root:Extracted GTF attributes: ['gene_id', 'gene_version', 'gene_name', 'gene_source', 'gene_biotype', 'transcript_id', 'transcript_version', 'transcript_name', 'transcript_source', 'transcript_biotype', 'tag', 'transcript_support_level', 'exon_number', 'exon_id', 'exon_version', 'protein_id', 'protein_version', 'ccds_id']

应该是要GENCODE版本的gtf文件?

下载infercnvpy自带的数据集:

bdata变量查看:

>>> bdata

AnnData object with n_obs × n_vars = 3000 × 55556

obs: 'age', 'sex', 'sample', 'patient', 'cell_type'

var: 'ensg', 'mito', 'n_cells_by_counts', 'mean_counts', 'pct_dropout_by_counts', 'total_counts', 'n_counts', 'chromosome', 'start', 'end', 'gene_id', 'gene_name'

uns: 'cell_type_colors', 'neighbors', 'umap', 'cnv'

obsm: 'X_scVI', 'X_umap', 'X_cnv'

obsp: 'connectivities', 'distances'

查看细胞注释数据:

>>> bdata.obs['cell_type']

Run

SRR10796381 T cell CD4

SRR10787713 Macrophage

SRR10785570 T cell CD4

SRR10790048 Monocyte

SRR10782528 Epithelial cell

...

SRR10782593 Fibroblast

SRR10789971 Epithelial cell

SRR10780468 Epithelial cell

SRR10779840 mDC

SRR10795768 other
Name: cell_type, Length: 3000, dtype: category

Categories (21, object): ['Alevolar cell type 1', 'Alevolar cell type 2', 'B cell', 'Ciliated', ...,

'T cell regulatory', 'mDC', 'other', 'pDC']

这种数据结构是Pandas库的DataFrame。DataFrame是Pandas库中的一种二维标签数据结构,可以看作是一种带有行和列标签的表格数据。它既有行索引,也有列索引,每列可以是不同的数据类型。DataFrame类似于电子表格或SQL表格,非常适合用于数据分析和处理。

>>> bdata.obs['cell_type'][1:3]

Run

SRR10787713 Macrophage

SRR10785570 T cell CD4

Name: cell_type, dtype: category

Categories (21, object): ['Alevolar cell type 1', 'Alevolar cell type 2', 'B cell', 'Ciliated', ...,

'T cell regulatory', 'mDC', 'other', 'pDC']

>>> bdata.obs["cell_type"].iloc[0]

'T cell CD4'

可通过bdata.obs["cell_type"].iloc[0] = "xxx" 的方式来修改细胞类型。

读取细胞名:

ind = bdata.obs["cell_type"].index

>>> print(ind)

Index(['SRR10796381', 'SRR10787713', 'SRR10785570', 'SRR10790048',

'SRR10782528', 'SRR10783499', 'SRR10782527', 'SRR10795166',

'SRR10798281', 'SRR10791041',

...

'SRR10795296', 'SRR10785910', 'SRR10779352', 'SRR10790560',

'SRR10779232', 'SRR10782593', 'SRR10789971', 'SRR10780468',

'SRR10779840', 'SRR10795768'],

dtype='object', name='Run', length=3000)

>>> ind[1:5]

Index(['SRR10787713', 'SRR10785570', 'SRR10790048', 'SRR10782528'], dtype='object', name='Run')

如何创建:

import pandas as pd

创建一个包含分类数据的DataFrame:

案例1:

df = pd.DataFrame({

'A': pd.Categorical(['apple', 'banana', 'apple', 'banana'], categories=['apple', 'banana']),

'B': pd.Categorical(['b', 'a', 'b', 'a'], categories=['b', 'a'])

})

将列的dtype设置为category

df = df.astype({'A': 'category', 'B': 'category'})

案例2:

data = {'Name': ['Alice', 'Bob', 'Charlie'],

'Age': [25, 30, 35],

'City': ['New York', 'London', 'Paris']}

df = pd.DataFrame(data)

print(df)

案例3:设置行名

data = {'Column1': [1, 2, 3], 'Column2': [4, 5, 6]}

row_names = ['Row1', 'Row2', 'Row3']

df = pd.DataFrame(data, index=row_names)

或者:

df = df.reset_index(drop=True) # 重置索引,不保留旧索引作为列

df.index = row_names # 设置新的行名

print(df)

scanpy数据结构保存方法:

adata.write('annotation_data.h5ad')

;python数据结构类型:数组、列表、元组、集合、字典、堆、栈、队列、树、图等

>>> cnv.pl.chromosome_heatmap(bdata, groupby="cell_type")

/root/miniconda3/lib/python3.12/site-packages/scanpy/plotting/_utils.py:363: UserWarning: FigureCanvasAgg is non-interactive, and thus cannot be shown

plt.show()

参考:

单细胞转录组实战04: infercnvpy识别恶性细胞

相关推荐
朝新_4 分钟前
【多线程初阶】阻塞队列 & 生产者消费者模型
java·开发语言·javaee
立莹Sir6 分钟前
Calendar类日期设置进位问题
java·开发语言
风逸hhh1 小时前
python打卡day46@浙大疏锦行
开发语言·python
火兮明兮1 小时前
Python训练第四十三天
开发语言·python
ascarl20102 小时前
准确--k8s cgroup问题排查
java·开发语言
Gaoithe2 小时前
ubuntu 端口复用
linux·运维·ubuntu
互联网杂货铺3 小时前
完美搭建appium自动化环境
自动化测试·软件测试·python·测试工具·职场和发展·appium·测试用例
Gyoku Mint3 小时前
机器学习×第二卷:概念下篇——她不再只是模仿,而是开始决定怎么靠近你
人工智能·python·算法·机器学习·pandas·ai编程·matplotlib
fpcc3 小时前
跟我学c++中级篇——理解类型推导和C++不同版本的支持
开发语言·c++
德先生&赛先生3 小时前
Linux编程:1、文件编程
linux