【问题标题】:subprocess.call to run mafftsubprocess.call 运行 mafft
【发布时间】:2019-05-12 19:32:25
【问题描述】:

我写了一个脚本来从终端运行 mafft 模块:

 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:])

由于某种原因,尝试从终端运行脚本时使用:

python ./MSA.py ./sample.fa

我在终端中直接打开了 mafft 交互式版本(要求输入..输出等)

当我尝试使用以下命令直接在终端中编写 cmd 时:

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

它按预期工作,并且在不打开交互式版本的情况下执行命令行版本。

我希望我的脚本能够在终端上执行命令行版本。似乎是什么问题?

谢谢!

【问题讨论】:

  • 对代码的小修正:删除 mafft 之前的空格;确保文件名不包含空格(如果包含空格,请不要使用split 命令,而是使用更复杂的内容,例如列表或 shlex。

标签: python command-line terminal subprocess bioinformatics


【解决方案1】:

如果你使用shell=True,你应该传递一个字符串作为参数,而不是一个列表,例如:

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

文档中没有解释,但我怀疑它与最终调用的低级库函数有关:

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.

【讨论】:

    猜你喜欢
    • 2020-08-22
    • 2018-03-07
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多