With every loop iteration in Python use resulting file for subprocess call

451 views Asked by At

I have one input file (inputFile) that needs to undergo six filtering steps with six different 'BED' files. After every single filter, there results a new input file. I need to use these for input to the next iteration of my loop and so on until the initial file has undergone six iterations with all six of my BED files.

Here is my current code. The loop works for the first two calls but then stops and says that it cannot open the file that got created after the second iteration. Any advice is greatly appreciated.

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

There are 1 answers

0
jfs On

Popen returns immediately. You should use check_call() instead (as @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