2

I am trying to pass in an integer value into python's subprocess.call method:

subprocess.call('samtools','view','-bq', '1','DS130.bam','>','DS130umr.bam')

The argument after '-bq' needs to be an integer value. However I believe that subprocess.call keeps treating it as a string and so I keep getting the error 'TypeError: bufsize must be an integer'.

How can I get subprocess.call to interpret that '1' as an integer value?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
JayKu12
  • 145
  • 2
  • 5
  • Does this answer your question? [Communicate with subprocess by sending int data](https://stackoverflow.com/questions/35171615/communicate-with-subprocess-by-sending-int-data) – alelom Aug 14 '22 at 19:45
  • This question is incorrectly titled as the problem has nothing to do with passing `int` data to subprocess – which is a topic on its own – but rather with incorrect argument passing. For a relevant question to passing `int`s to subprocess, see https://stackoverflow.com/questions/35171615/communicate-with-subprocess-by-sending-int-data. – alelom Aug 14 '22 at 19:45

1 Answers1

5

This has nothing to do with integers in your input; you are calling the function incorrectly. You need to pass in your command as one sequence, a list:

subprocess.call(['samtools','view','-bq', '1','DS130.bam','>','DS130umr.bam'])

This will not, however, be processed by a shell, so the output redirect (>) won't work. If you want to direct the output of the tool to another file, either use shell=True and pass in a string:

subprocess.call('samtools view -bq 1 DS130.bam > DS130umr.bam', shell=True)

for the shell to process, or use output redirection:

with open('DS130umr.bam', 'wb') as redirected:
    subprocess.call(['samtools','view','-bq', '1','DS130.bam'], stdout=redirected)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • I was able to execute subprocess.call with the argument shell=True correctly. However when I tried using output redirection, I got this error, ValueError: stdout argument not allowed, it will be overridden. I ended up doing this instead and it worked: with open('DS130umr.bam', 'wb') as sys.stdout: subprocess.check_output(['samtools','view','-bq', '1','DS130.bam']) – JayKu12 Jan 21 '14 at 20:25
  • @JayKu12: Sorry, I meant to use `subprocess.call()` instead; you need to be wary of assigning to `sys.stdout`; you'll now need to reinstate the original with `sys.stdout = sys.__stdout__`. – Martijn Pieters Jan 21 '14 at 21:34