Mercurial > repos > pimarin > amrfinderplus_data_manager_build
changeset 0:02a159f061a1 draft
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 15:05:09 +0000 |
parents | |
children | 7c2a5aeb5ee4 |
files | data_manager/data_manager_build_amrfinderplus.py data_manager/data_manager_build_amrfinderplus.xml data_manager/macro.xml data_manager_conf.xml test-data/amrfinderplus.loc test-data/amrfinderplus_test_data_manager.json tool-data/amrfinderplus.loc tool_data_table_conf.xml.sample tool_data_table_conf.xml.test |
diffstat | 8 files changed, 199 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_manager/data_manager_build_amrfinderplus.py Mon Nov 14 15:05:09 2022 +0000 @@ -0,0 +1,107 @@ +import argparse +import json +import os +import re +import subprocess as sp + +from datetime import datetime +from pathlib import Path + + +class GetDataManager: + + def __init__(self): + self.data_table_name = "amrfinderplus_database" + self._db_name = "amrfinderplus-db" + self._db_path = Path().absolute() + self._today = datetime.now().strftime("%Y-%m-%d_%H:%M") + + def get_data_table_format(self): + """ + Skeleton of a data_table format + return: a data table formatted for json output + """ + self.data_table_entry = { + "data_tables": { + self.data_table_name: {} + } + } + return self.data_table_entry + + def get_data_manager(self, amrfinderplus_version): + self.amrfinderplus_table_list = self.get_data_table_format() + + data_info = dict(value=self._today, + name=amrfinderplus_version, + path=self._db_name) + self.amrfinderplus_table_list["data_tables"][self.data_table_name] = [data_info] + return self.amrfinderplus_table_list + + def update_amrfinderplus_db(self): + amrfinderplus_db_path = Path(self._db_path).joinpath(self._db_name) + cmd = [ + 'amrfinder_update', + '--database', str(amrfinderplus_db_path), + '--force_update' + ] + print(cmd) + proc = sp.run( + cmd, + stdout=sp.PIPE, + stderr=sp.PIPE, + universal_newlines=True + ) + if (proc.returncode != 0): + print( + f"ERROR: AMRFinderPlus failed! command: 'amrfinder_update --force_update --database {amrfinderplus_db_path}', error code: {proc.returncode}") + else: + return amrfinderplus_db_path + + def get_amrfinderplus_version(self, amrfinderplus_path): + version_file = Path(f'{amrfinderplus_path}/latest/version.txt') + with open(version_file, "r") as version: + version_value = version.read() + version_value = re.sub("\n", "", version_value) + return version_value + + def parse_arguments(self): + # parse options and arguments + arg_parser = argparse.ArgumentParser() + arg_parser.add_argument("data_manager_json") + arg_parser.add_argument("-t", "--test", action='store_true', + help="option to test the script with an lighted database") + return arg_parser.parse_args() + + def read_json_input_file(self, json_file_path): + with open(json_file_path) as fh: + params = json.load(fh) + target_dir = params['output_data'][0]['extra_files_path'] + os.makedirs(target_dir) + return Path(target_dir) + + def write_json_infos(self, json_file_path, data_manager_infos): + with open(json_file_path, 'w') as fh: + json.dump(data_manager_infos, fh, sort_keys=True) + + +def main(): + # init the class + amrfinderplus_download = GetDataManager() + # import the arguments + all_args = amrfinderplus_download.parse_arguments() + # read the json input from galaxy to define the db path + path_to_download = amrfinderplus_download.read_json_input_file(json_file_path=all_args.data_manager_json) + # change the path to th json information + amrfinderplus_download._db_path = path_to_download + # download the last amrfinderplus database + amrfinder_output = amrfinderplus_download.update_amrfinderplus_db() + # extract the version number of the database + amrfinder_version = amrfinderplus_download.get_amrfinderplus_version(amrfinder_output) + # make a dic with database information + amrfinderplus_json_output = amrfinderplus_download.get_data_manager(amrfinder_version) + amrfinderplus_download.write_json_infos(json_file_path=all_args.data_manager_json, + data_manager_infos=amrfinderplus_json_output) + + +if __name__ == '__main__': + main()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_manager/data_manager_build_amrfinderplus.xml Mon Nov 14 15:05:09 2022 +0000 @@ -0,0 +1,32 @@ +<tool id="data_manager_build_amrfinderplus" name="amrfinderplus_datamanager" tool_type="manage_data" version="@TOOL_VERSION@+galaxy@VERSION_SUFFIX@" profile="@PROFILE@"> + <description>AMRfinderplus database builder</description> + <macros> + <import>macro.xml</import> + </macros> + <expand macro="requirements"/> + <command detect_errors="exit_code"> + <![CDATA[ + python '$__tool_directory__/data_manager_build_amrfinderplus.py' + '$output_file' + ]]></command> + <inputs> + <param name="database_select" type="select" label="Database version"> + <option value="latest" selected="true">Latest available version</option> + </param> + </inputs> + <outputs> + <data name="output_file" format="data_manager_json"/> + </outputs> + <tests> + <!-- Test download --> + <test expect_num_outputs="1"> + <output name="output_file" value="amrfinderplus_test_data_manager.json" /> + </test> + </tests> + <help><![CDATA[ + Download the latest version of amrfinderplus database from the NCBI + ]]></help> + <citations> + <citation type="doi">10.1038/s41598-021-91456-0</citation> + </citations> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_manager/macro.xml Mon Nov 14 15:05:09 2022 +0000 @@ -0,0 +1,13 @@ +<?xml version="1.0"?> +<macros> + <token name="@TOOL_VERSION@">3.10.45</token> + <token name="@PYTHON_VERSION@">3.10.6</token> + <token name="@VERSION_SUFFIX@">0</token> + <token name="@PROFILE@">21.05</token> + <xml name="requirements"> + <requirements> + <requirement type="package" version="@TOOL_VERSION@">ncbi-amrfinderplus</requirement> + <requirement type="package" version="@PYTHON_VERSION@">python</requirement> + </requirements> + </xml> +</macros> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_manager_conf.xml Mon Nov 14 15:05:09 2022 +0000 @@ -0,0 +1,19 @@ +<?xml version="1.0"?> +<data_managers> + <data_manager tool_file="data_manager/data_manager_build_amrfinderplus.xml" id="data_manager_build_amrfinderplus" version="@TOOL_VERSION@"> + <data_table name="amrfinderplus_database"> + <output> + <column name="value" /> + <column name="name" /> + <column name="path" output_ref="out_file"> + <move type="directory" relativize_symlinks="True"> + <source>${path}</source> + <target base="${GALAXY_DATA_MANAGER_DATA_PATH}">amrfinderplus/${value}</target> + </move> + <value_translation>${GALAXY_DATA_MANAGER_DATA_PATH}/amrfinderplus/${value}</value_translation> + <value_translation type="function">abspath</value_translation> + </column> + </output> + </data_table> + </data_manager> +</data_managers>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/test-data/amrfinderplus.loc Mon Nov 14 15:05:09 2022 +0000 @@ -0,0 +1,7 @@ +# this is a tab separated file describing the location of amrfinderplus database +# +# the columns are: +# value, name, path +# +# for example +2022-11-10_15:48 2022-10-11.2 ${__HERE__} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool-data/amrfinderplus.loc Mon Nov 14 15:05:09 2022 +0000 @@ -0,0 +1,7 @@ +# this is a tab separated file describing the location of amrfinderplus database +# +# the columns are: +# value, name, path +# +# for example +2022-11-10_15:48 2022-10-11.2 ${__HERE__} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool_data_table_conf.xml.sample Mon Nov 14 15:05:09 2022 +0000 @@ -0,0 +1,7 @@ +<tables> + <!-- Locations of amrfinderplus database in the required format --> + <table name="amrfinderplus_database" comment_char="#"> + <columns>value, name, path</columns> + <file path="tool-data/amrfinderplus.loc" /> + </table> +</tables> \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tool_data_table_conf.xml.test Mon Nov 14 15:05:09 2022 +0000 @@ -0,0 +1,7 @@ +<tables> + <!-- Locations of amrfinderplus database in the required format --> + <table name="amrfinderplus_database" comment_char="#"> + <columns>value, name, path</columns> + <file path="${__HERE__}/test-data/amrfinderplus.loc"/> + </table> +</tables> \ No newline at end of file