Program to split bam file according to size
0
0
Entering edit mode
9.5 years ago
kavitarege ▴ 10
import os
import pysam
import sys
from MT_Connection import bamfile_path
import time

#------------open bam file
infile = sys.argv[1]
samfile = pysam.Samfile(infile, "rb")


#....This gives the size of the file
statinfo = os.stat(infile)
infile_Size=statinfo.st_size

#........output file.............
x=0
base=os.path.basename(infile)
os.path.splitext(base)
fileout_base = os.path.splitext(base)[0]
ext = ".bam"
fileout = fileout_base+"_" + str(x)+ext
print fileout
header = samfile.header

outfile = pysam.Samfile( bamfile_path+fileout, "wh",header = header)
sum_Outfile_Size=0

# In this segment I loop through each read and write to file, once the file reaches 1gb mark, the file is closed and new #file is open
for alignreads in samfile.fetch():
        outfile.write(alignreads)
        statinfo_out = os.stat(bamfile_path+fileout)
        outfile_Size = statinfo_out.st_size
        if(outfile_Size >=1073741824 and sum_Outfile_Size <= infile_Size):
            #print statinfo_out.st_size
            print(sum_Outfile_Size < infile_Size)
            sum_Outfile_Size = sum_Outfile_Size + outfile_Size
            x = x +1
            outfile.close()
            fileout = fileout_base+"_" + str(x)+ext
            outfile = pysam.Samfile( bamfile_path+fileout, "wh",header = header)

This is the program I wrote to split the file, Can you please help me and tell what is wrong with this

I am not able to index the files

samtools index path /to/bamfile.bam
[bam_header_read] EOF marker is absent. The input is probably truncated.
[bam_header_read] invalid BAM binary header (this is not a BAM file).
python bam bam-index • 2.4k views
ADD COMMENT
0
Entering edit mode

Doesn't a "wh" mode create a SAM file?

ADD REPLY
0
Entering edit mode

I am not sure, I followed the below link, to 'write to file from scratch'

https://media.readthedocs.org/pdf/pysam/latest/pysam.pdf

ADD REPLY
0
Entering edit mode
9.5 years ago

Try "wbh" and see if that fixes things.

ADD COMMENT
0
Entering edit mode

Thanks a lot, got it have to change to "wb", it will work

ADD REPLY

Login before adding your answer.

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