This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Program to split bam file according to size
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

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

1 answer

Try "wbh" and see if that fixes things.

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

Log in to answer this question.