Skip to content

Commit 0d68d63

Browse files
authored
Merge pull request #204 from wltrimbl/master
Maintenance update
2 parents 0109ed4 + 6132942 commit 0d68d63

10 files changed

Lines changed: 119 additions & 284 deletions

File tree

examples/python/abundance_matrix.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44

55
from __future__ import print_function
66
import sys
7+
import json
78
from optparse import OptionParser
89
from mglib import async_rest_api, sparse_to_dense, get_auth_token, API_URL
910

1011
DEBUG = 0
1112

1213
if __name__ == '__main__':
13-
usage = "usage: %prog -i <input sequence file> -o <output file>"
14+
usage = "Usage: %prog [options]\nFunction: retrieves data from MG-RAST matrix API, unwraps into csv format"
1415
parser = OptionParser(usage)
1516
parser.add_option("-s", "--source", dest="source", default="RefSeq", help="Annotation source: RefSeq, GenBank, IMG, SEED, TrEMBL, SwissProt, PATRIC, KEG, RDP, Greengenes, LSU, SSU")
1617
parser.add_option("-g", "--grouplevel", dest="grouplevel", default="domain", help="Grouping level: strain, species, genus, family, order, class, phylum, domain / function, level1, level2, level3")
1718
parser.add_option("-i", "--hittype", dest="hittype", default="single", help="Hit type: all, single, lca")
1819
parser.add_option("-c", "--call", dest="call", default="organism", help="organism or function")
20+
parser.add_option("-b", "--biom", dest="biom", action="store_true", help="biom output (csv defaut)")
1921
parser.add_option("-d", "--identity", dest="identity", default=1, help="% identity threshold")
2022
parser.add_option("-e", "--evalue", dest="evalue", default="1", help="organism or function")
2123
parser.add_option("-t", "--type", dest="resulttype", default="abundance", help="Result type: abundnaance, evalue, identity, or length")
@@ -36,17 +38,22 @@
3638
length = opts.length
3739
identity = opts.identity
3840
hittype = opts.hittype
41+
biom = opts.biom
3942
# construct API call
4043
base_url = API_URL + "/matrix/organism"
4144
if opts.call == "function" or opts.source == "SubSystems":
4245
base_url = API_URL + "/matrix/function"
43-
base_url = base_url + "?asynchronous=1&group_level=%s&result_type=%s&auth=%s&source=%s&evalue=%s&length=%s&identity=%s&hittype=%s&" % (group_level, result_type, key, source, evalue, length, identity, hittype)
46+
base_url = base_url + "?asynchronous=1&group_level=%s&result_type=%s&source=%s&evalue=%s&length=%s&identity=%s&hittype=%s&" % (group_level, result_type, source, evalue, length, identity, hittype)
4447
URI = base_url + "&".join(["id=%s" % m for m in metagenomes.split(",")])
4548
print(URI, file=sys.stderr)
46-
print("#"+ URI, file=sys.stdout)
4749
# retrieve the data by sending at HTTP GET request to the MG-RAST API
4850

4951
jsonstructure = async_rest_api(URI, auth=key)
52+
if biom:
53+
print(json.dumps(jsonstructure))
54+
sys.exit()
55+
else:
56+
print("#"+ URI, file=sys.stdout)
5057

5158
# unpack and display the data table
5259
cols = [x["id"] for x in jsonstructure["columns"]]

examples/python/annotation_table.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99

1010
DEBUG = 0
1111

12-
1312
if __name__ == '__main__':
14-
usage = "usage: %prog -i <input sequence file> -o <output file>"
13+
usage = "usage: %prog [options]\nFunction: retrieves and presents table of sequence IDs and annotation table results"
1514
parser = OptionParser(usage)
1615
parser.add_option("-s", "--source", dest="source", default="RefSeq", help="Annotation source: RefSeq, GenBank, IMG, SEED, TrEMBL, SwissProt, PATRIC, KEG, RDP, Greengenes, LSU, SSU")
1716
parser.add_option("-g", "--grouplevel", dest="grouplevel", default="domain", help="Grouping level: strain, species, genus, family, order, class, phylum, domain / function, level1, level2, level3")
@@ -36,7 +35,7 @@
3635

3736
# construct API call
3837
base_url = API_URL + "/profile/{}".format(metagenomes)
39-
base_url = base_url + "?asynchronous=1&group_level=%s&result_type=%s&auth=%s&source=%s&evalue=%s&" % (group_level, result_type, key, source, evalue)
38+
base_url = base_url + "?asynchronous=1&group_level=%s&result_type=%s&source=%s&evalue=%s&" % (group_level, result_type, source, evalue)
4039
URI = base_url + "&".join(["id=%s" % m for m in metagenomes.split(",")])
4140
URI = base_url
4241
print(URI, file=sys.stderr)

examples/python/list_all_mg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def printlist(js):
1212
if "public" in item.keys():
1313
public = item["public"]
1414
else:
15-
public = False
15+
public = "False"
1616
try:
1717
mg_name= item["name"]
1818
project_id = item["project_id"]

