appropriate way to use pysam.depth
0
0
Entering edit mode
2.8 years ago
Medhat 8.9k

What is the right way to use pysam.[some tool]

This for example works,

merge_parameters = ["-@", "4", "-r", output] + input_bam
pysam.merge(*merge_parameters)

But this does not work:

depth = [my_input_bam] + [">", "my.depth"]
pysam.depth(*depth)

Nor this works:

pysam.depth(my_input_bam, ">", "my.depth")

Error will be:

pysam.utils.SamtoolsError: 'samtools returned with error 1: stdout=,
stderr=samtools depth: Could not open ">": No such file or
directory\n'

Is there a special way to use pysam.depth? (how to use pysam.tool the correct way)

python pysam • 1.9k views
ADD COMMENT
0
Entering edit mode

Hey Medhat ,

I guess the order of arguments matters and must fit the needs of the samtoolsprogram. For samtools mergethis would be:

$ Usage: samtools merge [-nurlf] [-h inh.sam] [-b <bamlist.fofn>] <out.bam> <in1.bam> [<in2.bam> ... <inN.bam>]

As you see the output is not streamed here and a file must be given explicit.

fin swimmer

ADD REPLY
0
Entering edit mode

Thanks,

The merge in my case is working while pysam.depth is the one that does not work.

ADD REPLY
0
Entering edit mode

I found that the way to write out the result of pysam.depth:

for line in pysam.depth("myfile.bam"):
    print(line)

Which is not convenient.
Is there a better way?

ADD REPLY
0
Entering edit mode

you can parse the output into a dataframe

import pandas as pd

df = pd.DataFrame([x.split('\t') for x in pysam.depth('sample.bam').split('\n')])
ADD REPLY

Login before adding your answer.

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