0

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"
Alexander
  • 105,104
  • 32
  • 201
  • 196
dahlia
  • 283
  • 1
  • 5
  • 18
  • 1
    using "with" will help you write nicer code: http://stackoverflow.com/questions/9282967/how-to-open-a-file-using-the-open-with-statement – Paul Collingwood Jun 11 '15 at 13:48
  • I guess `subprocess.Popen` is not what you want. Instead [`subprocess.check_call`](https://docs.python.org/2/library/subprocess.html#subprocess.check_call) might be more suitable in your case. – cel Jun 11 '15 at 18:33

1 Answers1

0

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
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670