def color_by_change(libraries, out_prefix, nucleotides_to_count='ATCG', exclude_constitutive=False, subtract_background=False): for library in libraries: log_fold_changes = {} maxval = 0.0 minval = 0.0 for rRNA_name in library.rRNA_mutation_data: log_fold_changes[rRNA_name] = {} for nucleotide in library.rRNA_mutation_data[rRNA_name].nucleotides: if library.rRNA_mutation_data[rRNA_name].nucleotides[nucleotide].identity in nucleotides_to_count and \ library.rRNA_mutation_data[rRNA_name].nucleotides[nucleotide].\ get_control_fold_change_in_mutation_rate(subtract_background=subtract_background) not in [0.0, float('inf'), float('-inf')]: if exclude_constitutive and library.rRNA_mutation_data[rRNA_name].nucleotides[nucleotide].exclude_constitutive: pass else: log_fold_changes[rRNA_name][nucleotide] = math.log(library.rRNA_mutation_data[rRNA_name].nucleotides[nucleotide].get_control_fold_change_in_mutation_rate(subtract_background=subtract_background), 10) maxval = max(maxval, log_fold_changes[rRNA_name][nucleotide]) minval = min(minval, log_fold_changes[rRNA_name][nucleotide]) else: pass absmax = max(abs(maxval), abs(minval)) output_file = open(os.path.join(out_prefix, "%s.txt" % (library.lib_settings.sample_name)), 'w') reference_pymol_script_file = open(library.experiment_settings.get_property('pymol_base_script_colorchange'), 'rU') for line in reference_pymol_script_file: if line.startswith('#<insert b-factors>'): output_file.write('python\n') output_file.write('cmd.alter(\'all\', \'b=0.0\')\n') for rRNA_name in log_fold_changes: for nucleotide in log_fold_changes[rRNA_name]: output_file.write('cmd.alter(\''+rRNA_name+' and resi '+str(nucleotide)+'\', \'b=float("'+str(log_fold_changes[rRNA_name][nucleotide])+'")\')\n') output_file.write('python end\n') elif line.startswith('#<insert spectrum>'): output_file.write('spectrum b, bluish_green white vermillion, minimum='+str(-absmax)+', maximum='+str(absmax)+'\n') output_file.write('ramp_new scale, S.c.25S__rRNA, ['+str(-absmax)+',0,'+str(absmax)+'], [bluish_green, white, vermillion]') else: output_file.write(line) reference_pymol_script_file.close() output_file.close()
def highlight_structure(libraries, out_prefix, nucleotides_to_count='ATCG', exclude_constitutive=False): """ :param libraries: :param out_prefix: :param nucleotides_to_count: :param exclude_constitutive: :return: for each library use bokeh to plot an interactive plot of magnitude of signal (experimental+control)/2 vs log10 fold change (experimental/control). Protected and de-protected calls will be colored, based on a fold change cutoff and confidence interval. All nucleotides will be labelled on mouseover. """ for library in libraries: protected_nucleotides = library.get_changed_nucleotides('protected', confidence_interval=library.experiment_settings.get_property('confidence_interval_cutoff'), fold_change_cutoff=library.experiment_settings.get_property('fold_change_cutoff')) num_protected = 0 for rRNA in protected_nucleotides: num_protected += len(protected_nucleotides[rRNA]) deprotected_nucleotides = library.get_changed_nucleotides('deprotected', confidence_interval=library.experiment_settings.get_property('confidence_interval_cutoff'), fold_change_cutoff=library.experiment_settings.get_property('fold_change_cutoff')) num_deprotected = 0 for rRNA in deprotected_nucleotides: num_deprotected += len(deprotected_nucleotides[rRNA]) if num_protected>0 or num_deprotected>0: output_file = open(os.path.join(out_prefix, "%s.txt" % (library.lib_settings.sample_name)), 'w') reference_pymol_script_file = open(library.experiment_settings.get_property('pymol_base_script'), 'rU') for line in reference_pymol_script_file: if line.startswith('#<insert nucleotide highlighting here>'): if num_protected>0: rRNA_selections = [] for rRNA in protected_nucleotides: if len(protected_nucleotides[rRNA])>0: rRNA_selections.append('%s and resi %s' % (rRNA, '+'.join([str(nucleotide.position) for nucleotide in protected_nucleotides[rRNA]]))) outline = 'create protected_nucleotides, %s\n' % (' or '.join(rRNA_selections)) output_file.write(outline) if num_deprotected>0: rRNA_selections = [] for rRNA in deprotected_nucleotides: if len(deprotected_nucleotides[rRNA])>0: rRNA_selections.append('%s and resi %s' % (rRNA, '+'.join([str(nucleotide.position) for nucleotide in deprotected_nucleotides[rRNA]]))) outline = 'create deprotected_nucleotides, %s\n' % (' or '.join(rRNA_selections)) output_file.write(outline) elif line.startswith('#<color groups here>'): if num_protected>0: output_file.write('color vermillion, protected_nucleotides\n') if num_deprotected>0: output_file.write('color bluish_green, deprotected_nucleotides\n') elif line.startswith('#<show spheres for changing nucleotides here>'): if num_protected>0: output_file.write('show spheres, protected_nucleotides\n') if num_deprotected>0: output_file.write('deprotected_nucleotides\n') else: output_file.write(line) reference_pymol_script_file.close() output_file.close()