Mercurial > repos > pimarin > data_manager_amrfinderplus
comparison data_manager/amrfinderplus_data_manager.py @ 2:f1ec3a39ffec draft default tip
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_amrfinderplus commit 25a0413337c540dc9f26bc7ee097d493f0d148ca-dirty
| author | pimarin |
|---|---|
| date | Mon, 14 Nov 2022 14:22:02 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 1:254479b50f86 | 2:f1ec3a39ffec |
|---|---|
| 1 import argparse | |
| 2 import json | |
| 3 import os | |
| 4 import re | |
| 5 import subprocess as sp | |
| 6 | |
| 7 from datetime import datetime | |
| 8 from pathlib import Path | |
| 9 | |
| 10 | |
| 11 class GetDataManager: | |
| 12 | |
| 13 def __init__(self): | |
| 14 self.data_table_name = "amrfinderplus_database" | |
| 15 self._db_name = "amrfinderplus-db" | |
| 16 self._db_path = Path().absolute() | |
| 17 self._today = datetime.now().strftime("%Y-%m-%d_%H:%M") | |
| 18 | |
| 19 def get_data_table_format(self): | |
| 20 """ | |
| 21 Skeleton of a data_table format | |
| 22 return: a data table formatted for json output | |
| 23 """ | |
| 24 self.data_table_entry = { | |
| 25 "data_tables": { | |
| 26 self.data_table_name: {} | |
| 27 } | |
| 28 } | |
| 29 return self.data_table_entry | |
| 30 | |
| 31 def get_data_manager(self, amrfinderplus_version): | |
| 32 self.amrfinderplus_table_list = self.get_data_table_format() | |
| 33 | |
| 34 data_info = dict(value=self._today, | |
| 35 name=amrfinderplus_version, | |
| 36 path=self._db_name) | |
| 37 self.amrfinderplus_table_list["data_tables"][self.data_table_name] = [data_info] | |
| 38 return self.amrfinderplus_table_list | |
| 39 | |
| 40 def update_amrfinderplus_db(self): | |
| 41 amrfinderplus_db_path = Path(self._db_path).joinpath(self._db_name) | |
| 42 cmd = [ | |
| 43 'amrfinder_update', | |
| 44 '--database', str(amrfinderplus_db_path), | |
| 45 '--force_update' | |
| 46 ] | |
| 47 print(cmd) | |
| 48 proc = sp.run( | |
| 49 cmd, | |
| 50 stdout=sp.PIPE, | |
| 51 stderr=sp.PIPE, | |
| 52 universal_newlines=True | |
| 53 ) | |
| 54 if (proc.returncode != 0): | |
| 55 print( | |
| 56 f"ERROR: AMRFinderPlus failed! command: 'amrfinder_update --force_update --database {amrfinderplus_db_path}', error code: {proc.returncode}") | |
| 57 else: | |
| 58 return amrfinderplus_db_path | |
| 59 | |
| 60 def get_amrfinderplus_version(self, amrfinderplus_path): | |
| 61 version_file = Path(f'{amrfinderplus_path}/latest/version.txt') | |
| 62 with open(version_file, "r") as version: | |
| 63 version_value = version.read() | |
| 64 version_value = re.sub("\n", "", version_value) | |
| 65 return version_value | |
| 66 | |
| 67 def parse_arguments(self): | |
| 68 # parse options and arguments | |
| 69 arg_parser = argparse.ArgumentParser() | |
| 70 arg_parser.add_argument("data_manager_json") | |
| 71 arg_parser.add_argument("-t", "--test", action='store_true', | |
| 72 help="option to test the script with an lighted database") | |
| 73 return arg_parser.parse_args() | |
| 74 | |
| 75 def read_json_input_file(self, json_file_path): | |
| 76 with open(json_file_path) as fh: | |
| 77 params = json.load(fh) | |
| 78 target_dir = params['output_data'][0]['extra_files_path'] | |
| 79 os.makedirs(target_dir) | |
| 80 return Path(target_dir) | |
| 81 | |
| 82 def write_json_infos(self, json_file_path, data_manager_infos): | |
| 83 with open(json_file_path, 'w') as fh: | |
| 84 json.dump(data_manager_infos, fh, sort_keys=True) | |
| 85 | |
| 86 | |
| 87 def main(): | |
| 88 # init the class | |
| 89 amrfinderplus_download = GetDataManager() | |
| 90 # import the arguments | |
| 91 all_args = amrfinderplus_download.parse_arguments() | |
| 92 # read the json input from galaxy to define the db path | |
| 93 path_to_download = amrfinderplus_download.read_json_input_file(json_file_path=all_args.data_manager_json) | |
| 94 # change the path to th json information | |
| 95 amrfinderplus_download._db_path = path_to_download | |
| 96 # download the last amrfinderplus database | |
| 97 amrfinder_output = amrfinderplus_download.update_amrfinderplus_db() | |
| 98 # extract the version number of the database | |
| 99 amrfinder_version = amrfinderplus_download.get_amrfinderplus_version(amrfinder_output) | |
| 100 # make a dic with database information | |
| 101 amrfinderplus_json_output = amrfinderplus_download.get_data_manager(amrfinder_version) | |
| 102 amrfinderplus_download.write_json_infos(json_file_path=all_args.data_manager_json, | |
| 103 data_manager_infos=amrfinderplus_json_output) | |
| 104 | |
| 105 | |
| 106 if __name__ == '__main__': | |
| 107 main() |
