Repository navigation
Expand file tree
/
Copy pathgprofiler_plotting.py
More file actions
113 lines (84 loc) · 4.15 KB
/
Copy pathgprofiler_plotting.py
File metadata and controls
113 lines (84 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# Plotting functions - 'GProfiler-official version'
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb
from matplotlib import colors
from matplotlib import rcParams
def scale_data_5_75(data):
mind = np.min(data)
maxd = np.max(data)
if maxd == mind:
maxd=maxd+1
mind=mind-1
drange = maxd - mind
return ((((data - mind)/drange*0.70)+0.05)*100)
def plot_enrich(data, n_terms=20, save=False):
# Test data input
if not isinstance(data, pd.DataFrame):
raise ValueError('Please input a Pandas Dataframe output by gprofiler.')
if not np.all([term in data.columns for term in ['p_value', 'name', 'intersection_size']]):
raise TypeError('The data frame {} does not contain enrichment results from gprofiler.'.format(data))
data_to_plot = data.iloc[:n_terms,:].copy()
data_to_plot['go.id'] = data_to_plot.index
min_pval = data_to_plot['p_value'].min()
max_pval = data_to_plot['p_value'].max()
# Scale intersection_size to be between 5 and 75 for plotting
#Note: this is done as calibration was done for values between 5 and 75
data_to_plot['scaled.overlap'] = scale_data_5_75(data_to_plot['intersection_size'])
norm = colors.LogNorm(min_pval, max_pval)
sm = plt.cm.ScalarMappable(cmap="cool", norm=norm)
sm.set_array([])
rcParams.update({'font.size': 14, 'font.weight': 'bold'})
sb.set(style="whitegrid")
path = plt.scatter(x='recall', y="name", c='p_value', cmap='cool',
norm=colors.LogNorm(min_pval, max_pval),
data=data_to_plot, linewidth=1, edgecolor="grey",
s=[(i+10)**1.5 for i in data_to_plot['scaled.overlap']])
ax = plt.gca()
ax.invert_yaxis()
ax.set_ylabel('')
ax.set_xlabel('Gene ratio', fontsize=14, fontweight='bold')
ax.xaxis.grid(False)
ax.yaxis.grid(True)
# Shrink current axis by 20%
box = ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
# Get tick marks for this plot
#Note: 6 ticks maximum
min_tick = np.floor(np.log10(min_pval)).astype(int)
max_tick = np.ceil(np.log10(max_pval)).astype(int)
tick_step = np.ceil((max_tick - min_tick)/6).astype(int)
# Ensure no 0 values
if tick_step == 0:
tick_step = 1
min_tick = max_tick-1
ticks_vals = [10**i for i in range(max_tick, min_tick-1, -tick_step)]
ticks_labs = ['$10^{'+str(i)+'}$' for i in range(max_tick, min_tick-1, -tick_step)]
#Colorbar
fig = plt.gcf()
cbaxes = fig.add_axes([0.8, 0.15, 0.03, 0.4])
cbar = ax.figure.colorbar(sm, ticks=ticks_vals, shrink=0.5, anchor=(0,0.1), cax=cbaxes)
cbar.ax.set_yticklabels(ticks_labs)
cbar.set_label("Adjusted p-value", fontsize=14, fontweight='bold')
#Size legend
min_olap = data_to_plot['intersection_size'].min()
max_olap = data_to_plot['intersection_size'].max()
olap_range = max_olap - min_olap
#Note: approximate scaled 5, 25, 50, 75 values are calculated
# and then rounded to nearest number divisible by 5
size_leg_vals = [np.round(i/5)*5 for i in
[min_olap, min_olap+(20/70)*olap_range, min_olap+(45/70)*olap_range, max_olap]]
size_leg_scaled_vals = scale_data_5_75(size_leg_vals)
l1 = plt.scatter([],[], s=(size_leg_scaled_vals[0]+10)**1.5, edgecolors='none', color='black')
l2 = plt.scatter([],[], s=(size_leg_scaled_vals[1]+10)**1.5, edgecolors='none', color='black')
l3 = plt.scatter([],[], s=(size_leg_scaled_vals[2]+10)**1.5, edgecolors='none', color='black')
l4 = plt.scatter([],[], s=(size_leg_scaled_vals[3]+10)**1.5, edgecolors='none', color='black')
labels = [str(int(i)) for i in size_leg_vals]
leg = plt.legend([l1, l2, l3, l4], labels, ncol=1, frameon=False, fontsize=12,
handlelength=1, loc = 'center left', borderpad = 1, labelspacing = 1.4,
handletextpad=2, title='Gene overlap', scatterpoints = 1, bbox_to_anchor=(-2, 1.5),
facecolor='black')
if save:
plt.savefig(save, dpi=300, format='pdf')
plt.show()