def _print_figure(axes, figure, plot_fhand, plot_legend=True, fmt='png'): if figure is None: return if plot_legend: axes.legend() canvas = FigureCanvas(figure) canvas.print_figure(plot_fhand, format=fmt) plot_fhand.flush()
def plot_haplotypes(vcf_fhand, plot_fhand, genotype_mode=REFERENCE, filter_alleles_gt=FILTER_ALLELES_GT): reader = VCFReader(vcf_fhand) # collect data genotypes = None samples = [] for snv in reader.parse_snvs(): if genotypes is None: genotypes = {} for call in snv.calls: sample = call.sample genotypes[sample] = [] samples.append(sample) for call in snv.calls: alleles = _get_alleles(call, filter_alleles_gt=filter_alleles_gt) genotypes[call.sample].append(alleles) # draw n_samples = len(samples) xsize = len(genotypes[sample]) / 100 if xsize >= 100: xsize = 100 if xsize <= 8: xsize = 8 ysize = n_samples * 2 if ysize >= 100: ysize = 100 # print xsize, ysize figure_size = (xsize, ysize) fig = Figure(figsize=figure_size) for index, sample in enumerate(samples): axes = fig.add_subplot(n_samples, 1, index) axes.set_title(sample) y_data = genotypes[sample] x_data = [i + 1 for i in range(len(y_data))] x_data, y_data = _flatten_data(x_data, y_data) axes.plot(x_data, y_data, marker='o', linestyle='None', markersize=3.0, markeredgewidth=0, markerfacecolor='red') ylim = axes.get_ylim() ylim = ylim[0] - 0.1, ylim[1] + 0.1 axes.set_ylim(ylim) axes.tick_params(axis='x', bottom='off', top='off', which='both', labelbottom='off') axes.tick_params(axis='y', left='on', right='off', labelleft='off') axes.set_ylabel(sample) canvas = FigureCanvas(fig) canvas.print_figure(plot_fhand, dpi=300) plot_fhand.flush()
def _plot_segregation_debug(plot_info, fhand): greens = ['#2d6e12', '#76b75b', '#164900'] reds = ['#8f0007', '#fb1f2a', '#b90009'] grays = ['0.5', '0.25', '0.75'] fig = Figure(figsize=(10, 4)) axes1 = fig.add_subplot(211) axes2 = fig.add_subplot(212) plot_info.sort(key=itemgetter('pos')) is_close_snp = [geno_info['close_snp'] for geno_info in plot_info] tested_snp_idx = is_close_snp.index(False) passed = [geno_info['result'] for geno_info in plot_info] total_cnts = [ geno_info['AA'] + geno_info['Aa'] + geno_info['aa'] for geno_info in plot_info ] num_snvs = len(passed) bottoms = [0] * num_snvs freq_bottoms = [0] * num_snvs lefts = range(num_snvs) for idx, geno in enumerate(('AA', 'Aa', 'aa')): cnts = [geno_info[geno] for geno_info in plot_info] backcolors = [greens[idx] if pss else reds[idx] for pss in passed] edgecolors = [grays[idx] for pss in passed] backcolors[tested_snp_idx], edgecolors[tested_snp_idx] = \ edgecolors[tested_snp_idx], backcolors[tested_snp_idx] heights = cnts axes1.bar(lefts, heights, bottom=bottoms, color=backcolors, edgecolor=edgecolors) freq_heights = [ height / total_cnt for height, total_cnt in zip(heights, total_cnts) ] axes2.bar(lefts, freq_heights, bottom=freq_bottoms, color=backcolors, edgecolor=edgecolors) bottoms = [bot + heig for bot, heig in zip(bottoms, heights)] freq_bottoms = [ bot + heig for bot, heig in zip(freq_bottoms, freq_heights) ] axes1.tick_params(axis='y', which='both', left='off', right='off') axes2.tick_params(axis='y', which='both', left='off', right='off') canvas = FigureCanvas(fig) canvas.print_figure(fhand) fhand.flush()
def _plot_segregation_debug(plot_info, fhand): greens = ['#2d6e12', '#76b75b', '#164900'] reds = ['#8f0007', '#fb1f2a', '#b90009'] grays = ['0.5', '0.25', '0.75'] fig = Figure(figsize=(10, 4)) axes1 = fig.add_subplot(211) axes2 = fig.add_subplot(212) plot_info.sort(key=itemgetter('pos')) is_close_snp = [geno_info['close_snp'] for geno_info in plot_info] tested_snp_idx = is_close_snp.index(False) passed = [geno_info['result'] for geno_info in plot_info] total_cnts = [geno_info['AA'] + geno_info['Aa'] + geno_info['aa'] for geno_info in plot_info] num_snvs = len(passed) bottoms = [0] * num_snvs freq_bottoms = [0] * num_snvs lefts = range(num_snvs) for idx, geno in enumerate(('AA', 'Aa', 'aa')): cnts = [geno_info[geno] for geno_info in plot_info] backcolors = [greens[idx] if pss else reds[idx] for pss in passed] edgecolors = [grays[idx] for pss in passed] backcolors[tested_snp_idx], edgecolors[tested_snp_idx] = \ edgecolors[tested_snp_idx], backcolors[tested_snp_idx] heights = cnts axes1.bar(lefts, heights, bottom=bottoms, color=backcolors, edgecolor=edgecolors) freq_heights = [height/total_cnt for height, total_cnt in zip(heights, total_cnts)] axes2.bar(lefts, freq_heights, bottom=freq_bottoms, color=backcolors, edgecolor=edgecolors) bottoms = [bot + heig for bot, heig in zip(bottoms, heights)] freq_bottoms = [bot + heig for bot, heig in zip(freq_bottoms, freq_heights)] axes1.tick_params(axis='y', which='both', left='off', right='off') axes2.tick_params(axis='y', which='both', left='off', right='off') canvas = FigureCanvas(fig) canvas.print_figure(fhand) fhand.flush()
def get_fig_and_canvas(num_rows=1, num_cols=1, figsize=None): if figsize is None: height = 5.0 * num_rows width = 7.5 * num_cols if height > 320.0: height = 320.0 figsize = (width, height) try: fig = Figure(figsize=figsize) canvas = FigureCanvas(fig) except NameError: msg = 'Matplotlib module is required to draw graphical histograms' raise OptionalRequirementError(msg) return fig, canvas
def get_canvas_and_axes(figure_size=FIGURE_SIZE, left=0.1, right=0.9, top=0.9, bottom=0.1, plot_type=111): 'It returns a matplotlib canvas and axes instance' try: fig = Figure(figsize=FIGURE_SIZE) canvas = FigureCanvas(fig) except NameError: msg = 'Matplotlib module is required to draw graphical histograms' raise OptionalRequirementError(msg) axes = fig.add_subplot(plot_type) fig.subplots_adjust(left=left, right=right, top=top, bottom=bottom) return canvas, axes