This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Pysam VariantFile.fetch() without specifying contig

Hello,

I'm using the Pysam module of Python and calling the function:

VF = VariantFile("input_file")

VF.fetch()

seems to retrieve all reads in the file,

while this retrieves reads from positions 0 to 1000 in one specific contig:

VF.fetch(contig="contig_name", start=0, end=1000)

My question is: is it possible to retrieve sequences based on positions without specifying the contig name? Or better, specifying multiple contig names? (gives error, expects one string).

This is because I have a file with multiple contigs, but I would like to get for example all reads in positions 0:1000 in all contigs.

If I do this:

VF.fetch( start=0, end=1000)

It ignores my start and end specification, and retrieves all reads, like if it were no arguments inside fetch.

pysam python vcf

1 answer

Hello,

you can get all the contig names via VCF.header.contigs. This returns a view you can iterate over (untested):

for name in VCF.header.contigs:
    VF.fetch(contig=name, start=0, end=1000)

This presumes that the contigs are listed in the header.

fin swimmer

Brilliant! Sometimes you just have to go around the problems, not force your way through.

Danke schön!

Ricardo

Log in to answer this question.