2

I am trying to find the paths between two vertices on a directed graph. I have an igraph object that is the directed graph; I have a parent node, and the list of vertex sequences for the nodes with the attribute which I want. I wish to find the paths in this directed graph from my parent node to these nodes with the attribute.

The only relevant command from igraph seems to be 'all_simple_paths' (if I were to use inbuilt functions for efficiency, rather than writing my own). I would then have to find a way to deal with the directionality issue.

However even as a preliminary approach, I cannot get all_simple_paths to work from igraph!

The error is thus:

Traceback (most recent call last): File "/homes/jlada/Documents/omnipath_folder/full_network/vss.py", line 3, in from igraph import all_simple_paths ImportError: cannot import name 'all_simple_paths' from 'igraph' (/nfs/software/software/Linux_x86_64/opt/stow/anaconda3/envs/pypath/lib/python3.7/site-packages/igraph/init.py)

Note: I have checked that I have the right version of igraph installed, i.e. python-igraph

And I am running the code:

from pypath import main, data_formats
import igraph
from igraph import all_simple_paths
import time


protein = 'BMP7'

max_order= 3


pa = main.PyPath()
pa.init_network(pfile = '/homes/jlada/Documents/omnipath_folder/mynetwork.pickle')


pa.set_transcription_factors()



#network_graph = pa.gs_neighborhood(protein, order = max_order)



sub_bmp7_2 = pa.get_directed(
    pa.graph.induced_subgraph(
        list(pa.gs_neighborhood(protein, order = max_order).vs()
    )
))




vsparent = [v for v in sub_bmp7_2.vs() if v['name']== protein]



#neighboraffects=set(pa.up_affects('BMP7').up())

tfs_ind=[v for v in sub_bmp7_2.vs() if v['tf']]

print(tfs_ind)


j = all_simple_paths(sub_bmp7_2, vsparent, tfs_ind[1], mode= "out")

print(j)

I thought I should check if the function all_simple_paths is in igraph, so have tried:

import pypath
import igraph
import inspect

print(inspect.getmembers(igraph))

The output is very long. Unfrtunately I do not know how to query the output to see if it contains the all_simple_paths. I did go through it manually, and as far as I could tell, there was no function all_simple_paths.

Has anyone had experience with igraph not containing all_simple_paths? Is this down to the version?

Meep
  • 351
  • 1
  • 4
  • 15
  • 1
    [This Previous Post](https://stackoverflow.com/q/3971876/4752675) may be helpful. – G5W Jul 17 '19 at 13:33
  • 1
    @21joanna12 I looked at the [documentation](https://igraph.org/python/doc/identifier-index.html) for the `igraph` API and `all_simple_paths` is not a listed method. There is [such a function](https://networkx.github.io/documentation/networkx-1.9/reference/generated/networkx.algorithms.simple_paths.all_simple_paths.html) in `NetworkX` - did you maybe mix these up? :) – HFBrowning Jul 18 '19 at 17:04
  • You might be confusing Python igraph with [R igraph](https://igraph.org/r/doc/all_simple_paths.html). – S.S. Anne Jul 19 '19 at 19:03

1 Answers1

0

igraph for Python does not contain all_simple_paths, however igraph for R does. You may have confused the two.

As per this answer, a simple solution can be concocted fairly easily (note that this may need a little adaptation to use with igraph):

def adjlist_find_paths(a, n, m, path=[]):
  "Find paths from node index n to m using adjacency list a."
  path = path + [n]
  if n == m:
    return [path]
  paths = []
  for child in a[n]:
    if child not in path:
      child_paths = adjlist_find_paths(a, child, m, path)
      for child_path in child_paths:
        paths.append(child_path)
  return paths

def paths_from_to(graph, source, dest):
  "Find paths in graph from vertex source to vertex dest."
  a = graph.get_adjlist()
  n = source.index
  m = dest.index
  return adjlist_find_paths(a, n, m)
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76