import pysam
import argparse
from numpy import mean, median

def Parser():
	the_parser = argparse.ArgumentParser()
	the_parser.add_argument('--input', action="store", type=str, help="input BAM file")
	the_parser.add_argument('--output', action="store", type=str, help="output tabular file")

	args = the_parser.parse_args()
	return args

bamfile= pysam.AlignmentFile("/home/artbio/Bureau/input.bam", "rb")
output = open("/home/artbio/Bureau/output2.tab", "w")

def Ref_length(bamfile):
	Ref = bamfile.references #Reference names
	lengths = bamfile.lengths # Reference lengths
	filename = bamfile.filename # filename
	ref_length = {}
	j=0
	for ref in Ref:
		ref_length[ref]=lengths[j]
		j += 1
	return ref_length		



def Ref_coord_pol(bamfile):
	ref_coord ={}
	i=0
	for read in bamfile:
		polarity = "Forward" if (not read.is_reverse) else "Reverse"
		if not read.is_unmapped:
			read_rname = read.reference_name
			read_position = read.pos
			
			if ref_coord.has_key(read_rname): 
				
				if int(ref_coord[read_rname][i])==read_position:
					ref_coord[read_rname][i] += 1 
				else : 
					ref_coord[read_rname].append(1)
					i = i+1				
			else :	
				ref_coord[read_rname] = [1]
				i=0			
		continue
		ref_coord[read_rname].append(polarity)
	return ref_coord



			
output.close()
bamfile.close()

def main(infile, output):
	
	print "\n"

if __name__ == "__main__":
	args= Parser()
	main(args.input, args.output)


@@ -753,20 +753,16 @@
 ord =%7B%7D%0A
-%09i=0
 %0A%09for re
@@ -933,36 +933,26 @@
 .pos
-%0A%09%09%09%0A%09%09%09if ref_coord.has
+ + 1%0A%09%09%09the
 _key
+ = 
 (rea
@@ -962,123 +962,106 @@
 name
-): %0A%09%09%09%09%0A%09%09%09%09if int(ref_coord%5Bread_rname%5D%5Bi%5D)==read_position:%0A%09%09%09%09%09ref_coord%5Bread_rname%5D%5Bi
+, read_position)%0A%09%09%09if ref_coord.has_key(the_key): %0A%09%09%09%09nmbr_read = ref_coord%5Bthe_key%5D%5B0
 %5D +
-=
  1
- %0A%09%09%09%09else : 
 %0A%09%09%09%09
-%09
 ref_
@@ -1070,44 +1070,39 @@
 ord%5B
-read_rname%5D.append(1)%0A%09%09%09%09%09i = i+1%09%09
+the_key%5D= (nmbr_read, polarity)
 %09%09%0A%09
@@ -1129,33 +1129,36 @@
 ord%5B
-read_rname%5D = %5B1%5D
+the_key%5D = (1,polarity)
 %0A%09%09%09%09
-i=0
 %09%09%09%0A
@@ -1173,144 +1173,555 @@
 ue%0A%09
+%0A
 %09re
-f_coord%5Bread_rname%5D.append(polarity)%0A%09return ref_coord%0A%0A%0A%0A%09%09%09%0Aoutput.close()%0Abamfile.close()%0A%0Adef main(infile, output):%0A%09%0A%09print
+turn ref_coord #dictionary %7B(read_rname,read_position):(nmbr_read,polarity)%7D%0A%0A%0A%0Adef main(infile, output):%0A%09with pysam.AlignmentFile(infile,%22rb%22) as infile:%0A%09%09with open(output,%22a%22) as outfile:%0A%09%09%09outfile.write(%22chromo%22 + %22%5Ct%22 + %22coord%22 + %22%5Ct%22 + %22nbr-reads%22 + %22%5Ct%22 + %22polarity%22 + %22%5Cn%22)%0A%09%09%09ref_coord = Ref_coord_pol(infile)%0A%09%09%09ref_length = Ref_length(infile)%0A%09%09%09for read in ref_coord:%0A%09%09%09%09outfile.write(str(read%5B0%5D) + %22%5Ct%22 + str(ref_length%5Bstr(i%5B0%5D)%5D) + %22%5Ct%22 + str(read%5B1%5D) + %22%5Ct%22 + str(ref_coord%5Bread%5D%5B0%5D) + %22%5Ct%22 + str(ref_coord%5Bread%5D%5B1%5D)+
  %22%5Cn%22
+)
 %0A%0Aif

Correct input and output files, Read bam file and gnerate tabular file contaning 5 columns: chromosome, chromo_length, position, nbr_read, polarity