pysam-developers / pysam

Pysam is a Python package for reading, manipulating, and writing genomics data such as SAM/BAM/CRAM and VCF/BCF files. It's a lightweight wrapper of the HTSlib API, the same one that powers samtools, bcftools, and tabix.
https://pysam.readthedocs.io/en/latest/
MIT License
774 stars 274 forks source link

running samtools view command produces no output #1241

Open robertwhbaldwin opened 10 months ago

robertwhbaldwin commented 10 months ago

greetings,

I was using the samtools view command as follows: samtools view -@ 10 -O BAM -o paired.bam -f 1 fixed.bam and it worked fine the output bam is there.

But when I use the same command in pysam: pysam.view("-@",str(threads), "-o", "test2.bam", "-O", "BAM", "-f","1","fixed.bam")

There's no output. Even when I remove the -o there's nothing to the terminal window. This is true even if I change the command to just convert a sam to bam. Running other samtools commands inside pysam like sort and index do work however.

jmarshall commented 10 months ago

By default, pysam.view() captures the output and returns it as the function's value. So you would see the output with print(pysam.view(…)).

You can prevent this by adding …, catch_stdout=False to enable your -o option to take effect. See also #1096.

At present pysam.samtools.*(…) and pysam.bcftools.*(…) are a bit too clever with I/O redirection. In particular, pysam.view()/pysam.samtools.view() will add its own -o output to effect the default redirection, which overrides your -o option and filename — hence why you need catch_stdout=False explicitly. In future I plan to change this to use more straightforward descriptor dup(2)-ing, which will make this more reliable and remove the need for this extra argument.