/
exercise1.py
63 lines (51 loc) · 1.66 KB
/
exercise1.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import csv
import sys
from urllib.parse import urlencode
from jsonapi_client import Filter, Session
API_BASE = "https://www.ebi.ac.uk/metagenomics/api/v1"
# if you change the filters you may want to rename the output file
FILE_NAME = "exercise1.csv"
print("Starting...")
with open(FILE_NAME, "w") as csvfile:
# CSV initialization
fieldnames = [
"accession",
"sample-name",
"longitude",
"latitude",
"geo-loc-name",
"studies",
"biome",
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
# API call
with Session(API_BASE) as session:
# configure the filters
params = {
"latitude_gte": 70,
"experiment_type": "metagenomic",
"ordering": "accession",
# other filters should be placed here
}
api_filter = Filter(urlencode(params))
total = 0
# sessions.iterate will take care of the pagination for us
for sample in session.iterate(
"biomes/root:Environmental:Aquatic/samples", api_filter
):
total += 1
row = {
"accession": sample.accession,
"sample-name": sample.sample_name,
"longitude": sample.longitude,
"latitude": sample.latitude,
"geo-loc-name": sample.geo_loc_name,
"studies": ",".join([study.accession for study in sample.studies]),
"biome": sample.biome.id,
}
writer.writerow(row)
print("Data retrieved from the API")