Skip to content

Commit 17c0b19

Browse files
committed
allow colors for nmr, adjust legends
1 parent cc3ba5b commit 17c0b19

File tree

2 files changed

+64
-211
lines changed

2 files changed

+64
-211
lines changed

code/04_GenerateFigures.py

Lines changed: 28 additions & 179 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
'Outcome'])
3333
p180_network = plt.imread(savepath +
3434
'CytoscapeInput-edges-brown-blue-yellow.png')
35-
#nmr_network = plt.imread(savepath +
36-
# 'CytoscapeInput-edges-green-turquoise.txt.png')
35+
nmr_network = plt.imread(savepath +
36+
'CytoscapeInput-edges-brown-turquoise.png')
3737

3838

3939
#### Figure 1 : PLS-DA ####
@@ -161,194 +161,43 @@
161161

162162

163163
#### FIGURE 4: NMR platform ####
164+
## Set Figure
164165
fig = plt.figure(figsize = (12,12))
165-
fig.suptitle('Sex differences in NMR platform', fontsize=18)
166-
ax1 = fig.add_subplot(221)
167-
ax2 = fig.add_subplot(222)
168-
ax3 = fig.add_subplot(223)
169-
ax4 = fig.add_subplot(224)
166+
fig.suptitle('Sex differences in metabolite modules\n(NMR platform)',
167+
fontsize=18)
168+
gs = gridspec.GridSpec(2, 2, wspace=space)
169+
axs = []
170+
axs.append(fig.add_subplot(gs[0:,0]))
171+
for row in range(2):
172+
axs.append(fig.add_subplot(gs[row,1]))
170173

171-
# AX1: forest plot
174+
## Forest Plot ##
172175
plots.female_male_forest(results_nmr_modules,
173176
color_pastel,
174-
ax1)
175-
176-
# AX2: network
177-
ax2.imshow(nmr_network,
178-
aspect='auto')
179-
ax2.axis('off')
180-
ax2.set_title('Green and turquoise metabolite modules')
177+
axs[0])
181178

182-
# AX3: scatter plot Component 1
183-
plots.female_male_scatter(results_nmr,
184-
['green', 'turquoise'],
185-
'Component 1',
186-
ax3)
179+
## Network image ##
180+
axs[1].imshow(nmr_network)
181+
axs[1].axis('off')
182+
axs[1].set_title('Brown and turquoise\nmetabolite modules')
187183

