comparison data_manager/bigg_model_sbml_fetcher.py @ 11:2bb0d8ca1710 draft

"planemo upload for repository https://github.com/brsynth/synbiocad-galaxy-wrappers commit 47caed1dd87e80ae226fabb584e9d63d7c86a436-dirty"
author ggricourt
date Thu, 24 Feb 2022 13:01:50 +0000
parents a9f72fd191b5
children e339b8d84de0
comparison
equal deleted inserted replaced
10:a9f72fd191b5 11:2bb0d8ca1710
38 except Exception as e: 38 except Exception as e:
39 sys.exit(str(e)) 39 sys.exit(str(e))
40 return data 40 return data
41 41
42 42
43 def get_model_organism(model_id): 43 def download_entries(model_ids, id2org, workdir):
44 data = url_json(MODEL_DETAIL_URL + model_id) 44 for ix, model_id in enumerate(model_ids):
45 org = data.get("organism", "")
46 if org is None:
47 org = ""
48 res = "%s - %s" % (model_id, org)
49 return res
50
51
52 def download_entries(model_ids, workdir):
53 for model_id in model_ids:
54 model_filename = model_id + ".xml" 45 model_filename = model_id + ".xml"
55 path = os.path.abspath(os.path.join(workdir, model_filename)) 46 path = os.path.abspath(os.path.join(workdir, model_filename))
56 47
57 url_download(MODEL_URL + model_filename, path) 48 url_download(MODEL_URL + model_filename, path)
58 data_manager_entry = {} 49 data_manager_entry = {}
59 data_manager_entry["value"] = model_id 50 data_manager_entry["value"] = model_id
60 data_manager_entry["name"] = get_model_organism(model_id) 51 data_manager_entry["name"] = id2org[model_id]
61 data_manager_entry["path"] = path 52 data_manager_entry["path"] = path
62 53
63 # Make sure that less than 10 requests per second, as required by host (http://bigg.ucsd.edu/data_access) 54 # Make sure that less than 10 requests per second, as required by host (http://bigg.ucsd.edu/data_access)
64 time.sleep(1) 55 if ix % 5 == 0:
56 time.sleep(1)
65 yield data_manager_entry 57 yield data_manager_entry
66 58
67 59
68 if __name__ == "__main__": 60 if __name__ == "__main__":
69 parser = argparse.ArgumentParser() 61 parser = argparse.ArgumentParser()
79 params = json.load(fh) 71 params = json.load(fh)
80 72
81 workdir = params["output_data"][0]["extra_files_path"] 73 workdir = params["output_data"][0]["extra_files_path"]
82 os.makedirs(workdir) 74 os.makedirs(workdir)
83 75
76 # Load models and models metadata.
77 models = url_json(MODEL_DETAIL_URL)
78 id2org = {}
79 for result in models.get("results", []):
80 ident = result["bigg_id"]
81 ident = ident.replace(" ", "")
82 id2org[ident] = result["organism"]
83
84 # Select model_ids.
84 model_ids = [] 85 model_ids = []
85 if args.model_id: 86 if args.model_id:
87 if args.model_id not in id2org.keys():
88 sys.exit("Model id: %s is not available" % (args.model_id,))
86 model_ids.append(args.model_id) 89 model_ids.append(args.model_id)
87 else: 90 else:
88 data = url_json(MODEL_DETAIL_URL) 91 model_ids.extend(list(id2org.keys()))
89 for result in data.get("results", []):
90 model_ids.append(result.get("bigg_id"))
91 92
92 entries = list(download_entries(model_ids, workdir)) 93 # Download.
94 entries = list(download_entries(model_ids, id2org, workdir))
93 95
94 # Write data. 96 # Write data.
95 data_manager_json["data_tables"]["bigg_model_sbml"] = entries 97 data_manager_json["data_tables"]["bigg_model_sbml"] = entries
96 with open(args.out_file, "w") as fh: 98 with open(args.out_file, "w") as fh:
97 json.dump(data_manager_json, fh, sort_keys=True) 99 json.dump(data_manager_json, fh, sort_keys=True)