import sys
from types import *
def opt_to_galaxy_param(opt, required, cmd_args, inputs, outputs):
pname = opt._long_opts[0].replace('--','')
name = pname.replace('-','_')
opt_help = ''
if opt.help != None:
opt_help = opt.help.replace('"',"'").replace('<','{').replace('>','}').replace('&','&')
# if opt.default != None and isinstance(opt.default,StringType):
if opt.default != None:
opt_help = opt_help.replace('%default',str(opt.default))
is_bool_param = opt.type == None and (opt.action == 'store_true' or opt.action == 'store_false')
if name.startswith('output_dir'):
cmd_args.append(opt._long_opts[0] + '=$__new_file_path__')
elif is_bool_param:
cmd_args.append('$' + name)
defval = 'true' if opt.default == 'True' else 'false'
tval = fval = ''
if opt.action == 'store_true':
tval = opt._long_opts[0]
else:
fval = opt._long_opts[0]
param = '' % (name, tval, fval, defval, pname, opt_help)
inputs.append(param)
elif name.startswith('output') or name.endswith('_outfile'):
cmd_args.append(opt._long_opts[0] + '=$' + name)
param = '' % pname
outputs.append(param)
else:
cmd_args.append(opt._long_opts[0] + '=$' + name)
if opt.type == 'choice':
param = '\n ' % (name,pname,opt_help)
popts = []
for choice in opt.choices:
popts.append('' % (choice,' selected="true"' if choice == opt.default else '',choice))
param += '\n '.join(popts)
param += '\n '
inputs.append(param)
else:
if name.endswith('file') or name.endswith('_fp') or name.find('fasta') >= 0 or name.find('map') >= 0 or name == 'qual':
fmt = 'txt'
if name.find('fasta') >= 0 or opt.help.find('fasta') >= 0:
fmt = 'fasta'
elif name.find('map') >= 0 or opt.help.find('map') >= 0:
fmt = 'tabular'
elif name.find('qual') >= 0:
fmt = 'qual'
param = '' % (name,fmt,pname,opt_help)
inputs.append(param)
else:
defval = ''
if opt.default != None:
try:
defval = 'value="%s"' % opt.default # problem when a tuple
except:
pass
ptype = opt.type
if opt.type == 'string':
ptype = 'text'
elif opt.type == 'int':
ptype = 'integer'
if defval == '':
defval = 'value="-1"'
elif opt.type == 'float':
if opt.default == None:
defval = 'value="0.0"'
param = '' % (name,ptype,defval,pname,opt_help)
inputs.append(param)
tool_binary = '__TOOL_BINARY__'
tool_id = tool_binary.replace('.py','')
tool_config_path = tool_binary.replace('.py','.xml')
tool_name = tool_id
tool_version = script_info['version']
tool_description = script_info['brief_description']
tool_template = """\
_tool_description
_tool_binary
qiime_wrapper.py
--galaxy_tmpdir='$__new_file_path__'
_tool_binary
_tool_args
_tool_inputs
_tool_outputs
"""
tool_args = []
tool_inputs = []
tool_outputs = []
for opt in script_info['required_options']:
opt_to_galaxy_param(opt, True, tool_args, tool_inputs, tool_outputs)
for opt in script_info['optional_options']:
opt_to_galaxy_param(opt, False, tool_args, tool_inputs, tool_outputs)
tool_config = tool_template
tool_config = tool_config.replace("_tool_id",tool_id)
tool_config = tool_config.replace("_tool_name",tool_name)
tool_config = tool_config.replace("_tool_version",tool_version)
tool_config = tool_config.replace("_tool_binary",tool_binary)
tool_config = tool_config.replace("_tool_description",tool_description)
tool_config = tool_config.replace("_tool_args",'\n '.join(tool_args))
tool_config = tool_config.replace("_tool_inputs",'\n '.join(tool_inputs))
tool_config = tool_config.replace("_tool_outputs",'\n '.join(tool_outputs))
fp = open( tool_config_path, 'wb' )
print >> fp, tool_config
fp.close()
quit()