This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to use pysam to execute "samtools fastq xxx.sam>xxx.fastq"

I want to use pysam(a module for reading, manipulating and writing genomic data sets) to get the fastq file from the sam file without samtools as well as shell '>' operation.

I guess the command may be like this

pysam.fastq(.....,"xxx.sam")

My sam file is single-end, so I use '-s' but the result outputs to standard output.

pysam samtools

What have you tried and what happened?

I use the following in my python script pysam.fastq("-s","xxx.fastq","xxx.sam") and there is a file named "xxx.sam"but it is Zero bytes.


I also try this command samtools fastq -s xxx.fastq xxx.sam The fastq fromat data is the standard output, and there is a file named "xxx.sam" with Zero bytes as well.

And what's the reason you don't want to use samtools

I also try this command samtools fastq -s xxx.fastq xxx.sam

I'd suggest:

samtools fastq input.sam > output.fastq

I would like to write a python script by pysam with lots of complex filtering steps which include a step of "sam "to "fastq". It ls said that Pysam can use samtools commands(but I only find an example of sort in the manual).

If I am not able to find a solution of this question,I may put samtools in the software requirement list and use the ">" at last. Thank you~

Please use ADD COMMENT/ADD REPLY when responding to existing posts to keep threads logically organized.

2 answers

If you're going to make a script for this it's not much more effort to go all the way and do the conversion yourself:

This uses simplesam, which has a much simpler (get it?) API than pysam.

If the reads are paired, would this be much more complicated?

ADD COMMENT It is pity that the add comment/add reply button may go wrong in my Google/Firefox browser.

To Matt Shirley: Thank you very much , simplesam is good but I have found the solution of this question.

When I use samtools fastq xxx.sam>xxx.fastq ,the standard output includes the fastq data and a message "[M::bam2fq_mainloop] processed 200000 reads".

I infer the return of pysam.fastq() function may be a string, and I try to store it in a var and print it in python like this out_str = pysam.fastq("xxx.sam") . print(out_str) . The result only includes the fastq data and it can be stored by python' s file write function easily.


In a word, thank you all and the question has been solved.

Log in to answer this question.