Repository navigation
Expand file tree
/
Copy pathabundance_matrix.py
More file actions
executable file
·88 lines (78 loc) · 3.92 KB
/
Copy pathabundance_matrix.py
File metadata and controls
executable file
·88 lines (78 loc) · 3.92 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
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
#!/usr/bin/env python
'''This script retrieves a metagenome_statistics data structure from the MG-RAST API and
plots a graph using data from the web interface'''
from __future__ import print_function
import sys
import json
from optparse import OptionParser
from mglib import async_rest_api, sparse_to_dense, get_auth_token, API_URL
DEBUG = 0
if __name__ == '__main__':
usage = "Usage: %prog [options]\nFunction: retrieves data from MG-RAST matrix API, unwraps into csv format"
parser = OptionParser(usage)
parser.add_option("-s", "--source", dest="source", default="RefSeq", help="Annotation source: RefSeq, GenBank, IMG, SEED, TrEMBL, SwissProt, PATRIC, KEG, RDP, Greengenes, LSU, SSU")
parser.add_option("-g", "--grouplevel", dest="grouplevel", default="domain", help="Grouping level: strain, species, genus, family, order, class, phylum, domain / function, level1, level2, level3")
parser.add_option("-i", "--hittype", dest="hittype", default="single", help="Hit type: all, single, lca")
parser.add_option("-c", "--call", dest="call", default="organism", help="organism or function")
parser.add_option("-b", "--biom", dest="biom", action="store_true", help="biom output (csv defaut)")
parser.add_option("-d", "--identity", dest="identity", default=1, help="% identity threshold")
parser.add_option("-e", "--evalue", dest="evalue", default="1", help="organism or function")
parser.add_option("-t", "--type", dest="resulttype", default="abundance", help="Result type: abundnaance, evalue, identity, or length")
parser.add_option("-n", "--ngth", dest="length", default="1", help="leNgth of hit")
# parser.add_option("-v", "--verbose", dest="verbose", action="store_true", default=True, help="Verbose [default off]")
parser.add_option("-k", "--token", dest="token", type="str", help="Auth token")
parser.add_option("-m", "--metagenomes", dest="metagenomes", default="mgm4447943.3,mgm4447102.3", type="str", help="Metagenome list")
(opts, args) = parser.parse_args()
key = get_auth_token(opts)
# assign parameters
metagenomes = opts.metagenomes
group_level = opts.grouplevel
result_type = opts.resulttype
source = opts.source
evalue = opts.evalue
length = opts.length
identity = opts.identity
hittype = opts.hittype
biom = opts.biom
# construct API call
base_url = API_URL + "/matrix/organism"
if opts.call == "function" or opts.source == "SubSystems":
base_url = API_URL + "/matrix/function"
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)
URI = base_url + "&".join(["id=%s" % m for m in metagenomes.split(",")])
print(URI, file=sys.stderr)
# retrieve the data by sending at HTTP GET request to the MG-RAST API
jsonstructure = async_rest_api(URI, auth=key)
if biom:
print(json.dumps(jsonstructure))
sys.exit()
else:
print("#"+ URI, file=sys.stdout)
matrixtype = jsonstructure["data"]["matrix_type"]
if matrixtype == "sparse":
data = sparse_to_dense(jsonstructure["data"], len(rows), len(cols))
else:
data = jsonstructure["data"]
# unpack and display the data table
cols = [x["id"] for x in data["columns"]]
rows = [x["id"] for x in data["rows"]]
if DEBUG:
print(jsonstructure)
print("COLS", cols)
print("ROWS", rows)
print("TYPE", matrixtype)
print("DATA", data)
h = data["data"]
sys.stdout.write("Taxon\t")
for j in range(0, len(cols)):
sys.stdout.write(cols[j])
sys.stdout.write("\n")
for i in range(0, len(rows)):
sys.stdout.write(rows[i])
for j in range(0, len(cols)):
sys.stdout.write("\t")
try:
sys.stdout.write(str(h[i][j]))
except KeyError:
sys.stdout.write("0")
sys.stdout.write("\n")