Mercurial > repos > iuc > data_manager_fetch_plasmidfinder
comparison data_manager/plasmidfinder_fetch_database.py @ 0:b56071bdecad draft
planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_fetch_plasmidfinder commit f6d40bf94e015c2690e97c4b3533517b812f3bfb
| author | iuc |
|---|---|
| date | Thu, 15 Jun 2023 09:14:08 +0000 |
| parents | |
| children | 3542cd220bc3 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:b56071bdecad |
|---|---|
| 1 import argparse | |
| 2 import json | |
| 3 import os | |
| 4 import time | |
| 5 from pathlib import Path | |
| 6 | |
| 7 | |
| 8 import git | |
| 9 | |
| 10 | |
| 11 class GetPlasmidfinderDataManager: | |
| 12 """ | |
| 13 Create the json file with database information for galaxy data manager | |
| 14 """ | |
| 15 | |
| 16 def __init__(self, | |
| 17 plasmidfinder_database="plasmidfinder_database", | |
| 18 db_name="plasmidfinder_database", | |
| 19 plasmidfinder_version="latest"): | |
| 20 self.data_table_name = plasmidfinder_database | |
| 21 self._db_name = db_name | |
| 22 self._plasmidfinder_version = plasmidfinder_version | |
| 23 self._plasmidfinder_date_version = None | |
| 24 self.data_table_entry = None | |
| 25 self.plasmidfinder_table_list = None | |
| 26 self._commit_number = None | |
| 27 | |
| 28 def get_data_table_format(self): | |
| 29 """ | |
| 30 Skeleton of a data_table format | |
| 31 return: a data table formatted for json output | |
| 32 """ | |
| 33 self.data_table_entry = { | |
| 34 "data_tables": { | |
| 35 self.data_table_name: {} | |
| 36 } | |
| 37 } | |
| 38 return self.data_table_entry | |
| 39 | |
| 40 def get_data_manager(self): | |
| 41 """ | |
| 42 Create the empty data table format and add all the information into | |
| 43 Commit number is added if latest is required instead of version number | |
| 44 return: The data table with database information | |
| 45 """ | |
| 46 self.plasmidfinder_table_list = self.get_data_table_format() | |
| 47 if self._plasmidfinder_version == "latest": | |
| 48 version_value = self._commit_number | |
| 49 else: | |
| 50 version_value = self._plasmidfinder_version | |
| 51 plasmidfinder_value = f"plasmidfinder_{self._commit_number}" \ | |
| 52 f"_{self._plasmidfinder_date_version}" | |
| 53 plasmidfinder_name = f"{version_value}" \ | |
| 54 f"_{self._plasmidfinder_date_version}" | |
| 55 data_info = dict(value=plasmidfinder_value, | |
| 56 name=plasmidfinder_name, | |
| 57 date=self._plasmidfinder_date_version, | |
| 58 path=self._db_name) | |
| 59 self.plasmidfinder_table_list["data_tables"][self.data_table_name] = [data_info] | |
| 60 return self.plasmidfinder_table_list | |
| 61 | |
| 62 | |
| 63 class DownloadPlasmidfinderDatabase(GetPlasmidfinderDataManager): | |
| 64 """ | |
| 65 Download the plasmidfinder database from the bitbucket repository. | |
| 66 Build the data manager info for galaxy | |
| 67 """ | |
| 68 | |
| 69 def __init__(self, | |
| 70 output_dir=Path.cwd(), | |
| 71 plasmidfinder_url="https://bitbucket.org/genomicepidemiology/plasmidfinder_db/src/master", | |
| 72 db_name="plasmidfinder_database", | |
| 73 db_tmp="tmp_database", | |
| 74 plasmidfinder_version="latest", | |
| 75 json_file_path=None, | |
| 76 date_version=None): | |
| 77 | |
| 78 super().__init__() | |
| 79 self.json_file_path = json_file_path | |
| 80 self._output_dir = output_dir | |
| 81 self._plasmidfinder_url = plasmidfinder_url | |
| 82 self._temporary_folder = db_tmp | |
| 83 self._db_name = db_name | |
| 84 self._db_name_tar = f'{db_name}.gz' | |
| 85 self._plasmidfinder_version = plasmidfinder_version | |
| 86 self._plasmidfinder_date_version = date_version | |
| 87 self._commit_number = None | |
| 88 | |
| 89 def git_clone(self): | |
| 90 git.Repo.clone_from(url=self._plasmidfinder_url, to_path=self._output_dir) | |
| 91 self._plasmidfinder_repository = git.Repo(path=self._output_dir) | |
| 92 | |
| 93 def get_commit_number(self): | |
| 94 sha = self._plasmidfinder_repository.head.commit.hexsha | |
| 95 short_sha = self._plasmidfinder_repository.git.rev_parse(sha, short=7) | |
| 96 self._commit_number = short_sha | |
| 97 | |
| 98 def get_commit_date(self): | |
| 99 self._plasmidfinder_date_version = time.strftime("%Y_%m_%d", time.gmtime(self._plasmidfinder_repository.head.commit.committed_date)) | |
| 100 | |
| 101 def download_database(self): | |
| 102 """ | |
| 103 Download the plasmidfinder database using git lib | |
| 104 Extract commit and commit date | |
| 105 """ | |
| 106 self._output_dir = Path(self._output_dir) | |
| 107 self.git_clone() | |
| 108 if self._plasmidfinder_version != "latest": | |
| 109 self._plasmidfinder_repository.git.checkout(self._plasmidfinder_version) | |
| 110 self.get_commit_number() | |
| 111 self.get_commit_date() | |
| 112 | |
| 113 def read_json_input_file(self): | |
| 114 """ | |
| 115 Import the json file | |
| 116 """ | |
| 117 with open(self.json_file_path) as fh: | |
| 118 params = json.load(fh) | |
| 119 target_dir = params['output_data'][0]['extra_files_path'] | |
| 120 os.makedirs(target_dir) | |
| 121 self._output_dir = target_dir | |
| 122 | |
| 123 def write_json_infos(self): | |
| 124 """ | |
| 125 Write in the imported json file | |
| 126 """ | |
| 127 with open(self.json_file_path, 'w') as fh: | |
| 128 json.dump(self.get_data_manager(), fh, sort_keys=True) | |
| 129 | |
| 130 | |
| 131 def parse_arguments(): | |
| 132 """ | |
| 133 List of arguments provided by the user | |
| 134 return: parsed arguments | |
| 135 """ | |
| 136 # parse options and arguments | |
| 137 arg_parser = argparse.ArgumentParser() | |
| 138 arg_parser.add_argument("data_manager_json", | |
| 139 help="json file from galaxy") | |
| 140 arg_parser.add_argument("-v", "--db_version", | |
| 141 help="version of the plasmidfinder (latest or 2.1)") | |
| 142 return arg_parser.parse_args() | |
| 143 | |
| 144 | |
| 145 def main(): | |
| 146 all_args = parse_arguments() | |
| 147 plasmidfinder_download = DownloadPlasmidfinderDatabase(json_file_path=all_args.data_manager_json, plasmidfinder_version=all_args.db_version) | |
| 148 plasmidfinder_download.read_json_input_file() | |
| 149 plasmidfinder_download.download_database() | |
| 150 plasmidfinder_download.write_json_infos() | |
| 151 | |
| 152 | |
| 153 if __name__ == '__main__': | |
| 154 main() |
