I use the NetWorkx module to find all the shortest strings between two points of a graph.
Everything works well at a close detail; as soon as there are no paths between two points, my script stops and does not work for the other couples (starting point, point of arrival).
So I want to know how to add a condition so that when there are no paths between two points, I print the following message "No way between these two points" and continue with other couples
Here is my code:
import networkx as nx from collections import defaultdict dd=defaultdict(set) P=[]
File content all couples (starting point, end point) to test
with open("3080_interactions_pour_736.txt","r") as f0: for lignes0 in f0: lignes0=lignes0.rstrip('\n').split(" ") prot1=lignes0[0] prot2=lignes0[1] couple=prot1,prot2 P.append(list(couple)) #print(P)
File containing all the binary interactions composing my graph
with open("736(int_connues_avec_scores_sup_0).txt","r") as f1: for lignes in f1: if lignes.startswith('9606'): lignes=lignes.rstrip('\n').split(" ") proteine1=lignes[2] proteine2=lignes[3] dd[proteine1].add(proteine2)
For every couple of my first file, I apply the module of the shortest chains:
for couples in P: prot1=couples[0] prot2=couples[1] Lchaines=([p for p in nx.all_shortest_paths(dd, source=prot1, target=prot2, weight=None)]) print("") print("The first protein in the chain is", prot1) print("The last protein in the chain is", prot2) print("") print("Minimal size chain(s):") print("")
for chaines in Lchaines:
#To have chains of size 11 maximum
if len(chaines) <= 11:
print(' '.join(chaines))
else :
print('ERROR - The minimum size of the chains exceeds the limit')
break
print("")
print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
Here is the error message I receive:
Traceback (most recent call last):
File "C:\Users\loisv\Desktop\pluscourtchemin.py", line 26, in
Lchaines=([p for p in nx.all_shortest_paths(dd, source=prot1, target=prot2, weight=None)])
File "C:\Users\loisv\Desktop\pluscourtchemin.py", line 26, in
Lchaines=([p for p in nx.all_shortest_paths(dd, source=prot1, target=prot2, weight=None)])
File "C:\Users\loisv\AppData\Local\Programs\Python\Python37-32\lib\site-packages\networkx\algorithms\shortest_paths\generic.py", line 481, in all_shortest_paths
'from Source {}'.format(target, source))
networkx.exception.NetworkXNoPath: Target IKBKE cannot be reachedfrom Source LCK
Thanks for your help :)
Aucun commentaire:
Enregistrer un commentaire