How to use pysam to execute "samtools fastq xxx.sam>xxx.fastq"
2
0
Entering edit mode
6.7 years ago
Zhixue ▴ 10

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 • 4.5k views
ADD COMMENT
1
Entering edit mode

What have you tried and what happened?

ADD REPLY
1
Entering edit mode

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
ADD REPLY
0
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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~

ADD REPLY
0
Entering edit mode

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

ADD REPLY
2
Entering edit mode
6.7 years ago

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.

ADD COMMENT
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode
6.7 years ago
Zhixue ▴ 10

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.

ADD COMMENT

Login before adding your answer.

Traffic: 1661 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6