def make_emission_plot(self, gene_name, model): plotting_info = [] for state in model.states: if state.emissions is None: assert state.name == 'init' continue plotting_info.append({}) plotting_info[-1]['name'] = state.name plotting_info[-1]['nuke_freqs'] = state.emissions['probs'] paramutils.make_mutefreq_plot(self.base_plotdir + '/emissions', gene_name, plotting_info)
def make_emission_plot(self, gene_name, model): plotting_info = [] for state in model.states: if state.emissions is None: assert state.name == 'init' continue plotting_info.append({ 'name' : state.name, 'nuke_freqs' : state.emissions['probs'], }) paramutils.make_mutefreq_plot(self.base_plotdir + '/emissions', gene_name, plotting_info, debug=True)
def make_emission_plot(self, gene_name, model): plotting_info = [] for state in model.states: if len(state.emissions) == 0: assert state.name == 'init' continue plotting_info.append({}) plotting_info[-1]['name'] = state.name plotting_info[-1]['nuke_freqs'] = state.emissions['probs'] paramutils.make_mutefreq_plot(self.base_plotdir + '/emissions', gene_name, plotting_info)
def plot(self, base_plotdir, cyst_positions=None, tryp_positions=None): if not self.finalized: self.finalize() plotdir = base_plotdir + '/mute-freqs' utils.prep_dir(plotdir + '/plots', multilings=('*.csv', '*.svg')) for region in utils.regions: utils.prep_dir(plotdir + '/' + region + '/plots', multilings=('*.csv', '*.svg')) utils.prep_dir(plotdir + '/' + region + '-per-base/plots', multilings=('*.csv', '*.png')) for gene in self.counts: counts, plotting_info = self.counts[gene], self.plotting_info[gene] sorted_positions = sorted(counts) hist = TH1D('hist_' + utils.sanitize_name(gene), '', sorted_positions[-1] - sorted_positions[0] + 1, sorted_positions[0] - 0.5, sorted_positions[-1] + 0.5) for position in sorted_positions: hist.SetBinContent(hist.FindBin(position), counts[position]['freq']) hi_diff = abs(counts[position]['freq'] - counts[position]['freq_hi_err']) lo_diff = abs(counts[position]['freq'] - counts[position]['freq_lo_err']) err = 0.5*(hi_diff + lo_diff) hist.SetBinError(hist.FindBin(position), err) plotfname = plotdir + '/' + utils.get_region(gene) + '/plots/' + utils.sanitize_name(gene) + '.svg' xline = None if utils.get_region(gene) == 'v' and cyst_positions is not None: xline = cyst_positions[gene]['cysteine-position'] elif utils.get_region(gene) == 'j' and tryp_positions is not None: xline = int(tryp_positions[gene]) plotting.draw(hist, 'int', plotdir=plotdir + '/' + utils.get_region(gene), plotname=utils.sanitize_name(gene), errors=True, write_csv=True, xline=xline, draw_str='e') #, cwidth=4000, cheight=1000) paramutils.make_mutefreq_plot(plotdir + '/' + utils.get_region(gene) + '-per-base', utils.sanitize_name(gene), plotting_info) # for region in utils.regions: # utils.prep_dir(plotdir + '/' + region + '/tmp/plots', multilings=('*.csv', '*.svg')) # for gene in self.tmpcounts: # for position in self.tmpcounts[gene]: # roothist = plotting.make_hist_from_my_hist_class(self.tmpcounts[gene][position]['muted'], gene + '_' + str(position)) # plotting.draw(roothist, 'int', plotdir=plotdir + '/' + utils.get_region(gene) + '/tmp', plotname=utils.sanitize_name(gene) + '_' + str(position), errors=True, write_csv=True) #, cwidth=4000, cheight=1000) # make mean mute freq hists hist = plotting.make_hist_from_my_hist_class(self.mean_rates['all'], 'all-mean-freq') plotting.draw(hist, 'float', plotname='all-mean-freq', plotdir=plotdir, stats='mean', bounds=(0.0, 0.4), write_csv=True) for region in utils.regions: hist = plotting.make_hist_from_my_hist_class(self.mean_rates[region], region+'-mean-freq') plotting.draw(hist, 'float', plotname=region+'-mean-freq', plotdir=plotdir, stats='mean', bounds=(0.0, 0.4), write_csv=True) check_call(['./bin/makeHtml', plotdir, '3', 'null', 'svg']) # then write html file and fix permissiions for region in utils.regions: check_call(['./bin/makeHtml', plotdir + '/' + region, '1', 'null', 'svg']) check_call(['./bin/makeHtml', plotdir + '/' + region + '-per-base', '1', 'null', 'png']) check_call(['./bin/permissify-www', plotdir]) # NOTE this should really permissify starting a few directories higher up
def plot(self, plotdir, only_csv=False, only_overall=False, make_per_base_plots=False): import plotting if not self.finalized: self.finalize() overall_plotdir = plotdir + '/overall' for gene in self.freqs: if only_overall: continue freqs = self.freqs[gene] if len(freqs) == 0: if gene not in glutils.dummy_d_genes.values(): print ' %s no mutefreqer obs for %s' % (utils.color( 'red', 'warning'), utils.color_gene(gene)) continue sorted_positions = sorted(freqs.keys()) genehist = Hist(sorted_positions[-1] - sorted_positions[0] + 1, sorted_positions[0] - 0.5, sorted_positions[-1] + 0.5, xtitle='position', ytitle='mut freq', title=gene) for position in sorted_positions: hi_diff = abs(freqs[position]['freq'] - freqs[position]['freq_hi_err']) lo_diff = abs(freqs[position]['freq'] - freqs[position]['freq_lo_err']) err = 0.5 * (hi_diff + lo_diff) genehist.set_ibin(genehist.find_bin(position), freqs[position]['freq'], error=err) xline = None figsize = [7, 4] if utils.get_region(gene) in utils.conserved_codons[ self.glfo['locus']]: xline = utils.cdn_pos(self.glfo, utils.get_region(gene), gene) if utils.get_region(gene) == 'v': figsize[0] *= 3.5 elif utils.get_region(gene) == 'j': figsize[0] *= 2 plotting.draw_no_root(self.per_gene_mean_rates[gene], plotdir=plotdir + '/per-gene/' + utils.get_region(gene), plotname=utils.sanitize_name(gene), errors=True, write_csv=True, only_csv=only_csv, shift_overflows=True) # per-position plots: plotting.draw_no_root(genehist, plotdir=plotdir + '/per-gene-per-position/' + utils.get_region(gene), plotname=utils.sanitize_name(gene), errors=True, write_csv=True, xline=xline, figsize=figsize, only_csv=only_csv, shift_overflows=True) if make_per_base_plots: # per-position, per-base plots: (super slow, so commented by default) import paramutils plotting_info = [] for pos in sorted_positions: plotting_info.append({ 'name': str(pos), # not sure if this is right, but I think so 'nuke_freqs': {n: freqs[pos][n] for n in utils.nukes}, 'gl_nuke': utils.gseq(self.glfo, gene)[pos], }) paramutils.make_mutefreq_plot( plotdir + '/' + utils.get_region(gene) + '-per-base', utils.sanitize_name(gene), plotting_info) # make mean mute freq hists for rstr in ['all', 'cdr3'] + utils.regions: if rstr == 'all': bounds = (0.0, 0.4) else: bounds = (0.0, 0.6 if rstr == 'd' else 0.4) plotting.draw_no_root(self.mean_rates[rstr], plotname=rstr + '_mean-freq', plotdir=overall_plotdir, stats='mean', bounds=bounds, write_csv=True, only_csv=only_csv, shift_overflows=True) plotting.draw_no_root(self.mean_n_muted[rstr], plotname=rstr + '_mean-n-muted', plotdir=overall_plotdir, stats='mean', write_csv=True, only_csv=only_csv, shift_overflows=True) if not only_csv: # write html file and fix permissiions for substr in self.subplotdirs: plotting.make_html(plotdir + '/' + substr)
def plot(self, base_plotdir, cyst_positions=None, tryp_positions=None): if not self.finalized: self.finalize() plotdir = base_plotdir + '/mute-freqs' utils.prep_dir(plotdir + '/plots', multilings=('*.csv', '*.svg')) for region in utils.regions: utils.prep_dir(plotdir + '/' + region + '/plots', multilings=('*.csv', '*.svg')) utils.prep_dir(plotdir + '/' + region + '-per-base/plots', multilings=('*.csv', '*.png')) for gene in self.counts: counts, plotting_info = self.counts[gene], self.plotting_info[gene] sorted_positions = sorted(counts) hist = TH1D('hist_' + utils.sanitize_name(gene), '', sorted_positions[-1] - sorted_positions[0] + 1, sorted_positions[0] - 0.5, sorted_positions[-1] + 0.5) for position in sorted_positions: hist.SetBinContent(hist.FindBin(position), counts[position]['freq']) hi_diff = abs(counts[position]['freq'] - counts[position]['freq_hi_err']) lo_diff = abs(counts[position]['freq'] - counts[position]['freq_lo_err']) err = 0.5 * (hi_diff + lo_diff) hist.SetBinError(hist.FindBin(position), err) plotfname = plotdir + '/' + utils.get_region( gene) + '/plots/' + utils.sanitize_name(gene) + '.svg' xline = None if utils.get_region(gene) == 'v' and cyst_positions is not None: xline = cyst_positions[gene]['cysteine-position'] elif utils.get_region(gene) == 'j' and tryp_positions is not None: xline = int(tryp_positions[gene]) plotting.draw(hist, 'int', plotdir=plotdir + '/' + utils.get_region(gene), plotname=utils.sanitize_name(gene), errors=True, write_csv=True, xline=xline, draw_str='e') #, cwidth=4000, cheight=1000) paramutils.make_mutefreq_plot( plotdir + '/' + utils.get_region(gene) + '-per-base', utils.sanitize_name(gene), plotting_info) # make mean mute freq hists hist = plotting.make_hist_from_my_hist_class(self.mean_rates['all'], 'all-mean-freq') plotting.draw(hist, 'float', plotname='all-mean-freq', plotdir=plotdir, stats='mean', bounds=(0.0, 0.4), write_csv=True) for region in utils.regions: hist = plotting.make_hist_from_my_hist_class( self.mean_rates[region], region + '-mean-freq') plotting.draw(hist, 'float', plotname=region + '-mean-freq', plotdir=plotdir, stats='mean', bounds=(0.0, 0.4), write_csv=True) check_call(['./bin/makeHtml', plotdir, '3', 'null', 'svg']) # then write html file and fix permissiions for region in utils.regions: check_call( ['./bin/makeHtml', plotdir + '/' + region, '1', 'null', 'svg']) check_call([ './bin/makeHtml', plotdir + '/' + region + '-per-base', '1', 'null', 'png' ]) check_call( ['./bin/permissify-www', plotdir] ) # NOTE this should really permissify starting a few directories higher up