计算生物学习——Code_基于PrimeKG创建异构图

一.PrimeKG是什么?

文献/数据(.csv)下载

Precision Medicine Oriented Knowledge Graph - Zitnik Lab

代码下载

https://github.com/mims-harvard/PrimeKG

浅浅弄了一个思维导图~

二.关于数据

可以在第一个网页处进行下载

获得压缩包后解压

不同数据集的介绍(也可以看README~)

三.基于数据集创建异构图

就在前两篇文章的基础上(如果有创建异构图过的环境可以直接用,如果没有就创建新环境并安装好dgl)

计算生物学习——Code_创建异构图_①安装dgl_dgl2.2.1-CSDN博客

1.确保可以安装好dgl

import pandas as pd
import dgl
from dgl import heterograph

2.读取节点和边数据集

# 读取节点和边数据
nodes_df = pd.read_csv('你的文件夹/nodes.csv')
edges_df = pd.read_csv('你的文件夹/edges.csv')
# 查看数据内容
print(nodes_df.head())
print(edges_df.head())

3.获取所有节点类型&边的类型

# 获取所有节点类型
node_types = nodes_df['node_type'].unique()
print(node_types)
# 获取所有边类型
edge_types = edges_df['relation'].unique()
print(edge_types)

4.创建节点字典,保存节点的ID索引

# 创建节点字典,保存节点的ID索引
# 假设每个节点类型的索引是唯一的,并存储在 'node_index' 列中
node_dict = {node_type: nodes_df[nodes_df['node_type'] == node_type]['node_index'].tolist() for node_type in node_types}

# 创建异构图的数据字典
data_dict = {}

for edge_type in edge_types:
    # 筛选对应边类型的数据
    edge_df = edges_df[edges_df['relation'] == edge_type]
    
    # 获取边的起点和终点索引
    src_nodes = edge_df['x_index'].tolist()
    dst_nodes = edge_df['y_index'].tolist()
    
    # 获取起点和终点的节点类型
    # 假设 'x_index' 和 'y_index' 对应的节点类型可以从 nodes_df 中查找
    src_types = nodes_df.loc[edge_df['x_index'], 'node_type'].values
    dst_types = nodes_df.loc[edge_df['y_index'], 'node_type'].values
    
    for src_type, dst_type, src_node, dst_node in zip(src_types, dst_types, src_nodes, dst_nodes):
        # 使用 (src_type, edge_type, dst_type) 作为键值,将每个边类型的信息添加到 data_dict
        if (src_type, edge_type, dst_type) not in data_dict:
            data_dict[(src_type, edge_type, dst_type)] = ([], [])
        
        data_dict[(src_type, edge_type, dst_type)][0].append(src_node)
        data_dict[(src_type, edge_type, dst_type)][1].append(dst_node)

5.创建异构图

# 使用 data_dict 创建异构图
g = dgl.heterograph(data_dict)

# 输出异构图的信息
print(g)

获得结果如下:

