This is a test version of Biostars. For the public version, visit https://www.biostars.org.
appropriate way to use pysam.depth

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

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

Thanks,

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

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?

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')])

0 answers

No answers yet.

Log in to answer this question.