#!/media/gyb/Seagate_Expansion_Drive/F/data/tools/smrtlink_bin/install/smrtlink-release_5.1.0.26412/bundles/smrttools/install/smrttools-release_5.1.0.26366/private/thirdparty/python/python_2.7.9/bin/python
import sys,os,re
from collections import defaultdict
## sam to gff format
#m170520_035125_42221_c101189592550000001823281009291703_s1_p0/55488/29_1649_CCS@0_0	0	Chr01	55154	40	562M250N246M122N141M2541N48M111N636M	*	0	0	AGCTGGACAAAACATCTGCTATGAGCATTATTGATAGATGTAACAACCTTGCCAGTGCCACACTATCAAAATTAAAATAAAAAATTTAACAACTACAATTATCTGATTCGTTTCTTCTGTTATTCACTACATAGAGCGCACTGTAAACTATCTCCGAAACTTTTCTGTTCATCATCATACAGATAAATCGGATTATTAGACTTGAATGAGAAAAAAAAAATAGGGAAGATTGAAAGAACAGAAACACAAACTGACCACAGTCACACCCTAATCAAACATCTACTTTGATGTGGCAAAGCAACCCCTTGACAGAATACAGAGTGATATGCTTCTTCACAAACCCACTATAGTCTTCACCGTTTCCTTTTTTCTCTCTATCTCTCTCTGCAAACCTTCAAATCACAAGCATTCATCTTCGAGGGGGGTCTTTGGTCCGAATATTAGCTCCTGTCATTCCTTCAATTCGATCTTTGGTTATCTCCACAAAATGACCCACAATCTCATGAAACTGCTTCAGTCCAGAAAGTGGGAGAATTATGGAGGACCTATCATTACCTGCAACCTCAGAAATCCTCAGAAAATGACCTCTGTTGTTGCTACCCAGATCAAAGAAGAATCTTTTCTGATCAACTCTGATTACCTTTGATACCCCCAGATTACCAATTTCATCCTGTGGGGGCAGGTCAACCGATCTGTCTACATTCAGTTCAGAAGTAGGCGCGGGTGATTGGCTACTGTGCCCAGATATGAAGCCAGCCCCTACGTCATCAGAAAGTCCAACAAGTTGCTCTGAAGTTTCAGAACTTTGCTGATTGGGCAGCAAAAAAAGTCTTGATGCTTCATTGATCTCAGCCAGAATGTTCCTAAACGCTGCCCACCCTTCATCCCGCGAACTTCCAGCAGGAATAATAATAGTACTTCGGTTTCTACTGACAGAAGCTTCAGACACCTTCAAGAAGCGGCCCCTTCTGTTTTCACCAATGTCAAAGTAGAAAACCTTGGTGTCGAGCTGCAATTCCTTGCTAAAGAGGTCCTGATCATCAGCAGAATTATCAACGTAGTGATTGATGAGATCCAAGAACCAAGAAATCCCAGAGAAGGGGACGATGATGGTGGACCTGGTGGCTGAAGTCTTCTCCGATATCTTCAGGTAACGACCCCGAGGATTCTCTTTTAGATCAAAATAGAAAAGCTTGTGTTCCACTTGTAGAGTCTTACACATGAGCTCAACATCATTCCCTCCTCCTCCTCCACCCGCTGCTGCTGCTCCACTGCCACTACCTCCACCAGAATTCCCCTCCATCTAGTCTACTAAATAATAATAAGTAATTTTGATTTAAAAAAAAGGACTGGTGATGGAGGAGGTAGATCTGAAAGATAACGATAGATTGCCACACACTCCAATCGGGAAGAGCAGGAGCAGCAAAAGCAAGAACAACAATAAGAAGAGTAAGATGTGGAATTTAGAGGTTTAGGAGGAAGATCGAGGAGGAGGATGGCGTAATGGAATTTTGGTTTAGGGTTCGGAGAGAGAAGAGAAGAGAAGAGGGGAAGGGGAAAAAGAGAAACTGAAGCAAGAGGGATAGTGAACGAACCACCATACCATTGACTTGGCCCCATTTTCATTTGGTTT	*	MD:Z:1633	NH:i:1	HI:i:1	NM:i:0	SM:i:40	XQ:i:40	X2:i:0	XO:Z:UU	XS:A:-	XG:A:M	XD:Z:GT-AG,GT-AG,GT-AG,GC-AG	XL:i:0	XR:i:0
#Chr09;0	9358965	9360703	-	M170519_231720_42221_C101189592550000001823281009291702_S1_P0/16353/1077_58_CCS.PATH1	9360446:9360703	9360054:9360127	9358965:9359651	
import pysam
bam=sys.argv[1]
bamfile = pysam.Samfile(bam , 'rb' )
trimMax=5
#############
sortlist=defaultdict(dict)
###########
for read in bamfile:
	chrom = bamfile.getrname(read.tid)
	start = read.blocks[0][0]+1
	end   = read.blocks[-1][1]
	tDict = dict(read.tags)
	qname=read.qname.split('@')[0]
	l=""
	####################################################################
	if tDict['XS'] == '+' and tDict['XR'] > trimMax or \
		tDict['XS'] == '-' and tDict['XL'] > trimMax :
		pass
	else:
		continue
	####################################################################
	if tDict['XS'] == '+':
		l+="%s\t%s\t%s\t%s\t"%(start,end,tDict['XS'],qname)
	for x,y in read.blocks:
		if tDict['XS'] == '+':
			l+="%d:%d\t"%(x+1,y)
		elif tDict['XS'] == '-':
			l="%d:%d\t"%(x+1,y)+l
	if tDict['XS'] == '-':
		l="%s\t%s\t%s\t%s\t"%(start,end,tDict['XS'],qname)+l
	sortlist[chrom][l]=1
#############
for i in sortlist:
	c=0
	for j in sorted(sortlist[i],key=lambda d:int(d.split()[0])):
		c+=1
		print "%s;%d\t%s"%(i,c,j)
##############
