Ejemplo n.º 1
0
def make_the_gene_with_exon_and_map(chr,
                                    gene_name,
                                    ax,
                                    exon_lib,
                                    x,
                                    y,
                                    locus,
                                    frequency,
                                    width=30.0,
                                    height=5.0,
                                    scale=1.0):
    genome = EnsemblRelease(75)
    genes = genome.genes_by_name(gene_name)
    gene = genes[0]
    gene_start = gene.start
    gene_end = gene.end
    gene_length = float(gene_end - gene_start)
    # draw the gene
    ax.add_patch(
        patches.Rectangle(
            (x, y - height / 2),
            width,
            height,
            alpha=0.5,
            color="dodgerblue",
            linewidth=0,
        ))
    # find the exon ids
    exon_id = genome.exon_ids_of_gene_name(gene_name)
    # plot the gene with exons
    for i in exon_id:
        position = exon_lib[i]
        exon_start = int(position[0])
        exon_end = int(position[1])
        exon_length = exon_end - exon_start
        plot_length = exon_length / gene_length * width
        plot_x = (exon_start - gene_start) / gene_length * width
        ax.add_patch(
            patches.Rectangle(
                (x + plot_x, y - height / 2 - 0.5 * scale),
                plot_length,
                height + 1 * scale,
                color="black",
                alpha=0.4,
                linewidth=0,
            ))
    # map the read to the plot
    for i in range(len(locus)):
        plot_x = (locus[0] - gene_start) / gene_length * width
        if frequency[i] < 0.0001:
            bar_length = 1
        else:
            bar_length = abs(math.log(frequency[i] / 0.01) * 2)
        ax.plot(
            (x + plot_x, x + plot_x),
            (y + height / 2 + scale * 0.5,
             y + height / 2 + scale * 0.5 + bar_length),
            color="darkblue",
            alpha=0.5,
            linewidth=2,
        )
    # gene name at the left
    ax.text(x - 0.5,
            y,
            gene_name + '\n' + chr,
            horizontalalignment='right',
            verticalalignment='center',
            fontsize=25,
            fontweight="bold")
    # gene start
    ax.text(
        x,
        y - height / 2 - 0.8 * scale,
        gene_start,
        horizontalalignment='center',
        verticalalignment='top',
        fontsize=12,
    )
    # gene end
    ax.text(
        x + width,
        y - height / 2 - 0.8 * scale,
        gene_end,
        horizontalalignment='center',
        verticalalignment='top',
        fontsize=12,
    )