Pysam.view report 'pysam.utils.SamtoolsError: "samtools returned with error 1: stdout=, stderr=samtools view: invalid option -- ' ''
0
0
Entering edit mode
5.7 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 • 4.6k views
ADD COMMENT
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode

It is already in the command line..

ADD REPLY
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
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")

Please see the documentation here:

https://pysam.readthedocs.io/en/latest/usage.html#using-samtools-commands-within-python

ADD REPLY
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?

ADD REPLY
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.

ADD REPLY
0
Entering edit mode

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

ADD REPLY

Login before adding your answer.

Traffic: 1953 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