This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Load/parse cwl in python

Hello,

I have been working with cwl a lot recently, and found myself needing to parse cwl documents within python for various tasks. Is there an existing simple library or tool to load cwl into a format that can be easily manipulated within python (ideally a dictionary or dictionary-like structure), rather than writing code to parse it myself each time? I'm generally only working with one file at a time, just trying to grab a list of inputs, outputs + metadata, etc. I know there are implementations like cwltool in python, but these are large and complicated, and lacking in documentation for tasks as simple as mine.

cwl

You’ll likely need to expand on why this is a bioinformatics question else it’s liable to be closed as off topic if pure programming.

2 answers

You can use pyyaml.
Install with pip install pyyaml

Load a CWL (which can be in JSON or YAML format) using

with open(cwl_file_path, 'r') as cwl_file:  
    cwl_dict = yaml.safe_load(cwl_file)

This will return a python dict with all the fields.

There's a new Python 3.6/3.7 method of accessing CWL documents, but you get full objects (with doc strings copied from the spec), not dictionaries: https://github.com/common-workflow-language/cwl-utils

Log in to answer this question.