Silvia VC
Silvia VC

Reputation: 45

How to read and change BAM files from a Python script?

I'm planning on using a Python script to change different BAM (Binary Alignment Map) file headers. Right now I am just testing the output of one bam file but every time I want to check my output, the stdout is not human readable. How can I see the output of my script? Should I use samtools view bam.file on my script? This is my code.

#!/usr/bin/env python

import os
import subprocess


if __name__=='__main__':
    for file in os.listdir(os.getcwd()):
        if file == "SRR4209928.bam":
            with open("SRR4209928.bam", "r") as input:
                content = input.readlines()
                for line in content:
                    print(line)

Upvotes: 1

Views: 6125

Answers (1)

accdias
accdias

Reputation: 5372

Since BAM is a binary type of SAM, you will need to write something that knows how to deal with the compressed data before you can extract something meaningful from it. Unfortunately, you can't just open() and readlines() from that type of file.

If you are going to write a module by yourself, you will need to read Sequence Alignment/Map Format Specification.

Fortunately someone already did that and created a Python module: You can go ahead and check pysam out. It will surely make your life easier.

I hope it helps.

Upvotes: 6

Related Questions