def calculate_peaks(ringer,threshold): """ Checks if something is greater than either of its neighbors (including wrapping) and returns if true and if above a threshold) """ new_peaks=Peaklist() list = ringer._angles[1].densities for i in range(len(list)): if (list[i]==max(list) and list[i]>threshold): new_peaks.add_new(ringer.resname, ringer.resid, ringer.chain_id, 1, i, list[i]) return new_peaks
def __init__(self, file_name, ringer_result=None, sampling_angle=5, out_dir=None, out=sys.stdout, quiet=False): self.threshold = waves = None if (ringer_result is not None): waves, self.thresholds = process_raw_results(ringer_result, out=out) else : assert (file_name is not None) waves, self.thresholds = parse_pickle(file_name, out=out) if not quiet: assert (out_dir is None) or os.path.isdir(out_dir) if (out_dir is None) and (not quiet): out_dir = file_name + ".output" if (not os.path.isdir(out_dir)): os.makedirs(file_name+'.output') Weird_residues=OrderedDict() self.peak_count={} residue_peak_count={} rotamer_ratios_residues={} zscores_residues={} for i in Residue_codes: residue_peak_count[i]={} rotamer_ratios_residues[i]=[] zscores_residues[i]=[] binned_peaks={} n_angles = iceil(360.0 / sampling_angle) self.zscores=[] self.rotamer_ratios=[] self.non_zero_thresholds=[] self.length = len(waves) self.peaks=OrderedDict() # calculate peaks and histogram for threshold in self.thresholds: if (not quiet): print >>out, "" print >>out, "===== Calculating Statistics for Threshold %.3f =====" %\ threshold self.peaks[threshold]=Peaklist() Weird_residues[threshold]=Peaklist() self.peak_count[threshold] = [0]*n_angles for i in Residue_codes: residue_peak_count[i][threshold]=[0]*n_angles for i in waves: self.peaks[threshold].append_lists(calculate_peaks(i, threshold)) for peak in self.peaks[threshold].get_peaks(): self.peak_count[threshold][peak.chi_value] += 1 residue_peak_count[peak.resname][threshold][peak.chi_value]+=1 if ((peak.chi_value<6) or (peak.chi_value>18 and peak.chi_value<30) or (peak.chi_value>42 and peak.chi_value<54) or (peak.chi_value>66)): Weird_residues[threshold].peaks.append(peak) # Calculate the binned peaks and ratios binned_peaks[threshold] = calculate_binned_counts(self.peak_count[threshold], 60) # print "For threshold %.3f" % threshold # print "Sample size = %d" % sum(binned_peaks[threshold]) zscore_n, rotamer_ratio_n = statistic(binned_peaks[threshold], n_angles) if rotamer_ratio_n==0: break for i in Residue_codes: rotamer_ratios_residues_n, zscores_n = calc_ratio(residue_peak_count[i][threshold], sampling_angle) rotamer_ratios_residues[i].append(rotamer_ratios_residues_n) zscores_residues[i].append(zscores_n) self.non_zero_thresholds.append(threshold) self.zscores.append(zscore_n) self.rotamer_ratios.append(rotamer_ratio_n) if (not quiet): print >> out, "===== Plotting Histogram for Threshold %.3f =====" % \ threshold out_file = os.path.join(out_dir, "%.3f.histogram.png" % threshold) plot_peaks( peak_count=self.peak_count[threshold], file_name=out_file, threshold=threshold, first=60, title=RMSD_statistic(self.peaks[threshold].peaks), n_angles=n_angles) print >> out, "Saved plot to %s" % out_file # plot_rotamers(binned_peaks[threshold], file, threshold, args.first_rotamer) # print "Outliers at threshold %.2f: %s" % (threshold, str(Weird_residues[threshold])) if len(self.zscores) == 0: raise Sorry("""No scores could be calculated at any threshold for this map. This could be because the map is not sufficiently featured, or because of data corruption in the map.""") if (not quiet): print >> out, "" print >> out, "===== Plotting Statistics Across Thresholds =====" out_file = os.path.join(out_dir, "Total.threshold_scan.png") plot_progression( non_zero_thresholds=self.non_zero_thresholds, rotamer_ratios=self.rotamer_ratios, file_name=out_file, zscores=self.all_scores) print >> out, "Saved plot to %s" % out_file # for i in Residue_codes: # plot_progression(non_zero_thresholds, rotamer_ratios_residues[i], file, zscores_residues[i], i) print >> out, "" print >> out, "===== Writing Pickles Out =====" easy_pickle.dump(out_dir + '/Outliers.pkl',Weird_residues) print >> out, 'Wrote ' + out_dir + '/Outliers.pkl' easy_pickle.dump(out_dir + '/rotamer_ratios.pkl', self.rotamer_ratios) print >> out, 'Wrote ' + out_dir + '/rotamer_ratios.pkl' easy_pickle.dump(out_dir + '/zscores.pkl', self.zscores) print >> out, 'Wrote ' + out_dir + '/zscores.pkl' easy_pickle.dump(out_dir + '/emringer_scores.pkl', self.all_scores) print >> out, 'Wrote ' + out_dir + '/emringer_scores.pkl' easy_pickle.dump(out_dir + '/thresholds.pkl', self.thresholds) print >> out, 'Wrote ' + out_dir + '/thresholds.pkl' easy_pickle.dump(out_dir + '/peak_counts.pkl', self.peak_count) print >> out, 'Wrote ' + out_dir + '/peak_counts.pkl' self.zscore_max = max(self.zscores) self._zscore_max_index = self.zscores.index(self.zscore_max)