common-workflow-language / cwl-utils

Python utilities for CWL
https://cwl-utils.readthedocs.io/
Apache License 2.0
36 stars 18 forks source link

Fix https://cwl-utils.readthedocs.io/ #57

Open nyue opened 3 years ago

nyue commented 3 years ago

I am looking at the possibility of using cwl-utils to generate CWL files programmatically in Python

I discover cwl-utils when browsing python-cwlgen

I am hoping to find example that illustrate how CommandLineTool and CommandInputParameter should be put together via cwl-utils

I have not found any examples thus far.

Cheers

illusional commented 3 years ago

Hi @nyue, this issue on cwlgen has a rough guide on how cwl-utils can be used to generate CWL: https://github.com/common-workflow-lab/python-cwlgen/issues/27

In essence, you can do something like the following:

import cwl_utils.parser_v1_2 as cwl

clt = cwl.CommandLineTool(
    # eg: keys from https://github.com/common-workflow-language/cwl-utils/blob/c7308efbbbd4d64f5d479f27bf46ca6041cfb7b2/cwl_utils/parser_v1_2.py#L7000-L7021
    id="mytoolid",
    inputs=[
        cwl. CommandInputParameter(**fields)
    ]
)

dict_representation = clt.save()
nyue commented 3 years ago

Thanks Michael. I am making some progress.

After the call to save() [I get a dictionary], what calls/method is there to write that out to a CWL file I can test with cwltool/cromwell ?

Cheers

illusional commented 3 years ago

This is a rough excerpt of the code I've used in the past:

def convert_tool_to_yaml(tool):
    # Convert dict to YAML
    import ruamel.yaml
    yaml = ruamel.yaml.YAML()

    tool_dict = tool.save()

    io = StringIO()
    yaml.dump(tool_dict, io)
    return io.getvalue()

Source: https://github.com/PMCC-BioinformaticsCore/janis-core/blob/e8d56ff04e093ffb91b6f4b7cc74b7124ad490cf/janis_core/translations/cwl.py#L93-L107

nyue commented 3 years ago

Thank you, got it writing out CWL/YAML file.

mr-c commented 3 years ago

Glad you found a solution! I'm reopening this to remind us to improve the docs

cwl-bot commented 3 years ago

This issue has been mentioned on Common Workflow Language Discourse. There might be relevant details there:

https://cwl.discourse.group/t/how-to-implement-a-reduce-method-in-cwl/294/3

golharam commented 2 years ago

This was super helpful.