Skip to content

Commit 872c728

Browse files
author
Will Trimble
committed
do not redefine max
1 parent 3723c4f commit 872c728

1 file changed

Lines changed: 47 additions & 43 deletions

File tree

scripts/mg-submit.py

Lines changed: 47 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
submit demultiplex <seq file> [<index file>, --barcode <barcode file>, --rc_index]
3030
submit pairjoin <pair1 seq file> <pair2 seq file> [--retain, --mg_name <name>]
3131
submit pairjoin_demultiplex <pair1 seq file> <pair2 seq file> <index file> [--barcode <barcode file>, --retain, --rc_index]
32-
32+
3333
Note:
3434
each 'submit' action must include one of: --project_id, --project_name, --metadata
3535
pairjoin or pairjoin_demultiplex must contain one of: --barcode, --metadata
3636
3737
DESCRIPTION
3838
MG-RAST submission client
39-
39+
4040
supported file types | extensions
4141
----------------------|--------------------------
4242
sequence | .fasta, .fastq
@@ -46,7 +46,7 @@
4646
bzip2 compressed | .bz2
4747
zip archive | .zip
4848
tar archive | .tar, .tar.gz, .tar.bz2
49-
49+
5050
pipeline options | description
5151
----------------------|---------------------------
5252
--assembled | sequences are assembeled
@@ -82,26 +82,26 @@
8282
synch_pause = 900
8383
mgrast_auth = {}
8484
valid_actions = ["login", "list", "status", "delete", "submit"]
85-
submit_types = ["simple", "batch", "demultiplex", "pairjoin", "pairjoin_demultiplex"]
85+
submit_types = ["simple", "batch", "demultiplex", "pairjoin", "pairjoin_demultiplex"]
8686

8787
def listall():
8888
data = obj_from_url(API_URL+"/submission/list", auth=mgrast_auth['token'])
8989
submissions = sorted(data['submissions'], key=itemgetter('timestamp'))
9090
pt = PrettyTable(["ID", "type", "status", "time"])
9191
for s in submissions:
92-
row = [ s['id'], s['type'], s['status'], s['timestamp'] ]
92+
row = [s['id'], s['type'], s['status'], s['timestamp']]
9393
pt.add_row(row)
9494
pt.align = "l"
9595
print(pt)
9696

9797
def status(sid):
9898
data = obj_from_url(API_URL+"/submission/"+sid+'?full=1', auth=mgrast_auth['token'])
99-
99+
100100
# check for errors
101101
if ('error' in data) and data['error']:
102102
sys.stderr.write("ERROR: %s\n"%data['error'])
103103
sys.exit(1)
104-
104+
105105
try:
106106
fids = map(lambda x: x['id'], data['inputs'])
107107
fnames = map(lambda x: x['filename'], data['inputs'])
@@ -110,13 +110,13 @@ def status(sid):
110110
fids = map(lambda x: x['id'], data['inputs'].values())
111111
fnames = map(lambda x: x['filename'], data['inputs'].values())
112112
fsizes = map(lambda x: str(x['filesize']), data['inputs'].values())
113-
113+
114114
# submission summary
115115
pt_summary = PrettyTable(["submission ID", "type", "project", "submit time", "input file ID", "input file name", "input file size", "status"])
116116
pt_summary.add_row([data['id'], data['type'], data['project'], data['info']['submittime'], "\n".join(fids), "\n".join(fnames), "\n".join(fsizes), data['state']])
117117
pt_summary.align = "l"
118118
print(pt_summary)
119-
119+
120120
# submission status
121121
if ('preprocessing' in data) and data['preprocessing']:
122122
pt_status = PrettyTable(["submission step", "step name", "step status", "step inputs"])
@@ -127,7 +127,7 @@ def status(sid):
127127
pt_status.add_row([i, p['stage'], pstatus, "\n".join(p['inputs'])])
128128
pt_status.align = "l"
129129
print(pt_status)
130-
130+
131131
# metagenome info
132132
if ('metagenomes' in data) and data['metagenomes']:
133133
pt_mg = PrettyTable(["metagenome ID", "metagenome name", "status", "current steps", "submit time", "complete time", "pipeline ID"])
@@ -250,13 +250,17 @@ def seqs_from_json(json_in, tmp_dir):
250250
return stype, files
251251

