python - networkX

时间:2017-08-09 22:41:38

标签: python graph networkx

我有一些函数用于计算networkX图的某些值,这里是它们的代码:

def signal_path_counter(G, node, inputs, outputs):
    c = 0
    paths = []
    for input, output in itertools.product(inputs, outputs):
        for path in all_simple_paths(G, input, output):
            if node in path:
                c += 1
    return c

def feedback_loop_counter(G, node):
    neighbors = G.neighbors(node)
    cycles = []
    for neighbor in neighbors:
        path = all_simple_paths(G, neighbor, node)
        if path not in cycles:
            cycles.append(path)
    return len(cycles)

def sigFluxCalc(G, node, inputs, outputs):
    numerator = signal_path_counter(G, node, inputs, outputs) + 
    feedback_loop_counter(G, node)
    denominator = 0
    for n in G.nodes():
        temp = signal_path_counter(G, n, inputs, outputs) + feedback_loop_counter(G, n)
        denominator += temp
    return numerator/denominator

这是我的输入图:

molecules = ["TNF", "RIP1", "FASL", "clAP", "CASP8", "ROS", "MPT", "MOMP", 
"NFkB", "ATP", "CASP3", "Survival", "NonACD", "Apoptosis"]
TNF = [("TNF", "RIP1"), ("TNF", "CASP8")]
FASL = [("FASL", "RIP1"), ("FASL", "CASP8")]
RIP1 = [("RIP1", "NFkB"), ("RIP1", "ROS")]
CASP8 = [("CASP8", "RIP1"), ("CASP8", "MOMP")]
cIAP = [("cIAP", "cIAP"), ("cIAP", "NFkB")]
NFkB = [("NFkB", "cIAP"),("NFkB", "Survival"), ("NFkB", "ROS"), ("NFkB", "CASP8"), ("NFkB", "MOMP"), ("NFkB", "MPT"), ("NFkB", "CASP3")]
ROS = [("ROS", "MPT")]
MPT = [("MPT", "MOMP"), ("MPT", "ATP"), ("MPT", "ROS")]
MOMP = [("MOMP", "cIAP"), ("MOMP", "CASP3")]
ATP = [("ATP", "NonACD"), ("ATP", "CASP3")]
CASP3 = [("CASP3", "Apoptosis"), ("CASP3", "NFkB"), ("CASP3", "CASP8")]
edges = TNF + FASL + RIP1 + CASP8 + cIAP + NFkB + ROS + MPT + MOMP + ATP + CASP3
G.add_nodes_from(molecules)
G.add_edges_from(edges)
sources = ["TNF", "FASL"]
targets = ["Survival", "NonACD", "Apoptosis"]

如果你不知道,这是一个代表人类细胞的网络。我正在尝试使用图表上的函数为每个输出计算网络中每个节点的“SigFlux”(因此3次)。这是我应该这样做的代码:

for output in targets:
    print("SigFlux calculations for " + output + " as output:")
    for node in G.nodes():
        if(node != "Survival" or node != "NonACD" or node != "Apoptosis"):
            print(node + ": " + str(sigFluxCalc(G, node, sources, output)))

但是,运行脚本时出现此错误:

Traceback (most recent call last):
  File "C:\Users\witcher\Desktop\Python Scripts\nx testing.py", line 200, in <module>
    print(node + ": " + str(sigFluxCalc(G, node, sources, output)))
  File "C:\Users\witcher\Desktop\Python Scripts\nx testing.py", line 144, in sigFluxCalc
    numerator = signal_path_counter(G, node, inputs, outputs) + feedback_loop_counter(G, node)
  File "C:\Users\witcher\Desktop\Python Scripts\nx testing.py", line 129, in signal_path_counter
    for path in all_simple_paths(G, input, output):
  File "C:\Users\witcher\Desktop\Python Scripts\nx testing.py", line 69, in all_simple_paths
    raise nx.NetworkXError('target node %s not in graph'%target)
networkx.exception.NetworkXError: target node S not in graph

无法解决问题所在。完整脚本,如果您想自己运行它:https://pastebin.com/jBeX7EHs

1 个答案:

答案 0 :(得分:2)

您的代码有这一行:

:not

但是当调用它时,for input, output in itertools.product(inputs, outputs) 是字符串outputs。因此,它会迭代&#39; 'Survival'中的所有值,即:Survival''S',...

您可以通过编辑函数或更改发送到函数的参数来解决此问题。因此,'u'替换sigFluxCalc(G, node, sources, output)会有效。

作为一个说明,我认为你的代码这一行:

sigFluxCalc(G, node, sources, [output])

会更好地阅读:

if(node != "Survival" or node != "NonACD" or node != "Apoptosis"):