def bam2graph( bam,contig,window ): """ """ #first get mean coverage c2cs = get_contig2coverage( bam ) gsize = sum( [ s for c,s in c2cs.itervalues() ] ) rcount = sum( [ c for c,s in c2cs.itervalues() ] ) expcount = 1.0 * rcount * window / gsize #generate graph data gdata = [] clen = c2cs[contig][1] sam = pysam.Samfile( bam ) for i in xrange( 0,clen,window ): #log2 = 0 c = sam.count( reference=contig,start=i,end=i+window ) if c: #last window is shorter! if i+window>clen: expcount = 1.0 * rcount * (clen-i) / gsize log2 = log( c/expcount,2 ) gdata.append( (i+1,log2) ) return gdata,clen