简体   繁体   中英

subprocess.call to run mafft

I wrote a script to run mafft module from the terminal:

 import subprocess


def linsi_MSA(sequnces_file_path):
    cmd = ' mafft --maxiterate 1000 --localpair {seqs} > {out}'.format(seqs=sequnces_file_path, out=sequnces_file_path)
    subprocess.call(cmd.split(), shell=True)

if __name__ == '__main__':
    import logging
    logger = logging.getLogger('main')
    from sys import argv
    if len(argv) < 2:
        logger.error('Usage: MSA <sequnces_file_path> ')
        exit()
    else:
        linsi_MSA(*argv[1:])

for some reason when trying to run the script from the terminal using:

python ./MSA.py ./sample.fa

I get the mafft interactive version opening directly in the trminal (asking for input ..output etc..)

when i'm trying to write the cmd directly in the terminal using:

mafft --maxiterate 1000 --localpair sample.fa > sample.fa 

its working as expected and perfoming the command line version as without opening the interactive version.

I want my script to be able to perform the cmd line version on the terminal. what seems to be the problem?

thanks!

If you use shell=True you should pass one string as argument, not a list, eg:

subprocess.call("ls > outfile", shell=True)

It's not explained in the docs, but I suspect it has to do with what low-level library function is ultimately called:

call(["ls", "-l"]) --> execlp("ls", "-l")

      ^^^^^^^^^^              ^^^^^^^^^^
call("ls -l", shell=True) --> execlp("sh", "-c", "ls -l")
     ^^^^^^^                                     ^^^^^^^ 

call(["ls", "-l"], shell=True) --> execlp("sh", "-c", "ls", "-l")

# which can be tried from command line:
sh -c ls -l
# result is a list of files without details, -l was ignored.
# see sh(1) man page for -c string syntax and what happens to further arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM