def reads_to_sam(reads,bam,bp1,bp2,dirout,name):
'''
For testing read assignemnts.
Takes reads from array, matches them to bam
file reads by query name and outputs them to Sam
'''
bamf = pysam.AlignmentFile(bam, "rb")
loc1 = '%s:%d:%d' % (bp1['chrom'], bp1['start'], bp1['end'])
loc2 = '%s:%d:%d' % (bp2['chrom'], bp2['start'], bp2['end'])
iter_loc1 = bamf.fetch(region=loc1,until_eof=True)
iter_loc2 = bamf.fetch(region=loc2,until_eof=True)
loc1 = '%s-%d' % (bp1['chrom'], (bp1['start']+bp1['end'])/2)
loc2 = '%s-%d' % (bp2['chrom'], (bp1['start']+bp1['end'])/2)
sam_name = '%s_%s-%s' % (name,loc1,loc2)
if not os.path.exists(dirout):
os.makedirouts(dirout)
bam_out = pysam.AlignmentFile('%s/%s.sam'%(dirout,sam_name), "w", header=bamf.header)
for x in iter_loc1:
if len(reads)==0:
break
if x.query_name in reads:
bam_out.write(x)
bam_out.write(bamf.mate(x))
idx = int(np.where(reads==x.query_name)[0])
reads = np.delete(reads,idx)
for x in iter_loc2:
if len(reads)==0:
break
if x.query_name in reads:
bam_out.write(x)
bam_out.write(bamf.mate(x))
idx = int(np.where(reads==x.query_name)[0])
reads = np.delete(reads,idx)
bamf.close()
bam_out.close()
评论列表
文章目录