visualize_5_5.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. def involcano(df="dataframe", lfc="logFC", pv="p_values", lfc_thr=(1, 1), pv_thr=(0.05, 0.05), color=("green", "grey", "red"),
  2. valpha=1, geneid=None, genenames=None, gfont=8, dim=(5, 5), r=300, ar=90, dotsize=8, markerdot="o",
  3. sign_line=False, gstyle=1, show=False, figtype='png', axtickfontsize=9,
  4. axtickfontname="Arial", axlabelfontsize=9, axlabelfontname="Arial", axxlabel=None,
  5. axylabel=None, xlm=None, ylm=None, plotlegend=False, legendpos='best',
  6. figname='involcano', legendanchor=None, legendlabels=['significant up', 'not significant', 'significant down'],
  7. theme=None):
  8. _x = r'$ log_{2}(Fold Change)$'
  9. _y = r'$ -log_{10}(P-value)$'
  10. color = color
  11. assert general.check_for_nonnumeric(df[lfc]) == 0, 'dataframe contains non-numeric values in lfc column'
  12. assert general.check_for_nonnumeric(df[pv]) == 0, 'dataframe contains non-numeric values in pv column'
  13. # this is important to check if color or logpv exists and drop them as if you run multiple times same command
  14. # it may update old instance of df
  15. df = df.drop(['color_add_axy', 'logpv_add_axy'], axis=1, errors='ignore')
  16. assert len(set(color)) == 3, 'unique color must be size of 3'
  17. df.loc[(df[lfc] >= lfc_thr[0]) & (df[pv] < pv_thr[0]), 'color_add_axy'] = color[0] # upregulated
  18. df.loc[(df[lfc] <= -lfc_thr[1]) & (df[pv] < pv_thr[1]), 'color_add_axy'] = color[2] # downregulated
  19. df['color_add_axy'].fillna(color[1], inplace=True) # intermediate
  20. df['logpv_add_axy'] = -(np.log10(df[pv]))
  21. # plot
  22. assign_values = {col: i for i, col in enumerate(color)}
  23. color_result_num = [assign_values[i] for i in df['color_add_axy']]
  24. assert len(set(color_result_num)) == 3, 'either significant or non-significant genes are missing; try to change lfc_thr or ' \
  25. 'pv_thr to include both significant and non-significant genes'
  26. if theme == 'dark':
  27. general.dark_bg()
  28. plt.subplots(figsize=dim)
  29. if plotlegend:
  30. s = plt.scatter(df[lfc], df['logpv_add_axy'], c=color_result_num, cmap=ListedColormap(color), alpha=valpha,
  31. s=dotsize, marker=markerdot)
  32. assert len(legendlabels) == 3, 'legendlabels must be size of 3'
  33. plt.legend(handles=s.legend_elements()[0], labels=legendlabels, loc=legendpos,
  34. bbox_to_anchor=legendanchor)
  35. else:
  36. plt.scatter(df[lfc], df['logpv_add_axy'], c=color_result_num, cmap=ListedColormap(color), alpha=valpha,
  37. s=dotsize, marker=markerdot)
  38. GeneExpression.gene_plot(df, geneid, lfc, lfc_thr, pv_thr, genenames, gfont, pv, gstyle)
  39. plt.gca().invert_yaxis()
  40. if axxlabel:
  41. _x = axxlabel
  42. if axylabel:
  43. _y = axylabel
  44. general.axis_labels(_x, _y, axlabelfontsize, axlabelfontname)
  45. if xlm:
  46. print('Error: xlm not compatible with involcano')
  47. sys.exit(1)
  48. if ylm:
  49. print('Error: ylm not compatible with involcano')
  50. sys.exit(1)
  51. general.axis_ticks(xlm, ylm, axtickfontsize, axtickfontname, ar)
  52. general.get_figure(show, r, figtype, figname, theme)