def write_results(self, out_dir, bname): for names_tup, (Imin, Imax) in sorted( self.Imin_max_pairs_given_names.items()): Imin_name, Imax_name = names_tup Imin = misc.list_if_scalar(Imin, len(self.concentrations)) Imax = misc.list_if_scalar(Imax, len(self.concentrations)) out_fname = '{}_{}_{}_Kds_and_ABAs.txt'.format( bname, Imin_name.replace(' ', '_'), Imax_name.replace(' ', '_')) out_fpath = os.path.join(out_dir, out_fname) with open(out_fpath, 'w') as out: out.write('# Target: {}\n'.format(self.target)) out.write('# Neg Control: {}\n'.format( self.neg_control_target)) out.write('# Concentration\tImin\tImax\n') for conc, imin, imax in zip(self.concentrations, Imin, Imax): out.write( '\t'.join(map(str, map(float, (conc, imin, imax)))) + '\n') out.write('\t'.join([ '# Seq', 'Kd (pM)', 'Kd error', 'ABA (kB T)', 'ABA error' ]) + '\n') out_zipper = zip(self.IA.seqs, self.Kds[names_tup], self.Kd_errors[names_tup], self.ABAs[names_tup], self.ABA_errors[names_tup]) out.write('\n'.join( '\t'.join(map(str, [seq, Kd, Kd_err, ABA, ABA_err])) for seq, Kd, Kd_err, ABA, ABA_err in out_zipper))
def plot_normalized_intensities(self, ax, seq, Imin, Imax, max_clust=2000, xvals=None): if xvals is None: xvals = self.course_trait_list Imin = misc.list_if_scalar(Imin, self.course_len) Imax = misc.list_if_scalar(Imax, self.course_len) inten_lol = self.intensity_lol_given_seq[seq] nclust = min(max_clust, len(inten_lol[0])) alpha = self._path_alpha(nclust) for i in range(nclust): path = [(inten_list[i] - imn) / (imx - imn) if inten_list[i] is not None else None for inten_list, imn, imx in zip(inten_lol, Imin, Imax)] ax.plot(xvals, path, 'b', alpha=alpha)
def all_normalized_trait_and_inten_vals_given_seq(self, seq, Imin, Imax, max_clust=None, bootstrap=False): """ Returns all concentration/intensities pairs in read_names, return as two lists, with the intensities adjusted by Imin and Imax to run typically between 0 and 1. Returns: :list: all_concentrations :list: all_intensities """ Imin = misc.list_if_scalar(Imin, self.course_len) Imax = misc.list_if_scalar(Imax, self.course_len) assert len(Imin) == len(Imax) == len(self.course_trait_list), (Imin, Imax) all_trait_vals, all_intensities = [], [] for tval, imn, imx, inten_arr, inten_list in zip( self.course_trait_list, Imin, Imax, self.intensity_loarr_given_seq[seq], self.intensity_lol_given_seq[seq]): if len(inten_arr) == 0: tmp_inten = np.array([]) elif bootstrap: tmp_idx = np.random.choice(np.arange(len(inten_list)), size=min(max_clust, len(inten_list)), replace=True) tmp_inten = np.array([ inten_list[idx] for idx in tmp_idx if inten_list[idx] != None ]) else: tmp_inten = np.array(list(inten_arr[:max_clust])) tmp_inten = list((tmp_inten - imn) / (imx - imn)) all_trait_vals.extend([tval] * len(tmp_inten)) all_intensities.extend(tmp_inten) return all_trait_vals, all_intensities
def plot_raw_fit(self, ax, seq, Kd, Imin, Imax): self.IA.plot_raw_intensities(ax, seq, xvals=self.nM_concentrations) Imin = misc.list_if_scalar(Imin, self.IA.course_len) Imax = misc.list_if_scalar(Imax, self.IA.course_len) nM_Kd = Kd / 1000.0 ax.plot(self.nM_concentrations, Imin, 'ko', alpha=0.8, label='$I_{min}$') ax.plot(self.nM_concentrations, Imax, 's', color='darkgoldenrod', alpha=0.8, label='$I_{max}$') def Iobs(x, Kd, Imin, Imax): return (Imax - Imin) / (1.0 + (float(Kd) / x)) + Imin fit_path = [ Iobs(conc, nM_Kd, imn, imx) for conc, imn, imx in zip(self.nM_concentrations, Imin, Imax) ] ax.plot(self.nM_concentrations, fit_path, 'k--') x = np.logspace(np.log10(self.nM_concentrations[0]), np.log10(self.nM_concentrations[-1]), 200) y = [Iobs(xx, nM_Kd, self.Imin_const, self.Imax_const) for xx in x] ax.plot(x, y, 'r') ax.set_xscale('log') ax.set_xlabel('Concentration (nM)', fontsize=18) ax.set_axis_bgcolor('white') ax.grid(False) ax.set_ylabel('Intensity', fontsize=18)
def ML_fit_Kd(self, seq, Imin_name, max_clust=None, bootstrap=False, *args, **kw_args): if max_clust is None: max_clust = self.max_clust if bootstrap: len_inten_list = len(self.IA.intensity_lol_given_seq[seq][0]) bootstrap_idxs = np.random.choice(np.arange(len_inten_list), size=min(len_inten_list, max_clust), replace=True) else: bootstrap_idxs = None Imin = getattr(self, Imin_name) Imin_list = misc.list_if_scalar(Imin, len(self.concentrations)) def neg_log_L(Kd): return -self.model_logL([seq], [Kd], Imin_list, self.Imax_ML[Imin_name], self.sigma_consts[Imin_name], self.sigma_I[Imin_name], bootstrap_idxs=bootstrap_idxs) res = minimize(neg_log_L, x0=20, method='powell', options=dict(maxiter=1000000, maxfev=1000000)) if not res.success: print '\nWarning: Failure on {} ({})'.format( seq, seqtools.mm_names(self.target, seq)) return float(res.x)
def fit_Imax_ML_given_Imin(self, ML_seqs, Imin): idx1 = len(ML_seqs) idx2 = idx1 + self.IA.course_len idx3 = idx2 + self.IA.course_len Imin_list = misc.list_if_scalar(Imin, len(self.concentrations)) def neg_log_L(params): params = map(abs, params) Kds = params[:idx1] Imax_list = params[idx1:idx2] sig_cs = params[idx2:idx3] sigI = params[idx3] return -self.model_logL(ML_seqs, Kds, Imin_list, Imax_list, sig_cs, sigI) x0 = list([ self.curve_fit_Kd(seq, self.Imin_const, self.Imax_const) for seq in ML_seqs ] + self.Imax_adjusted + [1] * self.IA.course_len + [(self.Imax_const - self.Imin_const) / 10]) assert len(x0) == idx3 + 1 res = minimize(neg_log_L, x0=x0, method='powell', options=dict(maxiter=1000000, maxfev=1000000, disp=True)) if not res.success: print '\nWarning: Failure on {} ({})'.format( seq, seqtools.mm_names(self.target, seq)) params = map(abs, res.x) Kds = params[:idx1] Imax_list = params[idx1:idx2] sig_cs = params[idx2:idx3] sigI = params[idx3] return Kds, Imax_list, sig_cs, sigI