def test_re_char_heights(self): """char heights for RE terms should be correct""" rets = array([[0, 0, 0.12535], [0.15782, 0, 0.67927], [0, 0, 0.28571], [-0.1, 0, -0.21429]]) expect_char_ret_height = array([[0, 0, 0.08417], [0.03539, 0, 0.45612], [0, 0, 0.19185], [-0.02243, 0, -0.14389]]) got_ret_height = get_re_char_heights(rets).round(decimals=5) self.assertEqual(got_ret_height, expect_char_ret_height)
def get_four_position_fig(four_pos_results, positions, figsize, group_label=None, group_ref=None, figwidth=None, xtick_fontsize=14, ytick_fontsize=14): position_sets = list(combinations(positions, 4)) assert len(position_sets) == 1 rel_entropies = [four_pos_results[position_sets[0]]['rel_entropy']] ylim = logo.est_ylim(rel_entropies) rel_entropy = rel_entropies[0] fig = pyplot.figure(figsize=figsize) ax = fig.gca() num_pos = len(positions) + 1 mid_pos = num_pos // 2 position_re = numpy.zeros((num_pos,), float) characters = numpy.zeros((num_pos, 256), str) rets = numpy.zeros((256, num_pos), float) indices = list(range(4)) # now adjust indices to reflect position along sequence for i in range(len(indices)): if indices[i] >= mid_pos: indices[i] += 1 position_re.put(indices, rel_entropy) stats = four_pos_results[position_sets[0]]['stats'] mut_stats = stats[ get_selected_indices(stats, group_label=group_label, group_ref=group_ref)][['base1', 'base2', 'base3', 'base4', 'ret']] mut_stats = mut_stats.sort_values(by='ret') characters[indices[0]] = list(mut_stats['base1']) characters[indices[1]] = list(mut_stats['base2']) characters[indices[2]] = list(mut_stats['base3']) characters[indices[3]] = list(mut_stats['base4']) for index in indices: rets[:, index] = mut_stats['ret'] heights = get_re_char_heights(rets, re_positionwise=position_re) logo.draw_multi_position(char_heights=heights.T, characters=characters, position_indices=indices, ax=ax, ylim=ylim, xtick_fontsize=xtick_fontsize, ytick_fontsize=ytick_fontsize) return fig
def get_plot_data(single_results, positions): num_pos = len(positions) + 1 mid = num_pos // 2 position_re = numpy.zeros((num_pos, ), float) rets = numpy.zeros((4, num_pos), float) characters = [list('ACGT') for i in range(num_pos)] for index, pos in enumerate(positions): if index >= mid: index += 1 stats = single_results[pos]['stats'] position_re[index] = single_results[pos]['rel_entropy'] mut_stats = stats[get_selected_indices(stats)][['base', 'ret']] mut_stats = mut_stats.sort_values(by='ret') characters[index] = list(mut_stats['base']) rets[:, index] = mut_stats['ret'] heights = get_re_char_heights(rets, re_positionwise=position_re) return heights.T, characters, list(range(num_pos))
def get_single_position_fig(single_results, positions, figsize, group_label=None, group_ref=None, figwidth=None, xlabel_fontsize=14, ylabel_fontsize=14, xtick_fontsize=14, ytick_fontsize=14): num_pos = len(positions) + 1 mid = num_pos // 2 position_re = numpy.zeros((num_pos,), float) rets = numpy.zeros((4, num_pos), float) characters = [list('ACGT') for i in range(num_pos)] for index, pos in enumerate(positions): if index >= mid: index += 1 stats = single_results[pos]['stats'] position_re[index] = single_results[pos]['rel_entropy'] mut_stats = stats[get_selected_indices(stats, group_label=group_label, group_ref=group_ref)][['base', 'ret']] mut_stats = mut_stats.sort_values(by='ret') characters[index] = list(mut_stats['base']) rets[:, index] = mut_stats['ret'] heights = get_re_char_heights(rets, re_positionwise=position_re) fig = logo.draw_multi_position(heights.T, characters=characters, position_indices=list(range(num_pos)), figsize=figsize, figwidth=figwidth, verbose=False) if figwidth: fig.set_figwidth(figwidth) ax = fig.gca() ax.set_xlabel('Position', fontsize=xlabel_fontsize) ax.set_ylabel('RE', rotation='vertical', fontsize=ylabel_fontsize) ax.tick_params(axis='x', labelsize=xtick_fontsize, pad=xtick_fontsize // 2, length=0) ax.tick_params(axis='y', labelsize=ytick_fontsize, pad=ytick_fontsize // 2) return fig
def get_three_position_fig(three_pos_results, positions, figsize, group_label=None, group_ref=None, figwidth=None, xtick_fontsize=14, ytick_fontsize=14): position_sets = list(combinations(positions, 3)) array_coords = get_resized_array_coordinates3(positions, position_sets) coords = list(array_coords.values()) xdim = max(v[0] for v in coords) + 1 ydim = max(v[1] for v in coords) + 1 fig, axarr = pyplot.subplots(xdim, ydim, figsize=figsize, sharex=True, sharey=True) for i in range(xdim): for j in range(ydim): if (i, j) in coords: continue ax = axarr[i, j] ax.set_frame_on(False) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) num_pos = len(positions) + 1 mid_pos = num_pos // 2 rel_entropies = [] for position_set in position_sets: rel_entropies.append(three_pos_results[position_set]['rel_entropy']) ylim = logo.est_ylim(rel_entropies) position_re = numpy.zeros((num_pos,), float) multi_positions = {} characters = numpy.zeros((num_pos, 64), str) for motif in combinations(positions, 3): rets = numpy.zeros((64, num_pos), float) indices = list(map(positions.index, motif)) row, col = array_coords[motif] ax = axarr[row, col] # now adjust indices to reflect position along sequence for i in range(len(indices)): if indices[i] >= mid_pos: indices[i] += 1 position_re.put(indices, three_pos_results[motif]['rel_entropy']) stats = three_pos_results[motif]['stats'] mut_stats = stats[ get_selected_indices(stats, group_label=group_label, group_ref=group_ref)][['base1', 'base2', 'base3', 'ret']] mut_stats = mut_stats.sort_values(by='ret') characters[indices[0]] = list(mut_stats['base1']) characters[indices[1]] = list(mut_stats['base2']) characters[indices[2]] = list(mut_stats['base3']) for index in indices: rets[:, index] = mut_stats['ret'] heights = get_re_char_heights(rets, re_positionwise=position_re) multi_positions[motif] = dict(rets=rets, indices=indices, characters=characters, heights=heights) logo.draw_multi_position(char_heights=heights.T, characters=characters, position_indices=indices, ax=ax, ylim=ylim, xtick_fontsize=xtick_fontsize, ytick_fontsize=ytick_fontsize) return fig