Mercurial > repos > dfornika > data_manager_build_kraken2_database
changeset 11:1dc93ae264e6 draft
planemo upload for repository https://github.com/dfornika/galaxytools/tree/master/data_managers/data_manager_build_kraken2_database/ commit 70494d589aa1990618ebc7a895e91477d41c3203-dirty
author | dfornika |
---|---|
date | Tue, 05 Mar 2019 14:33:23 -0500 |
parents | d0b9df6c21dd |
children | 039a65ff445d |
files | data_manager/kraken2_build_minikraken.py data_manager/kraken2_build_minikraken.xml data_manager/kraken2_build_special.py |
diffstat | 3 files changed, 196 insertions(+), 10 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_manager/kraken2_build_minikraken.py Tue Mar 05 14:33:23 2019 -0500 @@ -0,0 +1,130 @@ +#!/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 + + +DATA_TABLE_NAME = "kraken2_databases" + +def run(args, cwd): + proc = subprocess.Popen(args=args, shell=False, cwd=cwd) + return_code = proc.wait() + if return_code: + print("Error building database.", file=sys.stderr) + sys.exit( return_code ) + +def kraken2_build_standard(data_manager_dict, kraken2_args, target_directory, data_table_name=DATA_TABLE_NAME): + + now = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H%M%SZ") + + special_database_names = { + "rdp": "RDP", + "greengenes": "Greengenes", + "silva": "Silva", + } + + database_value = "_".join([ + now, + kraken2_args["special"], + "kmer-len", str(kraken2_args["kmer_len"]), + "minimizer-len", str(kraken2_args["minimizer_len"]), + "minimizer-spaces", str(kraken2_args["minimizer_spaces"]), + ]) + + database_name = " ".join([ + special_database_names[kraken2_args["special"]], + "(Created:", + now + "," + "kmer-len=" + str(kraken2_args["kmer_len"]) + ",", + "minimizer-len=" + str(kraken2_args["minimizer_len"]) + ",", + "minimizer-spaces=" + str(kraken2_args["minimizer_spaces"]) + ")", + ]) + + database_path = database_value + + args = [ + '--threads', str(kraken2_args["threads"]), + '--special', kraken2_args["special"], + '--kmer-len', str(kraken2_args["kmer_len"]), + '--minimizer-len', str(kraken2_args["minimizer_len"]), + '--minimizer-spaces', str(kraken2_args["minimizer_spaces"]), + '--db', database_path + ] + + run(['kraken2-build'] + args, target_directory) + + args = [ + '--threads', str(kraken2_args["threads"]), + '--clean', + '--db', database_path + ] + + run(['kraken2-build'] + args, target_directory) + + data_table_entry = { + "value": database_value, + "name": database_name, + "path": database_path, + } + + _add_data_table_entry(data_manager_dict, data_table_entry) + + +def _add_data_table_entry(data_manager_dict, data_table_entry, data_table_name=DATA_TABLE_NAME): + 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('data_manager_json') + parser.add_argument( '-b', '--db-type', dest='db_type', help='database type (one of: silva, rdp, greengenes)' ) + 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 = { + "special": args.db_type, + "kmer_len": args.kmer_len, + "minimizer_len": args.minimizer_len, + "minimizer_spaces": args.minimizer_spaces, + "threads": args.threads, + } + + data_manager_input = json.loads(open(args.data_manager_json).read()) + + target_directory = data_manager_input['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_output = {} + + kraken2_build_standard( + data_manager_output, + kraken2_args, + target_directory, + ) + + open(args.data_manager_json, 'wb').write(json.dumps(data_manager_output)) + + +if __name__ == "__main__": + main()
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_manager/kraken2_build_minikraken.xml Tue Mar 05 14:33:23 2019 -0500 @@ -0,0 +1,37 @@ +<?xml version="1.0"?> +<tool id="kraken2_build_special" name="Kraken2 Special 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[ + python '$__tool_directory__/kraken2_build_special.py' + '${out_file}' + --db-type ${db_type} + --threads \${GALAXY_SLOTS:-1} + --kmer-len ${kmer_len} + --minimizer-len ${minimizer_len} + --minimizer-spaces ${minimizer_spaces} + ]]> + </command> + <inputs> + <param name="db_type" type="select" multiple="false" label="Select database to build"> + <option value="greengenes">Greengenes</option> + <option value="silva">Silva</option> + <option value="rdp">RDP</option> + </param> + <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>
--- a/data_manager/kraken2_build_special.py Mon Mar 04 20:31:23 2019 -0500 +++ b/data_manager/kraken2_build_special.py Tue Mar 05 14:33:23 2019 -0500 @@ -22,14 +22,33 @@ sys.exit( return_code ) def kraken2_build_standard(data_manager_dict, kraken2_args, target_directory, data_table_name=DATA_TABLE_NAME): - today = datetime.date.today().isoformat() - database_name = "_".join([ - today, + + now = datetime.datetime.utcnow().strftime("%Y-%m-%dT%H%M%SZ") + + special_database_names = { + "rdp": "RDP", + "greengenes": "Greengenes", + "silva": "Silva", + } + + database_value = "_".join([ + now, kraken2_args["special"], - "kmer-len=" + str(kraken2_args["kmer_len"]), - "minimizer-len=" + str(kraken2_args["minimizer_len"]), - "minimizer-spaces=" + str(kraken2_args["minimizer_spaces"]), + "kmer-len", str(kraken2_args["kmer_len"]), + "minimizer-len", str(kraken2_args["minimizer_len"]), + "minimizer-spaces", str(kraken2_args["minimizer_spaces"]), ]) + + database_name = " ".join([ + special_database_names[kraken2_args["special"]], + "(Created:", + now + "," + "kmer-len=" + str(kraken2_args["kmer_len"]) + ",", + "minimizer-len=" + str(kraken2_args["minimizer_len"]) + ",", + "minimizer-spaces=" + str(kraken2_args["minimizer_spaces"]) + ")", + ]) + + database_path = database_value args = [ '--threads', str(kraken2_args["threads"]), @@ -37,7 +56,7 @@ '--kmer-len', str(kraken2_args["kmer_len"]), '--minimizer-len', str(kraken2_args["minimizer_len"]), '--minimizer-spaces', str(kraken2_args["minimizer_spaces"]), - '--db', database_name + '--db', database_path ] run(['kraken2-build'] + args, target_directory) @@ -45,15 +64,15 @@ args = [ '--threads', str(kraken2_args["threads"]), '--clean', - '--db', database_name + '--db', database_path ] run(['kraken2-build'] + args, target_directory) data_table_entry = { - "value": database_name, + "value": database_value, "name": database_name, - "path": database_name + "path": database_path, } _add_data_table_entry(data_manager_dict, data_table_entry)