comparison data_manager/data_manager_metaphlan2_download.py @ 0:9c4ad82be5bd draft

planemo upload for repository https://github.com/galaxyproject/tools-iuc/tree/master/data_managers/data_manager_metaphlan2_database_downloader commit 345fb7ef485456ae833be5ad2d2ce4f8765652c8
author iuc
date Sat, 04 Mar 2017 12:11:20 -0500
parents
children 83f07f40b200
comparison
equal deleted inserted replaced
-1:000000000000 0:9c4ad82be5bd
1 #!/usr/bin/env python
2 #
3 # Data manager for reference data for the 'humann2' Galaxy tools
4 import json
5 import optparse
6 import os
7 import subprocess
8 import sys
9
10
11 # Utility functions for interacting with Galaxy JSON
12 def read_input_json(jsonfile):
13 """Read the JSON supplied from the data manager tool
14
15 Returns a tuple (param_dict,extra_files_path)
16
17 'param_dict' is an arbitrary dictionary of parameters
18 input into the tool; 'extra_files_path' is the path
19 to a directory where output files must be put for the
20 receiving data manager to pick them up.
21
22 NB the directory pointed to by 'extra_files_path'
23 doesn't exist initially, it is the job of the script
24 to create it if necessary.
25
26 """
27 params = json.loads(open(jsonfile).read())
28 return (params['param_dict'],
29 params['output_data'][0]['extra_files_path'])
30
31
32 # Utility functions for creating data table dictionaries
33 #
34 # Example usage:
35 # >>> d = create_data_tables_dict()
36 # >>> add_data_table(d,'my_data')
37 # >>> add_data_table_entry(dict(dbkey='hg19',value='human'))
38 # >>> add_data_table_entry(dict(dbkey='mm9',value='mouse'))
39 # >>> print str(json.dumps(d))
40 def create_data_tables_dict():
41 """Return a dictionary for storing data table information
42
43 Returns a dictionary that can be used with 'add_data_table'
44 and 'add_data_table_entry' to store information about a
45 data table. It can be converted to JSON to be sent back to
46 the data manager.
47
48 """
49 d = {}
50 d['data_tables'] = {}
51 return d
52
53
54 def add_data_table(d, table):
55 """Add a data table to the data tables dictionary
56
57 Creates a placeholder for a data table called 'table'.
58
59 """
60 d['data_tables'][table] = []
61
62
63 def add_data_table_entry(d, table, entry):
64 """Add an entry to a data table
65
66 Appends an entry to the data table 'table'. 'entry'
67 should be a dictionary where the keys are the names of
68 columns in the data table.
69
70 Raises an exception if the named data table doesn't
71 exist.
72
73 """
74 try:
75 d['data_tables'][table].append(entry)
76 except KeyError:
77 raise Exception("add_data_table_entry: no table '%s'" % table)
78
79
80 def download_metaphlan2_db(data_tables, build, table_name, target_dir):
81 """Download MetaPhlAn2 database
82
83 Creates references to the specified file(s) on the Galaxy
84 server in the appropriate data table (determined from the
85 file extension).
86
87 The 'data_tables' dictionary should have been created using
88 the 'create_data_tables_dict' and 'add_data_table' functions.
89
90 Arguments:
91 data_tables: a dictionary containing the data table info
92 table_name: name of the table
93 target_dir: directory to put copy or link to the data file
94
95 """
96 cmd = "download_metaphlan2_db.py --output %s" % (target_dir)
97 db_dir = os.path.join(target_dir, build)
98 subprocess.check_call(cmd, shell=True)
99 os.rename(os.path.join(target_dir, "db_v20"), db_dir)
100 add_data_table_entry(
101 data_tables,
102 table_name,
103 dict(
104 dbkey=build,
105 value="mpa_v20_m200",
106 name="MetaPhlAn2 clade-specific marker genes",
107 path=db_dir))
108
109
110 if __name__ == "__main__":
111 print("Starting...")
112
113 # Read command line
114 parser = optparse.OptionParser(description='Download MetaPhlan2 database')
115 parser.add_option('--database', help="Database name")
116 options, args = parser.parse_args()
117 print("args : %s" % args)
118
119 # Check for JSON file
120 if len(args) != 1:
121 sys.stderr.write("Need to supply JSON file name")
122 sys.exit(1)
123
124 jsonfile = args[0]
125
126 # Read the input JSON
127 params, target_dir = read_input_json(jsonfile)
128
129 # Make the target directory
130 print("Making %s" % target_dir)
131 os.mkdir(target_dir)
132
133 # Set up data tables dictionary
134 data_tables = create_data_tables_dict()
135 add_data_table(data_tables, "metaphlan2_database")
136
137 # Fetch data from specified data sources
138 if options.database == "db_v20":
139 download_metaphlan2_db(
140 data_tables,
141 "v20",
142 "metaphlan2_database",
143 target_dir)
144
145 # Write output JSON
146 print("Outputting JSON")
147 print(str(json.dumps(data_tables)))
148 with open(jsonfile, 'wb') as out:
149 out.write(json.dumps(data_tables))
150 print("Done.")