Question: Pysam.view report 'pysam.utils.SamtoolsError: "samtools returned with error 1: stdout=, stderr=samtools view: invalid option -- ' ''
0
gravatar for SDin
11 days ago by
SDin0
Tsinghua University
SDin0 wrote:

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 • 90 views
ADD COMMENTlink modified 11 days ago by ATpoint14k • written 11 days ago by SDin0
1

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

ADD REPLYlink written 11 days ago by Vitis2.0k

It is already in the command line..

ADD REPLYlink written 10 days ago by SDin0
1

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

ADD REPLYlink written 10 days ago by Vitis2.0k

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

ADD REPLYlink written 9 days ago by SDin0
1

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 REPLYlink modified 9 days ago • written 9 days ago by Vitis2.0k

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 REPLYlink written 7 days ago by SDin0
1

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 REPLYlink modified 7 days ago • written 7 days ago by Vitis2.0k

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

ADD REPLYlink written 3 days ago by SDin0
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 2213 users visited in the last hour