def main():
    # first read the data file
    pathname_in = sys.argv[1]
    X = util.file_to_comma_separated_matrix(pathname_in, has_headers=True)
    logging.debug('matrix size: ' + str(X.shape))
    # now read the newick file
    pathname_in = sys.argv[2]
    fin = open(pathname_in)
    newick_string = fin.read()
    fin.close()
    ordered_indices = heatmap.get_newick_order(newick_string)
    logging.debug('newick order size: ' + str(len(ordered_indices)))
    # reorder the rows of the data file
    X = heatmap.get_permuted_rows(X, ordered_indices)
    # create the standardized data for drawing the small heatmap
    Z = khorr.get_standardized_matrix(X)
    # initialize the progress bar
    reduction = 100
    npixels, remainder = divmod(len(X), reduction)
    if remainder:
        npixels += 1
    pbar = progress.Bar((npixels*(npixels+1))/2)
    # show the small heatmap
    pathname_out = 'small.png'
    im = heatmap.get_reduced_heatmap_image(Z, reduction=reduction, pixel_callback=pbar.update)
    fout = open(pathname_out, 'wb')
    im.save(fout)
    fout.close()
    # finish the progress bar
    pbar.finish()
def analyze(input_data_path, output_image_path, tree_building_function):
    logging.debug('read the matrix')
    X = util.file_to_comma_separated_matrix(input_data_path, has_headers=True)
    logging.debug('build the tree')
    root = tree_building_function(X)
    logging.debug('extract ordered indices from the tree')
    ordered_indices = root.ordered_labels()
    logging.debug('create the elementwise squared correlation matrix')
    R = np.corrcoef(X)
    logging.debug('permute the elementwise squared correlation matrix according to the ordering')
    M = heatmap.get_permuted_rows_and_columns(R, ordered_indices)
    logging.debug('create the heatmap')
    f = gradient.correlation_to_rgb
    heatmap.get_heatmap_with_dendrogram(M, root, f, output_image_path)
def main():
    pathname_in = 'mmc-data-files/Starvation_Residual.TXT'
    X = util.file_to_comma_separated_matrix(pathname_in, has_headers=True)
    print X.shape
    # create the tree from the data
    root = treebuilder.build_tree(X)
    ordered_indices = root.ordered_labels()
    X = heatmap.get_permuted_rows(X, ordered_indices)
    # create the standardized data for drawing the small heatmap
    Z = khorr.get_standardized_matrix(X)
    # show the big heatmap
    pathname_out = 'big.png'
    color_function = gradient.correlation_to_rgb
    im = heatmap.get_heatmap_image(np.dot(Z, Z.T), color_function)
    fout = open(pathname_out, 'wb')
    im.save(fout)
    fout.close()
    # show the small heatmap
    pathname_out = 'small.png'
    im = heatmap.get_reduced_heatmap_image(Z, reduction=5)
    fout = open(pathname_out, 'wb')
    im.save(fout)
    fout.close()
 def __init__(self, parent, npixels, data_filename, tree_filename, low_zoom_image_filename):
     """
     @param parent: the parent frame
     @param npixels: larger for larger windows
     @param data_filename: the path to a csv file
     @param tree_filename: the path to a newick tree
     @param low_zoom_image_filename: the path to a low resolution image
     """
     # save some args
     self.parent = parent
     # define some options
     self.npixels = npixels
     # create the tkinter image
     print 'creating the low resolution image...'
     raw_pil_image = Image.open(low_zoom_image_filename)
     low_zoom_pil_image = raw_pil_image.resize((self.npixels, self.npixels), Image.ANTIALIAS)
     low_zoom_image = ImageTk.PhotoImage(low_zoom_pil_image)
     # create the list of ordered gene names
     print 'creating the list of ordered gene names...'
     self.mtree_root = mtree.newick_file_to_mtree(tree_filename)
     names = get_gene_names(data_filename)
     ordered_gene_names = [names[row_index] for row_index in self.mtree_root.ordered_labels()]
     # create the standardized and sorted data matrix
     print 'creating the ordered correlation square root...'
     X = util.file_to_comma_separated_matrix(data_filename, has_headers=True)
     Z = khorr.get_standardized_matrix(X)
     Z = np.vstack(Z[row_index] for row_index in self.mtree_root.ordered_labels())
     #Z = None
     # create the label to leaf map
     print 'creating the map from labels to leaves...'
     self.label_to_leaf = dict((tip.label, tip) for tip in self.mtree_root.ordered_tips())
     # initialize the windows individually
     self._init_windows(low_zoom_image, Z, ordered_gene_names)
     # redo the layout of the windows
     self.repack()
     # initialize the connections among the windows
     self._connect_windows()