#NEWLINE# def count( self , bam , library_type , coverage = False , uniq_only = False , fpkm = False , norm = '' , multiple = 'complete' , whitelist = None , blacklist = None , out = sys . stdout , quiet = False , start_only = False ) : #NEWLINE# #INDENT# stranded = ( library_type in [ 'FR' , 'RF' ] ) #NEWLINE# if coverage : #NEWLINE# #INDENT# sys . stderr . write ( 'Coverage calculations not supported with repeatmasker family models\n' ) #NEWLINE# sys . exit ( 1 ) #NEWLINE# #UNINDENT# if ( norm and ( norm not in [ 'all' , 'mapped' ] ) ) : #NEWLINE# #INDENT# sys . stderr . write ( ( 'Normalization "%s" not supported with repeatmasker family models\n' % norm ) ) #NEWLINE# sys . exit ( 1 ) #NEWLINE# #UNINDENT# repeats = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# total_count = 0.0 #NEWLINE# for ( family , member , chrom , start , end , strand ) in _repeatreader ( self . fname ) : #NEWLINE# #INDENT# if ( not ( ( family , member ) in repeats ) ) : #NEWLINE# #INDENT# repeats [ ( family , member ) ] = { #NEWLINE# #INDENT# 'count' : 0 , #NEWLINE# 'size' : 0 , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( not ( ( family , '*' ) in repeats ) ) : #NEWLINE# #INDENT# repeats [ ( family , '*' ) ] = { #NEWLINE# #INDENT# 'count' : 0 , #NEWLINE# 'size' : 0 , #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# if ( not ( chrom in bam . references ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# size = ( end - start ) #NEWLINE# repeats [ ( family , '*' ) ] [ 'size' ] += size #NEWLINE# repeats [ ( family , member ) ] [ 'size' ] += size #NEWLINE# ( count , reads ) = _fetch_reads ( bam , chrom , ( strand if stranded else None ) , [ start ] , [ end ] , multiple , False , whitelist , blacklist , library_type = library_type ) #NEWLINE# repeats [ ( family , '*' ) ] [ 'count' ] += count #NEWLINE# repeats [ ( family , member ) ] [ 'count' ] += count #NEWLINE# total_count += count #NEWLINE# #UNINDENT# sys . stderr . write ( 'Calculating normalization...' ) #NEWLINE# norm_val = None #NEWLINE# norm_val_orig = None #NEWLINE# if ( norm == 'all' ) : #NEWLINE# #INDENT# norm_val_orig = _find_mapped_count ( bam , whitelist , blacklist ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( norm == 'mapped' ) : #NEWLINE# #INDENT# norm_val_orig = total_count #NEWLINE# #UNINDENT# #UNINDENT# if norm_val_orig : #NEWLINE# #INDENT# norm_val = ( float ( norm_val_orig ) / 1000000 ) #NEWLINE# #UNINDENT# sys . stderr . write ( '\n' ) #NEWLINE# out . write ( ( '## input%s%s\n' % ( ( ' ' if bam . filename else '' ) , bam . filename ) ) ) #NEWLINE# out . write ( ( '## model %s %s\n' % ( self . get_name ( ) , self . get_source ( ) ) ) ) #NEWLINE# out . write ( ( '## library %s\n' % library_type ) ) #NEWLINE# out . write ( ( '## multiple %s\n' % multiple ) ) #NEWLINE# if norm_val : #NEWLINE# #INDENT# out . write ( ( '## norm %s %s\n' % ( norm , float ( norm_val_orig ) ) ) ) #NEWLINE# out . write ( ( '## CPM-factor %s\n' % norm_val ) ) #NEWLINE# #UNINDENT# out . write ( '#' ) #NEWLINE# out . write ( '\t' . join ( self . get_headers ( ) ) ) #NEWLINE# out . write ( '\tlength\tcount' ) #NEWLINE# if norm_val : #NEWLINE# #INDENT# out . write ( '\tcount (CPM)' ) #NEWLINE# if fpkm : #NEWLINE# #INDENT# out . write ( '\tRPKM' ) #NEWLINE# #UNINDENT# #UNINDENT# out . write ( '\n' ) #NEWLINE# sortedkeys = [ ] #NEWLINE# for k in repeats : #NEWLINE# #INDENT# sortedkeys . append ( k ) #NEWLINE# #UNINDENT# sortedkeys . sort ( ) #NEWLINE# for class_level in [ True , False ] : #NEWLINE# #INDENT# for k in sortedkeys : #NEWLINE# #INDENT# if ( class_level and ( k [ 1 ] != '*' ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( ( not class_level ) and ( k [ 1 ] == '*' ) ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# #UNINDENT# cols = [ k [ 0 ] , k [ 1 ] , repeats [ k ] [ 'size' ] , repeats [ k ] [ 'count' ] ] #NEWLINE# if norm_val : #NEWLINE# #INDENT# cols . append ( ( repeats [ k ] [ 'count' ] / norm_val ) ) #NEWLINE# if fpkm : #NEWLINE# #INDENT# cols . append ( ( ( repeats [ k ] [ 'count' ] / ( repeats [ k ] [ 'size' ] / 1000.0 ) ) / norm_val ) ) #NEWLINE# #UNINDENT# #UNINDENT# out . write ( ( '%s\n' % '\t' . join ( [ str ( x ) for x in cols ] ) ) )