188-
# AX4: scatter plot Component 2
184+
## Scatter Plot Single Metabolites ##
189185
plots.female_male_scatter(results_nmr,
190-
['green', 'turquoise'],
191-
'Component 2',
192-
ax4)
193-
# Annotate labels
194-
axes = [ax1,
195-
ax2,
196-
ax3,
197-
ax4]
198-
for i, label in enumerate(('A', 'B', 'C', 'D')):
199-
axes[i].text(-0.05, 1.1,
200-
label,
201-
transform=axes[i].transAxes,
202-
fontsize=16,
203-
fontweight='bold',
204-
va='top',
205-
ha='right')
206-
207-
for ext in file_extensions:
208-
filename = savepath +\
209-
'Figure4' +\
210-
ext
211-
plt.savefig(filename,
212-
dpi=300)
213-
214-
#### SUPP FIG 2 ####
215-
# PLS-DA scatter plots #
216-
217-
def score_plot(ax,
218-
percents:list,
219-
components:list=['Component 1', 'Component 2']):
220-
'''
221-
Generate a scatter plot from PLS-DA scores
222-
223-
Parameters
224-
----------
225-
components: list of str
226-
Name of Components to plot. Must match score column names
227-
percents: list of str
228-
Percentages to use for axis names
229-
ax: ax
230-
Matplotlib ax to use
231-
'''
232-
font_ax_title = 16
233-
font_axis = 12
234-
ax.set_xlabel(components[0] + ' ' + percents[0],
235-
fontsize = font_axis)
236-
ax.set_ylabel(components[1] + ' ' + percents[1],
237-
fontsize = font_axis)
238-
ax.set_title('Scores',
239-
fontsize = font_ax_title)
240-
targets = ['CN',
241-
'MCI',
242-
'AD']
243-
c = 0
244-
for target in targets:
245-
indicesToKeep = qtpad.data['DX.bl'] == target
246-
ax.scatter(qtpad.scores.loc[indicesToKeep,
247-
components[0]],
248-
qtpad.scores.loc[indicesToKeep,
249-
components[1]],
250-
color = color_pastel(c),
251-
s = 50,
252-
alpha = 0.6)
253-
c = c + 1
254-
ax.legend(targets)
255-
256-
def weight_plot(ax,
257-
percents:list,
258-
weights:list=['Weight 1', 'Weight 2'],
259-
offset:dict=None):
260-
'''
261-
Plot the weights of phenotypes and diagnosis
262-
263-
Parameters
264-
----------
265-
components: list of str
266-
Name of Components to plot. Must match score column names
267-
percents: list of str
268-
Percentages to use for axis names
269-
ax: ax
270-
Matplotlib ax to use
271-
offset: dict
272-
Phenotype offset dict to change annotation possition.
273-
E.g. {'Ventricles': (-10, 10)}
274-
'''
275-
font_ax_title = 16
276-
font_axis = 12
277-
components = [str(l) for i in weights for l in i.split() if l.isdigit()]
278-
ax.set_xlabel('Component' + components[0] + ' ' + percents[0],
279-
fontsize = font_axis)
280-
ax.set_ylabel('Component' + components[1] + ' ' + percents[1],
281-
fontsize = font_axis)
282-
ax.set_title('Weights',
283-
fontsize = font_ax_title)
284-
ax.scatter(qtpad.x_weights[weights[0]],
285-
qtpad.x_weights[weights[1]])
286-
for segment in qtpad.x_weights.index:
287-
if offset is not None:
288-
if segment in offset.keys():
289-
xy_offset = offset[segment]
290-
else:
291-
xy_offset = (-55, 4)
292-
ax.annotate(segment,
293-
xy=(qtpad.x_weights.loc[segment,
294-
weights[0]],
295-
qtpad.x_weights.loc[segment,
296-
weights[1]]),
297-
xytext=xy_offset,
298-
textcoords='offset points')
299-
ax.scatter(qtpad.y_weights[weights[0]],
300-
qtpad.y_weights[weights[1]])
301-
for group in qtpad.y_weights.index:
302-
xy_offset = (-10, 5)
303-
ax.annotate(group,
304-
xy=(qtpad.y_weights.loc[group,
305-
weights[0]],
306-
qtpad.y_weights.loc[group,
307-
weights[1]]),
308-
xytext=xy_offset,
309-
textcoords='offset points')
310-
311-
312-
fig = plt.figure(figsize = (12,12))
313-
fig.tight_layout(h_pad=10)
314-
fig.suptitle('PLS-DA Scores', fontsize=18)
315-
316-
ax1 = fig.add_subplot(2,2,1)
317-
ax2 = fig.add_subplot(2,2,2)
318-
ax3 = fig.add_subplot(2,2,3)
319-
ax4 = fig.add_subplot(2,2,4)
320-
percents = []
321-
for i in range(2, len(qtpad.x_variance_explained)):
322-
per = '(' + \
323-
str(round(qtpad.x_variance_explained[i] * 100)) + \
324-
'%)'
325-
percents.append(per)
326-
327-
#### AX 1 ####
328-
score_plot(ax1,
329-
percents[:2],
330-
['Component 3', 'Component 4'])
331-
332-
#### AX 2 ####
333-
weight_plot(ax2,
334-
percents[:2],
335-
['Weight 3', 'Weight 4'],
336-
{'Ventricles': (-10, 10),
337-
'Entorhinal': (-40, 10)})
338-
339-
#### AX 3 ####
340-
score_plot(ax3,
341-
percents[1:],
342-
['Component 4', 'Component 5'])
186+
axs[2],
187+
'All')
343188

344-
#### AX 4 ####
345-
weight_plot(ax4,
346-
percents[1:],
347-
['Weight 4', 'Weight 5'])
189+
for i, label in enumerate(('A', 'B', 'C')):
190+
axs[i].text(-0.05, 1.1,
191+
label,
192+
transform=axs[i].transAxes,
193+
fontsize=16,
194+
fontweight='bold',
195+
va='top',
196+
ha='right')
348197

349198
for ext in file_extensions:
350199
filename = savepath +\
351-
'FigureS2' +\
200+
'Figure4' +\
352201
ext
353202
plt.savefig(filename,
354203
dpi=300)

