进行subprocess.call命令

时间:2019-06-18 16:35:26

标签: python python-2.7

我通过联机检查器运行了脚本,并将其完全格式化。我希望它运行如下:

script.py --i smth

它执行subprocess.call并从raw_input定义字符串。

我认为subprocess.call SED是错误的。原始命令为sed -i 's/#include "martini.itp"/#include "martini_v2.1-dna.itp"\n#include "martini_v2.0_ions.itp"/' cg-1rna.top

import getopt
import sys
import subprocess

inputfile = ''
try:
    opts, args = getopt.getopt(sys.argv[1:], "v", ["ifile"])
    for opt, arg in opts:
        if opt in ("-i", "-sfile"):
            inputfile = arg
    subprocess.call(["grep", "-v", "HETATM", "rna.pdb", ">", "aa-1rna.pdb"])
    subprocess.call(["gmx", "editconf", "-f", "aa-1rna.pdb", "-o", "aa-1rna.gro"])
    subprocess.call(["python", "martinize-nucleotide.py", "-type", inputfile, "-f", "aa-1rna.gro", "-o", "cg-1rna.top", "-x", "cg-1rna.pdb"])
    subprocess.call(["cp", "cg-1rna.top", "cg-1rna.top.bak"])
    subprocess.call(["sed", "-i", "'s/#include", '"martini.itp"/#include', '"martini_v2.1-dna.itp"\n#include#', '"martini_v2.0_ions.itp"/', "cg-1rna.top"])
    subprocess.call(["vmd", "cg-1rna.pdb"])
    dvalue = raw_input("Cancel script using CTRL+C or write the distance between the first and last nucleotide with a total of 4 decimals (in nm): ")
    subprocess.call(["gmx", "editconf", "-f", "cg-1rna.pdb", "-d", dvalue, "-bt", "dodecahedron", "-o", "box.gro"])
    subprocess.call(["gmx", "solvate", "-cp", "box.gro", "-cs", "water.gro", "-o", "bw.gro"])
    subprocess.call(["vmd", "bw.gro"])
    wvalue = raw_input("Cancel script using CTRL+C or write the number of water molecules added: ")
    subprocess.call(["printf", '"\nW', "        ", wvalue + '"', ">>", "cg-1rna.top"])
    subprocess.call(["gmx", "grompp", "-f", "em.mdp", "-c", "bw.gro", "-p", "cg-1rna.top", "-o", "01-em"])
    nnvalue = raw_input("Warning: Last input step! Cancel script using CTRL+C or write the number of sodium ions to be added: ")
    subprocess.call(["gmx", "genion", "-s", "01-em.tpr", "-o", "bw.gro", "-p", "cg-1rna.top", "-pname", "NA", "-nname NA", "-nn", nnvalue])
    subprocess.call(["gmx", "mdrun", "-v", "-deffnm", "01-eq"])
    subprocess.call(["gmx", "grompp", "-f", "equil.mdp", "-c", "01-em.gro", "-p", "cg-1rna.top", "-o", "02-eq", "-maxwarn 1"])
    subprocess.call(["gmx", "mdrun", "-v", "-deffnm", "02-eq"])
    subprocess.call(["gmx", "grompp", "-f", "mdrun.mdp", "-c", "02-eq.gro", "-p", "cg-1rna.top", "-o", "output"])
    subprocess.call(["gmx", "mdrun", "-v", "-deffnm", "output"])
except getopt.GetoptError:
    print "rnascript.py --i <networkmodeltype>"
sys.exit(2)

1 个答案:

答案 0 :(得分:1)

subprocess.call的每个参数应该是一个单独的shell参数。

这将是一个shell参数:

's/#include "martini.itp"/#include "martini_v2.1-dna.itp"\n#include "martini_v2.0_ions.itp"/'

您已在内部将其拆分为空白:

subprocess.call(["sed", "-i", "'s/#include", '"martini.itp"/#include', '"martini_v2.1-dna.itp"\n#include#', '"martini_v2.0_ions.itp"/', "cg-1rna.top"])

sed收到这几个参数时,可能会不知道该怎么办并出错。看到错误了吗?我建议您下次阅读并包含在您的问题中。

尝试将单个shell参数作为单个参数发送到subprocess.call,也许是这样的:

subprocess.call(["sed", "-i", 's/#include "martini.itp"/#include "martini_v2.1-dna.itp"\n#include "martini_v2.0_ions.itp"/', "cg-1rna.top"])