Skip to content

Commit debf2e0

Browse files
committed
update search script
1 parent 09d06ae commit debf2e0

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

mglib/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
API_URL = "https://api.mg-rast.org/"
1010
SHOCK_URL = "https://shock.mg-rast.org/"
1111
AUTH_LIST = "Jared Bischof, Travis Harrison, Folker Meyer, Tobias Paczian, Andreas Wilke"
12-
SEARCH_FIELDS = ["name", "investigation_type", "biome", "feature", "material", "continent", "country", "location", "longitude", "latitude", "created_on", "env_package", "project_id", "project_name", "PI_firstname", "PI_lastname", "sequence_type", "seq_meth", "collection_date", "bp_count_raw", "sequence_count_raw", "average_length_raw"]
12+
SEARCH_FIELDS = ["all", "name", "investigation_type", "biome", "feature", "material", "continent", "country", "location", "longitude", "latitude", "created_on", "env_package", "project_id", "project_name", "PI_firstname", "PI_lastname", "sequence_type", "seq_meth", "collection_date", "bp_count_raw", "sequence_count_raw", "average_length_raw", "taxonomy", "function"]

scripts/mg-search-metagenomes.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@ def main(args):
5555
parser.add_option("", "--user", dest="user", default=None, help="OAuth username")
5656
parser.add_option("", "--passwd", dest="passwd", default=None, help="OAuth password")
5757
parser.add_option("", "--token", dest="token", default=None, help="OAuth token")
58-
parser.add_option("", "--order", dest="order", default=None, help="field metagenomes are ordered by, default is no ordering")
58+
parser.add_option("", "--limit", dest="limit", type="int", default=15, help="Number of results to show, if > 50 will use paginated queris to get all, default is 15")
59+
parser.add_option("", "--order", dest="order", default=None, help="field metagenomes are ordered by, default is by score")
5960
parser.add_option("", "--direction", dest="direction", default="asc", help="direction of order. 'asc' for ascending order, 'desc' for descending order, default is asc")
6061
parser.add_option("", "--status", dest="status", default="public", help="types of metagenomes to return. 'both' for all data (public and private), 'public' for public data, 'private' for users private data, default is public")
61-
parser.add_option("", "--verbosity", dest="verbosity", default='minimal', help="amount of information to display. use keyword 'minimal' for id and name, use keyword 'full' for MIxS GSC metadata, default is minimal")
62+
parser.add_option("", "--index", dest="index", default=None, help="index to search against, default is main DB")
6263
for sfield in SEARCH_FIELDS:
6364
parser.add_option("", "--"+sfield, dest=sfield, default=None, help="search parameter: query string for "+sfield)
6465

@@ -69,9 +70,12 @@ def main(args):
6970
token = get_auth_token(opts)
7071

7172
# build call url
72-
params = [ ('limit', '100'),
73-
('public', 0 if opts.status == 'private' else 1),
74-
('verbosity', opts.verbosity if opts.verbosity == 'minimal' else 'mixs') ]
73+
total = 0
74+
maxLimit = 50
75+
params = [ ('limit', opts.limit if opts.limit < maxLimit else maxLimit),
76+
('public', 0 if opts.status == 'private' else 1) ]
77+
if opts.index:
78+
params.append( ('index', opts.index) )
7579
for sfield in SEARCH_FIELDS:
7680
if hasattr(opts, sfield) and getattr(opts, sfield):
7781
params.append( (sfield, getattr(opts, sfield)) )
@@ -83,20 +87,21 @@ def main(args):
8387
# retrieve data
8488
fields = ['metagenome_id', 'public'] + SEARCH_FIELDS
8589
result = obj_from_url(url, auth=token)
86-
if len(result['data']) == 0:
90+
found = len(result['data'])
91+
if found == 0:
8792
sys.stdout.write("No results found for the given search parameters\n")
8893
return 0
89-
ids = [d['metagenome_id'] for d in result['data']]
94+
total += found
9095

9196
# output header
9297
safe_print("\t".join(fields)+"\n")
9398
# output rows
9499
display_search(result['data'], fields)
95100

96-
while result['next']:
101+
while ('next' in result) and result['next'] and (total < opts.limit):
97102
url = result['next']
98103
result = obj_from_url(url, auth=token)
99-
ids.extend([d['metagenome_id'] for d in result['data']])
104+
total += len(result['data'])
100105
display_search(result['data'], fields)
101106

102107
return 0

0 commit comments

Comments
 (0)