Filter BAM by chromosome using PySam
0
0
Entering edit mode
18 months ago
pablosolar.r ▴ 20

Hi all!

I am trying to filter a BAM by chromosome using PySam.

Using Samtools:

samtools view -h mybam.bam 22 > mybam.bam.chr22.sam
samtools view -bS mybam.bam.chr22.sam > mybam.bam.chr22.bam

Using PySam:

filtered_sam = pysam.view("-h", bam_path, chromosome) # This is returning a simple string instead of a PySam object
return pysam.view("-bS", filtered_sam) # This doesn't seem to work

What I want is to read the BAM, filter it out by chromosome but not saving the extra .sam/.bam files. Just read it, filter and the loop thought it by using:

for read in filtered_bam:
    ....

I also tried this without succeed:

pysam.AlignmentFile(pysam.view(bam_path, chromosome, '-b'))
...
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x8b in position 1: invalid start byte

What I am doing wrong? Thank you very much!

Pysam BAM Python Samtools • 404 views
ADD COMMENT

Login before adding your answer.

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