Graph(num_nodes={'anatomy': 129375, 'biological_process': 115051, 'cellular_component': 127435, 'disease': 100028, 'drug': 39898, 'effect/phenotype': 94551, 'exposure': 127598, 'gene/protein': 127440, 'molecular_function': 124222, 'pathway': 129372},
     

这部分列出了图中不同节点类型及其对应的节点数量。

以下是各节点类型的含义:

  • anatomy:解剖学相关的节点,数量为 129,375。
  • biological_process:生物过程的节点,数量为 115,051。
  • cellular_component:细胞成分相关的节点,数量为 127,435。
  • disease:疾病相关的节点,数量为 100,028。
  • drug:药物相关的节点,数量为 39,898。
  • effect/phenotype:效应或表型相关的节点,数量为 94,551。
  • exposure:暴露(外部因素)相关的节点,数量为 127,598。
  • gene/protein:基因或蛋白质相关的节点,数量为 127,440。
  • molecular_function:分子功能相关的节点,数量为 124,222。
  • pathway:路径(pathway)相关的节点,数量为 129,372。

num_edges={('anatomy', 'anatomy_anatomy', 'anatomy'): 28064, ('anatomy', 'anatomy_protein_absent', 'gene/protein'): 19887, ('anatomy', 'anatomy_protein_present', 'gene/protein'): 1518203, ('biological_process', 'bioprocess_bioprocess', 'biological_process'): 105772, ('biological_process', 'bioprocess_protein', 'gene/protein'): 144805, ('biological_process', 'exposure_bioprocess', 'exposure'): 1625, ('cellular_component', 'cellcomp_cellcomp', 'cellular_component'): 9690, ('cellular_component', 'cellcomp_protein', 'gene/protein'): 83402, ('cellular_component', 'exposure_cellcomp', 'exposure'): 10, ('disease', 'contraindication', 'drug'): 30675, ('disease', 'disease_disease', 'disease'): 64388, ('disease', 'disease_phenotype_negative', 'effect/phenotype'): 1193, ('disease', 'disease_phenotype_positive', 'effect/phenotype'): 150317, ('disease', 'disease_protein', 'gene/protein'): 80411, ('disease', 'exposure_disease', 'exposure'): 2304, ('disease', 'indication', 'drug'): 9388, ('disease', 'off-label use', 'drug'): 2568, ('drug', 'contraindication', 'disease'): 30675, ('drug', 'drug_drug', 'drug'): 2672628, ('drug', 'drug_effect', 'effect/phenotype'): 64784, ('drug', 'drug_protein', 'gene/protein'): 25653, ('drug', 'indication', 'disease'): 9388, ('drug', 'off-label use', 'disease'): 2568, ('effect/phenotype', 'disease_phenotype_negative', 'disease'): 1193, ('effect/phenotype', 'disease_phenotype_positive', 'disease'): 150317, ('effect/phenotype', 'drug_effect', 'drug'): 64784, ('effect/phenotype', 'phenotype_phenotype', 'effect/phenotype'): 37472, ('effect/phenotype', 'phenotype_protein', 'gene/protein'): 3330, ('exposure', 'exposure_bioprocess', 'biological_process'): 1625, ('exposure', 'exposure_cellcomp', 'cellular_component'): 10, ('exposure', 'exposure_disease', 'disease'): 2304, ('exposure', 'exposure_exposure', 'exposure'): 4140, ('exposure', 'exposure_molfunc', 'molecular_function'): 45, ('exposure', 'exposure_protein', 'gene/protein'): 1212, ('gene/protein', 'anatomy_protein_absent', 'anatomy'): 19887, ('gene/protein', 'anatomy_protein_present', 'anatomy'): 1518203, ('gene/protein', 'bioprocess_protein', 'biological_process'): 144805, ('gene/protein', 'cellcomp_protein', 'cellular_component'): 83402, ('gene/protein', 'disease_protein', 'disease'): 80411, ('gene/protein', 'drug_protein', 'drug'): 25653, ('gene/protein', 'exposure_protein', 'exposure'): 1212, ('gene/protein', 'molfunc_protein', 'molecular_function'): 69530, ('gene/protein', 'pathway_protein', 'pathway'): 42646, ('gene/protein', 'phenotype_protein', 'effect/phenotype'): 3330, ('gene/protein', 'protein_protein', 'gene/protein'): 642150, ('molecular_function', 'exposure_molfunc', 'exposure'): 45, ('molecular_function', 'molfunc_molfunc', 'molecular_function'): 27148, ('molecular_function', 'molfunc_protein', 'gene/protein'): 69530, ('pathway', 'pathway_pathway', 'pathway'): 5070, ('pathway', 'pathway_protein', 'gene/protein'): 42646},

这部分列出了不同类型的边及其对应的数量。

每个条目是一个三元组 (src_type, edge_type, dst_type),分别代表边的起点类型、边的类型和终点类型:

  • ('anatomy', 'anatomy_anatomy', 'anatomy'): 表示解剖学节点之间的连接关系(边类型为 anatomy_anatomy),该边的数量为 28,064。
  • ('anatomy', 'anatomy_protein_absent', 'gene/protein'): 表示解剖学和基因/蛋白质节点之间的一种关系,边的数量为 19,887。
  • ('anatomy', 'anatomy_protein_present', 'gene/protein'): 表示解剖学和基因/蛋白质节点之间的另一种关系(例如蛋白质存在的情况下),该边的数量为 1,518,203。

类似地,其他条目展示了不同节点类型之间的各种边。例如:

  • ('disease', 'contraindication', 'drug'): 表示疾病与药物之间的禁忌关系(contraindication),数量为 30,675。
  • ('disease', 'indication', 'drug'): 表示疾病与药物之间的适应症关系(indication),数量为 9,388。
  • ('drug', 'drug_drug', 'drug'): 表示药物与药物之间的关系,数量为 2,672,628。
  • ('gene/protein', 'protein_protein', 'gene/protein'): 表示基因或蛋白质之间的相互作用,数量为 642,150。

      metagraph=[('anatomy', 'anatomy', 'anatomy_anatomy'), ('anatomy', 'gene/protein', 'anatomy_protein_absent'), ('anatomy', 'gene/protein', 'anatomy_protein_present'), ('gene/protein', 'anatomy', 'anatomy_protein_absent'), ('gene/protein', 'anatomy', 'anatomy_protein_present'), ('gene/protein', 'biological_process', 'bioprocess_protein'), ('gene/protein', 'cellular_component', 'cellcomp_protein'), ('gene/protein', 'disease', 'disease_protein'), ('gene/protein', 'drug', 'drug_protein'), ('gene/protein', 'exposure', 'exposure_protein'), ('gene/protein', 'molecular_function', 'molfunc_protein'), ('gene/protein', 'pathway', 'pathway_protein'), ('gene/protein', 'effect/phenotype', 'phenotype_protein'), ('gene/protein', 'gene/protein', 'protein_protein'), ('biological_process', 'biological_process', 'bioprocess_bioprocess'), ('biological_process', 'gene/protein', 'bioprocess_protein'), ('biological_process', 'exposure', 'exposure_bioprocess'), ('exposure', 'biological_process', 'exposure_bioprocess'), ('exposure', 'cellular_component', 'exposure_cellcomp'), ('exposure', 'disease', 'exposure_disease'), ('exposure', 'exposure', 'exposure_exposure'), ('exposure', 'molecular_function', 'exposure_molfunc'), ('exposure', 'gene/protein', 'exposure_protein'), ('cellular_component', 'cellular_component', 'cellcomp_cellcomp'), ('cellular_component', 'gene/protein', 'cellcomp_protein'), ('cellular_component', 'exposure', 'exposure_cellcomp'), ('disease', 'drug', 'contraindication'), ('disease', 'drug', 'indication'), ('disease', 'drug', 'off-label use'), ('disease', 'disease', 'disease_disease'), ('disease', 'effect/phenotype', 'disease_phenotype_negative'), ('disease', 'effect/phenotype', 'disease_phenotype_positive'), ('disease', 'gene/protein', 'disease_protein'), ('disease', 'exposure', 'exposure_disease'), ('drug', 'disease', 'contraindication'), ('drug', 'disease', 'indication'), ('drug', 'disease', 'off-label use'), ('drug', 'drug', 'drug_drug'), ('drug', 'effect/phenotype', 'drug_effect'), ('drug', 'gene/protein', 'drug_protein'), ('effect/phenotype', 'disease', 'disease_phenotype_negative'), ('effect/phenotype', 'disease', 'disease_phenotype_positive'), ('effect/phenotype', 'drug', 'drug_effect'), ('effect/phenotype', 'effect/phenotype', 'phenotype_phenotype'), ('effect/phenotype', 'gene/protein', 'phenotype_protein'), ('molecular_function', 'exposure', 'exposure_molfunc'), ('molecular_function', 'molecular_function', 'molfunc_molfunc'), ('molecular_function', 'gene/protein', 'molfunc_protein'), ('pathway', 'pathway', 'pathway_pathway'), ('pathway', 'gene/protein', 'pathway_protein')])

metagraph 是异构图的元图(meta graph),定义了图中所有节点类型及其连接关系。每个元图条目代表一种边类型,它由一个三元组 (src_type, dst_type, edge_type) 构成,表示源节点类型、目标节点类型和边的类型:

  • ('anatomy', 'anatomy', 'anatomy_anatomy'): 表示解剖学节点之间的关系。
  • ('anatomy', 'gene/protein', 'anatomy_protein_absent'): 表示解剖学节点和基因/蛋白质节点之间的一种关系。
  • ('disease', 'drug', 'contraindication'): 表示疾病和药物之间的禁忌关系。

通过元图,可以直观了解图中各类节点的关系结构。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值