-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstacked_bar_plot.py
294 lines (236 loc) · 8.51 KB
/
stacked_bar_plot.py
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python
'''Create stacked bar plot from data table is tsv format.
This script reads in a datatable in tab separated format and builds a
stacked bar plot output as a publication ready PDF.
Each column will be a separate bar on the x-axis and each
row value will be a separate stack in the bar along the y-axis.
Requires two column tab separated color table:
row1 label #FFFFFF
row2 label #FFF45E
row3 label #5EFFF3
row4 label #FF5EC5
row5 label #6A36A5
-------------------------------------------
Author :: Roth Conrad
Email :: [email protected]
GitHub :: https://github.com/rotheconrad
Date Created :: Jan 2024
License :: GNU GPLv3
Copyright 2024 Roth Conrad
All rights reserved
-------------------------------------------
'''
import argparse
import pandas as pd; import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from scipy import stats
import seaborn as sns
from statsmodels.stats.multitest import multipletests
def parse_colors(incolors):
colors = {}
with open(incolors, 'r') as file:
for line in file:
X = line.rstrip().split('\t')
label = X[0]
value = X[1]
colors[label] = value
return colors
def post_hoc_test(adf):
''' loops through individual rows and performs chi2 post hoc '''
pvals = []
bdf = adf.T
for name in bdf.columns:
xdf = bdf.drop(name, axis=1)
xdf['OTHERs'] = xdf.sum(axis=1)
xdf[name] = bdf[name]
ydf = xdf[['OTHERs', name]].T
c, p, d, x = stats.chi2_contingency(ydf, correction=True)
# create expected frequency table
extab = pd.DataFrame(x, index=ydf.index, columns=ydf.columns)
extab['Total'] = extab.sum(axis=1)
extab.loc['Total'] = extab.sum(axis=0)
# create post hoc test contingency table
ydf['Total'] = ydf.sum(axis=1)
ydf.loc['Total'] = ydf.sum(axis=0)
# print post hoc info
print(f'\nPost hoc Chi2 test contingency table for {name}:\n')
print(ydf)
print(f'\nChi2 expected frequency table:\n\n{extab}')
print(f'\nchi2 statistic: {c:.4f}, dof: {d}, chi2 pvalue: {p:.6f}')
pvals.append(p)
reject_list, pvals_corrected = multipletests(pvals, method='fdr_bh')[:2]
return pvals, pvals_corrected
def chi2_hypothesis_test(adf):
''' performs chi square and post hoc tests between annotation
categorgies for recombining vs non-recombining genes.
'''
# create and print contingency table
ctab = adf.copy(deep=True)
ctab['Total'] = ctab.sum(axis=1)
ctab.loc['Total'] = ctab.sum(axis=0)
#print(f'\nInitial Chi2 test contingency table:\n\n{ctab}')
# chi2 test on the full data
chi2, chip, dof, ex = stats.chi2_contingency(adf, correction=True)
# create expected frequency table
efreq = pd.DataFrame(ex, index=adf.index, columns=adf.columns)
efreq['Total'] = efreq.sum(axis=1)
efreq.loc['Total'] = efreq.sum(axis=0)
print(f'\nChi2 expected frequency table:\n\n{efreq}')
# print test statitic and p value
print(f'\nchi2 statistic: {chi2:.4f}, dof: {dof}, chi2 pvalue: {chip:.6f}')
# perform post hoc test on combinations if significant (< 0.05)
if chip < 0.05:
pvals, pvals_corrected = post_hoc_test(adf)
print('\nPost hoc p values:\n', pvals)
print('\nBenjamini/Hochberg corrected p values:\n', pvals_corrected)
else:
pvals_corrected = [1] * len(adf)
return chip, pvals_corrected
def plot_stacked_barplot(df, title, colors, outfile, W, H):
'''Plots annotation by group name'''
print('\nBuilding annotation plot and tests ...')
# categorical hypothesis testing with raw counts
chip, pvals_corrected = chi2_hypothesis_test(df)
# calculate the percents of total per category
total = df.sum().to_list()
ptots = [f'({round(i/sum(total) * 100, 2)}%)' for i in total]
adf = df.div(df.sum(axis=0), axis=1)
# initiate plot
fig, ax = plt.subplots(figsize=(W,H))
# plot data
ax = adf.T.plot.bar(stacked=True, ax=ax, color=colors, width=.7)
# set plot title
ax.set_title(title)
# change axis labels
ax.set_xlabel('')
ax.set_ylabel("Gene fraction", fontsize=12)
# set the axis parameters / style
ax.minorticks_on()
ax.tick_params(axis='both', labelsize=12)
ax.tick_params(axis='x', labelrotation=45)
ax.tick_params(axis='x', which='minor', bottom=False)
# set grid style
ax.yaxis.grid(
which="minor", color='#f0f0f0', linestyle='--', linewidth=.75
)
ax.yaxis.grid(
which="major", color='#d9d9d9', linestyle='--', linewidth=1
)
ax.set_axisbelow(True)
# annotate percent of total gene difference
for i in range(len(ptots)):
ax.annotate(ptots[i], (i, 1.02), transform=ax.transAxes, ha='center')
# annotate individual percents
for i, p in enumerate(ax.patches):
width, height = p.get_width(), p.get_height()
x, y = p.get_xy()
# don't print categories with 0 genes on the bar plot
if round(height, 2) == 0:
continue
line = f'{height:.2f}'
ax.text(x+width/2,
y+height/2,
line,
horizontalalignment='center',
verticalalignment='center')
# add asterisk if significant post hoc
# double the corrected p value array since our plot has two columns
# create legend to separate file
handles, labels = ax.get_legend_handles_labels()
new_labels = []
for i, l in enumerate(labels):
nl = f'* {l} *' if pvals_corrected[i] <= 0.05 else l
new_labels.append(nl)
ax.legend(
reversed(handles),
reversed(new_labels),
ncol=1,
loc='center left',
frameon=False,
fontsize=12,
bbox_to_anchor=(1, 0.5)
)
# adjust layout, save, and close
fig.set_tight_layout(True)
fig.savefig(outfile)
plt.close()
return True
###############################################################################
##### MAIN MAIN MAIN MAIN MAIN MAIN MAIN ##############################
###############################################################################
def main():
# Configure Argument Parser
parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter
)
parser.add_argument(
'-i', '--input_datatable_tsv',
help='Please specify the tsv formated data table.',
metavar='',
type=str,
required=True
)
parser.add_argument(
'-c', '--row_value_colors',
help='Please specify the tsv formated color table.',
metavar='',
type=str,
required=True
)
parser.add_argument(
'-o', '--output_file_name',
help='Please specify the output file name (Use .pdf).',
metavar='',
type=str,
required=True
)
parser.add_argument(
'-t', '--plot_title',
help='(OPTIONAL) Please specify the plot title (default: My Stacked Bar Plot).',
metavar='',
type=str,
nargs='+',
required=False,
default=["My", "Stacked", "Bar", "Plot"]
)
parser.add_argument(
'-x', '--figure_width',
help='(OPTIONAL) Specify the figure width in inches.',
metavar='',
type=float,
required=False,
default=None
)
parser.add_argument(
'-y', '--figure_height',
help='(OPTIONAL) Specify the figure height in inches.',
metavar='',
type=float,
required=False,
default=None
)
args=vars(parser.parse_args())
# define input params
infile = args['input_datatable_tsv']
outfile = args['output_file_name']
title = ' '.join(args['plot_title'])
incolors = args['row_value_colors']
W = args['figure_width']
H = args['figure_height']
# Do what you came here to do:
print('\n\nRunning Script ...')
# read in the data table
df = pd.read_csv(infile, sep='\t', index_col=0)
colors = parse_colors(incolors)
# define width and height based on number of columns and rows
if not W:
W = 2 * len(df.columns)
if not H:
H = 1 * len(df)
# build the plotty plot
_ = plot_stacked_barplot(df, title, colors, outfile, W, H)
print(f'\n\nComplete success space cadet!! Finished without errors.\n\n')
if __name__ == "__main__":
main()