def writesorted(self, lineages, base_file, path, metric='euclidean'): ''' Do hierarchical clustering of rows and cols and write the sorted data to files. ''' from heatmap_clust import clust_data, heatmap_clust matrix, row_labels, col_labels = self.to_matrix() mat_sorted, row_labels_sorted, col_labels_sorted = clust_data( matrix, metric, row_labels=row_labels, col_labels=col_labels, row_label_width=.25) row_labels_sorted.reverse() mat_file = path + base_file + '_abunds_sorted.txt' np.savetxt(mat_file, mat_sorted) otu_file = path + base_file + '_OTUs_sorted.txt' f = open(otu_file, 'w') for otu in row_labels_sorted: f.write('\t'.join([otu, lineages[otu]]) + '\n') f.close() samples_file = path + base_file + '_samples_sorted.txt' f = open(samples_file, 'w') for sample in col_labels_sorted: f.write(sample + '\n') f.close()
def plot_heatmap(self, row_metric ='euclidean', col_metric ='euclidean', file = None, **kwargs): ''' Plot heatmap of self, sorted by heirarchical clustering with given distance metric ''' from heatmap_clust import clust_data, heatmap_clust matrix, row_labels, col_labels = self.to_matrix() plot_row_labels = kwargs.get('plot_row_labels',False) plot_col_labels = kwargs.get('plot_col_labels',False) if plot_row_labels and plot_col_labels: clust_data(matrix, row_metric, col_metric, file = file, row_labels = row_labels, col_labels = col_labels, **kwargs) elif plot_row_labels and not plot_col_labels: clust_data(matrix, row_metric, col_metric, file = file, row_labels = row_labels, **kwargs) elif not plot_row_labels and plot_col_labels: clust_data(matrix, row_metric, col_metric, file = file, col_labels = col_labels, **kwargs) else: clust_data(matrix, row_metric, col_metric, file = file, **kwargs)
def writesorted(self, lineages, base_file, path, metric = 'euclidean'): ''' Do hierarchical clustering of rows and cols and write the sorted data to files. ''' from heatmap_clust import clust_data, heatmap_clust matrix, row_labels, col_labels = self.to_matrix() mat_sorted, row_labels_sorted, col_labels_sorted = clust_data(matrix, metric, row_labels = row_labels,col_labels = col_labels, row_label_width = .25) row_labels_sorted.reverse() mat_file = path + base_file + '_abunds_sorted.txt' np.savetxt(mat_file, mat_sorted) otu_file = path + base_file + '_OTUs_sorted.txt' f = open(otu_file,'w') for otu in row_labels_sorted: f.write('\t'.join([otu, lineages[otu]]) + '\n') f.close() samples_file = path + base_file + '_samples_sorted.txt' f = open(samples_file,'w') for sample in col_labels_sorted: f.write(sample + '\n') f.close()