1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- def involcano(df="dataframe", lfc="logFC", pv="p_values", 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='involcano', 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
- 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)
- GeneExpression.gene_plot(df, geneid, lfc, lfc_thr, pv_thr, genenames, gfont, pv, gstyle)
- plt.gca().invert_yaxis()
- if axxlabel:
- _x = axxlabel
- if axylabel:
- _y = axylabel
- general.axis_labels(_x, _y, axlabelfontsize, axlabelfontname)
- if xlm:
- print('Error: xlm not compatible with involcano')
- sys.exit(1)
- if ylm:
- print('Error: ylm not compatible with involcano')
- sys.exit(1)
- general.axis_ticks(xlm, ylm, axtickfontsize, axtickfontname, ar)
- general.get_figure(show, r, figtype, figname, theme)
|