【问题标题】:Python program to map a series of chemical reactions with unique nodes only appearing once用于映射一系列化学反应的 Python 程序,其中唯一节点仅出现一次
【发布时间】:2016-08-17 17:12:59
【问题描述】:
H2 > H2
H2 > H2
H2 > H2*
H2 > H2*
H2 > H + H
H2 > H2^
H2 > H* + H
H2 > H + H
H2^ > H2^
H2^ > H^ + H
H2^ > H + H
H3^ > H3^
H3^ > H^ + H2
H3^ > H + H2
H > H
H > H*
H > H^
H^ > H^
H^ > H
H + H2^ > H2 + H^
H2 + H2^ > H + H3^
CF4 > CF4
CF4 > F- + CF3

我希望我的程序在地图上为化学中的每个物种创建节点,并在反应物和产物之间绘制路径,其中一个物种在地图上只出现一次,并且地图上有线条来表示反应中每个反应物的反应反应中的每个产物。

我已经编写了以下代码,但是该代码只是将每个反应绘制在地图上,并没有将它们连接起来,我不确定如何最好地继续连接反应中的常见物种。

import os
import sys
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
import copy

try:
data = sys.argv[1]
except IndexError:
    print('Insert filename')
    sys.exit(1)

def parse_line(line):
    new=line.split('>')
    reactants=new[0].split('+')
    products=new[1].split('+')
    return reactants,products



all_edges=[]
edge_labels={}
all_reactants=[]
all_products=[]

with open(data) as fi:
    for line in fi.readlines():
        line = line.strip()
        if not line or line[0] in '#%!':
        # skip blank and comment lines
            continue
        reactants,products = parse_line(line)

        for i in np.arange(len(reactants)):
            other_reactants=copy.copy(reactants)
            other_reactants.remove(reactants[i])
            other_reactants=', '.join(other_reactants)
            for j in np.arange(len(products)):
                edge=(reactants[i],products[j])
                all_edges.append(edge)
                edge_labels[edge]=other_reactants



gr=nx.DiGraph()

gr.add_edges_from(all_edges)

pos=nx.random_layout(gr)

nx.draw_networkx_nodes(gr,pos,node_size=2000,node_shape='o',node_color='0.75',alpha=10)
nx.draw_networkx_edges(gr,pos, width=0.05,edge_color='b')

nx.draw_networkx_labels(gr, pos,font_size=12, font_color='k', font_weight='normal', alpha=1.0, ax=None)
nx.draw_networkx_edge_labels(gr,pos,edge_labels=edge_labels, label_pos=0.01, ax=None, rotate=False)
plt.show()

任何人都可以建议我进行此操作的最佳方法吗,我需要一个函数来识别所有反应中的反应物和产物中的常见物种,并为每个独特的物种创建一个节点,并从每个反应物创建一条线到每个反应的每个产物。

任何帮助将不胜感激

非常感谢

【问题讨论】:

  • 化工背景出身,文字还是挺难理解的;有没有一种方法可以在没有化学术语的情况下概括这一点?我从来没有用过networkx,所以我不能半途而废,但我认为这在目前的状态下很难回答。
  • 从本质上讲,我认为您要求的是 有向图,其中 > 左侧的所有内容都指向与 @987654325 右侧相关联的所有内容@?但是H2 > H2 到底是什么意思,为什么会出现两次?另外,您认为(我认为是)激进的H*H 不同?我认为这些问题可能会成为答案的障碍......出于我自己的好奇心,^ 是什么?
  • 非常感谢您的关注和阐明问题。我将首先回答您的第二个问题,因为我将为您的第二个问题准备一些 .png。有两种类型的反应物“电子碰撞反应”和“重粒子反应”。在电子碰撞反应中,电子没有打印出来,因为它们不会出现在化学图上。所以 H2 > H2 实际上是 e + H2 > e + H2 这称为弹性反应。这些反应不应该出现在地图上,但我可以输入一些代码来消除它们。 H* 是 H 的“亚稳态”,它是不同的。
  • 而 H^ 仅表示 H+,即离子

标签: python networkx


【解决方案1】:

您似乎遇到的一个问题是,您没有从反应的“qstrings”中解析出反应物/产物周围的空白。这意味着例如' H2''H2 ' 被视为不同的物种,因此在您的图中得到不同的节点。

您可能希望通过以下方式处理此问题:

reactants = [s.strip() for s in new[0].split('+')]
products = [s.strip() for s in new[1].split('+')]

在您的 parse_line 函数中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-07
    • 1970-01-01
    • 2016-11-11
    • 2019-04-07
    • 2019-02-05
    • 1970-01-01
    • 2017-05-13
    • 2018-03-07
    相关资源
    最近更新 更多