3

I know this question have been asked multiple times, I read multiple questions trying to solve this issue. However, none of those actually worked.

I have a python script I downloaded from: https://github.com/endrebak/kg

I'm trying to run the following command from inside python. It works when I run it directly from the terminal but throws an error when I run it from inside python:

/usr/packages/kg-master/bin/kg --mergecol=0 --noheader --genes --definition --species=hsa <(echo 01200)

using the following code:

pathwayID = 01200

cmd="/usr/packages/kg-master/bin/kg --mergecol=0 --noheader --genes --definition --species=hsa <(echo {})".format(pathwayID)

tmp = os.popen(cmd).read()

However, I'm getting the following error:

sh: -c: line 0: syntax error near unexpected token `('

sh: -c: line 0: `/usr/packages/kg-master/bin/kg --mergecol=0 --noheader --genes --definition --species=hsa <(echo 05200)'

I tried multiple suggestions, like adding python before calling the script

cmd="python /usr/packages/kg-master/bin/kg --mergecol=0 --noheader --genes --definition --species=hsa <(echo {})".format(pathwayID)

Another suggestion was using:

subprocess.call(['/usr/packages/kg-master/bin/kg', "--mergecol=0","--noheader","--genes","--definition","--species=hsa <(echo '01200')"])

This solution was the closest to solving the issue since the actual script is executed. however, it seems that the parameters are not being passed correctly which I don't know why.

any help would be appreciated.

ifreak
  • 1,726
  • 4
  • 27
  • 45

2 Answers2

2

To run this command with subprocess, you need to use a shell that understands the process substitution syntax, e.g. bash. /bin/sh, which is the default shell used by subprocess, doesn't support it.

import subprocess

cmd = ("/usr/packages/kg-master/bin/kg --mergecol=0 --noheader --genes"
       "--definition --species=hsa <(echo {})".format(pathwayID))

process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE, executable="/usr/bin/bash")
out, err = process.communicate()

Alternatively, you can save the ID to a temporary file and use input redirection (<).

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
0

Try this:

import subprocess

cmd="/usr/packages/kg-master/bin/kg --mergecol=0 --noheader --genes --definition --species=hsa <(echo {})".format(pathwayID)

process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()

The output stream of the command will be piped into in "out" and the error stream will be piped into "err".

David Brown
  • 908
  • 2
  • 13
  • 25