mglib/mglib.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ def body_from_url(url, accept, auth=None, data=None, debug=False, method=None):
7373
# return python struct from JSON output of MG-RAST or Shock API
7474
def obj_from_url(url, auth=None, data=None, debug=False, method=None):
7575
result = body_from_url(url, 'application/json', auth=auth, data=data, debug=debug, method=method)
76-
obj = json.loads(result.read().decode("utf8"))
76+
if result.headers["content-type"] == "application/x-download":
77+
return(result.read())
78+
else:
79+
obj = json.loads(result.read().decode("utf8"))
7780
if obj is None:
7881
sys.stderr.write("ERROR: return structure not valid json format\n")
7982
sys.exit(1)
@@ -120,26 +123,31 @@ def async_rest_api(url, auth=None, data=None, debug=False, delay=60):
120123
submit = obj_from_url(url, auth=auth, data=data, debug=debug)
121124
# If "status" is nor present, or if "status" is somehow not "submitted"
122125
# assume this is not an asynchronous call and it's done.
123-
if ('status' in submit) and (submit['status'] == 'done') and ('url' in submit):
126+
if ('status' in submit) and (submit['status'] != 'submitted') and ('data' in submit):
124127
return submit['data']
125-
if not (('status' in submit) and (submit['status'] == 'submitted') and ('url' in submit)):
126-
return submit
127-
result = obj_from_url(submit['url'], debug=debug)
128+
# if not (('status' in submit) and (submit['status'] == 'submitted') and ('url' in submit)):
129+
# return submit # No status, no url and no submitted
130+
result = obj_from_url(submit['url'], auth=auth, debug=debug)
128131
try:
129-
while result['status'] != 'done':
132+
while result['status'] == 'submitted':
130133
if debug:
131134
print("waiting %d seconds ..."%delay)
132135
time.sleep(delay)
133-
result = obj_from_url(submit['url'], debug=debug)
136+
result = obj_from_url(submit['url'], auth=auth, debug=debug)
134137
except KeyError:
135138
print("Error in response to "+url, file=sys.stderr)
136139
print("Does not contain 'status' field, likely API syntax error", file=sys.stderr)
137140
print(json.dumps(result), file=sys.stderr)
138141
sys.exit(1)
139-
if 'ERROR' in result['data']:
140-
sys.stderr.write("ERROR: %s\n" %result['data']['ERROR'])
141-
print(json.dumps(result), file=sys.stderr)
142-
sys.exit(1)
142+
except TypeError: # result isn't json, return it anyway
143+
return(result.decode("utf8"))
144+
try:
145+
if 'ERROR' in result['data']:
146+
sys.stderr.write("ERROR: %s\n" %result['data']['ERROR'])
147+
print(json.dumps(result), file=sys.stderr)
148+
sys.exit(1)
149+
except KeyError: # result doesn't have "data"
150+
return result
143151
return result['data']
144152

145153
# POST file to MG-RAST or Shock
@@ -407,7 +415,7 @@ def kbids_to_mgids(kbids):
407415
def kbid_lookup(ids, reverse=False):
408416
request = 'mg2kb' if reverse else 'kb2mg'
409417
post = json.dumps({'ids': ids}, separators=(',',':'))
410-
data = obj_from_url(API_URL+'/job/'+request, data=post)
418+
data = obj_from_url(API_URL+'/job/'+request, auth=auth, data=post)
411419
return data['data']
412420

413421
def get_auth_token(opts=None):

scripts/mg-biom2metadata

Lines changed: 0 additions & 100 deletions
This file was deleted.

scripts/mg-biom2taxa

Lines changed: 0 additions & 75 deletions
This file was deleted.

scripts/mg-query.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
if __name__ == '__main__':
1414
usage = "usage: %prog [options] URI"
1515
parser = ArgumentParser(usage)
16-
# parser.add_argument("-v", "--verbose", dest="verbose", action="store_true")
16+
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true")
1717
parser.add_argument("-k", "--token", dest="token", type=str,
1818
help="Auth token")
1919
parser.add_argument("URI", type=str, help="URI to query")
2020

2121
opts = parser.parse_args()
2222
key = get_auth_token(opts)
23-
23+
if opts.verbose:
24+
print("KEY = {}".format(key), file=sys.stderr)
2425
# assign parameters
2526
URI = opts.URI
2627

@@ -31,4 +32,7 @@
3132
jsonstructure = async_rest_api(URI, auth=key)
3233

3334
# unpack and display the data table
34-
print(json.dumps(jsonstructure))
35+
if type(jsonstructure) == str: # If we have data, not json structure
36+
sys.stdout.write(jsonstructure)
37+
else:
38+
print(json.dumps(jsonstructure), file=sys.stdout)

tests/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ RUN apt-get update && apt-get install -y git make python python-setuptools gcc
44

55
# setproctile needs Python.h from python-dev
66
# requests needs gcc to build
7-
RUN git clone http://github.com/wltrimbl/MG-RAST-Tools
7+
RUN git clone http://github.com/MG-RAST/MG-RAST-Tools
88
RUN cd MG-RAST-Tools && make install

0 commit comments

Comments
 (0)