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