#NEWLINE# def cleanup( read , orig , RG ) : #NEWLINE# #INDENT# "\n    fixes unmapped reads that are marked as 'reverse'\n    fill in read group at random from existing RGs if \n    RG tags are present in .bam header \n    " #NEWLINE# if ( read . is_unmapped and read . is_reverse ) : #NEWLINE# #INDENT# read . is_reverse = False #NEWLINE# qual = read . qual #NEWLINE# read . seq = rc ( read . seq ) #NEWLINE# read . qual = qual [ : : ( - 1 ) ] #NEWLINE# #UNINDENT# if ( read . mate_is_unmapped and read . mate_is_reverse ) : #NEWLINE# #INDENT# read . mate_is_reverse = False #NEWLINE# #UNINDENT# if RG : #NEWLINE# #INDENT# hasRG = False #NEWLINE# if ( read . tags is not None ) : #NEWLINE# #INDENT# for tag in read . tags : #NEWLINE# #INDENT# if ( tag [ 0 ] == 'RG' ) : #NEWLINE# #INDENT# hasRG = True #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( orig is not None ) : #NEWLINE# #INDENT# if ( ( not hasRG ) and ( orig . tags is not None ) ) : #NEWLINE# #INDENT# for tag in orig . tags : #NEWLINE# #INDENT# if ( tag [ 0 ] == 'RG' ) : #NEWLINE# #INDENT# read . tags = ( read . tags + [ tag ] ) #NEWLINE# hasRG = True #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# #UNINDENT# if ( not hasRG ) : #NEWLINE# #INDENT# newRG = RG [ random . randint ( 0 , ( len ( RG ) - 1 ) ) ] #NEWLINE# read . tags = ( read . tags + [ ( 'RG' , newRG ) ] ) #NEWLINE# #UNINDENT# #UNINDENT# return read