随着Python中的每个循环迭代使用生成的文件进行子进程调用

时间:2015-06-11 13:46:41

标签: python loops subprocess bioinformatics

我有一个输入文件(inputFile)需要经历六个过滤步骤和六个不同的'BED'文件。在每个过滤器之后,会产生一个新的输入文件。我需要使用它们来输入我的循环的下一个迭代,依此类推,直到初始文件与我的所有六个BED文件进行了六次迭代。

这是我目前的代码。该循环适用于前两个调用,但随后停止并表示它无法打开在第二次迭代后创建的文件。任何意见是极大的赞赏。

samples = [0, 1, 2, 3, 4, 5]
fileName = ["first", "second", "third", "fourth", "fifth"]
inputFile = fileOne
for x in samples:
   orderName = fileName[x] + sampleCapture + ".bed"
   outputFile = open(orderName, "w")
   bedFile = filterFiles[x]
   subprocess.Popen(["bedtools", "intersect", "-a", inputFile, "-b", bedFile, "-v"], 
                    stdout=outputFile)
   outputFile.close()
   inputFile = fileName[x] + sampleCapture + ".bed"

1 个答案:

答案 0 :(得分:1)

Popen立即返回。您应该使用check_call()代替(@cel mentioned):

from subprocess import check_call

input_filename = "fileone"
for filename, bed_filename in zip(["first", "second", "third", "fourth", "fifth"],
                                  filter_files):
  args = ["bedtools", "intersect", "-a", input_filename, "-b", bed_filename, "-v"]
  output_filename = filename + sample_capture + ".bed"
  with open(output_filename, "wb", 0) as output_file:
    check_call(args, stdout=output_file)
  input_filename = output_filename