252252
def submit(stype, files, opts):
253+
''' submit(stype, files, opts)
254+
Call MG-RAST API to upload and submit one or more files
255+
stype one of "simple" "demultiplex" "pairjoin" "pairjoin_demultiplex" '''
256+
253257
fids = []
254258
# post files to shock
255259
if stype == 'batch':
256260
fids = archive_upload(files[0], opts.verbose)
257261
else:
258262
fids = upload(files, opts.verbose)
259-
263+
260264
# set POST data
261265
data = {}
262266
if opts.debug:
@@ -291,7 +295,7 @@ def submit(stype, files, opts):
291295
data['index_file'] = fids[2]
292296
data['retain'] = 1 if opts.retain else 0
293297
data['rc_index'] = 1 if opts.rc_index else 0
294-
298+
295299
# set pipeline flags - assembeled is special case
296300
if opts.assembled:
297301
data['assembled'] = 1
@@ -316,7 +320,7 @@ def submit(stype, files, opts):
316320
data['screen_indexes'] = opts.screen_indexes
317321
if opts.priority:
318322
data['priority'] = opts.priority
319-
323+
320324
# submit it
321325
if opts.verbose:
322326
print("Submitting to MG-RAST with the following parameters:")
@@ -336,6 +340,7 @@ def submit(stype, files, opts):
336340
status(result['id'])
337341

338342
def upload(files, verbose):
343+
''' upload(files, verbose) -- call MG-RAST api to upload one or more files to inbox '''
339344
fids = []
340345
attr = json.dumps({
341346
"type": "inbox",
@@ -345,18 +350,18 @@ def upload(files, verbose):
345350
})
346351

347352
results = {
348-
'submitted' : [] ,
349-
'failed' : [] ,
350-
'files' : files
353+
'submitted': [],
354+
'failed': [],
355+
'files': files
351356
}
352357

353358
# Settings for nr tries
354-
max = 3
359+
maxtries = 3
355360
current = 0
356361
sleep = 60
357362

358-
while len(results['files']) and current < max :
359-
363+
while len(results['files']) and current < maxtries:
364+
360365
# increase counter
361366
current += 1
362367

@@ -381,12 +386,12 @@ def upload(files, verbose):
381386
if len(files) > 1:
382387
print("Uploading file %d of %d (%s) to MG-RAST Shock"%(i+1, len(files), f))
383388
else:
384-
print("Uploading file %s to MG-RAST Shock"%(f) )
385-
if True : # change to debug
389+
print("Uploading file %s to MG-RAST Shock"%(f))
390+
if True: # change to debug
386391
print("Submitting %s to %s " % (f,SHOCK_URL))
387392
result = post_file(SHOCK_URL+"/node", fformat, f, data=data, auth=mgrast_auth['token'], debug=verbose)
388-
389-
if result :
393+
394+
if result:
390395
if verbose:
391396
print(json.dumps(result['data']))
392397
if len(files) > 1:
@@ -401,21 +406,21 @@ def upload(files, verbose):
401406
print(info['status'])
402407
fids.append(result['data']['id'])
403408
results['submitted'].append(f)
404-
else :
409+
else:
405410
print(f)
406-
sys.stderr.write("ERROR: can not submit %s\n" % (f) )
411+
sys.stderr.write("ERROR: can not submit %s\n" % (f))
407412
results['failed'].append(f)
408413

