pyscenic运行报错:ValueError: Intersection of gene_names and tf_names is empty
pyscenic运行报错:ValueError: Intersection of gene_names and tf_names is empty
- 首先查一下是否有重复基因
python check_common_genes.py
check_common_genes.py
import pandas as pd
# 定义文件路径
#这是转置后的基因表达矩阵
expression_matrix_file = "normalized_expression_matrix_transposed.csv" # 表达矩阵文件
tfs_file = "hs_hgnc_tfs.txt" # 转录因子列表文件
# 读取表达矩阵文件
# 基因名在列名中(第一行),细胞名在行名中(第一列)
expression_matrix = pd.read_csv(expression_matrix_file, index_col=0)
gene_names = set(expression_matrix.columns) # 获取基因名
# 读取转录因子列表文件
with open(tfs_file, 'r') as f:
tfs = set(line.strip() for line in f.readlines()) # 获取转录因子基因名
# 检查交集
common_genes = gene_names.intersection(tfs)
# 输出结果
if common_genes:
print(f"找到 {len(common_genes)} 个重复基因:")
print(common_genes)
else:
print("没有找到重复基因。")
如果查得到,那么应该是读取基因表达矩阵的时候没有正确转置
如果基因表达矩阵的csv 没有转置过,那么上面代码的一行修改一下
# 基因名在列名中(第一行),细胞名在行名中(第一列)
gene_names = set(expression_matrix.columns) # 获取基因名
这样应该会输出有重复基因了
如果没有,可能涉及大小写敏感:
基因名的大小写需要一致。如果文件中的基因名大小写不一致,可以在读取时统一转换为小写:
gene_names = set(expression_matrix.columns.str.lower())
tfs = set(line.strip().lower() for line in f.readlines())
- 解决
查看创建loom文件的时候是不是又转置了,全程应该只需要转一次
import loompy as lp
import numpy as np
import scanpy as sc
# 读取 CSV 文件,这里是转置后的
x = sc.read_csv("normalized_expression_matrix_transposed.csv")
# 定义行和列属性
row_attrs = {"CellID": np.array(x.obs_names)} # 行名是细胞 ID
col_attrs = {"Gene": np.array(x.var_names)} # 列名是基因名
# 创建 Loom 文件,这里不需要转了
lp.create("output.loom", x.X, row_attrs, col_attrs)
如果是没有转置的基因表达矩阵的csv,则生成loom需要转置
该行代码改成这样
lp.create("sample.loom",x.X.transpose(),row_attrs,col_attrs);