Mercurial > repos > dfornika > data_manager_build_kraken2_database
changeset 0:7e43f66abe9d draft
planemo upload for repository https://github.com/dfornika/galaxytools/tree/master/data_managers/data_manager_build_kraken2_database/ commit cc42cd530378bfa3df83b08b6b41aaf2f5072b49-dirty
author | dfornika |
---|---|
date | Thu, 28 Feb 2019 02:32:38 -0500 |
parents | |
children | 32d04371ed19 |
files | data_manager/kraken2_build_standard.py data_manager/kraken2_build_standard.xml data_manager/kraken2_custom_database_builder.xml data_manager/kraken2_standard_database_builder.xml data_manager/make_json.py data_manager_conf.xml |
diffstat | 6 files changed, 288 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_manager/kraken2_build_standard.py Thu Feb 28 02:32:38 2019 -0500 @@ -0,0 +1,96 @@ +#!/usr/bin/env python + +from __future__ import print_function + +import argparse +import datetime +import errno +import json +import os +import string +import subprocess +import sys + +from pprint import pprint + +DATA_TABLE_NAME = "kraken2_databases" + +def kraken2_build_standard(data_manager_dict, kraken2_args, params, target_directory, data_table_name=DATA_TABLE_NAME): + today = datetime.date.today().isoformat() + database_name = "_".join([today, "standard"]) + args = [ + 'kraken2-build', + '--threads', str(kraken2_args["threads"]), + '--standard', + '--kmer-len', str(kraken2_args["kmer_len"]), + '--minimizer-len', str(kraken2_args["minimizer_len"]), + '--minimizer-spaces', str(kraken2_args["minimizer_spaces"]), + '--db', database_name + ] + pprint(args) + proc = subprocess.Popen(args=args, shell=False, cwd=target_directory) + return_code = proc.wait() + if return_code: + print("Error building database.", file=sys.stderr) + sys.exit( return_code ) + + data_table_entry = { + "value": database_name, + "name": database_name, + "path": database_name + } + + _add_data_table_entry(data_manager_dict, data_table_name, data_table_entry) + + +def _add_data_table_entry(data_manager_dict, data_table_name, data_table_entry): + data_manager_dict['data_tables'] = data_manager_dict.get( 'data_tables', {} ) + data_manager_dict['data_tables'][ data_table_name ] = data_manager_dict['data_tables'].get( data_table_name, [] ) + data_manager_dict['data_tables'][ data_table_name ].append( data_table_entry ) + return data_manager_dict + + +def main(): + parser = argparse.ArgumentParser() + parser.add_argument('params') + parser.add_argument( '-k', '--kmer-len', dest='kmer_len', type=int, default=35, help='kmer length' ) + parser.add_argument( '-m', '--minimizer-len', dest='minimizer_len', type=int, default=31, help='minimizer length' ) + parser.add_argument( '-s', '--minimizer-spaces', dest='minimizer_spaces', default=6, help='minimizer spaces' ) + parser.add_argument( '-t', '--threads', dest='threads', default=1, help='threads' ) + args = parser.parse_args() + + kraken2_args = { + "kmer_len": args.kmer_len, + "minimizer_len": args.minimizer_len, + "minimizer_spaces": args.minimizer_spaces, + "threads": args.threads, + } + + params = json.loads(open(args.params).read()) + pprint(params) + target_directory = params['output_data'][0]['extra_files_path'] + + try: + os.mkdir( target_directory ) + except OSError as exc: + if exc.errno == errno.EEXIST and os.path.isdir( target_directory ): + pass + else: + raise + + data_manager_dict = {} + + # build the index + kraken2_build_standard( + data_manager_dict, + kraken2_args, + params, + target_directory + ) + + # save info to json file + open(args.params, 'wb').write(json.dumps(data_manager_dict)) + + +if __name__ == "__main__": + main()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_manager/kraken2_build_standard.xml Thu Feb 28 02:32:38 2019 -0500 @@ -0,0 +1,31 @@ +<?xml version="1.0"?> +<tool id="kraken2_build_standard" name="Kraken2 Standard Database Builder (subprocess)" tool_type="manage_data" version="2.0.7_beta"> + <description>database builder</description> + <requirements> + <requirement type="package" version="2.0.7_beta">kraken2</requirement> + </requirements> + <version_command>kraken2 -version | head -n 1 | awk '{print $NF}'</version_command> + <command detect_errors="exit_code"> + <![CDATA[ + python '$__tool_directory__/kraken2_build_standard.py' + '${out_file}' + --threads \${GALAXY_SLOTS:-1} + --kmer-len ${kmer_len} + --minimizer-len ${minimizer_len} + --minimizer-spaces ${minimizer_spaces} + ]]> + </command> + <inputs> + <param name="kmer_len" type="integer" value="35" label="K-mer length in BP" /> + <param name="minimizer_len" type="integer" value="31" label="Minimizer length" /> + <param name="minimizer_spaces" type="integer" value="6" label="Minimizer spaces" /> + </inputs> + <outputs> + <data name="out_file" format="data_manager_json" /> + </outputs> + <help> + </help> + <citations> + <citation type="doi">10.1186/gb-2014-15-3-r46</citation> + </citations> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_manager/kraken2_custom_database_builder.xml Thu Feb 28 02:32:38 2019 -0500 @@ -0,0 +1,50 @@ +<?xml version="1.0"?> +<tool id="kraken2_custom_database_builder" name="Kraken2 Custom Database Builder" tool_type="manage_data" version="2.0.7_beta"> + <description>database builder</description> + <requirements> + <requirement type="package" version="2.0.7_beta">kraken2</requirement> + </requirements> + <version_command>kraken2 -version | head -n 1 | awk '{print $NF}'</version_command> + <command detect_errors="exit_code"> + <![CDATA[ + #set $library_list = str($libraries).split(',') + mkdir kraken2-database && cd kraken2-database && + kraken2-build --threads \${GALAXY_SLOTS:-1} --download-taxonomy --db '${database_name}' && + #for $library in $library_list: + #if str($library) != 'None': + kraken2-build --threads \${GALAXY_SLOTS:-1} --download-library $library --db '${database_name}' && + #end if + #end for + #for $fasta in $input_fasta: + #if str($fasta) != 'None': + kraken2-build --threads \${GALAXY_SLOTS:-1} --db '${database_name}' --add-to-library '$fasta' && + #end if + #end for + kraken2-build --threads \${GALAXY_SLOTS:-1} --db '${database_name}' --build --kmer-len ${kmer_len} --minimizer-len ${minimizer_len} && + cd .. && + python '$__tool_directory__/make_json.py' --db '${database_name}' --out '${out_file}' + ]]> + </command> + <inputs> + <param name="database_name" type="text" label="Name for this database" /> + <param name="libraries" type="select" multiple="true" label="Select partial library to download"> + <option value="archaea">Archaea</option> + <option value="bacteria">Bacteria</option> + <option value="plasmid">Plasmid</option> + <option value="viral">Viral</option> + <option value="human">Human</option> + </param> + <param name="input_fasta" type="data" format="fasta" multiple="True" optional="true" label="Select history item" /> + <param name="kmer_len" type="integer" value="35" label="K-mer length in BP" /> + <param name="minimizer_len" type="integer" value="31" label="Minimizer length" /> + <param name="minimizer_spaces" type="integer" value="6" label="Minimizer spaces" /> + </inputs> + <outputs> + <data name="out_file" format="data_manager_json" /> + </outputs> + <help> + </help> + <citations> + <citation type="doi">10.1186/gb-2014-15-3-r46</citation> + </citations> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_manager/kraken2_standard_database_builder.xml Thu Feb 28 02:32:38 2019 -0500 @@ -0,0 +1,37 @@ +<?xml version="1.0"?> +<tool id="kraken2_standard_database_builder" name="Kraken2 Standard Database Builder" tool_type="manage_data" version="2.0.7_beta"> + <description>database builder</description> + <requirements> + <requirement type="package" version="2.0.7_beta">kraken2</requirement> + </requirements> + <version_command>kraken2 -version | head -n 1 | awk '{print $NF}'</version_command> + <command detect_errors="exit_code"> + <![CDATA[ + #import datetime + #set kraken2_db_name = datetime.date.today().strftime('%Y-%m-%d') + "_standard" + mkdir kraken2-database && cd kraken2-database && + kraken2-build + --standard + --threads \${GALAXY_SLOTS:-1} + --db '${kraken2_db_name}' + --kmer-len ${kmer_len} + --minimizer-len ${minimizer_len} + --minimizer-spaces ${minimizer_spaces} && + cd .. && + python '$__tool_directory__/make_json.py' --db '${kraken2_db_name}' --out '${out_file}' + ]]> + </command> + <inputs> + <param name="kmer_len" type="integer" value="35" label="K-mer length in BP" /> + <param name="minimizer_len" type="integer" value="31" label="Minimizer length" /> + <param name="minimizer_spaces" type="integer" value="6" label="Minimizer spaces" /> + </inputs> + <outputs> + <data name="out_file" format="data_manager_json" /> + </outputs> + <help> + </help> + <citations> + <citation type="doi">10.1186/gb-2014-15-3-r46</citation> + </citations> +</tool>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_manager/make_json.py Thu Feb 28 02:32:38 2019 -0500 @@ -0,0 +1,27 @@ +import argparse +import json +import os +import shutil + + +def main(args): + data_manager_entry = {} + data_manager_entry['value'] = args.database.lower() + data_manager_entry['name'] = args.database + data_manager_entry['path'] = '.' + data_manager_json = dict(data_tables=dict(kraken2_databases=data_manager_entry)) + params = json.loads(open(args.output).read()) + target_directory = params['output_data'][0]['extra_files_path'] + os.mkdir(target_directory) + output_path = os.path.join(os.getcwd(), 'kraken2-database') + for filename in os.listdir(output_path): + shutil.move(os.path.join(output_path, filename), target_directory) + file(args.output, 'w').write(json.dumps(data_manager_json)) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Create data manager json.') + parser.add_argument('--db', dest='database', action='store', help='Database name') + parser.add_argument('--out', dest='output', action='store', help='JSON filename') + args = parser.parse_args() + main(args)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_manager_conf.xml Thu Feb 28 02:32:38 2019 -0500 @@ -0,0 +1,47 @@ +<data_managers> + <data_manager tool_file="data_manager/kraken2_custom_database_builder.xml" id="kraken2_custom_database_builder" version="1.0"> + <data_table name="kraken2_databases"> + <output> + <column name="value"/> + <column name="name"/> + <column name="path" output_ref="out_file"> + <move type="directory" relativize_symlinks="True"> + <target base="${GALAXY_DATA_MANAGER_DATA_PATH}">kraken2_database/${value}</target> + </move> + <value_translation>${GALAXY_DATA_MANAGER_DATA_PATH}/kraken2_database/${value}</value_translation> + <value_translation type="function">abspath</value_translation> + </column> + </output> + </data_table> + </data_manager> + <data_manager tool_file="data_manager/kraken2_standard_database_builder.xml" id="kraken2_standard_database_builder" version="1.0"> + <data_table name="kraken2_databases"> + <output> + <column name="value"/> + <column name="name"/> + <column name="path" output_ref="out_file"> + <move type="directory" relativize_symlinks="True"> + <target base="${GALAXY_DATA_MANAGER_DATA_PATH}">kraken2_database/${value}</target> + </move> + <value_translation>${GALAXY_DATA_MANAGER_DATA_PATH}/kraken2_database/${value}</value_translation> + <value_translation type="function">abspath</value_translation> + </column> + </output> + </data_table> + </data_manager> + <data_manager tool_file="data_manager/kraken2_build_standard.xml" id="kraken2_build_standard" version="1.0"> + <data_table name="kraken2_databases"> + <output> + <column name="value"/> + <column name="name"/> + <column name="path" output_ref="out_file"> + <move type="directory" relativize_symlinks="True"> + <target base="${GALAXY_DATA_MANAGER_DATA_PATH}">kraken2_database/${value}</target> + </move> + <value_translation>${GALAXY_DATA_MANAGER_DATA_PATH}/kraken2_database/${value}</value_translation> + <value_translation type="function">abspath</value_translation> + </column> + </output> + </data_table> + </data_manager> +</data_managers>