code/plots.py

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def female_male_scatter(results,
2626
modules: list of str, str, or None
2727
module colors to highlight
2828
'''
29-
color_pastel = cm.get_cmap('Set2')
29+
color_set1 = cm.get_cmap('Set1')
3030
axis_names = ['Effect in females',
3131
'Effect in males']
3232
if component == 'All':
@@ -46,19 +46,30 @@ def female_male_scatter(results,
4646
# Divide sex_different by group of metabolites
4747
meta_names = dat.index.get_level_values('Variable')
4848

49-
PCs = meta_names.str.match(pat = 'PC.a[ae]') & sex_different
50-
lysoPCs = meta_names.str.match(pat = 'lysoPC') & sex_different
51-
acyl = meta_names.str.match(pat = 'C[0-9]') & sex_different
52-
sphyn = meta_names.str.match(pat = 'SM.') & sex_different
53-
amino = meta_names.str.match(pat = '[A-Z][a-z]{2}$') & sex_different
54-
amines = ~(amino + PCs + lysoPCs + acyl + sphyn) & sex_different
49+
if 'Class' in results:
50+
group_names = list(dat['Class'].unique())
51+
group_bools = []
52+
for n in group_names:
53+
p = (dat['Class'] == n) & sex_different
54+
if p.sum() == 0:
55+
group_names.remove(n)
56+
else:
57+
group_bools.append(p)
58+
else:
59+
PCs = meta_names.str.match(pat = 'PC.a[ae]') & sex_different
60+
lysoPCs = meta_names.str.match(pat = 'lysoPC') & sex_different
61+
acyl = meta_names.str.match(pat = 'C[0-9]') & sex_different
62+
sphyn = meta_names.str.match(pat = 'SM.') & sex_different
63+
amino = meta_names.str.match(pat = '[A-Z][a-z]{2}$') & sex_different
64+
amines = ~(amino + PCs + lysoPCs + acyl + sphyn) & sex_different
5565

56-
group_names = ['PCs',
57-
'lysoPC',
58-
'SMs',
59-
'Acylcarnitines',
60-
'Amino acids',
61-
'Biogenic amines']
66+
group_bools = [PCs, lysoPCs, sphyn, acyl, amino, amines]
67+
group_names = ['PCs',
68+
'lysoPC',
69+
'SMs',
70+
'Acylcarnitines',
71+
'Amino acids',
72+
'Biogenic amines']
6273

6374
modules_to_highlight = []
6475
mod_colors = []
@@ -72,22 +83,10 @@ def female_male_scatter(results,
7283
modules_to_highlight.append(temp_mod)
7384
mod_colors = modules
7485

75-
groups = [everything_else,
76-
PCs,
77-
lysoPCs,
78-
sphyn,
79-
acyl,
80-
amino,
81-
amines]
86+
groups = [everything_else]
87+
groups.extend(group_bools)
8288
groups.extend(modules_to_highlight)
8389

84-
colors = []
85-
for i in range(len(groups)):
86-
c = color_pastel.reversed()(i)
87-
colors.append(c)
88-
89-
colors = colors + mod_colors
90-
9190
max_value = max(abs(dat.loc[:,['Beta_female',
9291
'Beta_male'] ]).max())
9392
beta_female = dat['Beta_female']
@@ -102,19 +101,21 @@ def female_male_scatter(results,
102101
log = list(log)
103102
if i == 0:
104103
al = 0.2
104+
color = 'silver'
105105
else:
106-
al = 0.8
106+
al = 0.7
107+
color = color_set1(i-1)
107108
p = mlines.Line2D([], [],
108-
color=colors[i],
109+
color=color,
109110
marker='o',
110111
label=group_names[i-1],
111-
ms=10,
112+
ms=8,
112113
ls='')
113114
patches.append(p)
114115
ax.scatter(beta_female[log],
115116
beta_male[log],
116117
s=pvalue_diff[log],
117-
color=colors[i],
118+
color=color,
118119
alpha=al)
119120
ax.set_xlabel(axis_names[0], fontsize = 12)
120121
ax.set_ylabel(axis_names[1], fontsize = 12)
@@ -123,7 +124,10 @@ def female_male_scatter(results,
123124
ax.set_ylim(min_max)
124125
ax.axhline(y=0, color='k')
125126
ax.axvline(x=0, color='k')
126-
ax.legend(handles=patches)
127+
ax.legend(handles=patches,
128+
ncol=2,
129+
frameon=False,
130+
prop={'size': 8})
127131

128132
def female_male_forest(results,
129133
colors,

0 commit comments

Comments
 (0)