def parse_args_galaxy(self, *args, **kwargs): try: version = self.print_version() or '1.0' except AttributeError: # handle the potential absence of print_version version = '1.0' self.tool = gxt.Tool(self.prog, self.prog, version, self.description, self.prog, interpreter='python', version_command='python %s --version' % sys.argv[0]) self.inputs = gxtp.Inputs() self.outputs = gxtp.Outputs() self.at = agt.ArgparseGalaxyTranslation() # Only build up arguments if the user actually requests it for result in self.argument_list: # I am SO thankful they return the argument here. SO useful. argument_type = result.__class__.__name__ # http://stackoverflow.com/a/3071 if hasattr(self.at, argument_type): methodToCall = getattr(self.at, argument_type) gxt_parameter = methodToCall(result, tool=self.tool) if gxt_parameter is not None: if isinstance(gxt_parameter, gxtp.InputParameter): self.inputs.append(gxt_parameter) else: self.outputs.append(gxt_parameter) # TODO: replace with argparse-esque library to do this. stdout = gxtp.OutputData('default', 'txt') stdout.command_line_override = '> $default' self.outputs.append(stdout) self.tool.inputs = self.inputs self.tool.outputs = self.outputs if self.epilog is not None: self.tool.help = self.epilog else: self.tool.help = "TODO: Write help" data = self.tool.export() print(data) sys.exit()
def _parse_args_galaxy_argp(self, argp): try: version = self.print_version() or '1.0' except AttributeError: # handle the potential absence of print_version version = '1.0' tool = gxt.Tool(argp.prog, argp.prog.replace(" ", "_"), version, argp.description, "python " + argp.prog, interpreter=None, version_command='python %s --version' % argp.prog) inputs = gxtp.Inputs() outputs = gxtp.Outputs() at = agt.ArgparseGalaxyTranslation() # Only build up arguments if the user actually requests it for result in argp.argument_list: # I am SO thankful they return the argument here. SO useful. argument_type = result.__class__.__name__ # http://stackoverflow.com/a/3071 if hasattr(at, argument_type): methodToCall = getattr(at, argument_type) gxt_parameter = methodToCall(result, tool=tool) if gxt_parameter is not None: if isinstance(gxt_parameter, gxtp.InputParameter): inputs.append(gxt_parameter) else: outputs.append(gxt_parameter) # TODO: replace with argparse-esque library to do this. stdout = gxtp.OutputData('default', 'txt') stdout.command_line_override = '> $default' outputs.append(stdout) tool.inputs = inputs tool.outputs = outputs if argp.epilog is not None: tool.help = argp.epilog else: tool.help = "TODO: Write help" return tool.export()
def __init__(self, args=None): # noqa """ prepare command line cl for running the tool here and prepare elements needed for galaxyxml tool generation """ self.ourcwd = os.getcwd() self.collections = [] if len(args.collection) > 0: try: self.collections = [ json.loads(x) for x in args.collection if len(x.strip()) > 1 ] except Exception: print( f"--collections parameter {str(args.collection)} is malformed - should be a dictionary" ) try: self.infiles = [ json.loads(x) for x in args.input_files if len(x.strip()) > 1 ] except Exception: print( f"--input_files parameter {str(args.input_files)} is malformed - should be a dictionary" ) try: self.outfiles = [ json.loads(x) for x in args.output_files if len(x.strip()) > 1 ] except Exception: print( f"--output_files parameter {args.output_files} is malformed - should be a dictionary" ) try: self.addpar = [ json.loads(x) for x in args.additional_parameters if len(x.strip()) > 1 ] except Exception: print( f"--additional_parameters {args.additional_parameters} is malformed - should be a dictionary" ) try: self.selpar = [ json.loads(x) for x in args.selecttext_parameters if len(x.strip()) > 1 ] except Exception: print( f"--selecttext_parameters {args.selecttext_parameters} is malformed - should be a dictionary" ) self.args = args self.cleanuppar() self.lastxclredirect = None self.xmlcl = [] self.is_positional = self.args.parampass == "positional" if self.args.sysexe: if " " in self.args.sysexe: self.executeme = self.args.sysexe.split(" ") else: self.executeme = [ self.args.sysexe, ] else: if self.args.packages: self.executeme = [ self.args.packages.split(",")[0].split(":")[0].strip(), ] else: self.executeme = None aXCL = self.xmlcl.append assert args.parampass in [ "0", "argparse", "positional", ], 'args.parampass must be "0","positional" or "argparse"' self.tool_name = re.sub("[^a-zA-Z0-9_]+", "", args.tool_name) self.tool_id = self.tool_name self.newtool = gxt.Tool( self.tool_name, self.tool_id, self.args.tool_version, self.args.tool_desc, FAKEEXE, ) self.newtarpath = "%s_not_tested.toolshed.gz" % self.tool_name self.tooloutdir = "./tfout" self.repdir = "./toolgen" self.testdir = os.path.join(self.tooloutdir, "test-data") if not os.path.exists(self.tooloutdir): os.mkdir(self.tooloutdir) if not os.path.exists(self.testdir): os.mkdir(self.testdir) if not os.path.exists(self.repdir): os.mkdir(self.repdir) self.tinputs = gxtp.Inputs() self.toutputs = gxtp.Outputs() self.testparam = [] if self.args.script_path: self.prepScript() if self.args.command_override: scos = open(self.args.command_override, "r").readlines() self.command_override = [x.rstrip() for x in scos] else: self.command_override = None if self.args.test_override: stos = open(self.args.test_override, "r").readlines() self.test_override = [x.rstrip() for x in stos] else: self.test_override = None if self.args.script_path: for ex in self.executeme: aXCL(ex) aXCL("$runme") else: for ex in self.executeme: aXCL(ex) if self.args.parampass == "0": self.clsimple() else: if self.args.parampass == "positional": self.prepclpos() self.clpositional() else: self.prepargp() self.clargparse()
def __init__(self, biotool, galaxy_url=None, edam_url=None, mapping_json=None, existing_tool=None): """ Initialize a [Tool] object from galaxyxml with the minimal information (a name, an id, a version, a description, the command, the command version and a help). :param biotool: Biotool object of an entry from https://bio.tools. :type biotool: :class:`tooldog.biotool_model.Biotool` """ # Initialize GalaxyInfo self.etog = EdamToGalaxy(galaxy_url=galaxy_url, edam_url=edam_url, mapping_json=mapping_json) # Initialize counters for inputs and outputs from bio.tools self.input_ct = 0 self.output_ct = 0 self.biotool_id = biotool.tool_id if existing_tool: LOGGER.info("Loading existing XML from " + existing_tool) gxp = GalaxyXmlParser() self.tool = gxp.import_xml(existing_tool) # Add a description if missing from description if self.tool.root.find('description').text is None: self.tool.root.find( 'description' ).text = biotool.description.split('.')[0] + '.' # Add information about Tooldog version self.tool.add_comment( "This tool descriptor has been annotated by ToolDog v" + __version__) # Help if missing or TODO if self.tool.help is None: self.tool.help = biotool.generate_galaxy_help() elif "TODO" in self.tool.help: LOGGER.info( "TODO has been found in help, content has been replaced.") self.tool.help = biotool.generate_galaxy_help() else: LOGGER.info("Creating new GalaxyToolGen object...") # Initialize tool # Get the first sentence of the description only description = biotool.description.split('.')[0] + '.' self.tool = gxt.Tool(biotool.name, biotool.tool_id, biotool.version, description, "COMMAND", version_command="COMMAND --version") self.tool.help = biotool.generate_galaxy_help() # Add information about Galaxy and EDAM in the XML self.tool.add_comment( "Information was obtained from the Galaxy instance: " + self.etog.galaxy_url + " v" + self.etog.galaxy_version + " and EDAM v" + self.etog.edam_version) # Add information about Tooldog version self.tool.add_comment( "This tool descriptor has been generated by ToolDog v" + __version__)