409-
if verbose :
410-
print( results )
411-
print( "Processed %d\tFailed %d" % (len(results['files']), len(results['failed']) ) )
414+
if verbose:
415+
print(results)
416+
print("Processed %d\tFailed %d" % (len(results['files']), len(results['failed'])))
412417
# switch list, process failed again
413418
results['files'] = results['failed']
414419
results['failed'] = []
415420

416-
# wait
417-
time.sleep( current * sleep )
418-
421+
# wait
422+
time.sleep(current * sleep)
423+
419424
return fids
420425

421426
def archive_upload(afile, verbose):
@@ -498,12 +503,12 @@ def main(args):
498503
parser.add_argument("--tmp_dir", dest="tmp_dir", default="", help="Temp dir to download too if using json_in option, default is current working dir")
499504
parser.add_argument("-v", "--verbose", dest="verbose", action="store_true", default=False, help="Verbose STDOUT")
500505
parser.add_argument("--debug", dest="debug", action="store_true", default=False, help="Submit in debug mode")
501-
parser.add_argument("action", type=str, default=False, help="Action. One of "+ ",".join(valid_actions), nargs='+')
506+
parser.add_argument("action", type=str, default=False, help="Action. One of "+ ",".join(valid_actions), nargs='+')
502507
# parser.add_argument("subaction", type=str, default=False, help="Action word 2", default=None)
503-
508+
504509
# get inputs
505510
opts = parser.parse_args()
506-
511+
507512
# special case
508513
json_submit = True if opts.json_in and os.path.isfile(opts.json_in) else False
509514
if json_submit:
@@ -516,23 +521,23 @@ def main(args):
516521
args = opts.action
517522
API_URL = opts.mgrast_url
518523
SHOCK_URL = opts.shock_url
519-
524+
520525
if opts.verbose and opts.debug:
521526
print("##### Running in Debug Mode #####")
522-
527+
523528
# validate inputs
524529
if action not in valid_actions:
525530
sys.stderr.write("ERROR: invalid action. use one of: %s\n"%", ".join(valid_actions))
526531
return 1
527-
elif (action in ["status", "delete"]) and (len(args) < 2):
532+
if (action in ["status", "delete"]) and (len(args) < 2):
528533
sys.stderr.write("ERROR: %s missing submission ID\n"%action)
529534
return 1
530-
elif (action == "submit") and (not json_submit):
535+
if (action == "submit") and (not json_submit):
531536
if not (opts.project_id or opts.project_name or opts.metadata):
532537
sys.stderr.write("ERROR: invalid submit, must have one of project_id, project_name, or metadata\n")
533538
return 1
534539
if (len(args) < 2) or (args[1] not in submit_types):
535-
sys.stderr.write("ERROR: invalid submit option '%s'. use one of: %s\n"%( args[1], ", ".join(submit_types)))
540+
sys.stderr.write("ERROR: invalid submit option '%s'. use one of: %s\n"%(args[1], ", ".join(submit_types)))
536541
return 1
537542
if ( ((args[1] == "simple") and (len(args) < 3)) or
538543
((args[1] == "batch") and (len(args) != 3)) or
@@ -544,20 +549,20 @@ def main(args):
544549
if ((args[1] == "demultiplex") or (args[1] == "pairjoin_demultiplex")) and (not (opts.metadata or opts.barcode)):
545550
sys.stderr.write("ERROR: submit %s requires either metadata or barcode file\n"%args[1])
546551
return 1
547-
552+
548553
# explict login
549554
token = get_auth_token(opts)
550555
if action == "login":
551556
if not token:
552557
token = input('Enter your MG-RAST auth token: ')
553558
login(token)
554559
return 0
555-
560+
556561
# get auth object, get from token if no login
557562
mgrast_auth = get_auth(token)
558563
if not mgrast_auth:
559564
return 1
560-
565+
561566
# actions
562567
if action == "list":
563568
if opts.verbose:
@@ -589,4 +594,3 @@ def main(args):
589594

590595
if __name__ == "__main__":
591596
sys.exit(main(sys.argv))
592-

0 commit comments

Comments
 (0)