-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRead_functional.py
581 lines (351 loc) · 13.3 KB
/
Read_functional.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
#!/usr/bin/env python
# coding: utf-8
# # Functional Analysis from EBI-MGNify
#
# _Matin Nuhamunada_<sup>1*</sup>, _Ahmad Ardi_<sup>1*</sup>
#
# <sup>1</sup>Department of Tropical Biology, Universitas Gadjah Mada;
# Jl. Teknika Selatan, Sekip Utara, Bulaksumur, Yogyakarta, Indonesia, 55281;
#
# *Correspondence: [[email protected]](mailto:[email protected])
#
# ## Daftar Isi
# 1. Deskripsi Data
# 2. Challenge
# 3. Load Library
# 4. Web Scraping & Data Cleaning
# 5. Exploratory Data Analysis (EDA)
# 6. Data Mining
#
# ## A. Deskripsi Data
# Ada tiga file yang dapat diakses dari hasil analisis fungsional EBI-MGnify: (https://emg-docs.readthedocs.io/en/latest/portal.html#description-of-functional-annotation-files-available-to-download)
#
# 1. *InterPro matches file*: it is a tab-delimited file containing 15 columns.
# 2. *Complete GO annotation file*: it is a comma-separated file containing 4 columns. The first column lists the GO terms (labelled GO:XXXXXXX) having been associated with the predicted CDSs. The second gives the GO term description while the third indicates which category the GO term belong to. There is 3 category: ‘biological process’ (higher biological process such as ‘rRNA modification’) , ‘molecular function’ (individual catalytic activity such as ‘mannosyltransferase activity’) and ‘cellular component’ (cellular localisation of the activity such as ‘mitochondrion’). The last column give the number of predicted CDSs having been annotated with the GO terms for the run.
# 3. *GO slim annotation file*: this file is derived from the ‘Complete GO annotation file’ and has the same format. The GO slim set is a cut-down version of the GO terms containing a subset of the terms in the whole GO. They give a broad overview of the ontology content without the details of the specific fine grained terms. Go slim terms are used for visualisation on the website. To illustrate how the GO slim terms relates to the GO terms, the different metal binding GO terms present in the ‘Complete GO annotation’ file are summarized as one generic metal binding term in the ‘GO slim annotation’ file. The last column give the number of predicted CDSs having been annotated with the GO slim terms for the run.
#
# ## B. Challenge
# 1. Web Scraping menggunakan RESTful API dari EBI MGnify untuk mencari studi dengan kata kunci "human", "skin", dan kategori sampel "metagenome" (hasil shotgun sequencing)
# 2. Melakukan (a) filtering studi dan pemilihan sampel untuk studi komparatif, dan (b) mengambil metadata dari sampel terpilih
# 3. Web Scraping menggunakan RESTful API dari EBI MGnify untuk mengunduh data hasil analisis fungsional (ketiga file di atas)
# 4. Melakukan data cleaning dan eksplorasi data (visualisasi)
# 5. Data mining dengan menggunakan analisis statistik: dengan dimensional reduction (PCoA, UMAP, T2SNE), dan teknik multivariat lainnya
#
# ## C. Load Library
# In[1]:
import os
from pandas import DataFrame
import pandas as pd
import pycurl
from scipy import stats
import matplotlib.pyplot as plt
plt.close('all')
import seaborn as sns
get_ipython().run_line_magic('matplotlib', 'inline')
# In[2]:
from jsonapi_client import Session, Filter
import html
API_BASE = 'https://www.ebi.ac.uk/metagenomics/api/latest/'
# ## D. Webscraping & Data Cleaning
# Web Scraping menggunakan RESTful API dari EBI MGnify untuk mencari studi dengan kata kunci "human", "skin", dan kategori sampel "metagenome" (hasil shotgun sequencing)
# ### D.1 Study
# Pada part ini kita akan mencoba melihat ada berapa banyak studi terkait dengan Human Skin di EBI MGnify
# In[3]:
#obtaining study data - TO DO!
def get_biome(lineage, exp_type):
API_BASE_BIOME = 'https://www.ebi.ac.uk/metagenomics/api/latest/biomes'
with Session(API_BASE_BIOM) as s:
study = s.get(run,'analysis').resource
for i in study.downloads:
if extension in i.file_format['name']:
link = i.url
return link
# In[15]:
API_BASE_BIOME = 'https://www.ebi.ac.uk/metagenomics/api/latest/biomes'
with Session(API_BASE_BIOME) as s:
biome = s.get('root:Host-associated:Human:Skin').resource
# In[21]:
biome.samples_count
# In[22]:
biome.studies
# In[23]:
biome.samples
# In[4]:
#load study data
study_data = pd.read_csv("data_study.csv")
study_data
# In[5]:
print(study_data.describe())
print('\n' + 'Total sampel: ' + str(study_data.Samples.sum()) + '\n')
print('Centers : ' + str(len(study_data['Centre name'].unique())))
x = 0
for i in study_data['Centre name'].unique():
x = x + 1
print(str(x) + ' ' + i)
# ### D.2 Kesimpulan Studi
# Diperoleh 15 studi di EBI yang dilakukan dari 10 institusi, dengan total sampel terkait human skin microbiome sebanyak 4053
# ## D.3 Samples
# Pada part ini kita akan melihat sampel apa saja yang tersedia untuk human skin metagenome
# In[6]:
# Load samples
sample_data = pd.read_csv("data_sample.csv")
# In[7]:
# Rapikan label
label = []
for i in sample_data.Description:
y = " ".join(i.split())
label.append(y)
# In[8]:
# Replace label
for x in range(len(sample_data)):
sample_data.Description[x] = label[x]
sample_data.head()
# In[9]:
x = 0
for i in sample_data.Description.unique():
x = x + 1
print(str(x) + ' ' + i)
# In[10]:
sample_data.groupby(['MGnify ID']).describe()
# In[11]:
sample_data['MGnify ID'].count()
# In[12]:
study_id = sample_data['MGnify ID'].unique()
study_id_clean = []
for x in study_id:
y = x.split(',')
for z in y:
study_id_clean.append(z)
study_id_clean = list(dict.fromkeys(study_id_clean))
# In[13]:
x = 0
for i in study_id_clean:
x = x + 1
print(str(x) + ' ' + i)
# In[14]:
df = study_data.loc[study_data['MGnify ID'].isin(study_id_clean)]
df.reset_index(drop=True)
# ### Kesimpulan
# Hanya ada 9 Studi terkait data metagenome
# ### Ambil Metadata
# In[15]:
study_id
# In[16]:
filtered_data = sample_data.loc[sample_data['MGnify ID'] == study_id[0]] #ganti nomor ini dari study id
filtered_data = filtered_data.reset_index(drop=True)
filtered_data
# ### Getting Sample Metadata
# In[17]:
def ebi_sample(sample):
API_BASE = 'https://www.ebi.ac.uk/metagenomics/api/latest/samples'
with Session(API_BASE) as s:
sample = s.get(sample).resource
return sample
# In[18]:
sampled = ebi_sample('SRS731606')
# In[19]:
print(sampled.accession)
print(sampled.analysis_completed)
#print(sampled.biome)
print(sampled.biosample)
print(sampled.collection_date)
print(sampled.environment_biome)
print(sampled.environment_feature)
print(sampled.environment_material)
print(sampled.geo_loc_name)
print(sampled.host_tax_id)
print(sampled.last_update)
print(sampled.latitude)
print(sampled.longitude)
print(sampled.runs)
print(sampled.sample_alias)
print(sampled.sample_desc)
print(sampled.sample_metadata)
print(sampled.species)
#print(sampled.studies)
# In[20]:
sampled = ebi_sample('SRS731606')
for x in sampled.runs:
print(x.id)
for y in x.analyses:
print(y.id)
for z in y.downloads:
if 'FASTQ_GO_slim.csv' in z.url:
print(z.url)
# In[21]:
filename = y.id + '_FASTQ_GO_slim.csv'
link = z.url
if not os.path.isfile(filename):
with open(filename, 'wb') as f:
c = pycurl.Curl()
c.setopt(c.URL, link)
c.setopt(c.WRITEDATA, f)
c.perform()
c.close()
# In[21]:
with open('SRR1631558_MERGED_FASTQ_GO_slim.csv', 'wb') as f:
c = pycurl.Curl()
c.setopt(c.URL, 'https://www.ebi.ac.uk/metagenomics/api/v1/analyses/MGYA00023807/file/SRR1631558_MERGED_FASTQ_GO_slim.csv')
c.setopt(c.WRITEDATA, f)
c.perform()
c.close()
# In[23]:
t = y.downloads[7]
t.url
# ## Challenge 2
# In[24]:
def get_analysis_result(run, extension):
API_BASE_RUN = 'https://www.ebi.ac.uk/metagenomics/api/latest/runs'
with Session(API_BASE_RUN) as s:
study = s.get(run,'analysis').resource
for i in study.downloads:
if extension in i.file_format['name']:
link = i.url
return link
# In[ ]:
# Updated
def get_study(search, filename):
if not os.path.isfile(filename):
with open(filename, 'wb') as f:
c = pycurl.Curl()
c.setopt(c.URL, 'https://www.ebi.ac.uk/metagenomics/api/v1/studies?lineage=root&ordering=-last_update&search='+search+'&format=csv')
c.setopt(c.WRITEDATA, f)
c.perform()
c.close()
return filename
def get_metadata(metadata, key):
for m in metadata:
if m['key'].lower() == key.lower():
value = m['value']
unit = html.unescape(m['unit']) if m['unit'] else ""
return "{value} {unit}".format(value=value, unit=unit)
return None
def get_analysis_result(run, extension):
API_BASE_RUN = 'https://www.ebi.ac.uk/metagenomics/api/latest/runs'
with Session(API_BASE_RUN) as s:
study = s.get(run,'analysis').resource
for i in study.downloads:
if extension in i.file_format['name']:
link = i.url
return link
#perlu diupdate utk rhizosfer
def random_sampling(dataframe, amount):
df_random = DataFrame(columns=('Sample_ID','Run_ID','Release_version','Sex','Body_site', 'Description'))
df_random.index.name = 'No'
a = 0
while a < amount:
i = np.random.choice(dataframe.index.values, 1)
container = df_random.loc[:, 'Sample_ID']
if not container.isin([dataframe.loc[i[0], 'Sample_ID']]).any():
df_random.loc[i[0]] = [dataframe.loc[i[0], 'Sample_ID'], dataframe.loc[i[0], 'Run_ID'], dataframe.loc[i[0], 'Release_version'], dataframe.loc[i[0], 'Sex'], dataframe.loc[i[0], 'Body_site'], dataframe.loc[i[0], 'Description'] ]
a = a + 1
return df_random
# In[ ]:
API_BASE_SAMPLE = 'https://www.ebi.ac.uk/metagenomics/api/v1/samples'
API_BASE_RUNS = 'https://www.ebi.ac.uk/metagenomics/api/v1/runs'
with Session(API_BASE_SAMPLE) as s:
sample = s.get('SRS731606','runs').resources
for a in sample:
print(a)
# In[ ]:
a.accession
# In[ ]:
API_BASE_RUNS = 'https://www.ebi.ac.uk/metagenomics/api/v1/runs'
with Session(API_BASE_RUNS) as r:
runs = r.get('SRR1631560','analyses').resources
for x in runs:
print(x)
# In[ ]:
y = x.downloads
z = x.go_slim
# ## Challenge 4-5
# ### 5.1 Visualize Functional Analysis - Summary
# In[22]:
df_slim = pd.DataFrame(columns=['GO_ID', 'Category', 'Pathway'])
file = os.listdir()
for a in range(len(file)):
if 'slim.csv' in file[a]:
sample_ID = file[a].replace('_FASTQ_GO_slim.csv', '')
x = pd.read_csv(file[a], header=None)
x.columns = ['GO_ID', 'Category', 'Pathway', sample_ID]
x[sample_ID] = (x[sample_ID] / x[sample_ID].sum()) * 100
#print(x.head())
df_slim = pd.merge(x, df_slim, on=['GO_ID','Category','Pathway'], how='left')
df_slim.head()
# In[25]:
df_slim.to_csv('df_slim.csv')
# In[23]:
df_slim_sum = df_slim.drop(columns=['GO_ID','Pathway'])
df_slim_sum = df_slim_sum.set_index('Category')
df_slim_sum.head()
# In[27]:
df_slim_sum['total'] = df_slim_sum.sum(axis=1)
df_slim_sum['total'] = df_slim_sum['total']/3
df_slim_sum = df_slim_sum.sort_values('total', ascending=True)
df_plot = df_slim_sum.drop(columns=['total'])
plt.figure()
df_plot.plot.barh(figsize=(8, 35))
# In[28]:
sns.heatmap(df_plot[:100])
# In[29]:
df_plot_z = df_plot.apply(stats.zscore)
plt.figure()
df_plot_z.plot.barh(figsize=(8, 35))
# In[30]:
sns.heatmap(df_plot_z[:100])
# ### 5.2 Detailed GO
# In[80]:
df_GO = pd.DataFrame(columns=['GO_ID', 'Category', 'Pathway'])
file = os.listdir()
for a in range(len(file)):
if 'FASTQ_GO.csv' in file[a]:
sample_ID = file[a].replace('_FASTQ_GO.csv', '')
x = pd.read_csv(file[a], header=None)
x.columns = ['GO_ID', 'Category', 'Pathway', sample_ID]
x[sample_ID] = (x[sample_ID] / x[sample_ID].sum()) * 100
#print(x.head())
df_GO = pd.merge(x, df_GO, on=['GO_ID','Category','Pathway'], how='left')
df_GO.head()
# In[81]:
df_GO_sum = df_GO.drop(columns=['GO_ID','Pathway'])
df_GO_sum = df_GO_sum.set_index('Category')
df_GO_sum.describe()
# In[82]:
df_GO_sum['total'] = df_GO_sum.sum(axis=1)
df_GO_sum['total'] = df_GO_sum['total']/2
df_GO_sum = df_GO_sum.sort_values('total', ascending=False)
df_GO_sum.head()
# In[83]:
df_heatmap = df_GO_sum.drop(columns=['total'])
sns.heatmap(df_heatmap[:30])#, annot=True, linewidths=.25)
# ### 5.3 Interpro
# In[ ]:
df = pd.read_csv('ERR476421_FASTQ_I5.tsv', header=None, sep='\n')
df = df[0].str.split('\t', expand=True)
df.columns = ['Protein_accession',
'Sequence_MD5_digest',
'Sequence_length',
'Analysis',
'Signature_accession',
'Signature_description',
'Start_location',
'Stop_location',
'Score',
'Status',
'Date',
'Interpro_accession',
'Interpro_description',
'GO_annotations']
df.head()
# In[ ]:
df.Sequence_length.describe()
# In[ ]:
df.Sequence_length.unique()
# In[ ]:
x = df.Interpro_accession.unique()
x
# In[ ]:
df.Interpro_accession.describe()
# In[ ]:
df.Interpro_accession
# In[ ]: