changeset 2:cf0cda80b659 draft

planemo upload for repository https://github.com/dfornika/galaxytools/tree/master/data_managers/data_manager_build_kraken2_database/ commit f05f93ee024df8b38efa1c92db9785d52c726f85-dirty
author dfornika
date Thu, 28 Feb 2019 18:18:01 -0500
parents 32d04371ed19
children b03007bc0f05
files data_manager/kraken2_build_custom.py data_manager/kraken2_build_custom.xml
diffstat 2 files changed, 150 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data_manager/kraken2_build_custom.py	Thu Feb 28 18:18:01 2019 -0500
@@ -0,0 +1,117 @@
+#!/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 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(data_manager_dict, kraken2_args, params, target_directory, data_table_name=DATA_TABLE_NAME):
+    today = datetime.date.today().isoformat()
+    database_name = "_".join([today, "custom"])
+    
+    args = [
+        'kraken2-build',
+        '--threads', str(kraken2_args["threads"]),
+        '--download-taxonomy',
+        '--db', database_name
+    ]
+    
+    run(args, target_directory)
+    
+    args = [
+        'kraken2-build',
+        '--threads', str(kraken2_args["threads"]),
+        '--add-to-library', kraken2_args["fasta"],
+        '--db', database_name
+    ]
+    
+    run(args, target_directory)
+        
+    args = [
+        'kraken2-build',
+        '--threads', str(kraken2_args["threads"]),
+        '--clean',
+        '--db', database_name
+    ]
+
+    run(args, target_directory)
+    
+    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,
+        "fasta": args.fasta,
+        "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(
+        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_custom.xml	Thu Feb 28 18:18:01 2019 -0500
@@ -0,0 +1,33 @@
+<?xml version="1.0"?>
+<tool id="kraken2_build_custom" name="Kraken2 Custom 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}
+          --fasta ${input_fasta}
+          --kmer-len ${kmer_len}
+          --minimizer-len ${minimizer_len}
+          --minimizer-spaces ${minimizer_spaces}
+    ]]>
+    </command>
+    <inputs>
+        <param name="input_fasta" type="data" format="fasta" multiple="False" 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>