12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- def ma(df="dataframe", lfc=None, ct_count=None, st_count=None, basemean=None, pv=None, lfc_thr=(1, 1), pv_thr=0.05,
- valpha=1, dotsize=8,markerdot="o", dim=(6, 5), r=300, show=False, color=("green", "grey", "red"), ar=0,
- figtype='png',axtickfontsize=9, axtickfontname="Arial", axlabelfontsize=9, axlabelfontname="Arial",
- axxlabel=None, axylabel=None, xlm=None, ylm=None, fclines=False, fclinescolor='#2660a4', legendpos='best',
- figname='ma', legendanchor=None, legendlabels=['significant up', 'not significant', 'significant down'],
- plotlegend=False, theme=None, geneid=None, genenames=None, gfont=8, gstyle=1, title=None):
- _x, _y = 'A', 'M'
- assert General.check_for_nonnumeric(df[lfc]) == 0, 'dataframe contains non-numeric values in lfc column'
- if ct_count and st_count:
- assert General.check_for_nonnumeric(df[ct_count]) == 0, \
- 'dataframe contains non-numeric values in ct_count column'
- assert General.check_for_nonnumeric(
- df[st_count]) == 0, 'dataframe contains non-numeric values in ct_count column'
- if basemean:
- assert General.check_for_nonnumeric(df[basemean]) == 0, \
- 'dataframe contains non-numeric values in basemean column'
- # this is important to check if color or A 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', 'A_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), 'color_add_axy'] = color[0] # upregulated
- df.loc[(df[lfc] <= -lfc_thr[1]) & (df[pv] < pv_thr), 'color_add_axy'] = color[2] # downregulated
- df['color_add_axy'].fillna(color[1], inplace=True) # intermediate
- if basemean:
- # basemean (mean of normalized counts from DESeq2 results)
- df['A_add_axy'] = df[basemean]
- else:
- df['A_add_axy'] = (np.log2(df[ct_count]) + np.log2(df[st_count])) / 2
- # 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' \
- ' to include both significant and non-significant genes'
- if theme:
- General.style_bg(theme)
- plt.subplots(figsize=dim)
- if plotlegend:
- s = plt.scatter(df['A_add_axy'], df[lfc], 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['A_add_axy'], df[lfc], c=color_result_num, cmap=ListedColormap(color),
- alpha=valpha, s=dotsize, marker=markerdot)
- # draw a central line at M=0
- plt.axhline(y=0, color='#7d7d7d', linestyle='--')
- # draw lfc threshold lines
- if fclines:
- plt.axhline(y=lfc_thr[0], color=fclinescolor, linestyle='--')
- plt.axhline(y=-lfc_thr[1], color=fclinescolor, linestyle='--')
- if axxlabel:
- _x = axxlabel
- if axylabel:
- _y = axylabel
- GeneExpression.geneplot_ma(df, geneid, lfc, lfc_thr, genenames, gfont, gstyle)
- General.axis_labels(_x, _y, axlabelfontsize, axlabelfontname)
- General.axis_ticks(xlm, ylm, axtickfontsize, axtickfontname, ar)
- General.get_figure(show, r, figtype, figname, theme, title)
|