Repository navigation
Expand file tree
/
Copy pathlist_all_mg.py
More file actions
executable file
·62 lines (52 loc) · 2.04 KB
/
Copy pathlist_all_mg.py
File metadata and controls
executable file
·62 lines (52 loc) · 2.04 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
#!/usr/bin/env python
'''This script retrieves a list of metagenomes from the MG-RAST API.'''
from __future__ import print_function
from __future__ import unicode_literals
import sys
import time
from mglib import get_auth_token, obj_from_url, API_URL, urlencode
def printlist(js):
'''prints essential fields from metagenome list'''
for item in js["data"]:
if "public" in item.keys():
public = item["public"]
else:
public = "False"
try:
mg_name= item["name"]
project_id = item["project_id"]
project_name = item["project_name"]
except KeyError:
sys.stderr.write(repr(item) + "\n")
sys.stdout.write(("\t".join([item["metagenome_id"],
# str(len(item.keys())),
repr(public), item["created_on"],
mg_name, project_id, project_name]) + "\n"))
CALL = "/search"
key = get_auth_token()
# assign parameters
limit = 1000 # initial call
# construct API call
# public = 0 means "don't show public metagenomes"
parameters = {"limit": limit, "order":"created_on", "direction": "asc", "public": "1"}
API_URL= "https://api.mg-rast.org/"
base_url = API_URL + CALL + "?" + urlencode(parameters)
# convert the data from a JSON structure to a python data type, a dict of dicts.
jsonstructure = obj_from_url(base_url, auth=key)
# unpack and display the data table
total_count = int(jsonstructure["total_count"])
sys.stderr.write("Total number of records: {:d}\n".format(total_count))
for i in range(0, int(total_count / limit) +2):
# sys.stderr.write("Page {:d}\t".format(i))
sys.stderr.write("Page {:d}\t{}\n".format(i, base_url))
jsonstructure = obj_from_url(base_url, auth=key)
printlist(jsonstructure)
time.sleep(1)
if "next" in jsonstructure.keys():
next_url = jsonstructure["next"]
base_url = next_url
continue
else:
sys.stderr.write("No next, page {} url {} \n".format(i, base_url))
sys.stderr.write(repr(jsonstructure))
break