1- #!/usr/bin/env python2
1+ #!/usr/bin/env python
22'''This script retrieves a list of metagenomes from the MG-RAST API.'''
33from __future__ import print_function
4- import urllib
4+ from __future__ import unicode_literals
55import sys
66
7- from mglib import get_auth_token , obj_from_url , API_URL
7+ from mglib import get_auth_token , obj_from_url , API_URL , urlencode
88
99def printlist (js ):
1010 '''prints essential fields from metagenome list'''
1111 for item in js ["data" ]:
1212 if "public" in item .keys ():
13- public = repr ( item ["public" ])
13+ public = item ["public" ]
1414 else :
15- public = "False"
16- sys .stdout .write ( ("\t " .join ([item ["metagenome_id" ],
17- # str(len(item.keys())),
18- public , item ["created_on" ],
19- item ["name" ]]) + "\n " ).encode ("utf-8" ))
15+ public = False
16+ try :
17+ mg_name = item ["name" ]
18+ project_id = item ["project_id" ]
19+ project_name = item ["project_name" ]
20+ except KeyError :
21+ sys .stderr .write (repr (item ))
22+ sys .stdout .write (("\t " .join ([item ["metagenome_id" ],
23+ # str(len(item.keys())),
24+ repr (public ), item ["created_on" ],
25+ mg_name , project_id , project_name ]) + "\n " ))
2026
21- CALL = "/metagenome"
2227CALL = "/search"
2328
2429key = get_auth_token ()
@@ -28,19 +33,21 @@ def printlist(js):
2833
2934# construct API call
3035
31- parameters = {"limit" : limit , "auth" : key , "order" :"created_on" , "direction" : "asc" }
32- base_url = API_URL + CALL + "?" + urllib .urlencode (parameters )
36+ parameters = {"limit" : limit , "order" :"created_on" , "direction" : "asc" , "public" : "1" }
37+ API_URL = "https://api.mg-rast.org/"
38+
39+ base_url = API_URL + CALL + "?" + urlencode (parameters )
3340
3441# convert the data from a JSON structure to a python data type, a dict of dicts.
35- jsonstructure = obj_from_url (base_url )
42+ jsonstructure = obj_from_url (base_url , auth = key )
3643
3744# unpack and display the data table
3845total_count = int (jsonstructure ["total_count" ])
3946sys .stderr .write ("Total number of records: {:d}\n " .format (total_count ))
4047
41- for i in range (0 , total_count / limit + 1 ):
48+ for i in range (0 , int ( total_count / limit ) + 1 ):
4249 sys .stderr .write ("Page {:d}\t " .format (i ))
43- jsonstructure = obj_from_url (base_url )
50+ jsonstructure = obj_from_url (base_url , auth = key )
4451 printlist (jsonstructure )
4552 try :
4653 next_url = jsonstructure ["next" ]
0 commit comments