changeset 0:1a5151add645 draft

planemo upload for repository https://github.com/ASaiM/galaxytools/tree/master/data_managers/data_manager_metaphlan2_database_downloader commit d474c6ecde051fa66db3635ba28bbbf28623cfdc-dirty
author bebatut
date Tue, 21 Feb 2017 04:17:13 -0500
parents
children 0951afb2c270
files data_manager/data_manager_metaphlan2_download.py data_manager/data_manager_metaphlan2_download.xml data_manager_conf.xml shed.yml tool-data/metaphlan2_database.loc.sample tool_data_table_conf.xml.sample
diffstat 6 files changed, 233 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data_manager/data_manager_metaphlan2_download.py	Tue Feb 21 04:17:13 2017 -0500
@@ -0,0 +1,150 @@
+#!/usr/bin/env python
+#
+# Data manager for reference data for the 'humann2' Galaxy tools
+import datetime
+import json
+import optparse
+import os
+import subprocess
+import sys
+
+
+# Utility functions for interacting with Galaxy JSON
+def read_input_json(jsonfile):
+    """Read the JSON supplied from the data manager tool
+
+    Returns a tuple (param_dict,extra_files_path)
+
+    'param_dict' is an arbitrary dictionary of parameters
+    input into the tool; 'extra_files_path' is the path
+    to a directory where output files must be put for the
+    receiving data manager to pick them up.
+
+    NB the directory pointed to by 'extra_files_path'
+    doesn't exist initially, it is the job of the script
+    to create it if necessary.
+
+    """
+    params = json.loads(open(jsonfile).read())
+    return (params['param_dict'],
+            params['output_data'][0]['extra_files_path'])
+
+
+# Utility functions for creating data table dictionaries
+#
+# Example usage:
+# >>> d = create_data_tables_dict()
+# >>> add_data_table(d,'my_data')
+# >>> add_data_table_entry(dict(dbkey='hg19',value='human'))
+# >>> add_data_table_entry(dict(dbkey='mm9',value='mouse'))
+# >>> print str(json.dumps(d))
+def create_data_tables_dict():
+    """Return a dictionary for storing data table information
+
+    Returns a dictionary that can be used with 'add_data_table'
+    and 'add_data_table_entry' to store information about a
+    data table. It can be converted to JSON to be sent back to
+    the data manager.
+
+    """
+    d = {}
+    d['data_tables'] = {}
+    return d
+
+
+def add_data_table(d, table):
+    """Add a data table to the data tables dictionary
+
+    Creates a placeholder for a data table called 'table'.
+
+    """
+    d['data_tables'][table] = []
+
+
+def add_data_table_entry(d, table, entry):
+    """Add an entry to a data table
+
+    Appends an entry to the data table 'table'. 'entry'
+    should be a dictionary where the keys are the names of
+    columns in the data table.
+
+    Raises an exception if the named data table doesn't
+    exist.
+
+    """
+    try:
+        d['data_tables'][table].append(entry)
+    except KeyError:
+        raise Exception("add_data_table_entry: no table '%s'" % table)
+
+
+def download_metaphlan2_db(data_tables, table_name, target_dir):
+    """Download MetaPhlAn2 database
+
+    Creates references to the specified file(s) on the Galaxy
+    server in the appropriate data table (determined from the
+    file extension).
+
+    The 'data_tables' dictionary should have been created using
+    the 'create_data_tables_dict' and 'add_data_table' functions.
+
+    Arguments:
+      data_tables: a dictionary containing the data table info
+      table_name: name of the table
+      target_dir: directory to put copy or link to the data file
+
+    """
+    today = datetime.date.today()
+    db_target_dir = os.path.join(target_dir, database, build)
+    os.makedirs(db_target_dir)
+    cmd = "metaphlan2_databases --output %s" % (db_target_dir)
+    subprocess.check_call(cmd, shell=True)
+    add_data_table_entry(
+        data_tables,
+        table_name,
+        dict(
+            dbkey="db_v20",
+            value=today.isoformat(),
+            name="MetaPhlAn2 clade-specific marker genes")
+
+
+if __name__ == "__main__":
+    print("Starting...")
+
+    # Read command line
+    parser = optparse.OptionParser(description='Download MetaPhlan2 database')
+    parser.add_option('--database', help="Database name")
+    options, args = parser.parse_args()
+    print("args   : %s" % args)
+    
+
+    # Check for JSON file
+    if len(args) != 1:
+        sys.stderr.write("Need to supply JSON file name")
+        sys.exit(1)
+
+    jsonfile = args[0]
+
+    # Read the input JSON
+    params, target_dir = read_input_json(jsonfile)
+
+    # Make the target directory
+    print("Making %s" % target_dir)
+    os.mkdir(target_dir)
+
+    # Set up data tables dictionary
+    data_tables = create_data_tables_dict()
+    add_data_table(data_tables, "metaphlan2_database")
+
+    # Fetch data from specified data sources
+    if options.database == "db_v20":
+        download_metaphlan2_db(
+            data_tables,
+            "metaphlan2_database",
+            target_dir)
+
+    # Write output JSON
+    print("Outputting JSON")
+    print(str(json.dumps(data_tables)))
+    open(jsonfile, 'wb').write(json.dumps(data_tables))
+    print("Done.")
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data_manager/data_manager_metaphlan2_download.xml	Tue Feb 21 04:17:13 2017 -0500
@@ -0,0 +1,40 @@
+<tool id="data_manager_metaphlan2_download" name="MetaPhlAn2 download" version="2.6.0" tool_type="manage_data">
+    <description>Download MetaPhlAn2 database</description>
+
+    <requirements>
+        <requirement type="package" version="2.6.0">metaphlan2</requirement>
+    </requirements>
+
+    <stdio>
+        <exit_code range=":-1" level="fatal" description="Error: Cannot open file" />
+        <exit_code range="1:" level="fatal" description="Error" />
+    </stdio>
+
+    <command><![CDATA[
+        python '$__tool_directory__/data_manager_metaphlan2_download.py'
+            --database $database
+            "${out_file}"
+    ]]></command>
+
+    <inputs>
+        <param argument="--taxonomic_level" type="select" label="Database to download">
+            <option value="db_v20" selected="true">MetaPhlAn2 clade-specific marker genes</option>
+          </param>
+    </inputs>
+
+    <outputs>
+           <data name="out_file" format="data_manager_json" label="${tool.name}"/>
+    </outputs>
+
+    <tests>
+    </tests>
+
+    <help><![CDATA[
+This tool downloads the MetaPhlAn2 databases.
+    ]]></help>
+
+    <citations>
+        <citation type="doi">10.1371/journal.pcbi.1003153</citation>
+        <yield />
+    </citations>
+</tool>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data_manager_conf.xml	Tue Feb 21 04:17:13 2017 -0500
@@ -0,0 +1,20 @@
+<?xml version="1.0"?>
+<data_managers>
+    <data_manager tool_file="data_manager/data_manager_metaphlan2_download.xml" id="data_manager_metaphlan2_download" >
+        <data_table name="metaphlan2_database">  <!-- Defines a Data Table to be modified. -->
+            <output> <!-- Handle the output of the Data Manager Tool -->
+                <column name="value" />  <!-- columns that are going to be specified by the Data Manager Tool -->
+                <column name="name" />  <!-- columns that are going to be specified by the Data Manager Tool -->
+                <column name="dbkey" /> <!-- columns that are going to be specified by the Data Manager Tool -->
+                <column name="path" output_ref="out_file" >
+                    <move type="directory" relativize_symlinks="True">
+                        <target base="${GALAXY_DATA_MANAGER_DATA_PATH}">metaphlan2/data/${dbkey}</target>
+                    </move>
+                    <value_translation>${GALAXY_DATA_MANAGER_DATA_PATH}/metaphlan2/data/${dbkey}</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/shed.yml	Tue Feb 21 04:17:13 2017 -0500
@@ -0,0 +1,13 @@
+name: data_manager_metaphlan2_database_downloader
+owner: bebatut
+description: "MetaPhlAn for Metagenomic Phylogenetic Analysis"
+homepage_url: "https://bitbucket.org/biobakery/metaphlan2/"
+long_description: |
+    MetaPhlAn is a computational tool for profiling the composition of microbial
+    communities (Bacteria, Archaea, Eukaryotes and Viruses) from metagenomic shotgun
+    sequencing data with species level resolution
+remote_repository_url: "https://github.com/ASaiM/galaxytools/tree/master/data_managers/data_manager_metaphlan2_database_downloader"
+type: unrestricted
+categories:
+- Metagenomics
+- Data Managers
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool-data/metaphlan2_database.loc.sample	Tue Feb 21 04:17:13 2017 -0500
@@ -0,0 +1,4 @@
+#This is a sample file distributed with Galaxy that enables tools
+#to use a directory of metagenomics files.  
+#file has this format (white space characters are TAB characters)
+#02_16_2014	 MetaPhlAn2 clade-specific marker genes	db_v20	/path/to/data
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tool_data_table_conf.xml.sample	Tue Feb 21 04:17:13 2017 -0500
@@ -0,0 +1,6 @@
+<tables>
+    <table name="metaphlan2_database" comment_char="#">
+        <columns>value, name, dbkey, path</columns>
+        <file path="tool-data/metaphlan2_database.loc" />
+    </table>
+</tables>