Reputation: 99
I have a few functions I use to calculate some values off of a networkX graph, here's the code for them:
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
This is my input graph:
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"]
If you can't tell, this is a network that represents a human cell. I'm trying to use the functions on the graph to calculate the "SigFlux" for every node in the network once for every output (so 3 times). This is my code which is supposed to do that:
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)))
However, I get this error when running the script:
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
Can't wrap my head around what the issue is. The full script if you want to run it yourself: https://pastebin.com/jBeX7EHs
Upvotes: 1
Views: 2916
Reputation: 23887
Your code has this line:
for input, output in itertools.product(inputs, outputs)
but when this is called, outputs
is the string 'Survival'
. So it iterates over all values in 'Survival'
, namely: 'S'
, 'u'
, ...
You can get around this either by editing the function, or by changing the arguments sent to the function. So replacing sigFluxCalc(G, node, sources, output)
by sigFluxCalc(G, node, sources, [output])
would work.
As a note, I think this line of your code:
if(node != "Survival" or node != "NonACD" or node != "Apoptosis"):
would read better as:
if node not in targets:
Upvotes: 2