-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFigure_1.py
152 lines (112 loc) · 5.39 KB
/
Figure_1.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 30 13:07:43 2022
@author: scottrk
"""
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.patches as Patch
from GlobalVars_ import color_cycle, tissue_type, tissue_type_long, tissue_type_abbrev
from HelperFuncs_ import p_val_convert
from compile_data import melt_summary, summary_import
import os
from compute_stats import heatmap_stats
def setup_figure():
mosaic = """AA
BC"""
mosaic2 = """AA
BC"""
fig = plt.figure(constrained_layout=True, figsize=(15, 8))
left, right = fig.subfigures(nrows=1, ncols=2, wspace=0.08)
axd = left.subplot_mosaic(mosaic, gridspec_kw=dict(hspace=0.1))
axd2 = right.subplot_mosaic(mosaic2, gridspec_kw=dict(hspace=0.1))
return fig, axd, axd2
def mutation_bar(y, data, ax, alpha=1):
fc = color_cycle*2
sns.barplot(x="Tissue", y=y, hue="Age", data=data, order=tissue_type_abbrev[: -1],
ci='sd', edgecolor='black', lw=1.2, errwidth=1.5,
capsize=0.1, errcolor='black', ax=ax)
ax.spines['left'].set_linewidth(2)
ax.spines['bottom'].set_linewidth(2)
sns.stripplot(x="Tissue", y=y, hue="Age", data=data, order=tissue_type_abbrev[:-1],
dodge=True, ax=ax, alpha=0.7, color='black')
sns.despine(ax=ax)
for i in range(16):
ax.patches[i].set_facecolor(fc[i])
ax.patches[i].set_edgecolor('black')
if i < 8:
r, g, b, a = ax.patches[i].get_facecolor()
ax.patches[i].set_facecolor((r, g, b, .15))
ax.patches[i].set_edgecolor((r, g, b, 1))
ax.patches[i].set(lw=2.4)
plt.setp(ax.get_yaxis().get_offset_text(), visible=False)
ax.set_ylabel('Mutation Frequency($\mathregular{10^{-6}}$)', fontsize='xx-large')
ax.set_xlabel('', fontsize='xx-large')
ax.tick_params('y', labelsize='xx-large')
ax.tick_params('x', labelsize='x-large')
ax.set_xticklabels(tissue_type_long, rotation=45, fontdict={'horizontalalignment': 'center'})
legend = [Patch.Patch(facecolor='lightgrey', edgecolor='black', label='Young'),
Patch.Patch(facecolor='dimgrey', edgecolor='black', label='Old')]
ax.legend(handles=legend, fontsize='x-large')
return ax
def mod_heatmap(data, labels, ax):
plot = sns.heatmap(data, square=True, cbar=False, cmap='rocket_r',
linewidths=.5, linecolor='black', vmin=0, vmax=10,
xticklabels=labels, yticklabels=labels, ax=ax)
sns.despine(left=True, bottom=True, ax=plot)
plot.set_facecolor('silver')
plot.tick_params(labelsize=20, rotation=0)
return plot
if __name__ == '__main__':
if not os.path.isfile("data/imported_data/summary_data_tidy.csv"):
if not os.path.isdir("data/imported_data/"):
os.mkdir("data/imported_data")
data = melt_summary(summary_import("data/Mouse_aging_mtDNA_summary.csv"))
data.to_csv("data/imported_data/summary_data_tidy.csv")
else:
data = pd.read_csv("data/imported_data/summary_data_tidy.csv")
fig, axd, axd2 = setup_figure()
mutation_bar("Frequency", data.query("Treatment=='NT' & Class=='Total_SNV_Freq'"), axd['A'])
mutation_bar("Frequency", data.query("Treatment=='NT' & Class=='Total_InDel_Freq'"), axd2['A'])
if not os.path.isfile("data/stats/Figure_1A_Young_stats.csv"):
if not os.path.isdir("data/stats/"):
os.mkdir("data/stats/")
young_SNV = heatmap_stats(data, 'Young', 'Total_SNV_Freq', "data/stats/Figure_1A_Young_stats.csv")
else:
young_SNV = pd.read_csv("data/stats/Figure_1A_Young_stats.csv",
index_col=0)
if not os.path.isfile("data/stats/Figure_1A_Old_stats.csv"):
if not os.path.isdir("data/stats/"):
os.mkdir("data/stats/")
old_SNV = heatmap_stats(data, 'Old', 'Total_SNV_Freq')
else:
old_SNV = pd.read_csv("data/stats/Figure_1A_Old_stats.csv",
index_col=0)
if not os.path.isfile("data/stats/Figure_1D_Young_stats.csv"):
if not os.path.isdir("data/stats/"):
os.mkdir("data/stats/")
young_InDel = heatmap_stats(data, 'Young', 'Total_InDel_Freq')
else:
young_InDel = pd.read_csv("data/stats/Figure_1D_Young_stats.csv",
index_col=0)
if not os.path.isfile("data/stats/Figure_1D_Old_stats.csv"):
if not os.path.isdir("data/stats/"):
os.mkdir("data/stats/")
old_InDel = heatmap_stats(data, 'Old', 'Total_InDel_Freq')
else:
old_InDel = pd.read_csv("data/stats/Figure_1D_Old_stats.csv",
index_col=0)
mod_heatmap(p_val_convert(young_SNV), tissue_type_abbrev[:-1], axd['B'])
mod_heatmap(p_val_convert(old_SNV), tissue_type_abbrev[:-1], axd['C'])
mod_heatmap(p_val_convert(young_InDel), tissue_type_abbrev[:-1], axd2['B'])
mod_heatmap(p_val_convert(old_InDel), tissue_type_abbrev[:-1], axd2['C'])
for i, j in enumerate(['B', 'C']):
title = ['Young', 'Old']
axd[j].set_title(title[i], fontsize=23)
axd2[j].set_title(title[i], fontsize=23)
if not os.path.isdir("figures/"):
os.mkdir("figures")
fig.savefig('figures/Figure_1.png', dpi=600, facecolor='white')
fig.savefig('figures/Figure_1.pdf', dpi=600, facecolor='white')