def scatterplot(self, enrich, cutoff=0.05, nmax=10, gene_set_size=[]): df = self._get_final_df(enrich.results, cutoff=cutoff, nmax=nmax) pylab.clf() pylab.scatter(-pylab.log10(df['Adjusted P-value']), range(len(df)), s=10 * df['size'], c=df['size']) pylab.xlabel("Odd ratio") pylab.ylabel("Gene sets") pylab.yticks(range(len(df)), df.name) a, b = pylab.xlim() pylab.xlim([0, b]) pylab.grid(True) ax = pylab.gca() M = max(df['size']) if M > 100: l1, l2, l3 = "10", "100", str(M) else: l1, l2, l3 = str(round(M / 3)), str(round(M * 2 / 3)), str(M) handles = [ pylab.Line2D([0], [0], marker="o", markersize=5, label=l1, ls=""), pylab.Line2D([0], [0], marker="o", markersize=10, label=l2, ls=""), pylab.Line2D([0], [0], marker="o", markersize=15, label=l3, ls="") ] ax.legend(handles=handles, loc="upper left", title="gene-set size") pylab.axvline(1.3, lw=2, ls="--", color="r") pylab.tight_layout() ax = pylab.colorbar(pylab.gci()) return df
def scatter_length_cov_gc(self, min_length=200, min_cov=10): pylab.clf() pylab.scatter(self.df.length, self.df['cov'], c=self.df.GC) pylab.loglog() pylab.axvline(min_length, lw=2, c="r", ls='--') pylab.axhline(min_cov, lw=2, c="r", ls='--') pylab.xlabel("contig length") pylab.ylabel("contig coverage") pylab.colorbar(label="GC") pylab.grid(True)
def plot_volcano(self, plotly=False, marker_color='b'): if plotly: from plotly import express as px df = self.df.copy() df["log_adj_pvalue"] = self.df['-log10(pvalue)'] df["log2FoldChange"] = pylab.log2(self.df['fold_enrichment']) #df['hover_name'] = df['start'] hover_name = 'start' try: df['info'] = df['chr'] + ":" + df['start'].astype( str) + "-" + df['stop'].astype(str) except: df['info'] = df['chr'] + ":" + df['start'].astype( str) + "-" + df['end'].astype(str) fig = px.scatter( df, x="log2FoldChange", y="log_adj_pvalue", hover_name="info", log_y=False, color="length", height=600, labels={"log2_fold_enrichment": "log10 p-value"}, ) """fig.update_layout( shapes=[dict(type='line', xref='x', x0=df.log2FoldChange.min(), x1=df.log2FoldChange.max(), yref='y', y0=-pylab.log10(padj), y1=-pylab.log10(padj), line=dict( color="black", width=1, dash="dash")) ]) """ return fig pylab.scatter(pylab.log2(self.df['fold_enrichment']), self.df['-log10(pvalue)'], marker="o", alpha=0.5, color=marker_color, lw=0, s=self.df['length'] / self.df['length'].max() * 400) pylab.xlabel('Fold enrichment') pylab.ylabel('log10 pvalue')
def plot_scatter_contig_length_nread_cov(self, fontsize=16, vmin=0, vmax=50, min_nreads=20, min_length=50000): if self._df is None: _ = self.get_df() pylab.clf() df = self._df m1 = df.length.min() M1 = df.length.max() # least square X = df.query("nread>@min_nreads and length>@min_length")['length'] Y = df.query("nread>@min_nreads and length>@min_length")['nread'] Z = df.query("nread>@min_nreads and length>@min_length")['covStat'] print(X) print(Y) print(Z) A = np.vstack([X, np.ones(len(X))]).T m, c = np.linalg.lstsq(A, Y.as_matrix())[0] x = np.array([m1, M1]) X = df['length'] Y = df['nread'] Z = df['covStat'] pylab.scatter(X, Y, c=Z, vmin=vmin, vmax=vmax) pylab.colorbar() pylab.xlabel("Contig length", fontsize=fontsize) pylab.ylabel("Contig reads", fontsize=fontsize) pylab.title("coverage function of contig length and reads used") pylab.grid() pylab.plot(x, m * x + c, "o-r") pylab.loglog() pylab.tight_layout()
def _plot(self, Xr, pca=None, pc1=0, pc2=1, colors=None, show_labels=True): if colors is None: colors = [self.colors[k] for k in self.labels] if len(colors) != len(Xr): colors = ["r"] * len(Xr[:,0]) else: for k in self.labels: if k not in colors.keys(): logger.warning("No key color for this sample: {}. Set to red".format(k)) colors[k] = "r" colors = [colors[k] for k in self.labels] pylab.scatter(Xr[:,pc1], Xr[:,pc2], c=colors) ax = pylab.gca() X1, X2 = pylab.xlim() dX = X2 - X1 pylab.xlim([X1 + X1*0.05, X2 + X2*0.05]) Y1, Y2 = pylab.ylim() dY = Y2 - Y1 pylab.ylim([Y1 + Y1*0.05, Y2 + Y2*0.05]) count = 0 if show_labels: for x,y in zip(Xr[:,pc1], Xr[:,pc2]): x += dX / 40 y += dY / 40 ax.annotate(self.labels[count], (x,y)) count += 1 if count > 100: break if pca: pylab.xlabel("PC{} ({}%)".format(pc1+1, round(pca.explained_variance_ratio_[pc1]*100, 2))) pylab.ylabel("PC{} ({}%)".format(pc2+1, round(pca.explained_variance_ratio_[pc2]*100, 2))) pylab.grid(True)
def plot_go_terms(self, ontologies, max_features=50, log=False, fontsize=8, minimum_genes=0, pvalue=0.05, cmap="summer_r", sort_by="fold_enrichment", show_pvalues=False, include_negative_enrichment=False, fdr_threshold=0.05, compute_levels=True, progress=True): assert sort_by in ['pValue', 'fold_enrichment', 'fdr'] # FIXME: pvalue and fold_enrichment not sorted in same order pylab.clf() df = self.get_data( ontologies, include_negative_enrichment=include_negative_enrichment, fdr=fdr_threshold) if len(df) == 0: return df df = df.query("pValue<=@pvalue") logger.info("Filtering out pvalue>{}. Kept {} GO terms".format( pvalue, len(df))) df = df.reset_index(drop=True) # Select a subset of the data to keep the best max_features in terms of # pValue subdf = df.query("number_in_list>@minimum_genes").copy() logger.info( "Filtering out GO terms with less than {} genes: Kept {} GO terms". format(minimum_genes, len(subdf))) logger.info("Filtering out the 3 parent terms") subdf = subdf.query("id not in @self.ontologies") # Keeping only a part of the data, sorting by pValue if sort_by == "pValue": subdf = subdf.sort_values(by="pValue", ascending=False).iloc[-max_features:] df = df.sort_values(by="pValue", ascending=False) elif sort_by == "fold_enrichment": subdf = subdf.sort_values(by="abs_log2_fold_enrichment", ascending=True).iloc[-max_features:] df = df.sort_values(by="abs_log2_fold_enrichment", ascending=False) elif sort_by == "fdr": subdf = subdf.sort_values(by="fdr", ascending=False).iloc[-max_features:] df = df.sort_values(by="fdr", ascending=False) subdf = subdf.reset_index(drop=True) # We get all levels for each go id. # They are stored by MF, CC or BP if compute_levels: paths = self.get_graph(list(subdf['id'].values), progress=progress) levels = [] keys = list(paths.keys()) goid_levels = paths[keys[0]] if len(keys) > 1: for k in keys[1:]: goid_levels.update(paths[k]) levels = [goid_levels[ID] for ID in subdf['id'].values] subdf["level"] = levels else: subdf['level'] = "" N = len(subdf) size_factor = 12000 / len(subdf) max_size = subdf.number_in_list.max() min_size = subdf.number_in_list.min() sizes = [ max(max_size * 0.2, x) for x in size_factor * subdf.number_in_list.values / subdf.number_in_list.max() ] m1 = min(sizes) m3 = max(sizes) m2 = m1 + (m3 - m1) / 2 if log: pylab.scatter(pylab.log2(subdf.fold_enrichment), range(len(subdf)), c=subdf.fdr, s=sizes, cmap=cmap, alpha=0.8, ec="k", vmin=0, vmax=fdr_threshold, zorder=10) #pylab.barh(range(N), pylab.log2(subdf.fold_enrichment), color="r", # label="pvalue>0.05; FDR>0.05") #pylab.axvline(1, color="gray", ls="--") #pylab.axvline(-1, color="gray", ls="--") else: pylab.scatter(subdf.fold_enrichment, range(len(subdf)), c=subdf.fdr, cmap=cmap, s=sizes, ec="k", alpha=.8, vmin=0, vmax=fdr_threshold, zorder=10) # pylab.barh(range(N), subdf.fold_enrichment, color="r", # label="not significant") pylab.grid(zorder=-10) ax2 = pylab.colorbar(shrink=0.5) ax2.ax.set_ylabel('FDR') labels = [ x if len(x) < 50 else x[0:47] + "..." for x in list(subdf.label) ] ticks = [ "{} ({}) {}".format(ID, level, "; " + label.title()) for level, ID, label in zip(subdf['level'], subdf.id, labels) ] pylab.yticks(range(N), ticks, fontsize=fontsize, ha='left') yax = pylab.gca().get_yaxis() try: pad = [x.label.get_window_extent().width for x in yax.majorTicks] yax.set_tick_params(pad=max(pad)) except: yax.set_tick_params(pad=60 * fontsize * 0.7) yax.set_tick_params(pad=60 * fontsize * 0.6) fc_max = subdf.fold_enrichment.max(skipna=True) fc_min = subdf.fold_enrichment.min(skipna=True) # go into log2 space fc_max = pylab.log2(fc_max) fc_min = pylab.log2(fc_min) abs_max = max(fc_max, abs(fc_min), 1) if log: fc_max = abs_max * 1.5 else: fc_max = 2**abs_max * 1.2 pylab.axvline(0, color="k", lw=2) if log: pylab.xlabel("Fold Enrichment (log2)") else: pylab.xlabel("Fold Enrichment") if include_negative_enrichment: pylab.xlim([-fc_max, fc_max]) else: pylab.xlim([0, fc_max]) pylab.tight_layout() # The pvalue: if show_pvalues: ax = pylab.gca().twiny() ax.set_xlim([0, max(-pylab.log10(subdf.pValue)) * 1.2]) ax.set_xlabel("p-values (log10)", fontsize=12) ax.plot(-pylab.log10(subdf.pValue), range(len(subdf)), label="pvalue", lw=2, color="k") ax.axvline(1.33, lw=1, ls="--", color="grey", label="pvalue=0.05") pylab.tight_layout() pylab.legend(loc="lower right") s1 = pylab.scatter([], [], s=m1, marker='o', color='#555555', ec="k") s2 = pylab.scatter([], [], s=m2, marker='o', color='#555555', ec="k") s3 = pylab.scatter([], [], s=m3, marker='o', color='#555555', ec="k") if len(subdf) < 10: labelspacing = 1.5 * 4 borderpad = 4 handletextpad = 2 elif len(subdf) < 20: labelspacing = 1.5 * 2 borderpad = 1 handletextpad = 2 else: labelspacing = 1.5 borderpad = 2 handletextpad = 2 if len(subdf) >= 3: leg = pylab.legend( (s1, s2, s3), (str(int(min_size)), str(int(min_size + (max_size - min_size) / 2)), str(int(max_size))), scatterpoints=1, loc='lower right', ncol=1, frameon=True, title="gene-set size", labelspacing=labelspacing, borderpad=borderpad, handletextpad=handletextpad, fontsize=8) else: leg = pylab.legend((s1, ), (str(int(min_size)), ), scatterpoints=1, loc='lower right', ncol=1, frameon=True, title="gene-set size", labelspacing=labelspacing, borderpad=borderpad, handletextpad=handletextpad, fontsize=8) frame = leg.get_frame() frame.set_facecolor('#b4aeae') frame.set_edgecolor('black') frame.set_alpha(1) self.subdf = subdf self.df = df return df