Pysam.view report 'pysam.utils.SamtoolsError: "samtools returned with error 1: stdout=, stderr=samtools view: invalid option -- ' ''
0
0
Entering edit mode
4.1 years ago
SDin • 0

Hi there, I am a beginner in this area. I am trying to use pysam for extract some reads.

My code is

pysam.view("-u -f 8 -F 260","/usr/local/virussite/input.bam","-o","output2.bam")


and

pysam.view("-u -f 8 -F 260","/usr/local/virussite/input.bam > output2.bam")


both will report

pysam.utils.SamtoolsError: "samtools returned with error 1: stdout=, stderr=samtools view: invalid option -- ' '


I have no idea about it... Would you mind give me some advice? Many thanks!

pysam samtools • 3.3k views
1
Entering edit mode

Can you run the "samtools view " in the command line before wrapping it in the python script?

0
Entering edit mode

It is already in the command line..

1
Entering edit mode

I mean, did you run "samtools view " in a terminal? Does the same command return the same error message?

0
Entering edit mode

Hmmm, no, the 'samtools view' works. So it is quite weird..

1
Entering edit mode

I think you have to arrange the pysam.view() wrapper in a different way: different components of the command need to be put in separate quotes. For example:

pysam.sort("-o", "output.bam", "ex1.bam")

The samtools command is:

samtools sort -o output.bam ex1.bam

So your wrapped code need to be in a form like this (you can experiment with it):

pysam.view("-u", "-f", "8", "-F", "260", "/usr/local/virussite/input.bam", "-o", "output2.bam")

0
Entering edit mode

Thanks for your kind help, it can run now. But when I run the code, there are some messy code on the screen: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\x00\xdbx:Z\xe0\x9d\x00\x00\x1f\x8b\x08\x04\x00\x00\x00\x00\x00\xff\x06\x00BC\x02\x00\x1b\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00'

and I can not get the result, do you have any idea of it?

1
Entering edit mode

You should not get anything from the screen if everything runs as expected, as you're putting the results to the file "output2.bam". What you see here is the binary header of a BAM, which means you're not storing the output into a file, instead printing it out in standard output. See this:

Error Filtering BAM file based on mapping qualities in python's pysam

Maybe it is the order, for "samtools view" the input bam should be the last argument, before "-o", like this:

pysam.view("-u", "-f", "8", "-F", "260", "-o", "output2.bam", "/usr/local/virussite/input.bam")

Also, I don't recommend using pysam this way. The filtering could be done with the normal "samtools view" in command line. You don't have to wrap it in python. The last bit of discussion in aforementioned post provided a way to correctly use pysam and samtools in appropriate ways.

0
Entering edit mode

I decide to use command line mode of samtools at last, thanks for your kind help.