Question: appropriate way to use pysam.depth
0
gravatar for Medhat
22 months ago by
Medhat8.7k
Texas
Medhat8.7k wrote:

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)

pysam python • 1.1k views
ADD COMMENTlink modified 22 months ago • written 22 months ago by Medhat8.7k

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 REPLYlink modified 22 months ago • written 22 months ago by finswimmer13k

Thanks,

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

ADD REPLYlink written 22 months ago by Medhat8.7k

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 REPLYlink modified 22 months ago • written 22 months ago by Medhat8.7k

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 REPLYlink modified 12 days ago by finswimmer13k • written 12 days ago by sashitt20
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: 888 users visited in the last hour