Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

pysam.view method silently fails to write BAM file when --output is used #1096

Closed
sbslee opened this issue Apr 4, 2022 · 2 comments
Closed

Comments

@sbslee
Copy link

sbslee commented Apr 4, 2022

Dear pysam team,

Thanks always for your hard work!

Today I'd like to report a potential bug in the pysam.view method. Basically, it appears that the method silently fails to write BAM file when --output is used.

For example, it correctly works if I simply print the output of the method:

(test) sbslee@Seung-beens-Mac-mini Desktop % cat test1.py 
import pysam
input_bam = 'HG00276_PyPGx.sorted.markdup.recal.bam'
region = 'chr22:42513500-42513510'
print(pysam.view(input_bam, region))
(test) sbslee@Seung-beens-Mac-mini Desktop % python test1.py | head -n 1
[W::hts_idx_load3] The index file is older than the data file: HG00276_PyPGx.sorted.markdup.recal.bai
HNMY2CCXX:3:1108:1832082:0	163	chr22	42513351	60	150M	=	42513555	354	TTTTTAATTCCACAGTGTTTGAAGGACATACTTGAAATAAATTTAAAATCCATTGAGATTTGTTTCGTGGTCCAGAATATGGCCTATCTTGGTGAATGTTTCATGTGTATGTGAAACTGAGGCATCCATCCCTCAGTTTCAACCACCCAT	?EEEEFIGHFFIEGFDGGIHHIKGGIEJIHFJHHILJHFDKIIIHKKKIGGDIJHCFDIIIHHIIG?HHGIGGDGIIDHIHGFGEGHGKIGGIHDKIHHII;KIHDGHB?:HHCD??J@DGFFJIGHJD<@1EFJ@DII@D4@=D@@;<D	MC:Z:150M	MD:Z:150	PG:Z:MarkDuplicates	RG:Z:2117	NM:i:0	AS:i:150	XS:i:27

However, if I try to write the output to a new BAM file using --output, it does nothing without producing any errors:

(test) sbslee@Seung-beens-Mac-mini Desktop % cat test2.py
import pysam
input_bam = 'HG00276_PyPGx.sorted.markdup.recal.bam'
output_bam = 'HG00276_PyPGx_CYP2D6.bam'
region = 'chr22:42513500-42513510'
pysam.view(input_bam, region, '--output', output_bam)
(test) sbslee@Seung-beens-Mac-mini Desktop % python test2.py 
[W::hts_idx_load3] The index file is older than the data file: HG00276_PyPGx.sorted.markdup.recal.bai

Link to test BAM file: https://1drv.ms/u/s!Apgoq3uQ2gCqgrsQmcNbCDYyw9rzsg?e=c86K1u
Link to test index file: https://1drv.ms/u/s!Apgoq3uQ2gCqgrsPD0TSk-2Kx1yDUg?e=TUYAUK

I'm using the latest version of pysam (0.19.0):

(test) sbslee@Seung-beens-Mac-mini Desktop % conda list | grep "pysam"
pysam                     0.19.0           py39h7ab3854_0    bioconda

I installed pysam using conda:

conda create -n test pysam=0.19.0

Thanks for your time!

@jmarshall
Copy link
Member

jmarshall commented Apr 4, 2022

If you wrapped print(…) around the call in test2.py, you would see that the output is captured as the return value of pysam.view() — just as in test1.py.

If you are using -o or --output or etc options to redirect the output yourself within the pysam.view() etc call, you need to use pysam.view(…, catch_stdout=False) as per #677 (comment) and #958 (comment) et al.

To be sure, this is not clearly documented.

@sbslee
Copy link
Author

sbslee commented Apr 4, 2022

@jmarshall

Thanks for the prompt answer. I missed those issues when I was searching through the repository. Will close this as it's a duplicated question.

@sbslee sbslee closed this as completed Apr 4, 2022
jmarshall added a commit that referenced this issue Jan 21, 2025
…tation

Describe both samtools and bcftools invocations. Supersedes #1275.
Add reference documentation covering all the keyword arguments, etc.

Describe in detail how to redirect output to a file, a common use case.
Fixes #1096, fixes #1241, fixes #1323, et al.

Update doc/conf.py's rst_epilog samtools and bcftools URLs to point to
version-specific man pages, and have devtools/import.py update the URLs.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants