Mercurial > repos > iuc > data_manager_pharokka
comparison data_manager/data_manager_fetch_pharokka_db.py @ 0:3b3daabcc63e draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_pharokka_database_fetcher/ commit 31e99045208605780b3fe4b89a999137adcabe13
| author | iuc |
|---|---|
| date | Thu, 20 Apr 2023 07:51:13 +0000 |
| parents | |
| children | bba8135dc024 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:3b3daabcc63e |
|---|---|
| 1 #!/usr/bin/env python | |
| 2 | |
| 3 import argparse | |
| 4 import json | |
| 5 import os | |
| 6 import subprocess | |
| 7 import sys | |
| 8 from datetime import datetime | |
| 9 | |
| 10 | |
| 11 def main(): | |
| 12 # Parse Command Line | |
| 13 parser = argparse.ArgumentParser(description="Create data manager JSON.") | |
| 14 parser.add_argument("--out", dest="output", action="store", help="JSON filename") | |
| 15 parser.add_argument("--version", dest="version", action="store", help="Version of the DB") | |
| 16 parser.add_argument( | |
| 17 "--test", | |
| 18 action="store_true", | |
| 19 help="option to test the script with an lighted database", | |
| 20 ) | |
| 21 | |
| 22 args = parser.parse_args() | |
| 23 | |
| 24 # the output file of a DM is a json containing args that can be used by the DM | |
| 25 # most tools mainly use these args to find the extra_files_path for the DM, which can be used | |
| 26 # to store the DB data | |
| 27 with open(args.output) as fh: | |
| 28 params = json.load(fh) | |
| 29 | |
| 30 workdir = params["output_data"][0]["extra_files_path"] | |
| 31 os.mkdir(workdir) | |
| 32 | |
| 33 time = datetime.utcnow().strftime("%Y-%m-%dT%H%M%SZ") | |
| 34 db_value = "db_from_{0}".format(time) | |
| 35 db_path = os.path.join(workdir, db_value) | |
| 36 | |
| 37 # create DB | |
| 38 if args.test: # the test only checks that the pharokka download script is available and copies the test DB | |
| 39 | |
| 40 # check if install_databases.py is there | |
| 41 command_args = ["install_databases.py", "-h"] | |
| 42 proc = subprocess.Popen(args=command_args, shell=False) | |
| 43 return_code = proc.wait() | |
| 44 if return_code: | |
| 45 print("Error downloading Pharokka database.", file=sys.stderr) | |
| 46 sys.exit(return_code) | |
| 47 | |
| 48 # copy the test DB | |
| 49 test_db_path = os.path.join(os.path.dirname(os.path.realpath(__file__)), "subset_pharokka_db") | |
| 50 command_args = ["cp", "-r", test_db_path, db_path] | |
| 51 else: | |
| 52 command_args = ["install_databases.py", "-o", db_path] | |
| 53 | |
| 54 proc = subprocess.Popen(args=command_args, shell=False) | |
| 55 return_code = proc.wait() | |
| 56 if return_code: | |
| 57 print("Error downloading Pharokka database.", file=sys.stderr) | |
| 58 sys.exit(return_code) | |
| 59 | |
| 60 # Update Data Manager JSON and write to file | |
| 61 data_manager_entry = { | |
| 62 "data_tables": { | |
| 63 "pharokka_db": { | |
| 64 "value": db_value, | |
| 65 "dbkey": db_value, | |
| 66 "version": args.version, | |
| 67 "name": f"Pharokka DB version {args.version} downloaded at {datetime.now()}", | |
| 68 "path": db_path, | |
| 69 } | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 with open(os.path.join(args.output), "w+") as fh: | |
| 74 json.dump(data_manager_entry, fh, sort_keys=True) | |
| 75 | |
| 76 | |
| 77 if __name__ == "__main__": | |
| 78 main() |
