-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnormalization_analysis.py
More file actions
272 lines (216 loc) · 7.41 KB
/
Copy pathnormalization_analysis.py
File metadata and controls
272 lines (216 loc) · 7.41 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
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
# ---
# jupyter:
# jupytext:
# cell_data_filter: tags,-all
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.19.3
# kernelspec:
# display_name: Python 3 (ipykernel)
# language: python
# name: python3
# ---
# %% [markdown]
# # Normalization of samples
#
# We will explore an Alzheimer dataset where the data was collected in four different sites.
# We will see that the sites have a an effect where the data is in principal component space
# and in UMAP space. We will then normalize the data and see how the effect on these plots.
#
# Refers to the [`acore.normalization`](acore.normalization) module.
# %% tags=["hide-output"]
# %pip install acore vuecore
# %% tags=["hide-input"]
from typing import Optional
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import sklearn
import sklearn.impute
import sklearn.preprocessing
import vuecore.decomposition
import acore.decomposition
import acore.normalization
def plot_umap(X_scaled, y, meta_column=None, random_state=42) -> plt.Axes:
"""Fit and plot UMAP embedding with two components with colors defined by meta_column."""
embedding = acore.decomposition.umap.run_umap(
X_scaled, y, random_state=random_state
)
if meta_column is None:
meta_column = y.name
ax = embedding.plot.scatter("UMAP 1", "UMAP 2", c=meta_column, cmap="Paired")
return ax
def standard_normalize(X: pd.DataFrame) -> pd.DataFrame:
"""Standard normalize data and keep indices of DataFrame."""
X_scaled = (
sklearn.preprocessing.StandardScaler()
.set_output(transform="pandas")
.fit_transform(X)
)
return X_scaled
def median_impute(X: pd.DataFrame) -> pd.DataFrame:
X_imputed = (
sklearn.impute.SimpleImputer(strategy="median")
.set_output(transform="pandas")
.fit_transform(X)
)
return X_imputed
def run_and_plot_pca(
X_scaled,
y,
meta_column: Optional[str] = None,
n_components: int = 4,
) -> tuple[pd.DataFrame, plt.Figure]:
PCs, _ = acore.decomposition.pca.run_pca(X_scaled, n_components=n_components)
PCs.columns = [s.replace("principal component", "PC") for s in PCs.columns]
fig = vuecore.decomposition.pca_grid(
PCs=PCs, meta_column=y, n_components=n_components, meta_col_name=meta_column
)
return PCs, fig
# %% [markdown]
#
# ## Set some parameters
# %% tags=["parameters"]
BASE = (
"https://raw.githubusercontent.com/Multiomics-Analytics-Group/acore/"
"main/example_data/alzheimer_proteomics/"
)
# data is already preprocessed: log2, filtered
fname: str = "alzheimer_example_omics_and_clinic.csv" # combined omics and meta data
covariates: list[str] = ["age", "male"]
group: str = "collection_site"
subject_col: str = "Sample ID"
drop_cols: list[str] = ["AD"]
factor_and_covars: list[str] = [group, *covariates]
group_label: Optional[str] = "site" # optional: rename target variable
# %% [markdown]
# ## Data loading
# Use combined dataset for ANCOVA analysis.
# %% tags=["hide-input"]
omics_and_meta = (
pd.read_csv(f"{BASE}/{fname}", index_col=subject_col)
.convert_dtypes()
.dropna(subset=factor_and_covars)
)
omics_and_meta
# %% [markdown]
# Metadata here is of type integer. All floats are proteomics measurements.
# %% tags=["hide-input"]
omics_and_meta.dtypes.value_counts()
# %% tags=["hide-input"]
omics_and_meta[factor_and_covars]
# %%
omics = omics_and_meta.drop(columns=[*factor_and_covars, *drop_cols])
y = omics_and_meta[group].astype("category").rename(group_label)
# %% [markdown]
# For simplicity we normalize here all samples together, but normally you would need to
# apply the normalization from you training data to the test data. So see these examples
# here as a way to do it for your training data.
# %% [markdown]
# ### Fill missing values for preliminary plots
# %% [markdown]
# Impute using median to impute (before scaling, which can be changed).
# %% tags=["hide-input"]
omics_imputed = median_impute(omics)
assert omics_imputed.isna().sum().sum() == 0
omics_imputed.shape
# %% [markdown]
# Explained variance by first four principal components in data.
# %% tags=["hide-input"]
PCs, pca = acore.decomposition.pca.run_pca(omics_imputed, n_components=4)
ax = vuecore.decomposition.plot_explained_variance(pca)
ax.locator_params(axis="x", integer=True)
# %% [markdown]
# ## Normalization of samples in a dataset
# We will use the `acore.normalization` module to normalize the data.
#
# We will do it for each of the data on the omics dataset which is log transformed,
# but not yet imputed and normalized. Then we will reapply standard
# normalization before replotting the PCA and UMAP plots. The execption is combat as it
# need complete data.
# %% tags=["hide-input"]
omics
# %% [markdown]
# ## Median normalization
# Substracts a constant from all features of a sample. All samples will have the same
# global median.
# %%
# %%time
X = acore.normalization.normalize_data(omics, "median")
X
# %% tags=["hide-input"]
omics_imp = median_impute(X)
omics_imp_scaled = standard_normalize(omics_imp)
PCs, fig = run_and_plot_pca(omics_imp_scaled, y, y.name, n_components=4)
ax = plot_umap(omics_imp_scaled, y)
# %% [markdown]
# See change by substracting median normalized data from original data.
# %% tags=["hide-input"]
omics - X
# %% [markdown]
# ## Z-score normalization
# Normalize a sample by it's mean and standard deviation.
# %%
# %%time
X = acore.normalization.normalize_data(omics, "zscore")
X
# %% tags=["hide-input"]
omics_imp = median_impute(X)
omics_imp_scaled = standard_normalize(omics_imp)
PCs, fig = run_and_plot_pca(omics_imp_scaled, y, n_components=4)
ax = plot_umap(omics_imp_scaled, y)
# %% [markdown]
# See change by substracting z-score normalized data from original data.
# %% tags=["hide-input"]
omics_imp_scaled - X
# %% [markdown]
# ## Median Polish Normalization
# - normalize iteratively features and samples to have zero median.
# %%
# %%time
X = acore.normalization.normalize_data(omics, "median_polish")
X
# %% tags=["hide-input"]
omics_imp = median_impute(X)
omics_imp_scaled = standard_normalize(omics_imp)
PCs, fig = run_and_plot_pca(omics_imp_scaled, y, n_components=4)
ax = plot_umap(omics_imp_scaled, y)
# %% [markdown]
# See change by substracting median polish normalized data from original data.
# %% tags=["hide-input"]
omics_imp_scaled - X
# %% [markdown]
# ## Quantile normalization
# quantile normalize each feature column.
# %%
# %%time
X = acore.normalization.normalize_data(omics, "quantile")
X
# %% tags=["hide-input"]
omics_imp = median_impute(X)
omics_imp_scaled = standard_normalize(omics_imp)
PCs, fig = run_and_plot_pca(omics_imp_scaled, y, n_components=4)
ax = plot_umap(omics_imp_scaled, y)
# %%
omics - X
# %% [markdown]
# ## Linear normalization
# %%
# %%time
X = acore.normalization.normalize_data(omics, "linear")
X
# %% tags=["hide-input"]
omics_imp = median_impute(X)
omics_imp_scaled = standard_normalize(omics_imp)
PCs, fig = run_and_plot_pca(omics_imp_scaled, y, n_components=4)
ax = plot_umap(omics_imp_scaled, y)
# %% tags=["hide-input"]
omics - X
# %% [markdown]
# ## Summmary
# Besides the median polish normalization, the structure of the data is not changed
# too much by the normalization using this Alzheimer example. This notebook can be opened
# on colab and might be a good starting point for investigating the effect of normalization
# on your data - or to disect some approaches further.