12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- def volcano(df="dataframe", lfc=None, pv=None, lfc_thr=(1, 1), pv_thr=(0.05, 0.05), color=("green", "grey", "red"),
- valpha=1, geneid=None, genenames=None, gfont=8, dim=(5, 5), r=300, ar=90, dotsize=8, markerdot="o",
- sign_line=False, gstyle=1, show=False, figtype='png', axtickfontsize=9,
- axtickfontname="Arial", axlabelfontsize=9, axlabelfontname="Arial", axxlabel=None,
- axylabel=None, xlm=None, ylm=None, plotlegend=False, legendpos='best',
- figname='volcano', legendanchor=None,
- legendlabels=['significant up', 'not significant', 'significant down'], theme=None):
- _x = r'$ log_{2}(Fold Change)$'
- _y = r'$ -log_{10}(P-value)$'
- color = color
- # check if dataframe contains any non-numeric character
- assert general.check_for_nonnumeric(df[lfc]) == 0, 'dataframe contains non-numeric values in lfc column'
- assert general.check_for_nonnumeric(df[pv]) == 0, 'dataframe contains non-numeric values in pv column'
- # this is important to check if color or logpv exists and drop them as if you run multiple times same command
- # it may update old instance of df
- df = df.drop(['color_add_axy', 'logpv_add_axy'], axis=1, errors='ignore')
- assert len(set(color)) == 3, 'unique color must be size of 3'
- df.loc[(df[lfc] >= lfc_thr[0]) & (df[pv] < pv_thr[0]), 'color_add_axy'] = color[0] # upregulated
- df.loc[(df[lfc] <= -lfc_thr[1]) & (df[pv] < pv_thr[1]), 'color_add_axy'] = color[2] # downregulated
- df['color_add_axy'].fillna(color[1], inplace=True) # intermediate
- df['logpv_add_axy'] = -(np.log10(df[pv]))
- # plot
- assign_values = {col: i for i, col in enumerate(color)}
- color_result_num = [assign_values[i] for i in df['color_add_axy']]
- assert len(set(color_result_num)) == 3, \
- 'either significant or non-significant genes are missing; try to change lfc_thr or pv_thr to include ' \
- 'both significant and non-significant genes'
- if theme == 'dark':
- general.dark_bg()
- plt.subplots(figsize=dim)
- if plotlegend:
- s = plt.scatter(df[lfc], df['logpv_add_axy'], c=color_result_num, cmap=ListedColormap(color), alpha=valpha,
- s=dotsize, marker=markerdot)
- assert len(legendlabels) == 3, 'legendlabels must be size of 3'
- plt.legend(handles=s.legend_elements()[0], labels=legendlabels, loc=legendpos, bbox_to_anchor=legendanchor)
- else:
- plt.scatter(df[lfc], df['logpv_add_axy'], c=color_result_num, cmap=ListedColormap(color), alpha=valpha,
- s=dotsize, marker=markerdot)
- if sign_line:
- plt.axhline(y=-np.log10(pv_thr[0]), linestyle='--', color='#7d7d7d', linewidth=1)
- plt.axvline(x=lfc_thr[0], linestyle='--', color='#7d7d7d', linewidth=1)
- plt.axvline(x=-lfc_thr[1], linestyle='--', color='#7d7d7d', linewidth=1)
- GeneExpression.gene_plot(df, geneid, lfc, lfc_thr, pv_thr, genenames, gfont, pv, gstyle)
- if axxlabel:
- _x = axxlabel
- if axylabel:
- _y = axylabel
- general.axis_labels(_x, _y, axlabelfontsize, axlabelfontname)
- general.axis_ticks(xlm, ylm, axtickfontsize, axtickfontname, ar)
- general.get_figure(show, r, figtype, figname, theme)
|