Esempio n. 1
0
def plot_matrix(matrix, strain_labels, vfs_classes, gene_labels,  
        show_gene_labels, color_index, config_object, grid, seed, 
        dpi, size, svg, aspect='auto'):
    """
    Plot the VF hit matrix

    :param matrix: the numpy matrix of scores
    :param strain_labels: the strain (y labels)
    :param vfs_classes: the VFS class (in mfa header [class])
    :param gene_labels: the gene labels
    :param show_gene_labels: wheter top plot the gene labels
    :param color_index: for a single class, choose a specific color
    """
    if config_object['category_colors'] != None:
        colors = config_object['category_colors']
    else:
        colors = imaging.generate_colors(len(set(vfs_classes)), seed)
    if color_index != None:
        print color_index
        print colors
        colors = [colors[(color_index)]]
        print colors
        print "\n"
    # Build the regions to be shaded differently
    regions, prev = [], 0
    for i in xrange(0, len(vfs_classes)-1):
        if vfs_classes[i] != vfs_classes[i+1]:
            regions.append([prev+0.5, i+0.5])
            prev = i
    regions.append([prev+0.5, len(vfs_classes)-0.5])
    regions[0][0] = regions[0][0]-1.0
    plt.clf()
    fig = plt.figure()
    ax = fig.add_subplot(111)
    # aspect auto to widen
    ax.matshow(matrix, cmap=cm.gray, aspect=aspect)
    # Make sure every strain
    ax.yaxis.set_major_locator(MultipleLocator(1))
    ax.yaxis.set_major_formatter(FormatStrFormatter('%s'))
    ax.set_yticklabels(strain_labels)
    if len(gene_labels) < 999:
        ax.xaxis.set_major_locator(MultipleLocator(1))
        ax.xaxis.set_major_formatter(FormatStrFormatter('%s'))
        ax.xaxis.grid(False) 
    if show_gene_labels:
        ax.set_xticklabels([''] +gene_labels)#, rotation=90)
    for i in xrange(0, len(regions)):
        plt.axvspan(regions[i][0], regions[i][1], facecolor=colors[i],         \
                        alpha=0.1)
    if show_gene_labels:
        ax.tick_params(axis='both', which='both', labelsize=6, direction='out',\
                       labelleft='on', labelright='off', \
                       labelbottom='off', labeltop='on', \
                       left='on', right='off', bottom='off', top='on')
    else:
        ax.tick_params(axis='both', which='both', labelsize=6, direction='out',\
                       labelleft='on', labelright='off', \
                       labelbottom='off', labeltop='off', \
                       left='on', right='off', bottom='off', top='off')
    plt.xticks(rotation=90)
    if grid: ax.grid(True)
    x, y = size.split('x')
    x, y = float(x), float(y)
    fig.set_size_inches(x, y, dpi=dpi)
    if svg: 
        plt.savefig("results.svg", bbox_inches='tight',dpi=dpi)
    else:
        plt.savefig("results.png", bbox_inches='tight',dpi=dpi)
Esempio n. 2
0
def plot_matrix(matrix, strain_labels, vfs_classes, gene_labels,  
        show_gene_labels, color_index, config_object, grid, seed, 
        dpi, size, svg, compress, compressed_classes=None, 
        compressed_gene_labels=None,aspect='auto'):
    """
    Plot the VF hit matrix

    *NOTE:* Even if we're plotting a compressed matrix, we will always save
    all data required to plot an uncompressed version.

    TODO: update doc for this!!!

    compressed_classes     ~= vfs_classes
    compressed_gene_labels ~= gene_labels

    :param matrix: the numpy matrix of scores
    :param strain_labels: the strain (y labels)
    :param vfs_classes: the VFS class (in mfa header [class])
    :param gene_labels: the gene labels
    :param show_gene_labels: wheter top plot the gene labels
    :param color_index: for a single class, choose a specific color
    """
    # Saving data for regeneration
    if compress != None:
        classes = open(os.path.join('data', "classes.txt"), 'wb')
        for item in vfs_classes:
            classes.write(item+'\n')
        classes.close()
        xlab = open(os.path.join('data', "xlabels.txt"), 'wb')
        for item in gene_labels:
            xlab.write(item+'\n')
        xlab.close()
        ylab = open(os.path.join('data', "strain_labels.txt"), 'wb')
        for item in strain_labels:
            # Don't skip bleedthrough
            #if(item != ''):
            ylab.write(item+'\n')
        ylab.close()
    # Toggle between all and compressed.
    if compressed_classes != None and compressed_gene_labels != None:
        vfs_classes = compressed_classes
        gene_labels = compressed_gene_labels

    if config_object['category_colors'] != None:
        colors = config_object['category_colors']
    else:
        print compress
        print compressed_classes
        print compressed_gene_labels
        colors = imaging.generate_colors(len(set(vfs_classes)), seed)
    if color_index != None:
        colors = [colors[(color_index)]]
    # Build the regions to be shaded differently
    regions, prev = [], 0
    for i in xrange(0, len(vfs_classes)-1):
        if vfs_classes[i] != vfs_classes[i+1]:
            regions.append([prev+0.5, i+0.5])
            prev = i
    regions.append([prev+0.5, len(vfs_classes)-0.5])
    regions[0][0] = regions[0][0]-1.0
    plt.clf()
    fig = plt.figure()
    ax = fig.add_subplot(111)
    # aspect auto to widen
    ax.matshow(matrix, cmap=cm.gray, aspect=aspect)
    # Make sure every strain
    ax.yaxis.set_major_locator(MultipleLocator(1))
    ax.yaxis.set_major_formatter(FormatStrFormatter('%s'))
    ax.set_yticklabels(strain_labels)
    if len(gene_labels) < 999:
        ax.xaxis.set_major_locator(MultipleLocator(1))
        ax.xaxis.set_major_formatter(FormatStrFormatter('%s'))
        ax.xaxis.grid(False) 
    if show_gene_labels:
        ax.set_xticklabels([''] +gene_labels)#, rotation=90)
    for i in xrange(0, len(regions)):
        plt.axvspan(regions[i][0], regions[i][1], facecolor=colors[i],         \
                        alpha=0.1)
    if show_gene_labels:
        ax.tick_params(axis='both', which='both', labelsize=6, direction='out',\
                       labelleft='on', labelright='off', \
                       labelbottom='off', labeltop='on', \
                       left='on', right='off', bottom='off', top='on')
    else:
        ax.tick_params(axis='both', which='both', labelsize=6, direction='out',\
                       labelleft='on', labelright='off', \
                       labelbottom='off', labeltop='off', \
                       left='on', right='off', bottom='off', top='off')
    plt.xticks(rotation=90)
    if grid: ax.grid(True)
    x, y = size.split('x')
    x, y = float(x), float(y)
    fig.set_size_inches(x, y, dpi=dpi)
    if svg: 
        plt.savefig("results.svg", bbox_inches='tight',dpi=dpi)
    else:
        plt.savefig("results.png", bbox_inches='tight',dpi=dpi)