-1

I am having a problem with displaying the result of my code despite following the many answers provided here.

How do I get the result of running a python script I wrote. It takes a directory as input from command line and should return the index files of executing bwa index script but I am not getting any output.Here is my code:

def bwaIndex(argv):
    input_dir = sys.argv[1]
    script = 'bwa index'
    for infile in glob.glob(os.path.join(input_dir,"*.fasta")):
        if infile.endswith('*.fasta'):
            subprocess.call(["script, infile"])


if __name__ == '__main__':
    bwaIndex(sys.argv)

I am not sure what I am doing wrong or if I am even right to an extent.

Amir
  • 10,600
  • 9
  • 48
  • 75
rose
  • 9
  • 6
  • Given your argument to `subprocess.call` is a single string with a comma in it, I'm guessing that's at least part of the problem, but perhaps that was just a transcription error? If you have a file called `bwa index` with the space in the name your argument to `call` could be `[script, infile]` without quotes, but I bet you really want `bwa` and `index` to be different arguments, right? So you invoke `bwa` and pass it the first argument as `index`? – Eric Renouf Dec 31 '15 at 01:51
  • To index the fasta file, I need to invoke the command bwa index because index alone will not work since it is part of bwa. That was why I passed it as script in my code. Not sure if I am right but I tried earlier without the quotes in [script, infile] but it did not work. I am actually getting no result at all – rose Dec 31 '15 at 02:00
  • 2
    Possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) –  Dec 31 '15 at 02:04
  • I went through that earlier. it seem not to answer my question Jarrod – rose Dec 31 '15 at 02:10

2 Answers2

3

The argument to subprocess.call is a list of arguments, it does not do any wordsplitting or pass it to the shell (unless you specify shell=True but that way is error prone and generally not recommended). So each "word" or each thing you want to have treated as a separate argument should be its own item in the list. In this case, you want 3 arguments, bwa (the executable to run), index (the command to have bwa run) and the file to operate on, which is stored in infile. So you probably want to do something like the following:

def bwaIndex(input_dir):
    for infile in glob.glob(os.path.join(input_dir, "*.fasta")):
        subprocess.call(['bwa', 'index', infile])

if __name__ == '__main__':
    bwaIndex(sys.argv[1])

Note that I changed the argument to bwaIndex to be the directory instead of an unused reference to sys.argv and also since the glob will only expand to things that end in fasta I dropped that inner check. Then I made your arguments to subprocess.call be the tokens you want to execute.

Eric Renouf
  • 13,950
  • 3
  • 45
  • 67
  • Thank you Eric for the clear explanation. I understand how subprocess works now and yes that sys.argv was really unused. It works now. Thank you – rose Dec 31 '15 at 02:26
0

Instead of using subprocess.call() you can use subprocess.Popen()

from subprocess import Popen, PIPE
p = Popen(['program', 'arg1'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate()
print output

see here more info about subprocess

sathish
  • 149
  • 9