-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpca_analysis.py
414 lines (305 loc) · 12.5 KB
/
pca_analysis.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import os
from collections import defaultdict
from typing import List, Tuple
import pandas as pd
from matplotlib import pyplot as plt
from sklearn.decomposition import PCA
from sklearn.feature_selection import VarianceThreshold
from sklearn.preprocessing import StandardScaler
from utils import (ModelAnalysis, parse_organism_id, read_models, parse_reaction, parse_metabolite,
parse_organism_annotation, parse_template_annotation, parse_method_annotation, COLORS,
get_explained_variance_idx)
def models_dataframe(models: List[ModelAnalysis],
reactions: bool = True,
filter_boundaries: bool = True) -> Tuple[pd.DataFrame, Tuple[str]]:
features_lookup = defaultdict(list)
categorical = defaultdict(list)
index = []
for model_analysis in models:
index.append(model_analysis.model_id)
categorical['organism_id'].append(model_analysis.organism_id)
categorical['organism'].append(model_analysis.organism)
categorical['template'].append(model_analysis.template)
categorical['method'].append(model_analysis.method)
if reactions:
for rxn in model_analysis.model.reactions:
rxn_id = parse_reaction(rxn, filter_boundaries)
if rxn_id is not None:
features_lookup[rxn_id].append(model_analysis.model_id)
else:
for met in model_analysis.model.metabolites:
met_id = parse_metabolite(met, filter_boundaries)
if met_id is not None:
features_lookup[met_id].append(model_analysis.model_id)
data = [[0] * len(features_lookup)] * len(index)
df = pd.DataFrame(data=data,
index=index,
columns=features_lookup.keys())
for rxn_or_met, models_lkp in features_lookup.items():
for model_id in models_lkp:
df.loc[model_id, rxn_or_met] = 1
categorical_df = pd.DataFrame.from_dict(categorical)
categorical_df.index = index
df = pd.concat([df, categorical_df], axis=1)
return df, tuple(categorical.keys())
def cog_dataframe(file_path: str) -> Tuple[pd.DataFrame, Tuple[str]]:
df = pd.read_csv(file_path,
sep='\t',
index_col='organism')
df.loc[:, 'organism_id'] = [parse_organism_id(organism) for organism in df.index]
return df, ('domain', 'phylum', 'organism_id')
def models_genes_cog_dataframe(file_path: str) -> pd.DataFrame:
df = pd.read_csv(file_path,
sep='\t',
index_col='model_id')
organisms = []
organisms_id = []
templates = []
methods = []
for model_id in df.index:
model_annotation = model_id.split('_')
organism = parse_organism_annotation(model_annotation)
organism_id = parse_organism_id(organism)
method = parse_method_annotation(model_annotation)
template, random_id = parse_template_annotation(model_annotation)
if template == 'random':
organism_id = f'{organism_id}{random_id}'
organisms.append(organism)
organisms_id.append(organism_id)
templates.append(template)
methods.append(method)
df.loc[:, 'organism_id'] = organisms_id
df.loc[:, 'organism'] = organisms
df.loc[:, 'template'] = templates
df.loc[:, 'method'] = methods
return df, ('organism_id', 'organism', 'template', 'method')
def scaling(dataframe: pd.DataFrame,
categorical: Tuple[str],
standard: bool = True,
variance: bool = True) -> pd.DataFrame:
mask = dataframe.columns.isin(categorical)
cols = dataframe.columns[~mask]
x = dataframe.loc[:, cols]
y = dataframe.loc[:, categorical]
if variance:
scalier = VarianceThreshold()
scaled = scalier.fit(x)
x = x.iloc[:, scaled.get_support(indices=True)]
if standard:
scalier = StandardScaler()
scaled = scalier.fit_transform(x.T)
x = pd.DataFrame(scaled, columns=x.index, index=x.columns)
x = x.T
return pd.concat([x, y], axis=1)
def method_filter(dataframe: pd.DataFrame, label: str):
mask = dataframe.loc[:, 'method'] != label
return dataframe.loc[mask, :]
def pca_analysis(dataframe: pd.DataFrame,
categorical: Tuple[str],
components: int = 2) -> Tuple[pd.DataFrame, PCA]:
mask = dataframe.columns.isin(categorical)
cols = dataframe.columns[~mask]
x = dataframe.loc[:, cols]
y = dataframe.loc[:, categorical]
pca = PCA(n_components=components)
pc = pca.fit_transform(x)
columns = [f'PC {i + 1}' for i in range(components)]
df = pd.DataFrame(data=pc, index=dataframe.index, columns=columns)
df = pd.concat([df, y], axis=1)
return df, pca
def plot_pca(workdir: str,
dataframe: pd.DataFrame,
pca: PCA,
c1: str,
c2: str,
title: str,
factor: str):
explained_variance_1 = get_explained_variance_idx(c1)
explained_variance_2 = get_explained_variance_idx(c2)
fig = plt.figure(figsize=(8, 8))
ax = fig.add_subplot(1, 1, 1)
ax.set_title(title, fontsize=20)
x_label = f'{c1} ({round(pca.explained_variance_ratio_[explained_variance_1] * 100, 2)} %)'
y_label = f'{c2} ({round(pca.explained_variance_ratio_[explained_variance_2] * 100, 2)} %)'
ax.set_xlabel(x_label, fontsize=15)
ax.set_ylabel(y_label, fontsize=15)
labels = set(dataframe.loc[:, factor])
for label, color in zip(labels, COLORS):
mask = dataframe.loc[:, factor] == label
pc1 = dataframe.loc[mask, c1]
pc2 = dataframe.loc[mask, c2]
ax.scatter(pc1,
pc2,
c=color,
s=60)
organisms_id = dataframe.loc[mask, 'organism_id']
for pc1_pt, pc2_pt, annotation in zip(pc1, pc2, organisms_id):
ax.annotate(annotation, (pc1_pt + 1, pc2_pt - 1))
legend = ax.legend(labels, loc=(1.04, 0))
ax.grid()
file_name = f'{title}_{factor}_{c1}_{c2}.png'
file_path = os.path.join(workdir, file_name)
fig.savefig(fname=file_path, bbox_extra_artists=(legend,), bbox_inches='tight', dpi=300)
# ----------------------------------
# ANALYSIS RUN
# ----------------------------------
def organisms_analysis(cog_file: str,
analysis_dir: str):
df, categorical = cog_dataframe(cog_file)
df = scaling(dataframe=df, categorical=categorical)
pca_df, pca = pca_analysis(dataframe=df, categorical=categorical, components=3)
if not os.path.exists(analysis_dir):
os.makedirs(analysis_dir)
plot_pca(workdir=analysis_dir,
dataframe=pca_df,
pca=pca,
c1='PC 1',
c2='PC 2',
title='Metabolic COG Analysis',
factor='domain')
plot_pca(workdir=analysis_dir,
dataframe=pca_df,
pca=pca,
c1='PC 1',
c2='PC 3',
title='Metabolic COG Analysis',
factor='domain')
plot_pca(workdir=analysis_dir,
dataframe=pca_df,
pca=pca,
c1='PC 1',
c2='PC 2',
title='Metabolic COG Analysis',
factor='phylum')
plot_pca(workdir=analysis_dir,
dataframe=pca_df,
pca=pca,
c1='PC 1',
c2='PC 3',
title='Metabolic COG Analysis',
factor='phylum')
def reactions_analysis(models_dir: str,
analysis_dir: str,
filter_boundaries: bool,
method: str = 'permissive',
read: str = '',
write: str = ''):
if read:
df = pd.read_csv(read,
sep='\t',
index_col='model_id')
categorical = ('organism_id', 'organism', 'template', 'method')
else:
models_analysis = read_models(models_dir)
df, categorical = models_dataframe(models_analysis, reactions=True, filter_boundaries=filter_boundaries)
if write:
df.to_csv(write, sep='\t', index=True, index_label='model_id')
df = method_filter(df, method)
df = scaling(dataframe=df, categorical=categorical)
pca_df, pca = pca_analysis(dataframe=df, categorical=categorical, components=3)
if not os.path.exists(analysis_dir):
os.makedirs(analysis_dir)
if method == 'permissive':
title = 'Reactions restrictive analysis'
else:
title = 'Reactions permissive analysis'
plot_pca(workdir=analysis_dir,
dataframe=pca_df,
pca=pca,
c1='PC 1',
c2='PC 2',
title=title,
factor='template')
plot_pca(workdir=analysis_dir,
dataframe=pca_df,
pca=pca,
c1='PC 1',
c2='PC 3',
title=title,
factor='template')
def metabolites_analysis(models_dir: str,
analysis_dir: str,
filter_boundaries: bool,
method: str = 'permissive',
read: str = '',
write: str = ''):
if read:
df = pd.read_csv(read,
sep='\t',
index_col='model_id')
categorical = ('organism_id', 'organism', 'template', 'method')
else:
models_analysis = read_models(models_dir)
df, categorical = models_dataframe(models_analysis, reactions=False, filter_boundaries=filter_boundaries)
if write:
df.to_csv(write, sep='\t', index=True, index_label='model_id')
df = method_filter(df, method)
df = scaling(dataframe=df, categorical=categorical)
pca_df, pca = pca_analysis(dataframe=df, categorical=categorical, components=3)
if not os.path.exists(analysis_dir):
os.makedirs(analysis_dir)
if method == 'permissive':
title = 'Metabolites restrictive analysis'
else:
title = 'Metabolites permissive analysis'
plot_pca(workdir=analysis_dir,
dataframe=pca_df,
pca=pca,
c1='PC 1',
c2='PC 2',
title=title,
factor='template')
plot_pca(workdir=analysis_dir,
dataframe=pca_df,
pca=pca,
c1='PC 1',
c2='PC 3',
title=title,
factor='template')
def genes_analysis(cog_file: str, analysis_dir: str, method: str = 'permissive'):
df, categorical = models_genes_cog_dataframe(cog_file)
df = method_filter(df, method)
df = scaling(df, categorical=categorical)
pca_df, pca = pca_analysis(df, categorical=categorical, components=3)
if not os.path.exists(analysis_dir):
os.makedirs(analysis_dir)
if method == 'permissive':
title = 'Genes COG restrictive analysis'
else:
title = 'Genes COG permissive analysis'
plot_pca(workdir=analysis_dir,
dataframe=pca_df,
pca=pca,
c1='PC 1',
c2='PC 2',
title=title,
factor='template')
plot_pca(workdir=analysis_dir,
dataframe=pca_df,
pca=pca,
c1='PC 1',
c2='PC 3',
title=title,
factor='template')
if __name__ == '__main__':
base_dir = os.getcwd()
models_base_dir = os.path.join(base_dir, 'models')
comparative_dir = os.path.join(base_dir, 'genomes_analysis')
models_analysis_dir = os.path.join(base_dir, 'model_analysis', 'pca')
cogs_analysis_dir = os.path.join(base_dir, 'model_analysis', 'pca_cogs')
organisms_cog_file = os.path.join(comparative_dir, 'genomes_cog_analysis.tsv')
models_genes_cog_file = os.path.join(cogs_analysis_dir, 'models_cog_analysis.tsv')
reactions_dir = os.path.join(models_analysis_dir, 'reactions.tsv')
metabolites_dir = os.path.join(models_analysis_dir, 'metabolites.tsv')
organisms_analysis(cog_file=organisms_cog_file,
analysis_dir=comparative_dir)
reactions_analysis(models_dir=models_base_dir,
analysis_dir=models_analysis_dir,
filter_boundaries=True,
write=reactions_dir)
metabolites_analysis(models_dir=models_base_dir,
analysis_dir=models_analysis_dir,
filter_boundaries=True,
write=metabolites_dir)
genes_analysis(cog_file=models_genes_cog_file,
analysis_dir=cogs_analysis_dir)