def make_transition_plot(self, gene_name, model): """ NOTE shares a lot with make_mutefreq_plot() in python/paramutils.py """ fig, ax = plotting.mpl_init() fig.set_size_inches(plotting.plot_ratios[utils.get_region(gene_name)]) ibin = 0 print utils.color_gene(utils.unsanitize_name(gene_name)) legend_colors = set() # add a color to this the first time you plot it for state in model.states: # bin label ax.text(-0.5 + ibin, -0.075, paramutils.simplify_state_name(state.name), rotation='vertical', size=8) sorted_to_states = {} for name in state.transitions.keys(): if name.find('IG') == 0 or name.find('TR') == 0: sorted_to_states[name] = int(paramutils.simplify_state_name(name)) else: sorted_to_states[name] = name sorted_to_states = sorted(sorted_to_states.items(), key=operator.itemgetter(1)) total = 0.0 for to_state, simple_to_state in sorted_to_states: prob = state.transitions[to_state] alpha = 0.6 width = 3 if 'insert' in str(simple_to_state): label = 'insert' color = '#3498db' # blue elif str(simple_to_state) == 'end': label = 'end' color = 'red' else: # regional/internal states assert to_state.find('IG') == 0 or to_state.find('TR') == 0 label = 'internal' color = 'green' label_to_use = None if color not in legend_colors: label_to_use = label legend_colors.add(color) # horizontal line at height total+prob ax.plot([-0.5 + ibin, 0.5 + ibin], [total + prob, total + prob], color=color, linewidth=width, alpha=alpha, label=label_to_use) # vertical line from total to total + prob ax.plot([ibin, ibin], [total + 0.01, total + prob], color=color, alpha=alpha, linewidth=width) midpoint = 0.5*(prob + 2*total) # ax.text(ibin, midpoint, paramutils.simplify_state_name(to_state)) # nicely labels the midpoint of the chunk between lines, but there isn't really room for it total += prob ibin += 1 ax.get_xaxis().set_visible(False) plotting.mpl_finish(ax, self.base_plotdir + '/transitions', gene_name, ybounds=(-0.01, 1.01), xbounds=(-3, len(model.states) + 3), leg_loc=(0.95, 0.1), adjust={'left' : 0.1, 'right' : 0.8}, leg_prop={'size' : 8})
def make_transition_plot(self, gene_name, model): """ NOTE shares a lot with make_mutefreq_plot() in python/paramutils.py """ fig, ax = plotting.mpl_init() fig.set_size_inches(plotting.plot_ratios[utils.get_region(gene_name)]) ibin = 0 print utils.color_gene(utils.unsanitize_name(gene_name)) legend_colors = set() # add a color to this the first time you plot it for state in model.states: # bin label ax.text(-0.5 + ibin, -0.075, paramutils.simplify_state_name(state.name), rotation='vertical', size=8) sorted_to_states = {} for name in state.transitions.keys(): if name.find('IG') == 0: sorted_to_states[name] = int(paramutils.simplify_state_name(name)) else: sorted_to_states[name] = name sorted_to_states = sorted(sorted_to_states.items(), key=operator.itemgetter(1)) total = 0.0 for to_state, simple_to_state in sorted_to_states: prob = state.transitions[to_state] alpha = 0.6 width = 3 if 'insert' in str(simple_to_state): label = 'insert' color = '#3498db' # blue elif str(simple_to_state) == 'end': label = 'end' color = 'red' else: # regional/internal states assert to_state.find('IG') == 0 label = 'internal' color = 'green' label_to_use = None if color not in legend_colors: label_to_use = label legend_colors.add(color) # horizontal line at height total+prob ax.plot([-0.5 + ibin, 0.5 + ibin], [total + prob, total + prob], color=color, linewidth=width, alpha=alpha, label=label_to_use) # vertical line from total to total + prob ax.plot([ibin, ibin], [total + 0.01, total + prob], color=color, alpha=alpha, linewidth=width) midpoint = 0.5*(prob + 2*total) # ax.text(ibin, midpoint, paramutils.simplify_state_name(to_state)) # nicely labels the midpoint of the chunk between lines, but there isn't really room for it total += prob ibin += 1 ax.get_xaxis().set_visible(False) plotting.mpl_finish(ax, self.base_plotdir + '/transitions', gene_name, ybounds=(-0.01, 1.01), xbounds=(-3, len(model.states) + 3), leg_loc=(0.95, 0.1), adjust={'left' : 0.1, 'right' : 0.8}, leg_prop={'size' : 8})
def make_transition_plot(self, gene_name, model): ibin = 0 drawn_name_texts, lines, texts = {}, {}, {} for state in model.states: if utils.get_region(gene_name) in self.skip_boring_states: if state.name != 'init' and len(state.transitions) == 1: # skip uninteresting states to_state = state.transitions.keys()[0] # skip states with only transitions to end if to_state == 'end': continue if find_state_number(state.name) + 1 == find_state_number(to_state): # skip states with only transitions to next state continue drawn_name_texts[state.name] = TPaveText(-0.5 + ibin, -0.1, 0.5 + ibin, -0.05) drawn_name_texts[state.name].SetBorderSize(0) drawn_name_texts[state.name].SetFillColor(0) drawn_name_texts[state.name].SetFillStyle(0) drawn_name_texts[state.name].AddText(-0.5 + ibin, -0.075, paramutils.simplify_state_name(state.name)) sorted_to_states = {} for name in state.transitions.keys(): if name.find('IGH') == 0: sorted_to_states[name] = int(paramutils.simplify_state_name(name)) else: sorted_to_states[name] = name sorted_to_states = sorted(sorted_to_states.items(), key=operator.itemgetter(1)) total = 0.0 lines[state.name], texts[state.name] = [], [] for to_state, simple_to_state in sorted_to_states: prob = state.transitions[to_state] lines[state.name].append(TLine(-0.5 + ibin, total + prob, 0.5 + ibin, total + prob)) lines[state.name][-1].SetLineColor(kGreen+2) lines[state.name][-1].SetLineWidth(6) midpoint = 0.5*(prob + 2*total) texts[state.name].append(TPaveText(-0.5 + ibin, midpoint-0.04, 0.5 + ibin, midpoint + 0.01)) texts[state.name][-1].AddText(-0.5 + ibin, midpoint, paramutils.simplify_state_name(to_state)) texts[state.name][-1].SetBorderSize(0) texts[state.name][-1].SetFillColor(0) texts[state.name][-1].SetFillStyle(0) total += prob ibin += 1 cvn = TCanvas('mod-cvn', '', 1000, 400) n_bins = ibin hframe = TH1D(model.name + '-transition-frame', utils.unsanitize_name(model.name), n_bins, -0.5, n_bins - 0.5) if utils.get_region(gene_name) in self.skip_boring_states: hframe.SetTitle(hframe.GetTitle() + ' (skipped boring states)') hframe.SetNdivisions(202, 'y') hframe.SetNdivisions(0, 'x') hframe.Draw() for state_name in lines.keys(): drawn_name_texts[state_name].Draw() for itrans in range(len(lines[state_name])): lines[state_name][itrans].Draw() texts[state_name][itrans].Draw() cvn.SaveAs(self.base_plotdir + '/transitions/plots/' + gene_name + '.png')
def make_transition_plot(self, gene_name, model): ibin = 0 drawn_name_texts, lines, texts = {}, {}, {} for state in model.states: if utils.get_region(gene_name) in self.skip_boring_states: if state.name != 'init' and len( state.transitions) == 1: # skip uninteresting states to_state = state.transitions.keys()[ 0] # skip states with only transitions to end if to_state == 'end': continue if find_state_number(state.name) + 1 == find_state_number( to_state ): # skip states with only transitions to next state continue drawn_name_texts[state.name] = TPaveText(-0.5 + ibin, -0.1, 0.5 + ibin, -0.05) drawn_name_texts[state.name].SetBorderSize(0) drawn_name_texts[state.name].SetFillColor(0) drawn_name_texts[state.name].SetFillStyle(0) drawn_name_texts[state.name].AddText( -0.5 + ibin, -0.075, paramutils.simplify_state_name(state.name)) sorted_to_states = {} for name in state.transitions.keys(): if name.find('IGH') == 0: sorted_to_states[name] = int( paramutils.simplify_state_name(name)) else: sorted_to_states[name] = name sorted_to_states = sorted(sorted_to_states.items(), key=operator.itemgetter(1)) total = 0.0 lines[state.name], texts[state.name] = [], [] for to_state, simple_to_state in sorted_to_states: prob = state.transitions[to_state] lines[state.name].append( TLine(-0.5 + ibin, total + prob, 0.5 + ibin, total + prob)) lines[state.name][-1].SetLineColor(kGreen + 2) lines[state.name][-1].SetLineWidth(6) midpoint = 0.5 * (prob + 2 * total) texts[state.name].append( TPaveText(-0.5 + ibin, midpoint - 0.04, 0.5 + ibin, midpoint + 0.01)) texts[state.name][-1].AddText( -0.5 + ibin, midpoint, paramutils.simplify_state_name(to_state)) texts[state.name][-1].SetBorderSize(0) texts[state.name][-1].SetFillColor(0) texts[state.name][-1].SetFillStyle(0) total += prob ibin += 1 cvn = TCanvas('mod-cvn', '', 1000, 400) n_bins = ibin hframe = TH1D(model.name + '-transition-frame', utils.unsanitize_name(model.name), n_bins, -0.5, n_bins - 0.5) if utils.get_region(gene_name) in self.skip_boring_states: hframe.SetTitle(hframe.GetTitle() + ' (skipped boring states)') hframe.SetNdivisions(202, 'y') hframe.SetNdivisions(0, 'x') hframe.Draw() for state_name in lines.keys(): drawn_name_texts[state_name].Draw() for itrans in range(len(lines[state_name])): lines[state_name][itrans].Draw() texts[state_name][itrans].Draw() cvn.SaveAs(self.base_plotdir + '/transitions/plots/' + gene_name + '.png')