Mercurial > repos > ggricourt > data_manager_bigg
comparison data_manager/bigg_model_sbml_fetcher.py @ 0:262b8d79bc08 draft
"planemo upload for repository https://github.com/brsynth/synbiocad-galaxy-wrappers commit 19ad973add8651c1a18c1bda789e9296a57044b1"
author | ggricourt |
---|---|
date | Wed, 23 Feb 2022 14:33:49 +0000 |
parents | |
children | 5068c6484606 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:262b8d79bc08 |
---|---|
1 import argparse | |
2 import json | |
3 import os | |
4 import sys | |
5 try: | |
6 # For Python 3.0 and later | |
7 from urllib.request import Request, urlopen | |
8 except ImportError: | |
9 # Fall back to Python 2 imports | |
10 from urllib2 import Request, urlopen | |
11 | |
12 | |
13 def url_download(url, workdir): | |
14 file_path = os.path.abspath(os.path.join(workdir, os.path.basename(url))) | |
15 src = None | |
16 dst = None | |
17 try: | |
18 req = Request(url) | |
19 src = urlopen(req) | |
20 with open(file_path, 'wb') as dst: | |
21 while True: | |
22 chunk = src.read(2**10) | |
23 if chunk: | |
24 dst.write(chunk) | |
25 else: | |
26 break | |
27 except Exception as e: | |
28 sys.exit(str(e)) | |
29 finally: | |
30 if src: | |
31 src.close() | |
32 return file_path | |
33 | |
34 | |
35 def download(model_id, out_file): | |
36 | |
37 with open(out_file) as fh: | |
38 params = json.load(fh) | |
39 | |
40 workdir = params['output_data'][0]['extra_files_path'] | |
41 os.makedirs(workdir) | |
42 file_path = url_download('http://bigg.ucsd.edu/static/models/%s.xml' % (model_id,), workdir) | |
43 entry_name = os.path.basename(file_path) | |
44 | |
45 data_manager_json = {"data_tables": {}} | |
46 data_manager_entry = {} | |
47 data_manager_entry['value'] = model_id | |
48 data_manager_entry['name'] = entry_name | |
49 data_manager_entry['path'] = file_path | |
50 | |
51 with open(out_file, 'w') as fh: | |
52 json.dump(data_manager_json, fh, sort_keys=True) | |
53 | |
54 | |
55 parser = argparse.ArgumentParser() | |
56 | |
57 parser.add_argument('--model-id', help='Model id') | |
58 parser.add_argument('--out-file', help='JSON output file') | |
59 | |
60 args = parser.parse_args() | |
61 | |
62 download(args.model_id, args.out_file) |