pythonsubprocesscallopenbabel

Run external program to each element of list


I'm trying to call an external program (Openbabel) to each element (molecule) in a list of molecules (SMILES format). However, I keep getting the same error:

/bin/sh: 1: Syntax error: "(" unexpected (expecting ")").

What is wrong with my code?

from subprocess import call

with open('test_zinc.smi') as f:
    smiles = [(line.split())[0] for line in f]

def call_obabel(smi):
    for mol in smi:
        call('(obabel %s  -otxt -s %s -at %s -aa)' % ('fda_approved.fs', mol, '5'), shell=True)

call_obabel(smiles)

Solution

  • subprocess.call requires an iterable of commands and arguments. If you need to pass command-line args to a process, they belong in an iterable. It's also not recommended to use shell=True as it can be a security hazard. I'll omit it below.

    Try this:

    def call_obabel(smi):
        for mol in smi:
            cmd = ('obabel', 'fda_approved.fs',  '-otxt', '-s', mol, '-at', '5', '-aa')
            call(cmd)