-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats_plotting.py
233 lines (167 loc) · 8.34 KB
/
stats_plotting.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
#!/usr/bin/env python3
import numpy as np
import pandas as pd
import logomaker
import plotly.graph_objects as go
import plotly.figure_factory as ff
import matplotlib.pyplot as plt
from typing import List
def common_type_gen(fragments_dataframe: pd.DataFrame) -> pd.Series:
common_type = fragments_dataframe["frag_type1"].astype(str).str.cat(fragments_dataframe["frag_type2"], sep = "-")
common_type = common_type.replace("n", "not annotated", regex = True)
common_type = common_type.replace("t-", "", regex = True)
common_type = common_type.replace("-t", "", regex = True)
fragments_dataframe["frag_types"] = common_type
return common_type
def common_type_hist(fragments_dataframe: pd.DataFrame) -> go.Figure:
common_type = common_type_gen(fragments_dataframe)
return go.Figure([go.Histogram(x = common_type)])
def common_type_pie(fragments_dataframe: pd.DataFrame) -> go.Figure:
common_type = common_type_gen(fragments_dataframe)
counts = common_type.value_counts()
return go.Figure([go.Pie(labels = counts.keys(), values = counts)])
def log_ion_intens_dist(fragments_dataframe: pd.DataFrame) -> go.Figure:
types = fragments_dataframe["frag_types"].unique()
histograms = list()
for t in types:
histograms.append(go.Histogram(x = np.log(fragments_dataframe[fragments_dataframe.frag_types == t].frag_intensity),
histnorm = "probability", name = t, nbinsx = 50))
fig = go.Figure(histograms)
fig.update_layout(barmode = "group",
title = "Histograms of logarithmic intensities per ion type",
xaxis_title = "log2(intensity)",
yaxis_title = "probability")
return fig
def rel_ion_intens_perc(fragments_dataframe: pd.DataFrame) -> go.Figure:
types = fragments_dataframe["frag_types"].unique()
histograms = list()
for t in types:
histograms.append(go.Histogram(x = fragments_dataframe[fragments_dataframe.frag_types == t].perc_of_total_intensity,
histnorm = "probability", name = t, nbinsx = 50))
fig = go.Figure(histograms)
fig.update_layout(barmode = "group",
title = "Histograms of relative intensities per ion type",
xaxis_title = "intensity",
yaxis_title = "probability")
return fig
def rel_ion_intens_prop(fragments_dataframe: pd.DataFrame) -> go.Figure:
types = fragments_dataframe["frag_types"].unique()
histograms = list()
for t in types:
histograms.append(go.Histogram(x = fragments_dataframe[fragments_dataframe.frag_types == t].prop_intensity_to_base_peak,
histnorm = "probability", name = t, nbinsx = 50))
fig = go.Figure(histograms)
fig.update_layout(barmode = "group",
title = "Histograms of relative intensities to base peak per ion type",
xaxis_title = "percentage per base peak",
yaxis_title = "probability")
return fig
def mz_dist_ion_type(fragments_dataframe: pd.DataFrame) -> go.Figure:
types = fragments_dataframe["frag_types"].unique()
histograms = list()
for t in types:
histograms.append(go.Histogram(x = fragments_dataframe[fragments_dataframe.frag_types == t].frag_mz,
histnorm = "probability", name = t, nbinsx = 50))
fig = go.Figure(histograms)
fig.update_layout(barmode = "group",
title = "Histograms of mz values per ion type",
xaxis_title = "mz",
yaxis_title = "probability")
return fig
def per_spec_ion_type(spectra_dataframe: pd.DataFrame) -> go.Figure:
types = ["internal", "terminal", "other"]
histograms = list()
for t in types:
histograms.append(go.Histogram(x = spectra_dataframe["perc_" + t],
histnorm = "probability", name = t, nbinsx = 50))
fig = go.Figure(histograms)
fig.update_layout(barmode = "group",
title = "Histograms of percentages of ion type per spectrum",
xaxis_title = "Percentage",
yaxis_title = "probability")
return fig
def per_spec_ion_intens(spectra_dataframe: pd.DataFrame) -> go.Figure:
types = ["internal", "terminal", "other"]
histograms = list()
for t in types:
histograms.append(go.Histogram(x = spectra_dataframe["total_int_" + t],
histnorm = "probability", name = t, nbinsx = 50))
fig = go.Figure(histograms)
fig.update_layout(barmode = "group",
title = "Histograms of percentages of ion type per spectrum",
xaxis_title = "Percentage",
yaxis_title = "probability")
return fig
def log_ion_intens_ridge(fragments_dataframe: pd.DataFrame) -> go.Figure:
types = fragments_dataframe["frag_types"].unique()
fig = go.Figure()
for t in types:
fig.add_trace(go.Violin(x = np.log(fragments_dataframe[fragments_dataframe.frag_types == t].frag_intensity), name = t))
fig.update_traces(orientation = "h", side = "positive", width = 3, points = False)
fig.update_layout(barmode = "group",
title = "Ridgelines of logarithmic intensities per ion type",
xaxis_title = "log2(intensity)",
yaxis_title = "probability")
return fig
def rel_ion_intens_ridge(fragments_dataframe: pd.DataFrame) -> go.Figure:
types = fragments_dataframe["frag_types"].unique()
fig = go.Figure()
for t in types:
fig.add_trace(go.Violin(x = fragments_dataframe[fragments_dataframe.frag_types == t].perc_of_total_intensity, name = t))
fig.update_traces(orientation = "h", side = "positive", width = 3, points = False)
fig.update_layout(barmode = "group",
title = "Ridgelines of relative intensities per ion type",
xaxis_title = "intensity",
yaxis_title = "probability")
return fig
def rel_ion_intens_prop_ridge(fragments_dataframe: pd.DataFrame) -> go.Figure:
types = fragments_dataframe["frag_types"].unique()
fig = go.Figure()
for t in types:
fig.add_trace(go.Violin(x = fragments_dataframe[fragments_dataframe.frag_types == t].prop_intensity_to_base_peak, name = t))
fig.update_traces(orientation = "h", side = "positive", width = 3, points = False)
fig.update_layout(barmode = "group",
title = "Ridgelines of relative intensities to base peak per ion type",
xaxis_title = "intensity",
yaxis_title = "probability")
return fig
def logo_of_fraction(spectra_dataframe: pd.DataFrame,
fragments_dataframe: pd.DataFrame,
topn: int = 3,
max_length: int = 10,
min_length: int = 10) -> plt.figure:
fig1, ax1 = plt.subplots(facecolor = "white", figsize = (8, 4))
if topn == 0:
df = fragments_dataframe.frag_seq
else:
df = spectra_dataframe.top1_internal_seq
if topn > 1:
df = pd.concat([df, spectra_dataframe.top2_internal_seq])
if topn > 2:
df = pd.concat([df, spectra_dataframe.top3_internal_seq])
# Finding the maximum length of the strings in the column
# Creating a new dataframe to store the results
df_frequency = pd.DataFrame()
# Iterating over each position up to the maximum length
tt = df[(df.str.len() <= max_length) & (df.str.len() >= min_length) ]
for i in range(0, max_length):
# Counting the frequency of each letter at position i
frequency = tt.str[i].value_counts()
# Adding the frequency to the new dataframe
df_frequency[i] = frequency/sum(frequency)
# Displaying the frequency dataframe
df_frequency = df_frequency.fillna(0)
# Transposing and plotting logos
ww_df = df_frequency.T
if ww_df.shape[0] == 0 or ww_df.shape[1] == 0:
return fig1
logomaker.Logo(ww_df,
ax = ax1,
color_scheme = "NajafabadiEtAl2017",
vpad = 0.1,
width = 0.8
)
ax1.set_xlabel("Position in subset")
ax1.set_ylabel("Amino acid frequency")
ax1.set_facecolor("white")
return fig1