Question

I am using python to run a terminal program call bedtools. Bedtools takes file names as arguments. However, because I can only have 256 files open at once, I am limited in my multithreading ability. I was hoping to be able to send a file in memory to the terminal, but it doesn't have a name, like a SpooledTemporaryFile(). Is there a way to get around this?

example:

f=tempfile.NamedTemporaryFile(delete=True)
region=tempfile.SpooledTemporaryFile()
region.write(b'chr1\t1090917\t1136917\n')
region.seek(0)
subprocess.call(['/usr/local/bin/shuffleBed', '-incl',str(region),'-i','temp'+chromosome+'ShuffleMutations.bed','-g','hg19.genome'],stdout=f)

is there a away to give region a name so the terminal can find it?

Was it helpful?

Solution

Use NamedTemporaryFile to create the input file. If you close it, there should be no drain on file descriptors.

f=tempfile.NamedTemporaryFile(delete=True)
region=tempfile.NamedTemporaryFile(delete=False)
region.write(b'chr1\t1090917\t1136917\n')
region.close()
subprocess.call(['/usr/local/bin/shuffleBed', '-incl',region.name,'-i','temp'+chromosome+'ShuffleMutations.bed','-g','hg19.genome'],stdout=f)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top