def plot_matrix(self, matrix): fig, ax = plt.subplots(1, 1) ax.plot(range(len(matrix)), np.sort(matrix)) ax.set(xlabel="Genes", ylabel="log2(TPM+1)") fig.savefig(join_paths([self.images_dir, 'gene_count_distribution.png'])) fig, ax = plt.subplots(1,1) ax.hist(matrix) fig.savefig(join_paths([self.images_dir, 'gene_count_histogram.png']))
def __init__(self, dir_path_10x=BasePaths.Pilot19, cache_directory=BasePaths.Cache): super(UploadMatExpressionMatrix, self).__init__(cache_dir=cache_directory) self.dir_path_10x = dir_path_10x self.cache_directory = cache_directory self.expression_matrix_path = join_paths( [self.dir_path_10x, 'matrix.mtx']) self.cell_matrix_path = join_paths([self.dir_path_10x, 'barcodes.tsv']) self.genes_matrix_path = join_paths([self.dir_path_10x, 'genes.tsv']) self.composing_items.append(self.expression_matrix_path) self.composing_items.append(self.cell_matrix_path)
def plot_distance_distribution(self, distances, name=''): all_distances = distances.values upper_distances = np.triu(all_distances, k=0) upper_distances = upper_distances.reshape(-1) upper_distances = upper_distances[upper_distances > 0] plt.subplot() sns.distplot(upper_distances) plt.savefig(join_paths([self.outfile_path, name + 'distance_distribution.png']))
def __init__(self, filter_by_cell_line=True): self.expression_matrix = load_if_cached( join_paths([BasePaths.ExpressionProcessed]), pre_process_expression_matrix) if filter_by_cell_line: self.expression_matrix, self.cell_lines_matrix, self.cell_lines_set = filter_expression_by_cell_line( self.expression_matrix) self.separated_expressions = {} for cell_line in self.cell_lines_set: self.separated_expressions[ cell_line] = self.CellLineExpression( self.expression_matrix, self.cell_lines_matrix, cell_line)
def transform(self, expression_object, *args, **kwargs): try: if self.DEBUG and not self.ignore_debug: raise IOError with open(self.out_file_path(expression_object), 'rb') as in_file: ret = pickle.load(in_file) if ret.composing_items[-1] == self and ret.composing_items[:-1] == expression_object.composing_items: return ret else: logging.log("Wrong code version of transformer, running transformer again") raise Exception except: logging.info("Running transformer {}".format(type(self))) ret = self.transform_aux(expression_object, args, kwargs) ret.composing_items.append(self) with open(join_paths([self.cache_dir, ret.name]), 'wb') as out_file: pickle.dump(ret, out_file) logging.info( "Saved result from transformer {} with parameters {} in {}".format(type(self), self.composing_items, self.out_file_path( expression_object))) return ret
def out_file_path(self, expression_object=None, *args, **kwargs): if expression_object is None: return join_paths([self.cache_dir, self.out_file_name()]) else: return join_paths([self.cache_dir, self.out_file_name(expression_object.name)])
def out_file_path(self, expression_object=None, *args, **kwargs): return join_paths([self.cache_directory, self.file_suffix])