Entering edit mode
10.4 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).
Doesn't a
"wh"
mode create a SAM file?I am not sure, I followed the below link, to 'write to file from scratch'
https://media.readthedocs.org/pdf/pysam/latest/pysam.pdf